wasm_loader.c 588 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx].table_type.flags & TABLE64_FLAG);
  56. return false;
  57. }
  58. #endif
  59. static void
  60. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  61. {
  62. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  63. }
  64. #if WASM_ENABLE_MEMORY64 != 0
  65. static void
  66. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  67. {
  68. if (error_buf != NULL) {
  69. snprintf(error_buf, error_buf_size, "offset out of range");
  70. }
  71. }
  72. #endif
  73. static void
  74. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  75. {
  76. va_list args;
  77. char buf[128];
  78. if (error_buf != NULL) {
  79. va_start(args, format);
  80. vsnprintf(buf, sizeof(buf), format, args);
  81. va_end(args);
  82. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  83. }
  84. }
  85. static bool
  86. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  87. char *error_buf, uint32 error_buf_size)
  88. {
  89. if ((uintptr_t)buf + length < (uintptr_t)buf
  90. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  91. set_error_buf(error_buf, error_buf_size,
  92. "unexpected end of section or function");
  93. return false;
  94. }
  95. return true;
  96. }
  97. static bool
  98. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  99. char *error_buf, uint32 error_buf_size)
  100. {
  101. if ((uintptr_t)buf + length < (uintptr_t)buf
  102. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  103. set_error_buf(error_buf, error_buf_size, "unexpected end");
  104. return false;
  105. }
  106. return true;
  107. }
  108. #define CHECK_BUF(buf, buf_end, length) \
  109. do { \
  110. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  111. goto fail; \
  112. } \
  113. } while (0)
  114. #define CHECK_BUF1(buf, buf_end, length) \
  115. do { \
  116. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  117. goto fail; \
  118. } \
  119. } while (0)
  120. #define skip_leb(p) while (*p++ & 0x80)
  121. #define skip_leb_int64(p, p_end) skip_leb(p)
  122. #define skip_leb_uint32(p, p_end) skip_leb(p)
  123. #define skip_leb_int32(p, p_end) skip_leb(p)
  124. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  125. #define skip_leb_memidx(p, p_end) skip_leb(p)
  126. #if WASM_ENABLE_MULTI_MEMORY == 0
  127. #define skip_leb_align(p, p_end) skip_leb(p)
  128. #else
  129. /* Skip the following memidx if applicable */
  130. #define skip_leb_align(p, p_end) \
  131. do { \
  132. if (*p++ & OPT_MEMIDX_FLAG) \
  133. skip_leb_uint32(p, p_end); \
  134. } while (0)
  135. #endif
  136. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  137. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  138. #define read_leb_int64(p, p_end, res) \
  139. do { \
  140. uint64 res64; \
  141. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  142. error_buf_size)) \
  143. goto fail; \
  144. res = (int64)res64; \
  145. } while (0)
  146. #if WASM_ENABLE_MEMORY64 != 0
  147. #define read_leb_mem_offset(p, p_end, res) \
  148. do { \
  149. uint64 res64; \
  150. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  151. &res64, error_buf, error_buf_size)) { \
  152. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  153. goto fail; \
  154. } \
  155. res = (mem_offset_t)res64; \
  156. } while (0)
  157. #else
  158. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  159. #endif
  160. #define read_leb_uint32(p, p_end, res) \
  161. do { \
  162. uint64 res64; \
  163. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  164. error_buf_size)) \
  165. goto fail; \
  166. res = (uint32)res64; \
  167. } while (0)
  168. #define read_leb_int32(p, p_end, res) \
  169. do { \
  170. uint64 res64; \
  171. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  172. error_buf_size)) \
  173. goto fail; \
  174. res = (int32)res64; \
  175. } while (0)
  176. #define read_leb_memidx(p, p_end, res) read_leb_uint32(p, p_end, res)
  177. #if WASM_ENABLE_MULTI_MEMORY != 0
  178. #define check_memidx(module, memidx) \
  179. do { \
  180. if (memidx >= module->import_memory_count + module->memory_count) { \
  181. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  182. memidx); \
  183. goto fail; \
  184. } \
  185. } while (0)
  186. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  187. * alignment check */
  188. #define read_leb_memarg(p, p_end, res) \
  189. do { \
  190. read_leb_uint32(p, p_end, res); \
  191. if (res & OPT_MEMIDX_FLAG) { \
  192. res &= ~OPT_MEMIDX_FLAG; \
  193. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  194. check_memidx(module, memidx); \
  195. } \
  196. } while (0)
  197. #else
  198. /* reserved byte 0x00 */
  199. #define check_memidx(module, memidx) \
  200. do { \
  201. (void)module; \
  202. if (memidx != 0) { \
  203. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  204. goto fail; \
  205. } \
  206. } while (0)
  207. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  208. #endif
  209. static char *
  210. type2str(uint8 type)
  211. {
  212. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  213. #if WASM_ENABLE_GC != 0
  214. char *type_str_ref[] = { "stringview_iter",
  215. "stringview_wtf16",
  216. "(ref null ht)",
  217. "(ref ht)",
  218. "", /* reserved */
  219. "stringview_wtf8",
  220. "stringref",
  221. "", /* reserved */
  222. "", /* reserved */
  223. "arrayref",
  224. "structref",
  225. "i32ref",
  226. "eqref",
  227. "anyref",
  228. "externref",
  229. "funcref",
  230. "nullref",
  231. "nullexternref",
  232. "nullfuncref" };
  233. #endif
  234. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  235. return type_str[type - VALUE_TYPE_V128];
  236. #if WASM_ENABLE_GC != 0
  237. else if (wasm_is_type_reftype(type))
  238. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  239. #endif
  240. else if (type == VALUE_TYPE_FUNCREF)
  241. return "funcref";
  242. else if (type == VALUE_TYPE_EXTERNREF)
  243. return "externref";
  244. else
  245. return "unknown type";
  246. }
  247. static bool
  248. is_32bit_type(uint8 type)
  249. {
  250. if (type == VALUE_TYPE_I32
  251. || type == VALUE_TYPE_F32
  252. /* the operand stack is in polymorphic state */
  253. || type == VALUE_TYPE_ANY
  254. #if WASM_ENABLE_GC != 0
  255. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  256. #elif WASM_ENABLE_REF_TYPES != 0
  257. /* For reference types, we use uint32 index to represent
  258. the funcref and externref */
  259. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  260. #endif
  261. )
  262. return true;
  263. return false;
  264. }
  265. static bool
  266. is_64bit_type(uint8 type)
  267. {
  268. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  269. #if WASM_ENABLE_GC != 0
  270. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  271. #endif
  272. )
  273. return true;
  274. return false;
  275. }
  276. #if WASM_ENABLE_GC != 0
  277. static bool
  278. is_packed_type(uint8 type)
  279. {
  280. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  281. }
  282. #endif
  283. static bool
  284. is_byte_a_type(uint8 type)
  285. {
  286. return (is_valid_value_type_for_interpreter(type)
  287. || (type == VALUE_TYPE_VOID))
  288. ? true
  289. : false;
  290. }
  291. #if WASM_ENABLE_SIMD != 0
  292. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  293. static V128
  294. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  295. {
  296. V128 result;
  297. uint8 i;
  298. for (i = 0; i != 16; ++i) {
  299. result.i8x16[i] = read_uint8(p_buf);
  300. }
  301. return result;
  302. }
  303. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  304. #endif /* end of WASM_ENABLE_SIMD */
  305. static void *
  306. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  307. {
  308. void *mem;
  309. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  310. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  311. return NULL;
  312. }
  313. memset(mem, 0, (uint32)size);
  314. return mem;
  315. }
  316. static void *
  317. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  318. uint32 error_buf_size)
  319. {
  320. uint8 *mem_new;
  321. bh_assert(size_new > size_old);
  322. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  323. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  324. memset(mem_new + size_old, 0, size_new - size_old);
  325. wasm_runtime_free(mem_old);
  326. }
  327. return mem_new;
  328. }
  329. #define MEM_REALLOC(mem, size_old, size_new) \
  330. do { \
  331. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  332. error_buf_size); \
  333. if (!mem_new) \
  334. goto fail; \
  335. mem = mem_new; \
  336. } while (0)
  337. #if WASM_ENABLE_GC != 0
  338. static bool
  339. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  340. char *error_buf, uint32 error_buf_size)
  341. {
  342. if (type_index >= type_count) {
  343. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  344. type_index);
  345. return false;
  346. }
  347. return true;
  348. }
  349. static bool
  350. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  351. uint32 error_buf_size)
  352. {
  353. if (!check_type_index(module, module->type_count, type_index, error_buf,
  354. error_buf_size)) {
  355. return false;
  356. }
  357. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  358. set_error_buf(error_buf, error_buf_size, "unkown array type");
  359. return false;
  360. }
  361. return true;
  362. }
  363. #endif
  364. static bool
  365. check_function_index(const WASMModule *module, uint32 function_index,
  366. char *error_buf, uint32 error_buf_size)
  367. {
  368. if (function_index
  369. >= module->import_function_count + module->function_count) {
  370. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  371. function_index);
  372. return false;
  373. }
  374. return true;
  375. }
  376. typedef struct InitValue {
  377. uint8 type;
  378. uint8 flag;
  379. #if WASM_ENABLE_GC != 0
  380. uint8 gc_opcode;
  381. WASMRefType ref_type;
  382. #endif
  383. WASMValue value;
  384. } InitValue;
  385. typedef struct ConstExprContext {
  386. uint32 sp;
  387. uint32 size;
  388. WASMModule *module;
  389. InitValue *stack;
  390. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  391. } ConstExprContext;
  392. static void
  393. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  394. {
  395. ctx->sp = 0;
  396. ctx->module = module;
  397. ctx->stack = ctx->data;
  398. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  399. }
  400. static bool
  401. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  402. #if WASM_ENABLE_GC != 0
  403. WASMRefType *ref_type, uint8 gc_opcode,
  404. #endif
  405. WASMValue *value, char *error_buf, uint32 error_buf_size)
  406. {
  407. InitValue *cur_value;
  408. if (ctx->sp >= ctx->size) {
  409. if (ctx->stack != ctx->data) {
  410. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  411. (ctx->size + 4) * sizeof(InitValue));
  412. }
  413. else {
  414. if (!(ctx->stack =
  415. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  416. error_buf, error_buf_size))) {
  417. goto fail;
  418. }
  419. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  420. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  421. }
  422. ctx->size += 4;
  423. }
  424. cur_value = &ctx->stack[ctx->sp++];
  425. cur_value->type = type;
  426. cur_value->flag = flag;
  427. cur_value->value = *value;
  428. #if WASM_ENABLE_GC != 0
  429. cur_value->gc_opcode = gc_opcode;
  430. if (wasm_is_type_multi_byte_type(type)) {
  431. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  432. ref_type, wasm_reftype_struct_size(ref_type));
  433. }
  434. #endif
  435. return true;
  436. fail:
  437. return false;
  438. }
  439. #if WASM_ENABLE_GC != 0
  440. static void
  441. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  442. {
  443. WASMStructNewInitValues *struct_init_values =
  444. (WASMStructNewInitValues *)data;
  445. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  446. WASMType *wasm_type;
  447. uint32 i;
  448. if (!data)
  449. return;
  450. wasm_type = module->types[struct_init_values->type_idx];
  451. /* The data can only be type of `WASMStructNewInitValues *`
  452. or `WASMArrayNewInitValues *` */
  453. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  454. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  455. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  456. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  457. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  458. WASMRefType *ref_type;
  459. uint8 field_type;
  460. for (i = 0; i < struct_init_values->count; i++) {
  461. field_type = struct_type->fields[i].field_type;
  462. if (wasm_is_type_multi_byte_type(field_type))
  463. ref_type = ref_type_map->ref_type;
  464. else
  465. ref_type = NULL;
  466. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  467. REF_TYPE_STRUCTREF, NULL,
  468. module->types, module->type_count)
  469. || wasm_reftype_is_subtype_of(
  470. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  471. module->types, module->type_count)) {
  472. destroy_init_expr_data_recursive(
  473. module, struct_init_values->fields[i].data);
  474. }
  475. }
  476. }
  477. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  478. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  479. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  480. uint8 elem_type = array_type->elem_type;
  481. for (i = 0; i < array_init_values->length; i++) {
  482. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  483. REF_TYPE_STRUCTREF, NULL,
  484. module->types, module->type_count)
  485. || wasm_reftype_is_subtype_of(
  486. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  487. module->types, module->type_count)) {
  488. destroy_init_expr_data_recursive(
  489. module, array_init_values->elem_data[i].data);
  490. }
  491. }
  492. }
  493. wasm_runtime_free(data);
  494. }
  495. #endif
  496. static bool
  497. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  498. #if WASM_ENABLE_GC != 0
  499. WASMRefType *ref_type, uint8 *p_gc_opcode,
  500. #endif
  501. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  502. {
  503. InitValue *cur_value;
  504. if (ctx->sp == 0) {
  505. set_error_buf(error_buf, error_buf_size,
  506. "type mismatch: const expr stack underflow");
  507. return false;
  508. }
  509. cur_value = &ctx->stack[--ctx->sp];
  510. #if WASM_ENABLE_GC == 0
  511. if (cur_value->type != type) {
  512. set_error_buf(error_buf, error_buf_size, "type mismatch");
  513. return false;
  514. }
  515. #else
  516. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  517. ref_type, ctx->module->types,
  518. ctx->module->type_count)) {
  519. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  520. "type mismatch: expect ", type2str(type),
  521. " but got other");
  522. goto fail;
  523. }
  524. #endif
  525. if (p_flag)
  526. *p_flag = cur_value->flag;
  527. if (p_value)
  528. *p_value = cur_value->value;
  529. #if WASM_ENABLE_GC != 0
  530. if (p_gc_opcode)
  531. *p_gc_opcode = cur_value->gc_opcode;
  532. #endif
  533. return true;
  534. #if WASM_ENABLE_GC != 0
  535. fail:
  536. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  537. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  538. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  539. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  540. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  541. }
  542. return false;
  543. #endif
  544. }
  545. static void
  546. destroy_const_expr_stack(ConstExprContext *ctx)
  547. {
  548. #if WASM_ENABLE_GC != 0
  549. uint32 i;
  550. for (i = 0; i < ctx->sp; i++) {
  551. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  552. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  553. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  554. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  555. destroy_init_expr_data_recursive(ctx->module,
  556. ctx->stack[i].value.data);
  557. }
  558. }
  559. #endif
  560. if (ctx->stack != ctx->data) {
  561. wasm_runtime_free(ctx->stack);
  562. }
  563. }
  564. #if WASM_ENABLE_GC != 0
  565. static void
  566. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  567. {
  568. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  569. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  570. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  571. destroy_init_expr_data_recursive(module, expr->u.data);
  572. }
  573. }
  574. #endif /* end of WASM_ENABLE_GC != 0 */
  575. static bool
  576. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  577. InitializerExpression *init_expr, uint8 type, void *ref_type,
  578. char *error_buf, uint32 error_buf_size)
  579. {
  580. const uint8 *p = *p_buf, *p_end = buf_end;
  581. uint8 flag, *p_float;
  582. uint32 i;
  583. ConstExprContext const_expr_ctx = { 0 };
  584. WASMValue cur_value;
  585. #if WASM_ENABLE_GC != 0
  586. uint32 opcode1, type_idx;
  587. uint8 opcode;
  588. WASMRefType cur_ref_type = { 0 };
  589. #endif
  590. init_const_expr_stack(&const_expr_ctx, module);
  591. CHECK_BUF(p, p_end, 1);
  592. flag = read_uint8(p);
  593. while (flag != WASM_OP_END) {
  594. switch (flag) {
  595. /* i32.const */
  596. case INIT_EXPR_TYPE_I32_CONST:
  597. read_leb_int32(p, p_end, cur_value.i32);
  598. if (!push_const_expr_stack(
  599. &const_expr_ctx, flag, VALUE_TYPE_I32,
  600. #if WASM_ENABLE_GC != 0
  601. NULL, 0,
  602. #endif
  603. &cur_value, error_buf, error_buf_size))
  604. goto fail;
  605. break;
  606. /* i64.const */
  607. case INIT_EXPR_TYPE_I64_CONST:
  608. read_leb_int64(p, p_end, cur_value.i64);
  609. if (!push_const_expr_stack(
  610. &const_expr_ctx, flag, VALUE_TYPE_I64,
  611. #if WASM_ENABLE_GC != 0
  612. NULL, 0,
  613. #endif
  614. &cur_value, error_buf, error_buf_size))
  615. goto fail;
  616. break;
  617. /* f32.const */
  618. case INIT_EXPR_TYPE_F32_CONST:
  619. CHECK_BUF(p, p_end, 4);
  620. p_float = (uint8 *)&cur_value.f32;
  621. for (i = 0; i < sizeof(float32); i++)
  622. *p_float++ = *p++;
  623. if (!push_const_expr_stack(
  624. &const_expr_ctx, flag, VALUE_TYPE_F32,
  625. #if WASM_ENABLE_GC != 0
  626. NULL, 0,
  627. #endif
  628. &cur_value, error_buf, error_buf_size))
  629. goto fail;
  630. break;
  631. /* f64.const */
  632. case INIT_EXPR_TYPE_F64_CONST:
  633. CHECK_BUF(p, p_end, 8);
  634. p_float = (uint8 *)&cur_value.f64;
  635. for (i = 0; i < sizeof(float64); i++)
  636. *p_float++ = *p++;
  637. if (!push_const_expr_stack(
  638. &const_expr_ctx, flag, VALUE_TYPE_F64,
  639. #if WASM_ENABLE_GC != 0
  640. NULL, 0,
  641. #endif
  642. &cur_value, error_buf, error_buf_size))
  643. goto fail;
  644. break;
  645. #if WASM_ENABLE_SIMD != 0
  646. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  647. /* v128.const */
  648. case INIT_EXPR_TYPE_V128_CONST:
  649. {
  650. uint64 high, low;
  651. CHECK_BUF(p, p_end, 1);
  652. (void)read_uint8(p);
  653. CHECK_BUF(p, p_end, 16);
  654. wasm_runtime_read_v128(p, &high, &low);
  655. p += 16;
  656. cur_value.v128.i64x2[0] = high;
  657. cur_value.v128.i64x2[1] = low;
  658. if (!push_const_expr_stack(
  659. &const_expr_ctx, flag, VALUE_TYPE_V128,
  660. #if WASM_ENABLE_GC != 0
  661. NULL, 0,
  662. #endif
  663. &cur_value, error_buf, error_buf_size))
  664. goto fail;
  665. #if WASM_ENABLE_WAMR_COMPILER != 0
  666. /* If any init_expr is v128.const, mark SIMD used */
  667. module->is_simd_used = true;
  668. #endif
  669. break;
  670. }
  671. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  672. #endif /* end of WASM_ENABLE_SIMD */
  673. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  674. /* ref.func */
  675. case INIT_EXPR_TYPE_FUNCREF_CONST:
  676. {
  677. uint32 func_idx;
  678. read_leb_uint32(p, p_end, func_idx);
  679. cur_value.ref_index = func_idx;
  680. if (!check_function_index(module, func_idx, error_buf,
  681. error_buf_size)) {
  682. goto fail;
  683. }
  684. #if WASM_ENABLE_GC == 0
  685. if (!push_const_expr_stack(&const_expr_ctx, flag,
  686. VALUE_TYPE_FUNCREF, &cur_value,
  687. error_buf, error_buf_size))
  688. goto fail;
  689. #else
  690. if (func_idx < module->import_function_count) {
  691. type_idx =
  692. module->import_functions[func_idx].u.function.type_idx;
  693. }
  694. else {
  695. type_idx = module
  696. ->functions[func_idx
  697. - module->import_function_count]
  698. ->type_idx;
  699. }
  700. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  701. false, type_idx);
  702. if (!push_const_expr_stack(&const_expr_ctx, flag,
  703. cur_ref_type.ref_type, &cur_ref_type,
  704. 0, &cur_value, error_buf,
  705. error_buf_size))
  706. goto fail;
  707. #endif
  708. #if WASM_ENABLE_WAMR_COMPILER != 0
  709. module->is_ref_types_used = true;
  710. #endif
  711. break;
  712. }
  713. /* ref.null */
  714. case INIT_EXPR_TYPE_REFNULL_CONST:
  715. {
  716. uint8 type1;
  717. CHECK_BUF(p, p_end, 1);
  718. type1 = read_uint8(p);
  719. #if WASM_ENABLE_GC == 0
  720. cur_value.ref_index = NULL_REF;
  721. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  722. &cur_value, error_buf,
  723. error_buf_size))
  724. goto fail;
  725. #else
  726. cur_value.gc_obj = NULL_REF;
  727. if (!is_byte_a_type(type1)) {
  728. p--;
  729. read_leb_uint32(p, p_end, type_idx);
  730. if (!check_type_index(module, module->type_count, type_idx,
  731. error_buf, error_buf_size))
  732. goto fail;
  733. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  734. true, type_idx);
  735. if (!push_const_expr_stack(&const_expr_ctx, flag,
  736. cur_ref_type.ref_type,
  737. &cur_ref_type, 0, &cur_value,
  738. error_buf, error_buf_size))
  739. goto fail;
  740. }
  741. else {
  742. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  743. NULL, 0, &cur_value, error_buf,
  744. error_buf_size))
  745. goto fail;
  746. }
  747. #endif
  748. #if WASM_ENABLE_WAMR_COMPILER != 0
  749. module->is_ref_types_used = true;
  750. #endif
  751. break;
  752. }
  753. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  754. /* get_global */
  755. case INIT_EXPR_TYPE_GET_GLOBAL:
  756. {
  757. uint32 global_idx;
  758. uint8 global_type;
  759. read_leb_uint32(p, p_end, cur_value.global_index);
  760. global_idx = cur_value.global_index;
  761. /*
  762. * Currently, constant expressions occurring as initializers
  763. * of globals are further constrained in that contained
  764. * global.get instructions are
  765. * only allowed to refer to imported globals.
  766. *
  767. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  768. */
  769. if (global_idx >= module->import_global_count
  770. /* make spec test happy */
  771. #if WASM_ENABLE_GC != 0
  772. + module->global_count
  773. #endif
  774. ) {
  775. set_error_buf_v(error_buf, error_buf_size,
  776. "unknown global %u", global_idx);
  777. goto fail;
  778. }
  779. if (
  780. /* make spec test happy */
  781. #if WASM_ENABLE_GC != 0
  782. global_idx < module->import_global_count &&
  783. #endif
  784. module->import_globals[global_idx]
  785. .u.global.type.is_mutable) {
  786. set_error_buf_v(error_buf, error_buf_size,
  787. "constant expression required");
  788. goto fail;
  789. }
  790. if (global_idx < module->import_global_count) {
  791. global_type = module->import_globals[global_idx]
  792. .u.global.type.val_type;
  793. #if WASM_ENABLE_GC != 0
  794. if (wasm_is_type_multi_byte_type(global_type)) {
  795. WASMRefType *global_ref_type =
  796. module->import_globals[global_idx]
  797. .u.global.ref_type;
  798. bh_memcpy_s(&cur_ref_type,
  799. wasm_reftype_struct_size(global_ref_type),
  800. global_ref_type,
  801. wasm_reftype_struct_size(global_ref_type));
  802. }
  803. #endif
  804. }
  805. else {
  806. global_type =
  807. module
  808. ->globals[global_idx - module->import_global_count]
  809. .type.val_type;
  810. #if WASM_ENABLE_GC != 0
  811. if (wasm_is_type_multi_byte_type(global_type)) {
  812. WASMRefType *global_ref_type =
  813. module
  814. ->globals[global_idx
  815. - module->import_global_count]
  816. .ref_type;
  817. bh_memcpy_s(&cur_ref_type,
  818. wasm_reftype_struct_size(global_ref_type),
  819. global_ref_type,
  820. wasm_reftype_struct_size(global_ref_type));
  821. }
  822. #endif
  823. }
  824. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  825. #if WASM_ENABLE_GC != 0
  826. &cur_ref_type, 0,
  827. #endif
  828. &cur_value, error_buf,
  829. error_buf_size))
  830. goto fail;
  831. break;
  832. }
  833. #if WASM_ENABLE_GC != 0
  834. /* struct.new and array.new */
  835. case WASM_OP_GC_PREFIX:
  836. {
  837. read_leb_uint32(p, p_end, opcode1);
  838. switch (opcode1) {
  839. case WASM_OP_STRUCT_NEW:
  840. {
  841. WASMStructType *struct_type;
  842. WASMStructNewInitValues *struct_init_values = NULL;
  843. uint32 field_count;
  844. read_leb_uint32(p, p_end, type_idx);
  845. if (!check_type_index(module, module->type_count,
  846. type_idx, error_buf,
  847. error_buf_size)) {
  848. goto fail;
  849. }
  850. struct_type = (WASMStructType *)module->types[type_idx];
  851. if (struct_type->base_type.type_flag
  852. != WASM_TYPE_STRUCT) {
  853. set_error_buf(error_buf, error_buf_size,
  854. "unkown struct type");
  855. goto fail;
  856. }
  857. field_count = struct_type->field_count;
  858. if (!(struct_init_values = loader_malloc(
  859. offsetof(WASMStructNewInitValues, fields)
  860. + (uint64)field_count * sizeof(WASMValue),
  861. error_buf, error_buf_size))) {
  862. goto fail;
  863. }
  864. struct_init_values->type_idx = type_idx;
  865. struct_init_values->count = field_count;
  866. for (i = field_count; i > 0; i--) {
  867. WASMRefType *field_ref_type = NULL;
  868. uint32 field_idx = i - 1;
  869. uint8 field_type =
  870. struct_type->fields[field_idx].field_type;
  871. if (wasm_is_type_multi_byte_type(field_type)) {
  872. field_ref_type = wasm_reftype_map_find(
  873. struct_type->ref_type_maps,
  874. struct_type->ref_type_map_count, field_idx);
  875. }
  876. if (is_packed_type(field_type)) {
  877. field_type = VALUE_TYPE_I32;
  878. }
  879. if (!pop_const_expr_stack(
  880. &const_expr_ctx, NULL, field_type,
  881. field_ref_type, NULL,
  882. &struct_init_values->fields[field_idx],
  883. error_buf, error_buf_size)) {
  884. destroy_init_expr_data_recursive(
  885. module, struct_init_values);
  886. goto fail;
  887. }
  888. }
  889. cur_value.data = struct_init_values;
  890. wasm_set_refheaptype_typeidx(
  891. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  892. if (!push_const_expr_stack(
  893. &const_expr_ctx, flag, cur_ref_type.ref_type,
  894. &cur_ref_type, (uint8)opcode1, &cur_value,
  895. error_buf, error_buf_size)) {
  896. destroy_init_expr_data_recursive(
  897. module, struct_init_values);
  898. goto fail;
  899. }
  900. break;
  901. }
  902. case WASM_OP_STRUCT_NEW_DEFAULT:
  903. {
  904. read_leb_uint32(p, p_end, cur_value.type_index);
  905. type_idx = cur_value.type_index;
  906. if (!check_type_index(module, module->type_count,
  907. type_idx, error_buf,
  908. error_buf_size)) {
  909. goto fail;
  910. }
  911. if (module->types[type_idx]->type_flag
  912. != WASM_TYPE_STRUCT) {
  913. set_error_buf(error_buf, error_buf_size,
  914. "unkown struct type");
  915. goto fail;
  916. }
  917. cur_value.type_index = type_idx;
  918. cur_value.data = NULL;
  919. wasm_set_refheaptype_typeidx(
  920. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  921. if (!push_const_expr_stack(
  922. &const_expr_ctx, flag, cur_ref_type.ref_type,
  923. &cur_ref_type, (uint8)opcode1, &cur_value,
  924. error_buf, error_buf_size)) {
  925. goto fail;
  926. }
  927. break;
  928. }
  929. case WASM_OP_ARRAY_NEW:
  930. case WASM_OP_ARRAY_NEW_DEFAULT:
  931. case WASM_OP_ARRAY_NEW_FIXED:
  932. {
  933. WASMArrayNewInitValues *array_init_values = NULL;
  934. WASMArrayType *array_type = NULL;
  935. WASMRefType *elem_ref_type = NULL;
  936. uint64 total_size;
  937. uint8 elem_type;
  938. read_leb_uint32(p, p_end, cur_value.type_index);
  939. type_idx = cur_value.type_index;
  940. if (!check_type_index(module, module->type_count,
  941. type_idx, error_buf,
  942. error_buf_size)) {
  943. goto fail;
  944. }
  945. array_type = (WASMArrayType *)module->types[type_idx];
  946. if (array_type->base_type.type_flag
  947. != WASM_TYPE_ARRAY) {
  948. set_error_buf(error_buf, error_buf_size,
  949. "unkown array type");
  950. goto fail;
  951. }
  952. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  953. elem_type = array_type->elem_type;
  954. if (wasm_is_type_multi_byte_type(elem_type)) {
  955. elem_ref_type = array_type->elem_ref_type;
  956. }
  957. if (is_packed_type(elem_type)) {
  958. elem_type = VALUE_TYPE_I32;
  959. }
  960. if (opcode1 == WASM_OP_ARRAY_NEW) {
  961. WASMValue len_val;
  962. if (!(array_init_values = loader_malloc(
  963. sizeof(WASMArrayNewInitValues),
  964. error_buf, error_buf_size))) {
  965. goto fail;
  966. }
  967. array_init_values->type_idx = type_idx;
  968. if (!pop_const_expr_stack(
  969. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  970. NULL, NULL, &len_val, error_buf,
  971. error_buf_size)) {
  972. destroy_init_expr_data_recursive(
  973. module, array_init_values);
  974. goto fail;
  975. }
  976. array_init_values->length = len_val.i32;
  977. if (!pop_const_expr_stack(
  978. &const_expr_ctx, NULL, elem_type,
  979. elem_ref_type, NULL,
  980. &array_init_values->elem_data[0],
  981. error_buf, error_buf_size)) {
  982. destroy_init_expr_data_recursive(
  983. module, array_init_values);
  984. goto fail;
  985. }
  986. cur_value.data = array_init_values;
  987. }
  988. else {
  989. /* WASM_OP_ARRAY_NEW_FIXED */
  990. uint32 len;
  991. read_leb_uint32(p, p_end, len);
  992. total_size =
  993. (uint64)offsetof(WASMArrayNewInitValues,
  994. elem_data)
  995. + (uint64)sizeof(WASMValue) * len;
  996. if (!(array_init_values =
  997. loader_malloc(total_size, error_buf,
  998. error_buf_size))) {
  999. goto fail;
  1000. }
  1001. array_init_values->type_idx = type_idx;
  1002. array_init_values->length = len;
  1003. for (i = len; i > 0; i--) {
  1004. if (!pop_const_expr_stack(
  1005. &const_expr_ctx, NULL, elem_type,
  1006. elem_ref_type, NULL,
  1007. &array_init_values
  1008. ->elem_data[i - 1],
  1009. error_buf, error_buf_size)) {
  1010. destroy_init_expr_data_recursive(
  1011. module, array_init_values);
  1012. goto fail;
  1013. }
  1014. }
  1015. cur_value.data = array_init_values;
  1016. }
  1017. }
  1018. else {
  1019. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1020. WASMValue len_val;
  1021. uint32 len;
  1022. /* POP(i32) */
  1023. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1024. VALUE_TYPE_I32, NULL,
  1025. NULL, &len_val, error_buf,
  1026. error_buf_size)) {
  1027. goto fail;
  1028. }
  1029. len = len_val.i32;
  1030. cur_value.array_new_default.type_index = type_idx;
  1031. cur_value.array_new_default.length = len;
  1032. }
  1033. wasm_set_refheaptype_typeidx(
  1034. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1035. if (!push_const_expr_stack(
  1036. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1037. &cur_ref_type, (uint8)opcode1, &cur_value,
  1038. error_buf, error_buf_size)) {
  1039. if (array_init_values) {
  1040. destroy_init_expr_data_recursive(
  1041. module, array_init_values);
  1042. }
  1043. goto fail;
  1044. }
  1045. break;
  1046. }
  1047. case WASM_OP_ANY_CONVERT_EXTERN:
  1048. {
  1049. set_error_buf(error_buf, error_buf_size,
  1050. "unsupported constant expression of "
  1051. "extern.internalize");
  1052. goto fail;
  1053. }
  1054. case WASM_OP_EXTERN_CONVERT_ANY:
  1055. {
  1056. set_error_buf(error_buf, error_buf_size,
  1057. "unsupported constant expression of "
  1058. "extern.externalize");
  1059. goto fail;
  1060. }
  1061. case WASM_OP_REF_I31:
  1062. {
  1063. /* POP(i32) */
  1064. if (!pop_const_expr_stack(
  1065. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1066. NULL, &cur_value, error_buf, error_buf_size)) {
  1067. goto fail;
  1068. }
  1069. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1070. false, HEAP_TYPE_I31);
  1071. if (!push_const_expr_stack(
  1072. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1073. &cur_ref_type, (uint8)opcode1, &cur_value,
  1074. error_buf, error_buf_size)) {
  1075. goto fail;
  1076. }
  1077. break;
  1078. }
  1079. default:
  1080. set_error_buf(
  1081. error_buf, error_buf_size,
  1082. "type mismatch or constant expression required");
  1083. goto fail;
  1084. }
  1085. break;
  1086. }
  1087. #endif /* end of WASM_ENABLE_GC != 0 */
  1088. default:
  1089. {
  1090. set_error_buf(error_buf, error_buf_size,
  1091. "illegal opcode "
  1092. "or constant expression required "
  1093. "or type mismatch");
  1094. goto fail;
  1095. }
  1096. }
  1097. CHECK_BUF(p, p_end, 1);
  1098. flag = read_uint8(p);
  1099. }
  1100. /* There should be only one value left on the init value stack */
  1101. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1102. #if WASM_ENABLE_GC != 0
  1103. ref_type, &opcode,
  1104. #endif
  1105. &cur_value, error_buf, error_buf_size)) {
  1106. goto fail;
  1107. }
  1108. if (const_expr_ctx.sp != 0) {
  1109. set_error_buf(error_buf, error_buf_size,
  1110. "type mismatch: illegal constant opcode sequence");
  1111. goto fail;
  1112. }
  1113. init_expr->init_expr_type = flag;
  1114. init_expr->u = cur_value;
  1115. #if WASM_ENABLE_GC != 0
  1116. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1117. switch (opcode) {
  1118. case WASM_OP_STRUCT_NEW:
  1119. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1120. break;
  1121. case WASM_OP_STRUCT_NEW_DEFAULT:
  1122. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1123. break;
  1124. case WASM_OP_ARRAY_NEW:
  1125. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1126. break;
  1127. case WASM_OP_ARRAY_NEW_DEFAULT:
  1128. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1129. break;
  1130. case WASM_OP_ARRAY_NEW_FIXED:
  1131. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1132. break;
  1133. case WASM_OP_REF_I31:
  1134. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1135. break;
  1136. default:
  1137. bh_assert(0);
  1138. break;
  1139. }
  1140. }
  1141. #endif /* end of WASM_ENABLE_GC != 0 */
  1142. *p_buf = p;
  1143. destroy_const_expr_stack(&const_expr_ctx);
  1144. return true;
  1145. fail:
  1146. destroy_const_expr_stack(&const_expr_ctx);
  1147. return false;
  1148. }
  1149. static bool
  1150. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1151. {
  1152. if (mutable >= 2) {
  1153. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1154. return false;
  1155. }
  1156. return true;
  1157. }
  1158. #if WASM_ENABLE_GC != 0
  1159. static void
  1160. destroy_func_type(WASMFuncType *type)
  1161. {
  1162. /* Destroy the reference type hash set */
  1163. if (type->ref_type_maps)
  1164. wasm_runtime_free(type->ref_type_maps);
  1165. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1166. && WASM_ENABLE_LAZY_JIT != 0
  1167. if (type->call_to_llvm_jit_from_fast_jit)
  1168. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1169. #endif
  1170. /* Free the type */
  1171. wasm_runtime_free(type);
  1172. }
  1173. static void
  1174. destroy_struct_type(WASMStructType *type)
  1175. {
  1176. if (type->ref_type_maps)
  1177. wasm_runtime_free(type->ref_type_maps);
  1178. wasm_runtime_free(type);
  1179. }
  1180. static void
  1181. destroy_array_type(WASMArrayType *type)
  1182. {
  1183. wasm_runtime_free(type);
  1184. }
  1185. static void
  1186. destroy_wasm_type(WASMType *type)
  1187. {
  1188. if (type->ref_count > 1) {
  1189. /* The type is referenced by other types
  1190. of current wasm module */
  1191. type->ref_count--;
  1192. return;
  1193. }
  1194. if (type->type_flag == WASM_TYPE_FUNC)
  1195. destroy_func_type((WASMFuncType *)type);
  1196. else if (type->type_flag == WASM_TYPE_STRUCT)
  1197. destroy_struct_type((WASMStructType *)type);
  1198. else if (type->type_flag == WASM_TYPE_ARRAY)
  1199. destroy_array_type((WASMArrayType *)type);
  1200. else {
  1201. bh_assert(0);
  1202. }
  1203. }
  1204. /* Resolve (ref null ht) or (ref ht) */
  1205. static bool
  1206. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1207. WASMModule *module, uint32 type_count, bool nullable,
  1208. WASMRefType *ref_type, char *error_buf,
  1209. uint32 error_buf_size)
  1210. {
  1211. const uint8 *p = *p_buf, *p_end = buf_end;
  1212. ref_type->ref_type =
  1213. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1214. ref_type->ref_ht_common.nullable = nullable;
  1215. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1216. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1217. /* heap type is (type i), i : typeidx, >= 0 */
  1218. if (!check_type_index(module, type_count,
  1219. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1220. error_buf_size)) {
  1221. return false;
  1222. }
  1223. }
  1224. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1225. /* heap type is func, extern, any, eq, i31 or data */
  1226. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1227. return false;
  1228. }
  1229. *p_buf = p;
  1230. return true;
  1231. fail:
  1232. return false;
  1233. }
  1234. static bool
  1235. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1236. WASMModule *module, uint32 type_count,
  1237. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1238. bool allow_packed_type, char *error_buf,
  1239. uint32 error_buf_size)
  1240. {
  1241. const uint8 *p = *p_buf, *p_end = buf_end;
  1242. uint8 type;
  1243. memset(ref_type, 0, sizeof(WASMRefType));
  1244. CHECK_BUF(p, p_end, 1);
  1245. type = read_uint8(p);
  1246. if (wasm_is_reftype_htref_nullable(type)) {
  1247. /* (ref null ht) */
  1248. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1249. ref_type, error_buf, error_buf_size))
  1250. return false;
  1251. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1252. *p_need_ref_type_map = true;
  1253. else {
  1254. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1255. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1256. multi-byte type to one-byte type to reduce the footprint and
  1257. the complexity of type equal/subtype checking */
  1258. ref_type->ref_type =
  1259. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1260. *p_need_ref_type_map = false;
  1261. }
  1262. }
  1263. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1264. /* (ref ht) */
  1265. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1266. ref_type, error_buf, error_buf_size))
  1267. return false;
  1268. *p_need_ref_type_map = true;
  1269. #if WASM_ENABLE_STRINGREF != 0
  1270. /* covert (ref string) to stringref */
  1271. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1272. ref_type->ref_type =
  1273. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1274. *p_need_ref_type_map = false;
  1275. }
  1276. #endif
  1277. }
  1278. else {
  1279. /* type which can be represented by one byte */
  1280. if (!is_valid_value_type_for_interpreter(type)
  1281. && !(allow_packed_type && is_packed_type(type))) {
  1282. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1283. return false;
  1284. }
  1285. ref_type->ref_type = type;
  1286. *p_need_ref_type_map = false;
  1287. #if WASM_ENABLE_WAMR_COMPILER != 0
  1288. /* If any value's type is v128, mark the module as SIMD used */
  1289. if (type == VALUE_TYPE_V128)
  1290. module->is_simd_used = true;
  1291. #endif
  1292. }
  1293. *p_buf = p;
  1294. return true;
  1295. fail:
  1296. return false;
  1297. }
  1298. static WASMRefType *
  1299. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1300. char *error_buf, uint32 error_buf_size)
  1301. {
  1302. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1303. if (!ret) {
  1304. set_error_buf(error_buf, error_buf_size,
  1305. "insert ref type to hash set failed");
  1306. }
  1307. return ret;
  1308. }
  1309. static bool
  1310. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1311. uint32 type_count, uint32 type_idx, char *error_buf,
  1312. uint32 error_buf_size)
  1313. {
  1314. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1315. uint32 param_count, result_count, i, j = 0;
  1316. uint32 param_cell_num, ret_cell_num;
  1317. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1318. uint64 total_size;
  1319. bool need_ref_type_map;
  1320. WASMRefType ref_type;
  1321. WASMFuncType *type = NULL;
  1322. /* Parse first time to resolve param count, result count and
  1323. ref type map count */
  1324. read_leb_uint32(p, p_end, param_count);
  1325. p_org = p;
  1326. for (i = 0; i < param_count; i++) {
  1327. if (!resolve_value_type(&p, p_end, module, type_count,
  1328. &need_ref_type_map, &ref_type, false, error_buf,
  1329. error_buf_size)) {
  1330. return false;
  1331. }
  1332. if (need_ref_type_map)
  1333. ref_type_map_count++;
  1334. }
  1335. read_leb_uint32(p, p_end, result_count);
  1336. for (i = 0; i < result_count; i++) {
  1337. if (!resolve_value_type(&p, p_end, module, type_count,
  1338. &need_ref_type_map, &ref_type, false, error_buf,
  1339. error_buf_size)) {
  1340. return false;
  1341. }
  1342. if (need_ref_type_map) {
  1343. ref_type_map_count++;
  1344. result_ref_type_map_count++;
  1345. }
  1346. }
  1347. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1348. "ref type map count: %d",
  1349. type_idx, param_count, result_count, ref_type_map_count);
  1350. /* Parse second time to resolve param types, result types and
  1351. ref type map info */
  1352. p = p_org;
  1353. total_size = offsetof(WASMFuncType, types)
  1354. + sizeof(uint8) * (uint64)(param_count + result_count);
  1355. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1356. return false;
  1357. }
  1358. if (ref_type_map_count > 0) {
  1359. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1360. if (!(type->ref_type_maps =
  1361. loader_malloc(total_size, error_buf, error_buf_size))) {
  1362. goto fail;
  1363. }
  1364. }
  1365. type->base_type.type_flag = WASM_TYPE_FUNC;
  1366. type->param_count = param_count;
  1367. type->result_count = result_count;
  1368. type->ref_type_map_count = ref_type_map_count;
  1369. if (ref_type_map_count > 0) {
  1370. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1371. - result_ref_type_map_count;
  1372. }
  1373. for (i = 0; i < param_count; i++) {
  1374. if (!resolve_value_type(&p, p_end, module, type_count,
  1375. &need_ref_type_map, &ref_type, false, error_buf,
  1376. error_buf_size)) {
  1377. goto fail;
  1378. }
  1379. type->types[i] = ref_type.ref_type;
  1380. if (need_ref_type_map) {
  1381. type->ref_type_maps[j].index = i;
  1382. if (!(type->ref_type_maps[j++].ref_type =
  1383. reftype_set_insert(module->ref_type_set, &ref_type,
  1384. error_buf, error_buf_size))) {
  1385. goto fail;
  1386. }
  1387. }
  1388. }
  1389. read_leb_uint32(p, p_end, result_count);
  1390. for (i = 0; i < result_count; i++) {
  1391. if (!resolve_value_type(&p, p_end, module, type_count,
  1392. &need_ref_type_map, &ref_type, false, error_buf,
  1393. error_buf_size)) {
  1394. goto fail;
  1395. }
  1396. type->types[param_count + i] = ref_type.ref_type;
  1397. if (need_ref_type_map) {
  1398. type->ref_type_maps[j].index = param_count + i;
  1399. if (!(type->ref_type_maps[j++].ref_type =
  1400. reftype_set_insert(module->ref_type_set, &ref_type,
  1401. error_buf, error_buf_size))) {
  1402. goto fail;
  1403. }
  1404. }
  1405. }
  1406. bh_assert(j == type->ref_type_map_count);
  1407. #if TRACE_WASM_LOADER != 0
  1408. os_printf("type %d = ", type_idx);
  1409. wasm_dump_func_type(type);
  1410. #endif
  1411. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1412. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1413. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1414. set_error_buf(error_buf, error_buf_size,
  1415. "param count or result count too large");
  1416. goto fail;
  1417. }
  1418. type->param_cell_num = (uint16)param_cell_num;
  1419. type->ret_cell_num = (uint16)ret_cell_num;
  1420. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1421. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1422. #endif
  1423. #if WASM_ENABLE_WAMR_COMPILER != 0
  1424. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1425. if (type->types[i] == VALUE_TYPE_V128)
  1426. module->is_simd_used = true;
  1427. }
  1428. #endif
  1429. *p_buf = p;
  1430. module->types[type_idx] = (WASMType *)type;
  1431. return true;
  1432. fail:
  1433. if (type)
  1434. destroy_func_type(type);
  1435. return false;
  1436. }
  1437. static bool
  1438. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1439. WASMModule *module, uint32 type_count, uint32 type_idx,
  1440. char *error_buf, uint32 error_buf_size)
  1441. {
  1442. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1443. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1444. uint32 i, j = 0, offset;
  1445. uint16 *reference_table;
  1446. uint64 total_size;
  1447. uint8 mutable;
  1448. bool need_ref_type_map;
  1449. WASMRefType ref_type;
  1450. WASMStructType *type = NULL;
  1451. /* Parse first time to resolve field count and ref type map count */
  1452. read_leb_uint32(p, p_end, field_count);
  1453. p_org = p;
  1454. for (i = 0; i < field_count; i++) {
  1455. if (!resolve_value_type(&p, p_end, module, type_count,
  1456. &need_ref_type_map, &ref_type, true, error_buf,
  1457. error_buf_size)) {
  1458. return false;
  1459. }
  1460. if (need_ref_type_map)
  1461. ref_type_map_count++;
  1462. if (wasm_is_type_reftype(ref_type.ref_type))
  1463. ref_field_count++;
  1464. CHECK_BUF(p, p_end, 1);
  1465. mutable = read_uint8(p);
  1466. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1467. return false;
  1468. }
  1469. }
  1470. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1471. type_idx, field_count, ref_type_map_count);
  1472. /* Parse second time to resolve field types and ref type map info */
  1473. p = p_org;
  1474. total_size = offsetof(WASMStructType, fields)
  1475. + sizeof(WASMStructFieldType) * (uint64)field_count
  1476. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1477. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1478. return false;
  1479. }
  1480. if (ref_type_map_count > 0) {
  1481. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1482. if (!(type->ref_type_maps =
  1483. loader_malloc(total_size, error_buf, error_buf_size))) {
  1484. goto fail;
  1485. }
  1486. }
  1487. type->reference_table = reference_table =
  1488. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1489. + sizeof(WASMStructFieldType) * field_count);
  1490. *reference_table++ = ref_field_count;
  1491. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1492. type->field_count = field_count;
  1493. type->ref_type_map_count = ref_type_map_count;
  1494. offset = (uint32)offsetof(WASMStructObject, field_data);
  1495. for (i = 0; i < field_count; i++) {
  1496. if (!resolve_value_type(&p, p_end, module, type_count,
  1497. &need_ref_type_map, &ref_type, true, error_buf,
  1498. error_buf_size)) {
  1499. goto fail;
  1500. }
  1501. type->fields[i].field_type = ref_type.ref_type;
  1502. if (need_ref_type_map) {
  1503. type->ref_type_maps[j].index = i;
  1504. if (!(type->ref_type_maps[j++].ref_type =
  1505. reftype_set_insert(module->ref_type_set, &ref_type,
  1506. error_buf, error_buf_size))) {
  1507. goto fail;
  1508. }
  1509. }
  1510. CHECK_BUF(p, p_end, 1);
  1511. type->fields[i].field_flags = read_uint8(p);
  1512. type->fields[i].field_size =
  1513. (uint8)wasm_reftype_size(ref_type.ref_type);
  1514. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1515. || defined(BUILD_TARGET_X86_32))
  1516. if (type->fields[i].field_size == 2)
  1517. offset = align_uint(offset, 2);
  1518. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1519. offset = align_uint(offset, 4);
  1520. #endif
  1521. type->fields[i].field_offset = offset;
  1522. if (wasm_is_type_reftype(ref_type.ref_type))
  1523. *reference_table++ = offset;
  1524. offset += type->fields[i].field_size;
  1525. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1526. type->fields[i].field_flags, type->fields[i].field_type);
  1527. }
  1528. type->total_size = offset;
  1529. bh_assert(j == type->ref_type_map_count);
  1530. #if TRACE_WASM_LOADER != 0
  1531. os_printf("type %d = ", type_idx);
  1532. wasm_dump_struct_type(type);
  1533. #endif
  1534. *p_buf = p;
  1535. module->types[type_idx] = (WASMType *)type;
  1536. return true;
  1537. fail:
  1538. if (type)
  1539. destroy_struct_type(type);
  1540. return false;
  1541. }
  1542. static bool
  1543. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1544. WASMModule *module, uint32 type_count, uint32 type_idx,
  1545. char *error_buf, uint32 error_buf_size)
  1546. {
  1547. const uint8 *p = *p_buf, *p_end = buf_end;
  1548. uint8 mutable;
  1549. bool need_ref_type_map;
  1550. WASMRefType ref_type;
  1551. WASMArrayType *type = NULL;
  1552. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1553. &ref_type, true, error_buf, error_buf_size)) {
  1554. return false;
  1555. }
  1556. CHECK_BUF(p, p_end, 1);
  1557. mutable = read_uint8(p);
  1558. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1559. return false;
  1560. }
  1561. LOG_VERBOSE("type %u: array", type_idx);
  1562. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1563. error_buf_size))) {
  1564. return false;
  1565. }
  1566. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1567. type->elem_flags = mutable;
  1568. type->elem_type = ref_type.ref_type;
  1569. if (need_ref_type_map) {
  1570. if (!(type->elem_ref_type =
  1571. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1572. error_buf_size))) {
  1573. goto fail;
  1574. }
  1575. }
  1576. #if TRACE_WASM_LOADER != 0
  1577. os_printf("type %d = ", type_idx);
  1578. wasm_dump_array_type(type);
  1579. #endif
  1580. *p_buf = p;
  1581. module->types[type_idx] = (WASMType *)type;
  1582. return true;
  1583. fail:
  1584. if (type)
  1585. destroy_array_type(type);
  1586. return false;
  1587. }
  1588. static bool
  1589. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1590. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1591. {
  1592. if (heap_type >= 0) {
  1593. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1594. error_buf_size)) {
  1595. return false;
  1596. }
  1597. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1598. heap_type);
  1599. }
  1600. else {
  1601. if (!wasm_is_valid_heap_type(heap_type)) {
  1602. set_error_buf(error_buf, error_buf_size, "unknown type");
  1603. return false;
  1604. }
  1605. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1606. heap_type);
  1607. if (nullable) {
  1608. /* For (ref null func/extern/any/eq/i31/data),
  1609. they are same as
  1610. funcref/externref/anyref/eqref/i31ref/dataref,
  1611. we convert the multi-byte type to one-byte
  1612. type to reduce the footprint and the
  1613. complexity of type equal/subtype checking */
  1614. ref_type->ref_type =
  1615. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1616. }
  1617. }
  1618. return true;
  1619. }
  1620. static void
  1621. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1622. WASMRefType *ref_type2)
  1623. {
  1624. /**
  1625. * The difference rt1 ∖ rt2 between two reference types is defined as
  1626. * follows:
  1627. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1628. * (ref ht2) = (ref null?1 ht1)
  1629. */
  1630. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1631. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1632. ref_type1, wasm_reftype_struct_size(ref_type1));
  1633. }
  1634. else {
  1635. ref_type_diff->ref_type = ref_type1->ref_type;
  1636. }
  1637. if (ref_type2->ref_ht_common.nullable) {
  1638. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1639. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1640. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1641. (int32)ref_type_diff->ref_type - 0x80);
  1642. }
  1643. else {
  1644. ref_type_diff->ref_ht_typeidx.nullable = false;
  1645. }
  1646. }
  1647. }
  1648. #else /* else of WASM_ENABLE_GC != 0 */
  1649. static void
  1650. destroy_wasm_type(WASMType *type)
  1651. {
  1652. if (type->ref_count > 1) {
  1653. /* The type is referenced by other types
  1654. of current wasm module */
  1655. type->ref_count--;
  1656. return;
  1657. }
  1658. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1659. && WASM_ENABLE_LAZY_JIT != 0
  1660. if (type->call_to_llvm_jit_from_fast_jit)
  1661. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1662. #endif
  1663. wasm_runtime_free(type);
  1664. }
  1665. #endif /* end of WASM_ENABLE_GC != 0 */
  1666. static bool
  1667. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1668. char *error_buf, uint32 error_buf_size)
  1669. {
  1670. const uint8 *p = buf, *p_end = buf_end;
  1671. uint32 type_count, i;
  1672. uint64 total_size;
  1673. uint8 flag;
  1674. #if WASM_ENABLE_GC != 0
  1675. uint32 processed_type_count = 0;
  1676. #endif
  1677. read_leb_uint32(p, p_end, type_count);
  1678. if (type_count) {
  1679. module->type_count = type_count;
  1680. total_size = sizeof(WASMType *) * (uint64)type_count;
  1681. if (!(module->types =
  1682. loader_malloc(total_size, error_buf, error_buf_size))) {
  1683. return false;
  1684. }
  1685. #if WASM_ENABLE_GC == 0
  1686. for (i = 0; i < type_count; i++) {
  1687. WASMFuncType *type;
  1688. const uint8 *p_org;
  1689. uint32 param_count, result_count, j;
  1690. uint32 param_cell_num, ret_cell_num;
  1691. CHECK_BUF(p, p_end, 1);
  1692. flag = read_uint8(p);
  1693. if (flag != 0x60) {
  1694. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1695. return false;
  1696. }
  1697. read_leb_uint32(p, p_end, param_count);
  1698. /* Resolve param count and result count firstly */
  1699. p_org = p;
  1700. CHECK_BUF(p, p_end, param_count);
  1701. p += param_count;
  1702. read_leb_uint32(p, p_end, result_count);
  1703. CHECK_BUF(p, p_end, result_count);
  1704. p = p_org;
  1705. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1706. set_error_buf(error_buf, error_buf_size,
  1707. "param count or result count too large");
  1708. return false;
  1709. }
  1710. total_size = offsetof(WASMFuncType, types)
  1711. + sizeof(uint8) * (uint64)(param_count + result_count);
  1712. if (!(type = module->types[i] =
  1713. loader_malloc(total_size, error_buf, error_buf_size))) {
  1714. return false;
  1715. }
  1716. /* Resolve param types and result types */
  1717. type->ref_count = 1;
  1718. type->param_count = (uint16)param_count;
  1719. type->result_count = (uint16)result_count;
  1720. for (j = 0; j < param_count; j++) {
  1721. CHECK_BUF(p, p_end, 1);
  1722. type->types[j] = read_uint8(p);
  1723. }
  1724. read_leb_uint32(p, p_end, result_count);
  1725. for (j = 0; j < result_count; j++) {
  1726. CHECK_BUF(p, p_end, 1);
  1727. type->types[param_count + j] = read_uint8(p);
  1728. }
  1729. for (j = 0; j < param_count + result_count; j++) {
  1730. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1731. set_error_buf(error_buf, error_buf_size,
  1732. "unknown value type");
  1733. return false;
  1734. }
  1735. }
  1736. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1737. ret_cell_num =
  1738. wasm_get_cell_num(type->types + param_count, result_count);
  1739. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1740. set_error_buf(error_buf, error_buf_size,
  1741. "param count or result count too large");
  1742. return false;
  1743. }
  1744. type->param_cell_num = (uint16)param_cell_num;
  1745. type->ret_cell_num = (uint16)ret_cell_num;
  1746. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1747. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1748. #endif
  1749. #if WASM_ENABLE_WAMR_COMPILER != 0
  1750. for (j = 0; j < type->param_count + type->result_count; j++) {
  1751. if (type->types[j] == VALUE_TYPE_V128)
  1752. module->is_simd_used = true;
  1753. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1754. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1755. module->is_ref_types_used = true;
  1756. }
  1757. #endif
  1758. /* If there is already a same type created, use it instead */
  1759. for (j = 0; j < i; j++) {
  1760. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1761. if (module->types[j]->ref_count == UINT16_MAX) {
  1762. set_error_buf(error_buf, error_buf_size,
  1763. "wasm type's ref count too large");
  1764. return false;
  1765. }
  1766. destroy_wasm_type(type);
  1767. module->types[i] = module->types[j];
  1768. module->types[j]->ref_count++;
  1769. break;
  1770. }
  1771. }
  1772. }
  1773. #else /* else of WASM_ENABLE_GC == 0 */
  1774. for (i = 0; i < type_count; i++) {
  1775. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1776. uint32 rec_count = 1, j;
  1777. bool is_sub_final = true;
  1778. CHECK_BUF(p, p_end, 1);
  1779. flag = read_uint8(p);
  1780. if (flag == DEFINED_TYPE_REC) {
  1781. read_leb_uint32(p, p_end, rec_count);
  1782. if (rec_count > 1) {
  1783. uint64 new_total_size;
  1784. /* integer overflow */
  1785. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1786. set_error_buf(error_buf, error_buf_size,
  1787. "recursive type count too large");
  1788. return false;
  1789. }
  1790. module->type_count += rec_count - 1;
  1791. new_total_size =
  1792. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1793. if (new_total_size > UINT32_MAX) {
  1794. set_error_buf(error_buf, error_buf_size,
  1795. "allocate memory failed");
  1796. return false;
  1797. }
  1798. MEM_REALLOC(module->types, (uint32)total_size,
  1799. (uint32)new_total_size);
  1800. total_size = new_total_size;
  1801. }
  1802. LOG_VERBOSE("Processing rec group [%d-%d]",
  1803. processed_type_count,
  1804. processed_type_count + rec_count - 1);
  1805. }
  1806. else {
  1807. p--;
  1808. }
  1809. for (j = 0; j < rec_count; j++) {
  1810. WASMType *cur_type = NULL;
  1811. CHECK_BUF(p, p_end, 1);
  1812. flag = read_uint8(p);
  1813. parent_type_idx = -1;
  1814. if (flag == DEFINED_TYPE_SUB
  1815. || flag == DEFINED_TYPE_SUB_FINAL) {
  1816. read_leb_uint32(p, p_end, super_type_count);
  1817. if (super_type_count > 1) {
  1818. set_error_buf(error_buf, error_buf_size,
  1819. "super type count too large");
  1820. return false;
  1821. }
  1822. if (super_type_count > 0) {
  1823. read_leb_uint32(p, p_end, parent_type_idx);
  1824. if (parent_type_idx >= processed_type_count + j) {
  1825. set_error_buf_v(error_buf, error_buf_size,
  1826. "unknown type %d", parent_type_idx);
  1827. return false;
  1828. }
  1829. if (module->types[parent_type_idx]->is_sub_final) {
  1830. set_error_buf(error_buf, error_buf_size,
  1831. "sub type can not inherit from "
  1832. "a final super type");
  1833. return false;
  1834. }
  1835. }
  1836. if (flag == DEFINED_TYPE_SUB)
  1837. is_sub_final = false;
  1838. CHECK_BUF(p, p_end, 1);
  1839. flag = read_uint8(p);
  1840. }
  1841. if (flag == DEFINED_TYPE_FUNC) {
  1842. if (!resolve_func_type(&p, buf_end, module,
  1843. processed_type_count + rec_count,
  1844. processed_type_count + j, error_buf,
  1845. error_buf_size)) {
  1846. return false;
  1847. }
  1848. }
  1849. else if (flag == DEFINED_TYPE_STRUCT) {
  1850. if (!resolve_struct_type(&p, buf_end, module,
  1851. processed_type_count + rec_count,
  1852. processed_type_count + j,
  1853. error_buf, error_buf_size)) {
  1854. return false;
  1855. }
  1856. }
  1857. else if (flag == DEFINED_TYPE_ARRAY) {
  1858. if (!resolve_array_type(&p, buf_end, module,
  1859. processed_type_count + rec_count,
  1860. processed_type_count + j, error_buf,
  1861. error_buf_size)) {
  1862. return false;
  1863. }
  1864. }
  1865. else {
  1866. set_error_buf(error_buf, error_buf_size,
  1867. "invalid type flag");
  1868. return false;
  1869. }
  1870. cur_type = module->types[processed_type_count + j];
  1871. cur_type->ref_count = 1;
  1872. cur_type->parent_type_idx = parent_type_idx;
  1873. cur_type->is_sub_final = is_sub_final;
  1874. cur_type->rec_count = rec_count;
  1875. cur_type->rec_idx = j;
  1876. cur_type->rec_begin_type_idx = processed_type_count;
  1877. }
  1878. /* resolve subtyping relationship in current rec group */
  1879. for (j = 0; j < rec_count; j++) {
  1880. WASMType *cur_type = module->types[processed_type_count + j];
  1881. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1882. WASMType *parent_type =
  1883. module->types[cur_type->parent_type_idx];
  1884. cur_type->parent_type = parent_type;
  1885. cur_type->root_type = parent_type->root_type;
  1886. if (parent_type->inherit_depth == UINT16_MAX) {
  1887. set_error_buf(error_buf, error_buf_size,
  1888. "parent type's inherit depth too large");
  1889. return false;
  1890. }
  1891. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1892. }
  1893. else {
  1894. cur_type->parent_type = NULL;
  1895. cur_type->root_type = cur_type;
  1896. cur_type->inherit_depth = 0;
  1897. }
  1898. }
  1899. for (j = 0; j < rec_count; j++) {
  1900. WASMType *cur_type = module->types[processed_type_count + j];
  1901. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1902. WASMType *parent_type =
  1903. module->types[cur_type->parent_type_idx];
  1904. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1905. module->types,
  1906. module->type_count)) {
  1907. set_error_buf(error_buf, error_buf_size,
  1908. "sub type does not match super type");
  1909. return false;
  1910. }
  1911. }
  1912. }
  1913. /* If there is already an equivalence type or a group of equivalence
  1914. recursive types created, use it or them instead */
  1915. for (j = 0; j < processed_type_count;) {
  1916. WASMType *src_type = module->types[j];
  1917. WASMType *cur_type = module->types[processed_type_count];
  1918. uint32 k, src_rec_count;
  1919. src_rec_count = src_type->rec_count;
  1920. if (src_rec_count != rec_count) {
  1921. /* no type equivalence */
  1922. j += src_rec_count;
  1923. continue;
  1924. }
  1925. for (k = 0; k < rec_count; k++) {
  1926. src_type = module->types[j + k];
  1927. cur_type = module->types[processed_type_count + k];
  1928. if (!wasm_type_equal(src_type, cur_type, module->types,
  1929. module->type_count)) {
  1930. break;
  1931. }
  1932. }
  1933. if (k < rec_count) {
  1934. /* no type equivalence */
  1935. j += src_rec_count;
  1936. continue;
  1937. }
  1938. /* type equivalence */
  1939. for (k = 0; k < rec_count; k++) {
  1940. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1941. set_error_buf(error_buf, error_buf_size,
  1942. "wasm type's ref count too large");
  1943. return false;
  1944. }
  1945. destroy_wasm_type(module->types[processed_type_count + k]);
  1946. module->types[processed_type_count + k] =
  1947. module->types[j + k];
  1948. module->types[j + k]->ref_count++;
  1949. }
  1950. break;
  1951. }
  1952. if (rec_count > 1) {
  1953. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1954. processed_type_count,
  1955. processed_type_count + rec_count - 1);
  1956. }
  1957. processed_type_count += rec_count;
  1958. }
  1959. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1960. * module->type_count,
  1961. error_buf, error_buf_size))) {
  1962. return false;
  1963. }
  1964. #endif /* end of WASM_ENABLE_GC == 0 */
  1965. }
  1966. if (p != p_end) {
  1967. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1968. return false;
  1969. }
  1970. LOG_VERBOSE("Load type section success.\n");
  1971. return true;
  1972. fail:
  1973. return false;
  1974. }
  1975. static void
  1976. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  1977. uint32 *max_size)
  1978. {
  1979. uint32 default_max_size;
  1980. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  1981. * ABI unchanged */
  1982. (void)is_table64;
  1983. if (UINT32_MAX / 2 > init_size)
  1984. default_max_size = init_size * 2;
  1985. else
  1986. default_max_size = UINT32_MAX;
  1987. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1988. default_max_size = WASM_TABLE_MAX_SIZE;
  1989. if (max_size_flag) {
  1990. /* module defines the table limitation */
  1991. bh_assert(init_size <= *max_size);
  1992. if (init_size < *max_size) {
  1993. *max_size =
  1994. *max_size < default_max_size ? *max_size : default_max_size;
  1995. }
  1996. }
  1997. else {
  1998. /* partial defined table limitation, gives a default value */
  1999. *max_size = default_max_size;
  2000. }
  2001. }
  2002. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2003. /**
  2004. * Find export item of a module with export info:
  2005. * module name, field name and export kind
  2006. */
  2007. static WASMExport *
  2008. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2009. const char *field_name, uint8 export_kind,
  2010. char *error_buf, uint32 error_buf_size)
  2011. {
  2012. WASMExport *export =
  2013. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2014. export_kind, error_buf, error_buf_size);
  2015. return export;
  2016. }
  2017. #endif
  2018. #if WASM_ENABLE_MULTI_MODULE != 0
  2019. static WASMTable *
  2020. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2021. uint32 init_size, uint32 max_size, char *error_buf,
  2022. uint32 error_buf_size)
  2023. {
  2024. WASMModuleCommon *module_reg;
  2025. WASMTable *table = NULL;
  2026. WASMExport *export = NULL;
  2027. WASMModule *module = NULL;
  2028. module_reg = wasm_runtime_find_module_registered(module_name);
  2029. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2030. LOG_DEBUG("can not find a module named %s for table", module_name);
  2031. set_error_buf(error_buf, error_buf_size, "unknown import");
  2032. return NULL;
  2033. }
  2034. module = (WASMModule *)module_reg;
  2035. export =
  2036. wasm_loader_find_export(module, module_name, table_name,
  2037. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2038. if (!export) {
  2039. return NULL;
  2040. }
  2041. /* resolve table and check the init/max size */
  2042. if (export->index < module->import_table_count) {
  2043. table =
  2044. module->import_tables[export->index].u.table.import_table_linked;
  2045. }
  2046. else {
  2047. table = &(module->tables[export->index - module->import_table_count]);
  2048. }
  2049. if (table->table_type.init_size < init_size
  2050. || table->table_type.max_size > max_size) {
  2051. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2052. module_name, table_name, table->table_type.init_size,
  2053. table->table_type.max_size, init_size, max_size);
  2054. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2055. return NULL;
  2056. }
  2057. return table;
  2058. }
  2059. static WASMMemory *
  2060. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2061. uint32 init_page_count, uint32 max_page_count,
  2062. char *error_buf, uint32 error_buf_size)
  2063. {
  2064. WASMModuleCommon *module_reg;
  2065. WASMMemory *memory = NULL;
  2066. WASMExport *export = NULL;
  2067. WASMModule *module = NULL;
  2068. module_reg = wasm_runtime_find_module_registered(module_name);
  2069. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2070. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2071. set_error_buf(error_buf, error_buf_size, "unknown import");
  2072. return NULL;
  2073. }
  2074. module = (WASMModule *)module_reg;
  2075. export =
  2076. wasm_loader_find_export(module, module_name, memory_name,
  2077. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2078. if (!export) {
  2079. return NULL;
  2080. }
  2081. /* resolve memory and check the init/max page count */
  2082. if (export->index < module->import_memory_count) {
  2083. memory = module->import_memories[export->index]
  2084. .u.memory.import_memory_linked;
  2085. }
  2086. else {
  2087. memory =
  2088. &(module->memories[export->index - module->import_memory_count]);
  2089. }
  2090. if (memory->init_page_count < init_page_count
  2091. || memory->max_page_count > max_page_count) {
  2092. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2093. module_name, memory_name, memory->init_page_count,
  2094. memory->max_page_count, init_page_count, max_page_count);
  2095. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2096. return NULL;
  2097. }
  2098. return memory;
  2099. }
  2100. static WASMGlobal *
  2101. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2102. uint8 type, bool is_mutable, char *error_buf,
  2103. uint32 error_buf_size)
  2104. {
  2105. WASMModuleCommon *module_reg;
  2106. WASMGlobal *global = NULL;
  2107. WASMExport *export = NULL;
  2108. WASMModule *module = NULL;
  2109. module_reg = wasm_runtime_find_module_registered(module_name);
  2110. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2111. LOG_DEBUG("can not find a module named %s for global", module_name);
  2112. set_error_buf(error_buf, error_buf_size, "unknown import");
  2113. return NULL;
  2114. }
  2115. module = (WASMModule *)module_reg;
  2116. export =
  2117. wasm_loader_find_export(module, module_name, global_name,
  2118. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2119. if (!export) {
  2120. return NULL;
  2121. }
  2122. /* resolve and check the global */
  2123. if (export->index < module->import_global_count) {
  2124. global =
  2125. module->import_globals[export->index].u.global.import_global_linked;
  2126. }
  2127. else {
  2128. global =
  2129. &(module->globals[export->index - module->import_global_count]);
  2130. }
  2131. if (global->type.val_type != type
  2132. || global->type.is_mutable != is_mutable) {
  2133. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2134. module_name, global_name, global->type.val_type,
  2135. global->type.is_mutable, type, is_mutable);
  2136. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2137. return NULL;
  2138. }
  2139. return global;
  2140. }
  2141. #if WASM_ENABLE_TAGS != 0
  2142. static WASMTag *
  2143. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2144. const WASMType *expected_tag_type,
  2145. uint32 *linked_tag_index, char *error_buf,
  2146. uint32 error_buf_size)
  2147. {
  2148. WASMModuleCommon *module_reg;
  2149. WASMTag *tag = NULL;
  2150. WASMExport *export = NULL;
  2151. WASMModule *module = NULL;
  2152. module_reg = wasm_runtime_find_module_registered(module_name);
  2153. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2154. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2155. tag_name);
  2156. set_error_buf(error_buf, error_buf_size, "unknown import");
  2157. return NULL;
  2158. }
  2159. module = (WASMModule *)module_reg;
  2160. export =
  2161. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2162. error_buf, error_buf_size);
  2163. if (!export) {
  2164. return NULL;
  2165. }
  2166. /* resolve tag type and tag */
  2167. if (export->index < module->import_tag_count) {
  2168. /* importing an imported tag from the submodule */
  2169. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2170. }
  2171. else {
  2172. /* importing an section tag from the submodule */
  2173. tag = module->tags[export->index - module->import_tag_count];
  2174. }
  2175. /* check function type */
  2176. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2177. module->type_count)) {
  2178. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2179. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2180. return NULL;
  2181. }
  2182. if (linked_tag_index != NULL) {
  2183. *linked_tag_index = export->index;
  2184. }
  2185. return tag;
  2186. }
  2187. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2188. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2189. static bool
  2190. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2191. const WASMModule *parent_module,
  2192. const char *sub_module_name, const char *function_name,
  2193. WASMFunctionImport *function, bool no_resolve,
  2194. char *error_buf, uint32 error_buf_size)
  2195. {
  2196. const uint8 *p = *p_buf, *p_end = buf_end;
  2197. uint32 declare_type_index = 0;
  2198. read_leb_uint32(p, p_end, declare_type_index);
  2199. *p_buf = p;
  2200. if (declare_type_index >= parent_module->type_count) {
  2201. set_error_buf(error_buf, error_buf_size, "unknown type");
  2202. return false;
  2203. }
  2204. #if WASM_ENABLE_GC != 0
  2205. function->type_idx = declare_type_index;
  2206. #endif
  2207. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2208. declare_type_index = wasm_get_smallest_type_idx(
  2209. parent_module->types, parent_module->type_count, declare_type_index);
  2210. #endif
  2211. function->func_type =
  2212. (WASMFuncType *)parent_module->types[declare_type_index];
  2213. function->module_name = (char *)sub_module_name;
  2214. function->field_name = (char *)function_name;
  2215. function->attachment = NULL;
  2216. function->signature = NULL;
  2217. function->call_conv_raw = false;
  2218. /* lookup registered native symbols first */
  2219. if (!no_resolve) {
  2220. wasm_resolve_import_func(parent_module, function);
  2221. }
  2222. return true;
  2223. fail:
  2224. return false;
  2225. }
  2226. static bool
  2227. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2228. uint32 error_buf_size)
  2229. {
  2230. if (max_size < init_size) {
  2231. set_error_buf(error_buf, error_buf_size,
  2232. "size minimum must not be greater than maximum");
  2233. return false;
  2234. }
  2235. return true;
  2236. }
  2237. static bool
  2238. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2239. WASMModule *parent_module, const char *sub_module_name,
  2240. const char *table_name, WASMTableImport *table,
  2241. char *error_buf, uint32 error_buf_size)
  2242. {
  2243. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2244. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2245. declare_max_size = 0;
  2246. #if WASM_ENABLE_MULTI_MODULE != 0
  2247. WASMModule *sub_module = NULL;
  2248. WASMTable *linked_table = NULL;
  2249. #endif
  2250. #if WASM_ENABLE_GC != 0
  2251. WASMRefType ref_type;
  2252. bool need_ref_type_map;
  2253. #endif
  2254. bool is_table64 = false;
  2255. #if WASM_ENABLE_GC == 0
  2256. CHECK_BUF(p, p_end, 1);
  2257. /* 0x70 or 0x6F */
  2258. declare_elem_type = read_uint8(p);
  2259. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2260. #if WASM_ENABLE_REF_TYPES != 0
  2261. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2262. #endif
  2263. ) {
  2264. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2265. return false;
  2266. }
  2267. #else /* else of WASM_ENABLE_GC == 0 */
  2268. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2269. &need_ref_type_map, &ref_type, false, error_buf,
  2270. error_buf_size)) {
  2271. return false;
  2272. }
  2273. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2274. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2275. return false;
  2276. }
  2277. declare_elem_type = ref_type.ref_type;
  2278. if (need_ref_type_map) {
  2279. if (!(table->table_type.elem_ref_type =
  2280. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2281. error_buf, error_buf_size))) {
  2282. return false;
  2283. }
  2284. }
  2285. #if TRACE_WASM_LOADER != 0
  2286. os_printf("import table type: ");
  2287. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2288. os_printf("\n");
  2289. #endif
  2290. #endif /* end of WASM_ENABLE_GC == 0 */
  2291. p_org = p;
  2292. read_leb_uint32(p, p_end, table_flag);
  2293. is_table64 = table_flag & TABLE64_FLAG;
  2294. if (p - p_org > 1) {
  2295. LOG_VERBOSE("integer representation too long(import table)");
  2296. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2297. return false;
  2298. }
  2299. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2300. return false;
  2301. }
  2302. read_leb_uint32(p, p_end, declare_init_size);
  2303. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2304. read_leb_uint32(p, p_end, declare_max_size);
  2305. if (!check_table_max_size(declare_init_size, declare_max_size,
  2306. error_buf, error_buf_size))
  2307. return false;
  2308. }
  2309. adjust_table_max_size(is_table64, declare_init_size,
  2310. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2311. *p_buf = p;
  2312. #if WASM_ENABLE_MULTI_MODULE != 0
  2313. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2314. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2315. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2316. error_buf_size);
  2317. if (sub_module) {
  2318. linked_table = wasm_loader_resolve_table(
  2319. sub_module_name, table_name, declare_init_size,
  2320. declare_max_size, error_buf, error_buf_size);
  2321. if (linked_table) {
  2322. /* reset with linked table limit */
  2323. declare_elem_type = linked_table->table_type.elem_type;
  2324. declare_init_size = linked_table->table_type.init_size;
  2325. declare_max_size = linked_table->table_type.max_size;
  2326. table_flag = linked_table->table_type.flags;
  2327. table->import_table_linked = linked_table;
  2328. table->import_module = sub_module;
  2329. }
  2330. }
  2331. }
  2332. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2333. /* (table (export "table") 10 20 funcref) */
  2334. /* (table (export "table64") 10 20 funcref) */
  2335. /* we need this section working in wamrc */
  2336. if (!strcmp("spectest", sub_module_name)) {
  2337. const uint32 spectest_table_init_size = 10;
  2338. const uint32 spectest_table_max_size = 20;
  2339. if (strcmp("table", table_name)
  2340. #if WASM_ENABLE_MEMORY64 != 0
  2341. && strcmp("table64", table_name)
  2342. #endif
  2343. ) {
  2344. set_error_buf(error_buf, error_buf_size,
  2345. "incompatible import type or unknown import");
  2346. return false;
  2347. }
  2348. if (declare_init_size > spectest_table_init_size
  2349. || declare_max_size < spectest_table_max_size) {
  2350. set_error_buf(error_buf, error_buf_size,
  2351. "incompatible import type");
  2352. return false;
  2353. }
  2354. declare_init_size = spectest_table_init_size;
  2355. declare_max_size = spectest_table_max_size;
  2356. }
  2357. /* now we believe all declaration are ok */
  2358. table->table_type.elem_type = declare_elem_type;
  2359. table->table_type.init_size = declare_init_size;
  2360. table->table_type.flags = table_flag;
  2361. table->table_type.max_size = declare_max_size;
  2362. #if WASM_ENABLE_WAMR_COMPILER != 0
  2363. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2364. parent_module->is_ref_types_used = true;
  2365. #endif
  2366. (void)parent_module;
  2367. return true;
  2368. fail:
  2369. return false;
  2370. }
  2371. static bool
  2372. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2373. uint32 error_buf_size)
  2374. {
  2375. uint32 default_max_size =
  2376. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2377. if (!is_memory64 && init_size > default_max_size) {
  2378. set_error_buf(error_buf, error_buf_size,
  2379. "memory size must be at most 65536 pages (4GiB)");
  2380. return false;
  2381. }
  2382. #if WASM_ENABLE_MEMORY64 != 0
  2383. else if (is_memory64 && init_size > default_max_size) {
  2384. set_error_buf(
  2385. error_buf, error_buf_size,
  2386. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2387. return false;
  2388. }
  2389. #endif
  2390. return true;
  2391. }
  2392. static bool
  2393. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2394. char *error_buf, uint32 error_buf_size)
  2395. {
  2396. uint32 default_max_size =
  2397. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2398. if (max_size < init_size) {
  2399. set_error_buf(error_buf, error_buf_size,
  2400. "size minimum must not be greater than maximum");
  2401. return false;
  2402. }
  2403. if (!is_memory64 && max_size > default_max_size) {
  2404. set_error_buf(error_buf, error_buf_size,
  2405. "memory size must be at most 65536 pages (4GiB)");
  2406. return false;
  2407. }
  2408. #if WASM_ENABLE_MEMORY64 != 0
  2409. else if (is_memory64 && max_size > default_max_size) {
  2410. set_error_buf(
  2411. error_buf, error_buf_size,
  2412. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2413. return false;
  2414. }
  2415. #endif
  2416. return true;
  2417. }
  2418. static bool
  2419. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2420. WASMModule *parent_module, const char *sub_module_name,
  2421. const char *memory_name, WASMMemoryImport *memory,
  2422. char *error_buf, uint32 error_buf_size)
  2423. {
  2424. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2425. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2426. uint32 pool_size = wasm_runtime_memory_pool_size();
  2427. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2428. / DEFAULT_NUM_BYTES_PER_PAGE;
  2429. #else
  2430. uint32 max_page_count;
  2431. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2432. uint32 mem_flag = 0;
  2433. bool is_memory64 = false;
  2434. uint32 declare_init_page_count = 0;
  2435. uint32 declare_max_page_count = 0;
  2436. #if WASM_ENABLE_MULTI_MODULE != 0
  2437. WASMModule *sub_module = NULL;
  2438. WASMMemory *linked_memory = NULL;
  2439. #endif
  2440. p_org = p;
  2441. read_leb_uint32(p, p_end, mem_flag);
  2442. is_memory64 = mem_flag & MEMORY64_FLAG;
  2443. if (p - p_org > 1) {
  2444. LOG_VERBOSE("integer representation too long(import memory)");
  2445. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2446. return false;
  2447. }
  2448. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2449. return false;
  2450. }
  2451. read_leb_uint32(p, p_end, declare_init_page_count);
  2452. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2453. error_buf_size)) {
  2454. return false;
  2455. }
  2456. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2457. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2458. #endif
  2459. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2460. read_leb_uint32(p, p_end, declare_max_page_count);
  2461. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2462. declare_max_page_count, error_buf,
  2463. error_buf_size)) {
  2464. return false;
  2465. }
  2466. if (declare_max_page_count > max_page_count) {
  2467. declare_max_page_count = max_page_count;
  2468. }
  2469. }
  2470. else {
  2471. /* Limit the maximum memory size to max_page_count */
  2472. declare_max_page_count = max_page_count;
  2473. }
  2474. #if WASM_ENABLE_MULTI_MODULE != 0
  2475. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2476. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2477. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2478. error_buf_size);
  2479. if (sub_module) {
  2480. linked_memory = wasm_loader_resolve_memory(
  2481. sub_module_name, memory_name, declare_init_page_count,
  2482. declare_max_page_count, error_buf, error_buf_size);
  2483. if (linked_memory) {
  2484. /**
  2485. * reset with linked memory limit
  2486. */
  2487. memory->import_module = sub_module;
  2488. memory->import_memory_linked = linked_memory;
  2489. declare_init_page_count = linked_memory->init_page_count;
  2490. declare_max_page_count = linked_memory->max_page_count;
  2491. }
  2492. }
  2493. }
  2494. #endif
  2495. /* (memory (export "memory") 1 2) */
  2496. if (!strcmp("spectest", sub_module_name)) {
  2497. uint32 spectest_memory_init_page = 1;
  2498. uint32 spectest_memory_max_page = 2;
  2499. if (strcmp("memory", memory_name)) {
  2500. set_error_buf(error_buf, error_buf_size,
  2501. "incompatible import type or unknown import");
  2502. return false;
  2503. }
  2504. if (declare_init_page_count > spectest_memory_init_page
  2505. || declare_max_page_count < spectest_memory_max_page) {
  2506. set_error_buf(error_buf, error_buf_size,
  2507. "incompatible import type");
  2508. return false;
  2509. }
  2510. declare_init_page_count = spectest_memory_init_page;
  2511. declare_max_page_count = spectest_memory_max_page;
  2512. }
  2513. #if WASM_ENABLE_WASI_TEST != 0
  2514. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2515. else if (!strcmp("foo", sub_module_name)) {
  2516. uint32 spectest_memory_init_page = 1;
  2517. uint32 spectest_memory_max_page = 1;
  2518. if (strcmp("bar", memory_name)) {
  2519. set_error_buf(error_buf, error_buf_size,
  2520. "incompatible import type or unknown import");
  2521. return false;
  2522. }
  2523. if (declare_init_page_count > spectest_memory_init_page
  2524. || declare_max_page_count < spectest_memory_max_page) {
  2525. set_error_buf(error_buf, error_buf_size,
  2526. "incompatible import type");
  2527. return false;
  2528. }
  2529. declare_init_page_count = spectest_memory_init_page;
  2530. declare_max_page_count = spectest_memory_max_page;
  2531. }
  2532. #endif
  2533. /* now we believe all declaration are ok */
  2534. memory->mem_type.flags = mem_flag;
  2535. memory->mem_type.init_page_count = declare_init_page_count;
  2536. memory->mem_type.max_page_count = declare_max_page_count;
  2537. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2538. *p_buf = p;
  2539. (void)parent_module;
  2540. return true;
  2541. fail:
  2542. return false;
  2543. }
  2544. #if WASM_ENABLE_TAGS != 0
  2545. static bool
  2546. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2547. const WASMModule *parent_module, /* this module ! */
  2548. const char *sub_module_name, const char *tag_name,
  2549. WASMTagImport *tag, /* structure to fill */
  2550. char *error_buf, uint32 error_buf_size)
  2551. {
  2552. /* attribute and type of the import statement */
  2553. uint8 declare_tag_attribute;
  2554. uint32 declare_type_index;
  2555. const uint8 *p = *p_buf, *p_end = buf_end;
  2556. #if WASM_ENABLE_MULTI_MODULE != 0
  2557. WASMModule *sub_module = NULL;
  2558. #endif
  2559. /* get the one byte attribute */
  2560. CHECK_BUF(p, p_end, 1);
  2561. declare_tag_attribute = read_uint8(p);
  2562. if (declare_tag_attribute != 0) {
  2563. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2564. goto fail;
  2565. }
  2566. /* get type */
  2567. read_leb_uint32(p, p_end, declare_type_index);
  2568. /* compare against module->types */
  2569. if (declare_type_index >= parent_module->type_count) {
  2570. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2571. goto fail;
  2572. }
  2573. WASMFuncType *declare_tag_type =
  2574. (WASMFuncType *)parent_module->types[declare_type_index];
  2575. /* check, that the type of the declared tag returns void */
  2576. if (declare_tag_type->result_count != 0) {
  2577. set_error_buf(error_buf, error_buf_size,
  2578. "tag type signature does not return void");
  2579. goto fail;
  2580. }
  2581. #if WASM_ENABLE_MULTI_MODULE != 0
  2582. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2583. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2584. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2585. error_buf_size);
  2586. if (sub_module) {
  2587. /* wasm_loader_resolve_tag checks, that the imported tag
  2588. * and the declared tag have the same type
  2589. */
  2590. uint32 linked_tag_index = 0;
  2591. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2592. sub_module_name, tag_name, declare_tag_type,
  2593. &linked_tag_index /* out */, error_buf, error_buf_size);
  2594. if (linked_tag) {
  2595. tag->import_module = sub_module;
  2596. tag->import_tag_linked = linked_tag;
  2597. tag->import_tag_index_linked = linked_tag_index;
  2598. }
  2599. }
  2600. }
  2601. #endif
  2602. /* store to module tag declarations */
  2603. tag->attribute = declare_tag_attribute;
  2604. tag->type = declare_type_index;
  2605. tag->module_name = (char *)sub_module_name;
  2606. tag->field_name = (char *)tag_name;
  2607. tag->tag_type = declare_tag_type;
  2608. *p_buf = p;
  2609. (void)parent_module;
  2610. LOG_VERBOSE("Load tag import success\n");
  2611. return true;
  2612. fail:
  2613. return false;
  2614. }
  2615. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2616. static bool
  2617. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2618. WASMModule *parent_module, char *sub_module_name,
  2619. char *global_name, WASMGlobalImport *global, char *error_buf,
  2620. uint32 error_buf_size)
  2621. {
  2622. const uint8 *p = *p_buf, *p_end = buf_end;
  2623. uint8 declare_type = 0;
  2624. uint8 declare_mutable = 0;
  2625. #if WASM_ENABLE_MULTI_MODULE != 0
  2626. WASMModule *sub_module = NULL;
  2627. WASMGlobal *linked_global = NULL;
  2628. #endif
  2629. #if WASM_ENABLE_GC != 0
  2630. WASMRefType ref_type;
  2631. bool need_ref_type_map;
  2632. #endif
  2633. bool ret = false;
  2634. #if WASM_ENABLE_GC == 0
  2635. CHECK_BUF(p, p_end, 2);
  2636. /* global type */
  2637. declare_type = read_uint8(p);
  2638. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2639. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2640. return false;
  2641. }
  2642. declare_mutable = read_uint8(p);
  2643. #else
  2644. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2645. &need_ref_type_map, &ref_type, false, error_buf,
  2646. error_buf_size)) {
  2647. return false;
  2648. }
  2649. declare_type = ref_type.ref_type;
  2650. if (need_ref_type_map) {
  2651. if (!(global->ref_type =
  2652. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2653. error_buf, error_buf_size))) {
  2654. return false;
  2655. }
  2656. }
  2657. #if TRACE_WASM_LOADER != 0
  2658. os_printf("import global type: ");
  2659. wasm_dump_value_type(declare_type, global->ref_type);
  2660. os_printf("\n");
  2661. #endif
  2662. CHECK_BUF(p, p_end, 1);
  2663. declare_mutable = read_uint8(p);
  2664. #endif /* end of WASM_ENABLE_GC == 0 */
  2665. *p_buf = p;
  2666. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2667. return false;
  2668. }
  2669. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2670. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2671. global);
  2672. if (ret) {
  2673. if (global->type.val_type != declare_type
  2674. || global->type.is_mutable != declare_mutable) {
  2675. set_error_buf(error_buf, error_buf_size,
  2676. "incompatible import type");
  2677. return false;
  2678. }
  2679. global->is_linked = true;
  2680. }
  2681. #endif
  2682. #if WASM_ENABLE_MULTI_MODULE != 0
  2683. if (!global->is_linked
  2684. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2685. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2686. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2687. error_buf_size);
  2688. if (sub_module) {
  2689. /* check sub modules */
  2690. linked_global = wasm_loader_resolve_global(
  2691. sub_module_name, global_name, declare_type, declare_mutable,
  2692. error_buf, error_buf_size);
  2693. if (linked_global) {
  2694. global->import_module = sub_module;
  2695. global->import_global_linked = linked_global;
  2696. global->is_linked = true;
  2697. }
  2698. }
  2699. }
  2700. #endif
  2701. global->module_name = sub_module_name;
  2702. global->field_name = global_name;
  2703. global->type.val_type = declare_type;
  2704. global->type.is_mutable = (declare_mutable == 1);
  2705. #if WASM_ENABLE_WAMR_COMPILER != 0
  2706. if (global->type.val_type == VALUE_TYPE_V128)
  2707. parent_module->is_simd_used = true;
  2708. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2709. parent_module->is_ref_types_used = true;
  2710. #endif
  2711. (void)parent_module;
  2712. (void)ret;
  2713. return true;
  2714. fail:
  2715. return false;
  2716. }
  2717. static bool
  2718. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2719. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2720. {
  2721. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2722. #if WASM_ENABLE_GC != 0
  2723. WASMRefType ref_type;
  2724. bool need_ref_type_map;
  2725. #endif
  2726. bool is_table64 = false;
  2727. #if WASM_ENABLE_GC == 0
  2728. CHECK_BUF(p, p_end, 1);
  2729. /* 0x70 or 0x6F */
  2730. table->table_type.elem_type = read_uint8(p);
  2731. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2732. #if WASM_ENABLE_REF_TYPES != 0
  2733. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2734. #endif
  2735. ) {
  2736. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2737. return false;
  2738. }
  2739. #else /* else of WASM_ENABLE_GC == 0 */
  2740. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2741. &need_ref_type_map, &ref_type, false, error_buf,
  2742. error_buf_size)) {
  2743. return false;
  2744. }
  2745. table->table_type.elem_type = ref_type.ref_type;
  2746. if (need_ref_type_map) {
  2747. if (!(table->table_type.elem_ref_type =
  2748. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2749. error_buf_size))) {
  2750. return false;
  2751. }
  2752. }
  2753. #if TRACE_WASM_LOADER != 0
  2754. os_printf("table type: ");
  2755. wasm_dump_value_type(table->table_type.elem_type,
  2756. table->table_type.elem_ref_type);
  2757. os_printf("\n");
  2758. #endif
  2759. #endif /* end of WASM_ENABLE_GC == 0 */
  2760. p_org = p;
  2761. read_leb_uint32(p, p_end, table->table_type.flags);
  2762. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2763. if (p - p_org > 1) {
  2764. LOG_VERBOSE("integer representation too long(table)");
  2765. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2766. return false;
  2767. }
  2768. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2769. error_buf_size, false)) {
  2770. return false;
  2771. }
  2772. read_leb_uint32(p, p_end, table->table_type.init_size);
  2773. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2774. read_leb_uint32(p, p_end, table->table_type.max_size);
  2775. if (!check_table_max_size(table->table_type.init_size,
  2776. table->table_type.max_size, error_buf,
  2777. error_buf_size))
  2778. return false;
  2779. }
  2780. adjust_table_max_size(is_table64, table->table_type.init_size,
  2781. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2782. &table->table_type.max_size);
  2783. #if WASM_ENABLE_WAMR_COMPILER != 0
  2784. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2785. module->is_ref_types_used = true;
  2786. #endif
  2787. *p_buf = p;
  2788. return true;
  2789. fail:
  2790. return false;
  2791. }
  2792. static bool
  2793. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2794. char *error_buf, uint32 error_buf_size)
  2795. {
  2796. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2797. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2798. uint32 pool_size = wasm_runtime_memory_pool_size();
  2799. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2800. / DEFAULT_NUM_BYTES_PER_PAGE;
  2801. #else
  2802. uint32 max_page_count;
  2803. #endif
  2804. bool is_memory64 = false;
  2805. p_org = p;
  2806. read_leb_uint32(p, p_end, memory->flags);
  2807. is_memory64 = memory->flags & MEMORY64_FLAG;
  2808. if (p - p_org > 1) {
  2809. LOG_VERBOSE("integer representation too long(memory)");
  2810. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2811. return false;
  2812. }
  2813. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2814. false)) {
  2815. return false;
  2816. }
  2817. read_leb_uint32(p, p_end, memory->init_page_count);
  2818. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2819. error_buf_size))
  2820. return false;
  2821. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2822. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2823. #endif
  2824. if (memory->flags & 1) {
  2825. read_leb_uint32(p, p_end, memory->max_page_count);
  2826. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2827. memory->max_page_count, error_buf,
  2828. error_buf_size))
  2829. return false;
  2830. if (memory->max_page_count > max_page_count)
  2831. memory->max_page_count = max_page_count;
  2832. }
  2833. else {
  2834. /* Limit the maximum memory size to max_page_count */
  2835. memory->max_page_count = max_page_count;
  2836. }
  2837. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2838. *p_buf = p;
  2839. return true;
  2840. fail:
  2841. return false;
  2842. }
  2843. static int
  2844. cmp_export_name(const void *a, const void *b)
  2845. {
  2846. return strcmp(*(char **)a, *(char **)b);
  2847. }
  2848. static bool
  2849. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2850. bool is_load_from_file_buf, bool no_resolve,
  2851. char *error_buf, uint32 error_buf_size)
  2852. {
  2853. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2854. uint32 import_count, name_len, type_index, i, u32, flags;
  2855. uint64 total_size;
  2856. WASMImport *import;
  2857. WASMImport *import_functions = NULL, *import_tables = NULL;
  2858. WASMImport *import_memories = NULL, *import_globals = NULL;
  2859. #if WASM_ENABLE_TAGS != 0
  2860. WASMImport *import_tags = NULL;
  2861. #endif
  2862. char *sub_module_name, *field_name;
  2863. uint8 u8, kind, global_type;
  2864. read_leb_uint32(p, p_end, import_count);
  2865. if (import_count) {
  2866. module->import_count = import_count;
  2867. total_size = sizeof(WASMImport) * (uint64)import_count;
  2868. if (!(module->imports =
  2869. loader_malloc(total_size, error_buf, error_buf_size))) {
  2870. return false;
  2871. }
  2872. p_old = p;
  2873. /* Scan firstly to get import count of each type */
  2874. for (i = 0; i < import_count; i++) {
  2875. /* module name */
  2876. read_leb_uint32(p, p_end, name_len);
  2877. CHECK_BUF(p, p_end, name_len);
  2878. p += name_len;
  2879. /* field name */
  2880. read_leb_uint32(p, p_end, name_len);
  2881. CHECK_BUF(p, p_end, name_len);
  2882. p += name_len;
  2883. CHECK_BUF(p, p_end, 1);
  2884. /* 0x00/0x01/0x02/0x03/0x04 */
  2885. kind = read_uint8(p);
  2886. switch (kind) {
  2887. case IMPORT_KIND_FUNC: /* import function */
  2888. read_leb_uint32(p, p_end, type_index);
  2889. module->import_function_count++;
  2890. break;
  2891. case IMPORT_KIND_TABLE: /* import table */
  2892. CHECK_BUF(p, p_end, 1);
  2893. /* 0x70 */
  2894. u8 = read_uint8(p);
  2895. read_leb_uint32(p, p_end, flags);
  2896. read_leb_uint32(p, p_end, u32);
  2897. if (flags & 1)
  2898. read_leb_uint32(p, p_end, u32);
  2899. module->import_table_count++;
  2900. if (module->import_table_count > 1) {
  2901. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2902. set_error_buf(error_buf, error_buf_size,
  2903. "multiple tables");
  2904. return false;
  2905. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2906. module->is_ref_types_used = true;
  2907. #endif
  2908. }
  2909. break;
  2910. case IMPORT_KIND_MEMORY: /* import memory */
  2911. read_leb_uint32(p, p_end, flags);
  2912. read_leb_uint32(p, p_end, u32);
  2913. if (flags & 1)
  2914. read_leb_uint32(p, p_end, u32);
  2915. module->import_memory_count++;
  2916. #if WASM_ENABLE_MULTI_MEMORY == 0
  2917. if (module->import_memory_count > 1) {
  2918. set_error_buf(error_buf, error_buf_size,
  2919. "multiple memories");
  2920. return false;
  2921. }
  2922. #endif
  2923. break;
  2924. #if WASM_ENABLE_TAGS != 0
  2925. case IMPORT_KIND_TAG: /* import tags */
  2926. /* it only counts the number of tags to import */
  2927. module->import_tag_count++;
  2928. CHECK_BUF(p, p_end, 1);
  2929. u8 = read_uint8(p);
  2930. read_leb_uint32(p, p_end, type_index);
  2931. break;
  2932. #endif
  2933. case IMPORT_KIND_GLOBAL: /* import global */
  2934. #if WASM_ENABLE_GC != 0
  2935. /* valtype */
  2936. CHECK_BUF(p, p_end, 1);
  2937. global_type = read_uint8(p);
  2938. if (wasm_is_type_multi_byte_type(global_type)) {
  2939. int32 heap_type;
  2940. read_leb_int32(p, p_end, heap_type);
  2941. (void)heap_type;
  2942. }
  2943. /* mutability */
  2944. CHECK_BUF(p, p_end, 1);
  2945. p += 1;
  2946. #else
  2947. CHECK_BUF(p, p_end, 2);
  2948. p += 2;
  2949. #endif
  2950. (void)global_type;
  2951. module->import_global_count++;
  2952. break;
  2953. default:
  2954. set_error_buf(error_buf, error_buf_size,
  2955. "invalid import kind");
  2956. return false;
  2957. }
  2958. }
  2959. if (module->import_function_count)
  2960. import_functions = module->import_functions = module->imports;
  2961. if (module->import_table_count)
  2962. import_tables = module->import_tables =
  2963. module->imports + module->import_function_count;
  2964. if (module->import_memory_count)
  2965. import_memories = module->import_memories =
  2966. module->imports + module->import_function_count
  2967. + module->import_table_count;
  2968. #if WASM_ENABLE_TAGS != 0
  2969. if (module->import_tag_count)
  2970. import_tags = module->import_tags =
  2971. module->imports + module->import_function_count
  2972. + module->import_table_count + module->import_memory_count;
  2973. if (module->import_global_count)
  2974. import_globals = module->import_globals =
  2975. module->imports + module->import_function_count
  2976. + module->import_table_count + module->import_memory_count
  2977. + module->import_tag_count;
  2978. #else
  2979. if (module->import_global_count)
  2980. import_globals = module->import_globals =
  2981. module->imports + module->import_function_count
  2982. + module->import_table_count + module->import_memory_count;
  2983. #endif
  2984. p = p_old;
  2985. /* Scan again to resolve the data */
  2986. for (i = 0; i < import_count; i++) {
  2987. /* load module name */
  2988. read_leb_uint32(p, p_end, name_len);
  2989. CHECK_BUF(p, p_end, name_len);
  2990. if (!(sub_module_name = wasm_const_str_list_insert(
  2991. p, name_len, module, is_load_from_file_buf, error_buf,
  2992. error_buf_size))) {
  2993. return false;
  2994. }
  2995. p += name_len;
  2996. /* load field name */
  2997. read_leb_uint32(p, p_end, name_len);
  2998. CHECK_BUF(p, p_end, name_len);
  2999. if (!(field_name = wasm_const_str_list_insert(
  3000. p, name_len, module, is_load_from_file_buf, error_buf,
  3001. error_buf_size))) {
  3002. return false;
  3003. }
  3004. p += name_len;
  3005. CHECK_BUF(p, p_end, 1);
  3006. /* 0x00/0x01/0x02/0x03/0x4 */
  3007. kind = read_uint8(p);
  3008. switch (kind) {
  3009. case IMPORT_KIND_FUNC: /* import function */
  3010. bh_assert(import_functions);
  3011. import = import_functions++;
  3012. if (!load_function_import(&p, p_end, module,
  3013. sub_module_name, field_name,
  3014. &import->u.function, no_resolve,
  3015. error_buf, error_buf_size)) {
  3016. return false;
  3017. }
  3018. break;
  3019. case IMPORT_KIND_TABLE: /* import table */
  3020. bh_assert(import_tables);
  3021. import = import_tables++;
  3022. if (!load_table_import(&p, p_end, module, sub_module_name,
  3023. field_name, &import->u.table,
  3024. error_buf, error_buf_size)) {
  3025. LOG_DEBUG("can not import such a table (%s,%s)",
  3026. sub_module_name, field_name);
  3027. return false;
  3028. }
  3029. break;
  3030. case IMPORT_KIND_MEMORY: /* import memory */
  3031. bh_assert(import_memories);
  3032. import = import_memories++;
  3033. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3034. field_name, &import->u.memory,
  3035. error_buf, error_buf_size)) {
  3036. return false;
  3037. }
  3038. break;
  3039. #if WASM_ENABLE_TAGS != 0
  3040. case IMPORT_KIND_TAG:
  3041. bh_assert(import_tags);
  3042. import = import_tags++;
  3043. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3044. field_name, &import->u.tag, error_buf,
  3045. error_buf_size)) {
  3046. return false;
  3047. }
  3048. break;
  3049. #endif
  3050. case IMPORT_KIND_GLOBAL: /* import global */
  3051. bh_assert(import_globals);
  3052. import = import_globals++;
  3053. if (!load_global_import(&p, p_end, module, sub_module_name,
  3054. field_name, &import->u.global,
  3055. error_buf, error_buf_size)) {
  3056. return false;
  3057. }
  3058. break;
  3059. default:
  3060. set_error_buf(error_buf, error_buf_size,
  3061. "invalid import kind");
  3062. return false;
  3063. }
  3064. import->kind = kind;
  3065. import->u.names.module_name = sub_module_name;
  3066. import->u.names.field_name = field_name;
  3067. }
  3068. #if WASM_ENABLE_LIBC_WASI != 0
  3069. import = module->import_functions;
  3070. for (i = 0; i < module->import_function_count; i++, import++) {
  3071. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3072. || !strcmp(import->u.names.module_name,
  3073. "wasi_snapshot_preview1")) {
  3074. module->import_wasi_api = true;
  3075. break;
  3076. }
  3077. }
  3078. #endif
  3079. }
  3080. if (p != p_end) {
  3081. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3082. return false;
  3083. }
  3084. LOG_VERBOSE("Load import section success.\n");
  3085. (void)u8;
  3086. (void)u32;
  3087. (void)type_index;
  3088. return true;
  3089. fail:
  3090. return false;
  3091. }
  3092. static bool
  3093. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3094. uint32 error_buf_size)
  3095. {
  3096. WASMFuncType *param_type = func->func_type;
  3097. uint32 param_count = param_type->param_count;
  3098. uint8 *param_types = param_type->types;
  3099. uint32 local_count = func->local_count;
  3100. uint8 *local_types = func->local_types;
  3101. uint32 i, local_offset = 0;
  3102. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3103. /*
  3104. * Only allocate memory when total_size is not 0,
  3105. * or the return value of malloc(0) might be NULL on some platforms,
  3106. * which causes wasm loader return false.
  3107. */
  3108. if (total_size > 0
  3109. && !(func->local_offsets =
  3110. loader_malloc(total_size, error_buf, error_buf_size))) {
  3111. return false;
  3112. }
  3113. for (i = 0; i < param_count; i++) {
  3114. func->local_offsets[i] = (uint16)local_offset;
  3115. local_offset += wasm_value_type_cell_num(param_types[i]);
  3116. }
  3117. for (i = 0; i < local_count; i++) {
  3118. func->local_offsets[param_count + i] = (uint16)local_offset;
  3119. local_offset += wasm_value_type_cell_num(local_types[i]);
  3120. }
  3121. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3122. return true;
  3123. }
  3124. static bool
  3125. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3126. const uint8 *buf_code, const uint8 *buf_code_end,
  3127. WASMModule *module, char *error_buf,
  3128. uint32 error_buf_size)
  3129. {
  3130. const uint8 *p = buf, *p_end = buf_end;
  3131. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3132. uint32 func_count;
  3133. uint64 total_size;
  3134. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3135. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3136. uint8 type;
  3137. WASMFunction *func;
  3138. #if WASM_ENABLE_GC != 0
  3139. bool need_ref_type_map;
  3140. WASMRefType ref_type;
  3141. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3142. #endif
  3143. read_leb_uint32(p, p_end, func_count);
  3144. if (buf_code)
  3145. read_leb_uint32(p_code, buf_code_end, code_count);
  3146. if (func_count != code_count) {
  3147. set_error_buf(error_buf, error_buf_size,
  3148. "function and code section have inconsistent lengths or "
  3149. "unexpected end");
  3150. return false;
  3151. }
  3152. if (is_indices_overflow(module->import_function_count, func_count,
  3153. error_buf, error_buf_size))
  3154. return false;
  3155. if (func_count) {
  3156. module->function_count = func_count;
  3157. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3158. if (!(module->functions =
  3159. loader_malloc(total_size, error_buf, error_buf_size))) {
  3160. return false;
  3161. }
  3162. for (i = 0; i < func_count; i++) {
  3163. /* Resolve function type */
  3164. read_leb_uint32(p, p_end, type_index);
  3165. if (type_index >= module->type_count) {
  3166. set_error_buf(error_buf, error_buf_size, "unknown type");
  3167. return false;
  3168. }
  3169. #if WASM_ENABLE_GC != 0
  3170. type_index_org = type_index;
  3171. #endif
  3172. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3173. && WASM_ENABLE_GC == 0
  3174. type_index = wasm_get_smallest_type_idx(
  3175. module->types, module->type_count, type_index);
  3176. #endif
  3177. read_leb_uint32(p_code, buf_code_end, code_size);
  3178. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3179. set_error_buf(error_buf, error_buf_size,
  3180. "invalid function code size");
  3181. return false;
  3182. }
  3183. /* Resolve local set count */
  3184. p_code_end = p_code + code_size;
  3185. local_count = 0;
  3186. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3187. p_code_save = p_code;
  3188. #if WASM_ENABLE_GC != 0
  3189. ref_type_map_count = 0;
  3190. #endif
  3191. /* Calculate total local count */
  3192. for (j = 0; j < local_set_count; j++) {
  3193. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3194. if (sub_local_count > UINT32_MAX - local_count) {
  3195. set_error_buf(error_buf, error_buf_size, "too many locals");
  3196. return false;
  3197. }
  3198. #if WASM_ENABLE_GC == 0
  3199. CHECK_BUF(p_code, buf_code_end, 1);
  3200. /* 0x7F/0x7E/0x7D/0x7C */
  3201. type = read_uint8(p_code);
  3202. local_count += sub_local_count;
  3203. #if WASM_ENABLE_WAMR_COMPILER != 0
  3204. /* If any value's type is v128, mark the module as SIMD used */
  3205. if (type == VALUE_TYPE_V128)
  3206. module->is_simd_used = true;
  3207. #endif
  3208. #else
  3209. if (!resolve_value_type(&p_code, buf_code_end, module,
  3210. module->type_count, &need_ref_type_map,
  3211. &ref_type, false, error_buf,
  3212. error_buf_size)) {
  3213. return false;
  3214. }
  3215. local_count += sub_local_count;
  3216. if (need_ref_type_map)
  3217. ref_type_map_count += sub_local_count;
  3218. #endif
  3219. }
  3220. /* Code size in code entry can't be smaller than size of vec(locals)
  3221. * + expr(at least 1 for opcode end). And expressions are encoded by
  3222. * their instruction sequence terminated with an explicit 0x0B
  3223. * opcode for end. */
  3224. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3225. set_error_buf(
  3226. error_buf, error_buf_size,
  3227. "section size mismatch: function body END opcode expected");
  3228. return false;
  3229. }
  3230. /* Alloc memory, layout: function structure + local types */
  3231. code_size = (uint32)(p_code_end - p_code);
  3232. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3233. if (!(func = module->functions[i] =
  3234. loader_malloc(total_size, error_buf, error_buf_size))) {
  3235. return false;
  3236. }
  3237. #if WASM_ENABLE_GC != 0
  3238. if (ref_type_map_count > 0) {
  3239. total_size =
  3240. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3241. if (!(func->local_ref_type_maps = loader_malloc(
  3242. total_size, error_buf, error_buf_size))) {
  3243. return false;
  3244. }
  3245. func->local_ref_type_map_count = ref_type_map_count;
  3246. }
  3247. #endif
  3248. /* Set function type, local count, code size and code body */
  3249. func->func_type = (WASMFuncType *)module->types[type_index];
  3250. func->local_count = local_count;
  3251. if (local_count > 0)
  3252. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3253. func->code_size = code_size;
  3254. /*
  3255. * we shall make a copy of code body [p_code, p_code + code_size]
  3256. * when we are worrying about inappropriate releasing behaviour.
  3257. * all code bodies are actually in a buffer which user allocates in
  3258. * his embedding environment and we don't have power on them.
  3259. * it will be like:
  3260. * code_body_cp = malloc(code_size);
  3261. * memcpy(code_body_cp, p_code, code_size);
  3262. * func->code = code_body_cp;
  3263. */
  3264. func->code = (uint8 *)p_code;
  3265. #if WASM_ENABLE_GC != 0
  3266. func->type_idx = type_index_org;
  3267. #endif
  3268. #if WASM_ENABLE_GC != 0
  3269. t = 0;
  3270. #endif
  3271. /* Load each local type */
  3272. p_code = p_code_save;
  3273. local_type_index = 0;
  3274. for (j = 0; j < local_set_count; j++) {
  3275. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3276. /* Note: sub_local_count is allowed to be 0 */
  3277. if (local_type_index > UINT32_MAX - sub_local_count
  3278. || local_type_index + sub_local_count > local_count) {
  3279. set_error_buf(error_buf, error_buf_size,
  3280. "invalid local count");
  3281. return false;
  3282. }
  3283. #if WASM_ENABLE_GC == 0
  3284. CHECK_BUF(p_code, buf_code_end, 1);
  3285. /* 0x7F/0x7E/0x7D/0x7C */
  3286. type = read_uint8(p_code);
  3287. if (!is_valid_value_type_for_interpreter(type)) {
  3288. if (type == VALUE_TYPE_V128)
  3289. set_error_buf(error_buf, error_buf_size,
  3290. "v128 value type requires simd feature");
  3291. else if (type == VALUE_TYPE_FUNCREF
  3292. || type == VALUE_TYPE_EXTERNREF)
  3293. set_error_buf(error_buf, error_buf_size,
  3294. "ref value type requires "
  3295. "reference types feature");
  3296. else
  3297. set_error_buf_v(error_buf, error_buf_size,
  3298. "invalid local type 0x%02X", type);
  3299. return false;
  3300. }
  3301. #else
  3302. if (!resolve_value_type(&p_code, buf_code_end, module,
  3303. module->type_count, &need_ref_type_map,
  3304. &ref_type, false, error_buf,
  3305. error_buf_size)) {
  3306. return false;
  3307. }
  3308. if (need_ref_type_map) {
  3309. WASMRefType *ref_type_tmp;
  3310. if (!(ref_type_tmp = reftype_set_insert(
  3311. module->ref_type_set, &ref_type, error_buf,
  3312. error_buf_size))) {
  3313. return false;
  3314. }
  3315. for (k = 0; k < sub_local_count; k++) {
  3316. func->local_ref_type_maps[t + k].ref_type =
  3317. ref_type_tmp;
  3318. func->local_ref_type_maps[t + k].index =
  3319. local_type_index + k;
  3320. }
  3321. t += sub_local_count;
  3322. }
  3323. type = ref_type.ref_type;
  3324. #endif
  3325. for (k = 0; k < sub_local_count; k++) {
  3326. func->local_types[local_type_index++] = type;
  3327. }
  3328. #if WASM_ENABLE_WAMR_COMPILER != 0
  3329. if (type == VALUE_TYPE_V128)
  3330. module->is_simd_used = true;
  3331. else if (type == VALUE_TYPE_FUNCREF
  3332. || type == VALUE_TYPE_EXTERNREF)
  3333. module->is_ref_types_used = true;
  3334. #endif
  3335. }
  3336. bh_assert(local_type_index == func->local_count);
  3337. #if WASM_ENABLE_GC != 0
  3338. bh_assert(t == func->local_ref_type_map_count);
  3339. #if TRACE_WASM_LOADER != 0
  3340. os_printf("func %u, local types: [", i);
  3341. k = 0;
  3342. for (j = 0; j < func->local_count; j++) {
  3343. WASMRefType *ref_type_tmp = NULL;
  3344. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3345. bh_assert(j == func->local_ref_type_maps[k].index);
  3346. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3347. }
  3348. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3349. if (j < func->local_count - 1)
  3350. os_printf(" ");
  3351. }
  3352. os_printf("]\n");
  3353. #endif
  3354. #endif
  3355. func->param_cell_num = func->func_type->param_cell_num;
  3356. func->ret_cell_num = func->func_type->ret_cell_num;
  3357. local_cell_num =
  3358. wasm_get_cell_num(func->local_types, func->local_count);
  3359. if (local_cell_num > UINT16_MAX) {
  3360. set_error_buf(error_buf, error_buf_size,
  3361. "local count too large");
  3362. return false;
  3363. }
  3364. func->local_cell_num = (uint16)local_cell_num;
  3365. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3366. return false;
  3367. p_code = p_code_end;
  3368. }
  3369. }
  3370. if (p != p_end) {
  3371. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3372. return false;
  3373. }
  3374. LOG_VERBOSE("Load function section success.\n");
  3375. return true;
  3376. fail:
  3377. return false;
  3378. }
  3379. static bool
  3380. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3381. char *error_buf, uint32 error_buf_size)
  3382. {
  3383. const uint8 *p = buf, *p_end = buf_end;
  3384. uint32 table_count, i;
  3385. uint64 total_size;
  3386. WASMTable *table;
  3387. read_leb_uint32(p, p_end, table_count);
  3388. if (module->import_table_count + table_count > 1) {
  3389. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3390. /* a total of one table is allowed */
  3391. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3392. return false;
  3393. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3394. module->is_ref_types_used = true;
  3395. #endif
  3396. }
  3397. if (table_count) {
  3398. module->table_count = table_count;
  3399. total_size = sizeof(WASMTable) * (uint64)table_count;
  3400. if (!(module->tables =
  3401. loader_malloc(total_size, error_buf, error_buf_size))) {
  3402. return false;
  3403. }
  3404. /* load each table */
  3405. table = module->tables;
  3406. for (i = 0; i < table_count; i++, table++) {
  3407. #if WASM_ENABLE_GC != 0
  3408. uint8 flag;
  3409. bool has_init = false;
  3410. CHECK_BUF(buf, buf_end, 1);
  3411. flag = read_uint8(p);
  3412. if (flag == TABLE_INIT_EXPR_FLAG) {
  3413. CHECK_BUF(buf, buf_end, 1);
  3414. flag = read_uint8(p);
  3415. if (flag != 0x00) {
  3416. set_error_buf(error_buf, error_buf_size,
  3417. "invalid leading bytes for table");
  3418. return false;
  3419. }
  3420. has_init = true;
  3421. }
  3422. else {
  3423. p--;
  3424. }
  3425. #endif /* end of WASM_ENABLE_GC != 0 */
  3426. if (!load_table(&p, p_end, module, table, error_buf,
  3427. error_buf_size))
  3428. return false;
  3429. #if WASM_ENABLE_GC != 0
  3430. if (has_init) {
  3431. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3432. table->table_type.elem_type,
  3433. table->table_type.elem_ref_type, error_buf,
  3434. error_buf_size))
  3435. return false;
  3436. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3437. && table->init_expr.init_expr_type
  3438. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3439. set_error_buf(
  3440. error_buf, error_buf_size,
  3441. "unsupported initializer expression for table");
  3442. return false;
  3443. }
  3444. }
  3445. else {
  3446. if (wasm_is_reftype_htref_non_nullable(
  3447. table->table_type.elem_type)) {
  3448. set_error_buf(
  3449. error_buf, error_buf_size,
  3450. "type mismatch: non-nullable table without init expr");
  3451. return false;
  3452. }
  3453. }
  3454. #endif /* end of WASM_ENABLE_GC != 0 */
  3455. #if WASM_ENABLE_WAMR_COMPILER != 0
  3456. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3457. module->is_ref_types_used = true;
  3458. #endif
  3459. }
  3460. }
  3461. if (p != p_end) {
  3462. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3463. return false;
  3464. }
  3465. LOG_VERBOSE("Load table section success.\n");
  3466. return true;
  3467. fail:
  3468. return false;
  3469. }
  3470. static bool
  3471. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3472. char *error_buf, uint32 error_buf_size)
  3473. {
  3474. const uint8 *p = buf, *p_end = buf_end;
  3475. uint32 memory_count, i;
  3476. uint64 total_size;
  3477. WASMMemory *memory;
  3478. read_leb_uint32(p, p_end, memory_count);
  3479. #if WASM_ENABLE_MULTI_MEMORY == 0
  3480. /* a total of one memory is allowed */
  3481. if (module->import_memory_count + memory_count > 1) {
  3482. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3483. return false;
  3484. }
  3485. #endif
  3486. if (memory_count) {
  3487. module->memory_count = memory_count;
  3488. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3489. if (!(module->memories =
  3490. loader_malloc(total_size, error_buf, error_buf_size))) {
  3491. return false;
  3492. }
  3493. /* load each memory */
  3494. memory = module->memories;
  3495. for (i = 0; i < memory_count; i++, memory++)
  3496. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3497. return false;
  3498. }
  3499. if (p != p_end) {
  3500. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3501. return false;
  3502. }
  3503. LOG_VERBOSE("Load memory section success.\n");
  3504. return true;
  3505. fail:
  3506. return false;
  3507. }
  3508. static bool
  3509. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3510. char *error_buf, uint32 error_buf_size)
  3511. {
  3512. const uint8 *p = buf, *p_end = buf_end;
  3513. uint32 global_count, i;
  3514. uint64 total_size;
  3515. WASMGlobal *global;
  3516. uint8 mutable;
  3517. #if WASM_ENABLE_GC != 0
  3518. bool need_ref_type_map;
  3519. WASMRefType ref_type;
  3520. #endif
  3521. read_leb_uint32(p, p_end, global_count);
  3522. if (is_indices_overflow(module->import_global_count, global_count,
  3523. error_buf, error_buf_size))
  3524. return false;
  3525. module->global_count = 0;
  3526. if (global_count) {
  3527. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3528. if (!(module->globals =
  3529. loader_malloc(total_size, error_buf, error_buf_size))) {
  3530. return false;
  3531. }
  3532. global = module->globals;
  3533. for (i = 0; i < global_count; i++, global++) {
  3534. #if WASM_ENABLE_GC == 0
  3535. CHECK_BUF(p, p_end, 2);
  3536. /* global type */
  3537. global->type.val_type = read_uint8(p);
  3538. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3539. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3540. return false;
  3541. }
  3542. mutable = read_uint8(p);
  3543. #else
  3544. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3545. &need_ref_type_map, &ref_type, false,
  3546. error_buf, error_buf_size)) {
  3547. return false;
  3548. }
  3549. global->type.val_type = ref_type.ref_type;
  3550. CHECK_BUF(p, p_end, 1);
  3551. mutable = read_uint8(p);
  3552. #endif /* end of WASM_ENABLE_GC */
  3553. #if WASM_ENABLE_WAMR_COMPILER != 0
  3554. if (global->type.val_type == VALUE_TYPE_V128)
  3555. module->is_simd_used = true;
  3556. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3557. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3558. module->is_ref_types_used = true;
  3559. #endif
  3560. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3561. return false;
  3562. }
  3563. global->type.is_mutable = mutable ? true : false;
  3564. /* initialize expression */
  3565. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3566. global->type.val_type,
  3567. #if WASM_ENABLE_GC == 0
  3568. NULL,
  3569. #else
  3570. &ref_type,
  3571. #endif
  3572. error_buf, error_buf_size))
  3573. return false;
  3574. #if WASM_ENABLE_GC != 0
  3575. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3576. uint8 global_type;
  3577. WASMRefType *global_ref_type;
  3578. uint32 global_idx = global->init_expr.u.global_index;
  3579. if (global->init_expr.u.global_index
  3580. >= module->import_global_count + i) {
  3581. set_error_buf(error_buf, error_buf_size, "unknown global");
  3582. return false;
  3583. }
  3584. if (global_idx < module->import_global_count) {
  3585. global_type = module->import_globals[global_idx]
  3586. .u.global.type.val_type;
  3587. global_ref_type =
  3588. module->import_globals[global_idx].u.global.ref_type;
  3589. }
  3590. else {
  3591. global_type =
  3592. module
  3593. ->globals[global_idx - module->import_global_count]
  3594. .type.val_type;
  3595. global_ref_type =
  3596. module
  3597. ->globals[global_idx - module->import_global_count]
  3598. .ref_type;
  3599. }
  3600. if (!wasm_reftype_is_subtype_of(
  3601. global_type, global_ref_type, global->type.val_type,
  3602. global->ref_type, module->types, module->type_count)) {
  3603. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3604. return false;
  3605. }
  3606. }
  3607. if (need_ref_type_map) {
  3608. if (!(global->ref_type =
  3609. reftype_set_insert(module->ref_type_set, &ref_type,
  3610. error_buf, error_buf_size))) {
  3611. return false;
  3612. }
  3613. }
  3614. #if TRACE_WASM_LOADER != 0
  3615. os_printf("global type: ");
  3616. wasm_dump_value_type(global->type, global->ref_type);
  3617. os_printf("\n");
  3618. #endif
  3619. #endif
  3620. module->global_count++;
  3621. }
  3622. bh_assert(module->global_count == global_count);
  3623. }
  3624. if (p != p_end) {
  3625. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3626. return false;
  3627. }
  3628. LOG_VERBOSE("Load global section success.\n");
  3629. return true;
  3630. fail:
  3631. return false;
  3632. }
  3633. static bool
  3634. check_duplicate_exports(WASMModule *module, char *error_buf,
  3635. uint32 error_buf_size)
  3636. {
  3637. uint32 i;
  3638. bool result = false;
  3639. char *names_buf[32], **names = names_buf;
  3640. if (module->export_count > 32) {
  3641. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3642. error_buf_size);
  3643. if (!names) {
  3644. return result;
  3645. }
  3646. }
  3647. for (i = 0; i < module->export_count; i++) {
  3648. names[i] = module->exports[i].name;
  3649. }
  3650. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3651. for (i = 1; i < module->export_count; i++) {
  3652. if (!strcmp(names[i], names[i - 1])) {
  3653. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3654. goto cleanup;
  3655. }
  3656. }
  3657. result = true;
  3658. cleanup:
  3659. if (module->export_count > 32) {
  3660. wasm_runtime_free(names);
  3661. }
  3662. return result;
  3663. }
  3664. static bool
  3665. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3666. bool is_load_from_file_buf, char *error_buf,
  3667. uint32 error_buf_size)
  3668. {
  3669. const uint8 *p = buf, *p_end = buf_end;
  3670. uint32 export_count, i, index;
  3671. uint64 total_size;
  3672. uint32 str_len;
  3673. WASMExport *export;
  3674. read_leb_uint32(p, p_end, export_count);
  3675. if (export_count) {
  3676. module->export_count = export_count;
  3677. total_size = sizeof(WASMExport) * (uint64)export_count;
  3678. if (!(module->exports =
  3679. loader_malloc(total_size, error_buf, error_buf_size))) {
  3680. return false;
  3681. }
  3682. export = module->exports;
  3683. for (i = 0; i < export_count; i++, export ++) {
  3684. #if WASM_ENABLE_THREAD_MGR == 0
  3685. if (p == p_end) {
  3686. /* export section with inconsistent count:
  3687. n export declared, but less than n given */
  3688. set_error_buf(error_buf, error_buf_size,
  3689. "length out of bounds");
  3690. return false;
  3691. }
  3692. #endif
  3693. read_leb_uint32(p, p_end, str_len);
  3694. CHECK_BUF(p, p_end, str_len);
  3695. if (!(export->name = wasm_const_str_list_insert(
  3696. p, str_len, module, is_load_from_file_buf, error_buf,
  3697. error_buf_size))) {
  3698. return false;
  3699. }
  3700. p += str_len;
  3701. CHECK_BUF(p, p_end, 1);
  3702. export->kind = read_uint8(p);
  3703. read_leb_uint32(p, p_end, index);
  3704. export->index = index;
  3705. switch (export->kind) {
  3706. /* function index */
  3707. case EXPORT_KIND_FUNC:
  3708. if (index >= module->function_count
  3709. + module->import_function_count) {
  3710. set_error_buf(error_buf, error_buf_size,
  3711. "unknown function");
  3712. return false;
  3713. }
  3714. #if WASM_ENABLE_SIMD != 0
  3715. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3716. /* TODO: check func type, if it has v128 param or result,
  3717. report error */
  3718. #endif
  3719. #endif
  3720. break;
  3721. /* table index */
  3722. case EXPORT_KIND_TABLE:
  3723. if (index
  3724. >= module->table_count + module->import_table_count) {
  3725. set_error_buf(error_buf, error_buf_size,
  3726. "unknown table");
  3727. return false;
  3728. }
  3729. break;
  3730. /* memory index */
  3731. case EXPORT_KIND_MEMORY:
  3732. if (index
  3733. >= module->memory_count + module->import_memory_count) {
  3734. set_error_buf(error_buf, error_buf_size,
  3735. "unknown memory");
  3736. return false;
  3737. }
  3738. break;
  3739. #if WASM_ENABLE_TAGS != 0
  3740. /* export tag */
  3741. case EXPORT_KIND_TAG:
  3742. if (index >= module->tag_count + module->import_tag_count) {
  3743. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3744. return false;
  3745. }
  3746. break;
  3747. #endif
  3748. /* global index */
  3749. case EXPORT_KIND_GLOBAL:
  3750. if (index
  3751. >= module->global_count + module->import_global_count) {
  3752. set_error_buf(error_buf, error_buf_size,
  3753. "unknown global");
  3754. return false;
  3755. }
  3756. break;
  3757. default:
  3758. set_error_buf(error_buf, error_buf_size,
  3759. "invalid export kind");
  3760. return false;
  3761. }
  3762. }
  3763. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3764. return false;
  3765. }
  3766. }
  3767. if (p != p_end) {
  3768. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3769. return false;
  3770. }
  3771. LOG_VERBOSE("Load export section success.\n");
  3772. return true;
  3773. fail:
  3774. return false;
  3775. }
  3776. static bool
  3777. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3778. uint32 error_buf_size)
  3779. {
  3780. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3781. if (table_index != 0) {
  3782. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3783. return false;
  3784. }
  3785. #endif
  3786. if (table_index >= module->import_table_count + module->table_count) {
  3787. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3788. table_index);
  3789. return false;
  3790. }
  3791. return true;
  3792. }
  3793. static bool
  3794. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3795. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3796. {
  3797. const uint8 *p = *p_buf, *p_end = buf_end;
  3798. uint32 table_index;
  3799. read_leb_uint32(p, p_end, table_index);
  3800. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3801. return false;
  3802. }
  3803. *p_table_index = table_index;
  3804. *p_buf = p;
  3805. return true;
  3806. fail:
  3807. return false;
  3808. }
  3809. /* Element segments must match element type of table */
  3810. static bool
  3811. check_table_elem_type(WASMModule *module, uint32 table_index,
  3812. uint32 type_from_elem_seg, char *error_buf,
  3813. uint32 error_buf_size)
  3814. {
  3815. uint32 table_declared_elem_type;
  3816. if (table_index < module->import_table_count)
  3817. table_declared_elem_type =
  3818. module->import_tables[table_index].u.table.table_type.elem_type;
  3819. else
  3820. table_declared_elem_type =
  3821. (module->tables + table_index)->table_type.elem_type;
  3822. if (table_declared_elem_type == type_from_elem_seg)
  3823. return true;
  3824. #if WASM_ENABLE_GC != 0
  3825. /*
  3826. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3827. */
  3828. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3829. return true;
  3830. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3831. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3832. return true;
  3833. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3834. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3835. return true;
  3836. #endif
  3837. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3838. return false;
  3839. }
  3840. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3841. static bool
  3842. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3843. uint32 *p_elem_type,
  3844. #if WASM_ENABLE_GC != 0
  3845. WASMRefType **p_elem_ref_type,
  3846. #endif
  3847. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3848. {
  3849. const uint8 *p = *p_buf, *p_end = buf_end;
  3850. uint8 elem_type;
  3851. #if WASM_ENABLE_GC != 0
  3852. WASMRefType elem_ref_type;
  3853. bool need_ref_type_map;
  3854. #endif
  3855. CHECK_BUF(p, p_end, 1);
  3856. elem_type = read_uint8(p);
  3857. if (elemkind_zero) {
  3858. if (elem_type != 0) {
  3859. set_error_buf(error_buf, error_buf_size,
  3860. "invalid reference type or unknown type");
  3861. return false;
  3862. }
  3863. else {
  3864. *p_elem_type = VALUE_TYPE_FUNCREF;
  3865. *p_buf = p;
  3866. return true;
  3867. }
  3868. }
  3869. #if WASM_ENABLE_GC == 0
  3870. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3871. set_error_buf(error_buf, error_buf_size,
  3872. "invalid reference type or unknown type");
  3873. return false;
  3874. }
  3875. *p_elem_type = elem_type;
  3876. #else
  3877. p--;
  3878. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3879. module->type_count, &need_ref_type_map,
  3880. &elem_ref_type, false, error_buf, error_buf_size)) {
  3881. return false;
  3882. }
  3883. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3884. set_error_buf(error_buf, error_buf_size,
  3885. "invalid reference type or unknown type");
  3886. return false;
  3887. }
  3888. *p_elem_type = elem_ref_type.ref_type;
  3889. if (need_ref_type_map) {
  3890. if (!(*p_elem_ref_type =
  3891. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3892. error_buf, error_buf_size))) {
  3893. return false;
  3894. }
  3895. }
  3896. #endif
  3897. *p_buf = p;
  3898. return true;
  3899. fail:
  3900. return false;
  3901. }
  3902. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3903. static bool
  3904. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3905. WASMModule *module, WASMTableSeg *table_segment,
  3906. char *error_buf, uint32 error_buf_size)
  3907. {
  3908. const uint8 *p = *p_buf, *p_end = buf_end;
  3909. uint32 function_count, function_index = 0, i;
  3910. uint64 total_size;
  3911. read_leb_uint32(p, p_end, function_count);
  3912. table_segment->value_count = function_count;
  3913. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3914. if (total_size > 0
  3915. && !(table_segment->init_values =
  3916. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3917. error_buf_size))) {
  3918. return false;
  3919. }
  3920. for (i = 0; i < function_count; i++) {
  3921. InitializerExpression *init_expr = &table_segment->init_values[i];
  3922. read_leb_uint32(p, p_end, function_index);
  3923. if (!check_function_index(module, function_index, error_buf,
  3924. error_buf_size)) {
  3925. return false;
  3926. }
  3927. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3928. init_expr->u.ref_index = function_index;
  3929. }
  3930. *p_buf = p;
  3931. return true;
  3932. fail:
  3933. return false;
  3934. }
  3935. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3936. static bool
  3937. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3938. WASMModule *module, WASMTableSeg *table_segment,
  3939. char *error_buf, uint32 error_buf_size)
  3940. {
  3941. const uint8 *p = *p_buf, *p_end = buf_end;
  3942. uint32 ref_count, i;
  3943. uint64 total_size;
  3944. read_leb_uint32(p, p_end, ref_count);
  3945. table_segment->value_count = ref_count;
  3946. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3947. if (total_size > 0
  3948. && !(table_segment->init_values =
  3949. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3950. error_buf_size))) {
  3951. return false;
  3952. }
  3953. for (i = 0; i < ref_count; i++) {
  3954. InitializerExpression *init_expr = &table_segment->init_values[i];
  3955. if (!load_init_expr(module, &p, p_end, init_expr,
  3956. table_segment->elem_type,
  3957. #if WASM_ENABLE_GC == 0
  3958. NULL,
  3959. #else
  3960. table_segment->elem_ref_type,
  3961. #endif
  3962. error_buf, error_buf_size))
  3963. return false;
  3964. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3965. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3966. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3967. && init_expr->init_expr_type
  3968. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3969. }
  3970. *p_buf = p;
  3971. return true;
  3972. fail:
  3973. return false;
  3974. }
  3975. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3976. static bool
  3977. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3978. WASMModule *module, char *error_buf,
  3979. uint32 error_buf_size)
  3980. {
  3981. const uint8 *p = buf, *p_end = buf_end;
  3982. uint8 table_elem_idx_type;
  3983. uint32 table_segment_count, i;
  3984. uint64 total_size;
  3985. WASMTableSeg *table_segment;
  3986. read_leb_uint32(p, p_end, table_segment_count);
  3987. if (table_segment_count) {
  3988. module->table_seg_count = table_segment_count;
  3989. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3990. if (!(module->table_segments =
  3991. loader_malloc(total_size, error_buf, error_buf_size))) {
  3992. return false;
  3993. }
  3994. table_segment = module->table_segments;
  3995. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3996. if (p >= p_end) {
  3997. set_error_buf(error_buf, error_buf_size,
  3998. "invalid value type or "
  3999. "invalid elements segment kind");
  4000. return false;
  4001. }
  4002. table_elem_idx_type = VALUE_TYPE_I32;
  4003. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4004. read_leb_uint32(p, p_end, table_segment->mode);
  4005. /* last three bits */
  4006. table_segment->mode = table_segment->mode & 0x07;
  4007. switch (table_segment->mode) {
  4008. /* elemkind/elemtype + active */
  4009. case 0:
  4010. case 4:
  4011. {
  4012. #if WASM_ENABLE_GC != 0
  4013. if (table_segment->mode == 0) {
  4014. /* vec(funcidx), set elem type to (ref func) */
  4015. WASMRefType elem_ref_type = { 0 };
  4016. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4017. wasm_set_refheaptype_common(
  4018. &elem_ref_type.ref_ht_common, false,
  4019. HEAP_TYPE_FUNC);
  4020. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4021. module->ref_type_set, &elem_ref_type,
  4022. error_buf, error_buf_size)))
  4023. return false;
  4024. }
  4025. else {
  4026. /* vec(expr), set elem type to funcref */
  4027. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4028. }
  4029. #else
  4030. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4031. #endif
  4032. table_segment->table_index = 0;
  4033. if (!check_table_index(module, table_segment->table_index,
  4034. error_buf, error_buf_size))
  4035. return false;
  4036. #if WASM_ENABLE_MEMORY64 != 0
  4037. table_elem_idx_type =
  4038. is_table_64bit(module, table_segment->table_index)
  4039. ? VALUE_TYPE_I64
  4040. : VALUE_TYPE_I32;
  4041. #endif
  4042. if (!load_init_expr(module, &p, p_end,
  4043. &table_segment->base_offset,
  4044. table_elem_idx_type, NULL, error_buf,
  4045. error_buf_size))
  4046. return false;
  4047. if (table_segment->mode == 0) {
  4048. /* vec(funcidx) */
  4049. if (!load_func_index_vec(&p, p_end, module,
  4050. table_segment, error_buf,
  4051. error_buf_size))
  4052. return false;
  4053. }
  4054. else {
  4055. /* vec(expr) */
  4056. if (!load_init_expr_vec(&p, p_end, module,
  4057. table_segment, error_buf,
  4058. error_buf_size))
  4059. return false;
  4060. }
  4061. if (!check_table_elem_type(module,
  4062. table_segment->table_index,
  4063. table_segment->elem_type,
  4064. error_buf, error_buf_size))
  4065. return false;
  4066. break;
  4067. }
  4068. /* elemkind + passive/declarative */
  4069. case 1:
  4070. case 3:
  4071. if (!load_elem_type(module, &p, p_end,
  4072. &table_segment->elem_type,
  4073. #if WASM_ENABLE_GC != 0
  4074. &table_segment->elem_ref_type,
  4075. #endif
  4076. true, error_buf, error_buf_size))
  4077. return false;
  4078. /* vec(funcidx) */
  4079. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4080. error_buf, error_buf_size))
  4081. return false;
  4082. break;
  4083. /* elemkind/elemtype + table_idx + active */
  4084. case 2:
  4085. case 6:
  4086. if (!load_table_index(&p, p_end, module,
  4087. &table_segment->table_index,
  4088. error_buf, error_buf_size))
  4089. return false;
  4090. #if WASM_ENABLE_MEMORY64 != 0
  4091. table_elem_idx_type =
  4092. is_table_64bit(module, table_segment->table_index)
  4093. ? VALUE_TYPE_I64
  4094. : VALUE_TYPE_I32;
  4095. #endif
  4096. if (!load_init_expr(module, &p, p_end,
  4097. &table_segment->base_offset,
  4098. table_elem_idx_type, NULL, error_buf,
  4099. error_buf_size))
  4100. return false;
  4101. if (!load_elem_type(module, &p, p_end,
  4102. &table_segment->elem_type,
  4103. #if WASM_ENABLE_GC != 0
  4104. &table_segment->elem_ref_type,
  4105. #endif
  4106. table_segment->mode == 2 ? true : false,
  4107. error_buf, error_buf_size))
  4108. return false;
  4109. if (table_segment->mode == 2) {
  4110. /* vec(funcidx) */
  4111. if (!load_func_index_vec(&p, p_end, module,
  4112. table_segment, error_buf,
  4113. error_buf_size))
  4114. return false;
  4115. }
  4116. else {
  4117. /* vec(expr) */
  4118. if (!load_init_expr_vec(&p, p_end, module,
  4119. table_segment, error_buf,
  4120. error_buf_size))
  4121. return false;
  4122. }
  4123. if (!check_table_elem_type(module,
  4124. table_segment->table_index,
  4125. table_segment->elem_type,
  4126. error_buf, error_buf_size))
  4127. return false;
  4128. break;
  4129. case 5:
  4130. case 7:
  4131. if (!load_elem_type(module, &p, p_end,
  4132. &table_segment->elem_type,
  4133. #if WASM_ENABLE_GC != 0
  4134. &table_segment->elem_ref_type,
  4135. #endif
  4136. false, error_buf, error_buf_size))
  4137. return false;
  4138. /* vec(expr) */
  4139. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4140. error_buf, error_buf_size))
  4141. return false;
  4142. break;
  4143. default:
  4144. set_error_buf(error_buf, error_buf_size,
  4145. "unknown element segment kind");
  4146. return false;
  4147. }
  4148. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4149. /*
  4150. * like: 00 41 05 0b 04 00 01 00 01
  4151. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4152. */
  4153. if (!load_table_index(&p, p_end, module,
  4154. &table_segment->table_index, error_buf,
  4155. error_buf_size))
  4156. return false;
  4157. #if WASM_ENABLE_MEMORY64 != 0
  4158. table_elem_idx_type =
  4159. is_table_64bit(module, table_segment->table_index)
  4160. ? VALUE_TYPE_I64
  4161. : VALUE_TYPE_I32;
  4162. #endif
  4163. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4164. table_elem_idx_type, NULL, error_buf,
  4165. error_buf_size))
  4166. return false;
  4167. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4168. error_buf, error_buf_size))
  4169. return false;
  4170. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4171. if (!check_table_elem_type(module, table_segment->table_index,
  4172. table_segment->elem_type, error_buf,
  4173. error_buf_size))
  4174. return false;
  4175. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4176. #if WASM_ENABLE_MEMORY64 != 0
  4177. if (table_elem_idx_type == VALUE_TYPE_I64
  4178. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4179. set_error_buf(error_buf, error_buf_size,
  4180. "In table64, table base offset can't be "
  4181. "larger than UINT32_MAX");
  4182. return false;
  4183. }
  4184. #endif
  4185. #if WASM_ENABLE_WAMR_COMPILER != 0
  4186. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4187. module->is_ref_types_used = true;
  4188. #endif
  4189. }
  4190. }
  4191. if (p != p_end) {
  4192. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4193. return false;
  4194. }
  4195. LOG_VERBOSE("Load table segment section success.\n");
  4196. return true;
  4197. fail:
  4198. return false;
  4199. }
  4200. static bool
  4201. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4202. WASMModule *module,
  4203. #if WASM_ENABLE_BULK_MEMORY != 0
  4204. bool has_datacount_section,
  4205. #endif
  4206. bool clone_data_seg, char *error_buf,
  4207. uint32 error_buf_size)
  4208. {
  4209. const uint8 *p = buf, *p_end = buf_end;
  4210. uint32 data_seg_count, i, mem_index, data_seg_len;
  4211. uint64 total_size;
  4212. WASMDataSeg *dataseg;
  4213. InitializerExpression init_expr;
  4214. #if WASM_ENABLE_BULK_MEMORY != 0
  4215. bool is_passive = false;
  4216. uint32 mem_flag;
  4217. #endif
  4218. uint8 mem_offset_type = VALUE_TYPE_I32;
  4219. read_leb_uint32(p, p_end, data_seg_count);
  4220. #if WASM_ENABLE_BULK_MEMORY != 0
  4221. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4222. set_error_buf(error_buf, error_buf_size,
  4223. "data count and data section have inconsistent lengths");
  4224. return false;
  4225. }
  4226. #endif
  4227. if (data_seg_count) {
  4228. module->data_seg_count = data_seg_count;
  4229. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4230. if (!(module->data_segments =
  4231. loader_malloc(total_size, error_buf, error_buf_size))) {
  4232. return false;
  4233. }
  4234. for (i = 0; i < data_seg_count; i++) {
  4235. read_leb_uint32(p, p_end, mem_index);
  4236. #if WASM_ENABLE_BULK_MEMORY != 0
  4237. is_passive = false;
  4238. mem_flag = mem_index & 0x03;
  4239. switch (mem_flag) {
  4240. case 0x01:
  4241. is_passive = true;
  4242. #if WASM_ENABLE_WAMR_COMPILER != 0
  4243. module->is_bulk_memory_used = true;
  4244. #endif
  4245. break;
  4246. case 0x00:
  4247. /* no memory index, treat index as 0 */
  4248. mem_index = 0;
  4249. goto check_mem_index;
  4250. case 0x02:
  4251. /* read following memory index */
  4252. read_leb_uint32(p, p_end, mem_index);
  4253. #if WASM_ENABLE_WAMR_COMPILER != 0
  4254. module->is_bulk_memory_used = true;
  4255. #endif
  4256. check_mem_index:
  4257. if (mem_index
  4258. >= module->import_memory_count + module->memory_count) {
  4259. set_error_buf_v(error_buf, error_buf_size,
  4260. "unknown memory %d", mem_index);
  4261. return false;
  4262. }
  4263. break;
  4264. case 0x03:
  4265. default:
  4266. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4267. return false;
  4268. break;
  4269. }
  4270. #else
  4271. if (mem_index
  4272. >= module->import_memory_count + module->memory_count) {
  4273. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4274. mem_index);
  4275. return false;
  4276. }
  4277. #endif /* WASM_ENABLE_BULK_MEMORY */
  4278. #if WASM_ENABLE_BULK_MEMORY != 0
  4279. if (!is_passive)
  4280. #endif
  4281. {
  4282. #if WASM_ENABLE_MEMORY64 != 0
  4283. /* This memory_flag is from memory instead of data segment */
  4284. uint8 memory_flag;
  4285. if (module->import_memory_count > 0) {
  4286. memory_flag = module->import_memories[mem_index]
  4287. .u.memory.mem_type.flags;
  4288. }
  4289. else {
  4290. memory_flag =
  4291. module
  4292. ->memories[mem_index - module->import_memory_count]
  4293. .flags;
  4294. }
  4295. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4296. : VALUE_TYPE_I32;
  4297. #else
  4298. mem_offset_type = VALUE_TYPE_I32;
  4299. #endif
  4300. }
  4301. #if WASM_ENABLE_BULK_MEMORY != 0
  4302. if (!is_passive)
  4303. #endif
  4304. if (!load_init_expr(module, &p, p_end, &init_expr,
  4305. mem_offset_type, NULL, error_buf,
  4306. error_buf_size))
  4307. return false;
  4308. read_leb_uint32(p, p_end, data_seg_len);
  4309. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4310. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4311. return false;
  4312. }
  4313. #if WASM_ENABLE_BULK_MEMORY != 0
  4314. dataseg->is_passive = is_passive;
  4315. if (!is_passive)
  4316. #endif
  4317. {
  4318. bh_memcpy_s(&dataseg->base_offset,
  4319. sizeof(InitializerExpression), &init_expr,
  4320. sizeof(InitializerExpression));
  4321. dataseg->memory_index = mem_index;
  4322. }
  4323. dataseg->data_length = data_seg_len;
  4324. CHECK_BUF(p, p_end, data_seg_len);
  4325. if (clone_data_seg) {
  4326. if (!(dataseg->data = loader_malloc(
  4327. dataseg->data_length, error_buf, error_buf_size))) {
  4328. return false;
  4329. }
  4330. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4331. data_seg_len);
  4332. }
  4333. else {
  4334. dataseg->data = (uint8 *)p;
  4335. }
  4336. dataseg->is_data_cloned = clone_data_seg;
  4337. p += data_seg_len;
  4338. }
  4339. }
  4340. if (p != p_end) {
  4341. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4342. return false;
  4343. }
  4344. LOG_VERBOSE("Load data segment section success.\n");
  4345. return true;
  4346. fail:
  4347. return false;
  4348. }
  4349. #if WASM_ENABLE_BULK_MEMORY != 0
  4350. static bool
  4351. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4352. WASMModule *module, char *error_buf,
  4353. uint32 error_buf_size)
  4354. {
  4355. const uint8 *p = buf, *p_end = buf_end;
  4356. uint32 data_seg_count1 = 0;
  4357. read_leb_uint32(p, p_end, data_seg_count1);
  4358. module->data_seg_count1 = data_seg_count1;
  4359. if (p != p_end) {
  4360. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4361. return false;
  4362. }
  4363. #if WASM_ENABLE_WAMR_COMPILER != 0
  4364. module->is_bulk_memory_used = true;
  4365. #endif
  4366. LOG_VERBOSE("Load datacount section success.\n");
  4367. return true;
  4368. fail:
  4369. return false;
  4370. }
  4371. #endif
  4372. #if WASM_ENABLE_TAGS != 0
  4373. static bool
  4374. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4375. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4376. uint32 error_buf_size)
  4377. {
  4378. (void)buf_code;
  4379. (void)buf_code_end;
  4380. const uint8 *p = buf, *p_end = buf_end;
  4381. size_t total_size = 0;
  4382. /* number of tags defined in the section */
  4383. uint32 section_tag_count = 0;
  4384. uint8 tag_attribute;
  4385. uint32 tag_type;
  4386. WASMTag *tag = NULL;
  4387. /* get tag count */
  4388. read_leb_uint32(p, p_end, section_tag_count);
  4389. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4390. error_buf, error_buf_size))
  4391. return false;
  4392. module->tag_count = section_tag_count;
  4393. if (section_tag_count) {
  4394. total_size = sizeof(WASMTag *) * module->tag_count;
  4395. if (!(module->tags =
  4396. loader_malloc(total_size, error_buf, error_buf_size))) {
  4397. return false;
  4398. }
  4399. /* load each tag, imported tags precede the tags */
  4400. uint32 tag_index;
  4401. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4402. /* get the one byte attribute */
  4403. CHECK_BUF(p, p_end, 1);
  4404. tag_attribute = read_uint8(p);
  4405. /* get type */
  4406. read_leb_uint32(p, p_end, tag_type);
  4407. /* compare against module->types */
  4408. if (tag_type >= module->type_count) {
  4409. set_error_buf(error_buf, error_buf_size, "unknown type");
  4410. return false;
  4411. }
  4412. /* get return type (must be 0) */
  4413. /* check, that the type of the referred tag returns void */
  4414. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4415. if (func_type->result_count != 0) {
  4416. set_error_buf(error_buf, error_buf_size,
  4417. "non-empty tag result type");
  4418. goto fail;
  4419. }
  4420. if (!(tag = module->tags[tag_index] = loader_malloc(
  4421. sizeof(WASMTag), error_buf, error_buf_size))) {
  4422. return false;
  4423. }
  4424. /* store to module tag declarations */
  4425. tag->attribute = tag_attribute;
  4426. tag->type = tag_type;
  4427. tag->tag_type = func_type;
  4428. }
  4429. }
  4430. if (p != p_end) {
  4431. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4432. return false;
  4433. }
  4434. LOG_VERBOSE("Load tag section success.\n");
  4435. return true;
  4436. fail:
  4437. return false;
  4438. }
  4439. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4440. static bool
  4441. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4442. const uint8 *buf_func_end, WASMModule *module,
  4443. char *error_buf, uint32 error_buf_size)
  4444. {
  4445. const uint8 *p = buf, *p_end = buf_end;
  4446. const uint8 *p_func = buf_func;
  4447. uint32 func_count = 0, code_count;
  4448. /* code has been loaded in function section, so pass it here, just check
  4449. * whether function and code section have inconsistent lengths */
  4450. read_leb_uint32(p, p_end, code_count);
  4451. if (buf_func)
  4452. read_leb_uint32(p_func, buf_func_end, func_count);
  4453. if (func_count != code_count) {
  4454. set_error_buf(error_buf, error_buf_size,
  4455. "function and code section have inconsistent lengths");
  4456. return false;
  4457. }
  4458. LOG_VERBOSE("Load code segment section success.\n");
  4459. (void)module;
  4460. return true;
  4461. fail:
  4462. return false;
  4463. }
  4464. static bool
  4465. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4466. char *error_buf, uint32 error_buf_size)
  4467. {
  4468. const uint8 *p = buf, *p_end = buf_end;
  4469. WASMFuncType *type;
  4470. uint32 start_function;
  4471. read_leb_uint32(p, p_end, start_function);
  4472. if (start_function
  4473. >= module->function_count + module->import_function_count) {
  4474. set_error_buf(error_buf, error_buf_size, "unknown function");
  4475. return false;
  4476. }
  4477. if (start_function < module->import_function_count)
  4478. type = module->import_functions[start_function].u.function.func_type;
  4479. else
  4480. type = module->functions[start_function - module->import_function_count]
  4481. ->func_type;
  4482. if (type->param_count != 0 || type->result_count != 0) {
  4483. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4484. return false;
  4485. }
  4486. module->start_function = start_function;
  4487. if (p != p_end) {
  4488. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4489. return false;
  4490. }
  4491. LOG_VERBOSE("Load start section success.\n");
  4492. return true;
  4493. fail:
  4494. return false;
  4495. }
  4496. #if WASM_ENABLE_STRINGREF != 0
  4497. static bool
  4498. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4499. WASMModule *module, bool is_load_from_file_buf,
  4500. char *error_buf, uint32 error_buf_size)
  4501. {
  4502. const uint8 *p = buf, *p_end = buf_end;
  4503. int32 deferred_count, immediate_count, string_length, i;
  4504. uint64 total_size;
  4505. read_leb_uint32(p, p_end, deferred_count);
  4506. read_leb_uint32(p, p_end, immediate_count);
  4507. /* proposal set deferred_count for future extension */
  4508. if (deferred_count != 0) {
  4509. goto fail;
  4510. }
  4511. if (immediate_count > 0) {
  4512. total_size = sizeof(char *) * (uint64)immediate_count;
  4513. if (!(module->string_literal_ptrs =
  4514. loader_malloc(total_size, error_buf, error_buf_size))) {
  4515. goto fail;
  4516. }
  4517. module->string_literal_count = immediate_count;
  4518. total_size = sizeof(uint32) * (uint64)immediate_count;
  4519. if (!(module->string_literal_lengths =
  4520. loader_malloc(total_size, error_buf, error_buf_size))) {
  4521. goto fail;
  4522. }
  4523. for (i = 0; i < immediate_count; i++) {
  4524. read_leb_uint32(p, p_end, string_length);
  4525. CHECK_BUF(p, p_end, string_length);
  4526. module->string_literal_ptrs[i] = p;
  4527. module->string_literal_lengths[i] = string_length;
  4528. p += string_length;
  4529. }
  4530. }
  4531. if (p != p_end) {
  4532. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4533. goto fail;
  4534. }
  4535. LOG_VERBOSE("Load stringref section success.\n");
  4536. return true;
  4537. fail:
  4538. return false;
  4539. }
  4540. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4541. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4542. static bool
  4543. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4544. bool is_load_from_file_buf, char *error_buf,
  4545. uint32 error_buf_size)
  4546. {
  4547. const uint8 *p = buf, *p_end = buf_end;
  4548. uint32 name_type, subsection_size;
  4549. uint32 previous_name_type = 0;
  4550. uint32 num_func_name;
  4551. uint32 func_index;
  4552. uint32 previous_func_index = ~0U;
  4553. uint32 func_name_len;
  4554. uint32 name_index;
  4555. int i = 0;
  4556. if (p >= p_end) {
  4557. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4558. return false;
  4559. }
  4560. while (p < p_end) {
  4561. read_leb_uint32(p, p_end, name_type);
  4562. if (i != 0) {
  4563. if (name_type == previous_name_type) {
  4564. set_error_buf(error_buf, error_buf_size,
  4565. "duplicate sub-section");
  4566. return false;
  4567. }
  4568. if (name_type < previous_name_type) {
  4569. set_error_buf(error_buf, error_buf_size,
  4570. "out-of-order sub-section");
  4571. return false;
  4572. }
  4573. }
  4574. previous_name_type = name_type;
  4575. read_leb_uint32(p, p_end, subsection_size);
  4576. CHECK_BUF(p, p_end, subsection_size);
  4577. switch (name_type) {
  4578. case SUB_SECTION_TYPE_FUNC:
  4579. if (subsection_size) {
  4580. read_leb_uint32(p, p_end, num_func_name);
  4581. for (name_index = 0; name_index < num_func_name;
  4582. name_index++) {
  4583. read_leb_uint32(p, p_end, func_index);
  4584. if (func_index == previous_func_index) {
  4585. set_error_buf(error_buf, error_buf_size,
  4586. "duplicate function name");
  4587. return false;
  4588. }
  4589. if (func_index < previous_func_index
  4590. && previous_func_index != ~0U) {
  4591. set_error_buf(error_buf, error_buf_size,
  4592. "out-of-order function index ");
  4593. return false;
  4594. }
  4595. previous_func_index = func_index;
  4596. read_leb_uint32(p, p_end, func_name_len);
  4597. CHECK_BUF(p, p_end, func_name_len);
  4598. /* Skip the import functions */
  4599. if (func_index >= module->import_function_count) {
  4600. func_index -= module->import_function_count;
  4601. if (func_index >= module->function_count) {
  4602. set_error_buf(error_buf, error_buf_size,
  4603. "out-of-range function index");
  4604. return false;
  4605. }
  4606. if (!(module->functions[func_index]->field_name =
  4607. wasm_const_str_list_insert(
  4608. p, func_name_len, module,
  4609. #if WASM_ENABLE_WAMR_COMPILER != 0
  4610. false,
  4611. #else
  4612. is_load_from_file_buf,
  4613. #endif
  4614. error_buf, error_buf_size))) {
  4615. return false;
  4616. }
  4617. }
  4618. p += func_name_len;
  4619. }
  4620. }
  4621. break;
  4622. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4623. */
  4624. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4625. default:
  4626. p = p + subsection_size;
  4627. break;
  4628. }
  4629. i++;
  4630. }
  4631. return true;
  4632. fail:
  4633. return false;
  4634. }
  4635. #endif
  4636. static bool
  4637. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4638. bool is_load_from_file_buf, char *error_buf,
  4639. uint32 error_buf_size)
  4640. {
  4641. const uint8 *p = buf, *p_end = buf_end;
  4642. char section_name[32];
  4643. uint32 name_len, buffer_len;
  4644. if (p >= p_end) {
  4645. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4646. return false;
  4647. }
  4648. read_leb_uint32(p, p_end, name_len);
  4649. if (p + name_len > p_end) {
  4650. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4651. return false;
  4652. }
  4653. if (!wasm_check_utf8_str(p, name_len)) {
  4654. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4655. return false;
  4656. }
  4657. buffer_len = sizeof(section_name);
  4658. memset(section_name, 0, buffer_len);
  4659. if (name_len < buffer_len) {
  4660. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4661. }
  4662. else {
  4663. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4664. memset(section_name + buffer_len - 4, '.', 3);
  4665. }
  4666. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4667. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4668. module->name_section_buf = buf;
  4669. module->name_section_buf_end = buf_end;
  4670. p += name_len;
  4671. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4672. error_buf, error_buf_size)) {
  4673. return false;
  4674. }
  4675. LOG_VERBOSE("Load custom name section success.");
  4676. }
  4677. #endif
  4678. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4679. {
  4680. WASMCustomSection *section =
  4681. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4682. if (!section) {
  4683. return false;
  4684. }
  4685. section->name_addr = (char *)p;
  4686. section->name_len = name_len;
  4687. section->content_addr = (uint8 *)(p + name_len);
  4688. section->content_len = (uint32)(p_end - p - name_len);
  4689. section->next = module->custom_section_list;
  4690. module->custom_section_list = section;
  4691. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4692. return true;
  4693. }
  4694. #endif
  4695. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4696. (void)is_load_from_file_buf;
  4697. (void)module;
  4698. return true;
  4699. fail:
  4700. return false;
  4701. }
  4702. static void
  4703. calculate_global_data_offset(WASMModule *module)
  4704. {
  4705. uint32 i, data_offset;
  4706. data_offset = 0;
  4707. for (i = 0; i < module->import_global_count; i++) {
  4708. WASMGlobalImport *import_global =
  4709. &((module->import_globals + i)->u.global);
  4710. #if WASM_ENABLE_FAST_JIT != 0
  4711. import_global->data_offset = data_offset;
  4712. #endif
  4713. data_offset += wasm_value_type_size(import_global->type.val_type);
  4714. }
  4715. for (i = 0; i < module->global_count; i++) {
  4716. WASMGlobal *global = module->globals + i;
  4717. #if WASM_ENABLE_FAST_JIT != 0
  4718. global->data_offset = data_offset;
  4719. #endif
  4720. data_offset += wasm_value_type_size(global->type.val_type);
  4721. }
  4722. module->global_data_size = data_offset;
  4723. }
  4724. #if WASM_ENABLE_FAST_JIT != 0
  4725. static bool
  4726. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4727. uint32 error_buf_size)
  4728. {
  4729. #if WASM_ENABLE_LAZY_JIT != 0
  4730. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4731. #endif
  4732. uint32 i;
  4733. if (!module->function_count)
  4734. return true;
  4735. if (!(module->fast_jit_func_ptrs =
  4736. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4737. error_buf_size))) {
  4738. return false;
  4739. }
  4740. #if WASM_ENABLE_LAZY_JIT != 0
  4741. for (i = 0; i < module->function_count; i++) {
  4742. module->fast_jit_func_ptrs[i] =
  4743. jit_globals->compile_fast_jit_and_then_call;
  4744. }
  4745. #endif
  4746. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4747. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4748. set_error_buf(error_buf, error_buf_size,
  4749. "init fast jit thread lock failed");
  4750. return false;
  4751. }
  4752. module->fast_jit_thread_locks_inited[i] = true;
  4753. }
  4754. return true;
  4755. }
  4756. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4757. #if WASM_ENABLE_JIT != 0
  4758. static bool
  4759. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4760. uint32 error_buf_size)
  4761. {
  4762. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4763. AOTCompOption option = { 0 };
  4764. char *aot_last_error;
  4765. uint64 size;
  4766. #if WASM_ENABLE_GC != 0
  4767. bool gc_enabled = true;
  4768. #else
  4769. bool gc_enabled = false;
  4770. #endif
  4771. if (module->function_count == 0)
  4772. return true;
  4773. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4774. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4775. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4776. return false;
  4777. }
  4778. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4779. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4780. os_mutex_destroy(&module->tierup_wait_lock);
  4781. return false;
  4782. }
  4783. module->tierup_wait_lock_inited = true;
  4784. #endif
  4785. size = sizeof(void *) * (uint64)module->function_count
  4786. + sizeof(bool) * (uint64)module->function_count;
  4787. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4788. return false;
  4789. }
  4790. module->func_ptrs_compiled =
  4791. (bool *)((uint8 *)module->func_ptrs
  4792. + sizeof(void *) * module->function_count);
  4793. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4794. if (!module->comp_data) {
  4795. aot_last_error = aot_get_last_error();
  4796. bh_assert(aot_last_error != NULL);
  4797. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4798. return false;
  4799. }
  4800. option.is_jit_mode = true;
  4801. option.opt_level = llvm_jit_options->opt_level;
  4802. option.size_level = llvm_jit_options->size_level;
  4803. option.segue_flags = llvm_jit_options->segue_flags;
  4804. option.quick_invoke_c_api_import =
  4805. llvm_jit_options->quick_invoke_c_api_import;
  4806. #if WASM_ENABLE_BULK_MEMORY != 0
  4807. option.enable_bulk_memory = true;
  4808. #endif
  4809. #if WASM_ENABLE_THREAD_MGR != 0
  4810. option.enable_thread_mgr = true;
  4811. #endif
  4812. #if WASM_ENABLE_TAIL_CALL != 0
  4813. option.enable_tail_call = true;
  4814. #endif
  4815. #if WASM_ENABLE_SIMD != 0
  4816. option.enable_simd = true;
  4817. #endif
  4818. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4819. option.enable_ref_types = true;
  4820. #elif WASM_ENABLE_GC != 0
  4821. option.enable_gc = true;
  4822. #endif
  4823. option.enable_aux_stack_check = true;
  4824. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4825. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4826. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4827. aot_call_stack_features_init_default(&option.call_stack_features);
  4828. #endif
  4829. #if WASM_ENABLE_PERF_PROFILING != 0
  4830. option.enable_perf_profiling = true;
  4831. #endif
  4832. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4833. option.enable_memory_profiling = true;
  4834. option.enable_stack_estimation = true;
  4835. #endif
  4836. #if WASM_ENABLE_SHARED_HEAP != 0
  4837. option.enable_shared_heap = true;
  4838. #endif
  4839. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4840. if (!module->comp_ctx) {
  4841. aot_last_error = aot_get_last_error();
  4842. bh_assert(aot_last_error != NULL);
  4843. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4844. return false;
  4845. }
  4846. return true;
  4847. }
  4848. static bool
  4849. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4850. uint32 error_buf_size)
  4851. {
  4852. char *aot_last_error;
  4853. uint32 i;
  4854. if (module->function_count == 0)
  4855. return true;
  4856. if (!aot_compile_wasm(module->comp_ctx)) {
  4857. aot_last_error = aot_get_last_error();
  4858. bh_assert(aot_last_error != NULL);
  4859. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4860. return false;
  4861. }
  4862. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4863. if (module->orcjit_stop_compiling)
  4864. return false;
  4865. #endif
  4866. bh_print_time("Begin to lookup llvm jit functions");
  4867. for (i = 0; i < module->function_count; i++) {
  4868. LLVMOrcJITTargetAddress func_addr = 0;
  4869. LLVMErrorRef error;
  4870. char func_name[48];
  4871. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4872. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4873. func_name);
  4874. if (error != LLVMErrorSuccess) {
  4875. char *err_msg = LLVMGetErrorMessage(error);
  4876. set_error_buf_v(error_buf, error_buf_size,
  4877. "failed to compile llvm jit function: %s", err_msg);
  4878. LLVMDisposeErrorMessage(err_msg);
  4879. return false;
  4880. }
  4881. /**
  4882. * No need to lock the func_ptr[func_idx] here as it is basic
  4883. * data type, the load/store for it can be finished by one cpu
  4884. * instruction, and there can be only one cpu instruction
  4885. * loading/storing at the same time.
  4886. */
  4887. module->func_ptrs[i] = (void *)func_addr;
  4888. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4889. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4890. if (module->orcjit_stop_compiling)
  4891. return false;
  4892. #endif
  4893. }
  4894. bh_print_time("End lookup llvm jit functions");
  4895. return true;
  4896. }
  4897. #endif /* end of WASM_ENABLE_JIT != 0 */
  4898. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4899. && WASM_ENABLE_LAZY_JIT != 0
  4900. static void *
  4901. init_llvm_jit_functions_stage2_callback(void *arg)
  4902. {
  4903. WASMModule *module = (WASMModule *)arg;
  4904. char error_buf[128];
  4905. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4906. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4907. module->orcjit_stop_compiling = true;
  4908. return NULL;
  4909. }
  4910. os_mutex_lock(&module->tierup_wait_lock);
  4911. module->llvm_jit_inited = true;
  4912. os_cond_broadcast(&module->tierup_wait_cond);
  4913. os_mutex_unlock(&module->tierup_wait_lock);
  4914. return NULL;
  4915. }
  4916. #endif
  4917. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4918. /* The callback function to compile jit functions */
  4919. static void *
  4920. orcjit_thread_callback(void *arg)
  4921. {
  4922. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4923. #if WASM_ENABLE_JIT != 0
  4924. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4925. #endif
  4926. WASMModule *module = thread_arg->module;
  4927. uint32 group_idx = thread_arg->group_idx;
  4928. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4929. uint32 func_count = module->function_count;
  4930. uint32 i;
  4931. #if WASM_ENABLE_FAST_JIT != 0
  4932. /* Compile fast jit functions of this group */
  4933. for (i = group_idx; i < func_count; i += group_stride) {
  4934. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4935. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4936. break;
  4937. }
  4938. if (module->orcjit_stop_compiling) {
  4939. return NULL;
  4940. }
  4941. }
  4942. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4943. os_mutex_lock(&module->tierup_wait_lock);
  4944. module->fast_jit_ready_groups++;
  4945. os_mutex_unlock(&module->tierup_wait_lock);
  4946. #endif
  4947. #endif
  4948. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4949. && WASM_ENABLE_LAZY_JIT != 0
  4950. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4951. for (i = group_idx; i < func_count;
  4952. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4953. uint32 j;
  4954. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4955. if (i + j * group_stride < func_count) {
  4956. if (!jit_compiler_set_call_to_fast_jit(
  4957. module,
  4958. i + j * group_stride + module->import_function_count)) {
  4959. LOG_ERROR(
  4960. "failed to compile call_to_fast_jit for func %u\n",
  4961. i + j * group_stride + module->import_function_count);
  4962. module->orcjit_stop_compiling = true;
  4963. return NULL;
  4964. }
  4965. }
  4966. if (module->orcjit_stop_compiling) {
  4967. return NULL;
  4968. }
  4969. }
  4970. }
  4971. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4972. fast jit functions are compiled */
  4973. os_mutex_lock(&module->tierup_wait_lock);
  4974. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4975. && module->fast_jit_ready_groups >= group_stride)) {
  4976. os_cond_reltimedwait(&module->tierup_wait_cond,
  4977. &module->tierup_wait_lock, 10000);
  4978. if (module->orcjit_stop_compiling) {
  4979. /* init_llvm_jit_functions_stage2 failed */
  4980. os_mutex_unlock(&module->tierup_wait_lock);
  4981. return NULL;
  4982. }
  4983. }
  4984. os_mutex_unlock(&module->tierup_wait_lock);
  4985. #endif
  4986. #if WASM_ENABLE_JIT != 0
  4987. /* Compile llvm jit functions of this group */
  4988. for (i = group_idx; i < func_count;
  4989. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4990. LLVMOrcJITTargetAddress func_addr = 0;
  4991. LLVMErrorRef error;
  4992. char func_name[48];
  4993. typedef void (*F)(void);
  4994. union {
  4995. F f;
  4996. void *v;
  4997. } u;
  4998. uint32 j;
  4999. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5000. "_wrapper");
  5001. LOG_DEBUG("compile llvm jit func %s", func_name);
  5002. error =
  5003. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5004. if (error != LLVMErrorSuccess) {
  5005. char *err_msg = LLVMGetErrorMessage(error);
  5006. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5007. LLVMDisposeErrorMessage(err_msg);
  5008. break;
  5009. }
  5010. /* Call the jit wrapper function to trigger its compilation, so as
  5011. to compile the actual jit functions, since we add the latter to
  5012. function list in the PartitionFunction callback */
  5013. u.v = (void *)func_addr;
  5014. u.f();
  5015. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5016. if (i + j * group_stride < func_count) {
  5017. module->func_ptrs_compiled[i + j * group_stride] = true;
  5018. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5019. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5020. i + j * group_stride);
  5021. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5022. func_name);
  5023. if (error != LLVMErrorSuccess) {
  5024. char *err_msg = LLVMGetErrorMessage(error);
  5025. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5026. err_msg);
  5027. LLVMDisposeErrorMessage(err_msg);
  5028. /* Ignore current llvm jit func, as its func ptr is
  5029. previous set to call_to_fast_jit, which also works */
  5030. continue;
  5031. }
  5032. jit_compiler_set_llvm_jit_func_ptr(
  5033. module,
  5034. i + j * group_stride + module->import_function_count,
  5035. (void *)func_addr);
  5036. /* Try to switch to call this llvm jit function instead of
  5037. fast jit function from fast jit jitted code */
  5038. jit_compiler_set_call_to_llvm_jit(
  5039. module,
  5040. i + j * group_stride + module->import_function_count);
  5041. #endif
  5042. }
  5043. }
  5044. if (module->orcjit_stop_compiling) {
  5045. break;
  5046. }
  5047. }
  5048. #endif
  5049. return NULL;
  5050. }
  5051. static void
  5052. orcjit_stop_compile_threads(WASMModule *module)
  5053. {
  5054. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5055. / sizeof(OrcJitThreadArg));
  5056. module->orcjit_stop_compiling = true;
  5057. for (i = 0; i < thread_num; i++) {
  5058. if (module->orcjit_threads[i])
  5059. os_thread_join(module->orcjit_threads[i], NULL);
  5060. }
  5061. }
  5062. static bool
  5063. compile_jit_functions(WASMModule *module, char *error_buf,
  5064. uint32 error_buf_size)
  5065. {
  5066. uint32 thread_num =
  5067. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5068. uint32 i, j;
  5069. bh_print_time("Begin to compile jit functions");
  5070. /* Create threads to compile the jit functions */
  5071. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5072. #if WASM_ENABLE_JIT != 0
  5073. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5074. #endif
  5075. module->orcjit_thread_args[i].module = module;
  5076. module->orcjit_thread_args[i].group_idx = i;
  5077. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5078. (void *)&module->orcjit_thread_args[i],
  5079. APP_THREAD_STACK_SIZE_DEFAULT)
  5080. != 0) {
  5081. set_error_buf(error_buf, error_buf_size,
  5082. "create orcjit compile thread failed");
  5083. /* Terminate the threads created */
  5084. module->orcjit_stop_compiling = true;
  5085. for (j = 0; j < i; j++) {
  5086. os_thread_join(module->orcjit_threads[j], NULL);
  5087. }
  5088. return false;
  5089. }
  5090. }
  5091. #if WASM_ENABLE_LAZY_JIT == 0
  5092. /* Wait until all jit functions are compiled for eager mode */
  5093. for (i = 0; i < thread_num; i++) {
  5094. if (module->orcjit_threads[i])
  5095. os_thread_join(module->orcjit_threads[i], NULL);
  5096. }
  5097. #if WASM_ENABLE_FAST_JIT != 0
  5098. /* Ensure all the fast-jit functions are compiled */
  5099. for (i = 0; i < module->function_count; i++) {
  5100. if (!jit_compiler_is_compiled(module,
  5101. i + module->import_function_count)) {
  5102. set_error_buf(error_buf, error_buf_size,
  5103. "failed to compile fast jit function");
  5104. return false;
  5105. }
  5106. }
  5107. #endif
  5108. #if WASM_ENABLE_JIT != 0
  5109. /* Ensure all the llvm-jit functions are compiled */
  5110. for (i = 0; i < module->function_count; i++) {
  5111. if (!module->func_ptrs_compiled[i]) {
  5112. set_error_buf(error_buf, error_buf_size,
  5113. "failed to compile llvm jit function");
  5114. return false;
  5115. }
  5116. }
  5117. #endif
  5118. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5119. bh_print_time("End compile jit functions");
  5120. return true;
  5121. }
  5122. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5123. static bool
  5124. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5125. uint32 cur_func_idx, char *error_buf,
  5126. uint32 error_buf_size);
  5127. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5128. void **
  5129. wasm_interp_get_handle_table(void);
  5130. static void **handle_table;
  5131. #endif
  5132. static bool
  5133. load_from_sections(WASMModule *module, WASMSection *sections,
  5134. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5135. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5136. {
  5137. WASMExport *export;
  5138. WASMSection *section = sections;
  5139. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5140. *buf_func = NULL, *buf_func_end = NULL;
  5141. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5142. WASMGlobal *aux_stack_top_global = NULL, *global;
  5143. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5144. aux_stack_top = (uint64)-1LL;
  5145. uint32 global_index, func_index, i;
  5146. uint32 aux_data_end_global_index = (uint32)-1;
  5147. uint32 aux_heap_base_global_index = (uint32)-1;
  5148. WASMFuncType *func_type;
  5149. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5150. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5151. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5152. #if WASM_ENABLE_BULK_MEMORY != 0
  5153. bool has_datacount_section = false;
  5154. #endif
  5155. /* Find code and function sections if have */
  5156. while (section) {
  5157. if (section->section_type == SECTION_TYPE_CODE) {
  5158. buf_code = section->section_body;
  5159. buf_code_end = buf_code + section->section_body_size;
  5160. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5161. module->buf_code = (uint8 *)buf_code;
  5162. module->buf_code_size = section->section_body_size;
  5163. #endif
  5164. }
  5165. else if (section->section_type == SECTION_TYPE_FUNC) {
  5166. buf_func = section->section_body;
  5167. buf_func_end = buf_func + section->section_body_size;
  5168. }
  5169. section = section->next;
  5170. }
  5171. section = sections;
  5172. while (section) {
  5173. buf = section->section_body;
  5174. buf_end = buf + section->section_body_size;
  5175. switch (section->section_type) {
  5176. case SECTION_TYPE_USER:
  5177. /* unsupported user section, ignore it. */
  5178. if (!load_user_section(buf, buf_end, module,
  5179. reuse_const_strings, error_buf,
  5180. error_buf_size))
  5181. return false;
  5182. break;
  5183. case SECTION_TYPE_TYPE:
  5184. if (!load_type_section(buf, buf_end, module, error_buf,
  5185. error_buf_size))
  5186. return false;
  5187. break;
  5188. case SECTION_TYPE_IMPORT:
  5189. if (!load_import_section(buf, buf_end, module,
  5190. reuse_const_strings, no_resolve,
  5191. error_buf, error_buf_size))
  5192. return false;
  5193. break;
  5194. case SECTION_TYPE_FUNC:
  5195. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5196. module, error_buf, error_buf_size))
  5197. return false;
  5198. break;
  5199. case SECTION_TYPE_TABLE:
  5200. if (!load_table_section(buf, buf_end, module, error_buf,
  5201. error_buf_size))
  5202. return false;
  5203. break;
  5204. case SECTION_TYPE_MEMORY:
  5205. if (!load_memory_section(buf, buf_end, module, error_buf,
  5206. error_buf_size))
  5207. return false;
  5208. break;
  5209. #if WASM_ENABLE_TAGS != 0
  5210. case SECTION_TYPE_TAG:
  5211. /* load tag declaration section */
  5212. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5213. module, error_buf, error_buf_size))
  5214. return false;
  5215. break;
  5216. #endif
  5217. case SECTION_TYPE_GLOBAL:
  5218. if (!load_global_section(buf, buf_end, module, error_buf,
  5219. error_buf_size))
  5220. return false;
  5221. break;
  5222. case SECTION_TYPE_EXPORT:
  5223. if (!load_export_section(buf, buf_end, module,
  5224. reuse_const_strings, error_buf,
  5225. error_buf_size))
  5226. return false;
  5227. break;
  5228. case SECTION_TYPE_START:
  5229. if (!load_start_section(buf, buf_end, module, error_buf,
  5230. error_buf_size))
  5231. return false;
  5232. break;
  5233. case SECTION_TYPE_ELEM:
  5234. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5235. error_buf_size))
  5236. return false;
  5237. break;
  5238. case SECTION_TYPE_CODE:
  5239. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5240. module, error_buf, error_buf_size))
  5241. return false;
  5242. break;
  5243. case SECTION_TYPE_DATA:
  5244. if (!load_data_segment_section(buf, buf_end, module,
  5245. #if WASM_ENABLE_BULK_MEMORY != 0
  5246. has_datacount_section,
  5247. #endif
  5248. clone_data_seg, error_buf,
  5249. error_buf_size))
  5250. return false;
  5251. break;
  5252. #if WASM_ENABLE_BULK_MEMORY != 0
  5253. case SECTION_TYPE_DATACOUNT:
  5254. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5255. error_buf_size))
  5256. return false;
  5257. has_datacount_section = true;
  5258. break;
  5259. #endif
  5260. #if WASM_ENABLE_STRINGREF != 0
  5261. case SECTION_TYPE_STRINGREF:
  5262. if (!load_stringref_section(buf, buf_end, module,
  5263. reuse_const_strings, error_buf,
  5264. error_buf_size))
  5265. return false;
  5266. break;
  5267. #endif
  5268. default:
  5269. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5270. return false;
  5271. }
  5272. section = section->next;
  5273. }
  5274. module->aux_data_end_global_index = (uint32)-1;
  5275. module->aux_heap_base_global_index = (uint32)-1;
  5276. module->aux_stack_top_global_index = (uint32)-1;
  5277. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5278. export = module->exports;
  5279. for (i = 0; i < module->export_count; i++, export ++) {
  5280. if (export->kind == EXPORT_KIND_GLOBAL) {
  5281. if (!strcmp(export->name, "__heap_base")) {
  5282. global_index = export->index - module->import_global_count;
  5283. global = module->globals + global_index;
  5284. if (global->type.val_type == VALUE_TYPE_I32
  5285. && !global->type.is_mutable
  5286. && global->init_expr.init_expr_type
  5287. == INIT_EXPR_TYPE_I32_CONST) {
  5288. aux_heap_base_global = global;
  5289. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5290. aux_heap_base_global_index = export->index;
  5291. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5292. aux_heap_base);
  5293. }
  5294. }
  5295. else if (!strcmp(export->name, "__data_end")) {
  5296. global_index = export->index - module->import_global_count;
  5297. global = module->globals + global_index;
  5298. if (global->type.val_type == VALUE_TYPE_I32
  5299. && !global->type.is_mutable
  5300. && global->init_expr.init_expr_type
  5301. == INIT_EXPR_TYPE_I32_CONST) {
  5302. aux_data_end_global = global;
  5303. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5304. aux_data_end_global_index = export->index;
  5305. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5306. aux_data_end);
  5307. aux_data_end = align_uint64(aux_data_end, 16);
  5308. }
  5309. }
  5310. /* For module compiled with -pthread option, the global is:
  5311. [0] stack_top <-- 0
  5312. [1] tls_pointer
  5313. [2] tls_size
  5314. [3] data_end <-- 3
  5315. [4] global_base
  5316. [5] heap_base <-- 5
  5317. [6] dso_handle
  5318. For module compiled without -pthread option:
  5319. [0] stack_top <-- 0
  5320. [1] data_end <-- 1
  5321. [2] global_base
  5322. [3] heap_base <-- 3
  5323. [4] dso_handle
  5324. */
  5325. if (aux_data_end_global && aux_heap_base_global
  5326. && aux_data_end <= aux_heap_base) {
  5327. module->aux_data_end_global_index = aux_data_end_global_index;
  5328. module->aux_data_end = aux_data_end;
  5329. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5330. module->aux_heap_base = aux_heap_base;
  5331. /* Resolve aux stack top global */
  5332. for (global_index = 0; global_index < module->global_count;
  5333. global_index++) {
  5334. global = module->globals + global_index;
  5335. if (global->type.is_mutable /* heap_base and data_end is
  5336. not mutable */
  5337. && global->type.val_type == VALUE_TYPE_I32
  5338. && global->init_expr.init_expr_type
  5339. == INIT_EXPR_TYPE_I32_CONST
  5340. && (uint64)(uint32)global->init_expr.u.i32
  5341. <= aux_heap_base) {
  5342. aux_stack_top_global = global;
  5343. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5344. module->aux_stack_top_global_index =
  5345. module->import_global_count + global_index;
  5346. module->aux_stack_bottom = aux_stack_top;
  5347. module->aux_stack_size =
  5348. aux_stack_top > aux_data_end
  5349. ? (uint32)(aux_stack_top - aux_data_end)
  5350. : (uint32)aux_stack_top;
  5351. LOG_VERBOSE(
  5352. "Found aux stack top global, value: %" PRIu64 ", "
  5353. "global index: %d, stack size: %d",
  5354. aux_stack_top, global_index,
  5355. module->aux_stack_size);
  5356. break;
  5357. }
  5358. }
  5359. if (!aux_stack_top_global) {
  5360. /* Auxiliary stack global isn't found, it must be unused
  5361. in the wasm app, as if it is used, the global must be
  5362. defined. Here we set it to __heap_base global and set
  5363. its size to 0. */
  5364. aux_stack_top_global = aux_heap_base_global;
  5365. aux_stack_top = aux_heap_base;
  5366. module->aux_stack_top_global_index =
  5367. module->aux_heap_base_global_index;
  5368. module->aux_stack_bottom = aux_stack_top;
  5369. module->aux_stack_size = 0;
  5370. }
  5371. break;
  5372. }
  5373. }
  5374. }
  5375. module->malloc_function = (uint32)-1;
  5376. module->free_function = (uint32)-1;
  5377. module->retain_function = (uint32)-1;
  5378. /* Resolve malloc/free function exported by wasm module */
  5379. #if WASM_ENABLE_MEMORY64 != 0
  5380. if (has_module_memory64(module))
  5381. malloc_free_io_type = VALUE_TYPE_I64;
  5382. #endif
  5383. export = module->exports;
  5384. for (i = 0; i < module->export_count; i++, export ++) {
  5385. if (export->kind == EXPORT_KIND_FUNC) {
  5386. if (!strcmp(export->name, "malloc")
  5387. && export->index >= module->import_function_count) {
  5388. func_index = export->index - module->import_function_count;
  5389. func_type = module->functions[func_index]->func_type;
  5390. if (func_type->param_count == 1 && func_type->result_count == 1
  5391. && func_type->types[0] == malloc_free_io_type
  5392. && func_type->types[1] == malloc_free_io_type) {
  5393. bh_assert(module->malloc_function == (uint32)-1);
  5394. module->malloc_function = export->index;
  5395. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5396. export->name, export->index);
  5397. }
  5398. }
  5399. else if (!strcmp(export->name, "__new")
  5400. && export->index >= module->import_function_count) {
  5401. /* __new && __pin for AssemblyScript */
  5402. func_index = export->index - module->import_function_count;
  5403. func_type = module->functions[func_index]->func_type;
  5404. if (func_type->param_count == 2 && func_type->result_count == 1
  5405. && func_type->types[0] == malloc_free_io_type
  5406. && func_type->types[1] == VALUE_TYPE_I32
  5407. && func_type->types[2] == malloc_free_io_type) {
  5408. uint32 j;
  5409. WASMExport *export_tmp;
  5410. bh_assert(module->malloc_function == (uint32)-1);
  5411. module->malloc_function = export->index;
  5412. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5413. export->name, export->index);
  5414. /* resolve retain function.
  5415. If not found, reset malloc function index */
  5416. export_tmp = module->exports;
  5417. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5418. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5419. && (!strcmp(export_tmp->name, "__retain")
  5420. || (!strcmp(export_tmp->name, "__pin")))
  5421. && (export_tmp->index
  5422. >= module->import_function_count)) {
  5423. func_index = export_tmp->index
  5424. - module->import_function_count;
  5425. func_type =
  5426. module->functions[func_index]->func_type;
  5427. if (func_type->param_count == 1
  5428. && func_type->result_count == 1
  5429. && func_type->types[0] == malloc_free_io_type
  5430. && func_type->types[1] == malloc_free_io_type) {
  5431. bh_assert(module->retain_function
  5432. == (uint32)-1);
  5433. module->retain_function = export_tmp->index;
  5434. LOG_VERBOSE("Found retain function, name: %s, "
  5435. "index: %u",
  5436. export_tmp->name,
  5437. export_tmp->index);
  5438. break;
  5439. }
  5440. }
  5441. }
  5442. if (j == module->export_count) {
  5443. module->malloc_function = (uint32)-1;
  5444. LOG_VERBOSE("Can't find retain function,"
  5445. "reset malloc function index to -1");
  5446. }
  5447. }
  5448. }
  5449. else if (((!strcmp(export->name, "free"))
  5450. || (!strcmp(export->name, "__release"))
  5451. || (!strcmp(export->name, "__unpin")))
  5452. && export->index >= module->import_function_count) {
  5453. func_index = export->index - module->import_function_count;
  5454. func_type = module->functions[func_index]->func_type;
  5455. if (func_type->param_count == 1 && func_type->result_count == 0
  5456. && func_type->types[0] == malloc_free_io_type) {
  5457. bh_assert(module->free_function == (uint32)-1);
  5458. module->free_function = export->index;
  5459. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5460. export->name, export->index);
  5461. }
  5462. }
  5463. }
  5464. }
  5465. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5466. handle_table = wasm_interp_get_handle_table();
  5467. #endif
  5468. for (i = 0; i < module->function_count; i++) {
  5469. WASMFunction *func = module->functions[i];
  5470. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5471. error_buf_size)) {
  5472. return false;
  5473. }
  5474. if (i == module->function_count - 1
  5475. && func->code + func->code_size != buf_code_end) {
  5476. set_error_buf(error_buf, error_buf_size,
  5477. "code section size mismatch");
  5478. return false;
  5479. }
  5480. }
  5481. if (!module->possible_memory_grow) {
  5482. WASMMemoryImport *memory_import;
  5483. WASMMemory *memory;
  5484. if (aux_data_end_global && aux_heap_base_global
  5485. && aux_stack_top_global) {
  5486. uint64 init_memory_size;
  5487. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5488. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5489. * valid range of uint32 */
  5490. if (shrunk_memory_size <= UINT32_MAX) {
  5491. if (module->import_memory_count) {
  5492. memory_import = &module->import_memories[0].u.memory;
  5493. init_memory_size =
  5494. (uint64)memory_import->mem_type.num_bytes_per_page
  5495. * memory_import->mem_type.init_page_count;
  5496. if (shrunk_memory_size <= init_memory_size) {
  5497. /* Reset memory info to decrease memory usage */
  5498. memory_import->mem_type.num_bytes_per_page =
  5499. (uint32)shrunk_memory_size;
  5500. memory_import->mem_type.init_page_count = 1;
  5501. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5502. shrunk_memory_size);
  5503. }
  5504. }
  5505. if (module->memory_count) {
  5506. memory = &module->memories[0];
  5507. init_memory_size = (uint64)memory->num_bytes_per_page
  5508. * memory->init_page_count;
  5509. if (shrunk_memory_size <= init_memory_size) {
  5510. /* Reset memory info to decrease memory usage */
  5511. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5512. memory->init_page_count = 1;
  5513. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5514. shrunk_memory_size);
  5515. }
  5516. }
  5517. }
  5518. }
  5519. #if WASM_ENABLE_MULTI_MODULE == 0
  5520. if (module->import_memory_count) {
  5521. memory_import = &module->import_memories[0].u.memory;
  5522. /* Only resize the memory to one big page if num_bytes_per_page is
  5523. * in valid range of uint32 */
  5524. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5525. memory_import->mem_type.num_bytes_per_page *=
  5526. memory_import->mem_type.init_page_count;
  5527. if (memory_import->mem_type.init_page_count > 0)
  5528. memory_import->mem_type.init_page_count =
  5529. memory_import->mem_type.max_page_count = 1;
  5530. else
  5531. memory_import->mem_type.init_page_count =
  5532. memory_import->mem_type.max_page_count = 0;
  5533. }
  5534. }
  5535. if (module->memory_count) {
  5536. memory = &module->memories[0];
  5537. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5538. * valid range of uint32 */
  5539. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5540. memory->num_bytes_per_page *= memory->init_page_count;
  5541. if (memory->init_page_count > 0)
  5542. memory->init_page_count = memory->max_page_count = 1;
  5543. else
  5544. memory->init_page_count = memory->max_page_count = 0;
  5545. }
  5546. }
  5547. #endif
  5548. }
  5549. #if WASM_ENABLE_MEMORY64 != 0
  5550. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5551. false))
  5552. return false;
  5553. #endif
  5554. calculate_global_data_offset(module);
  5555. #if WASM_ENABLE_FAST_JIT != 0
  5556. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5557. return false;
  5558. }
  5559. #endif
  5560. #if WASM_ENABLE_JIT != 0
  5561. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5562. return false;
  5563. }
  5564. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5565. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5566. return false;
  5567. }
  5568. #else
  5569. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5570. thread fast jit execution, since applying llvm optimizations in
  5571. aot_compile_wasm may cost a lot of time.
  5572. Create thread with enough native stack to apply llvm optimizations */
  5573. if (os_thread_create(&module->llvm_jit_init_thread,
  5574. init_llvm_jit_functions_stage2_callback,
  5575. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5576. != 0) {
  5577. set_error_buf(error_buf, error_buf_size,
  5578. "create orcjit compile thread failed");
  5579. return false;
  5580. }
  5581. #endif
  5582. #endif
  5583. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5584. /* Create threads to compile the jit functions */
  5585. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5586. return false;
  5587. }
  5588. #endif
  5589. #if WASM_ENABLE_MEMORY_TRACING != 0
  5590. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5591. #endif
  5592. return true;
  5593. }
  5594. static WASMModule *
  5595. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5596. {
  5597. WASMModule *module =
  5598. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5599. bh_list_status ret;
  5600. if (!module) {
  5601. return NULL;
  5602. }
  5603. module->module_type = Wasm_Module_Bytecode;
  5604. /* Set start_function to -1, means no start function */
  5605. module->start_function = (uint32)-1;
  5606. module->name = name;
  5607. module->is_binary_freeable = false;
  5608. #if WASM_ENABLE_FAST_INTERP == 0
  5609. module->br_table_cache_list = &module->br_table_cache_list_head;
  5610. ret = bh_list_init(module->br_table_cache_list);
  5611. bh_assert(ret == BH_LIST_SUCCESS);
  5612. #endif
  5613. #if WASM_ENABLE_MULTI_MODULE != 0
  5614. module->import_module_list = &module->import_module_list_head;
  5615. ret = bh_list_init(module->import_module_list);
  5616. bh_assert(ret == BH_LIST_SUCCESS);
  5617. #endif
  5618. #if WASM_ENABLE_DEBUG_INTERP != 0
  5619. ret = bh_list_init(&module->fast_opcode_list);
  5620. bh_assert(ret == BH_LIST_SUCCESS);
  5621. #endif
  5622. #if WASM_ENABLE_GC != 0
  5623. if (!(module->ref_type_set =
  5624. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5625. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5626. goto fail1;
  5627. }
  5628. if (os_mutex_init(&module->rtt_type_lock)) {
  5629. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5630. goto fail2;
  5631. }
  5632. #endif
  5633. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5634. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5635. && WASM_ENABLE_LAZY_JIT != 0)
  5636. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5637. set_error_buf(error_buf, error_buf_size,
  5638. "init instance list lock failed");
  5639. goto fail3;
  5640. }
  5641. #endif
  5642. (void)ret;
  5643. return module;
  5644. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5645. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5646. && WASM_ENABLE_LAZY_JIT != 0)
  5647. fail3:
  5648. #endif
  5649. #if WASM_ENABLE_GC != 0
  5650. os_mutex_destroy(&module->rtt_type_lock);
  5651. fail2:
  5652. bh_hash_map_destroy(module->ref_type_set);
  5653. fail1:
  5654. #endif
  5655. wasm_runtime_free(module);
  5656. return NULL;
  5657. }
  5658. #if WASM_ENABLE_DEBUG_INTERP != 0
  5659. static bool
  5660. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5661. uint32 error_buf_size)
  5662. {
  5663. WASMFastOPCodeNode *fast_op =
  5664. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5665. if (fast_op) {
  5666. fast_op->offset = pos - module->load_addr;
  5667. fast_op->orig_op = orig_op;
  5668. bh_list_insert(&module->fast_opcode_list, fast_op);
  5669. }
  5670. return fast_op ? true : false;
  5671. }
  5672. #endif
  5673. WASMModule *
  5674. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5675. uint32 error_buf_size)
  5676. {
  5677. WASMModule *module = create_module("", error_buf, error_buf_size);
  5678. if (!module)
  5679. return NULL;
  5680. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5681. error_buf_size)) {
  5682. wasm_loader_unload(module);
  5683. return NULL;
  5684. }
  5685. LOG_VERBOSE("Load module from sections success.\n");
  5686. return module;
  5687. }
  5688. static void
  5689. destroy_sections(WASMSection *section_list)
  5690. {
  5691. WASMSection *section = section_list, *next;
  5692. while (section) {
  5693. next = section->next;
  5694. wasm_runtime_free(section);
  5695. section = next;
  5696. }
  5697. }
  5698. /* clang-format off */
  5699. static uint8 section_ids[] = {
  5700. SECTION_TYPE_USER,
  5701. SECTION_TYPE_TYPE,
  5702. SECTION_TYPE_IMPORT,
  5703. SECTION_TYPE_FUNC,
  5704. SECTION_TYPE_TABLE,
  5705. SECTION_TYPE_MEMORY,
  5706. #if WASM_ENABLE_TAGS != 0
  5707. SECTION_TYPE_TAG,
  5708. #endif
  5709. #if WASM_ENABLE_STRINGREF != 0
  5710. /* must immediately precede the global section,
  5711. or where the global section would be */
  5712. SECTION_TYPE_STRINGREF,
  5713. #endif
  5714. SECTION_TYPE_GLOBAL,
  5715. SECTION_TYPE_EXPORT,
  5716. SECTION_TYPE_START,
  5717. SECTION_TYPE_ELEM,
  5718. #if WASM_ENABLE_BULK_MEMORY != 0
  5719. SECTION_TYPE_DATACOUNT,
  5720. #endif
  5721. SECTION_TYPE_CODE,
  5722. SECTION_TYPE_DATA
  5723. };
  5724. /* clang-format on */
  5725. static uint8
  5726. get_section_index(uint8 section_type)
  5727. {
  5728. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5729. for (uint8 i = 0; i < max_id; i++) {
  5730. if (section_type == section_ids[i])
  5731. return i;
  5732. }
  5733. return (uint8)-1;
  5734. }
  5735. static bool
  5736. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5737. char *error_buf, uint32 error_buf_size)
  5738. {
  5739. WASMSection *section_list_end = NULL, *section;
  5740. const uint8 *p = buf, *p_end = buf + size;
  5741. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5742. uint32 section_size;
  5743. bh_assert(!*p_section_list);
  5744. p += 8;
  5745. while (p < p_end) {
  5746. CHECK_BUF(p, p_end, 1);
  5747. section_type = read_uint8(p);
  5748. section_index = get_section_index(section_type);
  5749. if (section_index != (uint8)-1) {
  5750. if (section_type != SECTION_TYPE_USER) {
  5751. /* Custom sections may be inserted at any place,
  5752. while other sections must occur at most once
  5753. and in prescribed order. */
  5754. if (last_section_index != (uint8)-1
  5755. && (section_index <= last_section_index)) {
  5756. set_error_buf(error_buf, error_buf_size,
  5757. "unexpected content after last section or "
  5758. "junk after last section");
  5759. return false;
  5760. }
  5761. last_section_index = section_index;
  5762. }
  5763. read_leb_uint32(p, p_end, section_size);
  5764. CHECK_BUF1(p, p_end, section_size);
  5765. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5766. error_buf_size))) {
  5767. return false;
  5768. }
  5769. section->section_type = section_type;
  5770. section->section_body = (uint8 *)p;
  5771. section->section_body_size = section_size;
  5772. if (!section_list_end)
  5773. *p_section_list = section_list_end = section;
  5774. else {
  5775. section_list_end->next = section;
  5776. section_list_end = section;
  5777. }
  5778. p += section_size;
  5779. }
  5780. else {
  5781. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5782. return false;
  5783. }
  5784. }
  5785. return true;
  5786. fail:
  5787. return false;
  5788. }
  5789. static void
  5790. exchange32(uint8 *p_data)
  5791. {
  5792. uint8 value = *p_data;
  5793. *p_data = *(p_data + 3);
  5794. *(p_data + 3) = value;
  5795. value = *(p_data + 1);
  5796. *(p_data + 1) = *(p_data + 2);
  5797. *(p_data + 2) = value;
  5798. }
  5799. static union {
  5800. int a;
  5801. char b;
  5802. } __ue = { .a = 1 };
  5803. #define is_little_endian() (__ue.b == 1)
  5804. static bool
  5805. load(const uint8 *buf, uint32 size, WASMModule *module,
  5806. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5807. uint32 error_buf_size)
  5808. {
  5809. const uint8 *buf_end = buf + size;
  5810. const uint8 *p = buf, *p_end = buf_end;
  5811. uint32 magic_number, version;
  5812. WASMSection *section_list = NULL;
  5813. CHECK_BUF1(p, p_end, sizeof(uint32));
  5814. magic_number = read_uint32(p);
  5815. if (!is_little_endian())
  5816. exchange32((uint8 *)&magic_number);
  5817. if (magic_number != WASM_MAGIC_NUMBER) {
  5818. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5819. return false;
  5820. }
  5821. CHECK_BUF1(p, p_end, sizeof(uint32));
  5822. version = read_uint32(p);
  5823. if (!is_little_endian())
  5824. exchange32((uint8 *)&version);
  5825. if (version != WASM_CURRENT_VERSION) {
  5826. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5827. return false;
  5828. }
  5829. module->package_version = version;
  5830. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5831. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5832. no_resolve, error_buf, error_buf_size)) {
  5833. destroy_sections(section_list);
  5834. return false;
  5835. }
  5836. destroy_sections(section_list);
  5837. return true;
  5838. fail:
  5839. return false;
  5840. }
  5841. #if WASM_ENABLE_LIBC_WASI != 0
  5842. /**
  5843. * refer to
  5844. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5845. */
  5846. static bool
  5847. check_wasi_abi_compatibility(const WASMModule *module,
  5848. #if WASM_ENABLE_MULTI_MODULE != 0
  5849. bool main_module,
  5850. #endif
  5851. char *error_buf, uint32 error_buf_size)
  5852. {
  5853. /**
  5854. * be careful with:
  5855. * wasi compatible modules(command/reactor) which don't import any wasi
  5856. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5857. * reactor can depend on nothing. At the same time, each has its own entry
  5858. * point.
  5859. *
  5860. * observations:
  5861. * - clang always injects `_start` into a command
  5862. * - clang always injects `_initialize` into a reactor
  5863. * - `iwasm -f` allows to run a function in the reactor
  5864. *
  5865. * strong assumptions:
  5866. * - no one will define either `_start` or `_initialize` on purpose
  5867. * - `_start` should always be `void _start(void)`
  5868. * - `_initialize` should always be `void _initialize(void)`
  5869. *
  5870. */
  5871. /* clang-format off */
  5872. /**
  5873. *
  5874. * | | import_wasi_api True | | import_wasi_api False | |
  5875. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5876. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5877. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5878. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5879. */
  5880. /* clang-format on */
  5881. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5882. uint32 import_function_count = module->import_function_count;
  5883. WASMFuncType *func_type;
  5884. /* (func (export "_start") (...) */
  5885. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5886. error_buf, error_buf_size);
  5887. if (start) {
  5888. if (start->index < import_function_count) {
  5889. set_error_buf(
  5890. error_buf, error_buf_size,
  5891. "the builtin _start function can not be an import function");
  5892. return false;
  5893. }
  5894. func_type =
  5895. module->functions[start->index - import_function_count]->func_type;
  5896. if (func_type->param_count || func_type->result_count) {
  5897. set_error_buf(error_buf, error_buf_size,
  5898. "the signature of builtin _start function is wrong");
  5899. return false;
  5900. }
  5901. }
  5902. else {
  5903. /* (func (export "_initialize") (...) */
  5904. initialize =
  5905. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5906. error_buf, error_buf_size);
  5907. if (initialize) {
  5908. if (initialize->index < import_function_count) {
  5909. set_error_buf(error_buf, error_buf_size,
  5910. "the builtin _initialize function can not be an "
  5911. "import function");
  5912. return false;
  5913. }
  5914. func_type =
  5915. module->functions[initialize->index - import_function_count]
  5916. ->func_type;
  5917. if (func_type->param_count || func_type->result_count) {
  5918. set_error_buf(
  5919. error_buf, error_buf_size,
  5920. "the signature of builtin _initialize function is wrong");
  5921. return false;
  5922. }
  5923. }
  5924. }
  5925. /* filter out non-wasi compatible modules */
  5926. if (!module->import_wasi_api && !start && !initialize) {
  5927. return true;
  5928. }
  5929. /* should have one at least */
  5930. if (module->import_wasi_api && !start && !initialize) {
  5931. LOG_WARNING("warning: a module with WASI apis should be either "
  5932. "a command or a reactor");
  5933. }
  5934. /*
  5935. * there is at least one of `_start` and `_initialize` in below cases.
  5936. * according to the assumption, they should be all wasi compatible
  5937. */
  5938. #if WASM_ENABLE_MULTI_MODULE != 0
  5939. /* filter out commands (with `_start`) cases */
  5940. if (start && !main_module) {
  5941. set_error_buf(
  5942. error_buf, error_buf_size,
  5943. "a command (with _start function) can not be a sub-module");
  5944. return false;
  5945. }
  5946. #endif
  5947. /*
  5948. * it is ok a reactor acts as a main module,
  5949. * so skip the check about (with `_initialize`)
  5950. */
  5951. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5952. error_buf, error_buf_size);
  5953. if (!memory
  5954. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5955. /*
  5956. * with wasi-threads, it's still an open question if a memory
  5957. * should be exported.
  5958. *
  5959. * https://github.com/WebAssembly/wasi-threads/issues/22
  5960. * https://github.com/WebAssembly/WASI/issues/502
  5961. *
  5962. * Note: this code assumes the number of memories is at most 1.
  5963. */
  5964. && module->import_memory_count == 0
  5965. #endif
  5966. ) {
  5967. set_error_buf(error_buf, error_buf_size,
  5968. "a module with WASI apis must export memory by default");
  5969. return false;
  5970. }
  5971. return true;
  5972. }
  5973. #endif
  5974. WASMModule *
  5975. wasm_loader_load(uint8 *buf, uint32 size,
  5976. #if WASM_ENABLE_MULTI_MODULE != 0
  5977. bool main_module,
  5978. #endif
  5979. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5980. {
  5981. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5982. if (!module) {
  5983. return NULL;
  5984. }
  5985. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5986. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5987. module->load_addr = (uint8 *)buf;
  5988. module->load_size = size;
  5989. #endif
  5990. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  5991. error_buf, error_buf_size)) {
  5992. goto fail;
  5993. }
  5994. #if WASM_ENABLE_LIBC_WASI != 0
  5995. /* Check the WASI application ABI */
  5996. if (!check_wasi_abi_compatibility(module,
  5997. #if WASM_ENABLE_MULTI_MODULE != 0
  5998. main_module,
  5999. #endif
  6000. error_buf, error_buf_size)) {
  6001. goto fail;
  6002. }
  6003. #endif
  6004. LOG_VERBOSE("Load module success.\n");
  6005. return module;
  6006. fail:
  6007. wasm_loader_unload(module);
  6008. return NULL;
  6009. }
  6010. void
  6011. wasm_loader_unload(WASMModule *module)
  6012. {
  6013. uint32 i;
  6014. if (!module)
  6015. return;
  6016. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6017. && WASM_ENABLE_LAZY_JIT != 0
  6018. module->orcjit_stop_compiling = true;
  6019. if (module->llvm_jit_init_thread)
  6020. os_thread_join(module->llvm_jit_init_thread, NULL);
  6021. #endif
  6022. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6023. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6024. module internal data after they were freed */
  6025. orcjit_stop_compile_threads(module);
  6026. #endif
  6027. #if WASM_ENABLE_JIT != 0
  6028. if (module->func_ptrs)
  6029. wasm_runtime_free(module->func_ptrs);
  6030. if (module->comp_ctx)
  6031. aot_destroy_comp_context(module->comp_ctx);
  6032. if (module->comp_data)
  6033. aot_destroy_comp_data(module->comp_data);
  6034. #endif
  6035. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6036. && WASM_ENABLE_LAZY_JIT != 0
  6037. if (module->tierup_wait_lock_inited) {
  6038. os_mutex_destroy(&module->tierup_wait_lock);
  6039. os_cond_destroy(&module->tierup_wait_cond);
  6040. }
  6041. #endif
  6042. if (module->imports)
  6043. wasm_runtime_free(module->imports);
  6044. if (module->functions) {
  6045. for (i = 0; i < module->function_count; i++) {
  6046. if (module->functions[i]) {
  6047. if (module->functions[i]->local_offsets)
  6048. wasm_runtime_free(module->functions[i]->local_offsets);
  6049. #if WASM_ENABLE_FAST_INTERP != 0
  6050. if (module->functions[i]->code_compiled)
  6051. wasm_runtime_free(module->functions[i]->code_compiled);
  6052. if (module->functions[i]->consts)
  6053. wasm_runtime_free(module->functions[i]->consts);
  6054. #endif
  6055. #if WASM_ENABLE_FAST_JIT != 0
  6056. if (module->functions[i]->fast_jit_jitted_code) {
  6057. jit_code_cache_free(
  6058. module->functions[i]->fast_jit_jitted_code);
  6059. }
  6060. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6061. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6062. jit_code_cache_free(
  6063. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6064. }
  6065. #endif
  6066. #endif
  6067. #if WASM_ENABLE_GC != 0
  6068. if (module->functions[i]->local_ref_type_maps) {
  6069. wasm_runtime_free(
  6070. module->functions[i]->local_ref_type_maps);
  6071. }
  6072. #endif
  6073. wasm_runtime_free(module->functions[i]);
  6074. }
  6075. }
  6076. wasm_runtime_free(module->functions);
  6077. }
  6078. if (module->tables) {
  6079. #if WASM_ENABLE_GC != 0
  6080. for (i = 0; i < module->table_count; i++) {
  6081. destroy_init_expr(module, &module->tables[i].init_expr);
  6082. }
  6083. #endif
  6084. wasm_runtime_free(module->tables);
  6085. }
  6086. if (module->memories)
  6087. wasm_runtime_free(module->memories);
  6088. if (module->globals) {
  6089. #if WASM_ENABLE_GC != 0
  6090. for (i = 0; i < module->global_count; i++) {
  6091. destroy_init_expr(module, &module->globals[i].init_expr);
  6092. }
  6093. #endif
  6094. wasm_runtime_free(module->globals);
  6095. }
  6096. #if WASM_ENABLE_TAGS != 0
  6097. if (module->tags) {
  6098. for (i = 0; i < module->tag_count; i++) {
  6099. if (module->tags[i])
  6100. wasm_runtime_free(module->tags[i]);
  6101. }
  6102. wasm_runtime_free(module->tags);
  6103. }
  6104. #endif
  6105. if (module->exports)
  6106. wasm_runtime_free(module->exports);
  6107. if (module->table_segments) {
  6108. for (i = 0; i < module->table_seg_count; i++) {
  6109. if (module->table_segments[i].init_values) {
  6110. #if WASM_ENABLE_GC != 0
  6111. uint32 j;
  6112. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6113. destroy_init_expr(
  6114. module, &module->table_segments[i].init_values[j]);
  6115. }
  6116. #endif
  6117. wasm_runtime_free(module->table_segments[i].init_values);
  6118. }
  6119. }
  6120. wasm_runtime_free(module->table_segments);
  6121. }
  6122. if (module->data_segments) {
  6123. for (i = 0; i < module->data_seg_count; i++) {
  6124. if (module->data_segments[i]) {
  6125. if (module->data_segments[i]->is_data_cloned)
  6126. wasm_runtime_free(module->data_segments[i]->data);
  6127. wasm_runtime_free(module->data_segments[i]);
  6128. }
  6129. }
  6130. wasm_runtime_free(module->data_segments);
  6131. }
  6132. if (module->types) {
  6133. for (i = 0; i < module->type_count; i++) {
  6134. if (module->types[i])
  6135. destroy_wasm_type(module->types[i]);
  6136. }
  6137. wasm_runtime_free(module->types);
  6138. }
  6139. if (module->const_str_list) {
  6140. StringNode *node = module->const_str_list, *node_next;
  6141. while (node) {
  6142. node_next = node->next;
  6143. wasm_runtime_free(node);
  6144. node = node_next;
  6145. }
  6146. }
  6147. #if WASM_ENABLE_STRINGREF != 0
  6148. if (module->string_literal_ptrs) {
  6149. wasm_runtime_free((void *)module->string_literal_ptrs);
  6150. }
  6151. if (module->string_literal_lengths) {
  6152. wasm_runtime_free(module->string_literal_lengths);
  6153. }
  6154. #endif
  6155. #if WASM_ENABLE_FAST_INTERP == 0
  6156. if (module->br_table_cache_list) {
  6157. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6158. BrTableCache *node_next;
  6159. while (node) {
  6160. node_next = bh_list_elem_next(node);
  6161. wasm_runtime_free(node);
  6162. node = node_next;
  6163. }
  6164. }
  6165. #endif
  6166. #if WASM_ENABLE_MULTI_MODULE != 0
  6167. /* just release the sub module list */
  6168. if (module->import_module_list) {
  6169. WASMRegisteredModule *node =
  6170. bh_list_first_elem(module->import_module_list);
  6171. while (node) {
  6172. WASMRegisteredModule *next = bh_list_elem_next(node);
  6173. bh_list_remove(module->import_module_list, node);
  6174. /*
  6175. * unload(sub_module) will be triggered during runtime_destroy().
  6176. * every module in the global module list will be unloaded one by
  6177. * one. so don't worry.
  6178. */
  6179. wasm_runtime_free(node);
  6180. /*
  6181. * the module file reading buffer will be released
  6182. * in runtime_destroy()
  6183. */
  6184. node = next;
  6185. }
  6186. }
  6187. #endif
  6188. #if WASM_ENABLE_DEBUG_INTERP != 0
  6189. WASMFastOPCodeNode *fast_opcode =
  6190. bh_list_first_elem(&module->fast_opcode_list);
  6191. while (fast_opcode) {
  6192. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6193. wasm_runtime_free(fast_opcode);
  6194. fast_opcode = next;
  6195. }
  6196. #endif
  6197. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6198. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6199. && WASM_ENABLE_LAZY_JIT != 0)
  6200. os_mutex_destroy(&module->instance_list_lock);
  6201. #endif
  6202. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6203. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6204. #endif
  6205. #if WASM_ENABLE_FAST_JIT != 0
  6206. if (module->fast_jit_func_ptrs) {
  6207. wasm_runtime_free(module->fast_jit_func_ptrs);
  6208. }
  6209. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6210. if (module->fast_jit_thread_locks_inited[i]) {
  6211. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6212. }
  6213. }
  6214. #endif
  6215. #if WASM_ENABLE_GC != 0
  6216. os_mutex_destroy(&module->rtt_type_lock);
  6217. bh_hash_map_destroy(module->ref_type_set);
  6218. if (module->rtt_types) {
  6219. for (i = 0; i < module->type_count; i++) {
  6220. if (module->rtt_types[i])
  6221. wasm_runtime_free(module->rtt_types[i]);
  6222. }
  6223. wasm_runtime_free(module->rtt_types);
  6224. }
  6225. #if WASM_ENABLE_STRINGREF != 0
  6226. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6227. if (module->stringref_rtts[i])
  6228. wasm_runtime_free(module->stringref_rtts[i]);
  6229. }
  6230. #endif
  6231. #endif
  6232. wasm_runtime_free(module);
  6233. }
  6234. bool
  6235. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6236. const uint8 *start_addr, const uint8 *code_end_addr,
  6237. uint8 label_type, uint8 **p_else_addr,
  6238. uint8 **p_end_addr)
  6239. {
  6240. const uint8 *p = start_addr, *p_end = code_end_addr;
  6241. uint8 *else_addr = NULL;
  6242. char error_buf[128];
  6243. uint32 block_nested_depth = 1, count, i, j, t;
  6244. uint32 error_buf_size = sizeof(error_buf);
  6245. uint8 opcode, u8;
  6246. BlockAddr block_stack[16] = { { 0 } }, *block;
  6247. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6248. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6249. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6250. if (block[j].start_addr == start_addr) {
  6251. /* Cache hit */
  6252. *p_else_addr = block[j].else_addr;
  6253. *p_end_addr = block[j].end_addr;
  6254. return true;
  6255. }
  6256. }
  6257. /* Cache unhit */
  6258. block_stack[0].start_addr = start_addr;
  6259. while (p < code_end_addr) {
  6260. opcode = *p++;
  6261. #if WASM_ENABLE_DEBUG_INTERP != 0
  6262. op_break_retry:
  6263. #endif
  6264. switch (opcode) {
  6265. case WASM_OP_UNREACHABLE:
  6266. case WASM_OP_NOP:
  6267. break;
  6268. #if WASM_ENABLE_EXCE_HANDLING != 0
  6269. case WASM_OP_TRY:
  6270. u8 = read_uint8(p);
  6271. if (block_nested_depth
  6272. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6273. block_stack[block_nested_depth].start_addr = p;
  6274. block_stack[block_nested_depth].else_addr = NULL;
  6275. }
  6276. block_nested_depth++;
  6277. break;
  6278. case EXT_OP_TRY:
  6279. skip_leb_uint32(p, p_end);
  6280. if (block_nested_depth
  6281. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6282. block_stack[block_nested_depth].start_addr = p;
  6283. block_stack[block_nested_depth].else_addr = NULL;
  6284. }
  6285. block_nested_depth++;
  6286. break;
  6287. case WASM_OP_CATCH:
  6288. if (block_nested_depth == 1) {
  6289. *p_end_addr = (uint8 *)(p - 1);
  6290. /* stop search and return the address of the catch block */
  6291. return true;
  6292. }
  6293. break;
  6294. case WASM_OP_CATCH_ALL:
  6295. if (block_nested_depth == 1) {
  6296. *p_end_addr = (uint8 *)(p - 1);
  6297. /* stop search and return the address of the catch_all block
  6298. */
  6299. return true;
  6300. }
  6301. break;
  6302. case WASM_OP_THROW:
  6303. /* skip tag_index */
  6304. skip_leb(p);
  6305. break;
  6306. case WASM_OP_RETHROW:
  6307. /* skip depth */
  6308. skip_leb(p);
  6309. break;
  6310. case WASM_OP_DELEGATE:
  6311. if (block_nested_depth == 1) {
  6312. *p_end_addr = (uint8 *)(p - 1);
  6313. return true;
  6314. }
  6315. else {
  6316. skip_leb(p);
  6317. /* the DELEGATE opcode ends the tryblock, */
  6318. block_nested_depth--;
  6319. if (block_nested_depth
  6320. < sizeof(block_stack) / sizeof(BlockAddr))
  6321. block_stack[block_nested_depth].end_addr =
  6322. (uint8 *)(p - 1);
  6323. }
  6324. break;
  6325. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6326. case WASM_OP_BLOCK:
  6327. case WASM_OP_LOOP:
  6328. case WASM_OP_IF:
  6329. {
  6330. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6331. u8 = read_uint8(p);
  6332. if (is_byte_a_type(u8)) {
  6333. #if WASM_ENABLE_GC != 0
  6334. if (wasm_is_type_multi_byte_type(u8)) {
  6335. /* the possible extra bytes of GC ref type have been
  6336. modified to OP_NOP, no need to resolve them again */
  6337. }
  6338. #endif
  6339. }
  6340. else {
  6341. p--;
  6342. /* block type */
  6343. skip_leb_int32(p, p_end);
  6344. }
  6345. if (block_nested_depth
  6346. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6347. block_stack[block_nested_depth].start_addr = p;
  6348. block_stack[block_nested_depth].else_addr = NULL;
  6349. }
  6350. block_nested_depth++;
  6351. break;
  6352. }
  6353. case EXT_OP_BLOCK:
  6354. case EXT_OP_LOOP:
  6355. case EXT_OP_IF:
  6356. /* block type */
  6357. skip_leb_int32(p, p_end);
  6358. if (block_nested_depth
  6359. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6360. block_stack[block_nested_depth].start_addr = p;
  6361. block_stack[block_nested_depth].else_addr = NULL;
  6362. }
  6363. block_nested_depth++;
  6364. break;
  6365. case WASM_OP_ELSE:
  6366. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6367. else_addr = (uint8 *)(p - 1);
  6368. if (block_nested_depth - 1
  6369. < sizeof(block_stack) / sizeof(BlockAddr))
  6370. block_stack[block_nested_depth - 1].else_addr =
  6371. (uint8 *)(p - 1);
  6372. break;
  6373. case WASM_OP_END:
  6374. if (block_nested_depth == 1) {
  6375. if (label_type == LABEL_TYPE_IF)
  6376. *p_else_addr = else_addr;
  6377. *p_end_addr = (uint8 *)(p - 1);
  6378. block_stack[0].end_addr = (uint8 *)(p - 1);
  6379. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6380. t++) {
  6381. start_addr = block_stack[t].start_addr;
  6382. if (start_addr) {
  6383. i = ((uintptr_t)start_addr)
  6384. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6385. block =
  6386. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6387. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6388. if (!block[j].start_addr)
  6389. break;
  6390. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6391. memmove(block + 1, block,
  6392. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6393. * sizeof(BlockAddr));
  6394. j = 0;
  6395. }
  6396. block[j].start_addr = block_stack[t].start_addr;
  6397. block[j].else_addr = block_stack[t].else_addr;
  6398. block[j].end_addr = block_stack[t].end_addr;
  6399. }
  6400. else
  6401. break;
  6402. }
  6403. return true;
  6404. }
  6405. else {
  6406. block_nested_depth--;
  6407. if (block_nested_depth
  6408. < sizeof(block_stack) / sizeof(BlockAddr))
  6409. block_stack[block_nested_depth].end_addr =
  6410. (uint8 *)(p - 1);
  6411. }
  6412. break;
  6413. case WASM_OP_BR:
  6414. case WASM_OP_BR_IF:
  6415. skip_leb_uint32(p, p_end); /* labelidx */
  6416. break;
  6417. case WASM_OP_BR_TABLE:
  6418. read_leb_uint32(p, p_end, count); /* label num */
  6419. #if WASM_ENABLE_FAST_INTERP != 0
  6420. for (i = 0; i <= count; i++) /* labelidxs */
  6421. skip_leb_uint32(p, p_end);
  6422. #else
  6423. p += count + 1;
  6424. while (*p == WASM_OP_NOP)
  6425. p++;
  6426. #endif
  6427. break;
  6428. #if WASM_ENABLE_FAST_INTERP == 0
  6429. case EXT_OP_BR_TABLE_CACHE:
  6430. read_leb_uint32(p, p_end, count); /* label num */
  6431. while (*p == WASM_OP_NOP)
  6432. p++;
  6433. break;
  6434. #endif
  6435. case WASM_OP_RETURN:
  6436. break;
  6437. case WASM_OP_CALL:
  6438. #if WASM_ENABLE_TAIL_CALL != 0
  6439. case WASM_OP_RETURN_CALL:
  6440. #endif
  6441. skip_leb_uint32(p, p_end); /* funcidx */
  6442. break;
  6443. case WASM_OP_CALL_INDIRECT:
  6444. #if WASM_ENABLE_TAIL_CALL != 0
  6445. case WASM_OP_RETURN_CALL_INDIRECT:
  6446. #endif
  6447. skip_leb_uint32(p, p_end); /* typeidx */
  6448. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6449. skip_leb_uint32(p, p_end); /* tableidx */
  6450. #else
  6451. u8 = read_uint8(p); /* 0x00 */
  6452. #endif
  6453. break;
  6454. #if WASM_ENABLE_GC != 0
  6455. case WASM_OP_CALL_REF:
  6456. case WASM_OP_RETURN_CALL_REF:
  6457. skip_leb_uint32(p, p_end); /* typeidx */
  6458. break;
  6459. #endif
  6460. case WASM_OP_DROP:
  6461. case WASM_OP_SELECT:
  6462. case WASM_OP_DROP_64:
  6463. case WASM_OP_SELECT_64:
  6464. break;
  6465. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6466. case WASM_OP_SELECT_T:
  6467. {
  6468. skip_leb_uint32(p, p_end); /* vec length */
  6469. u8 = read_uint8(p); /* typeidx */
  6470. /* the possible extra bytes of GC ref type have been
  6471. modified to OP_NOP, no need to resolve them again */
  6472. break;
  6473. }
  6474. case WASM_OP_TABLE_GET:
  6475. case WASM_OP_TABLE_SET:
  6476. skip_leb_uint32(p, p_end); /* table index */
  6477. break;
  6478. case WASM_OP_REF_NULL:
  6479. {
  6480. u8 = read_uint8(p); /* type */
  6481. if (is_byte_a_type(u8)) {
  6482. #if WASM_ENABLE_GC != 0
  6483. if (wasm_is_type_multi_byte_type(u8)) {
  6484. /* the possible extra bytes of GC ref type have been
  6485. modified to OP_NOP, no need to resolve them again */
  6486. }
  6487. #endif
  6488. }
  6489. else {
  6490. p--;
  6491. skip_leb_uint32(p, p_end);
  6492. }
  6493. break;
  6494. }
  6495. case WASM_OP_REF_IS_NULL:
  6496. break;
  6497. case WASM_OP_REF_FUNC:
  6498. skip_leb_uint32(p, p_end); /* func index */
  6499. break;
  6500. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6501. #if WASM_ENABLE_GC != 0
  6502. case WASM_OP_REF_AS_NON_NULL:
  6503. case WASM_OP_REF_EQ:
  6504. break;
  6505. case WASM_OP_BR_ON_NULL:
  6506. case WASM_OP_BR_ON_NON_NULL:
  6507. skip_leb_uint32(p, p_end); /* label index */
  6508. break;
  6509. #endif /* end of WASM_ENABLE_GC != 0 */
  6510. case WASM_OP_GET_LOCAL:
  6511. case WASM_OP_SET_LOCAL:
  6512. case WASM_OP_TEE_LOCAL:
  6513. case WASM_OP_GET_GLOBAL:
  6514. case WASM_OP_SET_GLOBAL:
  6515. case WASM_OP_GET_GLOBAL_64:
  6516. case WASM_OP_SET_GLOBAL_64:
  6517. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6518. skip_leb_uint32(p, p_end); /* local index */
  6519. break;
  6520. case EXT_OP_GET_LOCAL_FAST:
  6521. case EXT_OP_SET_LOCAL_FAST:
  6522. case EXT_OP_TEE_LOCAL_FAST:
  6523. CHECK_BUF(p, p_end, 1);
  6524. p++;
  6525. break;
  6526. case WASM_OP_I32_LOAD:
  6527. case WASM_OP_I64_LOAD:
  6528. case WASM_OP_F32_LOAD:
  6529. case WASM_OP_F64_LOAD:
  6530. case WASM_OP_I32_LOAD8_S:
  6531. case WASM_OP_I32_LOAD8_U:
  6532. case WASM_OP_I32_LOAD16_S:
  6533. case WASM_OP_I32_LOAD16_U:
  6534. case WASM_OP_I64_LOAD8_S:
  6535. case WASM_OP_I64_LOAD8_U:
  6536. case WASM_OP_I64_LOAD16_S:
  6537. case WASM_OP_I64_LOAD16_U:
  6538. case WASM_OP_I64_LOAD32_S:
  6539. case WASM_OP_I64_LOAD32_U:
  6540. case WASM_OP_I32_STORE:
  6541. case WASM_OP_I64_STORE:
  6542. case WASM_OP_F32_STORE:
  6543. case WASM_OP_F64_STORE:
  6544. case WASM_OP_I32_STORE8:
  6545. case WASM_OP_I32_STORE16:
  6546. case WASM_OP_I64_STORE8:
  6547. case WASM_OP_I64_STORE16:
  6548. case WASM_OP_I64_STORE32:
  6549. skip_leb_align(p, p_end); /* align */
  6550. skip_leb_mem_offset(p, p_end); /* offset */
  6551. break;
  6552. case WASM_OP_MEMORY_SIZE:
  6553. case WASM_OP_MEMORY_GROW:
  6554. skip_leb_memidx(p, p_end); /* memidx */
  6555. break;
  6556. case WASM_OP_I32_CONST:
  6557. skip_leb_int32(p, p_end);
  6558. break;
  6559. case WASM_OP_I64_CONST:
  6560. skip_leb_int64(p, p_end);
  6561. break;
  6562. case WASM_OP_F32_CONST:
  6563. p += sizeof(float32);
  6564. break;
  6565. case WASM_OP_F64_CONST:
  6566. p += sizeof(float64);
  6567. break;
  6568. case WASM_OP_I32_EQZ:
  6569. case WASM_OP_I32_EQ:
  6570. case WASM_OP_I32_NE:
  6571. case WASM_OP_I32_LT_S:
  6572. case WASM_OP_I32_LT_U:
  6573. case WASM_OP_I32_GT_S:
  6574. case WASM_OP_I32_GT_U:
  6575. case WASM_OP_I32_LE_S:
  6576. case WASM_OP_I32_LE_U:
  6577. case WASM_OP_I32_GE_S:
  6578. case WASM_OP_I32_GE_U:
  6579. case WASM_OP_I64_EQZ:
  6580. case WASM_OP_I64_EQ:
  6581. case WASM_OP_I64_NE:
  6582. case WASM_OP_I64_LT_S:
  6583. case WASM_OP_I64_LT_U:
  6584. case WASM_OP_I64_GT_S:
  6585. case WASM_OP_I64_GT_U:
  6586. case WASM_OP_I64_LE_S:
  6587. case WASM_OP_I64_LE_U:
  6588. case WASM_OP_I64_GE_S:
  6589. case WASM_OP_I64_GE_U:
  6590. case WASM_OP_F32_EQ:
  6591. case WASM_OP_F32_NE:
  6592. case WASM_OP_F32_LT:
  6593. case WASM_OP_F32_GT:
  6594. case WASM_OP_F32_LE:
  6595. case WASM_OP_F32_GE:
  6596. case WASM_OP_F64_EQ:
  6597. case WASM_OP_F64_NE:
  6598. case WASM_OP_F64_LT:
  6599. case WASM_OP_F64_GT:
  6600. case WASM_OP_F64_LE:
  6601. case WASM_OP_F64_GE:
  6602. case WASM_OP_I32_CLZ:
  6603. case WASM_OP_I32_CTZ:
  6604. case WASM_OP_I32_POPCNT:
  6605. case WASM_OP_I32_ADD:
  6606. case WASM_OP_I32_SUB:
  6607. case WASM_OP_I32_MUL:
  6608. case WASM_OP_I32_DIV_S:
  6609. case WASM_OP_I32_DIV_U:
  6610. case WASM_OP_I32_REM_S:
  6611. case WASM_OP_I32_REM_U:
  6612. case WASM_OP_I32_AND:
  6613. case WASM_OP_I32_OR:
  6614. case WASM_OP_I32_XOR:
  6615. case WASM_OP_I32_SHL:
  6616. case WASM_OP_I32_SHR_S:
  6617. case WASM_OP_I32_SHR_U:
  6618. case WASM_OP_I32_ROTL:
  6619. case WASM_OP_I32_ROTR:
  6620. case WASM_OP_I64_CLZ:
  6621. case WASM_OP_I64_CTZ:
  6622. case WASM_OP_I64_POPCNT:
  6623. case WASM_OP_I64_ADD:
  6624. case WASM_OP_I64_SUB:
  6625. case WASM_OP_I64_MUL:
  6626. case WASM_OP_I64_DIV_S:
  6627. case WASM_OP_I64_DIV_U:
  6628. case WASM_OP_I64_REM_S:
  6629. case WASM_OP_I64_REM_U:
  6630. case WASM_OP_I64_AND:
  6631. case WASM_OP_I64_OR:
  6632. case WASM_OP_I64_XOR:
  6633. case WASM_OP_I64_SHL:
  6634. case WASM_OP_I64_SHR_S:
  6635. case WASM_OP_I64_SHR_U:
  6636. case WASM_OP_I64_ROTL:
  6637. case WASM_OP_I64_ROTR:
  6638. case WASM_OP_F32_ABS:
  6639. case WASM_OP_F32_NEG:
  6640. case WASM_OP_F32_CEIL:
  6641. case WASM_OP_F32_FLOOR:
  6642. case WASM_OP_F32_TRUNC:
  6643. case WASM_OP_F32_NEAREST:
  6644. case WASM_OP_F32_SQRT:
  6645. case WASM_OP_F32_ADD:
  6646. case WASM_OP_F32_SUB:
  6647. case WASM_OP_F32_MUL:
  6648. case WASM_OP_F32_DIV:
  6649. case WASM_OP_F32_MIN:
  6650. case WASM_OP_F32_MAX:
  6651. case WASM_OP_F32_COPYSIGN:
  6652. case WASM_OP_F64_ABS:
  6653. case WASM_OP_F64_NEG:
  6654. case WASM_OP_F64_CEIL:
  6655. case WASM_OP_F64_FLOOR:
  6656. case WASM_OP_F64_TRUNC:
  6657. case WASM_OP_F64_NEAREST:
  6658. case WASM_OP_F64_SQRT:
  6659. case WASM_OP_F64_ADD:
  6660. case WASM_OP_F64_SUB:
  6661. case WASM_OP_F64_MUL:
  6662. case WASM_OP_F64_DIV:
  6663. case WASM_OP_F64_MIN:
  6664. case WASM_OP_F64_MAX:
  6665. case WASM_OP_F64_COPYSIGN:
  6666. case WASM_OP_I32_WRAP_I64:
  6667. case WASM_OP_I32_TRUNC_S_F32:
  6668. case WASM_OP_I32_TRUNC_U_F32:
  6669. case WASM_OP_I32_TRUNC_S_F64:
  6670. case WASM_OP_I32_TRUNC_U_F64:
  6671. case WASM_OP_I64_EXTEND_S_I32:
  6672. case WASM_OP_I64_EXTEND_U_I32:
  6673. case WASM_OP_I64_TRUNC_S_F32:
  6674. case WASM_OP_I64_TRUNC_U_F32:
  6675. case WASM_OP_I64_TRUNC_S_F64:
  6676. case WASM_OP_I64_TRUNC_U_F64:
  6677. case WASM_OP_F32_CONVERT_S_I32:
  6678. case WASM_OP_F32_CONVERT_U_I32:
  6679. case WASM_OP_F32_CONVERT_S_I64:
  6680. case WASM_OP_F32_CONVERT_U_I64:
  6681. case WASM_OP_F32_DEMOTE_F64:
  6682. case WASM_OP_F64_CONVERT_S_I32:
  6683. case WASM_OP_F64_CONVERT_U_I32:
  6684. case WASM_OP_F64_CONVERT_S_I64:
  6685. case WASM_OP_F64_CONVERT_U_I64:
  6686. case WASM_OP_F64_PROMOTE_F32:
  6687. case WASM_OP_I32_REINTERPRET_F32:
  6688. case WASM_OP_I64_REINTERPRET_F64:
  6689. case WASM_OP_F32_REINTERPRET_I32:
  6690. case WASM_OP_F64_REINTERPRET_I64:
  6691. case WASM_OP_I32_EXTEND8_S:
  6692. case WASM_OP_I32_EXTEND16_S:
  6693. case WASM_OP_I64_EXTEND8_S:
  6694. case WASM_OP_I64_EXTEND16_S:
  6695. case WASM_OP_I64_EXTEND32_S:
  6696. break;
  6697. #if WASM_ENABLE_GC != 0
  6698. case WASM_OP_GC_PREFIX:
  6699. {
  6700. uint32 opcode1;
  6701. read_leb_uint32(p, p_end, opcode1);
  6702. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6703. is no larger than UINT8_MAX */
  6704. opcode = (uint8)opcode1;
  6705. switch (opcode) {
  6706. case WASM_OP_STRUCT_NEW:
  6707. case WASM_OP_STRUCT_NEW_DEFAULT:
  6708. skip_leb_uint32(p, p_end); /* typeidx */
  6709. break;
  6710. case WASM_OP_STRUCT_GET:
  6711. case WASM_OP_STRUCT_GET_S:
  6712. case WASM_OP_STRUCT_GET_U:
  6713. case WASM_OP_STRUCT_SET:
  6714. skip_leb_uint32(p, p_end); /* typeidx */
  6715. skip_leb_uint32(p, p_end); /* fieldidx */
  6716. break;
  6717. case WASM_OP_ARRAY_NEW:
  6718. case WASM_OP_ARRAY_NEW_DEFAULT:
  6719. case WASM_OP_ARRAY_GET:
  6720. case WASM_OP_ARRAY_GET_S:
  6721. case WASM_OP_ARRAY_GET_U:
  6722. case WASM_OP_ARRAY_SET:
  6723. case WASM_OP_ARRAY_FILL:
  6724. skip_leb_uint32(p, p_end); /* typeidx */
  6725. break;
  6726. case WASM_OP_ARRAY_COPY:
  6727. skip_leb_uint32(p, p_end); /* typeidx1 */
  6728. skip_leb_uint32(p, p_end); /* typeidx2 */
  6729. break;
  6730. case WASM_OP_ARRAY_LEN:
  6731. break;
  6732. case WASM_OP_ARRAY_NEW_FIXED:
  6733. case WASM_OP_ARRAY_NEW_DATA:
  6734. case WASM_OP_ARRAY_NEW_ELEM:
  6735. skip_leb_uint32(p, p_end); /* typeidx */
  6736. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6737. break;
  6738. case WASM_OP_REF_I31:
  6739. case WASM_OP_I31_GET_S:
  6740. case WASM_OP_I31_GET_U:
  6741. break;
  6742. case WASM_OP_REF_TEST:
  6743. case WASM_OP_REF_CAST:
  6744. case WASM_OP_REF_TEST_NULLABLE:
  6745. case WASM_OP_REF_CAST_NULLABLE:
  6746. skip_leb_int32(p, p_end); /* heaptype */
  6747. break;
  6748. case WASM_OP_BR_ON_CAST:
  6749. case WASM_OP_BR_ON_CAST_FAIL:
  6750. p += sizeof(uint8); /* castflag */
  6751. skip_leb_uint32(p, p_end); /* labelidx */
  6752. skip_leb_int32(p, p_end); /* heaptype */
  6753. skip_leb_int32(p, p_end); /* heaptype2 */
  6754. break;
  6755. case WASM_OP_ANY_CONVERT_EXTERN:
  6756. case WASM_OP_EXTERN_CONVERT_ANY:
  6757. break;
  6758. #if WASM_ENABLE_STRINGREF != 0
  6759. case WASM_OP_STRING_NEW_UTF8:
  6760. case WASM_OP_STRING_NEW_WTF16:
  6761. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6762. case WASM_OP_STRING_NEW_WTF8:
  6763. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6764. break;
  6765. case WASM_OP_STRING_CONST:
  6766. skip_leb_int32(p, p_end); /* contents */
  6767. break;
  6768. case WASM_OP_STRING_MEASURE_UTF8:
  6769. case WASM_OP_STRING_MEASURE_WTF8:
  6770. case WASM_OP_STRING_MEASURE_WTF16:
  6771. break;
  6772. case WASM_OP_STRING_ENCODE_UTF8:
  6773. case WASM_OP_STRING_ENCODE_WTF16:
  6774. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6775. case WASM_OP_STRING_ENCODE_WTF8:
  6776. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6777. break;
  6778. case WASM_OP_STRING_CONCAT:
  6779. case WASM_OP_STRING_EQ:
  6780. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6781. case WASM_OP_STRING_AS_WTF8:
  6782. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6783. break;
  6784. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6785. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6786. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6787. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6788. break;
  6789. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6790. case WASM_OP_STRING_AS_WTF16:
  6791. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6792. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6793. break;
  6794. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6795. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6796. break;
  6797. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6798. case WASM_OP_STRING_AS_ITER:
  6799. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6800. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6801. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6802. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6803. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6804. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6805. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6806. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6807. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6808. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6809. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6810. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6811. break;
  6812. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6813. default:
  6814. return false;
  6815. }
  6816. break;
  6817. }
  6818. #endif /* end of WASM_ENABLE_GC != 0 */
  6819. case WASM_OP_MISC_PREFIX:
  6820. {
  6821. uint32 opcode1;
  6822. read_leb_uint32(p, p_end, opcode1);
  6823. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6824. is no larger than UINT8_MAX */
  6825. opcode = (uint8)opcode1;
  6826. switch (opcode) {
  6827. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6828. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6829. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6830. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6831. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6832. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6833. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6834. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6835. break;
  6836. #if WASM_ENABLE_BULK_MEMORY != 0
  6837. case WASM_OP_MEMORY_INIT:
  6838. skip_leb_uint32(p, p_end);
  6839. skip_leb_memidx(p, p_end);
  6840. break;
  6841. case WASM_OP_DATA_DROP:
  6842. skip_leb_uint32(p, p_end);
  6843. break;
  6844. case WASM_OP_MEMORY_COPY:
  6845. skip_leb_memidx(p, p_end);
  6846. skip_leb_memidx(p, p_end);
  6847. break;
  6848. case WASM_OP_MEMORY_FILL:
  6849. skip_leb_memidx(p, p_end);
  6850. break;
  6851. #endif /* WASM_ENABLE_BULK_MEMORY */
  6852. #if WASM_ENABLE_REF_TYPES != 0
  6853. case WASM_OP_TABLE_INIT:
  6854. case WASM_OP_TABLE_COPY:
  6855. /* tableidx */
  6856. skip_leb_uint32(p, p_end);
  6857. /* elemidx */
  6858. skip_leb_uint32(p, p_end);
  6859. break;
  6860. case WASM_OP_ELEM_DROP:
  6861. /* elemidx */
  6862. skip_leb_uint32(p, p_end);
  6863. break;
  6864. case WASM_OP_TABLE_SIZE:
  6865. case WASM_OP_TABLE_GROW:
  6866. case WASM_OP_TABLE_FILL:
  6867. skip_leb_uint32(p, p_end); /* table idx */
  6868. break;
  6869. #endif /* WASM_ENABLE_REF_TYPES */
  6870. default:
  6871. return false;
  6872. }
  6873. break;
  6874. }
  6875. #if WASM_ENABLE_SIMD != 0
  6876. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6877. case WASM_OP_SIMD_PREFIX:
  6878. {
  6879. uint32 opcode1;
  6880. read_leb_uint32(p, p_end, opcode1);
  6881. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6882. is no larger than UINT8_MAX */
  6883. opcode = (uint8)opcode1;
  6884. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6885. */
  6886. switch (opcode) {
  6887. case SIMD_v128_load:
  6888. case SIMD_v128_load8x8_s:
  6889. case SIMD_v128_load8x8_u:
  6890. case SIMD_v128_load16x4_s:
  6891. case SIMD_v128_load16x4_u:
  6892. case SIMD_v128_load32x2_s:
  6893. case SIMD_v128_load32x2_u:
  6894. case SIMD_v128_load8_splat:
  6895. case SIMD_v128_load16_splat:
  6896. case SIMD_v128_load32_splat:
  6897. case SIMD_v128_load64_splat:
  6898. case SIMD_v128_store:
  6899. /* memarg align */
  6900. skip_leb_uint32(p, p_end);
  6901. /* memarg offset */
  6902. skip_leb_mem_offset(p, p_end);
  6903. break;
  6904. case SIMD_v128_const:
  6905. case SIMD_v8x16_shuffle:
  6906. /* immByte[16] immLaneId[16] */
  6907. CHECK_BUF1(p, p_end, 16);
  6908. p += 16;
  6909. break;
  6910. case SIMD_i8x16_extract_lane_s:
  6911. case SIMD_i8x16_extract_lane_u:
  6912. case SIMD_i8x16_replace_lane:
  6913. case SIMD_i16x8_extract_lane_s:
  6914. case SIMD_i16x8_extract_lane_u:
  6915. case SIMD_i16x8_replace_lane:
  6916. case SIMD_i32x4_extract_lane:
  6917. case SIMD_i32x4_replace_lane:
  6918. case SIMD_i64x2_extract_lane:
  6919. case SIMD_i64x2_replace_lane:
  6920. case SIMD_f32x4_extract_lane:
  6921. case SIMD_f32x4_replace_lane:
  6922. case SIMD_f64x2_extract_lane:
  6923. case SIMD_f64x2_replace_lane:
  6924. /* ImmLaneId */
  6925. CHECK_BUF(p, p_end, 1);
  6926. p++;
  6927. break;
  6928. case SIMD_v128_load8_lane:
  6929. case SIMD_v128_load16_lane:
  6930. case SIMD_v128_load32_lane:
  6931. case SIMD_v128_load64_lane:
  6932. case SIMD_v128_store8_lane:
  6933. case SIMD_v128_store16_lane:
  6934. case SIMD_v128_store32_lane:
  6935. case SIMD_v128_store64_lane:
  6936. /* memarg align */
  6937. skip_leb_uint32(p, p_end);
  6938. /* memarg offset */
  6939. skip_leb_mem_offset(p, p_end);
  6940. /* ImmLaneId */
  6941. CHECK_BUF(p, p_end, 1);
  6942. p++;
  6943. break;
  6944. case SIMD_v128_load32_zero:
  6945. case SIMD_v128_load64_zero:
  6946. /* memarg align */
  6947. skip_leb_uint32(p, p_end);
  6948. /* memarg offset */
  6949. skip_leb_mem_offset(p, p_end);
  6950. break;
  6951. default:
  6952. /*
  6953. * since latest SIMD specific used almost every value
  6954. * from 0x00 to 0xff, the default branch will present
  6955. * all opcodes without imm
  6956. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6957. */
  6958. break;
  6959. }
  6960. break;
  6961. }
  6962. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6963. #endif /* end of WASM_ENABLE_SIMD */
  6964. #if WASM_ENABLE_SHARED_MEMORY != 0
  6965. case WASM_OP_ATOMIC_PREFIX:
  6966. {
  6967. uint32 opcode1;
  6968. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6969. read_leb_uint32(p, p_end, opcode1);
  6970. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6971. is no larger than UINT8_MAX */
  6972. opcode = (uint8)opcode1;
  6973. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6974. skip_leb_uint32(p, p_end); /* align */
  6975. skip_leb_mem_offset(p, p_end); /* offset */
  6976. }
  6977. else {
  6978. /* atomic.fence doesn't have memarg */
  6979. p++;
  6980. }
  6981. break;
  6982. }
  6983. #endif
  6984. #if WASM_ENABLE_DEBUG_INTERP != 0
  6985. case DEBUG_OP_BREAK:
  6986. {
  6987. WASMDebugInstance *debug_instance =
  6988. wasm_exec_env_get_instance(exec_env);
  6989. char original_opcode[1];
  6990. uint64 size = 1;
  6991. WASMModuleInstance *module_inst =
  6992. (WASMModuleInstance *)exec_env->module_inst;
  6993. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6994. ? (p - 1) - module_inst->module->load_addr
  6995. : ~0;
  6996. if (debug_instance) {
  6997. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6998. original_opcode, &size)
  6999. && size == 1) {
  7000. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7001. "with %02x: ",
  7002. original_opcode[0]);
  7003. opcode = original_opcode[0];
  7004. goto op_break_retry;
  7005. }
  7006. }
  7007. break;
  7008. }
  7009. #endif
  7010. default:
  7011. return false;
  7012. }
  7013. }
  7014. (void)u8;
  7015. (void)exec_env;
  7016. return false;
  7017. fail:
  7018. return false;
  7019. }
  7020. #if WASM_ENABLE_FAST_INTERP != 0
  7021. #if WASM_DEBUG_PREPROCESSOR != 0
  7022. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7023. #else
  7024. #define LOG_OP(...) (void)0
  7025. #endif
  7026. #define PATCH_ELSE 0
  7027. #define PATCH_END 1
  7028. typedef struct BranchBlockPatch {
  7029. struct BranchBlockPatch *next;
  7030. uint8 patch_type;
  7031. uint8 *code_compiled;
  7032. } BranchBlockPatch;
  7033. #endif
  7034. typedef struct BranchBlock {
  7035. uint8 label_type;
  7036. BlockType block_type;
  7037. uint8 *start_addr;
  7038. uint8 *else_addr;
  7039. uint8 *end_addr;
  7040. uint32 stack_cell_num;
  7041. #if WASM_ENABLE_GC != 0
  7042. uint32 reftype_map_num;
  7043. /* Indicate which local is used inside current block, used to validate
  7044. * local.get with non-nullable ref types */
  7045. uint8 *local_use_mask;
  7046. uint32 local_use_mask_size;
  7047. #endif
  7048. #if WASM_ENABLE_FAST_INTERP != 0
  7049. uint16 dynamic_offset;
  7050. uint8 *code_compiled;
  7051. BranchBlockPatch *patch_list;
  7052. /* This is used to save params frame_offset of of if block */
  7053. int16 *param_frame_offsets;
  7054. /* This is used to recover the dynamic offset for else branch,
  7055. * and also to remember the start offset of dynamic space which
  7056. * stores the block arguments for loop block, so we can use it
  7057. * to copy the stack operands to the loop block's arguments in
  7058. * wasm_loader_emit_br_info for opcode br. */
  7059. uint16 start_dynamic_offset;
  7060. #endif
  7061. /* Indicate the operand stack is in polymorphic state.
  7062. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7063. * to polymorphic state until the block's 'end' opcode is processed.
  7064. * If stack is in polymorphic state and stack is empty, instruction can
  7065. * pop any type of value directly without decreasing stack top pointer
  7066. * and stack cell num. */
  7067. bool is_stack_polymorphic;
  7068. } BranchBlock;
  7069. typedef struct WASMLoaderContext {
  7070. /* frame ref stack */
  7071. uint8 *frame_ref;
  7072. uint8 *frame_ref_bottom;
  7073. uint8 *frame_ref_boundary;
  7074. uint32 frame_ref_size;
  7075. uint32 stack_cell_num;
  7076. uint32 max_stack_cell_num;
  7077. #if WASM_ENABLE_GC != 0
  7078. /* frame reftype map stack */
  7079. WASMRefTypeMap *frame_reftype_map;
  7080. WASMRefTypeMap *frame_reftype_map_bottom;
  7081. WASMRefTypeMap *frame_reftype_map_boundary;
  7082. uint32 frame_reftype_map_size;
  7083. uint32 reftype_map_num;
  7084. uint32 max_reftype_map_num;
  7085. /* Current module */
  7086. WASMModule *module;
  7087. /* Current module's ref_type_set */
  7088. HashMap *ref_type_set;
  7089. /* Always point to local variable ref_type of
  7090. wasm_loader_prepare_bytecode */
  7091. WASMRefType *ref_type_tmp;
  7092. #endif
  7093. /* frame csp stack */
  7094. BranchBlock *frame_csp;
  7095. BranchBlock *frame_csp_bottom;
  7096. BranchBlock *frame_csp_boundary;
  7097. uint32 frame_csp_size;
  7098. uint32 csp_num;
  7099. uint32 max_csp_num;
  7100. #if WASM_ENABLE_FAST_INTERP != 0
  7101. /* frame offset stack */
  7102. int16 *frame_offset;
  7103. int16 *frame_offset_bottom;
  7104. int16 *frame_offset_boundary;
  7105. uint32 frame_offset_size;
  7106. int16 dynamic_offset;
  7107. int16 start_dynamic_offset;
  7108. int16 max_dynamic_offset;
  7109. /* preserved local offset */
  7110. int16 preserved_local_offset;
  7111. /* const buffer */
  7112. uint8 *const_buf;
  7113. uint16 num_const;
  7114. uint16 const_cell_num;
  7115. uint32 const_buf_size;
  7116. /* processed code */
  7117. uint8 *p_code_compiled;
  7118. uint8 *p_code_compiled_end;
  7119. uint32 code_compiled_size;
  7120. /* If the last opcode will be dropped, the peak memory usage will be larger
  7121. * than the final code_compiled_size, we record the peak size to ensure
  7122. * there will not be invalid memory access during second traverse */
  7123. uint32 code_compiled_peak_size;
  7124. #endif
  7125. } WASMLoaderContext;
  7126. typedef struct Const {
  7127. WASMValue value;
  7128. uint16 slot_index;
  7129. uint8 value_type;
  7130. } Const;
  7131. #define CHECK_CSP_PUSH() \
  7132. do { \
  7133. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7134. MEM_REALLOC( \
  7135. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7136. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7137. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7138. ctx->frame_csp_boundary = \
  7139. ctx->frame_csp_bottom \
  7140. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7141. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7142. } \
  7143. } while (0)
  7144. #define CHECK_CSP_POP() \
  7145. do { \
  7146. if (ctx->csp_num < 1) { \
  7147. set_error_buf(error_buf, error_buf_size, \
  7148. "type mismatch: " \
  7149. "expect data but block stack was empty"); \
  7150. goto fail; \
  7151. } \
  7152. } while (0)
  7153. #if WASM_ENABLE_FAST_INTERP != 0
  7154. static bool
  7155. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7156. uint32 error_buf_size)
  7157. {
  7158. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7159. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7160. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7161. ctx->frame_offset_size + 16);
  7162. ctx->frame_offset_size += 16;
  7163. ctx->frame_offset_boundary =
  7164. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7165. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7166. }
  7167. return true;
  7168. fail:
  7169. return false;
  7170. }
  7171. static bool
  7172. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7173. {
  7174. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7175. return false;
  7176. return true;
  7177. }
  7178. static void
  7179. free_label_patch_list(BranchBlock *frame_csp)
  7180. {
  7181. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7182. BranchBlockPatch *next;
  7183. while (label_patch != NULL) {
  7184. next = label_patch->next;
  7185. wasm_runtime_free(label_patch);
  7186. label_patch = next;
  7187. }
  7188. frame_csp->patch_list = NULL;
  7189. }
  7190. static void
  7191. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7192. {
  7193. BranchBlock *tmp_csp = frame_csp;
  7194. uint32 i;
  7195. for (i = 0; i < csp_num; i++) {
  7196. free_label_patch_list(tmp_csp);
  7197. tmp_csp++;
  7198. }
  7199. }
  7200. static void
  7201. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7202. {
  7203. BranchBlock *tmp_csp = frame_csp;
  7204. uint32 i;
  7205. for (i = 0; i < csp_num; i++) {
  7206. if (tmp_csp->param_frame_offsets)
  7207. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7208. tmp_csp++;
  7209. }
  7210. }
  7211. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7212. #if WASM_ENABLE_GC != 0
  7213. static bool
  7214. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7215. char *error_buf, uint32 error_buf_size)
  7216. {
  7217. BranchBlock *current_csp = ctx->frame_csp - 1;
  7218. uint32 local_mask_size;
  7219. if (local_count == 0) {
  7220. current_csp->local_use_mask_size = 0;
  7221. return true;
  7222. }
  7223. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7224. * else branch, we don't need to allocate memory again */
  7225. if (!current_csp->local_use_mask) {
  7226. local_mask_size = (local_count + 7) / sizeof(uint8);
  7227. if (!(current_csp->local_use_mask =
  7228. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7229. return false;
  7230. }
  7231. current_csp->local_use_mask_size = local_mask_size;
  7232. }
  7233. else {
  7234. local_mask_size = current_csp->local_use_mask_size;
  7235. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7236. }
  7237. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7238. /* For non-function blocks, inherit the use status from parent block */
  7239. BranchBlock *parent_csp = current_csp - 1;
  7240. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7241. bh_assert(parent_csp->local_use_mask);
  7242. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7243. parent_csp->local_use_mask, local_mask_size);
  7244. }
  7245. return true;
  7246. }
  7247. static void
  7248. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7249. {
  7250. BranchBlock *current_csp = ctx->frame_csp - 1;
  7251. bh_assert(current_csp->local_use_mask
  7252. || current_csp->local_use_mask_size == 0);
  7253. if (current_csp->local_use_mask) {
  7254. wasm_runtime_free(current_csp->local_use_mask);
  7255. }
  7256. current_csp->local_use_mask = NULL;
  7257. current_csp->local_use_mask_size = 0;
  7258. }
  7259. static void
  7260. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7261. {
  7262. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7263. uint32 i;
  7264. for (i = 0; i < ctx->csp_num; i++) {
  7265. if (tmp_csp->local_use_mask) {
  7266. wasm_runtime_free(tmp_csp->local_use_mask);
  7267. tmp_csp->local_use_mask = NULL;
  7268. tmp_csp->local_use_mask_size = 0;
  7269. }
  7270. tmp_csp++;
  7271. }
  7272. }
  7273. static void
  7274. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7275. {
  7276. BranchBlock *current_csp = ctx->frame_csp - 1;
  7277. uint32 byte_offset = index / sizeof(uint8);
  7278. uint32 bit_offset = index % sizeof(uint8);
  7279. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7280. bh_assert(current_csp->local_use_mask);
  7281. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7282. }
  7283. static bool
  7284. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7285. {
  7286. BranchBlock *current_csp = ctx->frame_csp - 1;
  7287. uint32 byte_offset = index / sizeof(uint8);
  7288. uint32 bit_offset = index % sizeof(uint8);
  7289. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7290. bh_assert(current_csp->local_use_mask);
  7291. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7292. ? true
  7293. : false;
  7294. }
  7295. #endif /* end of WASM_ENABLE_GC != 0 */
  7296. static void
  7297. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7298. {
  7299. if (ctx) {
  7300. if (ctx->frame_ref_bottom)
  7301. wasm_runtime_free(ctx->frame_ref_bottom);
  7302. #if WASM_ENABLE_GC != 0
  7303. if (ctx->frame_reftype_map_bottom)
  7304. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7305. #endif
  7306. if (ctx->frame_csp_bottom) {
  7307. #if WASM_ENABLE_FAST_INTERP != 0
  7308. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7309. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7310. ctx->csp_num);
  7311. #endif
  7312. #if WASM_ENABLE_GC != 0
  7313. wasm_loader_clean_all_local_use_masks(ctx);
  7314. #endif
  7315. wasm_runtime_free(ctx->frame_csp_bottom);
  7316. }
  7317. #if WASM_ENABLE_FAST_INTERP != 0
  7318. if (ctx->frame_offset_bottom)
  7319. wasm_runtime_free(ctx->frame_offset_bottom);
  7320. if (ctx->const_buf)
  7321. wasm_runtime_free(ctx->const_buf);
  7322. #endif
  7323. wasm_runtime_free(ctx);
  7324. }
  7325. }
  7326. static WASMLoaderContext *
  7327. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7328. {
  7329. WASMLoaderContext *loader_ctx =
  7330. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7331. if (!loader_ctx)
  7332. return NULL;
  7333. loader_ctx->frame_ref_size = 32;
  7334. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7335. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7336. goto fail;
  7337. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7338. #if WASM_ENABLE_GC != 0
  7339. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7340. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7341. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7342. error_buf_size)))
  7343. goto fail;
  7344. loader_ctx->frame_reftype_map_boundary =
  7345. loader_ctx->frame_reftype_map_bottom + 16;
  7346. #endif
  7347. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7348. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7349. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7350. goto fail;
  7351. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7352. #if WASM_ENABLE_EXCE_HANDLING != 0
  7353. func->exception_handler_count = 0;
  7354. #endif
  7355. #if WASM_ENABLE_FAST_INTERP != 0
  7356. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7357. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7358. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7359. error_buf_size)))
  7360. goto fail;
  7361. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7362. loader_ctx->num_const = 0;
  7363. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7364. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7365. error_buf, error_buf_size)))
  7366. goto fail;
  7367. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7368. set_error_buf(error_buf, error_buf_size,
  7369. "fast interpreter offset overflow");
  7370. goto fail;
  7371. }
  7372. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7373. loader_ctx->max_dynamic_offset =
  7374. func->param_cell_num + func->local_cell_num;
  7375. #endif
  7376. return loader_ctx;
  7377. fail:
  7378. wasm_loader_ctx_destroy(loader_ctx);
  7379. return NULL;
  7380. }
  7381. static bool
  7382. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7383. uint32 error_buf_size)
  7384. {
  7385. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7386. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7387. /* Increase the frame ref stack */
  7388. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7389. ctx->frame_ref_size + 16);
  7390. ctx->frame_ref_size += 16;
  7391. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7392. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7393. }
  7394. #if WASM_ENABLE_GC != 0
  7395. if (wasm_is_type_multi_byte_type(type)
  7396. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7397. /* Increase the frame reftype map stack */
  7398. bh_assert(
  7399. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7400. * sizeof(WASMRefTypeMap))
  7401. == ctx->frame_reftype_map_size);
  7402. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7403. ctx->frame_reftype_map_size
  7404. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7405. ctx->frame_reftype_map =
  7406. ctx->frame_reftype_map_bottom
  7407. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7408. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7409. ctx->frame_reftype_map_boundary =
  7410. ctx->frame_reftype_map_bottom
  7411. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7412. }
  7413. #endif
  7414. return true;
  7415. fail:
  7416. return false;
  7417. }
  7418. static bool
  7419. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7420. uint32 error_buf_size)
  7421. {
  7422. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7423. uint32 i;
  7424. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7425. return false;
  7426. #if WASM_ENABLE_GC != 0
  7427. if (wasm_is_type_multi_byte_type(type)) {
  7428. WASMRefType *ref_type;
  7429. if (!(ref_type =
  7430. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7431. error_buf, error_buf_size))) {
  7432. return false;
  7433. }
  7434. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7435. /* Increase the frame reftype map stack */
  7436. bh_assert((uint32)((ctx->frame_reftype_map
  7437. - ctx->frame_reftype_map_bottom)
  7438. * sizeof(WASMRefTypeMap))
  7439. == ctx->frame_reftype_map_size);
  7440. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7441. ctx->frame_reftype_map_size,
  7442. ctx->frame_reftype_map_size
  7443. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7444. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7445. + ctx->frame_reftype_map_size
  7446. / ((uint32)sizeof(WASMRefTypeMap));
  7447. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7448. ctx->frame_reftype_map_boundary =
  7449. ctx->frame_reftype_map_bottom
  7450. + ctx->frame_reftype_map_size
  7451. / ((uint32)sizeof(WASMRefTypeMap));
  7452. }
  7453. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7454. ctx->frame_reftype_map->ref_type = ref_type;
  7455. ctx->frame_reftype_map++;
  7456. ctx->reftype_map_num++;
  7457. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7458. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7459. }
  7460. #endif
  7461. for (i = 0; i < type_cell_num; i++)
  7462. *ctx->frame_ref++ = type;
  7463. ctx->stack_cell_num += type_cell_num;
  7464. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7465. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7466. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7467. set_error_buf(error_buf, error_buf_size,
  7468. "operand stack depth limit exceeded");
  7469. return false;
  7470. }
  7471. }
  7472. return true;
  7473. #if WASM_ENABLE_GC != 0
  7474. fail:
  7475. return false;
  7476. #endif
  7477. }
  7478. static bool
  7479. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7480. int32 stack_cell_num,
  7481. #if WASM_ENABLE_GC != 0
  7482. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7483. #endif
  7484. uint8 type,
  7485. #if WASM_ENABLE_GC != 0
  7486. WASMRefType *ref_type,
  7487. #endif
  7488. char *error_buf, uint32 error_buf_size)
  7489. {
  7490. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7491. #if WASM_ENABLE_GC != 0
  7492. WASMRefType *frame_reftype = NULL;
  7493. #endif
  7494. if (stack_cell_num < type_cell_num) {
  7495. set_error_buf(error_buf, error_buf_size,
  7496. "type mismatch: expect data but stack was empty");
  7497. return false;
  7498. }
  7499. #if WASM_ENABLE_GC == 0
  7500. for (i = 0; i < type_cell_num; i++) {
  7501. if (*(frame_ref - 1 - i) != type) {
  7502. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7503. "type mismatch: expect ", type2str(type),
  7504. " but got other");
  7505. return false;
  7506. }
  7507. }
  7508. #else
  7509. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7510. bh_assert(reftype_map_num > 0);
  7511. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7512. }
  7513. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7514. ref_type, ctx->module->types,
  7515. ctx->module->type_count)) {
  7516. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7517. "type mismatch: expect ", type2str(type),
  7518. " but got other");
  7519. return false;
  7520. }
  7521. for (i = 0; i < type_cell_num - 1; i++) {
  7522. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7523. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7524. "type mismatch: expect ", type2str(type),
  7525. " but got other");
  7526. return false;
  7527. }
  7528. }
  7529. #endif
  7530. return true;
  7531. }
  7532. static bool
  7533. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7534. uint32 error_buf_size)
  7535. {
  7536. int32 block_stack_cell_num =
  7537. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7538. #if WASM_ENABLE_GC != 0
  7539. int32 reftype_map_num =
  7540. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7541. #endif
  7542. if (block_stack_cell_num > 0) {
  7543. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7544. /* the stack top is a value of any type, return success */
  7545. return true;
  7546. }
  7547. #if WASM_ENABLE_GC != 0
  7548. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7549. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7550. WASMRefType *stack_top_ref_type = NULL;
  7551. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7552. bh_assert(reftype_map_num > 0);
  7553. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7554. }
  7555. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7556. ctx->ref_type_tmp, ctx->module->types,
  7557. ctx->module->type_count)) {
  7558. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7559. uint32 ref_type_struct_size =
  7560. wasm_reftype_struct_size(stack_top_ref_type);
  7561. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7562. stack_top_ref_type, ref_type_struct_size);
  7563. }
  7564. return true;
  7565. }
  7566. }
  7567. #endif
  7568. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7569. #if WASM_ENABLE_GC != 0
  7570. ctx->frame_reftype_map, reftype_map_num,
  7571. #endif
  7572. type,
  7573. #if WASM_ENABLE_GC != 0
  7574. ctx->ref_type_tmp,
  7575. #endif
  7576. error_buf, error_buf_size)) {
  7577. return false;
  7578. }
  7579. return true;
  7580. }
  7581. static bool
  7582. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7583. uint32 error_buf_size)
  7584. {
  7585. BranchBlock *cur_block = ctx->frame_csp - 1;
  7586. int32 available_stack_cell =
  7587. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7588. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7589. /* Directly return success if current block is in stack
  7590. polymorphic state while stack is empty. */
  7591. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7592. return true;
  7593. if (type == VALUE_TYPE_VOID)
  7594. return true;
  7595. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7596. return false;
  7597. bh_assert(available_stack_cell > 0);
  7598. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7599. type = VALUE_TYPE_ANY;
  7600. cell_num_to_pop = 1;
  7601. }
  7602. ctx->frame_ref -= cell_num_to_pop;
  7603. ctx->stack_cell_num -= cell_num_to_pop;
  7604. #if WASM_ENABLE_GC != 0
  7605. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7606. ctx->frame_reftype_map--;
  7607. ctx->reftype_map_num--;
  7608. }
  7609. #endif
  7610. return true;
  7611. }
  7612. #if WASM_ENABLE_GC != 0
  7613. /* Get the stack top element of current block */
  7614. static bool
  7615. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7616. WASMRefType **p_ref_type, char *error_buf,
  7617. uint32 error_buf_size)
  7618. {
  7619. BranchBlock *cur_block = ctx->frame_csp - 1;
  7620. int32 available_stack_cell =
  7621. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7622. if (available_stack_cell <= 0) {
  7623. /* Directly return success if current block is in stack
  7624. polymorphic state while stack is empty. */
  7625. if (cur_block->is_stack_polymorphic) {
  7626. *p_type = VALUE_TYPE_ANY;
  7627. return true;
  7628. }
  7629. else {
  7630. set_error_buf(
  7631. error_buf, error_buf_size,
  7632. "type mismatch: expect data but block stack was empty");
  7633. return false;
  7634. }
  7635. }
  7636. *p_type = *(ctx->frame_ref - 1);
  7637. if (wasm_is_type_multi_byte_type(*p_type)) {
  7638. int32 available_reftype_map =
  7639. (int32)(ctx->reftype_map_num
  7640. - (ctx->frame_csp - 1)->reftype_map_num);
  7641. bh_assert(available_reftype_map > 0);
  7642. (void)available_reftype_map;
  7643. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7644. }
  7645. return true;
  7646. }
  7647. #if WASM_ENABLE_FAST_INTERP != 0
  7648. static bool
  7649. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7650. char *error_buf, uint32 error_buf_size);
  7651. #endif
  7652. /* Check whether the stack top elem is a heap object, and if yes,
  7653. pop and return it */
  7654. static bool
  7655. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7656. WASMRefType *ref_ht_ret, char *error_buf,
  7657. uint32 error_buf_size)
  7658. {
  7659. uint8 type = 0;
  7660. WASMRefType *ref_type = NULL;
  7661. /* Get stack top element */
  7662. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7663. error_buf_size)) {
  7664. return false;
  7665. }
  7666. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7667. /* stack top isn't a ref type */
  7668. && !wasm_is_type_reftype(type)) {
  7669. set_error_buf(error_buf, error_buf_size,
  7670. "type mismatch: expect heap object but got others");
  7671. return false;
  7672. }
  7673. /* POP stack top */
  7674. if (wasm_is_type_multi_byte_type(type)) {
  7675. bh_assert(ref_type);
  7676. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7677. wasm_reftype_struct_size(ref_type));
  7678. }
  7679. #if WASM_ENABLE_FAST_INTERP != 0
  7680. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7681. error_buf_size)) {
  7682. return false;
  7683. }
  7684. #else
  7685. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7686. return false;
  7687. }
  7688. #endif
  7689. if (p_type)
  7690. *p_type = type;
  7691. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7692. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7693. wasm_reftype_struct_size(ref_type));
  7694. }
  7695. return true;
  7696. }
  7697. /* Check whether the stack top elem is subtype of (ref null ht),
  7698. and if yes, pop it and return the converted (ref ht) */
  7699. static bool
  7700. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7701. WASMRefType *ref_ht_ret, char *error_buf,
  7702. uint32 error_buf_size)
  7703. {
  7704. uint8 type = 0;
  7705. WASMRefType ref_type = { 0 };
  7706. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7707. error_buf_size)) {
  7708. return false;
  7709. }
  7710. /* Convert to related (ref ht) and return */
  7711. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7712. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7713. */
  7714. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7715. HEAP_TYPE_ARRAY
  7716. + (type - REF_TYPE_ARRAYREF));
  7717. type = ref_ht_ret->ref_type;
  7718. }
  7719. else if (wasm_is_reftype_htref_nullable(type)
  7720. || wasm_is_reftype_htref_non_nullable(type)) {
  7721. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7722. wasm_reftype_struct_size(&ref_type));
  7723. /* Convert to (ref ht) */
  7724. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7725. ref_ht_ret->ref_ht_common.nullable = false;
  7726. type = ref_ht_ret->ref_type;
  7727. }
  7728. *p_type = type;
  7729. return true;
  7730. }
  7731. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7732. and if yes, pop it and return the type_idx */
  7733. static bool
  7734. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7735. uint32 *p_type_idx, char *error_buf,
  7736. uint32 error_buf_size)
  7737. {
  7738. uint8 type = 0;
  7739. int32 type_idx = -1;
  7740. WASMRefType *ref_type = NULL;
  7741. /* Get stack top element */
  7742. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7743. error_buf_size)) {
  7744. return false;
  7745. }
  7746. if (type != VALUE_TYPE_ANY) {
  7747. /* stack top isn't (ref null $t) */
  7748. if (!((wasm_is_reftype_htref_nullable(type)
  7749. || wasm_is_reftype_htref_non_nullable(type))
  7750. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7751. set_error_buf(error_buf, error_buf_size,
  7752. "type mismatch: expect (ref null $t) but got others");
  7753. return false;
  7754. }
  7755. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7756. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7757. wasm_reftype_struct_size(ref_type));
  7758. }
  7759. /* POP stack top */
  7760. #if WASM_ENABLE_FAST_INTERP != 0
  7761. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7762. error_buf_size)) {
  7763. return false;
  7764. }
  7765. #else
  7766. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7767. return false;
  7768. }
  7769. #endif
  7770. /* Convert to type_idx and return */
  7771. *p_type = type;
  7772. if (type != VALUE_TYPE_ANY)
  7773. *p_type_idx = (uint32)type_idx;
  7774. return true;
  7775. }
  7776. #endif /* WASM_ENABLE_GC != 0 */
  7777. #if WASM_ENABLE_FAST_INTERP == 0
  7778. static bool
  7779. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7780. uint8 type_push, uint8 type_pop, char *error_buf,
  7781. uint32 error_buf_size)
  7782. {
  7783. for (int i = 0; i < pop_cnt; i++) {
  7784. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7785. error_buf_size))
  7786. return false;
  7787. }
  7788. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7789. return false;
  7790. return true;
  7791. }
  7792. #endif
  7793. static bool
  7794. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7795. BlockType block_type, uint8 *start_addr,
  7796. char *error_buf, uint32 error_buf_size)
  7797. {
  7798. CHECK_CSP_PUSH();
  7799. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7800. ctx->frame_csp->label_type = label_type;
  7801. ctx->frame_csp->block_type = block_type;
  7802. ctx->frame_csp->start_addr = start_addr;
  7803. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7804. #if WASM_ENABLE_GC != 0
  7805. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7806. #endif
  7807. #if WASM_ENABLE_FAST_INTERP != 0
  7808. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7809. ctx->frame_csp->patch_list = NULL;
  7810. #endif
  7811. ctx->frame_csp++;
  7812. ctx->csp_num++;
  7813. if (ctx->csp_num > ctx->max_csp_num) {
  7814. ctx->max_csp_num = ctx->csp_num;
  7815. if (ctx->max_csp_num > UINT16_MAX) {
  7816. set_error_buf(error_buf, error_buf_size,
  7817. "label stack depth limit exceeded");
  7818. return false;
  7819. }
  7820. }
  7821. return true;
  7822. fail:
  7823. return false;
  7824. }
  7825. static bool
  7826. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7827. uint32 error_buf_size)
  7828. {
  7829. CHECK_CSP_POP();
  7830. #if WASM_ENABLE_FAST_INTERP != 0
  7831. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7832. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7833. #endif
  7834. ctx->frame_csp--;
  7835. ctx->csp_num--;
  7836. return true;
  7837. fail:
  7838. return false;
  7839. }
  7840. #if WASM_ENABLE_FAST_INTERP != 0
  7841. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7842. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7843. #define emit_label(opcode) \
  7844. do { \
  7845. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7846. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7847. } while (0)
  7848. #define skip_label() \
  7849. do { \
  7850. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7851. LOG_OP("\ndelete last op\n"); \
  7852. } while (0)
  7853. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7854. #if UINTPTR_MAX == UINT64_MAX
  7855. #define emit_label(opcode) \
  7856. do { \
  7857. int32 offset = \
  7858. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7859. /* emit int32 relative offset in 64-bit target */ \
  7860. wasm_loader_emit_uint32(loader_ctx, offset); \
  7861. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7862. } while (0)
  7863. #else
  7864. #define emit_label(opcode) \
  7865. do { \
  7866. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7867. /* emit uint32 label address in 32-bit target */ \
  7868. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7869. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7870. } while (0)
  7871. #endif
  7872. #define skip_label() \
  7873. do { \
  7874. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7875. LOG_OP("\ndelete last op\n"); \
  7876. } while (0)
  7877. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7878. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7879. #define emit_label(opcode) \
  7880. do { \
  7881. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7882. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7883. } while (0)
  7884. #define skip_label() \
  7885. do { \
  7886. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7887. LOG_OP("\ndelete last op\n"); \
  7888. } while (0)
  7889. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7890. #define emit_empty_label_addr_and_frame_ip(type) \
  7891. do { \
  7892. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7893. loader_ctx->p_code_compiled, error_buf, \
  7894. error_buf_size)) \
  7895. goto fail; \
  7896. /* label address, to be patched */ \
  7897. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7898. } while (0)
  7899. #define emit_br_info(frame_csp, is_br) \
  7900. do { \
  7901. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7902. error_buf_size)) \
  7903. goto fail; \
  7904. } while (0)
  7905. #define LAST_OP_OUTPUT_I32() \
  7906. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7907. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7908. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7909. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7910. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7911. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7912. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7913. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7914. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7915. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7916. || (last_op == EXT_OP_COPY_STACK_TOP)
  7917. #define LAST_OP_OUTPUT_I64() \
  7918. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7919. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7920. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7921. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7922. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7923. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7924. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7925. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7926. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7927. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7928. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7929. #define GET_CONST_OFFSET(type, val) \
  7930. do { \
  7931. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7932. &operand_offset, error_buf, \
  7933. error_buf_size))) \
  7934. goto fail; \
  7935. } while (0)
  7936. #define GET_CONST_F32_OFFSET(type, fval) \
  7937. do { \
  7938. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7939. &operand_offset, error_buf, \
  7940. error_buf_size))) \
  7941. goto fail; \
  7942. } while (0)
  7943. #define GET_CONST_F64_OFFSET(type, fval) \
  7944. do { \
  7945. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7946. &operand_offset, error_buf, \
  7947. error_buf_size))) \
  7948. goto fail; \
  7949. } while (0)
  7950. #define emit_operand(ctx, offset) \
  7951. do { \
  7952. wasm_loader_emit_int16(ctx, offset); \
  7953. LOG_OP("%d\t", offset); \
  7954. } while (0)
  7955. #define emit_byte(ctx, byte) \
  7956. do { \
  7957. wasm_loader_emit_uint8(ctx, byte); \
  7958. LOG_OP("%d\t", byte); \
  7959. } while (0)
  7960. #define emit_uint32(ctx, value) \
  7961. do { \
  7962. wasm_loader_emit_uint32(ctx, value); \
  7963. LOG_OP("%d\t", value); \
  7964. } while (0)
  7965. #define emit_uint64(ctx, value) \
  7966. do { \
  7967. wasm_loader_emit_const(ctx, &value, false); \
  7968. LOG_OP("%lld\t", value); \
  7969. } while (0)
  7970. #define emit_float32(ctx, value) \
  7971. do { \
  7972. wasm_loader_emit_const(ctx, &value, true); \
  7973. LOG_OP("%f\t", value); \
  7974. } while (0)
  7975. #define emit_float64(ctx, value) \
  7976. do { \
  7977. wasm_loader_emit_const(ctx, &value, false); \
  7978. LOG_OP("%f\t", value); \
  7979. } while (0)
  7980. static bool
  7981. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7982. {
  7983. if (!(ctx->p_code_compiled =
  7984. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7985. return false;
  7986. ctx->p_code_compiled_end =
  7987. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7988. /* clean up frame ref */
  7989. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7990. ctx->frame_ref = ctx->frame_ref_bottom;
  7991. ctx->stack_cell_num = 0;
  7992. #if WASM_ENABLE_GC != 0
  7993. /* clean up reftype map */
  7994. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7995. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7996. ctx->reftype_map_num = 0;
  7997. #endif
  7998. /* clean up frame csp */
  7999. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8000. ctx->frame_csp = ctx->frame_csp_bottom;
  8001. ctx->csp_num = 0;
  8002. ctx->max_csp_num = 0;
  8003. /* clean up frame offset */
  8004. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8005. ctx->frame_offset = ctx->frame_offset_bottom;
  8006. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8007. /* init preserved local offsets */
  8008. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8009. /* const buf is reserved */
  8010. return true;
  8011. }
  8012. static void
  8013. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8014. {
  8015. ctx->code_compiled_size += size;
  8016. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8017. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8018. }
  8019. }
  8020. static void
  8021. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8022. {
  8023. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8024. if (ctx->p_code_compiled) {
  8025. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8026. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8027. #endif
  8028. bh_memcpy_s(ctx->p_code_compiled,
  8029. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8030. value, size);
  8031. ctx->p_code_compiled += size;
  8032. }
  8033. else {
  8034. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8035. bh_assert((ctx->code_compiled_size & 1) == 0);
  8036. #endif
  8037. increase_compiled_code_space(ctx, size);
  8038. }
  8039. }
  8040. static void
  8041. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8042. {
  8043. if (ctx->p_code_compiled) {
  8044. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8045. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8046. #endif
  8047. STORE_U32(ctx->p_code_compiled, value);
  8048. ctx->p_code_compiled += sizeof(uint32);
  8049. }
  8050. else {
  8051. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8052. bh_assert((ctx->code_compiled_size & 1) == 0);
  8053. #endif
  8054. increase_compiled_code_space(ctx, sizeof(uint32));
  8055. }
  8056. }
  8057. static void
  8058. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8059. {
  8060. if (ctx->p_code_compiled) {
  8061. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8062. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8063. #endif
  8064. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8065. ctx->p_code_compiled += sizeof(int16);
  8066. }
  8067. else {
  8068. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8069. bh_assert((ctx->code_compiled_size & 1) == 0);
  8070. #endif
  8071. increase_compiled_code_space(ctx, sizeof(uint16));
  8072. }
  8073. }
  8074. static void
  8075. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8076. {
  8077. if (ctx->p_code_compiled) {
  8078. *(ctx->p_code_compiled) = value;
  8079. ctx->p_code_compiled += sizeof(uint8);
  8080. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8081. ctx->p_code_compiled++;
  8082. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8083. #endif
  8084. }
  8085. else {
  8086. increase_compiled_code_space(ctx, sizeof(uint8));
  8087. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8088. increase_compiled_code_space(ctx, sizeof(uint8));
  8089. bh_assert((ctx->code_compiled_size & 1) == 0);
  8090. #endif
  8091. }
  8092. }
  8093. static void
  8094. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8095. {
  8096. if (ctx->p_code_compiled) {
  8097. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8098. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8099. #endif
  8100. STORE_PTR(ctx->p_code_compiled, value);
  8101. ctx->p_code_compiled += sizeof(void *);
  8102. }
  8103. else {
  8104. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8105. bh_assert((ctx->code_compiled_size & 1) == 0);
  8106. #endif
  8107. increase_compiled_code_space(ctx, sizeof(void *));
  8108. }
  8109. }
  8110. static void
  8111. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8112. {
  8113. if (ctx->p_code_compiled) {
  8114. ctx->p_code_compiled -= size;
  8115. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8116. if (size == sizeof(uint8)) {
  8117. ctx->p_code_compiled--;
  8118. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8119. }
  8120. #endif
  8121. }
  8122. else {
  8123. ctx->code_compiled_size -= size;
  8124. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8125. if (size == sizeof(uint8)) {
  8126. ctx->code_compiled_size--;
  8127. bh_assert((ctx->code_compiled_size & 1) == 0);
  8128. }
  8129. #endif
  8130. }
  8131. }
  8132. static bool
  8133. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8134. uint32 local_index, uint32 local_type,
  8135. bool *preserved, char *error_buf,
  8136. uint32 error_buf_size)
  8137. {
  8138. uint32 i = 0;
  8139. int16 preserved_offset = (int16)local_index;
  8140. *preserved = false;
  8141. while (i < loader_ctx->stack_cell_num) {
  8142. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8143. /* move previous local into dynamic space before a set/tee_local opcode
  8144. */
  8145. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8146. if (!(*preserved)) {
  8147. *preserved = true;
  8148. skip_label();
  8149. preserved_offset = loader_ctx->preserved_local_offset;
  8150. if (loader_ctx->p_code_compiled) {
  8151. bh_assert(preserved_offset != (int16)local_index);
  8152. }
  8153. if (is_32bit_type(local_type)) {
  8154. /* Only increase preserve offset in the second traversal */
  8155. if (loader_ctx->p_code_compiled)
  8156. loader_ctx->preserved_local_offset++;
  8157. emit_label(EXT_OP_COPY_STACK_TOP);
  8158. }
  8159. else {
  8160. if (loader_ctx->p_code_compiled)
  8161. loader_ctx->preserved_local_offset += 2;
  8162. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8163. }
  8164. emit_operand(loader_ctx, local_index);
  8165. emit_operand(loader_ctx, preserved_offset);
  8166. emit_label(opcode);
  8167. }
  8168. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8169. }
  8170. if (is_32bit_type(cur_type))
  8171. i++;
  8172. else
  8173. i += 2;
  8174. }
  8175. (void)error_buf;
  8176. (void)error_buf_size;
  8177. return true;
  8178. }
  8179. static bool
  8180. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8181. char *error_buf, uint32 error_buf_size)
  8182. {
  8183. uint32 i = 0;
  8184. bool preserve_local;
  8185. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8186. blocks will not influence the value of these locals */
  8187. while (i < loader_ctx->stack_cell_num) {
  8188. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8189. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8190. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8191. && (cur_offset >= 0)) {
  8192. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8193. cur_type, &preserve_local,
  8194. error_buf, error_buf_size)))
  8195. return false;
  8196. }
  8197. if (is_32bit_type(cur_type)) {
  8198. i++;
  8199. }
  8200. else {
  8201. i += 2;
  8202. }
  8203. }
  8204. return true;
  8205. }
  8206. static bool
  8207. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8208. uint8 *p_code_compiled, char *error_buf,
  8209. uint32 error_buf_size)
  8210. {
  8211. BranchBlockPatch *patch =
  8212. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8213. if (!patch) {
  8214. return false;
  8215. }
  8216. patch->patch_type = patch_type;
  8217. patch->code_compiled = p_code_compiled;
  8218. if (!frame_csp->patch_list) {
  8219. frame_csp->patch_list = patch;
  8220. patch->next = NULL;
  8221. }
  8222. else {
  8223. patch->next = frame_csp->patch_list;
  8224. frame_csp->patch_list = patch;
  8225. }
  8226. return true;
  8227. }
  8228. static void
  8229. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8230. {
  8231. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8232. BranchBlockPatch *node = frame_csp->patch_list;
  8233. BranchBlockPatch *node_prev = NULL, *node_next;
  8234. if (!ctx->p_code_compiled)
  8235. return;
  8236. while (node) {
  8237. node_next = node->next;
  8238. if (node->patch_type == patch_type) {
  8239. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8240. if (node_prev == NULL) {
  8241. frame_csp->patch_list = node_next;
  8242. }
  8243. else {
  8244. node_prev->next = node_next;
  8245. }
  8246. wasm_runtime_free(node);
  8247. }
  8248. else {
  8249. node_prev = node;
  8250. }
  8251. node = node_next;
  8252. }
  8253. }
  8254. static bool
  8255. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8256. bool is_br, char *error_buf, uint32 error_buf_size)
  8257. {
  8258. /* br info layout:
  8259. * a) arity of target block
  8260. * b) total cell num of arity values
  8261. * c) each arity value's cell num
  8262. * d) each arity value's src frame offset
  8263. * e) each arity values's dst dynamic offset
  8264. * f) branch target address
  8265. *
  8266. * Note: b-e are omitted when arity is 0 so that
  8267. * interpreter can recover the br info quickly.
  8268. */
  8269. BlockType *block_type = &frame_csp->block_type;
  8270. uint8 *types = NULL, cell;
  8271. #if WASM_ENABLE_GC != 0
  8272. WASMRefTypeMap *reftype_maps;
  8273. uint32 reftype_map_count;
  8274. #endif
  8275. uint32 arity = 0;
  8276. int32 i;
  8277. int16 *frame_offset = ctx->frame_offset;
  8278. uint16 dynamic_offset;
  8279. /* Note: loop's arity is different from if and block. loop's arity is
  8280. * its parameter count while if and block arity is result count.
  8281. */
  8282. #if WASM_ENABLE_GC == 0
  8283. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8284. arity = block_type_get_param_types(block_type, &types);
  8285. else
  8286. arity = block_type_get_result_types(block_type, &types);
  8287. #else
  8288. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8289. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8290. &reftype_map_count);
  8291. else
  8292. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8293. &reftype_map_count);
  8294. #endif
  8295. /* Part a */
  8296. emit_uint32(ctx, arity);
  8297. if (arity) {
  8298. /* Part b */
  8299. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8300. /* Part c */
  8301. for (i = (int32)arity - 1; i >= 0; i--) {
  8302. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8303. emit_byte(ctx, cell);
  8304. }
  8305. /* Part d */
  8306. for (i = (int32)arity - 1; i >= 0; i--) {
  8307. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8308. frame_offset -= cell;
  8309. emit_operand(ctx, *(int16 *)(frame_offset));
  8310. }
  8311. /* Part e */
  8312. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8313. /* Use start_dynamic_offset which was set in
  8314. copy_params_to_dynamic_space */
  8315. dynamic_offset = frame_csp->start_dynamic_offset
  8316. + wasm_get_cell_num(types, arity);
  8317. else
  8318. dynamic_offset =
  8319. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8320. if (is_br)
  8321. ctx->dynamic_offset = dynamic_offset;
  8322. for (i = (int32)arity - 1; i >= 0; i--) {
  8323. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8324. dynamic_offset -= cell;
  8325. emit_operand(ctx, dynamic_offset);
  8326. }
  8327. }
  8328. /* Part f */
  8329. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8330. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8331. }
  8332. else {
  8333. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8334. error_buf, error_buf_size))
  8335. return false;
  8336. /* label address, to be patched */
  8337. wasm_loader_emit_ptr(ctx, NULL);
  8338. }
  8339. return true;
  8340. }
  8341. static bool
  8342. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8343. bool disable_emit, int16 operand_offset,
  8344. char *error_buf, uint32 error_buf_size)
  8345. {
  8346. uint32 cell_num_to_push, i;
  8347. if (type == VALUE_TYPE_VOID)
  8348. return true;
  8349. /* only check memory overflow in first traverse */
  8350. if (ctx->p_code_compiled == NULL) {
  8351. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8352. return false;
  8353. }
  8354. if (disable_emit)
  8355. *(ctx->frame_offset)++ = operand_offset;
  8356. else {
  8357. emit_operand(ctx, ctx->dynamic_offset);
  8358. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8359. ctx->dynamic_offset++;
  8360. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8361. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8362. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8363. goto fail;
  8364. }
  8365. }
  8366. }
  8367. if (is_32bit_type(type))
  8368. return true;
  8369. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8370. for (i = 0; i < cell_num_to_push; i++) {
  8371. if (ctx->p_code_compiled == NULL) {
  8372. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8373. return false;
  8374. }
  8375. ctx->frame_offset++;
  8376. if (!disable_emit) {
  8377. ctx->dynamic_offset++;
  8378. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8379. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8380. if (ctx->max_dynamic_offset >= INT16_MAX)
  8381. goto fail;
  8382. }
  8383. }
  8384. }
  8385. return true;
  8386. fail:
  8387. set_error_buf(error_buf, error_buf_size,
  8388. "fast interpreter offset overflow");
  8389. return false;
  8390. }
  8391. /* This function should be in front of wasm_loader_pop_frame_ref
  8392. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8393. will be modified by wasm_loader_pop_frame_ref */
  8394. static bool
  8395. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8396. char *error_buf, uint32 error_buf_size)
  8397. {
  8398. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8399. then current block is the function block */
  8400. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8401. BranchBlock *cur_block = ctx->frame_csp - depth;
  8402. int32 available_stack_cell =
  8403. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8404. uint32 cell_num_to_pop;
  8405. /* Directly return success if current block is in stack
  8406. polymorphic state while stack is empty. */
  8407. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8408. return true;
  8409. if (type == VALUE_TYPE_VOID)
  8410. return true;
  8411. /* Change type to ANY when the stack top is ANY, so as to avoid
  8412. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8413. two offsets */
  8414. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8415. type = VALUE_TYPE_ANY;
  8416. cell_num_to_pop = wasm_value_type_cell_num(type);
  8417. /* Check the offset stack bottom to ensure the frame offset
  8418. stack will not go underflow. But we don't thrown error
  8419. and return true here, because the error msg should be
  8420. given in wasm_loader_pop_frame_ref */
  8421. if (!check_offset_pop(ctx, cell_num_to_pop))
  8422. return true;
  8423. ctx->frame_offset -= cell_num_to_pop;
  8424. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8425. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8426. ctx->dynamic_offset -= cell_num_to_pop;
  8427. emit_operand(ctx, *(ctx->frame_offset));
  8428. (void)error_buf;
  8429. (void)error_buf_size;
  8430. return true;
  8431. }
  8432. static bool
  8433. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8434. bool disable_emit, int16 operand_offset,
  8435. char *error_buf, uint32 error_buf_size)
  8436. {
  8437. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8438. error_buf, error_buf_size)))
  8439. return false;
  8440. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8441. return false;
  8442. return true;
  8443. }
  8444. static bool
  8445. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8446. char *error_buf, uint32 error_buf_size)
  8447. {
  8448. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8449. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8450. return false;
  8451. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8452. return false;
  8453. return true;
  8454. }
  8455. static bool
  8456. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8457. uint8 type_push, uint8 type_pop,
  8458. bool disable_emit, int16 operand_offset,
  8459. char *error_buf, uint32 error_buf_size)
  8460. {
  8461. uint8 i;
  8462. for (i = 0; i < pop_cnt; i++) {
  8463. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8464. error_buf_size))
  8465. return false;
  8466. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8467. error_buf_size))
  8468. return false;
  8469. }
  8470. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8471. operand_offset, error_buf,
  8472. error_buf_size))
  8473. return false;
  8474. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8475. return false;
  8476. return true;
  8477. }
  8478. static bool
  8479. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8480. int16 *offset, char *error_buf,
  8481. uint32 error_buf_size)
  8482. {
  8483. int8 bytes_to_increase;
  8484. int16 operand_offset = 0;
  8485. Const *c;
  8486. /* Search existing constant */
  8487. for (c = (Const *)ctx->const_buf;
  8488. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8489. /* TODO: handle v128 type? */
  8490. if ((type == c->value_type)
  8491. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8492. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8493. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8494. || (type == VALUE_TYPE_FUNCREF
  8495. && *(int32 *)value == c->value.i32)
  8496. || (type == VALUE_TYPE_EXTERNREF
  8497. && *(int32 *)value == c->value.i32)
  8498. #endif
  8499. || (type == VALUE_TYPE_F64
  8500. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8501. || (type == VALUE_TYPE_F32
  8502. && (0
  8503. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8504. operand_offset = c->slot_index;
  8505. break;
  8506. }
  8507. if (is_32bit_type(c->value_type))
  8508. operand_offset += 1;
  8509. else
  8510. operand_offset += 2;
  8511. }
  8512. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8513. /* New constant, append to the const buffer */
  8514. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8515. bytes_to_increase = 2;
  8516. }
  8517. else {
  8518. bytes_to_increase = 1;
  8519. }
  8520. /* The max cell num of const buffer is 32768 since the valid index range
  8521. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8522. * full */
  8523. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8524. *offset = 0;
  8525. return true;
  8526. }
  8527. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8528. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8529. ctx->const_buf_size + 4 * sizeof(Const));
  8530. ctx->const_buf_size += 4 * sizeof(Const);
  8531. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8532. }
  8533. c->value_type = type;
  8534. switch (type) {
  8535. case VALUE_TYPE_F64:
  8536. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8537. sizeof(float64));
  8538. ctx->const_cell_num += 2;
  8539. /* The const buf will be reversed, we use the second cell */
  8540. /* of the i64/f64 const so the final offset is correct */
  8541. operand_offset++;
  8542. break;
  8543. case VALUE_TYPE_I64:
  8544. c->value.i64 = *(int64 *)value;
  8545. ctx->const_cell_num += 2;
  8546. operand_offset++;
  8547. break;
  8548. case VALUE_TYPE_F32:
  8549. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8550. sizeof(float32));
  8551. ctx->const_cell_num++;
  8552. break;
  8553. case VALUE_TYPE_I32:
  8554. c->value.i32 = *(int32 *)value;
  8555. ctx->const_cell_num++;
  8556. break;
  8557. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8558. case VALUE_TYPE_EXTERNREF:
  8559. case VALUE_TYPE_FUNCREF:
  8560. c->value.i32 = *(int32 *)value;
  8561. ctx->const_cell_num++;
  8562. break;
  8563. #endif
  8564. default:
  8565. break;
  8566. }
  8567. c->slot_index = operand_offset;
  8568. ctx->num_const++;
  8569. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8570. (int64)c->value.i64);
  8571. }
  8572. /* use negative index for const */
  8573. operand_offset = -(operand_offset + 1);
  8574. *offset = operand_offset;
  8575. return true;
  8576. fail:
  8577. return false;
  8578. }
  8579. /*
  8580. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8581. -- Mostly used for the binary / compare operation
  8582. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8583. -- Mostly used in block / control instructions
  8584. The POP will always emit the offset on the top of the frame_offset stack
  8585. PUSH can be used in two ways:
  8586. 1. directly PUSH:
  8587. PUSH_XXX();
  8588. will allocate a dynamic space and emit
  8589. 2. silent PUSH:
  8590. operand_offset = xxx; disable_emit = true;
  8591. PUSH_XXX();
  8592. only push the frame_offset stack, no emit
  8593. */
  8594. #define TEMPLATE_PUSH(Type) \
  8595. do { \
  8596. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8597. disable_emit, operand_offset, \
  8598. error_buf, error_buf_size)) \
  8599. goto fail; \
  8600. } while (0)
  8601. #define TEMPLATE_PUSH_REF(Type) \
  8602. do { \
  8603. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8604. operand_offset, error_buf, \
  8605. error_buf_size)) \
  8606. goto fail; \
  8607. } while (0)
  8608. #define TEMPLATE_POP(Type) \
  8609. do { \
  8610. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8611. error_buf, error_buf_size)) \
  8612. goto fail; \
  8613. } while (0)
  8614. #define TEMPLATE_POP_REF(Type) \
  8615. do { \
  8616. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8617. error_buf_size)) \
  8618. goto fail; \
  8619. } while (0)
  8620. #define PUSH_OFFSET_TYPE(type) \
  8621. do { \
  8622. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8623. operand_offset, error_buf, \
  8624. error_buf_size))) \
  8625. goto fail; \
  8626. } while (0)
  8627. #define POP_OFFSET_TYPE(type) \
  8628. do { \
  8629. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8630. error_buf_size))) \
  8631. goto fail; \
  8632. } while (0)
  8633. #define POP_AND_PUSH(type_pop, type_push) \
  8634. do { \
  8635. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8636. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8637. operand_offset, error_buf, error_buf_size))) \
  8638. goto fail; \
  8639. } while (0)
  8640. /* type of POPs should be the same */
  8641. #define POP2_AND_PUSH(type_pop, type_push) \
  8642. do { \
  8643. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8644. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8645. operand_offset, error_buf, error_buf_size))) \
  8646. goto fail; \
  8647. } while (0)
  8648. #else /* WASM_ENABLE_FAST_INTERP */
  8649. #define TEMPLATE_PUSH(Type) \
  8650. do { \
  8651. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8652. error_buf, error_buf_size))) \
  8653. goto fail; \
  8654. } while (0)
  8655. #define TEMPLATE_PUSH_REF(Type) \
  8656. do { \
  8657. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8658. error_buf_size))) \
  8659. goto fail; \
  8660. } while (0)
  8661. #define TEMPLATE_POP(Type) \
  8662. do { \
  8663. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8664. error_buf, error_buf_size))) \
  8665. goto fail; \
  8666. } while (0)
  8667. #define TEMPLATE_POP_REF(Type) \
  8668. do { \
  8669. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8670. error_buf_size))) \
  8671. goto fail; \
  8672. } while (0)
  8673. #define POP_AND_PUSH(type_pop, type_push) \
  8674. do { \
  8675. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8676. type_pop, error_buf, \
  8677. error_buf_size))) \
  8678. goto fail; \
  8679. } while (0)
  8680. /* type of POPs should be the same */
  8681. #define POP2_AND_PUSH(type_pop, type_push) \
  8682. do { \
  8683. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8684. type_pop, error_buf, \
  8685. error_buf_size))) \
  8686. goto fail; \
  8687. } while (0)
  8688. #endif /* WASM_ENABLE_FAST_INTERP */
  8689. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8690. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8691. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8692. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8693. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8694. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8695. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8696. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8697. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8698. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8699. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8700. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8701. #define POP_I32() TEMPLATE_POP(I32)
  8702. #define POP_F32() TEMPLATE_POP(F32)
  8703. #define POP_I64() TEMPLATE_POP(I64)
  8704. #define POP_F64() TEMPLATE_POP(F64)
  8705. #define POP_V128() TEMPLATE_POP(V128)
  8706. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8707. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8708. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8709. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8710. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8711. #if WASM_ENABLE_FAST_INTERP != 0
  8712. static bool
  8713. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8714. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8715. {
  8716. int16 operand_offset = 0;
  8717. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8718. : loader_ctx->frame_csp;
  8719. BlockType *block_type = &block->block_type;
  8720. uint8 *return_types = NULL;
  8721. #if WASM_ENABLE_GC != 0
  8722. WASMRefTypeMap *reftype_maps = NULL;
  8723. uint32 reftype_map_count;
  8724. #endif
  8725. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8726. int32 i = 0;
  8727. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8728. *frame_offset_org = NULL;
  8729. #if WASM_ENABLE_GC == 0
  8730. return_count = block_type_get_result_types(block_type, &return_types);
  8731. #else
  8732. return_count = block_type_get_result_types(
  8733. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8734. #endif
  8735. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8736. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8737. if (return_count == 1) {
  8738. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8739. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8740. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8741. /* insert op_copy before else opcode */
  8742. if (opcode == WASM_OP_ELSE)
  8743. skip_label();
  8744. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8745. : EXT_OP_COPY_STACK_TOP_I64);
  8746. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8747. emit_operand(loader_ctx, block->dynamic_offset);
  8748. if (opcode == WASM_OP_ELSE) {
  8749. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8750. }
  8751. else {
  8752. loader_ctx->frame_offset -= cell;
  8753. loader_ctx->dynamic_offset = block->dynamic_offset;
  8754. PUSH_OFFSET_TYPE(return_types[0]);
  8755. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8756. }
  8757. if (opcode == WASM_OP_ELSE)
  8758. emit_label(opcode);
  8759. }
  8760. return true;
  8761. }
  8762. /* Copy stack top values to block's results which are in dynamic space.
  8763. * The instruction format:
  8764. * Part a: values count
  8765. * Part b: all values total cell num
  8766. * Part c: each value's cell_num, src offset and dst offset
  8767. * Part d: each value's src offset and dst offset
  8768. * Part e: each value's dst offset
  8769. */
  8770. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8771. dynamic_offset = dynamic_offset_org =
  8772. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8773. /* First traversal to get the count of values needed to be copied. */
  8774. for (i = (int32)return_count - 1; i >= 0; i--) {
  8775. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8776. frame_offset -= cells;
  8777. dynamic_offset -= cells;
  8778. if (dynamic_offset != *frame_offset) {
  8779. value_count++;
  8780. total_cel_num += cells;
  8781. }
  8782. }
  8783. if (value_count) {
  8784. uint32 j = 0;
  8785. uint8 *emit_data = NULL, *cells = NULL;
  8786. int16 *src_offsets = NULL;
  8787. uint16 *dst_offsets = NULL;
  8788. uint64 size =
  8789. (uint64)value_count
  8790. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8791. /* Allocate memory for the emit data */
  8792. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8793. return false;
  8794. cells = emit_data;
  8795. src_offsets = (int16 *)(cells + value_count);
  8796. dst_offsets = (uint16 *)(src_offsets + value_count);
  8797. /* insert op_copy before else opcode */
  8798. if (opcode == WASM_OP_ELSE)
  8799. skip_label();
  8800. emit_label(EXT_OP_COPY_STACK_VALUES);
  8801. /* Part a) */
  8802. emit_uint32(loader_ctx, value_count);
  8803. /* Part b) */
  8804. emit_uint32(loader_ctx, total_cel_num);
  8805. /* Second traversal to get each value's cell num, src offset and dst
  8806. * offset. */
  8807. frame_offset = frame_offset_org;
  8808. dynamic_offset = dynamic_offset_org;
  8809. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8810. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8811. frame_offset -= cell;
  8812. dynamic_offset -= cell;
  8813. if (dynamic_offset != *frame_offset) {
  8814. /* cell num */
  8815. cells[j] = cell;
  8816. /* src offset */
  8817. src_offsets[j] = *frame_offset;
  8818. /* dst offset */
  8819. dst_offsets[j] = dynamic_offset;
  8820. j++;
  8821. }
  8822. if (opcode == WASM_OP_ELSE) {
  8823. *frame_offset = dynamic_offset;
  8824. }
  8825. else {
  8826. loader_ctx->frame_offset = frame_offset;
  8827. loader_ctx->dynamic_offset = dynamic_offset;
  8828. PUSH_OFFSET_TYPE(return_types[i]);
  8829. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8830. loader_ctx->frame_offset = frame_offset_org;
  8831. loader_ctx->dynamic_offset = dynamic_offset_org;
  8832. }
  8833. }
  8834. bh_assert(j == value_count);
  8835. /* Emit the cells, src_offsets and dst_offsets */
  8836. for (j = 0; j < value_count; j++)
  8837. emit_byte(loader_ctx, cells[j]);
  8838. for (j = 0; j < value_count; j++)
  8839. emit_operand(loader_ctx, src_offsets[j]);
  8840. for (j = 0; j < value_count; j++)
  8841. emit_operand(loader_ctx, dst_offsets[j]);
  8842. if (opcode == WASM_OP_ELSE)
  8843. emit_label(opcode);
  8844. wasm_runtime_free(emit_data);
  8845. }
  8846. return true;
  8847. fail:
  8848. return false;
  8849. }
  8850. #endif /* WASM_ENABLE_FAST_INTERP */
  8851. #define PUSH_TYPE(type) \
  8852. do { \
  8853. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8854. error_buf_size))) \
  8855. goto fail; \
  8856. } while (0)
  8857. #define POP_TYPE(type) \
  8858. do { \
  8859. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8860. error_buf_size))) \
  8861. goto fail; \
  8862. } while (0)
  8863. #if WASM_ENABLE_GC == 0
  8864. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8865. do { \
  8866. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8867. _start_addr, error_buf, \
  8868. error_buf_size)) \
  8869. goto fail; \
  8870. } while (0)
  8871. #define POP_CSP() \
  8872. do { \
  8873. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8874. goto fail; \
  8875. } while (0)
  8876. #else
  8877. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8878. do { \
  8879. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8880. _start_addr, error_buf, \
  8881. error_buf_size)) \
  8882. goto fail; \
  8883. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8884. error_buf, error_buf_size)) { \
  8885. goto fail; \
  8886. } \
  8887. } while (0)
  8888. #define POP_CSP() \
  8889. do { \
  8890. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8891. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8892. goto fail; \
  8893. } while (0)
  8894. #endif /* end of WASM_ENABLE_GC == 0 */
  8895. #if WASM_ENABLE_GC == 0
  8896. #define GET_LOCAL_REFTYPE() (void)0
  8897. #else
  8898. #define GET_LOCAL_REFTYPE() \
  8899. do { \
  8900. if (wasm_is_type_multi_byte_type(local_type)) { \
  8901. WASMRefType *_ref_type; \
  8902. if (local_idx < param_count) \
  8903. _ref_type = wasm_reftype_map_find( \
  8904. param_reftype_maps, param_reftype_map_count, local_idx); \
  8905. else \
  8906. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8907. local_reftype_map_count, \
  8908. local_idx - param_count); \
  8909. bh_assert(_ref_type); \
  8910. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8911. wasm_reftype_struct_size(_ref_type)); \
  8912. } \
  8913. } while (0)
  8914. #endif /* end of WASM_ENABLE_GC == 0 */
  8915. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8916. do { \
  8917. read_leb_uint32(p, p_end, local_idx); \
  8918. if (local_idx >= param_count + local_count) { \
  8919. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8920. goto fail; \
  8921. } \
  8922. local_type = local_idx < param_count \
  8923. ? param_types[local_idx] \
  8924. : local_types[local_idx - param_count]; \
  8925. local_offset = local_offsets[local_idx]; \
  8926. GET_LOCAL_REFTYPE(); \
  8927. } while (0)
  8928. static bool
  8929. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8930. {
  8931. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8932. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8933. return false;
  8934. }
  8935. return true;
  8936. }
  8937. #define CHECK_MEMORY() \
  8938. do { \
  8939. if (!check_memory(module, error_buf, error_buf_size)) \
  8940. goto fail; \
  8941. } while (0)
  8942. static bool
  8943. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8944. uint32 error_buf_size)
  8945. {
  8946. uint8 mem_access_aligns[] = {
  8947. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8948. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8949. };
  8950. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8951. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8952. set_error_buf(error_buf, error_buf_size,
  8953. "alignment must not be larger than natural");
  8954. return false;
  8955. }
  8956. return true;
  8957. }
  8958. #if WASM_ENABLE_SIMD != 0
  8959. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8960. static bool
  8961. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8962. uint32 error_buf_size)
  8963. {
  8964. uint8 mem_access_aligns[] = {
  8965. 4, /* load */
  8966. 3, 3, 3, 3, 3, 3, /* load and extend */
  8967. 0, 1, 2, 3, /* load and splat */
  8968. 4, /* store */
  8969. };
  8970. uint8 mem_access_aligns_load_lane[] = {
  8971. 0, 1, 2, 3, /* load lane */
  8972. 0, 1, 2, 3, /* store lane */
  8973. 2, 3 /* store zero */
  8974. };
  8975. if (!((opcode <= SIMD_v128_store)
  8976. || (SIMD_v128_load8_lane <= opcode
  8977. && opcode <= SIMD_v128_load64_zero))) {
  8978. set_error_buf(error_buf, error_buf_size,
  8979. "the opcode doesn't include memarg");
  8980. return false;
  8981. }
  8982. if ((opcode <= SIMD_v128_store
  8983. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8984. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8985. && align > mem_access_aligns_load_lane[opcode
  8986. - SIMD_v128_load8_lane])) {
  8987. set_error_buf(error_buf, error_buf_size,
  8988. "alignment must not be larger than natural");
  8989. return false;
  8990. }
  8991. return true;
  8992. }
  8993. static bool
  8994. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8995. uint32 error_buf_size)
  8996. {
  8997. switch (opcode) {
  8998. case SIMD_i8x16_extract_lane_s:
  8999. case SIMD_i8x16_extract_lane_u:
  9000. case SIMD_i8x16_replace_lane:
  9001. if (lane >= 16) {
  9002. goto fail;
  9003. }
  9004. break;
  9005. case SIMD_i16x8_extract_lane_s:
  9006. case SIMD_i16x8_extract_lane_u:
  9007. case SIMD_i16x8_replace_lane:
  9008. if (lane >= 8) {
  9009. goto fail;
  9010. }
  9011. break;
  9012. case SIMD_i32x4_extract_lane:
  9013. case SIMD_i32x4_replace_lane:
  9014. case SIMD_f32x4_extract_lane:
  9015. case SIMD_f32x4_replace_lane:
  9016. if (lane >= 4) {
  9017. goto fail;
  9018. }
  9019. break;
  9020. case SIMD_i64x2_extract_lane:
  9021. case SIMD_i64x2_replace_lane:
  9022. case SIMD_f64x2_extract_lane:
  9023. case SIMD_f64x2_replace_lane:
  9024. if (lane >= 2) {
  9025. goto fail;
  9026. }
  9027. break;
  9028. case SIMD_v128_load8_lane:
  9029. case SIMD_v128_load16_lane:
  9030. case SIMD_v128_load32_lane:
  9031. case SIMD_v128_load64_lane:
  9032. case SIMD_v128_store8_lane:
  9033. case SIMD_v128_store16_lane:
  9034. case SIMD_v128_store32_lane:
  9035. case SIMD_v128_store64_lane:
  9036. case SIMD_v128_load32_zero:
  9037. case SIMD_v128_load64_zero:
  9038. {
  9039. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9040. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9041. goto fail;
  9042. }
  9043. break;
  9044. }
  9045. default:
  9046. goto fail;
  9047. }
  9048. return true;
  9049. fail:
  9050. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9051. return false;
  9052. }
  9053. static bool
  9054. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9055. {
  9056. uint8 i;
  9057. for (i = 0; i != 16; ++i) {
  9058. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9059. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9060. return false;
  9061. }
  9062. }
  9063. return true;
  9064. }
  9065. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9066. #endif /* end of WASM_ENABLE_SIMD */
  9067. #if WASM_ENABLE_SHARED_MEMORY != 0
  9068. static bool
  9069. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9070. uint32 error_buf_size)
  9071. {
  9072. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9073. uint8 mem_access_aligns[] = {
  9074. 2, 3, 0, 1, 0, 1, 2,
  9075. };
  9076. uint8 expect;
  9077. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9078. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9079. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9080. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9081. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9082. }
  9083. else {
  9084. /* 7 opcodes in every group */
  9085. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9086. }
  9087. if (align != expect) {
  9088. set_error_buf(error_buf, error_buf_size,
  9089. "alignment isn't equal to natural");
  9090. return false;
  9091. }
  9092. return true;
  9093. }
  9094. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9095. static bool
  9096. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9097. char *error_buf, uint32 error_buf_size)
  9098. {
  9099. BranchBlock *target_block, *cur_block;
  9100. BlockType *target_block_type;
  9101. uint8 type, *types = NULL, *frame_ref;
  9102. uint32 arity = 0;
  9103. int32 i, available_stack_cell;
  9104. uint16 cell_num;
  9105. #if WASM_ENABLE_GC != 0
  9106. WASMRefTypeMap *frame_reftype_map;
  9107. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9108. WASMRefType *ref_type;
  9109. uint32 reftype_map_count = 0;
  9110. int32 available_reftype_map;
  9111. bool is_type_multi_byte;
  9112. #endif
  9113. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9114. uint8 *frame_ref_after_popped = NULL;
  9115. uint8 frame_ref_tmp[4] = { 0 };
  9116. uint8 *frame_ref_buf = frame_ref_tmp;
  9117. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9118. #if WASM_ENABLE_GC != 0
  9119. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9120. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9121. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9122. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9123. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9124. #endif
  9125. #if WASM_ENABLE_FAST_INTERP != 0
  9126. int16 *frame_offset_old = loader_ctx->frame_offset;
  9127. int16 *frame_offset_after_popped = NULL;
  9128. int16 frame_offset_tmp[4] = { 0 };
  9129. int16 *frame_offset_buf = frame_offset_tmp;
  9130. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9131. #endif
  9132. bool ret = false;
  9133. bh_assert(loader_ctx->csp_num > 0);
  9134. if (loader_ctx->csp_num - 1 < depth) {
  9135. set_error_buf(error_buf, error_buf_size,
  9136. "unknown label, "
  9137. "unexpected end of section or function");
  9138. return false;
  9139. }
  9140. cur_block = loader_ctx->frame_csp - 1;
  9141. target_block = loader_ctx->frame_csp - (depth + 1);
  9142. target_block_type = &target_block->block_type;
  9143. frame_ref = loader_ctx->frame_ref;
  9144. #if WASM_ENABLE_GC != 0
  9145. frame_reftype_map = loader_ctx->frame_reftype_map;
  9146. #endif
  9147. /* Note: loop's arity is different from if and block. loop's arity is
  9148. * its parameter count while if and block arity is result count.
  9149. */
  9150. #if WASM_ENABLE_GC == 0
  9151. if (target_block->label_type == LABEL_TYPE_LOOP)
  9152. arity = block_type_get_param_types(target_block_type, &types);
  9153. else
  9154. arity = block_type_get_result_types(target_block_type, &types);
  9155. #else
  9156. if (target_block->label_type == LABEL_TYPE_LOOP)
  9157. arity = block_type_get_param_types(target_block_type, &types,
  9158. &reftype_maps, &reftype_map_count);
  9159. else
  9160. arity = block_type_get_result_types(target_block_type, &types,
  9161. &reftype_maps, &reftype_map_count);
  9162. #endif
  9163. /* If the stack is in polymorphic state, just clear the stack
  9164. * and then re-push the values to make the stack top values
  9165. * match block type. */
  9166. if (cur_block->is_stack_polymorphic) {
  9167. #if WASM_ENABLE_GC != 0
  9168. int32 j = reftype_map_count - 1;
  9169. #endif
  9170. for (i = (int32)arity - 1; i >= 0; i--) {
  9171. #if WASM_ENABLE_GC != 0
  9172. if (wasm_is_type_multi_byte_type(types[i])) {
  9173. bh_assert(reftype_maps[j].index == i);
  9174. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9175. reftype_maps[j].ref_type,
  9176. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9177. j--;
  9178. }
  9179. #endif
  9180. #if WASM_ENABLE_FAST_INTERP != 0
  9181. POP_OFFSET_TYPE(types[i]);
  9182. #endif
  9183. POP_TYPE(types[i]);
  9184. }
  9185. /* Backup stack data since it may be changed in the below
  9186. push operations, and the stack data may be used when
  9187. checking other target blocks of opcode br_table */
  9188. if (opcode == WASM_OP_BR_TABLE) {
  9189. uint64 total_size;
  9190. frame_ref_after_popped = loader_ctx->frame_ref;
  9191. total_size = (uint64)sizeof(uint8)
  9192. * (frame_ref_old - frame_ref_after_popped);
  9193. if (total_size > sizeof(frame_ref_tmp)
  9194. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9195. error_buf_size))) {
  9196. goto fail;
  9197. }
  9198. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9199. frame_ref_after_popped, (uint32)total_size);
  9200. #if WASM_ENABLE_GC != 0
  9201. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9202. total_size =
  9203. (uint64)sizeof(WASMRefTypeMap)
  9204. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9205. if (total_size > sizeof(frame_reftype_map_tmp)
  9206. && !(frame_reftype_map_buf = loader_malloc(
  9207. total_size, error_buf, error_buf_size))) {
  9208. goto fail;
  9209. }
  9210. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9211. frame_reftype_map_after_popped, (uint32)total_size);
  9212. #endif
  9213. #if WASM_ENABLE_FAST_INTERP != 0
  9214. frame_offset_after_popped = loader_ctx->frame_offset;
  9215. total_size = (uint64)sizeof(int16)
  9216. * (frame_offset_old - frame_offset_after_popped);
  9217. if (total_size > sizeof(frame_offset_tmp)
  9218. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9219. error_buf_size))) {
  9220. goto fail;
  9221. }
  9222. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9223. frame_offset_after_popped, (uint32)total_size);
  9224. #endif
  9225. }
  9226. #if WASM_ENABLE_GC != 0
  9227. j = 0;
  9228. #endif
  9229. for (i = 0; i < (int32)arity; i++) {
  9230. #if WASM_ENABLE_GC != 0
  9231. if (wasm_is_type_multi_byte_type(types[i])) {
  9232. bh_assert(reftype_maps[j].index == i);
  9233. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9234. reftype_maps[j].ref_type,
  9235. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9236. j++;
  9237. }
  9238. #endif
  9239. #if WASM_ENABLE_FAST_INTERP != 0
  9240. bool disable_emit = true;
  9241. int16 operand_offset = 0;
  9242. PUSH_OFFSET_TYPE(types[i]);
  9243. #endif
  9244. PUSH_TYPE(types[i]);
  9245. }
  9246. #if WASM_ENABLE_FAST_INTERP != 0
  9247. emit_br_info(target_block, opcode == WASM_OP_BR);
  9248. #endif
  9249. /* Restore the stack data, note that frame_ref_bottom,
  9250. frame_reftype_map_bottom, frame_offset_bottom may be
  9251. re-allocated in the above push operations */
  9252. if (opcode == WASM_OP_BR_TABLE) {
  9253. uint32 total_size;
  9254. /* The stack operand num should not be smaller than before
  9255. after pop and push operations */
  9256. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9257. loader_ctx->stack_cell_num = stack_cell_num_old;
  9258. loader_ctx->frame_ref =
  9259. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9260. total_size = (uint32)(sizeof(uint8)
  9261. * (frame_ref_old - frame_ref_after_popped));
  9262. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9263. frame_ref_buf, total_size);
  9264. #if WASM_ENABLE_GC != 0
  9265. /* The stack operand num should not be smaller than before
  9266. after pop and push operations */
  9267. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9268. loader_ctx->reftype_map_num = reftype_map_num_old;
  9269. loader_ctx->frame_reftype_map =
  9270. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9271. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9272. * (frame_reftype_map_old
  9273. - frame_reftype_map_after_popped));
  9274. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9275. total_size, frame_reftype_map_buf, total_size);
  9276. #endif
  9277. #if WASM_ENABLE_FAST_INTERP != 0
  9278. loader_ctx->frame_offset =
  9279. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9280. total_size =
  9281. (uint32)(sizeof(int16)
  9282. * (frame_offset_old - frame_offset_after_popped));
  9283. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9284. total_size, frame_offset_buf, total_size);
  9285. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9286. #endif
  9287. }
  9288. ret = true;
  9289. goto cleanup_and_return;
  9290. }
  9291. available_stack_cell =
  9292. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9293. #if WASM_ENABLE_GC != 0
  9294. available_reftype_map =
  9295. (int32)(loader_ctx->reftype_map_num
  9296. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9297. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9298. #endif
  9299. /* Check stack top values match target block type */
  9300. for (i = (int32)arity - 1; i >= 0; i--) {
  9301. type = types[i];
  9302. #if WASM_ENABLE_GC != 0
  9303. ref_type = NULL;
  9304. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9305. if (is_type_multi_byte) {
  9306. bh_assert(reftype_map);
  9307. ref_type = reftype_map->ref_type;
  9308. }
  9309. #endif
  9310. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9311. break;
  9312. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9313. #if WASM_ENABLE_GC != 0
  9314. frame_reftype_map, available_reftype_map,
  9315. #endif
  9316. type,
  9317. #if WASM_ENABLE_GC != 0
  9318. ref_type,
  9319. #endif
  9320. error_buf, error_buf_size)) {
  9321. goto fail;
  9322. }
  9323. cell_num = wasm_value_type_cell_num(types[i]);
  9324. frame_ref -= cell_num;
  9325. available_stack_cell -= cell_num;
  9326. #if WASM_ENABLE_GC != 0
  9327. if (is_type_multi_byte) {
  9328. frame_reftype_map--;
  9329. available_reftype_map--;
  9330. reftype_map--;
  9331. }
  9332. #endif
  9333. }
  9334. #if WASM_ENABLE_FAST_INTERP != 0
  9335. emit_br_info(target_block, opcode == WASM_OP_BR);
  9336. #endif
  9337. ret = true;
  9338. cleanup_and_return:
  9339. fail:
  9340. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9341. wasm_runtime_free(frame_ref_buf);
  9342. #if WASM_ENABLE_GC != 0
  9343. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9344. wasm_runtime_free(frame_reftype_map_buf);
  9345. #endif
  9346. #if WASM_ENABLE_FAST_INTERP != 0
  9347. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9348. wasm_runtime_free(frame_offset_buf);
  9349. #endif
  9350. return ret;
  9351. }
  9352. static BranchBlock *
  9353. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9354. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9355. {
  9356. uint8 *p = *p_buf, *p_end = buf_end;
  9357. BranchBlock *frame_csp_tmp;
  9358. uint32 depth;
  9359. read_leb_uint32(p, p_end, depth);
  9360. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9361. error_buf_size)) {
  9362. goto fail;
  9363. }
  9364. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9365. *p_buf = p;
  9366. return frame_csp_tmp;
  9367. fail:
  9368. return NULL;
  9369. }
  9370. #if WASM_ENABLE_EXCE_HANDLING != 0
  9371. static BranchBlock *
  9372. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9373. uint8 *buf_end, char *error_buf,
  9374. uint32 error_buf_size)
  9375. {
  9376. uint8 *p = *p_buf, *p_end = buf_end;
  9377. BranchBlock *frame_csp_tmp;
  9378. uint32 depth;
  9379. read_leb_uint32(p, p_end, depth);
  9380. /*
  9381. * Note: "delegate 0" means the surrounding block, not the
  9382. * try-delegate block itself.
  9383. *
  9384. * Note: the caller hasn't popped the try-delegate frame yet.
  9385. */
  9386. bh_assert(loader_ctx->csp_num > 0);
  9387. if (loader_ctx->csp_num - 1 <= depth) {
  9388. #if WASM_ENABLE_SPEC_TEST == 0
  9389. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9390. #else
  9391. set_error_buf(error_buf, error_buf_size, "unknown label");
  9392. #endif
  9393. goto fail;
  9394. }
  9395. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9396. #if WASM_ENABLE_FAST_INTERP != 0
  9397. emit_br_info(frame_csp_tmp, false);
  9398. #endif
  9399. *p_buf = p;
  9400. return frame_csp_tmp;
  9401. fail:
  9402. return NULL;
  9403. }
  9404. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9405. static bool
  9406. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9407. char *error_buf, uint32 error_buf_size)
  9408. {
  9409. BlockType *block_type = &block->block_type;
  9410. uint8 *return_types = NULL;
  9411. uint32 return_count = 0;
  9412. int32 available_stack_cell, return_cell_num, i;
  9413. uint8 *frame_ref = NULL;
  9414. #if WASM_ENABLE_GC != 0
  9415. WASMRefTypeMap *frame_reftype_map;
  9416. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9417. WASMRefType *ref_type;
  9418. uint32 param_count, return_reftype_map_count = 0;
  9419. int32 available_reftype_map =
  9420. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9421. #endif
  9422. available_stack_cell =
  9423. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9424. #if WASM_ENABLE_GC == 0
  9425. return_count = block_type_get_result_types(block_type, &return_types);
  9426. #else
  9427. return_count = block_type_get_result_types(block_type, &return_types,
  9428. &return_reftype_maps,
  9429. &return_reftype_map_count);
  9430. param_count =
  9431. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9432. (void)param_count;
  9433. #endif
  9434. return_cell_num =
  9435. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9436. /* If the stack is in polymorphic state, just clear the stack
  9437. * and then re-push the values to make the stack top values
  9438. * match block type. */
  9439. if (block->is_stack_polymorphic) {
  9440. #if WASM_ENABLE_GC != 0
  9441. int32 j = return_reftype_map_count - 1;
  9442. #endif
  9443. for (i = (int32)return_count - 1; i >= 0; i--) {
  9444. #if WASM_ENABLE_GC != 0
  9445. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9446. bh_assert(return_reftype_maps[j].index == i + param_count);
  9447. bh_memcpy_s(
  9448. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9449. return_reftype_maps[j].ref_type,
  9450. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9451. j--;
  9452. }
  9453. #endif
  9454. #if WASM_ENABLE_FAST_INTERP != 0
  9455. POP_OFFSET_TYPE(return_types[i]);
  9456. #endif
  9457. POP_TYPE(return_types[i]);
  9458. }
  9459. /* Check stack is empty */
  9460. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9461. set_error_buf(
  9462. error_buf, error_buf_size,
  9463. "type mismatch: stack size does not match block type");
  9464. goto fail;
  9465. }
  9466. #if WASM_ENABLE_GC != 0
  9467. j = 0;
  9468. #endif
  9469. for (i = 0; i < (int32)return_count; i++) {
  9470. #if WASM_ENABLE_GC != 0
  9471. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9472. bh_assert(return_reftype_maps[j].index == i + param_count);
  9473. bh_memcpy_s(
  9474. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9475. return_reftype_maps[j].ref_type,
  9476. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9477. j++;
  9478. }
  9479. #endif
  9480. #if WASM_ENABLE_FAST_INTERP != 0
  9481. bool disable_emit = true;
  9482. int16 operand_offset = 0;
  9483. PUSH_OFFSET_TYPE(return_types[i]);
  9484. #endif
  9485. PUSH_TYPE(return_types[i]);
  9486. }
  9487. return true;
  9488. }
  9489. if (available_stack_cell != return_cell_num) {
  9490. #if WASM_ENABLE_EXCE_HANDLING != 0
  9491. /* testspec: this error message format is expected by try_catch.wast */
  9492. snprintf(
  9493. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9494. block->label_type == LABEL_TYPE_TRY
  9495. || (block->label_type == LABEL_TYPE_CATCH
  9496. && return_cell_num > 0)
  9497. ? "instruction"
  9498. : "block",
  9499. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9500. " but stack has ",
  9501. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9502. : "");
  9503. goto fail;
  9504. #else
  9505. set_error_buf(error_buf, error_buf_size,
  9506. "type mismatch: stack size does not match block type");
  9507. goto fail;
  9508. #endif
  9509. }
  9510. /* Check stack values match return types */
  9511. frame_ref = loader_ctx->frame_ref;
  9512. #if WASM_ENABLE_GC != 0
  9513. frame_reftype_map = loader_ctx->frame_reftype_map;
  9514. return_reftype_map =
  9515. return_reftype_map_count
  9516. ? return_reftype_maps + return_reftype_map_count - 1
  9517. : NULL;
  9518. #endif
  9519. for (i = (int32)return_count - 1; i >= 0; i--) {
  9520. uint8 type = return_types[i];
  9521. #if WASM_ENABLE_GC != 0
  9522. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9523. ref_type = NULL;
  9524. if (is_type_multi_byte) {
  9525. bh_assert(return_reftype_map);
  9526. ref_type = return_reftype_map->ref_type;
  9527. }
  9528. #endif
  9529. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9530. #if WASM_ENABLE_GC != 0
  9531. frame_reftype_map, available_reftype_map,
  9532. #endif
  9533. type,
  9534. #if WASM_ENABLE_GC != 0
  9535. ref_type,
  9536. #endif
  9537. error_buf, error_buf_size))
  9538. return false;
  9539. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9540. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9541. #if WASM_ENABLE_GC != 0
  9542. if (is_type_multi_byte) {
  9543. frame_reftype_map--;
  9544. available_reftype_map--;
  9545. return_reftype_map--;
  9546. }
  9547. #endif
  9548. }
  9549. return true;
  9550. fail:
  9551. return false;
  9552. }
  9553. #if WASM_ENABLE_FAST_INTERP != 0
  9554. /* Copy parameters to dynamic space.
  9555. * 1) POP original parameter out;
  9556. * 2) Push and copy original values to dynamic space.
  9557. * The copy instruction format:
  9558. * Part a: param count
  9559. * Part b: all param total cell num
  9560. * Part c: each param's cell_num, src offset and dst offset
  9561. * Part d: each param's src offset
  9562. * Part e: each param's dst offset
  9563. */
  9564. static bool
  9565. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9566. uint32 error_buf_size)
  9567. {
  9568. bool ret = false;
  9569. int16 *frame_offset = NULL;
  9570. uint8 *cells = NULL, cell;
  9571. int16 *src_offsets = NULL;
  9572. uint8 *emit_data = NULL;
  9573. uint32 i;
  9574. BranchBlock *block = loader_ctx->frame_csp - 1;
  9575. BlockType *block_type = &block->block_type;
  9576. WASMFuncType *wasm_type = block_type->u.type;
  9577. uint32 param_count = block_type->u.type->param_count;
  9578. int16 condition_offset = 0;
  9579. bool disable_emit = false;
  9580. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9581. int16 operand_offset = 0;
  9582. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9583. bh_assert(size > 0);
  9584. /* For if block, we also need copy the condition operand offset. */
  9585. if (is_if_block)
  9586. size += sizeof(*cells) + sizeof(*src_offsets);
  9587. /* Allocate memory for the emit data */
  9588. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9589. return false;
  9590. cells = emit_data;
  9591. src_offsets = (int16 *)(cells + param_count);
  9592. if (is_if_block)
  9593. condition_offset = *loader_ctx->frame_offset;
  9594. /* POP original parameter out */
  9595. for (i = 0; i < param_count; i++) {
  9596. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9597. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9598. }
  9599. frame_offset = loader_ctx->frame_offset;
  9600. /* Get each param's cell num and src offset */
  9601. for (i = 0; i < param_count; i++) {
  9602. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9603. cells[i] = cell;
  9604. src_offsets[i] = *frame_offset;
  9605. frame_offset += cell;
  9606. }
  9607. /* emit copy instruction */
  9608. emit_label(EXT_OP_COPY_STACK_VALUES);
  9609. /* Part a) */
  9610. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9611. /* Part b) */
  9612. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9613. : wasm_type->param_cell_num);
  9614. /* Part c) */
  9615. for (i = 0; i < param_count; i++)
  9616. emit_byte(loader_ctx, cells[i]);
  9617. if (is_if_block)
  9618. emit_byte(loader_ctx, 1);
  9619. /* Part d) */
  9620. for (i = 0; i < param_count; i++)
  9621. emit_operand(loader_ctx, src_offsets[i]);
  9622. if (is_if_block)
  9623. emit_operand(loader_ctx, condition_offset);
  9624. /* Since the start offset to save the block's params and
  9625. * the start offset to save the block's results may be
  9626. * different, we remember the dynamic offset for loop block
  9627. * so that we can use it to copy the stack operands to the
  9628. * loop block's params in wasm_loader_emit_br_info. */
  9629. if (block->label_type == LABEL_TYPE_LOOP)
  9630. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9631. /* Part e) */
  9632. /* Push to dynamic space. The push will emit the dst offset. */
  9633. for (i = 0; i < param_count; i++)
  9634. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9635. if (is_if_block)
  9636. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9637. ret = true;
  9638. fail:
  9639. /* Free the emit data */
  9640. wasm_runtime_free(emit_data);
  9641. return ret;
  9642. }
  9643. #endif
  9644. #if WASM_ENABLE_GC == 0
  9645. #define RESET_REFTYPE_MAP_STACK() (void)0
  9646. #else
  9647. #define RESET_REFTYPE_MAP_STACK() \
  9648. do { \
  9649. loader_ctx->reftype_map_num = \
  9650. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9651. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9652. + loader_ctx->reftype_map_num; \
  9653. } while (0)
  9654. #endif
  9655. /* reset the stack to the state of before entering the last block */
  9656. #if WASM_ENABLE_FAST_INTERP != 0
  9657. #define RESET_STACK() \
  9658. do { \
  9659. loader_ctx->stack_cell_num = \
  9660. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9661. loader_ctx->frame_ref = \
  9662. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9663. loader_ctx->frame_offset = \
  9664. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9665. RESET_REFTYPE_MAP_STACK(); \
  9666. } while (0)
  9667. #else
  9668. #define RESET_STACK() \
  9669. do { \
  9670. loader_ctx->stack_cell_num = \
  9671. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9672. loader_ctx->frame_ref = \
  9673. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9674. RESET_REFTYPE_MAP_STACK(); \
  9675. } while (0)
  9676. #endif
  9677. /* set current block's stack polymorphic state */
  9678. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9679. do { \
  9680. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9681. _cur_block->is_stack_polymorphic = flag; \
  9682. } while (0)
  9683. #define BLOCK_HAS_PARAM(block_type) \
  9684. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9685. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9686. do { \
  9687. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9688. error_buf_size))) { \
  9689. goto fail; \
  9690. } \
  9691. } while (0)
  9692. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9693. static bool
  9694. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9695. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9696. uint32 error_buf_size)
  9697. {
  9698. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9699. return false;
  9700. }
  9701. if (table_idx < module->import_table_count) {
  9702. if (p_elem_type)
  9703. *p_elem_type =
  9704. module->import_tables[table_idx].u.table.table_type.elem_type;
  9705. #if WASM_ENABLE_GC != 0
  9706. if (p_ref_type)
  9707. *((WASMRefType **)p_ref_type) =
  9708. module->import_tables[table_idx]
  9709. .u.table.table_type.elem_ref_type;
  9710. #endif
  9711. }
  9712. else {
  9713. if (p_elem_type)
  9714. *p_elem_type =
  9715. module->tables[module->import_table_count + table_idx]
  9716. .table_type.elem_type;
  9717. #if WASM_ENABLE_GC != 0
  9718. if (p_ref_type)
  9719. *((WASMRefType **)p_ref_type) =
  9720. module->tables[module->import_table_count + table_idx]
  9721. .table_type.elem_ref_type;
  9722. #endif
  9723. }
  9724. return true;
  9725. }
  9726. static bool
  9727. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9728. uint8 *p_elem_type, void **p_elem_ref_type,
  9729. char *error_buf, uint32 error_buf_size)
  9730. {
  9731. if (table_seg_idx >= module->table_seg_count) {
  9732. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9733. table_seg_idx);
  9734. return false;
  9735. }
  9736. if (p_elem_type) {
  9737. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9738. }
  9739. #if WASM_ENABLE_GC != 0
  9740. if (p_elem_ref_type)
  9741. *((WASMRefType **)p_elem_ref_type) =
  9742. module->table_segments[table_seg_idx].elem_ref_type;
  9743. #endif
  9744. return true;
  9745. }
  9746. #endif
  9747. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9748. const uint8 *
  9749. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9750. uint32 *len)
  9751. {
  9752. WASMCustomSection *section = module->custom_section_list;
  9753. while (section) {
  9754. if ((section->name_len == strlen(name))
  9755. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9756. if (len) {
  9757. *len = section->content_len;
  9758. }
  9759. return section->content_addr;
  9760. }
  9761. section = section->next;
  9762. }
  9763. return NULL;
  9764. }
  9765. #endif
  9766. #if 0
  9767. #define HANDLE_OPCODE(opcode) #opcode
  9768. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9769. #undef HANDLE_OPCODE
  9770. #endif
  9771. static bool
  9772. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9773. uint32 cur_func_idx, char *error_buf,
  9774. uint32 error_buf_size)
  9775. {
  9776. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9777. uint32 param_count, local_count, global_count;
  9778. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  9779. table_elem_idx_type;
  9780. BlockType func_block_type;
  9781. uint16 *local_offsets, local_offset;
  9782. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9783. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9784. mem_offset_t mem_offset;
  9785. int32 i32_const = 0;
  9786. int64 i64_const;
  9787. uint8 opcode;
  9788. bool return_value = false;
  9789. WASMLoaderContext *loader_ctx;
  9790. BranchBlock *frame_csp_tmp;
  9791. #if WASM_ENABLE_GC != 0
  9792. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9793. uint32 param_reftype_map_count, local_reftype_map_count;
  9794. int32 heap_type;
  9795. WASMRefType wasm_ref_type = { 0 };
  9796. bool need_ref_type_map;
  9797. #endif
  9798. #if WASM_ENABLE_FAST_INTERP != 0
  9799. uint8 *func_const_end, *func_const = NULL;
  9800. int16 operand_offset = 0;
  9801. uint8 last_op = 0;
  9802. bool disable_emit, preserve_local = false, if_condition_available = true;
  9803. float32 f32_const;
  9804. float64 f64_const;
  9805. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9806. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9807. #endif
  9808. #if WASM_ENABLE_MEMORY64 != 0
  9809. bool is_memory64 = has_module_memory64(module);
  9810. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9811. #else
  9812. mem_offset_type = VALUE_TYPE_I32;
  9813. table_elem_idx_type = VALUE_TYPE_I32;
  9814. #endif
  9815. uint32 memidx;
  9816. global_count = module->import_global_count + module->global_count;
  9817. param_count = func->func_type->param_count;
  9818. param_types = func->func_type->types;
  9819. func_block_type.is_value_type = false;
  9820. func_block_type.u.type = func->func_type;
  9821. local_count = func->local_count;
  9822. local_types = func->local_types;
  9823. local_offsets = func->local_offsets;
  9824. #if WASM_ENABLE_GC != 0
  9825. param_reftype_maps = func->func_type->ref_type_maps;
  9826. param_reftype_map_count = func->func_type->ref_type_map_count;
  9827. local_reftype_maps = func->local_ref_type_maps;
  9828. local_reftype_map_count = func->local_ref_type_map_count;
  9829. #endif
  9830. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9831. goto fail;
  9832. }
  9833. #if WASM_ENABLE_GC != 0
  9834. loader_ctx->module = module;
  9835. loader_ctx->ref_type_set = module->ref_type_set;
  9836. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9837. #endif
  9838. #if WASM_ENABLE_FAST_INTERP != 0
  9839. /* For the first traverse, the initial value of preserved_local_offset has
  9840. * not been determined, we use the INT16_MAX to represent that a slot has
  9841. * been copied to preserve space. For second traverse, this field will be
  9842. * set to the appropriate value in wasm_loader_ctx_reinit.
  9843. * This is for Issue #1230,
  9844. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9845. * drop opcodes need to know which slots are preserved, so those slots will
  9846. * not be treated as dynamically allocated slots */
  9847. loader_ctx->preserved_local_offset = INT16_MAX;
  9848. re_scan:
  9849. if (loader_ctx->code_compiled_size > 0) {
  9850. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9851. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9852. goto fail;
  9853. }
  9854. p = func->code;
  9855. func->code_compiled = loader_ctx->p_code_compiled;
  9856. func->code_compiled_size = loader_ctx->code_compiled_size;
  9857. }
  9858. #endif
  9859. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9860. while (p < p_end) {
  9861. opcode = *p++;
  9862. #if WASM_ENABLE_FAST_INTERP != 0
  9863. p_org = p;
  9864. disable_emit = false;
  9865. emit_label(opcode);
  9866. #endif
  9867. switch (opcode) {
  9868. case WASM_OP_UNREACHABLE:
  9869. RESET_STACK();
  9870. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9871. break;
  9872. case WASM_OP_NOP:
  9873. #if WASM_ENABLE_FAST_INTERP != 0
  9874. skip_label();
  9875. #endif
  9876. break;
  9877. case WASM_OP_IF:
  9878. {
  9879. #if WASM_ENABLE_FAST_INTERP != 0
  9880. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9881. int32 available_stack_cell =
  9882. (int32)(loader_ctx->stack_cell_num
  9883. - parent_block->stack_cell_num);
  9884. if (available_stack_cell <= 0
  9885. && parent_block->is_stack_polymorphic)
  9886. if_condition_available = false;
  9887. else
  9888. if_condition_available = true;
  9889. PRESERVE_LOCAL_FOR_BLOCK();
  9890. #endif
  9891. #if WASM_ENABLE_GC == 0
  9892. POP_I32();
  9893. #endif
  9894. goto handle_op_block_and_loop;
  9895. }
  9896. case WASM_OP_BLOCK:
  9897. case WASM_OP_LOOP:
  9898. #if WASM_ENABLE_EXCE_HANDLING != 0
  9899. case WASM_OP_TRY:
  9900. if (opcode == WASM_OP_TRY) {
  9901. /*
  9902. * keep track of exception handlers to account for
  9903. * memory allocation
  9904. */
  9905. func->exception_handler_count++;
  9906. /*
  9907. * try is a block
  9908. * do nothing special, but execution continues to
  9909. * to handle_op_block_and_loop,
  9910. * and that be pushes the csp
  9911. */
  9912. }
  9913. #endif
  9914. #if WASM_ENABLE_FAST_INTERP != 0
  9915. PRESERVE_LOCAL_FOR_BLOCK();
  9916. #endif
  9917. handle_op_block_and_loop:
  9918. {
  9919. uint8 value_type;
  9920. BlockType block_type;
  9921. #if WASM_ENABLE_FAST_INTERP != 0
  9922. uint32 available_params = 0;
  9923. #endif
  9924. CHECK_BUF(p, p_end, 1);
  9925. value_type = read_uint8(p);
  9926. if (is_byte_a_type(value_type)) {
  9927. /* If the first byte is one of these special values:
  9928. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9929. * the single return value. */
  9930. block_type.is_value_type = true;
  9931. block_type.u.value_type.type = value_type;
  9932. #if WASM_ENABLE_WAMR_COMPILER != 0
  9933. if (value_type == VALUE_TYPE_V128)
  9934. module->is_simd_used = true;
  9935. else if (value_type == VALUE_TYPE_FUNCREF
  9936. || value_type == VALUE_TYPE_EXTERNREF)
  9937. module->is_ref_types_used = true;
  9938. #endif
  9939. #if WASM_ENABLE_GC != 0
  9940. if (value_type != VALUE_TYPE_VOID) {
  9941. p_org = p;
  9942. p--;
  9943. if (!resolve_value_type((const uint8 **)&p, p_end,
  9944. module, module->type_count,
  9945. &need_ref_type_map,
  9946. &wasm_ref_type, false,
  9947. error_buf, error_buf_size)) {
  9948. goto fail;
  9949. }
  9950. if (need_ref_type_map) {
  9951. block_type.u.value_type.ref_type_map.index = 0;
  9952. if (!(block_type.u.value_type.ref_type_map
  9953. .ref_type = reftype_set_insert(
  9954. module->ref_type_set, &wasm_ref_type,
  9955. error_buf, error_buf_size))) {
  9956. goto fail;
  9957. }
  9958. }
  9959. /* Set again as the type might be changed, e.g.
  9960. (ref null any) to anyref */
  9961. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9962. #if WASM_ENABLE_FAST_INTERP == 0
  9963. while (p_org < p) {
  9964. #if WASM_ENABLE_DEBUG_INTERP != 0
  9965. if (!record_fast_op(module, p_org, *p_org,
  9966. error_buf, error_buf_size)) {
  9967. goto fail;
  9968. }
  9969. #endif
  9970. /* Ignore extra bytes for interpreter */
  9971. *p_org++ = WASM_OP_NOP;
  9972. }
  9973. #endif
  9974. }
  9975. #endif /* end of WASM_ENABLE_GC != 0 */
  9976. }
  9977. else {
  9978. int32 type_index;
  9979. /* Resolve the leb128 encoded type index as block type */
  9980. p--;
  9981. p_org = p - 1;
  9982. read_leb_int32(p, p_end, type_index);
  9983. if ((uint32)type_index >= module->type_count) {
  9984. set_error_buf(error_buf, error_buf_size,
  9985. "unknown type");
  9986. goto fail;
  9987. }
  9988. block_type.is_value_type = false;
  9989. block_type.u.type =
  9990. (WASMFuncType *)module->types[type_index];
  9991. #if WASM_ENABLE_FAST_INTERP == 0
  9992. /* If block use type index as block type, change the opcode
  9993. * to new extended opcode so that interpreter can resolve
  9994. * the block quickly.
  9995. */
  9996. #if WASM_ENABLE_DEBUG_INTERP != 0
  9997. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9998. error_buf_size)) {
  9999. goto fail;
  10000. }
  10001. #endif
  10002. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10003. #endif
  10004. }
  10005. #if WASM_ENABLE_GC != 0
  10006. if (opcode == WASM_OP_IF) {
  10007. POP_I32();
  10008. }
  10009. #endif
  10010. /* Pop block parameters from stack */
  10011. if (BLOCK_HAS_PARAM(block_type)) {
  10012. WASMFuncType *wasm_type = block_type.u.type;
  10013. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10014. #if WASM_ENABLE_FAST_INTERP != 0
  10015. uint32 cell_num;
  10016. available_params = block_type.u.type->param_count;
  10017. #endif
  10018. for (i = 0; i < block_type.u.type->param_count; i++) {
  10019. int32 available_stack_cell =
  10020. (int32)(loader_ctx->stack_cell_num
  10021. - cur_block->stack_cell_num);
  10022. if (available_stack_cell <= 0
  10023. && cur_block->is_stack_polymorphic) {
  10024. #if WASM_ENABLE_FAST_INTERP != 0
  10025. available_params = i;
  10026. #endif
  10027. break;
  10028. }
  10029. POP_TYPE(
  10030. wasm_type->types[wasm_type->param_count - i - 1]);
  10031. #if WASM_ENABLE_FAST_INTERP != 0
  10032. /* decrease the frame_offset pointer accordingly to keep
  10033. * consistent with frame_ref stack */
  10034. cell_num = wasm_value_type_cell_num(
  10035. wasm_type->types[wasm_type->param_count - i - 1]);
  10036. loader_ctx->frame_offset -= cell_num;
  10037. #endif
  10038. }
  10039. }
  10040. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10041. block_type, p);
  10042. /* Pass parameters to block */
  10043. if (BLOCK_HAS_PARAM(block_type)) {
  10044. WASMFuncType *func_type = block_type.u.type;
  10045. #if WASM_ENABLE_GC != 0
  10046. WASMRefType *ref_type;
  10047. uint32 j = 0;
  10048. #endif
  10049. for (i = 0; i < func_type->param_count; i++) {
  10050. #if WASM_ENABLE_FAST_INTERP != 0
  10051. uint32 cell_num =
  10052. wasm_value_type_cell_num(func_type->types[i]);
  10053. if (i >= available_params) {
  10054. /* If there isn't enough data on stack, push a dummy
  10055. * offset to keep the stack consistent with
  10056. * frame_ref.
  10057. * Since the stack is already in polymorphic state,
  10058. * the opcode will not be executed, so the dummy
  10059. * offset won't cause any error */
  10060. uint32 n;
  10061. for (n = 0; n < cell_num; n++) {
  10062. if (loader_ctx->p_code_compiled == NULL) {
  10063. if (!check_offset_push(loader_ctx,
  10064. error_buf,
  10065. error_buf_size))
  10066. goto fail;
  10067. }
  10068. *loader_ctx->frame_offset++ = 0;
  10069. }
  10070. }
  10071. else {
  10072. loader_ctx->frame_offset += cell_num;
  10073. }
  10074. #endif
  10075. #if WASM_ENABLE_GC != 0
  10076. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10077. bh_assert(func_type->ref_type_maps[j].index == i);
  10078. ref_type = func_type->ref_type_maps[j].ref_type;
  10079. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10080. ref_type,
  10081. wasm_reftype_struct_size(ref_type));
  10082. j++;
  10083. }
  10084. #endif
  10085. PUSH_TYPE(func_type->types[i]);
  10086. }
  10087. }
  10088. #if WASM_ENABLE_FAST_INTERP != 0
  10089. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10090. skip_label();
  10091. if (BLOCK_HAS_PARAM(block_type)) {
  10092. /* Make sure params are in dynamic space */
  10093. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10094. error_buf_size))
  10095. goto fail;
  10096. }
  10097. if (opcode == WASM_OP_LOOP) {
  10098. (loader_ctx->frame_csp - 1)->code_compiled =
  10099. loader_ctx->p_code_compiled;
  10100. }
  10101. }
  10102. #if WASM_ENABLE_EXCE_HANDLING != 0
  10103. else if (opcode == WASM_OP_TRY) {
  10104. skip_label();
  10105. }
  10106. #endif
  10107. else if (opcode == WASM_OP_IF) {
  10108. BranchBlock *block = loader_ctx->frame_csp - 1;
  10109. /* If block has parameters, we should make sure they are in
  10110. * dynamic space. Otherwise, when else branch is missing,
  10111. * the later opcode may consume incorrect operand offset.
  10112. * Spec case:
  10113. * (func (export "params-id") (param i32) (result i32)
  10114. * (i32.const 1)
  10115. * (i32.const 2)
  10116. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10117. * (then)) (i32.add)
  10118. * )
  10119. *
  10120. * So we should emit a copy instruction before the if.
  10121. *
  10122. * And we also need to save the parameter offsets and
  10123. * recover them before entering else branch.
  10124. *
  10125. */
  10126. if (BLOCK_HAS_PARAM(block_type)) {
  10127. uint64 size;
  10128. /* In polymorphic state, there may be no if condition on
  10129. * the stack, so the offset may not emitted */
  10130. if (if_condition_available) {
  10131. /* skip the if condition operand offset */
  10132. wasm_loader_emit_backspace(loader_ctx,
  10133. sizeof(int16));
  10134. }
  10135. /* skip the if label */
  10136. skip_label();
  10137. /* Emit a copy instruction */
  10138. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10139. error_buf_size))
  10140. goto fail;
  10141. /* Emit the if instruction */
  10142. emit_label(opcode);
  10143. /* Emit the new condition operand offset */
  10144. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10145. /* Save top param_count values of frame_offset stack, so
  10146. * that we can recover it before executing else branch
  10147. */
  10148. size = sizeof(int16)
  10149. * (uint64)block_type.u.type->param_cell_num;
  10150. if (!(block->param_frame_offsets = loader_malloc(
  10151. size, error_buf, error_buf_size)))
  10152. goto fail;
  10153. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10154. loader_ctx->frame_offset
  10155. - size / sizeof(int16),
  10156. (uint32)size);
  10157. }
  10158. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10159. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10160. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10161. }
  10162. #endif
  10163. break;
  10164. }
  10165. #if WASM_ENABLE_EXCE_HANDLING != 0
  10166. case WASM_OP_THROW:
  10167. {
  10168. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10169. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10170. uint8 label_type = cur_block->label_type;
  10171. uint32 tag_index = 0;
  10172. read_leb_int32(p, p_end, tag_index);
  10173. /* check validity of tag_index against module->tag_count */
  10174. /* check tag index is within the tag index space */
  10175. if (tag_index >= module->import_tag_count + module->tag_count) {
  10176. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10177. tag_index);
  10178. goto fail;
  10179. }
  10180. /* the tag_type is stored in either the WASMTag (section tags)
  10181. * or WASMTagImport (import tag) */
  10182. WASMFuncType *tag_type = NULL;
  10183. if (tag_index < module->import_tag_count) {
  10184. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10185. }
  10186. else {
  10187. tag_type =
  10188. module->tags[tag_index - module->import_tag_count]
  10189. ->tag_type;
  10190. }
  10191. if (tag_type->result_count != 0) {
  10192. set_error_buf(error_buf, error_buf_size,
  10193. "tag type signature does not return void");
  10194. goto fail;
  10195. }
  10196. int32 available_stack_cell =
  10197. (int32)(loader_ctx->stack_cell_num
  10198. - cur_block->stack_cell_num);
  10199. int32 tti;
  10200. /* Check stack values match return types by comparing tag param
  10201. * types with stack cells */
  10202. uint8 *frame_ref = loader_ctx->frame_ref;
  10203. #if WASM_ENABLE_GC != 0
  10204. WASMRefTypeMap *frame_reftype_map =
  10205. loader_ctx->frame_reftype_map;
  10206. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10207. /* Temporarily set these values since they may be used in
  10208. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10209. param_reftype_maps = tag_type->ref_type_maps;
  10210. /* For tag_type function, it shouldn't have result_count = 0 */
  10211. param_reftype_map_count = tag_type->ref_type_map_count;
  10212. param_count = tag_type->param_count;
  10213. #endif
  10214. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10215. #if WASM_ENABLE_GC != 0
  10216. local_type = tag_type->types[tti];
  10217. local_idx = tti;
  10218. /* Get the wasm_ref_type if the local_type is multibyte
  10219. type */
  10220. GET_LOCAL_REFTYPE();
  10221. #endif
  10222. if (!check_stack_top_values(
  10223. loader_ctx, frame_ref, available_stack_cell,
  10224. #if WASM_ENABLE_GC != 0
  10225. frame_reftype_map, frame_reftype_map_num,
  10226. #endif
  10227. tag_type->types[tti],
  10228. #if WASM_ENABLE_GC != 0
  10229. &wasm_ref_type,
  10230. #endif
  10231. error_buf, error_buf_size)) {
  10232. snprintf(error_buf, error_buf_size,
  10233. "type mismatch: instruction requires [%s] but "
  10234. "stack has [%s]",
  10235. tag_type->param_count > 0
  10236. ? type2str(tag_type->types[tti])
  10237. : "",
  10238. available_stack_cell > 0
  10239. ? type2str(*(loader_ctx->frame_ref - 1))
  10240. : "");
  10241. goto fail;
  10242. }
  10243. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10244. available_stack_cell -=
  10245. wasm_value_type_cell_num(tag_type->types[tti]);
  10246. }
  10247. #if WASM_ENABLE_GC != 0
  10248. /* Restore the values */
  10249. param_reftype_maps = func->func_type->ref_type_maps;
  10250. param_reftype_map_count = func->func_type->ref_type_map_count;
  10251. param_count = func->func_type->param_count;
  10252. #endif
  10253. /* throw is stack polymorphic */
  10254. (void)label_type;
  10255. RESET_STACK();
  10256. break;
  10257. }
  10258. case WASM_OP_RETHROW:
  10259. {
  10260. /* must be done before checking branch block */
  10261. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10262. /* check the target catching block: LABEL_TYPE_CATCH */
  10263. if (!(frame_csp_tmp =
  10264. check_branch_block(loader_ctx, &p, p_end, opcode,
  10265. error_buf, error_buf_size)))
  10266. goto fail;
  10267. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10268. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10269. /* trap according to spectest (rethrow.wast) */
  10270. set_error_buf(error_buf, error_buf_size,
  10271. "invalid rethrow label");
  10272. goto fail;
  10273. }
  10274. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10275. uint8 label_type = cur_block->label_type;
  10276. (void)label_type;
  10277. /* rethrow is stack polymorphic */
  10278. RESET_STACK();
  10279. break;
  10280. }
  10281. case WASM_OP_DELEGATE:
  10282. {
  10283. /* check target block is valid */
  10284. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10285. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10286. goto fail;
  10287. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10288. uint8 label_type = cur_block->label_type;
  10289. (void)label_type;
  10290. /* DELEGATE ends the block */
  10291. POP_CSP();
  10292. break;
  10293. }
  10294. case WASM_OP_CATCH:
  10295. {
  10296. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10297. uint8 label_type = cur_block->label_type;
  10298. uint32 tag_index = 0;
  10299. read_leb_int32(p, p_end, tag_index);
  10300. /* check validity of tag_index against module->tag_count */
  10301. /* check tag index is within the tag index space */
  10302. if (tag_index >= module->import_tag_count + module->tag_count) {
  10303. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10304. __FUNCTION__);
  10305. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10306. goto fail;
  10307. }
  10308. /* the tag_type is stored in either the WASMTag (section tags)
  10309. * or WASMTagImport (import tag) */
  10310. WASMFuncType *func_type = NULL;
  10311. if (tag_index < module->import_tag_count) {
  10312. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10313. }
  10314. else {
  10315. func_type =
  10316. module->tags[tag_index - module->import_tag_count]
  10317. ->tag_type;
  10318. }
  10319. if (func_type->result_count != 0) {
  10320. set_error_buf(error_buf, error_buf_size,
  10321. "tag type signature does not return void");
  10322. goto fail;
  10323. }
  10324. /* check validity of current label (expect LABEL_TYPE_TRY or
  10325. * LABEL_TYPE_CATCH) */
  10326. if ((LABEL_TYPE_CATCH != label_type)
  10327. && (LABEL_TYPE_TRY != label_type)) {
  10328. set_error_buf(error_buf, error_buf_size,
  10329. "Unexpected block sequence encountered.");
  10330. goto fail;
  10331. }
  10332. /*
  10333. * replace frame_csp by LABEL_TYPE_CATCH
  10334. */
  10335. cur_block->label_type = LABEL_TYPE_CATCH;
  10336. /* RESET_STACK removes the values pushed in TRY or pervious
  10337. * CATCH Blocks */
  10338. RESET_STACK();
  10339. #if WASM_ENABLE_GC != 0
  10340. WASMRefType *ref_type;
  10341. uint32 j = 0;
  10342. #endif
  10343. /* push types on the stack according to caught type */
  10344. for (i = 0; i < func_type->param_count; i++) {
  10345. #if WASM_ENABLE_GC != 0
  10346. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10347. bh_assert(func_type->ref_type_maps[j].index == i);
  10348. ref_type = func_type->ref_type_maps[j].ref_type;
  10349. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10350. ref_type,
  10351. wasm_reftype_struct_size(ref_type));
  10352. j++;
  10353. }
  10354. #endif
  10355. PUSH_TYPE(func_type->types[i]);
  10356. }
  10357. break;
  10358. }
  10359. case WASM_OP_CATCH_ALL:
  10360. {
  10361. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10362. /* expecting a TRY or CATCH, anything else will be considered an
  10363. * error */
  10364. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10365. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10366. set_error_buf(error_buf, error_buf_size,
  10367. "Unexpected block sequence encountered.");
  10368. goto fail;
  10369. }
  10370. /* no immediates */
  10371. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10372. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10373. /* RESET_STACK removes the values pushed in TRY or pervious
  10374. * CATCH Blocks */
  10375. RESET_STACK();
  10376. /* catch_all has no tagtype and therefore no parameters */
  10377. break;
  10378. }
  10379. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10380. case WASM_OP_ELSE:
  10381. handle_op_else:
  10382. {
  10383. BranchBlock *block = NULL;
  10384. BlockType block_type;
  10385. if (loader_ctx->csp_num < 2
  10386. /* the matched if isn't found */
  10387. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10388. /* duplicated else is found */
  10389. || (loader_ctx->frame_csp - 1)->else_addr) {
  10390. set_error_buf(
  10391. error_buf, error_buf_size,
  10392. "opcode else found without matched opcode if");
  10393. goto fail;
  10394. }
  10395. block = loader_ctx->frame_csp - 1;
  10396. /* check whether if branch's stack matches its result type */
  10397. if (!check_block_stack(loader_ctx, block, error_buf,
  10398. error_buf_size))
  10399. goto fail;
  10400. block->else_addr = p - 1;
  10401. block_type = block->block_type;
  10402. #if WASM_ENABLE_GC != 0
  10403. if (!wasm_loader_init_local_use_masks(
  10404. loader_ctx, local_count, error_buf, error_buf_size)) {
  10405. goto fail;
  10406. }
  10407. #endif
  10408. #if WASM_ENABLE_FAST_INTERP != 0
  10409. /* if the result of if branch is in local or const area, add a
  10410. * copy op */
  10411. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10412. error_buf, error_buf_size)) {
  10413. goto fail;
  10414. }
  10415. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10416. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10417. #endif
  10418. RESET_STACK();
  10419. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10420. /* Pass parameters to if-false branch */
  10421. if (BLOCK_HAS_PARAM(block_type)) {
  10422. for (i = 0; i < block_type.u.type->param_count; i++)
  10423. PUSH_TYPE(block_type.u.type->types[i]);
  10424. }
  10425. #if WASM_ENABLE_FAST_INTERP != 0
  10426. /* Recover top param_count values of frame_offset stack */
  10427. if (BLOCK_HAS_PARAM((block_type))) {
  10428. uint32 size;
  10429. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10430. bh_memcpy_s(loader_ctx->frame_offset, size,
  10431. block->param_frame_offsets, size);
  10432. loader_ctx->frame_offset += (size / sizeof(int16));
  10433. }
  10434. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10435. #endif
  10436. break;
  10437. }
  10438. case WASM_OP_END:
  10439. {
  10440. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10441. /* check whether block stack matches its result type */
  10442. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10443. error_buf_size))
  10444. goto fail;
  10445. /* if there is no else branch, make a virtual else opcode for
  10446. easier integrity check and to copy the correct results to
  10447. the block return address for fast-interp mode:
  10448. change if block from `if ... end` to `if ... else end` */
  10449. if (cur_block->label_type == LABEL_TYPE_IF
  10450. && !cur_block->else_addr) {
  10451. opcode = WASM_OP_ELSE;
  10452. p--;
  10453. #if WASM_ENABLE_FAST_INTERP != 0
  10454. p_org = p;
  10455. skip_label();
  10456. disable_emit = false;
  10457. emit_label(opcode);
  10458. #endif
  10459. goto handle_op_else;
  10460. }
  10461. POP_CSP();
  10462. #if WASM_ENABLE_FAST_INTERP != 0
  10463. skip_label();
  10464. /* copy the result to the block return address */
  10465. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10466. error_buf, error_buf_size)) {
  10467. /* it could be tmp frame_csp allocated from opcode like
  10468. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10469. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10470. * to free the loader_ctx->frame_csp if fails */
  10471. free_label_patch_list(loader_ctx->frame_csp);
  10472. goto fail;
  10473. }
  10474. apply_label_patch(loader_ctx, 0, PATCH_END);
  10475. free_label_patch_list(loader_ctx->frame_csp);
  10476. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10477. int32 idx;
  10478. uint8 ret_type;
  10479. emit_label(WASM_OP_RETURN);
  10480. for (idx = (int32)func->func_type->result_count - 1;
  10481. idx >= 0; idx--) {
  10482. ret_type = *(func->func_type->types
  10483. + func->func_type->param_count + idx);
  10484. POP_OFFSET_TYPE(ret_type);
  10485. }
  10486. }
  10487. #endif
  10488. if (loader_ctx->csp_num > 0) {
  10489. loader_ctx->frame_csp->end_addr = p - 1;
  10490. }
  10491. else {
  10492. /* end of function block, function will return */
  10493. if (p < p_end) {
  10494. set_error_buf(error_buf, error_buf_size,
  10495. "section size mismatch");
  10496. goto fail;
  10497. }
  10498. }
  10499. break;
  10500. }
  10501. case WASM_OP_BR:
  10502. {
  10503. if (!(frame_csp_tmp =
  10504. check_branch_block(loader_ctx, &p, p_end, opcode,
  10505. error_buf, error_buf_size)))
  10506. goto fail;
  10507. RESET_STACK();
  10508. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10509. break;
  10510. }
  10511. case WASM_OP_BR_IF:
  10512. {
  10513. POP_I32();
  10514. if (!(frame_csp_tmp =
  10515. check_branch_block(loader_ctx, &p, p_end, opcode,
  10516. error_buf, error_buf_size)))
  10517. goto fail;
  10518. break;
  10519. }
  10520. case WASM_OP_BR_TABLE:
  10521. {
  10522. uint32 depth = 0, default_arity, arity = 0;
  10523. BranchBlock *target_block;
  10524. BlockType *target_block_type;
  10525. #if WASM_ENABLE_FAST_INTERP == 0
  10526. BrTableCache *br_table_cache = NULL;
  10527. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10528. uint32 j;
  10529. #endif
  10530. read_leb_uint32(p, p_end, count);
  10531. #if WASM_ENABLE_FAST_INTERP != 0
  10532. emit_uint32(loader_ctx, count);
  10533. #endif
  10534. POP_I32();
  10535. /* Get each depth and check it */
  10536. p_org = p;
  10537. for (i = 0; i <= count; i++) {
  10538. read_leb_uint32(p, p_end, depth);
  10539. bh_assert(loader_ctx->csp_num > 0);
  10540. if (loader_ctx->csp_num - 1 < depth) {
  10541. set_error_buf(error_buf, error_buf_size,
  10542. "unknown label, "
  10543. "unexpected end of section or function");
  10544. goto fail;
  10545. }
  10546. }
  10547. p = p_org;
  10548. /* Get the default block's arity */
  10549. target_block = loader_ctx->frame_csp - (depth + 1);
  10550. target_block_type = &target_block->block_type;
  10551. default_arity = block_type_get_arity(target_block_type,
  10552. target_block->label_type);
  10553. #if WASM_ENABLE_FAST_INTERP == 0
  10554. p_depth_begin = p_depth = p;
  10555. #endif
  10556. for (i = 0; i <= count; i++) {
  10557. p_org = p;
  10558. read_leb_uint32(p, p_end, depth);
  10559. p = p_org;
  10560. /* Get the target block's arity and check it */
  10561. target_block = loader_ctx->frame_csp - (depth + 1);
  10562. target_block_type = &target_block->block_type;
  10563. arity = block_type_get_arity(target_block_type,
  10564. target_block->label_type);
  10565. if (arity != default_arity) {
  10566. set_error_buf(error_buf, error_buf_size,
  10567. "type mismatch: br_table targets must "
  10568. "all use same result type");
  10569. goto fail;
  10570. }
  10571. if (!(frame_csp_tmp =
  10572. check_branch_block(loader_ctx, &p, p_end, opcode,
  10573. error_buf, error_buf_size))) {
  10574. goto fail;
  10575. }
  10576. #if WASM_ENABLE_FAST_INTERP == 0
  10577. if (br_table_cache) {
  10578. br_table_cache->br_depths[i] = depth;
  10579. }
  10580. else {
  10581. if (depth > 255) {
  10582. /* The depth cannot be stored in one byte,
  10583. create br_table cache to store each depth */
  10584. #if WASM_ENABLE_DEBUG_INTERP != 0
  10585. if (!record_fast_op(module, p_opcode, *p_opcode,
  10586. error_buf, error_buf_size)) {
  10587. goto fail;
  10588. }
  10589. #endif
  10590. if (!(br_table_cache = loader_malloc(
  10591. offsetof(BrTableCache, br_depths)
  10592. + sizeof(uint32)
  10593. * (uint64)(count + 1),
  10594. error_buf, error_buf_size))) {
  10595. goto fail;
  10596. }
  10597. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10598. br_table_cache->br_table_op_addr = p_opcode;
  10599. br_table_cache->br_count = count;
  10600. /* Copy previous depths which are one byte */
  10601. for (j = 0; j < i; j++) {
  10602. br_table_cache->br_depths[j] = p_depth_begin[j];
  10603. }
  10604. br_table_cache->br_depths[i] = depth;
  10605. bh_list_insert(module->br_table_cache_list,
  10606. br_table_cache);
  10607. }
  10608. else {
  10609. /* The depth can be stored in one byte, use the
  10610. byte of the leb to store it */
  10611. *p_depth++ = (uint8)depth;
  10612. }
  10613. }
  10614. #endif
  10615. }
  10616. #if WASM_ENABLE_FAST_INTERP == 0
  10617. /* Set the tailing bytes to nop */
  10618. if (br_table_cache)
  10619. p_depth = p_depth_begin;
  10620. while (p_depth < p)
  10621. *p_depth++ = WASM_OP_NOP;
  10622. #endif
  10623. RESET_STACK();
  10624. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10625. break;
  10626. }
  10627. case WASM_OP_RETURN:
  10628. {
  10629. WASMFuncType *func_type = func->func_type;
  10630. int32 idx;
  10631. uint8 ret_type;
  10632. #if WASM_ENABLE_GC != 0
  10633. uint32 j = func_type->ref_type_map_count - 1;
  10634. #endif
  10635. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10636. idx--) {
  10637. ret_type =
  10638. *(func_type->types + func_type->param_count + idx);
  10639. #if WASM_ENABLE_GC != 0
  10640. if (wasm_is_type_multi_byte_type(ret_type)) {
  10641. WASMRefType *ref_type =
  10642. func_type->ref_type_maps[j].ref_type;
  10643. bh_assert(func_type->ref_type_maps[j].index
  10644. == func_type->param_count + idx);
  10645. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10646. ref_type,
  10647. wasm_reftype_struct_size(ref_type));
  10648. j--;
  10649. }
  10650. #endif
  10651. #if WASM_ENABLE_FAST_INTERP != 0
  10652. /* emit the offset after return opcode */
  10653. POP_OFFSET_TYPE(ret_type);
  10654. #endif
  10655. POP_TYPE(ret_type);
  10656. }
  10657. RESET_STACK();
  10658. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10659. break;
  10660. }
  10661. case WASM_OP_CALL:
  10662. #if WASM_ENABLE_TAIL_CALL != 0
  10663. case WASM_OP_RETURN_CALL:
  10664. #endif
  10665. #if WASM_ENABLE_GC != 0
  10666. case WASM_OP_CALL_REF:
  10667. case WASM_OP_RETURN_CALL_REF:
  10668. #endif
  10669. {
  10670. WASMFuncType *func_type;
  10671. uint8 type;
  10672. int32 idx;
  10673. #if WASM_ENABLE_GC != 0
  10674. WASMRefType *ref_type;
  10675. uint32 type_idx1;
  10676. int32 j;
  10677. #endif
  10678. #if WASM_ENABLE_GC != 0
  10679. if (opcode == WASM_OP_CALL_REF
  10680. || opcode == WASM_OP_RETURN_CALL_REF) {
  10681. read_leb_uint32(p, p_end, type_idx1);
  10682. if (!check_type_index(module, module->type_count, type_idx1,
  10683. error_buf, error_buf_size)) {
  10684. goto fail;
  10685. }
  10686. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10687. set_error_buf(error_buf, error_buf_size,
  10688. "unkown function type");
  10689. goto fail;
  10690. }
  10691. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10692. &type_idx, error_buf,
  10693. error_buf_size)) {
  10694. goto fail;
  10695. }
  10696. if (type == VALUE_TYPE_ANY) {
  10697. type_idx = type_idx1;
  10698. }
  10699. if (!check_type_index(module, module->type_count, type_idx,
  10700. error_buf, error_buf_size)) {
  10701. goto fail;
  10702. }
  10703. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10704. set_error_buf(error_buf, error_buf_size,
  10705. "unkown function type");
  10706. goto fail;
  10707. }
  10708. if (!wasm_func_type_is_super_of(
  10709. (WASMFuncType *)module->types[type_idx1],
  10710. (WASMFuncType *)module->types[type_idx])) {
  10711. set_error_buf(error_buf, error_buf_size,
  10712. "function type mismatch");
  10713. goto fail;
  10714. }
  10715. func_type = (WASMFuncType *)module->types[type_idx];
  10716. }
  10717. else
  10718. #endif
  10719. {
  10720. read_leb_uint32(p, p_end, func_idx);
  10721. #if WASM_ENABLE_FAST_INTERP != 0
  10722. /* we need to emit func_idx before arguments */
  10723. emit_uint32(loader_ctx, func_idx);
  10724. #endif
  10725. if (!check_function_index(module, func_idx, error_buf,
  10726. error_buf_size)) {
  10727. goto fail;
  10728. }
  10729. if (func_idx < module->import_function_count)
  10730. func_type = module->import_functions[func_idx]
  10731. .u.function.func_type;
  10732. else
  10733. func_type =
  10734. module
  10735. ->functions[func_idx
  10736. - module->import_function_count]
  10737. ->func_type;
  10738. }
  10739. if (func_type->param_count > 0) {
  10740. #if WASM_ENABLE_GC != 0
  10741. j = (int32)(func_type->result_ref_type_maps
  10742. - func_type->ref_type_maps - 1);
  10743. #endif
  10744. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10745. idx--) {
  10746. #if WASM_ENABLE_GC != 0
  10747. if (wasm_is_type_multi_byte_type(
  10748. func_type->types[idx])) {
  10749. ref_type = func_type->ref_type_maps[j].ref_type;
  10750. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10751. ref_type,
  10752. wasm_reftype_struct_size(ref_type));
  10753. j--;
  10754. }
  10755. #endif
  10756. #if WASM_ENABLE_FAST_INTERP != 0
  10757. POP_OFFSET_TYPE(func_type->types[idx]);
  10758. #endif
  10759. POP_TYPE(func_type->types[idx]);
  10760. }
  10761. }
  10762. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10763. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10764. #endif
  10765. #if WASM_ENABLE_GC != 0
  10766. j = (int32)(func_type->result_ref_type_maps
  10767. - func_type->ref_type_maps);
  10768. #endif
  10769. for (i = 0; i < func_type->result_count; i++) {
  10770. #if WASM_ENABLE_GC != 0
  10771. if (wasm_is_type_multi_byte_type(
  10772. func_type->types[func_type->param_count + i])) {
  10773. ref_type = func_type->ref_type_maps[j].ref_type;
  10774. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10775. ref_type,
  10776. wasm_reftype_struct_size(ref_type));
  10777. j++;
  10778. }
  10779. #endif
  10780. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10781. #if WASM_ENABLE_FAST_INTERP != 0
  10782. /* Here we emit each return value's dynamic_offset. But
  10783. * in fact these offsets are continuous, so interpreter
  10784. * only need to get the first return value's offset.
  10785. */
  10786. PUSH_OFFSET_TYPE(
  10787. func_type->types[func_type->param_count + i]);
  10788. #endif
  10789. }
  10790. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10791. }
  10792. else {
  10793. #if WASM_ENABLE_GC == 0
  10794. if (func_type->result_count
  10795. != func->func_type->result_count) {
  10796. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10797. "type mismatch: expect ",
  10798. func->func_type->result_count,
  10799. " return values but got other");
  10800. goto fail;
  10801. }
  10802. for (i = 0; i < func_type->result_count; i++) {
  10803. type = func->func_type
  10804. ->types[func->func_type->param_count + i];
  10805. if (func_type->types[func_type->param_count + i]
  10806. != type) {
  10807. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10808. "type mismatch: expect ",
  10809. type2str(type), " but got other");
  10810. goto fail;
  10811. }
  10812. }
  10813. #else
  10814. if (!wasm_func_type_result_is_subtype_of(
  10815. func_type, func->func_type, module->types,
  10816. module->type_count)) {
  10817. set_error_buf(
  10818. error_buf, error_buf_size,
  10819. "type mismatch: invalid func result types");
  10820. goto fail;
  10821. }
  10822. #endif
  10823. RESET_STACK();
  10824. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10825. }
  10826. #endif
  10827. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10828. || WASM_ENABLE_WAMR_COMPILER != 0
  10829. func->has_op_func_call = true;
  10830. #endif
  10831. (void)type;
  10832. break;
  10833. }
  10834. /*
  10835. * if disable reference type: call_indirect typeidx, 0x00
  10836. * if enable reference type: call_indirect typeidx, tableidx
  10837. */
  10838. case WASM_OP_CALL_INDIRECT:
  10839. #if WASM_ENABLE_TAIL_CALL != 0
  10840. case WASM_OP_RETURN_CALL_INDIRECT:
  10841. #endif
  10842. {
  10843. int32 idx;
  10844. WASMFuncType *func_type;
  10845. read_leb_uint32(p, p_end, type_idx);
  10846. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10847. #if WASM_ENABLE_WAMR_COMPILER != 0
  10848. if (p + 1 < p_end && *p != 0x00) {
  10849. /*
  10850. * Any non-0x00 byte requires the ref types proposal.
  10851. * This is different from checking the table_idx value
  10852. * since `0x80 0x00` etc. are all valid encodings of zero.
  10853. */
  10854. module->is_ref_types_used = true;
  10855. }
  10856. #endif
  10857. read_leb_uint32(p, p_end, table_idx);
  10858. #else
  10859. CHECK_BUF(p, p_end, 1);
  10860. table_idx = read_uint8(p);
  10861. #endif
  10862. if (!check_table_index(module, table_idx, error_buf,
  10863. error_buf_size)) {
  10864. goto fail;
  10865. }
  10866. #if WASM_ENABLE_FAST_INTERP != 0
  10867. /* we need to emit before arguments */
  10868. #if WASM_ENABLE_TAIL_CALL != 0
  10869. emit_byte(loader_ctx, opcode);
  10870. #endif
  10871. emit_uint32(loader_ctx, type_idx);
  10872. emit_uint32(loader_ctx, table_idx);
  10873. #endif
  10874. #if WASM_ENABLE_MEMORY64 != 0
  10875. table_elem_idx_type = is_table_64bit(module, table_idx)
  10876. ? VALUE_TYPE_I64
  10877. : VALUE_TYPE_I32;
  10878. #endif
  10879. /* skip elem idx */
  10880. POP_TBL_ELEM_IDX();
  10881. if (type_idx >= module->type_count) {
  10882. set_error_buf(error_buf, error_buf_size, "unknown type");
  10883. goto fail;
  10884. }
  10885. func_type = (WASMFuncType *)module->types[type_idx];
  10886. if (func_type->param_count > 0) {
  10887. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10888. idx--) {
  10889. #if WASM_ENABLE_FAST_INTERP != 0
  10890. POP_OFFSET_TYPE(func_type->types[idx]);
  10891. #endif
  10892. POP_TYPE(func_type->types[idx]);
  10893. }
  10894. }
  10895. #if WASM_ENABLE_TAIL_CALL != 0
  10896. if (opcode == WASM_OP_CALL_INDIRECT) {
  10897. #endif
  10898. for (i = 0; i < func_type->result_count; i++) {
  10899. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10900. #if WASM_ENABLE_FAST_INTERP != 0
  10901. PUSH_OFFSET_TYPE(
  10902. func_type->types[func_type->param_count + i]);
  10903. #endif
  10904. }
  10905. #if WASM_ENABLE_TAIL_CALL != 0
  10906. }
  10907. else {
  10908. uint8 type;
  10909. if (func_type->result_count
  10910. != func->func_type->result_count) {
  10911. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10912. "type mismatch: expect ",
  10913. func->func_type->result_count,
  10914. " return values but got other");
  10915. goto fail;
  10916. }
  10917. for (i = 0; i < func_type->result_count; i++) {
  10918. type = func->func_type
  10919. ->types[func->func_type->param_count + i];
  10920. if (func_type->types[func_type->param_count + i]
  10921. != type) {
  10922. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10923. "type mismatch: expect ",
  10924. type2str(type), " but got other");
  10925. goto fail;
  10926. }
  10927. }
  10928. RESET_STACK();
  10929. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10930. }
  10931. #endif
  10932. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10933. || WASM_ENABLE_WAMR_COMPILER != 0
  10934. func->has_op_func_call = true;
  10935. #endif
  10936. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10937. func->has_op_call_indirect = true;
  10938. #endif
  10939. break;
  10940. }
  10941. case WASM_OP_DROP:
  10942. {
  10943. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10944. int32 available_stack_cell =
  10945. (int32)(loader_ctx->stack_cell_num
  10946. - cur_block->stack_cell_num);
  10947. if (available_stack_cell <= 0
  10948. && !cur_block->is_stack_polymorphic) {
  10949. set_error_buf(error_buf, error_buf_size,
  10950. "type mismatch, opcode drop was found "
  10951. "but stack was empty");
  10952. goto fail;
  10953. }
  10954. if (available_stack_cell > 0) {
  10955. #if WASM_ENABLE_GC != 0
  10956. if (wasm_is_type_multi_byte_type(
  10957. *(loader_ctx->frame_ref - 1))) {
  10958. bh_assert((int32)(loader_ctx->reftype_map_num
  10959. - cur_block->reftype_map_num)
  10960. > 0);
  10961. loader_ctx->frame_reftype_map--;
  10962. loader_ctx->reftype_map_num--;
  10963. }
  10964. #endif
  10965. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10966. loader_ctx->frame_ref--;
  10967. loader_ctx->stack_cell_num--;
  10968. #if WASM_ENABLE_FAST_INTERP != 0
  10969. skip_label();
  10970. loader_ctx->frame_offset--;
  10971. if ((*(loader_ctx->frame_offset)
  10972. > loader_ctx->start_dynamic_offset)
  10973. && (*(loader_ctx->frame_offset)
  10974. < loader_ctx->max_dynamic_offset))
  10975. loader_ctx->dynamic_offset--;
  10976. #endif
  10977. }
  10978. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10979. loader_ctx->frame_ref -= 2;
  10980. loader_ctx->stack_cell_num -= 2;
  10981. #if WASM_ENABLE_FAST_INTERP == 0
  10982. *(p - 1) = WASM_OP_DROP_64;
  10983. #endif
  10984. #if WASM_ENABLE_FAST_INTERP != 0
  10985. skip_label();
  10986. loader_ctx->frame_offset -= 2;
  10987. if ((*(loader_ctx->frame_offset)
  10988. > loader_ctx->start_dynamic_offset)
  10989. && (*(loader_ctx->frame_offset)
  10990. < loader_ctx->max_dynamic_offset))
  10991. loader_ctx->dynamic_offset -= 2;
  10992. #endif
  10993. }
  10994. #if WASM_ENABLE_SIMD != 0
  10995. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10996. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10997. loader_ctx->frame_ref -= 4;
  10998. loader_ctx->stack_cell_num -= 4;
  10999. }
  11000. #endif
  11001. #endif
  11002. else {
  11003. set_error_buf(error_buf, error_buf_size,
  11004. "type mismatch");
  11005. goto fail;
  11006. }
  11007. }
  11008. else {
  11009. #if WASM_ENABLE_FAST_INTERP != 0
  11010. skip_label();
  11011. #endif
  11012. }
  11013. break;
  11014. }
  11015. case WASM_OP_SELECT:
  11016. {
  11017. uint8 ref_type;
  11018. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11019. int32 available_stack_cell;
  11020. #if WASM_ENABLE_FAST_INTERP != 0
  11021. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11022. #endif
  11023. POP_I32();
  11024. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11025. - cur_block->stack_cell_num);
  11026. if (available_stack_cell <= 0
  11027. && !cur_block->is_stack_polymorphic) {
  11028. set_error_buf(error_buf, error_buf_size,
  11029. "type mismatch or invalid result arity, "
  11030. "opcode select was found "
  11031. "but stack was empty");
  11032. goto fail;
  11033. }
  11034. if (available_stack_cell > 0) {
  11035. switch (*(loader_ctx->frame_ref - 1)) {
  11036. case VALUE_TYPE_I32:
  11037. case VALUE_TYPE_F32:
  11038. case VALUE_TYPE_ANY:
  11039. break;
  11040. case VALUE_TYPE_I64:
  11041. case VALUE_TYPE_F64:
  11042. #if WASM_ENABLE_FAST_INTERP == 0
  11043. *(p - 1) = WASM_OP_SELECT_64;
  11044. #endif
  11045. #if WASM_ENABLE_FAST_INTERP != 0
  11046. if (loader_ctx->p_code_compiled) {
  11047. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11048. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11049. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11050. *(void **)(p_code_compiled_tmp
  11051. - sizeof(void *)) =
  11052. handle_table[opcode_tmp];
  11053. #else
  11054. #if UINTPTR_MAX == UINT64_MAX
  11055. /* emit int32 relative offset in 64-bit target
  11056. */
  11057. int32 offset =
  11058. (int32)((uint8 *)handle_table[opcode_tmp]
  11059. - (uint8 *)handle_table[0]);
  11060. *(int32 *)(p_code_compiled_tmp
  11061. - sizeof(int32)) = offset;
  11062. #else
  11063. /* emit uint32 label address in 32-bit target */
  11064. *(uint32 *)(p_code_compiled_tmp
  11065. - sizeof(uint32)) =
  11066. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11067. #endif
  11068. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11069. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11070. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11071. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11072. #else
  11073. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11074. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11075. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11076. }
  11077. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11078. break;
  11079. #if WASM_ENABLE_SIMD != 0
  11080. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11081. case VALUE_TYPE_V128:
  11082. break;
  11083. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11084. #endif /* WASM_ENABLE_SIMD != 0 */
  11085. default:
  11086. {
  11087. set_error_buf(error_buf, error_buf_size,
  11088. "type mismatch");
  11089. goto fail;
  11090. }
  11091. }
  11092. ref_type = *(loader_ctx->frame_ref - 1);
  11093. #if WASM_ENABLE_FAST_INTERP != 0
  11094. POP_OFFSET_TYPE(ref_type);
  11095. POP_TYPE(ref_type);
  11096. POP_OFFSET_TYPE(ref_type);
  11097. POP_TYPE(ref_type);
  11098. PUSH_OFFSET_TYPE(ref_type);
  11099. PUSH_TYPE(ref_type);
  11100. #else
  11101. POP2_AND_PUSH(ref_type, ref_type);
  11102. #endif
  11103. }
  11104. else {
  11105. #if WASM_ENABLE_FAST_INTERP != 0
  11106. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11107. #endif
  11108. PUSH_TYPE(VALUE_TYPE_ANY);
  11109. }
  11110. break;
  11111. }
  11112. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11113. case WASM_OP_SELECT_T:
  11114. {
  11115. uint8 vec_len, type;
  11116. #if WASM_ENABLE_GC != 0
  11117. WASMRefType *ref_type = NULL;
  11118. #endif
  11119. #if WASM_ENABLE_FAST_INTERP != 0
  11120. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11121. #endif
  11122. read_leb_uint32(p, p_end, vec_len);
  11123. if (vec_len != 1) {
  11124. /* typed select must have exactly one result */
  11125. set_error_buf(error_buf, error_buf_size,
  11126. "invalid result arity");
  11127. goto fail;
  11128. }
  11129. #if WASM_ENABLE_GC == 0
  11130. CHECK_BUF(p, p_end, 1);
  11131. type = read_uint8(p);
  11132. if (!is_valid_value_type_for_interpreter(type)) {
  11133. set_error_buf(error_buf, error_buf_size,
  11134. "unknown value type");
  11135. goto fail;
  11136. }
  11137. #else
  11138. p_org = p + 1;
  11139. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11140. module->type_count, &need_ref_type_map,
  11141. &wasm_ref_type, false, error_buf,
  11142. error_buf_size)) {
  11143. goto fail;
  11144. }
  11145. type = wasm_ref_type.ref_type;
  11146. if (need_ref_type_map) {
  11147. if (!(ref_type = reftype_set_insert(
  11148. module->ref_type_set, &wasm_ref_type, error_buf,
  11149. error_buf_size))) {
  11150. goto fail;
  11151. }
  11152. }
  11153. #if WASM_ENABLE_FAST_INTERP == 0
  11154. while (p_org < p) {
  11155. #if WASM_ENABLE_DEBUG_INTERP != 0
  11156. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11157. error_buf_size)) {
  11158. goto fail;
  11159. }
  11160. #endif
  11161. /* Ignore extra bytes for interpreter */
  11162. *p_org++ = WASM_OP_NOP;
  11163. }
  11164. #endif
  11165. #endif /* end of WASM_ENABLE_GC == 0 */
  11166. POP_I32();
  11167. #if WASM_ENABLE_FAST_INTERP != 0
  11168. if (loader_ctx->p_code_compiled) {
  11169. uint8 opcode_tmp = WASM_OP_SELECT;
  11170. if (type == VALUE_TYPE_V128) {
  11171. #if (WASM_ENABLE_SIMD == 0) \
  11172. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11173. set_error_buf(error_buf, error_buf_size,
  11174. "SIMD v128 type isn't supported");
  11175. goto fail;
  11176. #endif
  11177. }
  11178. else {
  11179. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11180. opcode_tmp = WASM_OP_SELECT_64;
  11181. #if WASM_ENABLE_GC != 0
  11182. if (wasm_is_type_reftype(type))
  11183. opcode_tmp = WASM_OP_SELECT_T;
  11184. #endif
  11185. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11186. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11187. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11188. handle_table[opcode_tmp];
  11189. #else
  11190. #if UINTPTR_MAX == UINT64_MAX
  11191. /* emit int32 relative offset in 64-bit target */
  11192. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11193. - (uint8 *)handle_table[0]);
  11194. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11195. offset;
  11196. #else
  11197. /* emit uint32 label address in 32-bit target */
  11198. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11199. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11200. #endif
  11201. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11202. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11203. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11204. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11205. #else
  11206. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11207. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11208. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11209. }
  11210. }
  11211. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11212. POP_REF(type);
  11213. #if WASM_ENABLE_GC != 0
  11214. if (need_ref_type_map) {
  11215. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11216. wasm_reftype_struct_size(ref_type));
  11217. }
  11218. #endif
  11219. POP_REF(type);
  11220. #if WASM_ENABLE_GC != 0
  11221. if (need_ref_type_map) {
  11222. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11223. wasm_reftype_struct_size(ref_type));
  11224. }
  11225. #endif
  11226. PUSH_REF(type);
  11227. #if WASM_ENABLE_WAMR_COMPILER != 0
  11228. module->is_ref_types_used = true;
  11229. #endif
  11230. (void)vec_len;
  11231. break;
  11232. }
  11233. /* table.get x. tables[x]. [it] -> [t] */
  11234. /* table.set x. tables[x]. [it t] -> [] */
  11235. case WASM_OP_TABLE_GET:
  11236. case WASM_OP_TABLE_SET:
  11237. {
  11238. uint8 decl_ref_type;
  11239. #if WASM_ENABLE_GC != 0
  11240. WASMRefType *ref_type;
  11241. #endif
  11242. read_leb_uint32(p, p_end, table_idx);
  11243. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11244. #if WASM_ENABLE_GC != 0
  11245. (void **)&ref_type,
  11246. #else
  11247. NULL,
  11248. #endif
  11249. error_buf, error_buf_size))
  11250. goto fail;
  11251. #if WASM_ENABLE_GC != 0
  11252. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11253. bh_assert(ref_type);
  11254. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11255. ref_type, wasm_reftype_struct_size(ref_type));
  11256. }
  11257. #endif
  11258. #if WASM_ENABLE_FAST_INTERP != 0
  11259. emit_uint32(loader_ctx, table_idx);
  11260. #endif
  11261. #if WASM_ENABLE_MEMORY64 != 0
  11262. table_elem_idx_type = is_table_64bit(module, table_idx)
  11263. ? VALUE_TYPE_I64
  11264. : VALUE_TYPE_I32;
  11265. #endif
  11266. if (opcode == WASM_OP_TABLE_GET) {
  11267. POP_TBL_ELEM_IDX();
  11268. #if WASM_ENABLE_FAST_INTERP != 0
  11269. PUSH_OFFSET_TYPE(decl_ref_type);
  11270. #endif
  11271. PUSH_TYPE(decl_ref_type);
  11272. }
  11273. else {
  11274. #if WASM_ENABLE_FAST_INTERP != 0
  11275. POP_OFFSET_TYPE(decl_ref_type);
  11276. #endif
  11277. POP_TYPE(decl_ref_type);
  11278. POP_TBL_ELEM_IDX();
  11279. }
  11280. #if WASM_ENABLE_WAMR_COMPILER != 0
  11281. module->is_ref_types_used = true;
  11282. #endif
  11283. break;
  11284. }
  11285. case WASM_OP_REF_NULL:
  11286. {
  11287. uint8 ref_type;
  11288. #if WASM_ENABLE_GC == 0
  11289. CHECK_BUF(p, p_end, 1);
  11290. ref_type = read_uint8(p);
  11291. if (ref_type != VALUE_TYPE_FUNCREF
  11292. && ref_type != VALUE_TYPE_EXTERNREF) {
  11293. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11294. goto fail;
  11295. }
  11296. #else
  11297. read_leb_int32(p, p_end, heap_type);
  11298. if (heap_type >= 0) {
  11299. if (!check_type_index(module, module->type_count, heap_type,
  11300. error_buf, error_buf_size)) {
  11301. goto fail;
  11302. }
  11303. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11304. true, heap_type);
  11305. ref_type = wasm_ref_type.ref_type;
  11306. }
  11307. else {
  11308. if (!wasm_is_valid_heap_type(heap_type)) {
  11309. set_error_buf(error_buf, error_buf_size,
  11310. "unknown type");
  11311. goto fail;
  11312. }
  11313. ref_type = (uint8)((int32)0x80 + heap_type);
  11314. }
  11315. #endif /* end of WASM_ENABLE_GC == 0 */
  11316. #if WASM_ENABLE_FAST_INTERP != 0
  11317. PUSH_OFFSET_TYPE(ref_type);
  11318. #endif
  11319. PUSH_TYPE(ref_type);
  11320. #if WASM_ENABLE_WAMR_COMPILER != 0
  11321. module->is_ref_types_used = true;
  11322. #endif
  11323. break;
  11324. }
  11325. case WASM_OP_REF_IS_NULL:
  11326. {
  11327. #if WASM_ENABLE_GC == 0
  11328. #if WASM_ENABLE_FAST_INTERP != 0
  11329. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11330. int32 block_stack_cell_num =
  11331. (int32)(loader_ctx->stack_cell_num
  11332. - cur_block->stack_cell_num);
  11333. if (block_stack_cell_num <= 0) {
  11334. if (!cur_block->is_stack_polymorphic) {
  11335. set_error_buf(
  11336. error_buf, error_buf_size,
  11337. "type mismatch: expect data but stack was empty");
  11338. goto fail;
  11339. }
  11340. }
  11341. else {
  11342. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11343. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11344. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11345. if (!wasm_loader_pop_frame_ref_offset(
  11346. loader_ctx, *(loader_ctx->frame_ref - 1),
  11347. error_buf, error_buf_size)) {
  11348. goto fail;
  11349. }
  11350. }
  11351. else {
  11352. set_error_buf(error_buf, error_buf_size,
  11353. "type mismatch");
  11354. goto fail;
  11355. }
  11356. }
  11357. #else
  11358. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11359. error_buf, error_buf_size)
  11360. && !wasm_loader_pop_frame_ref(loader_ctx,
  11361. VALUE_TYPE_EXTERNREF,
  11362. error_buf, error_buf_size)) {
  11363. goto fail;
  11364. }
  11365. #endif
  11366. #else /* else of WASM_ENABLE_GC == 0 */
  11367. uint8 type;
  11368. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11369. error_buf, error_buf_size)) {
  11370. goto fail;
  11371. }
  11372. #endif
  11373. PUSH_I32();
  11374. #if WASM_ENABLE_WAMR_COMPILER != 0
  11375. module->is_ref_types_used = true;
  11376. #endif
  11377. break;
  11378. }
  11379. case WASM_OP_REF_FUNC:
  11380. {
  11381. read_leb_uint32(p, p_end, func_idx);
  11382. if (!check_function_index(module, func_idx, error_buf,
  11383. error_buf_size)) {
  11384. goto fail;
  11385. }
  11386. /* Refer to a forward-declared function:
  11387. the function must be an import, exported, or present in
  11388. a table elem segment or global initializer to be used as
  11389. the operand to ref.func */
  11390. if (func_idx >= module->import_function_count) {
  11391. WASMTableSeg *table_seg = module->table_segments;
  11392. bool func_declared = false;
  11393. uint32 j;
  11394. for (i = 0; i < module->global_count; i++) {
  11395. if (module->globals[i].type.val_type
  11396. == VALUE_TYPE_FUNCREF
  11397. && module->globals[i].init_expr.init_expr_type
  11398. == INIT_EXPR_TYPE_FUNCREF_CONST
  11399. && module->globals[i].init_expr.u.u32 == func_idx) {
  11400. func_declared = true;
  11401. break;
  11402. }
  11403. }
  11404. if (!func_declared) {
  11405. /* Check whether the function is declared in table segs,
  11406. note that it doesn't matter whether the table seg's
  11407. mode is passive, active or declarative. */
  11408. for (i = 0; i < module->table_seg_count;
  11409. i++, table_seg++) {
  11410. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11411. #if WASM_ENABLE_GC != 0
  11412. /* elem type is (ref null? func) or
  11413. (ref null? $t) */
  11414. || ((table_seg->elem_type
  11415. == REF_TYPE_HT_NON_NULLABLE
  11416. || table_seg->elem_type
  11417. == REF_TYPE_HT_NULLABLE)
  11418. && (table_seg->elem_ref_type->ref_ht_common
  11419. .heap_type
  11420. == HEAP_TYPE_FUNC
  11421. || table_seg->elem_ref_type
  11422. ->ref_ht_common.heap_type
  11423. > 0))
  11424. #endif
  11425. ) {
  11426. for (j = 0; j < table_seg->value_count; j++) {
  11427. if (table_seg->init_values[j].u.ref_index
  11428. == func_idx) {
  11429. func_declared = true;
  11430. break;
  11431. }
  11432. }
  11433. }
  11434. }
  11435. }
  11436. if (!func_declared) {
  11437. /* Check whether the function is exported */
  11438. for (i = 0; i < module->export_count; i++) {
  11439. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11440. && module->exports[i].index == func_idx) {
  11441. func_declared = true;
  11442. break;
  11443. }
  11444. }
  11445. }
  11446. if (!func_declared) {
  11447. set_error_buf(error_buf, error_buf_size,
  11448. "undeclared function reference");
  11449. goto fail;
  11450. }
  11451. }
  11452. #if WASM_ENABLE_FAST_INTERP != 0
  11453. emit_uint32(loader_ctx, func_idx);
  11454. #endif
  11455. #if WASM_ENABLE_GC == 0
  11456. PUSH_FUNCREF();
  11457. #else
  11458. if (func_idx < module->import_function_count)
  11459. type_idx =
  11460. module->import_functions[func_idx].u.function.type_idx;
  11461. else
  11462. type_idx = module
  11463. ->functions[func_idx
  11464. - module->import_function_count]
  11465. ->type_idx;
  11466. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11467. false, type_idx);
  11468. PUSH_REF(wasm_ref_type.ref_type);
  11469. #endif
  11470. #if WASM_ENABLE_WAMR_COMPILER != 0
  11471. module->is_ref_types_used = true;
  11472. #endif
  11473. break;
  11474. }
  11475. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11476. #if WASM_ENABLE_GC != 0
  11477. case WASM_OP_REF_AS_NON_NULL:
  11478. case WASM_OP_BR_ON_NULL:
  11479. {
  11480. uint8 type;
  11481. WASMRefType ref_type;
  11482. /* POP (ref null ht) and get the converted (ref ht) */
  11483. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11484. error_buf, error_buf_size)) {
  11485. goto fail;
  11486. }
  11487. if (opcode == WASM_OP_BR_ON_NULL) {
  11488. if (!(frame_csp_tmp =
  11489. check_branch_block(loader_ctx, &p, p_end, opcode,
  11490. error_buf, error_buf_size))) {
  11491. goto fail;
  11492. }
  11493. #if WASM_ENABLE_FAST_INTERP != 0
  11494. disable_emit = true;
  11495. #endif
  11496. }
  11497. /* PUSH the converted (ref ht) */
  11498. if (type != VALUE_TYPE_ANY) {
  11499. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11500. sizeof(WASMRefType));
  11501. }
  11502. PUSH_REF(type);
  11503. break;
  11504. }
  11505. case WASM_OP_BR_ON_NON_NULL:
  11506. {
  11507. uint8 type;
  11508. WASMRefType ref_type;
  11509. uint32 available_stack_cell =
  11510. loader_ctx->stack_cell_num
  11511. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11512. /* POP (ref null ht) and get the converted (ref ht) */
  11513. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11514. error_buf, error_buf_size)) {
  11515. goto fail;
  11516. }
  11517. #if WASM_ENABLE_FAST_INTERP != 0
  11518. disable_emit = true;
  11519. #endif
  11520. /* Temporarily PUSH back (ref ht), check brach block and
  11521. then POP it */
  11522. if (available_stack_cell
  11523. > 0) { /* stack isn't in polymorphic state */
  11524. if (type != VALUE_TYPE_ANY) {
  11525. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11526. &ref_type, sizeof(WASMRefType));
  11527. }
  11528. PUSH_REF(type);
  11529. }
  11530. if (!(frame_csp_tmp =
  11531. check_branch_block(loader_ctx, &p, p_end, opcode,
  11532. error_buf, error_buf_size))) {
  11533. goto fail;
  11534. }
  11535. if (available_stack_cell
  11536. > 0) { /* stack isn't in polymorphic state */
  11537. POP_REF(type);
  11538. #if WASM_ENABLE_FAST_INTERP != 0
  11539. /* Erase the opnd offset emitted by POP_REF() */
  11540. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11541. #endif
  11542. }
  11543. break;
  11544. }
  11545. case WASM_OP_REF_EQ:
  11546. POP_REF(REF_TYPE_EQREF);
  11547. POP_REF(REF_TYPE_EQREF);
  11548. PUSH_I32();
  11549. break;
  11550. #endif /* end of WASM_ENABLE_GC != 0 */
  11551. case WASM_OP_GET_LOCAL:
  11552. {
  11553. p_org = p - 1;
  11554. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11555. PUSH_TYPE(local_type);
  11556. #if WASM_ENABLE_GC != 0
  11557. /* Cannot get a non-nullable and unset local */
  11558. if (local_idx >= param_count
  11559. && wasm_is_reftype_htref_non_nullable(local_type)
  11560. && !wasm_loader_get_local_status(loader_ctx,
  11561. local_idx - param_count)) {
  11562. set_error_buf(error_buf, error_buf_size,
  11563. "uninitialized local");
  11564. goto fail;
  11565. }
  11566. #endif
  11567. #if WASM_ENABLE_FAST_INTERP != 0
  11568. /* Get Local is optimized out */
  11569. skip_label();
  11570. disable_emit = true;
  11571. operand_offset = local_offset;
  11572. PUSH_OFFSET_TYPE(local_type);
  11573. #else
  11574. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11575. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11576. if (local_offset < 0x80
  11577. #if WASM_ENABLE_GC != 0
  11578. && !wasm_is_type_reftype(local_type)
  11579. #endif
  11580. ) {
  11581. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11582. if (is_32bit_type(local_type)) {
  11583. *p_org++ = (uint8)local_offset;
  11584. }
  11585. else {
  11586. *p_org++ = (uint8)(local_offset | 0x80);
  11587. }
  11588. while (p_org < p) {
  11589. *p_org++ = WASM_OP_NOP;
  11590. }
  11591. }
  11592. #endif
  11593. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11594. break;
  11595. }
  11596. case WASM_OP_SET_LOCAL:
  11597. {
  11598. p_org = p - 1;
  11599. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11600. #if WASM_ENABLE_FAST_INTERP != 0
  11601. if (!(preserve_referenced_local(
  11602. loader_ctx, opcode, local_offset, local_type,
  11603. &preserve_local, error_buf, error_buf_size)))
  11604. goto fail;
  11605. if (local_offset < 256
  11606. #if WASM_ENABLE_GC != 0
  11607. && !wasm_is_type_reftype(local_type)
  11608. #endif
  11609. ) {
  11610. skip_label();
  11611. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11612. if (loader_ctx->p_code_compiled)
  11613. STORE_U16(loader_ctx->p_code_compiled - 2,
  11614. local_offset);
  11615. loader_ctx->frame_offset--;
  11616. loader_ctx->dynamic_offset--;
  11617. }
  11618. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11619. if (loader_ctx->p_code_compiled)
  11620. STORE_U16(loader_ctx->p_code_compiled - 2,
  11621. local_offset);
  11622. loader_ctx->frame_offset -= 2;
  11623. loader_ctx->dynamic_offset -= 2;
  11624. }
  11625. else {
  11626. if (is_32bit_type(local_type)) {
  11627. emit_label(EXT_OP_SET_LOCAL_FAST);
  11628. emit_byte(loader_ctx, (uint8)local_offset);
  11629. }
  11630. else {
  11631. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11632. emit_byte(loader_ctx, (uint8)local_offset);
  11633. }
  11634. POP_OFFSET_TYPE(local_type);
  11635. }
  11636. }
  11637. else { /* local index larger than 255, reserve leb */
  11638. emit_uint32(loader_ctx, local_idx);
  11639. POP_OFFSET_TYPE(local_type);
  11640. }
  11641. #else
  11642. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11643. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11644. if (local_offset < 0x80
  11645. #if WASM_ENABLE_GC != 0
  11646. && !wasm_is_type_reftype(local_type)
  11647. #endif
  11648. ) {
  11649. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11650. if (is_32bit_type(local_type)) {
  11651. *p_org++ = (uint8)local_offset;
  11652. }
  11653. else {
  11654. *p_org++ = (uint8)(local_offset | 0x80);
  11655. }
  11656. while (p_org < p) {
  11657. *p_org++ = WASM_OP_NOP;
  11658. }
  11659. }
  11660. #endif
  11661. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11662. #if WASM_ENABLE_GC != 0
  11663. if (local_idx >= param_count) {
  11664. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11665. }
  11666. #endif
  11667. POP_TYPE(local_type);
  11668. break;
  11669. }
  11670. case WASM_OP_TEE_LOCAL:
  11671. {
  11672. p_org = p - 1;
  11673. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11674. #if WASM_ENABLE_FAST_INTERP != 0
  11675. /* If the stack is in polymorphic state, do fake pop and push on
  11676. offset stack to keep the depth of offset stack to be the
  11677. same with ref stack */
  11678. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11679. if (cur_block->is_stack_polymorphic) {
  11680. POP_OFFSET_TYPE(local_type);
  11681. PUSH_OFFSET_TYPE(local_type);
  11682. }
  11683. #endif
  11684. POP_TYPE(local_type);
  11685. PUSH_TYPE(local_type);
  11686. #if WASM_ENABLE_FAST_INTERP != 0
  11687. if (!(preserve_referenced_local(
  11688. loader_ctx, opcode, local_offset, local_type,
  11689. &preserve_local, error_buf, error_buf_size)))
  11690. goto fail;
  11691. if (local_offset < 256
  11692. #if WASM_ENABLE_GC != 0
  11693. && !wasm_is_type_reftype(local_type)
  11694. #endif
  11695. ) {
  11696. skip_label();
  11697. if (is_32bit_type(local_type)) {
  11698. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11699. emit_byte(loader_ctx, (uint8)local_offset);
  11700. }
  11701. else {
  11702. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11703. emit_byte(loader_ctx, (uint8)local_offset);
  11704. }
  11705. }
  11706. else { /* local index larger than 255, reserve leb */
  11707. emit_uint32(loader_ctx, local_idx);
  11708. }
  11709. emit_operand(loader_ctx,
  11710. *(loader_ctx->frame_offset
  11711. - wasm_value_type_cell_num(local_type)));
  11712. #else
  11713. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11714. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11715. if (local_offset < 0x80
  11716. #if WASM_ENABLE_GC != 0
  11717. && !wasm_is_type_reftype(local_type)
  11718. #endif
  11719. ) {
  11720. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11721. if (is_32bit_type(local_type)) {
  11722. *p_org++ = (uint8)local_offset;
  11723. }
  11724. else {
  11725. *p_org++ = (uint8)(local_offset | 0x80);
  11726. }
  11727. while (p_org < p) {
  11728. *p_org++ = WASM_OP_NOP;
  11729. }
  11730. }
  11731. #endif
  11732. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11733. #if WASM_ENABLE_GC != 0
  11734. if (local_idx >= param_count) {
  11735. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11736. }
  11737. #endif
  11738. break;
  11739. }
  11740. case WASM_OP_GET_GLOBAL:
  11741. {
  11742. #if WASM_ENABLE_GC != 0
  11743. WASMRefType *ref_type;
  11744. #endif
  11745. p_org = p - 1;
  11746. read_leb_uint32(p, p_end, global_idx);
  11747. if (global_idx >= global_count) {
  11748. set_error_buf(error_buf, error_buf_size, "unknown global");
  11749. goto fail;
  11750. }
  11751. global_type = global_idx < module->import_global_count
  11752. ? module->import_globals[global_idx]
  11753. .u.global.type.val_type
  11754. : module
  11755. ->globals[global_idx
  11756. - module->import_global_count]
  11757. .type.val_type;
  11758. #if WASM_ENABLE_GC != 0
  11759. ref_type =
  11760. global_idx < module->import_global_count
  11761. ? module->import_globals[global_idx].u.global.ref_type
  11762. : module
  11763. ->globals[global_idx
  11764. - module->import_global_count]
  11765. .ref_type;
  11766. if (wasm_is_type_multi_byte_type(global_type)) {
  11767. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11768. wasm_reftype_struct_size(ref_type));
  11769. }
  11770. #endif
  11771. PUSH_TYPE(global_type);
  11772. #if WASM_ENABLE_FAST_INTERP == 0
  11773. if (global_type == VALUE_TYPE_I64
  11774. || global_type == VALUE_TYPE_F64) {
  11775. #if WASM_ENABLE_DEBUG_INTERP != 0
  11776. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11777. error_buf_size)) {
  11778. goto fail;
  11779. }
  11780. #endif
  11781. *p_org = WASM_OP_GET_GLOBAL_64;
  11782. }
  11783. #else /* else of WASM_ENABLE_FAST_INTERP */
  11784. if (global_type == VALUE_TYPE_I64
  11785. || global_type == VALUE_TYPE_F64) {
  11786. skip_label();
  11787. emit_label(WASM_OP_GET_GLOBAL_64);
  11788. }
  11789. emit_uint32(loader_ctx, global_idx);
  11790. PUSH_OFFSET_TYPE(global_type);
  11791. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11792. break;
  11793. }
  11794. case WASM_OP_SET_GLOBAL:
  11795. {
  11796. bool is_mutable = false;
  11797. #if WASM_ENABLE_GC != 0
  11798. WASMRefType *ref_type;
  11799. #endif
  11800. p_org = p - 1;
  11801. read_leb_uint32(p, p_end, global_idx);
  11802. if (global_idx >= global_count) {
  11803. set_error_buf(error_buf, error_buf_size, "unknown global");
  11804. goto fail;
  11805. }
  11806. is_mutable = global_idx < module->import_global_count
  11807. ? module->import_globals[global_idx]
  11808. .u.global.type.is_mutable
  11809. : module
  11810. ->globals[global_idx
  11811. - module->import_global_count]
  11812. .type.is_mutable;
  11813. if (!is_mutable) {
  11814. #if WASM_ENABLE_GC == 0
  11815. set_error_buf(error_buf, error_buf_size,
  11816. "global is immutable");
  11817. #else
  11818. set_error_buf(error_buf, error_buf_size,
  11819. "immutable global");
  11820. #endif
  11821. goto fail;
  11822. }
  11823. global_type = global_idx < module->import_global_count
  11824. ? module->import_globals[global_idx]
  11825. .u.global.type.val_type
  11826. : module
  11827. ->globals[global_idx
  11828. - module->import_global_count]
  11829. .type.val_type;
  11830. #if WASM_ENABLE_GC != 0
  11831. ref_type =
  11832. global_idx < module->import_global_count
  11833. ? module->import_globals[global_idx].u.global.ref_type
  11834. : module
  11835. ->globals[global_idx
  11836. - module->import_global_count]
  11837. .ref_type;
  11838. if (wasm_is_type_multi_byte_type(global_type)) {
  11839. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11840. wasm_reftype_struct_size(ref_type));
  11841. }
  11842. #endif
  11843. #if WASM_ENABLE_FAST_INTERP == 0
  11844. if (global_type == VALUE_TYPE_I64
  11845. || global_type == VALUE_TYPE_F64) {
  11846. #if WASM_ENABLE_DEBUG_INTERP != 0
  11847. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11848. error_buf_size)) {
  11849. goto fail;
  11850. }
  11851. #endif
  11852. *p_org = WASM_OP_SET_GLOBAL_64;
  11853. }
  11854. else if (module->aux_stack_size > 0
  11855. && global_idx == module->aux_stack_top_global_index) {
  11856. #if WASM_ENABLE_DEBUG_INTERP != 0
  11857. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11858. error_buf_size)) {
  11859. goto fail;
  11860. }
  11861. #endif
  11862. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11863. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11864. func->has_op_set_global_aux_stack = true;
  11865. #endif
  11866. }
  11867. #else /* else of WASM_ENABLE_FAST_INTERP */
  11868. if (global_type == VALUE_TYPE_I64
  11869. || global_type == VALUE_TYPE_F64) {
  11870. skip_label();
  11871. emit_label(WASM_OP_SET_GLOBAL_64);
  11872. }
  11873. else if (module->aux_stack_size > 0
  11874. && global_idx == module->aux_stack_top_global_index) {
  11875. skip_label();
  11876. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11877. }
  11878. emit_uint32(loader_ctx, global_idx);
  11879. POP_OFFSET_TYPE(global_type);
  11880. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11881. POP_TYPE(global_type);
  11882. break;
  11883. }
  11884. /* load */
  11885. case WASM_OP_I32_LOAD:
  11886. case WASM_OP_I32_LOAD8_S:
  11887. case WASM_OP_I32_LOAD8_U:
  11888. case WASM_OP_I32_LOAD16_S:
  11889. case WASM_OP_I32_LOAD16_U:
  11890. case WASM_OP_I64_LOAD:
  11891. case WASM_OP_I64_LOAD8_S:
  11892. case WASM_OP_I64_LOAD8_U:
  11893. case WASM_OP_I64_LOAD16_S:
  11894. case WASM_OP_I64_LOAD16_U:
  11895. case WASM_OP_I64_LOAD32_S:
  11896. case WASM_OP_I64_LOAD32_U:
  11897. case WASM_OP_F32_LOAD:
  11898. case WASM_OP_F64_LOAD:
  11899. /* store */
  11900. case WASM_OP_I32_STORE:
  11901. case WASM_OP_I32_STORE8:
  11902. case WASM_OP_I32_STORE16:
  11903. case WASM_OP_I64_STORE:
  11904. case WASM_OP_I64_STORE8:
  11905. case WASM_OP_I64_STORE16:
  11906. case WASM_OP_I64_STORE32:
  11907. case WASM_OP_F32_STORE:
  11908. case WASM_OP_F64_STORE:
  11909. {
  11910. #if WASM_ENABLE_FAST_INTERP != 0
  11911. /* change F32/F64 into I32/I64 */
  11912. if (opcode == WASM_OP_F32_LOAD) {
  11913. skip_label();
  11914. emit_label(WASM_OP_I32_LOAD);
  11915. }
  11916. else if (opcode == WASM_OP_F64_LOAD) {
  11917. skip_label();
  11918. emit_label(WASM_OP_I64_LOAD);
  11919. }
  11920. else if (opcode == WASM_OP_F32_STORE) {
  11921. skip_label();
  11922. emit_label(WASM_OP_I32_STORE);
  11923. }
  11924. else if (opcode == WASM_OP_F64_STORE) {
  11925. skip_label();
  11926. emit_label(WASM_OP_I64_STORE);
  11927. }
  11928. #endif
  11929. CHECK_MEMORY();
  11930. read_leb_memarg(p, p_end, align); /* align */
  11931. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11932. if (!check_memory_access_align(opcode, align, error_buf,
  11933. error_buf_size)) {
  11934. goto fail;
  11935. }
  11936. #if WASM_ENABLE_FAST_INTERP != 0
  11937. emit_uint32(loader_ctx, mem_offset);
  11938. #endif
  11939. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11940. func->has_memory_operations = true;
  11941. #endif
  11942. switch (opcode) {
  11943. /* load */
  11944. case WASM_OP_I32_LOAD:
  11945. case WASM_OP_I32_LOAD8_S:
  11946. case WASM_OP_I32_LOAD8_U:
  11947. case WASM_OP_I32_LOAD16_S:
  11948. case WASM_OP_I32_LOAD16_U:
  11949. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11950. break;
  11951. case WASM_OP_I64_LOAD:
  11952. case WASM_OP_I64_LOAD8_S:
  11953. case WASM_OP_I64_LOAD8_U:
  11954. case WASM_OP_I64_LOAD16_S:
  11955. case WASM_OP_I64_LOAD16_U:
  11956. case WASM_OP_I64_LOAD32_S:
  11957. case WASM_OP_I64_LOAD32_U:
  11958. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11959. break;
  11960. case WASM_OP_F32_LOAD:
  11961. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11962. break;
  11963. case WASM_OP_F64_LOAD:
  11964. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11965. break;
  11966. /* store */
  11967. case WASM_OP_I32_STORE:
  11968. case WASM_OP_I32_STORE8:
  11969. case WASM_OP_I32_STORE16:
  11970. POP_I32();
  11971. POP_MEM_OFFSET();
  11972. break;
  11973. case WASM_OP_I64_STORE:
  11974. case WASM_OP_I64_STORE8:
  11975. case WASM_OP_I64_STORE16:
  11976. case WASM_OP_I64_STORE32:
  11977. POP_I64();
  11978. POP_MEM_OFFSET();
  11979. break;
  11980. case WASM_OP_F32_STORE:
  11981. POP_F32();
  11982. POP_MEM_OFFSET();
  11983. break;
  11984. case WASM_OP_F64_STORE:
  11985. POP_F64();
  11986. POP_MEM_OFFSET();
  11987. break;
  11988. default:
  11989. break;
  11990. }
  11991. break;
  11992. }
  11993. case WASM_OP_MEMORY_SIZE:
  11994. CHECK_MEMORY();
  11995. read_leb_uint32(p, p_end, memidx);
  11996. check_memidx(module, memidx);
  11997. PUSH_PAGE_COUNT();
  11998. module->possible_memory_grow = true;
  11999. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12000. func->has_memory_operations = true;
  12001. #endif
  12002. break;
  12003. case WASM_OP_MEMORY_GROW:
  12004. CHECK_MEMORY();
  12005. read_leb_uint32(p, p_end, memidx);
  12006. check_memidx(module, memidx);
  12007. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12008. module->possible_memory_grow = true;
  12009. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12010. || WASM_ENABLE_WAMR_COMPILER != 0
  12011. func->has_op_memory_grow = true;
  12012. #endif
  12013. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12014. func->has_memory_operations = true;
  12015. #endif
  12016. break;
  12017. case WASM_OP_I32_CONST:
  12018. read_leb_int32(p, p_end, i32_const);
  12019. #if WASM_ENABLE_FAST_INTERP != 0
  12020. skip_label();
  12021. disable_emit = true;
  12022. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12023. if (operand_offset == 0) {
  12024. disable_emit = false;
  12025. emit_label(WASM_OP_I32_CONST);
  12026. emit_uint32(loader_ctx, i32_const);
  12027. }
  12028. #else
  12029. (void)i32_const;
  12030. #endif
  12031. PUSH_I32();
  12032. break;
  12033. case WASM_OP_I64_CONST:
  12034. read_leb_int64(p, p_end, i64_const);
  12035. #if WASM_ENABLE_FAST_INTERP != 0
  12036. skip_label();
  12037. disable_emit = true;
  12038. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12039. if (operand_offset == 0) {
  12040. disable_emit = false;
  12041. emit_label(WASM_OP_I64_CONST);
  12042. emit_uint64(loader_ctx, i64_const);
  12043. }
  12044. #endif
  12045. PUSH_I64();
  12046. break;
  12047. case WASM_OP_F32_CONST:
  12048. CHECK_BUF(p, p_end, sizeof(float32));
  12049. p += sizeof(float32);
  12050. #if WASM_ENABLE_FAST_INTERP != 0
  12051. skip_label();
  12052. disable_emit = true;
  12053. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12054. sizeof(float32));
  12055. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12056. if (operand_offset == 0) {
  12057. disable_emit = false;
  12058. emit_label(WASM_OP_F32_CONST);
  12059. emit_float32(loader_ctx, f32_const);
  12060. }
  12061. #endif
  12062. PUSH_F32();
  12063. break;
  12064. case WASM_OP_F64_CONST:
  12065. CHECK_BUF(p, p_end, sizeof(float64));
  12066. p += sizeof(float64);
  12067. #if WASM_ENABLE_FAST_INTERP != 0
  12068. skip_label();
  12069. disable_emit = true;
  12070. /* Some MCU may require 8-byte align */
  12071. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12072. sizeof(float64));
  12073. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12074. if (operand_offset == 0) {
  12075. disable_emit = false;
  12076. emit_label(WASM_OP_F64_CONST);
  12077. emit_float64(loader_ctx, f64_const);
  12078. }
  12079. #endif
  12080. PUSH_F64();
  12081. break;
  12082. case WASM_OP_I32_EQZ:
  12083. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12084. break;
  12085. case WASM_OP_I32_EQ:
  12086. case WASM_OP_I32_NE:
  12087. case WASM_OP_I32_LT_S:
  12088. case WASM_OP_I32_LT_U:
  12089. case WASM_OP_I32_GT_S:
  12090. case WASM_OP_I32_GT_U:
  12091. case WASM_OP_I32_LE_S:
  12092. case WASM_OP_I32_LE_U:
  12093. case WASM_OP_I32_GE_S:
  12094. case WASM_OP_I32_GE_U:
  12095. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12096. break;
  12097. case WASM_OP_I64_EQZ:
  12098. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12099. break;
  12100. case WASM_OP_I64_EQ:
  12101. case WASM_OP_I64_NE:
  12102. case WASM_OP_I64_LT_S:
  12103. case WASM_OP_I64_LT_U:
  12104. case WASM_OP_I64_GT_S:
  12105. case WASM_OP_I64_GT_U:
  12106. case WASM_OP_I64_LE_S:
  12107. case WASM_OP_I64_LE_U:
  12108. case WASM_OP_I64_GE_S:
  12109. case WASM_OP_I64_GE_U:
  12110. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12111. break;
  12112. case WASM_OP_F32_EQ:
  12113. case WASM_OP_F32_NE:
  12114. case WASM_OP_F32_LT:
  12115. case WASM_OP_F32_GT:
  12116. case WASM_OP_F32_LE:
  12117. case WASM_OP_F32_GE:
  12118. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12119. break;
  12120. case WASM_OP_F64_EQ:
  12121. case WASM_OP_F64_NE:
  12122. case WASM_OP_F64_LT:
  12123. case WASM_OP_F64_GT:
  12124. case WASM_OP_F64_LE:
  12125. case WASM_OP_F64_GE:
  12126. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12127. break;
  12128. case WASM_OP_I32_CLZ:
  12129. case WASM_OP_I32_CTZ:
  12130. case WASM_OP_I32_POPCNT:
  12131. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12132. break;
  12133. case WASM_OP_I32_ADD:
  12134. case WASM_OP_I32_SUB:
  12135. case WASM_OP_I32_MUL:
  12136. case WASM_OP_I32_DIV_S:
  12137. case WASM_OP_I32_DIV_U:
  12138. case WASM_OP_I32_REM_S:
  12139. case WASM_OP_I32_REM_U:
  12140. case WASM_OP_I32_AND:
  12141. case WASM_OP_I32_OR:
  12142. case WASM_OP_I32_XOR:
  12143. case WASM_OP_I32_SHL:
  12144. case WASM_OP_I32_SHR_S:
  12145. case WASM_OP_I32_SHR_U:
  12146. case WASM_OP_I32_ROTL:
  12147. case WASM_OP_I32_ROTR:
  12148. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12149. break;
  12150. case WASM_OP_I64_CLZ:
  12151. case WASM_OP_I64_CTZ:
  12152. case WASM_OP_I64_POPCNT:
  12153. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12154. break;
  12155. case WASM_OP_I64_ADD:
  12156. case WASM_OP_I64_SUB:
  12157. case WASM_OP_I64_MUL:
  12158. case WASM_OP_I64_DIV_S:
  12159. case WASM_OP_I64_DIV_U:
  12160. case WASM_OP_I64_REM_S:
  12161. case WASM_OP_I64_REM_U:
  12162. case WASM_OP_I64_AND:
  12163. case WASM_OP_I64_OR:
  12164. case WASM_OP_I64_XOR:
  12165. case WASM_OP_I64_SHL:
  12166. case WASM_OP_I64_SHR_S:
  12167. case WASM_OP_I64_SHR_U:
  12168. case WASM_OP_I64_ROTL:
  12169. case WASM_OP_I64_ROTR:
  12170. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12171. break;
  12172. case WASM_OP_F32_ABS:
  12173. case WASM_OP_F32_NEG:
  12174. case WASM_OP_F32_CEIL:
  12175. case WASM_OP_F32_FLOOR:
  12176. case WASM_OP_F32_TRUNC:
  12177. case WASM_OP_F32_NEAREST:
  12178. case WASM_OP_F32_SQRT:
  12179. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12180. break;
  12181. case WASM_OP_F32_ADD:
  12182. case WASM_OP_F32_SUB:
  12183. case WASM_OP_F32_MUL:
  12184. case WASM_OP_F32_DIV:
  12185. case WASM_OP_F32_MIN:
  12186. case WASM_OP_F32_MAX:
  12187. case WASM_OP_F32_COPYSIGN:
  12188. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12189. break;
  12190. case WASM_OP_F64_ABS:
  12191. case WASM_OP_F64_NEG:
  12192. case WASM_OP_F64_CEIL:
  12193. case WASM_OP_F64_FLOOR:
  12194. case WASM_OP_F64_TRUNC:
  12195. case WASM_OP_F64_NEAREST:
  12196. case WASM_OP_F64_SQRT:
  12197. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12198. break;
  12199. case WASM_OP_F64_ADD:
  12200. case WASM_OP_F64_SUB:
  12201. case WASM_OP_F64_MUL:
  12202. case WASM_OP_F64_DIV:
  12203. case WASM_OP_F64_MIN:
  12204. case WASM_OP_F64_MAX:
  12205. case WASM_OP_F64_COPYSIGN:
  12206. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12207. break;
  12208. case WASM_OP_I32_WRAP_I64:
  12209. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12210. break;
  12211. case WASM_OP_I32_TRUNC_S_F32:
  12212. case WASM_OP_I32_TRUNC_U_F32:
  12213. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12214. break;
  12215. case WASM_OP_I32_TRUNC_S_F64:
  12216. case WASM_OP_I32_TRUNC_U_F64:
  12217. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12218. break;
  12219. case WASM_OP_I64_EXTEND_S_I32:
  12220. case WASM_OP_I64_EXTEND_U_I32:
  12221. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12222. break;
  12223. case WASM_OP_I64_TRUNC_S_F32:
  12224. case WASM_OP_I64_TRUNC_U_F32:
  12225. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12226. break;
  12227. case WASM_OP_I64_TRUNC_S_F64:
  12228. case WASM_OP_I64_TRUNC_U_F64:
  12229. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12230. break;
  12231. case WASM_OP_F32_CONVERT_S_I32:
  12232. case WASM_OP_F32_CONVERT_U_I32:
  12233. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12234. break;
  12235. case WASM_OP_F32_CONVERT_S_I64:
  12236. case WASM_OP_F32_CONVERT_U_I64:
  12237. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12238. break;
  12239. case WASM_OP_F32_DEMOTE_F64:
  12240. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12241. break;
  12242. case WASM_OP_F64_CONVERT_S_I32:
  12243. case WASM_OP_F64_CONVERT_U_I32:
  12244. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12245. break;
  12246. case WASM_OP_F64_CONVERT_S_I64:
  12247. case WASM_OP_F64_CONVERT_U_I64:
  12248. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12249. break;
  12250. case WASM_OP_F64_PROMOTE_F32:
  12251. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12252. break;
  12253. case WASM_OP_I32_REINTERPRET_F32:
  12254. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12255. break;
  12256. case WASM_OP_I64_REINTERPRET_F64:
  12257. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12258. break;
  12259. case WASM_OP_F32_REINTERPRET_I32:
  12260. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12261. break;
  12262. case WASM_OP_F64_REINTERPRET_I64:
  12263. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12264. break;
  12265. case WASM_OP_I32_EXTEND8_S:
  12266. case WASM_OP_I32_EXTEND16_S:
  12267. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12268. break;
  12269. case WASM_OP_I64_EXTEND8_S:
  12270. case WASM_OP_I64_EXTEND16_S:
  12271. case WASM_OP_I64_EXTEND32_S:
  12272. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12273. break;
  12274. #if WASM_ENABLE_GC != 0
  12275. case WASM_OP_GC_PREFIX:
  12276. {
  12277. uint32 opcode1;
  12278. read_leb_uint32(p, p_end, opcode1);
  12279. #if WASM_ENABLE_FAST_INTERP != 0
  12280. emit_byte(loader_ctx, ((uint8)opcode1));
  12281. #endif
  12282. switch (opcode1) {
  12283. case WASM_OP_STRUCT_NEW:
  12284. case WASM_OP_STRUCT_NEW_DEFAULT:
  12285. {
  12286. read_leb_uint32(p, p_end, type_idx);
  12287. #if WASM_ENABLE_FAST_INTERP != 0
  12288. emit_uint32(loader_ctx, type_idx);
  12289. #endif
  12290. if (!check_type_index(module, module->type_count,
  12291. type_idx, error_buf,
  12292. error_buf_size)) {
  12293. goto fail;
  12294. }
  12295. if (module->types[type_idx]->type_flag
  12296. != WASM_TYPE_STRUCT) {
  12297. set_error_buf(error_buf, error_buf_size,
  12298. "unkown struct type");
  12299. goto fail;
  12300. }
  12301. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12302. int32 j, k;
  12303. uint8 value_type;
  12304. uint32 ref_type_struct_size;
  12305. WASMStructType *struct_type =
  12306. (WASMStructType *)module->types[type_idx];
  12307. k = struct_type->ref_type_map_count - 1;
  12308. for (j = struct_type->field_count - 1; j >= 0;
  12309. j--) {
  12310. value_type = struct_type->fields[j].field_type;
  12311. if (wasm_is_type_reftype(value_type)) {
  12312. if (wasm_is_type_multi_byte_type(
  12313. value_type)) {
  12314. ref_type_struct_size =
  12315. wasm_reftype_struct_size(
  12316. struct_type->ref_type_maps[k]
  12317. .ref_type);
  12318. bh_memcpy_s(
  12319. &wasm_ref_type,
  12320. (uint32)sizeof(WASMRefType),
  12321. struct_type->ref_type_maps[k]
  12322. .ref_type,
  12323. ref_type_struct_size);
  12324. k--;
  12325. }
  12326. POP_REF(value_type);
  12327. }
  12328. else {
  12329. switch (value_type) {
  12330. case VALUE_TYPE_I32:
  12331. case PACKED_TYPE_I8:
  12332. case PACKED_TYPE_I16:
  12333. POP_I32();
  12334. break;
  12335. case VALUE_TYPE_I64:
  12336. POP_I64();
  12337. break;
  12338. case VALUE_TYPE_F32:
  12339. POP_F32();
  12340. break;
  12341. case VALUE_TYPE_F64:
  12342. POP_F64();
  12343. break;
  12344. default:
  12345. set_error_buf(error_buf,
  12346. error_buf_size,
  12347. "unknown type");
  12348. goto fail;
  12349. }
  12350. }
  12351. }
  12352. }
  12353. /* PUSH struct obj, (ref $t) */
  12354. wasm_set_refheaptype_typeidx(
  12355. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12356. PUSH_REF(wasm_ref_type.ref_type);
  12357. break;
  12358. }
  12359. case WASM_OP_STRUCT_GET:
  12360. case WASM_OP_STRUCT_GET_S:
  12361. case WASM_OP_STRUCT_GET_U:
  12362. case WASM_OP_STRUCT_SET:
  12363. {
  12364. WASMStructType *struct_type;
  12365. WASMRefType *ref_type = NULL;
  12366. uint32 field_idx;
  12367. uint8 field_type;
  12368. read_leb_uint32(p, p_end, type_idx);
  12369. #if WASM_ENABLE_FAST_INTERP != 0
  12370. emit_uint32(loader_ctx, type_idx);
  12371. #endif
  12372. if (!check_type_index(module, module->type_count,
  12373. type_idx, error_buf,
  12374. error_buf_size)) {
  12375. goto fail;
  12376. }
  12377. if (module->types[type_idx]->type_flag
  12378. != WASM_TYPE_STRUCT) {
  12379. set_error_buf(error_buf, error_buf_size,
  12380. "unknown struct type");
  12381. goto fail;
  12382. }
  12383. struct_type = (WASMStructType *)module->types[type_idx];
  12384. read_leb_uint32(p, p_end, field_idx);
  12385. #if WASM_ENABLE_FAST_INTERP != 0
  12386. emit_uint32(loader_ctx, field_idx);
  12387. #endif
  12388. if (field_idx >= struct_type->field_count) {
  12389. set_error_buf(error_buf, error_buf_size,
  12390. "unknown struct field");
  12391. goto fail;
  12392. }
  12393. if (opcode1 == WASM_OP_STRUCT_SET
  12394. && !(struct_type->fields[field_idx].field_flags
  12395. & 1)) {
  12396. set_error_buf(error_buf, error_buf_size,
  12397. "field is immutable");
  12398. goto fail;
  12399. }
  12400. field_type = struct_type->fields[field_idx].field_type;
  12401. if (is_packed_type(field_type)) {
  12402. if (opcode1 == WASM_OP_STRUCT_GET) {
  12403. set_error_buf(error_buf, error_buf_size,
  12404. "type mismatch");
  12405. goto fail;
  12406. }
  12407. else {
  12408. field_type = VALUE_TYPE_I32;
  12409. }
  12410. }
  12411. if (wasm_is_type_multi_byte_type(field_type)) {
  12412. ref_type = wasm_reftype_map_find(
  12413. struct_type->ref_type_maps,
  12414. struct_type->ref_type_map_count, field_idx);
  12415. bh_assert(ref_type);
  12416. }
  12417. if (opcode1 == WASM_OP_STRUCT_SET) {
  12418. /* POP field */
  12419. if (wasm_is_type_multi_byte_type(field_type)) {
  12420. bh_memcpy_s(&wasm_ref_type,
  12421. (uint32)sizeof(WASMRefType),
  12422. ref_type,
  12423. wasm_reftype_struct_size(ref_type));
  12424. }
  12425. POP_REF(field_type);
  12426. /* POP struct obj, (ref null $t) */
  12427. wasm_set_refheaptype_typeidx(
  12428. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12429. POP_REF(wasm_ref_type.ref_type);
  12430. }
  12431. else {
  12432. /* POP struct obj, (ref null $t) */
  12433. wasm_set_refheaptype_typeidx(
  12434. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12435. POP_REF(wasm_ref_type.ref_type);
  12436. /* PUSH field */
  12437. if (wasm_is_type_multi_byte_type(field_type)) {
  12438. bh_memcpy_s(&wasm_ref_type,
  12439. (uint32)sizeof(WASMRefType),
  12440. ref_type,
  12441. wasm_reftype_struct_size(ref_type));
  12442. }
  12443. PUSH_REF(field_type);
  12444. }
  12445. break;
  12446. }
  12447. case WASM_OP_ARRAY_NEW:
  12448. case WASM_OP_ARRAY_NEW_DEFAULT:
  12449. case WASM_OP_ARRAY_NEW_FIXED:
  12450. case WASM_OP_ARRAY_NEW_DATA:
  12451. case WASM_OP_ARRAY_NEW_ELEM:
  12452. {
  12453. WASMArrayType *array_type;
  12454. uint8 elem_type;
  12455. uint32 u32 = 0;
  12456. read_leb_uint32(p, p_end, type_idx);
  12457. #if WASM_ENABLE_FAST_INTERP != 0
  12458. emit_uint32(loader_ctx, type_idx);
  12459. #endif
  12460. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12461. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12462. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12463. read_leb_uint32(p, p_end, u32);
  12464. #if WASM_ENABLE_FAST_INTERP != 0
  12465. emit_uint32(loader_ctx, u32);
  12466. #endif
  12467. }
  12468. if (!check_array_type(module, type_idx, error_buf,
  12469. error_buf_size)) {
  12470. goto fail;
  12471. }
  12472. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12473. /* length */
  12474. POP_I32();
  12475. }
  12476. array_type = (WASMArrayType *)module->types[type_idx];
  12477. elem_type = array_type->elem_type;
  12478. if (opcode1 == WASM_OP_ARRAY_NEW
  12479. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12480. if (wasm_is_type_multi_byte_type(elem_type)) {
  12481. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12482. array_type->elem_ref_type,
  12483. wasm_reftype_struct_size(
  12484. array_type->elem_ref_type));
  12485. }
  12486. if (is_packed_type(elem_type)) {
  12487. elem_type = VALUE_TYPE_I32;
  12488. }
  12489. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12490. uint32 N = u32;
  12491. for (i = 0; i < N; i++) {
  12492. if (wasm_is_type_multi_byte_type(
  12493. elem_type)) {
  12494. bh_memcpy_s(
  12495. &wasm_ref_type, sizeof(WASMRefType),
  12496. array_type->elem_ref_type,
  12497. wasm_reftype_struct_size(
  12498. array_type->elem_ref_type));
  12499. }
  12500. POP_REF(elem_type);
  12501. }
  12502. }
  12503. else
  12504. POP_REF(elem_type);
  12505. }
  12506. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12507. /* offset of data segment */
  12508. POP_I32();
  12509. if (u32 >= module->data_seg_count) {
  12510. set_error_buf(error_buf, error_buf_size,
  12511. "unknown data segment");
  12512. goto fail;
  12513. }
  12514. if (wasm_is_type_reftype(elem_type)) {
  12515. set_error_buf(error_buf, error_buf_size,
  12516. "array elem type mismatch");
  12517. goto fail;
  12518. }
  12519. }
  12520. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12521. WASMTableSeg *table_seg =
  12522. module->table_segments + u32;
  12523. /* offset of element segment */
  12524. POP_I32();
  12525. if (u32 >= module->table_seg_count) {
  12526. set_error_buf(error_buf, error_buf_size,
  12527. "unknown element segment");
  12528. goto fail;
  12529. }
  12530. if (!wasm_reftype_is_subtype_of(
  12531. table_seg->elem_type,
  12532. table_seg->elem_ref_type, elem_type,
  12533. array_type->elem_ref_type, module->types,
  12534. module->type_count)) {
  12535. set_error_buf(error_buf, error_buf_size,
  12536. "array elem type mismatch");
  12537. goto fail;
  12538. }
  12539. }
  12540. /* PUSH array obj, (ref $t) */
  12541. wasm_set_refheaptype_typeidx(
  12542. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12543. PUSH_REF(wasm_ref_type.ref_type);
  12544. break;
  12545. }
  12546. case WASM_OP_ARRAY_GET:
  12547. case WASM_OP_ARRAY_GET_S:
  12548. case WASM_OP_ARRAY_GET_U:
  12549. case WASM_OP_ARRAY_SET:
  12550. {
  12551. uint8 elem_type;
  12552. WASMArrayType *array_type;
  12553. WASMRefType *ref_type = NULL;
  12554. read_leb_uint32(p, p_end, type_idx);
  12555. #if WASM_ENABLE_FAST_INTERP != 0
  12556. emit_uint32(loader_ctx, type_idx);
  12557. #endif
  12558. if (!check_array_type(module, type_idx, error_buf,
  12559. error_buf_size)) {
  12560. goto fail;
  12561. }
  12562. array_type = (WASMArrayType *)module->types[type_idx];
  12563. if (opcode1 == WASM_OP_ARRAY_SET
  12564. && !(array_type->elem_flags & 1)) {
  12565. set_error_buf(error_buf, error_buf_size,
  12566. "array is immutable");
  12567. goto fail;
  12568. }
  12569. elem_type = array_type->elem_type;
  12570. if (is_packed_type(elem_type)) {
  12571. if (opcode1 != WASM_OP_ARRAY_GET_S
  12572. && opcode1 != WASM_OP_ARRAY_GET_U
  12573. && opcode1 != WASM_OP_ARRAY_SET) {
  12574. set_error_buf(error_buf, error_buf_size,
  12575. "type mismatch");
  12576. goto fail;
  12577. }
  12578. else {
  12579. elem_type = VALUE_TYPE_I32;
  12580. }
  12581. }
  12582. ref_type = array_type->elem_ref_type;
  12583. if (opcode1 == WASM_OP_ARRAY_SET) {
  12584. /* POP elem to set */
  12585. if (wasm_is_type_multi_byte_type(elem_type)) {
  12586. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12587. ref_type,
  12588. wasm_reftype_struct_size(ref_type));
  12589. }
  12590. POP_REF(elem_type);
  12591. }
  12592. /* elem idx */
  12593. POP_I32();
  12594. /* POP array obj, (ref null $t) */
  12595. wasm_set_refheaptype_typeidx(
  12596. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12597. POP_REF(wasm_ref_type.ref_type);
  12598. if (opcode1 != WASM_OP_ARRAY_SET) {
  12599. /* PUSH elem */
  12600. if (wasm_is_type_multi_byte_type(elem_type)) {
  12601. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12602. ref_type,
  12603. wasm_reftype_struct_size(ref_type));
  12604. }
  12605. PUSH_REF(elem_type);
  12606. }
  12607. break;
  12608. }
  12609. case WASM_OP_ARRAY_LEN:
  12610. {
  12611. POP_REF(REF_TYPE_ARRAYREF);
  12612. /* length */
  12613. PUSH_I32();
  12614. break;
  12615. }
  12616. case WASM_OP_ARRAY_FILL:
  12617. {
  12618. WASMArrayType *array_type;
  12619. uint8 elem_type;
  12620. /* typeidx */
  12621. read_leb_uint32(p, p_end, type_idx);
  12622. #if WASM_ENABLE_FAST_INTERP != 0
  12623. emit_uint32(loader_ctx, type_idx);
  12624. #endif
  12625. if (!check_array_type(module, type_idx, error_buf,
  12626. error_buf_size)) {
  12627. goto fail;
  12628. }
  12629. array_type = (WASMArrayType *)module->types[type_idx];
  12630. if (!(array_type->elem_flags & 1)) {
  12631. set_error_buf(error_buf, error_buf_size,
  12632. "array is immutable");
  12633. goto fail;
  12634. }
  12635. elem_type = array_type->elem_type;
  12636. if (is_packed_type(elem_type)) {
  12637. elem_type = VALUE_TYPE_I32;
  12638. }
  12639. POP_I32(); /* length */
  12640. #if WASM_ENABLE_FAST_INTERP != 0
  12641. POP_OFFSET_TYPE(elem_type);
  12642. #endif
  12643. POP_TYPE(elem_type);
  12644. POP_I32(); /* start */
  12645. /* POP array obj, (ref null $t) */
  12646. wasm_set_refheaptype_typeidx(
  12647. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12648. POP_REF(wasm_ref_type.ref_type);
  12649. break;
  12650. }
  12651. case WASM_OP_ARRAY_COPY:
  12652. {
  12653. uint32 src_type_idx;
  12654. uint8 src_elem_type, dst_elem_type;
  12655. WASMRefType src_ref_type = { 0 },
  12656. *src_elem_ref_type = NULL;
  12657. WASMRefType dst_ref_type = { 0 },
  12658. *dst_elem_ref_type = NULL;
  12659. WASMArrayType *array_type;
  12660. /* typeidx1 */
  12661. read_leb_uint32(p, p_end, type_idx);
  12662. #if WASM_ENABLE_FAST_INTERP != 0
  12663. emit_uint32(loader_ctx, type_idx);
  12664. #endif
  12665. /* typeidx2 */
  12666. read_leb_uint32(p, p_end, src_type_idx);
  12667. #if WASM_ENABLE_FAST_INTERP != 0
  12668. emit_uint32(loader_ctx, src_type_idx);
  12669. #endif
  12670. if (!check_array_type(module, type_idx, error_buf,
  12671. error_buf_size)) {
  12672. goto fail;
  12673. }
  12674. if (!check_array_type(module, src_type_idx, error_buf,
  12675. error_buf_size)) {
  12676. goto fail;
  12677. }
  12678. POP_I32();
  12679. POP_I32();
  12680. /* POP array obj, (ref null $t) */
  12681. wasm_set_refheaptype_typeidx(
  12682. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12683. POP_REF(wasm_ref_type.ref_type);
  12684. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12685. &wasm_ref_type,
  12686. wasm_reftype_struct_size(&wasm_ref_type));
  12687. POP_I32();
  12688. /* POP array obj, (ref null $t) */
  12689. wasm_set_refheaptype_typeidx(
  12690. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12691. POP_REF(wasm_ref_type.ref_type);
  12692. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12693. &wasm_ref_type,
  12694. wasm_reftype_struct_size(&wasm_ref_type));
  12695. array_type = (WASMArrayType *)module->types[type_idx];
  12696. if (!(array_type->elem_flags & 1)) {
  12697. set_error_buf(error_buf, error_buf_size,
  12698. "destination array is immutable");
  12699. goto fail;
  12700. }
  12701. dst_elem_type = array_type->elem_type;
  12702. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12703. dst_elem_ref_type = array_type->elem_ref_type;
  12704. }
  12705. array_type =
  12706. (WASMArrayType *)module->types[src_type_idx];
  12707. src_elem_type = array_type->elem_type;
  12708. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12709. src_elem_ref_type = array_type->elem_ref_type;
  12710. }
  12711. if (!wasm_reftype_is_subtype_of(
  12712. src_elem_type, src_elem_ref_type, dst_elem_type,
  12713. dst_elem_ref_type, module->types,
  12714. module->type_count)) {
  12715. set_error_buf(error_buf, error_buf_size,
  12716. "array types do not match");
  12717. goto fail;
  12718. }
  12719. break;
  12720. }
  12721. case WASM_OP_REF_I31:
  12722. {
  12723. POP_I32();
  12724. wasm_set_refheaptype_common(
  12725. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12726. PUSH_REF(wasm_ref_type.ref_type);
  12727. break;
  12728. }
  12729. case WASM_OP_I31_GET_S:
  12730. case WASM_OP_I31_GET_U:
  12731. {
  12732. POP_REF(REF_TYPE_I31REF);
  12733. PUSH_I32();
  12734. break;
  12735. }
  12736. case WASM_OP_REF_TEST:
  12737. case WASM_OP_REF_CAST:
  12738. case WASM_OP_REF_TEST_NULLABLE:
  12739. case WASM_OP_REF_CAST_NULLABLE:
  12740. {
  12741. uint8 type;
  12742. read_leb_int32(p, p_end, heap_type);
  12743. #if WASM_ENABLE_FAST_INTERP != 0
  12744. emit_uint32(loader_ctx, (uint32)heap_type);
  12745. #endif
  12746. if (heap_type >= 0) {
  12747. if (!check_type_index(module, module->type_count,
  12748. heap_type, error_buf,
  12749. error_buf_size)) {
  12750. goto fail;
  12751. }
  12752. }
  12753. else {
  12754. if (!wasm_is_valid_heap_type(heap_type)) {
  12755. set_error_buf(error_buf, error_buf_size,
  12756. "unknown type");
  12757. goto fail;
  12758. }
  12759. }
  12760. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12761. &wasm_ref_type, error_buf,
  12762. error_buf_size)) {
  12763. goto fail;
  12764. }
  12765. if (opcode1 == WASM_OP_REF_TEST
  12766. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12767. PUSH_I32();
  12768. else {
  12769. bool nullable =
  12770. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12771. : false;
  12772. if (heap_type >= 0 || !nullable) {
  12773. wasm_set_refheaptype_typeidx(
  12774. &wasm_ref_type.ref_ht_typeidx, nullable,
  12775. heap_type);
  12776. PUSH_REF(wasm_ref_type.ref_type);
  12777. }
  12778. else {
  12779. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12780. }
  12781. }
  12782. break;
  12783. }
  12784. case WASM_OP_BR_ON_CAST:
  12785. case WASM_OP_BR_ON_CAST_FAIL:
  12786. {
  12787. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12788. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12789. uint8 type_tmp, castflags;
  12790. uint32 depth;
  12791. int32 heap_type_dst;
  12792. bool src_nullable, dst_nullable;
  12793. CHECK_BUF(p, p_end, 1);
  12794. castflags = read_uint8(p);
  12795. #if WASM_ENABLE_FAST_INTERP != 0
  12796. /* Emit heap_type firstly */
  12797. emit_byte(loader_ctx, castflags);
  12798. #endif
  12799. p_org = p;
  12800. read_leb_uint32(p, p_end, depth);
  12801. read_leb_int32(p, p_end, heap_type);
  12802. #if WASM_ENABLE_FAST_INTERP != 0
  12803. /* Emit heap_type firstly */
  12804. emit_uint32(loader_ctx, (uint32)heap_type);
  12805. #endif
  12806. read_leb_int32(p, p_end, heap_type_dst);
  12807. #if WASM_ENABLE_FAST_INTERP != 0
  12808. /* Emit heap_type firstly */
  12809. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12810. #endif
  12811. (void)depth;
  12812. /*
  12813. * castflags should be 0~3:
  12814. * 0: (non-null, non-null)
  12815. * 1: (null, non-null)
  12816. * 2: (non-null, null)
  12817. * 3: (null, null)
  12818. */
  12819. if (castflags > 3) {
  12820. set_error_buf(error_buf, error_buf_size,
  12821. "invalid castflags");
  12822. break;
  12823. }
  12824. src_nullable =
  12825. (castflags == 1) || (castflags == 3) ? true : false;
  12826. dst_nullable =
  12827. (castflags == 2) || (castflags == 3) ? true : false;
  12828. /* Pop and backup the stack top's ref type */
  12829. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12830. &ref_type_tmp, error_buf,
  12831. error_buf_size)) {
  12832. goto fail;
  12833. }
  12834. /* The reference type rt1 must be valid */
  12835. if (!init_ref_type(module, &ref_type1, src_nullable,
  12836. heap_type, error_buf,
  12837. error_buf_size)) {
  12838. goto fail;
  12839. }
  12840. /* The reference type rt2 must be valid. */
  12841. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12842. heap_type_dst, error_buf,
  12843. error_buf_size)) {
  12844. goto fail;
  12845. }
  12846. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12847. &ref_type2);
  12848. /* The reference type rt2 must match rt1. */
  12849. if (!wasm_reftype_is_subtype_of(
  12850. ref_type2.ref_type, &ref_type2,
  12851. ref_type1.ref_type, &ref_type1, module->types,
  12852. module->type_count)) {
  12853. set_error_buf(error_buf, error_buf_size,
  12854. "type mismatch");
  12855. goto fail;
  12856. }
  12857. p = p_org;
  12858. /* Push ref type casted for branch block check */
  12859. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12860. /* The reference type rt2 must match rt′. */
  12861. type_tmp = ref_type2.ref_type;
  12862. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12863. bh_memcpy_s(
  12864. &wasm_ref_type,
  12865. wasm_reftype_struct_size(&ref_type2),
  12866. &ref_type2,
  12867. wasm_reftype_struct_size(&ref_type2));
  12868. }
  12869. }
  12870. else {
  12871. /* The reference type rt′1 must match rt′. */
  12872. type_tmp = ref_type_diff.ref_type;
  12873. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12874. bh_memcpy_s(
  12875. &wasm_ref_type,
  12876. wasm_reftype_struct_size(&ref_type_diff),
  12877. &ref_type_diff,
  12878. wasm_reftype_struct_size(&ref_type_diff));
  12879. }
  12880. }
  12881. PUSH_REF(type_tmp);
  12882. if (!(frame_csp_tmp = check_branch_block(
  12883. loader_ctx, &p, p_end, opcode, error_buf,
  12884. error_buf_size))) {
  12885. goto fail;
  12886. }
  12887. /* Ignore heap_types */
  12888. skip_leb_uint32(p, p_end);
  12889. skip_leb_uint32(p, p_end);
  12890. /* Restore the original stack top's ref type */
  12891. POP_REF(type_tmp);
  12892. #if WASM_ENABLE_FAST_INTERP != 0
  12893. /* Erase the opnd offset emitted by POP_REF() */
  12894. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12895. #endif
  12896. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12897. type_tmp = ref_type_diff.ref_type;
  12898. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12899. bh_memcpy_s(
  12900. &wasm_ref_type,
  12901. wasm_reftype_struct_size(&ref_type_diff),
  12902. &ref_type_diff,
  12903. wasm_reftype_struct_size(&ref_type_diff));
  12904. }
  12905. }
  12906. else {
  12907. type_tmp = ref_type2.ref_type;
  12908. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12909. bh_memcpy_s(
  12910. &wasm_ref_type,
  12911. wasm_reftype_struct_size(&ref_type2),
  12912. &ref_type2,
  12913. wasm_reftype_struct_size(&ref_type2));
  12914. }
  12915. }
  12916. PUSH_REF(type_tmp);
  12917. #if WASM_ENABLE_FAST_INTERP != 0
  12918. /* Erase the opnd offset emitted by PUSH_REF() */
  12919. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12920. #endif
  12921. break;
  12922. }
  12923. case WASM_OP_ANY_CONVERT_EXTERN:
  12924. {
  12925. uint8 type;
  12926. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12927. &wasm_ref_type, error_buf,
  12928. error_buf_size)) {
  12929. goto fail;
  12930. }
  12931. if (!(type == REF_TYPE_EXTERNREF
  12932. || (type == REF_TYPE_HT_NON_NULLABLE
  12933. && wasm_ref_type.ref_ht_common.heap_type
  12934. == HEAP_TYPE_EXTERN)
  12935. || type == VALUE_TYPE_ANY)) {
  12936. set_error_buf(error_buf, error_buf_size,
  12937. "type mismatch");
  12938. goto fail;
  12939. }
  12940. if (type == REF_TYPE_EXTERNREF)
  12941. type = REF_TYPE_ANYREF;
  12942. else {
  12943. wasm_ref_type.ref_ht_common.heap_type =
  12944. HEAP_TYPE_ANY;
  12945. }
  12946. PUSH_REF(type);
  12947. break;
  12948. }
  12949. case WASM_OP_EXTERN_CONVERT_ANY:
  12950. {
  12951. uint8 type;
  12952. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12953. &wasm_ref_type, error_buf,
  12954. error_buf_size)) {
  12955. goto fail;
  12956. }
  12957. if (type == REF_TYPE_EXTERNREF
  12958. || ((type == REF_TYPE_HT_NULLABLE
  12959. || type == REF_TYPE_HT_NON_NULLABLE)
  12960. && wasm_ref_type.ref_ht_common.heap_type
  12961. == HEAP_TYPE_EXTERN)) {
  12962. set_error_buf(error_buf, error_buf_size,
  12963. "type mismatch");
  12964. goto fail;
  12965. }
  12966. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12967. /* push (ref null extern) */
  12968. type = REF_TYPE_EXTERNREF;
  12969. }
  12970. else {
  12971. /* push (ref extern) */
  12972. type = REF_TYPE_HT_NON_NULLABLE;
  12973. wasm_set_refheaptype_common(
  12974. &wasm_ref_type.ref_ht_common, false,
  12975. HEAP_TYPE_EXTERN);
  12976. }
  12977. PUSH_REF(type);
  12978. break;
  12979. }
  12980. #if WASM_ENABLE_STRINGREF != 0
  12981. case WASM_OP_STRING_NEW_UTF8:
  12982. case WASM_OP_STRING_NEW_WTF16:
  12983. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12984. case WASM_OP_STRING_NEW_WTF8:
  12985. {
  12986. uint32 memidx;
  12987. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12988. func->has_memory_operations = true;
  12989. #endif
  12990. read_leb_uint32(p, p_end, memidx);
  12991. #if WASM_ENABLE_FAST_INTERP != 0
  12992. emit_uint32(loader_ctx, (uint32)memidx);
  12993. #endif
  12994. POP_I32();
  12995. POP_I32();
  12996. PUSH_REF(REF_TYPE_STRINGREF);
  12997. (void)memidx;
  12998. break;
  12999. }
  13000. case WASM_OP_STRING_CONST:
  13001. {
  13002. uint32 contents;
  13003. read_leb_uint32(p, p_end, contents);
  13004. #if WASM_ENABLE_FAST_INTERP != 0
  13005. emit_uint32(loader_ctx, (uint32)contents);
  13006. #endif
  13007. PUSH_REF(REF_TYPE_STRINGREF);
  13008. (void)contents;
  13009. break;
  13010. }
  13011. case WASM_OP_STRING_MEASURE_UTF8:
  13012. case WASM_OP_STRING_MEASURE_WTF8:
  13013. case WASM_OP_STRING_MEASURE_WTF16:
  13014. {
  13015. POP_STRINGREF();
  13016. PUSH_I32();
  13017. break;
  13018. }
  13019. case WASM_OP_STRING_ENCODE_UTF8:
  13020. case WASM_OP_STRING_ENCODE_WTF16:
  13021. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13022. case WASM_OP_STRING_ENCODE_WTF8:
  13023. {
  13024. uint32 memidx;
  13025. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13026. func->has_memory_operations = true;
  13027. #endif
  13028. read_leb_uint32(p, p_end, memidx);
  13029. #if WASM_ENABLE_FAST_INTERP != 0
  13030. emit_uint32(loader_ctx, (uint32)memidx);
  13031. #endif
  13032. POP_I32();
  13033. POP_STRINGREF();
  13034. PUSH_I32();
  13035. (void)memidx;
  13036. break;
  13037. }
  13038. case WASM_OP_STRING_CONCAT:
  13039. {
  13040. POP_STRINGREF();
  13041. POP_STRINGREF();
  13042. PUSH_REF(REF_TYPE_STRINGREF);
  13043. break;
  13044. }
  13045. case WASM_OP_STRING_EQ:
  13046. {
  13047. POP_STRINGREF();
  13048. POP_STRINGREF();
  13049. PUSH_I32();
  13050. break;
  13051. }
  13052. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13053. {
  13054. POP_STRINGREF();
  13055. PUSH_I32();
  13056. break;
  13057. }
  13058. case WASM_OP_STRING_AS_WTF8:
  13059. {
  13060. POP_STRINGREF();
  13061. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13062. break;
  13063. }
  13064. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13065. {
  13066. POP_I32();
  13067. POP_I32();
  13068. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13069. PUSH_I32();
  13070. break;
  13071. }
  13072. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13073. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13074. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13075. {
  13076. uint32 memidx;
  13077. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13078. func->has_memory_operations = true;
  13079. #endif
  13080. read_leb_uint32(p, p_end, memidx);
  13081. #if WASM_ENABLE_FAST_INTERP != 0
  13082. emit_uint32(loader_ctx, (uint32)memidx);
  13083. #endif
  13084. POP_I32();
  13085. POP_I32();
  13086. POP_I32();
  13087. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13088. PUSH_I32();
  13089. PUSH_I32();
  13090. (void)memidx;
  13091. break;
  13092. }
  13093. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13094. {
  13095. POP_I32();
  13096. POP_I32();
  13097. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13098. PUSH_REF(REF_TYPE_STRINGREF);
  13099. break;
  13100. }
  13101. case WASM_OP_STRING_AS_WTF16:
  13102. {
  13103. POP_STRINGREF();
  13104. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13105. break;
  13106. }
  13107. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13108. {
  13109. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13110. PUSH_I32();
  13111. break;
  13112. }
  13113. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13114. {
  13115. POP_I32();
  13116. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13117. PUSH_I32();
  13118. break;
  13119. }
  13120. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13121. {
  13122. uint32 memidx;
  13123. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13124. func->has_memory_operations = true;
  13125. #endif
  13126. read_leb_uint32(p, p_end, memidx);
  13127. #if WASM_ENABLE_FAST_INTERP != 0
  13128. emit_uint32(loader_ctx, (uint32)memidx);
  13129. #endif
  13130. POP_I32();
  13131. POP_I32();
  13132. POP_I32();
  13133. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13134. PUSH_I32();
  13135. (void)memidx;
  13136. break;
  13137. }
  13138. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13139. {
  13140. POP_I32();
  13141. POP_I32();
  13142. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13143. PUSH_REF(REF_TYPE_STRINGREF);
  13144. break;
  13145. }
  13146. case WASM_OP_STRING_AS_ITER:
  13147. {
  13148. POP_STRINGREF();
  13149. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13150. break;
  13151. }
  13152. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13153. {
  13154. POP_REF(REF_TYPE_STRINGVIEWITER);
  13155. PUSH_I32();
  13156. break;
  13157. }
  13158. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13159. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13160. {
  13161. POP_I32();
  13162. POP_REF(REF_TYPE_STRINGVIEWITER);
  13163. PUSH_I32();
  13164. break;
  13165. }
  13166. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13167. {
  13168. POP_I32();
  13169. POP_REF(REF_TYPE_STRINGVIEWITER);
  13170. PUSH_REF(REF_TYPE_STRINGREF);
  13171. break;
  13172. }
  13173. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13174. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13175. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13176. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13177. {
  13178. POP_I32();
  13179. POP_I32();
  13180. POP_REF(REF_TYPE_ARRAYREF);
  13181. PUSH_REF(REF_TYPE_STRINGREF);
  13182. break;
  13183. }
  13184. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13185. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13186. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13187. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13188. {
  13189. POP_I32();
  13190. POP_REF(REF_TYPE_ARRAYREF);
  13191. POP_STRINGREF();
  13192. PUSH_I32();
  13193. break;
  13194. }
  13195. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13196. default:
  13197. set_error_buf_v(error_buf, error_buf_size,
  13198. "%s %02x %02x", "unsupported opcode",
  13199. 0xfb, opcode1);
  13200. goto fail;
  13201. }
  13202. break;
  13203. }
  13204. #endif /* end of WASM_ENABLE_GC != 0 */
  13205. case WASM_OP_MISC_PREFIX:
  13206. {
  13207. uint32 opcode1;
  13208. read_leb_uint32(p, p_end, opcode1);
  13209. #if WASM_ENABLE_FAST_INTERP != 0
  13210. emit_byte(loader_ctx, ((uint8)opcode1));
  13211. #endif
  13212. switch (opcode1) {
  13213. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13214. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13215. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13216. break;
  13217. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13218. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13219. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13220. break;
  13221. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13222. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13223. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13224. break;
  13225. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13226. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13227. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13228. break;
  13229. #if WASM_ENABLE_BULK_MEMORY != 0
  13230. case WASM_OP_MEMORY_INIT:
  13231. {
  13232. read_leb_uint32(p, p_end, data_seg_idx);
  13233. #if WASM_ENABLE_FAST_INTERP != 0
  13234. emit_uint32(loader_ctx, data_seg_idx);
  13235. #endif
  13236. if (module->import_memory_count == 0
  13237. && module->memory_count == 0)
  13238. goto fail_unknown_memory;
  13239. read_leb_uint32(p, p_end, memidx);
  13240. check_memidx(module, memidx);
  13241. if (data_seg_idx >= module->data_seg_count) {
  13242. set_error_buf_v(error_buf, error_buf_size,
  13243. "unknown data segment %d",
  13244. data_seg_idx);
  13245. goto fail;
  13246. }
  13247. if (module->data_seg_count1 == 0)
  13248. goto fail_data_cnt_sec_require;
  13249. POP_I32();
  13250. POP_I32();
  13251. POP_MEM_OFFSET();
  13252. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13253. func->has_memory_operations = true;
  13254. #endif
  13255. #if WASM_ENABLE_WAMR_COMPILER != 0
  13256. module->is_bulk_memory_used = true;
  13257. #endif
  13258. break;
  13259. }
  13260. case WASM_OP_DATA_DROP:
  13261. {
  13262. read_leb_uint32(p, p_end, data_seg_idx);
  13263. #if WASM_ENABLE_FAST_INTERP != 0
  13264. emit_uint32(loader_ctx, data_seg_idx);
  13265. #endif
  13266. if (data_seg_idx >= module->data_seg_count) {
  13267. set_error_buf(error_buf, error_buf_size,
  13268. "unknown data segment");
  13269. goto fail;
  13270. }
  13271. if (module->data_seg_count1 == 0)
  13272. goto fail_data_cnt_sec_require;
  13273. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13274. func->has_memory_operations = true;
  13275. #endif
  13276. #if WASM_ENABLE_WAMR_COMPILER != 0
  13277. module->is_bulk_memory_used = true;
  13278. #endif
  13279. break;
  13280. }
  13281. case WASM_OP_MEMORY_COPY:
  13282. {
  13283. CHECK_BUF(p, p_end, sizeof(int16));
  13284. /* check both src and dst memory index */
  13285. read_leb_uint32(p, p_end, memidx);
  13286. check_memidx(module, memidx);
  13287. read_leb_uint32(p, p_end, memidx);
  13288. check_memidx(module, memidx);
  13289. if (module->import_memory_count == 0
  13290. && module->memory_count == 0)
  13291. goto fail_unknown_memory;
  13292. POP_MEM_OFFSET();
  13293. POP_MEM_OFFSET();
  13294. POP_MEM_OFFSET();
  13295. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13296. func->has_memory_operations = true;
  13297. #endif
  13298. #if WASM_ENABLE_WAMR_COMPILER != 0
  13299. module->is_bulk_memory_used = true;
  13300. #endif
  13301. break;
  13302. }
  13303. case WASM_OP_MEMORY_FILL:
  13304. {
  13305. read_leb_uint32(p, p_end, memidx);
  13306. check_memidx(module, memidx);
  13307. if (module->import_memory_count == 0
  13308. && module->memory_count == 0) {
  13309. goto fail_unknown_memory;
  13310. }
  13311. POP_MEM_OFFSET();
  13312. POP_I32();
  13313. POP_MEM_OFFSET();
  13314. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13315. func->has_memory_operations = true;
  13316. #endif
  13317. #if WASM_ENABLE_WAMR_COMPILER != 0
  13318. module->is_bulk_memory_used = true;
  13319. #endif
  13320. break;
  13321. }
  13322. fail_unknown_memory:
  13323. set_error_buf(error_buf, error_buf_size,
  13324. "unknown memory 0");
  13325. goto fail;
  13326. fail_data_cnt_sec_require:
  13327. set_error_buf(error_buf, error_buf_size,
  13328. "data count section required");
  13329. goto fail;
  13330. #endif /* WASM_ENABLE_BULK_MEMORY */
  13331. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13332. case WASM_OP_TABLE_INIT:
  13333. {
  13334. uint8 seg_type = 0, tbl_type = 0;
  13335. #if WASM_ENABLE_GC != 0
  13336. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13337. #endif
  13338. read_leb_uint32(p, p_end, table_seg_idx);
  13339. read_leb_uint32(p, p_end, table_idx);
  13340. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13341. #if WASM_ENABLE_GC != 0
  13342. (void **)&tbl_ref_type,
  13343. #else
  13344. NULL,
  13345. #endif
  13346. error_buf, error_buf_size))
  13347. goto fail;
  13348. if (!get_table_seg_elem_type(module, table_seg_idx,
  13349. &seg_type,
  13350. #if WASM_ENABLE_GC != 0
  13351. (void **)&seg_ref_type,
  13352. #else
  13353. NULL,
  13354. #endif
  13355. error_buf, error_buf_size))
  13356. goto fail;
  13357. #if WASM_ENABLE_GC == 0
  13358. if (seg_type != tbl_type) {
  13359. set_error_buf(error_buf, error_buf_size,
  13360. "type mismatch");
  13361. goto fail;
  13362. }
  13363. #else
  13364. if (!wasm_reftype_is_subtype_of(
  13365. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13366. module->types, module->type_count)) {
  13367. set_error_buf(error_buf, error_buf_size,
  13368. "type mismatch");
  13369. goto fail;
  13370. }
  13371. #endif
  13372. #if WASM_ENABLE_FAST_INTERP != 0
  13373. emit_uint32(loader_ctx, table_seg_idx);
  13374. emit_uint32(loader_ctx, table_idx);
  13375. #endif
  13376. POP_I32();
  13377. POP_I32();
  13378. #if WASM_ENABLE_MEMORY64 != 0
  13379. table_elem_idx_type = is_table_64bit(module, table_idx)
  13380. ? VALUE_TYPE_I64
  13381. : VALUE_TYPE_I32;
  13382. #endif
  13383. POP_TBL_ELEM_IDX();
  13384. #if WASM_ENABLE_WAMR_COMPILER != 0
  13385. module->is_ref_types_used = true;
  13386. #endif
  13387. break;
  13388. }
  13389. case WASM_OP_ELEM_DROP:
  13390. {
  13391. read_leb_uint32(p, p_end, table_seg_idx);
  13392. if (!get_table_seg_elem_type(module, table_seg_idx,
  13393. NULL, NULL, error_buf,
  13394. error_buf_size))
  13395. goto fail;
  13396. #if WASM_ENABLE_FAST_INTERP != 0
  13397. emit_uint32(loader_ctx, table_seg_idx);
  13398. #endif
  13399. #if WASM_ENABLE_WAMR_COMPILER != 0
  13400. module->is_ref_types_used = true;
  13401. #endif
  13402. break;
  13403. }
  13404. case WASM_OP_TABLE_COPY:
  13405. {
  13406. uint8 src_type, dst_type, src_tbl_idx_type,
  13407. dst_tbl_idx_type, min_tbl_idx_type;
  13408. #if WASM_ENABLE_GC != 0
  13409. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13410. #endif
  13411. uint32 src_tbl_idx, dst_tbl_idx;
  13412. read_leb_uint32(p, p_end, dst_tbl_idx);
  13413. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13414. #if WASM_ENABLE_GC != 0
  13415. (void **)&dst_ref_type,
  13416. #else
  13417. NULL,
  13418. #endif
  13419. error_buf, error_buf_size))
  13420. goto fail;
  13421. read_leb_uint32(p, p_end, src_tbl_idx);
  13422. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13423. #if WASM_ENABLE_GC != 0
  13424. (void **)&src_ref_type,
  13425. #else
  13426. NULL,
  13427. #endif
  13428. error_buf, error_buf_size))
  13429. goto fail;
  13430. #if WASM_ENABLE_GC == 0
  13431. if (src_type != dst_type) {
  13432. set_error_buf(error_buf, error_buf_size,
  13433. "type mismatch");
  13434. goto fail;
  13435. }
  13436. #else
  13437. if (!wasm_reftype_is_subtype_of(
  13438. src_type, src_ref_type, dst_type, dst_ref_type,
  13439. module->types, module->type_count)) {
  13440. set_error_buf(error_buf, error_buf_size,
  13441. "type mismatch");
  13442. goto fail;
  13443. }
  13444. #endif
  13445. #if WASM_ENABLE_FAST_INTERP != 0
  13446. emit_uint32(loader_ctx, dst_tbl_idx);
  13447. emit_uint32(loader_ctx, src_tbl_idx);
  13448. #endif
  13449. #if WASM_ENABLE_MEMORY64 != 0
  13450. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13451. ? VALUE_TYPE_I64
  13452. : VALUE_TYPE_I32;
  13453. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13454. ? VALUE_TYPE_I64
  13455. : VALUE_TYPE_I32;
  13456. min_tbl_idx_type =
  13457. (src_tbl_idx_type == VALUE_TYPE_I32
  13458. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13459. ? VALUE_TYPE_I32
  13460. : VALUE_TYPE_I64;
  13461. #else
  13462. src_tbl_idx_type = VALUE_TYPE_I32;
  13463. dst_tbl_idx_type = VALUE_TYPE_I32;
  13464. min_tbl_idx_type = VALUE_TYPE_I32;
  13465. #endif
  13466. table_elem_idx_type = min_tbl_idx_type;
  13467. POP_TBL_ELEM_IDX();
  13468. table_elem_idx_type = src_tbl_idx_type;
  13469. POP_TBL_ELEM_IDX();
  13470. table_elem_idx_type = dst_tbl_idx_type;
  13471. POP_TBL_ELEM_IDX();
  13472. #if WASM_ENABLE_WAMR_COMPILER != 0
  13473. module->is_ref_types_used = true;
  13474. #endif
  13475. break;
  13476. }
  13477. case WASM_OP_TABLE_SIZE:
  13478. {
  13479. read_leb_uint32(p, p_end, table_idx);
  13480. /* TODO: shall we create a new function to check
  13481. table idx instead of using below function? */
  13482. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13483. error_buf, error_buf_size))
  13484. goto fail;
  13485. #if WASM_ENABLE_FAST_INTERP != 0
  13486. emit_uint32(loader_ctx, table_idx);
  13487. #endif
  13488. #if WASM_ENABLE_MEMORY64 != 0
  13489. table_elem_idx_type = is_table_64bit(module, table_idx)
  13490. ? VALUE_TYPE_I64
  13491. : VALUE_TYPE_I32;
  13492. #endif
  13493. PUSH_TBL_ELEM_IDX();
  13494. #if WASM_ENABLE_WAMR_COMPILER != 0
  13495. module->is_ref_types_used = true;
  13496. #endif
  13497. break;
  13498. }
  13499. case WASM_OP_TABLE_GROW:
  13500. case WASM_OP_TABLE_FILL:
  13501. {
  13502. uint8 decl_type;
  13503. #if WASM_ENABLE_GC != 0
  13504. WASMRefType *ref_type = NULL;
  13505. #endif
  13506. read_leb_uint32(p, p_end, table_idx);
  13507. if (!get_table_elem_type(module, table_idx, &decl_type,
  13508. #if WASM_ENABLE_GC != 0
  13509. (void **)&ref_type,
  13510. #else
  13511. NULL,
  13512. #endif
  13513. error_buf, error_buf_size))
  13514. goto fail;
  13515. #if WASM_ENABLE_GC != 0
  13516. if (wasm_is_type_multi_byte_type(decl_type)) {
  13517. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13518. ref_type,
  13519. wasm_reftype_struct_size(ref_type));
  13520. }
  13521. #endif
  13522. if (opcode1 == WASM_OP_TABLE_GROW) {
  13523. if (table_idx < module->import_table_count) {
  13524. module->import_tables[table_idx]
  13525. .u.table.table_type.possible_grow = true;
  13526. }
  13527. else {
  13528. module
  13529. ->tables[table_idx
  13530. - module->import_table_count]
  13531. .table_type.possible_grow = true;
  13532. }
  13533. }
  13534. #if WASM_ENABLE_FAST_INTERP != 0
  13535. emit_uint32(loader_ctx, table_idx);
  13536. #endif
  13537. #if WASM_ENABLE_MEMORY64 != 0
  13538. table_elem_idx_type = is_table_64bit(module, table_idx)
  13539. ? VALUE_TYPE_I64
  13540. : VALUE_TYPE_I32;
  13541. #endif
  13542. POP_TBL_ELEM_IDX();
  13543. #if WASM_ENABLE_FAST_INTERP != 0
  13544. POP_OFFSET_TYPE(decl_type);
  13545. #endif
  13546. POP_TYPE(decl_type);
  13547. if (opcode1 == WASM_OP_TABLE_GROW)
  13548. PUSH_TBL_ELEM_IDX();
  13549. else
  13550. POP_TBL_ELEM_IDX();
  13551. #if WASM_ENABLE_WAMR_COMPILER != 0
  13552. module->is_ref_types_used = true;
  13553. #endif
  13554. break;
  13555. }
  13556. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13557. default:
  13558. set_error_buf_v(error_buf, error_buf_size,
  13559. "%s %02x %02x", "unsupported opcode",
  13560. 0xfc, opcode1);
  13561. goto fail;
  13562. }
  13563. break;
  13564. }
  13565. #if WASM_ENABLE_SIMD != 0
  13566. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13567. case WASM_OP_SIMD_PREFIX:
  13568. {
  13569. uint32 opcode1;
  13570. #if WASM_ENABLE_WAMR_COMPILER != 0
  13571. /* Mark the SIMD instruction is used in this module */
  13572. module->is_simd_used = true;
  13573. #endif
  13574. read_leb_uint32(p, p_end, opcode1);
  13575. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13576. */
  13577. switch (opcode1) {
  13578. /* memory instruction */
  13579. case SIMD_v128_load:
  13580. case SIMD_v128_load8x8_s:
  13581. case SIMD_v128_load8x8_u:
  13582. case SIMD_v128_load16x4_s:
  13583. case SIMD_v128_load16x4_u:
  13584. case SIMD_v128_load32x2_s:
  13585. case SIMD_v128_load32x2_u:
  13586. case SIMD_v128_load8_splat:
  13587. case SIMD_v128_load16_splat:
  13588. case SIMD_v128_load32_splat:
  13589. case SIMD_v128_load64_splat:
  13590. {
  13591. CHECK_MEMORY();
  13592. read_leb_uint32(p, p_end, align); /* align */
  13593. if (!check_simd_memory_access_align(
  13594. opcode1, align, error_buf, error_buf_size)) {
  13595. goto fail;
  13596. }
  13597. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13598. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13599. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13600. func->has_memory_operations = true;
  13601. #endif
  13602. break;
  13603. }
  13604. case SIMD_v128_store:
  13605. {
  13606. CHECK_MEMORY();
  13607. read_leb_uint32(p, p_end, align); /* align */
  13608. if (!check_simd_memory_access_align(
  13609. opcode1, align, error_buf, error_buf_size)) {
  13610. goto fail;
  13611. }
  13612. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13613. POP_V128();
  13614. POP_MEM_OFFSET();
  13615. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13616. func->has_memory_operations = true;
  13617. #endif
  13618. break;
  13619. }
  13620. /* basic operation */
  13621. case SIMD_v128_const:
  13622. {
  13623. CHECK_BUF1(p, p_end, 16);
  13624. p += 16;
  13625. PUSH_V128();
  13626. break;
  13627. }
  13628. case SIMD_v8x16_shuffle:
  13629. {
  13630. V128 mask;
  13631. CHECK_BUF1(p, p_end, 16);
  13632. mask = read_i8x16(p, error_buf, error_buf_size);
  13633. p += 16;
  13634. if (!check_simd_shuffle_mask(mask, error_buf,
  13635. error_buf_size)) {
  13636. goto fail;
  13637. }
  13638. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13639. break;
  13640. }
  13641. case SIMD_v8x16_swizzle:
  13642. {
  13643. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13644. break;
  13645. }
  13646. /* splat operation */
  13647. case SIMD_i8x16_splat:
  13648. case SIMD_i16x8_splat:
  13649. case SIMD_i32x4_splat:
  13650. case SIMD_i64x2_splat:
  13651. case SIMD_f32x4_splat:
  13652. case SIMD_f64x2_splat:
  13653. {
  13654. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13655. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13656. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13657. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13658. VALUE_TYPE_V128);
  13659. break;
  13660. }
  13661. /* lane operation */
  13662. case SIMD_i8x16_extract_lane_s:
  13663. case SIMD_i8x16_extract_lane_u:
  13664. case SIMD_i8x16_replace_lane:
  13665. case SIMD_i16x8_extract_lane_s:
  13666. case SIMD_i16x8_extract_lane_u:
  13667. case SIMD_i16x8_replace_lane:
  13668. case SIMD_i32x4_extract_lane:
  13669. case SIMD_i32x4_replace_lane:
  13670. case SIMD_i64x2_extract_lane:
  13671. case SIMD_i64x2_replace_lane:
  13672. case SIMD_f32x4_extract_lane:
  13673. case SIMD_f32x4_replace_lane:
  13674. case SIMD_f64x2_extract_lane:
  13675. case SIMD_f64x2_replace_lane:
  13676. {
  13677. uint8 lane;
  13678. /* clang-format off */
  13679. uint8 replace[] = {
  13680. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13681. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13682. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13683. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13684. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13685. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13686. };
  13687. uint8 push_type[] = {
  13688. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13689. VALUE_TYPE_V128,
  13690. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13691. VALUE_TYPE_V128,
  13692. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13693. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13694. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13695. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13696. };
  13697. /* clang-format on */
  13698. CHECK_BUF(p, p_end, 1);
  13699. lane = read_uint8(p);
  13700. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13701. error_buf_size)) {
  13702. goto fail;
  13703. }
  13704. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13705. if (!(wasm_loader_pop_frame_ref(
  13706. loader_ctx,
  13707. replace[opcode1
  13708. - SIMD_i8x16_extract_lane_s],
  13709. error_buf, error_buf_size)))
  13710. goto fail;
  13711. }
  13712. POP_AND_PUSH(
  13713. VALUE_TYPE_V128,
  13714. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13715. break;
  13716. }
  13717. /* i8x16 compare operation */
  13718. case SIMD_i8x16_eq:
  13719. case SIMD_i8x16_ne:
  13720. case SIMD_i8x16_lt_s:
  13721. case SIMD_i8x16_lt_u:
  13722. case SIMD_i8x16_gt_s:
  13723. case SIMD_i8x16_gt_u:
  13724. case SIMD_i8x16_le_s:
  13725. case SIMD_i8x16_le_u:
  13726. case SIMD_i8x16_ge_s:
  13727. case SIMD_i8x16_ge_u:
  13728. /* i16x8 compare operation */
  13729. case SIMD_i16x8_eq:
  13730. case SIMD_i16x8_ne:
  13731. case SIMD_i16x8_lt_s:
  13732. case SIMD_i16x8_lt_u:
  13733. case SIMD_i16x8_gt_s:
  13734. case SIMD_i16x8_gt_u:
  13735. case SIMD_i16x8_le_s:
  13736. case SIMD_i16x8_le_u:
  13737. case SIMD_i16x8_ge_s:
  13738. case SIMD_i16x8_ge_u:
  13739. /* i32x4 compare operation */
  13740. case SIMD_i32x4_eq:
  13741. case SIMD_i32x4_ne:
  13742. case SIMD_i32x4_lt_s:
  13743. case SIMD_i32x4_lt_u:
  13744. case SIMD_i32x4_gt_s:
  13745. case SIMD_i32x4_gt_u:
  13746. case SIMD_i32x4_le_s:
  13747. case SIMD_i32x4_le_u:
  13748. case SIMD_i32x4_ge_s:
  13749. case SIMD_i32x4_ge_u:
  13750. /* f32x4 compare operation */
  13751. case SIMD_f32x4_eq:
  13752. case SIMD_f32x4_ne:
  13753. case SIMD_f32x4_lt:
  13754. case SIMD_f32x4_gt:
  13755. case SIMD_f32x4_le:
  13756. case SIMD_f32x4_ge:
  13757. /* f64x2 compare operation */
  13758. case SIMD_f64x2_eq:
  13759. case SIMD_f64x2_ne:
  13760. case SIMD_f64x2_lt:
  13761. case SIMD_f64x2_gt:
  13762. case SIMD_f64x2_le:
  13763. case SIMD_f64x2_ge:
  13764. {
  13765. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13766. break;
  13767. }
  13768. /* v128 operation */
  13769. case SIMD_v128_not:
  13770. {
  13771. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13772. break;
  13773. }
  13774. case SIMD_v128_and:
  13775. case SIMD_v128_andnot:
  13776. case SIMD_v128_or:
  13777. case SIMD_v128_xor:
  13778. {
  13779. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13780. break;
  13781. }
  13782. case SIMD_v128_bitselect:
  13783. {
  13784. POP_V128();
  13785. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13786. break;
  13787. }
  13788. case SIMD_v128_any_true:
  13789. {
  13790. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13791. break;
  13792. }
  13793. /* Load Lane Operation */
  13794. case SIMD_v128_load8_lane:
  13795. case SIMD_v128_load16_lane:
  13796. case SIMD_v128_load32_lane:
  13797. case SIMD_v128_load64_lane:
  13798. case SIMD_v128_store8_lane:
  13799. case SIMD_v128_store16_lane:
  13800. case SIMD_v128_store32_lane:
  13801. case SIMD_v128_store64_lane:
  13802. {
  13803. uint8 lane;
  13804. CHECK_MEMORY();
  13805. read_leb_uint32(p, p_end, align); /* align */
  13806. if (!check_simd_memory_access_align(
  13807. opcode1, align, error_buf, error_buf_size)) {
  13808. goto fail;
  13809. }
  13810. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13811. CHECK_BUF(p, p_end, 1);
  13812. lane = read_uint8(p);
  13813. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13814. error_buf_size)) {
  13815. goto fail;
  13816. }
  13817. POP_V128();
  13818. POP_MEM_OFFSET();
  13819. if (opcode1 < SIMD_v128_store8_lane) {
  13820. PUSH_V128();
  13821. }
  13822. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13823. func->has_memory_operations = true;
  13824. #endif
  13825. break;
  13826. }
  13827. case SIMD_v128_load32_zero:
  13828. case SIMD_v128_load64_zero:
  13829. {
  13830. CHECK_MEMORY();
  13831. read_leb_uint32(p, p_end, align); /* align */
  13832. if (!check_simd_memory_access_align(
  13833. opcode1, align, error_buf, error_buf_size)) {
  13834. goto fail;
  13835. }
  13836. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13837. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13838. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13839. func->has_memory_operations = true;
  13840. #endif
  13841. break;
  13842. }
  13843. /* Float conversion */
  13844. case SIMD_f32x4_demote_f64x2_zero:
  13845. case SIMD_f64x2_promote_low_f32x4_zero:
  13846. {
  13847. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13848. break;
  13849. }
  13850. /* i8x16 Operation */
  13851. case SIMD_i8x16_abs:
  13852. case SIMD_i8x16_neg:
  13853. case SIMD_i8x16_popcnt:
  13854. {
  13855. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13856. break;
  13857. }
  13858. case SIMD_i8x16_all_true:
  13859. case SIMD_i8x16_bitmask:
  13860. {
  13861. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13862. break;
  13863. }
  13864. case SIMD_i8x16_narrow_i16x8_s:
  13865. case SIMD_i8x16_narrow_i16x8_u:
  13866. {
  13867. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13868. break;
  13869. }
  13870. case SIMD_f32x4_ceil:
  13871. case SIMD_f32x4_floor:
  13872. case SIMD_f32x4_trunc:
  13873. case SIMD_f32x4_nearest:
  13874. {
  13875. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13876. break;
  13877. }
  13878. case SIMD_i8x16_shl:
  13879. case SIMD_i8x16_shr_s:
  13880. case SIMD_i8x16_shr_u:
  13881. {
  13882. POP_I32();
  13883. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13884. break;
  13885. }
  13886. case SIMD_i8x16_add:
  13887. case SIMD_i8x16_add_sat_s:
  13888. case SIMD_i8x16_add_sat_u:
  13889. case SIMD_i8x16_sub:
  13890. case SIMD_i8x16_sub_sat_s:
  13891. case SIMD_i8x16_sub_sat_u:
  13892. {
  13893. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13894. break;
  13895. }
  13896. case SIMD_f64x2_ceil:
  13897. case SIMD_f64x2_floor:
  13898. {
  13899. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13900. break;
  13901. }
  13902. case SIMD_i8x16_min_s:
  13903. case SIMD_i8x16_min_u:
  13904. case SIMD_i8x16_max_s:
  13905. case SIMD_i8x16_max_u:
  13906. {
  13907. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13908. break;
  13909. }
  13910. case SIMD_f64x2_trunc:
  13911. {
  13912. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13913. break;
  13914. }
  13915. case SIMD_i8x16_avgr_u:
  13916. {
  13917. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13918. break;
  13919. }
  13920. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13921. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13922. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13923. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13924. /* i16x8 operation */
  13925. case SIMD_i16x8_abs:
  13926. case SIMD_i16x8_neg:
  13927. {
  13928. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13929. break;
  13930. }
  13931. case SIMD_i16x8_q15mulr_sat_s:
  13932. {
  13933. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13934. break;
  13935. }
  13936. case SIMD_i16x8_all_true:
  13937. case SIMD_i16x8_bitmask:
  13938. {
  13939. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13940. break;
  13941. }
  13942. case SIMD_i16x8_narrow_i32x4_s:
  13943. case SIMD_i16x8_narrow_i32x4_u:
  13944. {
  13945. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13946. break;
  13947. }
  13948. case SIMD_i16x8_extend_low_i8x16_s:
  13949. case SIMD_i16x8_extend_high_i8x16_s:
  13950. case SIMD_i16x8_extend_low_i8x16_u:
  13951. case SIMD_i16x8_extend_high_i8x16_u:
  13952. {
  13953. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13954. break;
  13955. }
  13956. case SIMD_i16x8_shl:
  13957. case SIMD_i16x8_shr_s:
  13958. case SIMD_i16x8_shr_u:
  13959. {
  13960. POP_I32();
  13961. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13962. break;
  13963. }
  13964. case SIMD_i16x8_add:
  13965. case SIMD_i16x8_add_sat_s:
  13966. case SIMD_i16x8_add_sat_u:
  13967. case SIMD_i16x8_sub:
  13968. case SIMD_i16x8_sub_sat_s:
  13969. case SIMD_i16x8_sub_sat_u:
  13970. {
  13971. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13972. break;
  13973. }
  13974. case SIMD_f64x2_nearest:
  13975. {
  13976. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13977. break;
  13978. }
  13979. case SIMD_i16x8_mul:
  13980. case SIMD_i16x8_min_s:
  13981. case SIMD_i16x8_min_u:
  13982. case SIMD_i16x8_max_s:
  13983. case SIMD_i16x8_max_u:
  13984. case SIMD_i16x8_avgr_u:
  13985. case SIMD_i16x8_extmul_low_i8x16_s:
  13986. case SIMD_i16x8_extmul_high_i8x16_s:
  13987. case SIMD_i16x8_extmul_low_i8x16_u:
  13988. case SIMD_i16x8_extmul_high_i8x16_u:
  13989. {
  13990. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13991. break;
  13992. }
  13993. /* i32x4 operation */
  13994. case SIMD_i32x4_abs:
  13995. case SIMD_i32x4_neg:
  13996. {
  13997. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13998. break;
  13999. }
  14000. case SIMD_i32x4_all_true:
  14001. case SIMD_i32x4_bitmask:
  14002. {
  14003. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14004. break;
  14005. }
  14006. case SIMD_i32x4_extend_low_i16x8_s:
  14007. case SIMD_i32x4_extend_high_i16x8_s:
  14008. case SIMD_i32x4_extend_low_i16x8_u:
  14009. case SIMD_i32x4_extend_high_i16x8_u:
  14010. {
  14011. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14012. break;
  14013. }
  14014. case SIMD_i32x4_shl:
  14015. case SIMD_i32x4_shr_s:
  14016. case SIMD_i32x4_shr_u:
  14017. {
  14018. POP_I32();
  14019. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14020. break;
  14021. }
  14022. case SIMD_i32x4_add:
  14023. case SIMD_i32x4_sub:
  14024. case SIMD_i32x4_mul:
  14025. case SIMD_i32x4_min_s:
  14026. case SIMD_i32x4_min_u:
  14027. case SIMD_i32x4_max_s:
  14028. case SIMD_i32x4_max_u:
  14029. case SIMD_i32x4_dot_i16x8_s:
  14030. case SIMD_i32x4_extmul_low_i16x8_s:
  14031. case SIMD_i32x4_extmul_high_i16x8_s:
  14032. case SIMD_i32x4_extmul_low_i16x8_u:
  14033. case SIMD_i32x4_extmul_high_i16x8_u:
  14034. {
  14035. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14036. break;
  14037. }
  14038. /* i64x2 operation */
  14039. case SIMD_i64x2_abs:
  14040. case SIMD_i64x2_neg:
  14041. {
  14042. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14043. break;
  14044. }
  14045. case SIMD_i64x2_all_true:
  14046. case SIMD_i64x2_bitmask:
  14047. {
  14048. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14049. break;
  14050. }
  14051. case SIMD_i64x2_extend_low_i32x4_s:
  14052. case SIMD_i64x2_extend_high_i32x4_s:
  14053. case SIMD_i64x2_extend_low_i32x4_u:
  14054. case SIMD_i64x2_extend_high_i32x4_u:
  14055. {
  14056. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14057. break;
  14058. }
  14059. case SIMD_i64x2_shl:
  14060. case SIMD_i64x2_shr_s:
  14061. case SIMD_i64x2_shr_u:
  14062. {
  14063. POP_I32();
  14064. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14065. break;
  14066. }
  14067. case SIMD_i64x2_add:
  14068. case SIMD_i64x2_sub:
  14069. case SIMD_i64x2_mul:
  14070. case SIMD_i64x2_eq:
  14071. case SIMD_i64x2_ne:
  14072. case SIMD_i64x2_lt_s:
  14073. case SIMD_i64x2_gt_s:
  14074. case SIMD_i64x2_le_s:
  14075. case SIMD_i64x2_ge_s:
  14076. case SIMD_i64x2_extmul_low_i32x4_s:
  14077. case SIMD_i64x2_extmul_high_i32x4_s:
  14078. case SIMD_i64x2_extmul_low_i32x4_u:
  14079. case SIMD_i64x2_extmul_high_i32x4_u:
  14080. {
  14081. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14082. break;
  14083. }
  14084. /* f32x4 operation */
  14085. case SIMD_f32x4_abs:
  14086. case SIMD_f32x4_neg:
  14087. case SIMD_f32x4_sqrt:
  14088. {
  14089. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14090. break;
  14091. }
  14092. case SIMD_f32x4_add:
  14093. case SIMD_f32x4_sub:
  14094. case SIMD_f32x4_mul:
  14095. case SIMD_f32x4_div:
  14096. case SIMD_f32x4_min:
  14097. case SIMD_f32x4_max:
  14098. case SIMD_f32x4_pmin:
  14099. case SIMD_f32x4_pmax:
  14100. {
  14101. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14102. break;
  14103. }
  14104. /* f64x2 operation */
  14105. case SIMD_f64x2_abs:
  14106. case SIMD_f64x2_neg:
  14107. case SIMD_f64x2_sqrt:
  14108. {
  14109. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14110. break;
  14111. }
  14112. case SIMD_f64x2_add:
  14113. case SIMD_f64x2_sub:
  14114. case SIMD_f64x2_mul:
  14115. case SIMD_f64x2_div:
  14116. case SIMD_f64x2_min:
  14117. case SIMD_f64x2_max:
  14118. case SIMD_f64x2_pmin:
  14119. case SIMD_f64x2_pmax:
  14120. {
  14121. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14122. break;
  14123. }
  14124. case SIMD_i32x4_trunc_sat_f32x4_s:
  14125. case SIMD_i32x4_trunc_sat_f32x4_u:
  14126. case SIMD_f32x4_convert_i32x4_s:
  14127. case SIMD_f32x4_convert_i32x4_u:
  14128. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14129. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14130. case SIMD_f64x2_convert_low_i32x4_s:
  14131. case SIMD_f64x2_convert_low_i32x4_u:
  14132. {
  14133. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14134. break;
  14135. }
  14136. default:
  14137. {
  14138. if (error_buf != NULL) {
  14139. snprintf(error_buf, error_buf_size,
  14140. "WASM module load failed: "
  14141. "invalid opcode 0xfd %02x.",
  14142. opcode1);
  14143. }
  14144. goto fail;
  14145. }
  14146. }
  14147. break;
  14148. }
  14149. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14150. #endif /* end of WASM_ENABLE_SIMD */
  14151. #if WASM_ENABLE_SHARED_MEMORY != 0
  14152. case WASM_OP_ATOMIC_PREFIX:
  14153. {
  14154. uint32 opcode1;
  14155. read_leb_uint32(p, p_end, opcode1);
  14156. #if WASM_ENABLE_FAST_INTERP != 0
  14157. emit_byte(loader_ctx, opcode1);
  14158. #endif
  14159. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14160. CHECK_MEMORY();
  14161. read_leb_uint32(p, p_end, align); /* align */
  14162. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14163. if (!check_memory_align_equal(opcode1, align, error_buf,
  14164. error_buf_size)) {
  14165. goto fail;
  14166. }
  14167. #if WASM_ENABLE_FAST_INTERP != 0
  14168. emit_uint32(loader_ctx, mem_offset);
  14169. #endif
  14170. }
  14171. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14172. func->has_memory_operations = true;
  14173. #endif
  14174. switch (opcode1) {
  14175. case WASM_OP_ATOMIC_NOTIFY:
  14176. POP_I32();
  14177. POP_MEM_OFFSET();
  14178. PUSH_I32();
  14179. break;
  14180. case WASM_OP_ATOMIC_WAIT32:
  14181. POP_I64();
  14182. POP_I32();
  14183. POP_MEM_OFFSET();
  14184. PUSH_I32();
  14185. break;
  14186. case WASM_OP_ATOMIC_WAIT64:
  14187. POP_I64();
  14188. POP_I64();
  14189. POP_MEM_OFFSET();
  14190. PUSH_I32();
  14191. break;
  14192. case WASM_OP_ATOMIC_FENCE:
  14193. /* reserved byte 0x00 */
  14194. if (*p++ != 0x00) {
  14195. set_error_buf(error_buf, error_buf_size,
  14196. "zero byte expected");
  14197. goto fail;
  14198. }
  14199. break;
  14200. case WASM_OP_ATOMIC_I32_LOAD:
  14201. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14202. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14203. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14204. break;
  14205. case WASM_OP_ATOMIC_I32_STORE:
  14206. case WASM_OP_ATOMIC_I32_STORE8:
  14207. case WASM_OP_ATOMIC_I32_STORE16:
  14208. POP_I32();
  14209. POP_MEM_OFFSET();
  14210. break;
  14211. case WASM_OP_ATOMIC_I64_LOAD:
  14212. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14213. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14214. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14215. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14216. break;
  14217. case WASM_OP_ATOMIC_I64_STORE:
  14218. case WASM_OP_ATOMIC_I64_STORE8:
  14219. case WASM_OP_ATOMIC_I64_STORE16:
  14220. case WASM_OP_ATOMIC_I64_STORE32:
  14221. POP_I64();
  14222. POP_MEM_OFFSET();
  14223. break;
  14224. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14225. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14226. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14227. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14228. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14229. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14230. case WASM_OP_ATOMIC_RMW_I32_AND:
  14231. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14232. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14233. case WASM_OP_ATOMIC_RMW_I32_OR:
  14234. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14235. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14236. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14237. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14238. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14239. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14240. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14241. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14242. POP_I32();
  14243. POP_MEM_OFFSET();
  14244. PUSH_I32();
  14245. break;
  14246. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14247. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14248. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14249. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14250. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14251. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14252. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14253. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14254. case WASM_OP_ATOMIC_RMW_I64_AND:
  14255. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14256. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14257. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14258. case WASM_OP_ATOMIC_RMW_I64_OR:
  14259. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14260. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14261. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14262. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14263. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14264. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14265. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14266. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14267. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14268. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14269. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14270. POP_I64();
  14271. POP_MEM_OFFSET();
  14272. PUSH_I64();
  14273. break;
  14274. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14275. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14276. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14277. POP_I32();
  14278. POP_I32();
  14279. POP_MEM_OFFSET();
  14280. PUSH_I32();
  14281. break;
  14282. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14283. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14284. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14285. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14286. POP_I64();
  14287. POP_I64();
  14288. POP_MEM_OFFSET();
  14289. PUSH_I64();
  14290. break;
  14291. default:
  14292. set_error_buf_v(error_buf, error_buf_size,
  14293. "%s %02x %02x", "unsupported opcode",
  14294. 0xfe, opcode1);
  14295. goto fail;
  14296. }
  14297. break;
  14298. }
  14299. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14300. default:
  14301. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14302. "unsupported opcode", opcode);
  14303. goto fail;
  14304. }
  14305. #if WASM_ENABLE_FAST_INTERP != 0
  14306. last_op = opcode;
  14307. #endif
  14308. }
  14309. if (loader_ctx->csp_num > 0) {
  14310. /* unmatched end opcodes result from unbalanced control flow structures,
  14311. * for example, br_table with inconsistent target count (1 declared, 2
  14312. * given), or simply superfluous end opcodes */
  14313. set_error_buf(
  14314. error_buf, error_buf_size,
  14315. "unexpected end opcodes from unbalanced control flow structures");
  14316. goto fail;
  14317. }
  14318. #if WASM_ENABLE_FAST_INTERP != 0
  14319. if (loader_ctx->p_code_compiled == NULL)
  14320. goto re_scan;
  14321. func->const_cell_num = loader_ctx->const_cell_num;
  14322. if (func->const_cell_num > 0) {
  14323. int32 j;
  14324. if (!(func->consts = func_const = loader_malloc(
  14325. func->const_cell_num * 4, error_buf, error_buf_size)))
  14326. goto fail;
  14327. func_const_end = func->consts + func->const_cell_num * 4;
  14328. /* reverse the const buf */
  14329. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14330. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14331. if (c->value_type == VALUE_TYPE_F64
  14332. || c->value_type == VALUE_TYPE_I64) {
  14333. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14334. &(c->value.f64), (uint32)sizeof(int64));
  14335. func_const += sizeof(int64);
  14336. }
  14337. else {
  14338. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14339. &(c->value.f32), (uint32)sizeof(int32));
  14340. func_const += sizeof(int32);
  14341. }
  14342. }
  14343. }
  14344. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14345. - loader_ctx->start_dynamic_offset + 1;
  14346. #else
  14347. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14348. #endif
  14349. func->max_block_num = loader_ctx->max_csp_num;
  14350. return_value = true;
  14351. fail:
  14352. wasm_loader_ctx_destroy(loader_ctx);
  14353. (void)table_idx;
  14354. (void)table_seg_idx;
  14355. (void)data_seg_idx;
  14356. (void)i64_const;
  14357. (void)local_offset;
  14358. (void)p_org;
  14359. (void)mem_offset;
  14360. (void)align;
  14361. return return_value;
  14362. }