wasm_loader.c 582 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.mem_type.flags
  44. & MEMORY64_FLAG);
  45. else if (module->memory_count > 0)
  46. return !!(module->memories[0].flags & MEMORY64_FLAG);
  47. return false;
  48. }
  49. #endif
  50. static void
  51. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  52. {
  53. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  54. }
  55. #if WASM_ENABLE_MEMORY64 != 0
  56. static void
  57. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  58. {
  59. if (error_buf != NULL) {
  60. snprintf(error_buf, error_buf_size, "offset out of range");
  61. }
  62. }
  63. #endif
  64. static void
  65. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  66. {
  67. va_list args;
  68. char buf[128];
  69. if (error_buf != NULL) {
  70. va_start(args, format);
  71. vsnprintf(buf, sizeof(buf), format, args);
  72. va_end(args);
  73. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  74. }
  75. }
  76. static bool
  77. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  78. char *error_buf, uint32 error_buf_size)
  79. {
  80. if ((uintptr_t)buf + length < (uintptr_t)buf
  81. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  82. set_error_buf(error_buf, error_buf_size,
  83. "unexpected end of section or function");
  84. return false;
  85. }
  86. return true;
  87. }
  88. static bool
  89. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  90. char *error_buf, uint32 error_buf_size)
  91. {
  92. if ((uintptr_t)buf + length < (uintptr_t)buf
  93. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  94. set_error_buf(error_buf, error_buf_size, "unexpected end");
  95. return false;
  96. }
  97. return true;
  98. }
  99. #define CHECK_BUF(buf, buf_end, length) \
  100. do { \
  101. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  102. goto fail; \
  103. } \
  104. } while (0)
  105. #define CHECK_BUF1(buf, buf_end, length) \
  106. do { \
  107. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  108. goto fail; \
  109. } \
  110. } while (0)
  111. #define skip_leb(p) while (*p++ & 0x80)
  112. #define skip_leb_int64(p, p_end) skip_leb(p)
  113. #define skip_leb_uint32(p, p_end) skip_leb(p)
  114. #define skip_leb_int32(p, p_end) skip_leb(p)
  115. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  116. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  117. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  118. #define read_leb_int64(p, p_end, res) \
  119. do { \
  120. uint64 res64; \
  121. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  122. error_buf_size)) \
  123. goto fail; \
  124. res = (int64)res64; \
  125. } while (0)
  126. #if WASM_ENABLE_MEMORY64 != 0
  127. #define read_leb_mem_offset(p, p_end, res) \
  128. do { \
  129. uint64 res64; \
  130. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  131. &res64, error_buf, error_buf_size)) { \
  132. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  133. goto fail; \
  134. } \
  135. res = (mem_offset_t)res64; \
  136. } while (0)
  137. #else
  138. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  139. #endif
  140. #define read_leb_uint32(p, p_end, res) \
  141. do { \
  142. uint64 res64; \
  143. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  144. error_buf_size)) \
  145. goto fail; \
  146. res = (uint32)res64; \
  147. } while (0)
  148. #define read_leb_int32(p, p_end, res) \
  149. do { \
  150. uint64 res64; \
  151. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  152. error_buf_size)) \
  153. goto fail; \
  154. res = (int32)res64; \
  155. } while (0)
  156. static char *
  157. type2str(uint8 type)
  158. {
  159. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  160. #if WASM_ENABLE_GC != 0
  161. char *type_str_ref[] = { "stringview_iter",
  162. "stringview_wtf16",
  163. "(ref null ht)",
  164. "(ref ht)",
  165. "", /* reserved */
  166. "stringview_wtf8",
  167. "stringref",
  168. "", /* reserved */
  169. "", /* reserved */
  170. "arrayref",
  171. "structref",
  172. "i32ref",
  173. "eqref",
  174. "anyref",
  175. "externref",
  176. "funcref",
  177. "nullref",
  178. "nullexternref",
  179. "nullfuncref" };
  180. #endif
  181. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  182. return type_str[type - VALUE_TYPE_V128];
  183. #if WASM_ENABLE_GC != 0
  184. else if (wasm_is_type_reftype(type))
  185. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  186. #endif
  187. else if (type == VALUE_TYPE_FUNCREF)
  188. return "funcref";
  189. else if (type == VALUE_TYPE_EXTERNREF)
  190. return "externref";
  191. else
  192. return "unknown type";
  193. }
  194. static bool
  195. is_32bit_type(uint8 type)
  196. {
  197. if (type == VALUE_TYPE_I32
  198. || type == VALUE_TYPE_F32
  199. /* the operand stack is in polymorphic state */
  200. || type == VALUE_TYPE_ANY
  201. #if WASM_ENABLE_GC != 0
  202. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  203. #elif WASM_ENABLE_REF_TYPES != 0
  204. /* For reference types, we use uint32 index to represent
  205. the funcref and externref */
  206. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  207. #endif
  208. )
  209. return true;
  210. return false;
  211. }
  212. static bool
  213. is_64bit_type(uint8 type)
  214. {
  215. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  216. #if WASM_ENABLE_GC != 0
  217. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  218. #endif
  219. )
  220. return true;
  221. return false;
  222. }
  223. #if WASM_ENABLE_GC != 0
  224. static bool
  225. is_packed_type(uint8 type)
  226. {
  227. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  228. }
  229. #endif
  230. static bool
  231. is_byte_a_type(uint8 type)
  232. {
  233. return (is_valid_value_type_for_interpreter(type)
  234. || (type == VALUE_TYPE_VOID))
  235. ? true
  236. : false;
  237. }
  238. #if WASM_ENABLE_SIMD != 0
  239. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  240. static V128
  241. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  242. {
  243. V128 result;
  244. uint8 i;
  245. for (i = 0; i != 16; ++i) {
  246. result.i8x16[i] = read_uint8(p_buf);
  247. }
  248. return result;
  249. }
  250. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  251. #endif /* end of WASM_ENABLE_SIMD */
  252. static void *
  253. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  254. {
  255. void *mem;
  256. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  257. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  258. return NULL;
  259. }
  260. memset(mem, 0, (uint32)size);
  261. return mem;
  262. }
  263. static void *
  264. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  265. uint32 error_buf_size)
  266. {
  267. uint8 *mem_new;
  268. bh_assert(size_new > size_old);
  269. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  270. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  271. memset(mem_new + size_old, 0, size_new - size_old);
  272. wasm_runtime_free(mem_old);
  273. }
  274. return mem_new;
  275. }
  276. #define MEM_REALLOC(mem, size_old, size_new) \
  277. do { \
  278. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  279. error_buf_size); \
  280. if (!mem_new) \
  281. goto fail; \
  282. mem = mem_new; \
  283. } while (0)
  284. #if WASM_ENABLE_GC != 0
  285. static bool
  286. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  287. char *error_buf, uint32 error_buf_size)
  288. {
  289. if (type_index >= type_count) {
  290. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  291. type_index);
  292. return false;
  293. }
  294. return true;
  295. }
  296. static bool
  297. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  298. uint32 error_buf_size)
  299. {
  300. if (!check_type_index(module, module->type_count, type_index, error_buf,
  301. error_buf_size)) {
  302. return false;
  303. }
  304. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  305. set_error_buf(error_buf, error_buf_size, "unkown array type");
  306. return false;
  307. }
  308. return true;
  309. }
  310. #endif
  311. static bool
  312. check_function_index(const WASMModule *module, uint32 function_index,
  313. char *error_buf, uint32 error_buf_size)
  314. {
  315. if (function_index
  316. >= module->import_function_count + module->function_count) {
  317. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  318. function_index);
  319. return false;
  320. }
  321. return true;
  322. }
  323. typedef struct InitValue {
  324. uint8 type;
  325. uint8 flag;
  326. #if WASM_ENABLE_GC != 0
  327. uint8 gc_opcode;
  328. WASMRefType ref_type;
  329. #endif
  330. WASMValue value;
  331. } InitValue;
  332. typedef struct ConstExprContext {
  333. uint32 sp;
  334. uint32 size;
  335. WASMModule *module;
  336. InitValue *stack;
  337. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  338. } ConstExprContext;
  339. static void
  340. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  341. {
  342. ctx->sp = 0;
  343. ctx->module = module;
  344. ctx->stack = ctx->data;
  345. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  346. }
  347. static bool
  348. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  349. #if WASM_ENABLE_GC != 0
  350. WASMRefType *ref_type, uint8 gc_opcode,
  351. #endif
  352. WASMValue *value, char *error_buf, uint32 error_buf_size)
  353. {
  354. InitValue *cur_value;
  355. if (ctx->sp >= ctx->size) {
  356. if (ctx->stack != ctx->data) {
  357. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  358. (ctx->size + 4) * sizeof(InitValue));
  359. }
  360. else {
  361. if (!(ctx->stack =
  362. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  363. error_buf, error_buf_size))) {
  364. goto fail;
  365. }
  366. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  367. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  368. }
  369. ctx->size += 4;
  370. }
  371. cur_value = &ctx->stack[ctx->sp++];
  372. cur_value->type = type;
  373. cur_value->flag = flag;
  374. cur_value->value = *value;
  375. #if WASM_ENABLE_GC != 0
  376. cur_value->gc_opcode = gc_opcode;
  377. if (wasm_is_type_multi_byte_type(type)) {
  378. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  379. ref_type, wasm_reftype_struct_size(ref_type));
  380. }
  381. #endif
  382. return true;
  383. fail:
  384. return false;
  385. }
  386. #if WASM_ENABLE_GC != 0
  387. static void
  388. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  389. {
  390. WASMStructNewInitValues *struct_init_values =
  391. (WASMStructNewInitValues *)data;
  392. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  393. WASMType *wasm_type;
  394. uint32 i;
  395. if (!data)
  396. return;
  397. wasm_type = module->types[struct_init_values->type_idx];
  398. /* The data can only be type of `WASMStructNewInitValues *`
  399. or `WASMArrayNewInitValues *` */
  400. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  401. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  402. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  403. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  404. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  405. WASMRefType *ref_type;
  406. uint8 field_type;
  407. for (i = 0; i < struct_init_values->count; i++) {
  408. field_type = struct_type->fields[i].field_type;
  409. if (wasm_is_type_multi_byte_type(field_type))
  410. ref_type = ref_type_map->ref_type;
  411. else
  412. ref_type = NULL;
  413. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  414. REF_TYPE_STRUCTREF, NULL,
  415. module->types, module->type_count)
  416. || wasm_reftype_is_subtype_of(
  417. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  418. module->types, module->type_count)) {
  419. destroy_init_expr_data_recursive(
  420. module, struct_init_values->fields[i].data);
  421. }
  422. }
  423. }
  424. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  425. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  426. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  427. uint8 elem_type = array_type->elem_type;
  428. for (i = 0; i < array_init_values->length; i++) {
  429. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  430. REF_TYPE_STRUCTREF, NULL,
  431. module->types, module->type_count)
  432. || wasm_reftype_is_subtype_of(
  433. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  434. module->types, module->type_count)) {
  435. destroy_init_expr_data_recursive(
  436. module, array_init_values->elem_data[i].data);
  437. }
  438. }
  439. }
  440. wasm_runtime_free(data);
  441. }
  442. #endif
  443. static bool
  444. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  445. #if WASM_ENABLE_GC != 0
  446. WASMRefType *ref_type, uint8 *p_gc_opcode,
  447. #endif
  448. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  449. {
  450. InitValue *cur_value;
  451. if (ctx->sp == 0) {
  452. set_error_buf(error_buf, error_buf_size,
  453. "type mismatch: const expr stack underflow");
  454. return false;
  455. }
  456. cur_value = &ctx->stack[--ctx->sp];
  457. #if WASM_ENABLE_GC == 0
  458. if (cur_value->type != type) {
  459. set_error_buf(error_buf, error_buf_size, "type mismatch");
  460. return false;
  461. }
  462. #else
  463. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  464. ref_type, ctx->module->types,
  465. ctx->module->type_count)) {
  466. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  467. "type mismatch: expect ", type2str(type),
  468. " but got other");
  469. goto fail;
  470. }
  471. #endif
  472. if (p_flag)
  473. *p_flag = cur_value->flag;
  474. if (p_value)
  475. *p_value = cur_value->value;
  476. #if WASM_ENABLE_GC != 0
  477. if (p_gc_opcode)
  478. *p_gc_opcode = cur_value->gc_opcode;
  479. #endif
  480. return true;
  481. #if WASM_ENABLE_GC != 0
  482. fail:
  483. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  484. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  485. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  486. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  487. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  488. }
  489. return false;
  490. #endif
  491. }
  492. static void
  493. destroy_const_expr_stack(ConstExprContext *ctx)
  494. {
  495. #if WASM_ENABLE_GC != 0
  496. uint32 i;
  497. for (i = 0; i < ctx->sp; i++) {
  498. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  499. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  500. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  501. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  502. destroy_init_expr_data_recursive(ctx->module,
  503. ctx->stack[i].value.data);
  504. }
  505. }
  506. #endif
  507. if (ctx->stack != ctx->data) {
  508. wasm_runtime_free(ctx->stack);
  509. }
  510. }
  511. #if WASM_ENABLE_GC != 0
  512. static void
  513. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  514. {
  515. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  516. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  517. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  518. destroy_init_expr_data_recursive(module, expr->u.data);
  519. }
  520. }
  521. #endif /* end of WASM_ENABLE_GC != 0 */
  522. static bool
  523. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  524. InitializerExpression *init_expr, uint8 type, void *ref_type,
  525. char *error_buf, uint32 error_buf_size)
  526. {
  527. const uint8 *p = *p_buf, *p_end = buf_end;
  528. uint8 flag, *p_float;
  529. uint32 i;
  530. ConstExprContext const_expr_ctx = { 0 };
  531. WASMValue cur_value;
  532. #if WASM_ENABLE_GC != 0
  533. uint32 opcode1, type_idx;
  534. uint8 opcode;
  535. WASMRefType cur_ref_type = { 0 };
  536. #endif
  537. init_const_expr_stack(&const_expr_ctx, module);
  538. CHECK_BUF(p, p_end, 1);
  539. flag = read_uint8(p);
  540. while (flag != WASM_OP_END) {
  541. switch (flag) {
  542. /* i32.const */
  543. case INIT_EXPR_TYPE_I32_CONST:
  544. read_leb_int32(p, p_end, cur_value.i32);
  545. if (!push_const_expr_stack(
  546. &const_expr_ctx, flag, VALUE_TYPE_I32,
  547. #if WASM_ENABLE_GC != 0
  548. NULL, 0,
  549. #endif
  550. &cur_value, error_buf, error_buf_size))
  551. goto fail;
  552. break;
  553. /* i64.const */
  554. case INIT_EXPR_TYPE_I64_CONST:
  555. read_leb_int64(p, p_end, cur_value.i64);
  556. if (!push_const_expr_stack(
  557. &const_expr_ctx, flag, VALUE_TYPE_I64,
  558. #if WASM_ENABLE_GC != 0
  559. NULL, 0,
  560. #endif
  561. &cur_value, error_buf, error_buf_size))
  562. goto fail;
  563. break;
  564. /* f32.const */
  565. case INIT_EXPR_TYPE_F32_CONST:
  566. CHECK_BUF(p, p_end, 4);
  567. p_float = (uint8 *)&cur_value.f32;
  568. for (i = 0; i < sizeof(float32); i++)
  569. *p_float++ = *p++;
  570. if (!push_const_expr_stack(
  571. &const_expr_ctx, flag, VALUE_TYPE_F32,
  572. #if WASM_ENABLE_GC != 0
  573. NULL, 0,
  574. #endif
  575. &cur_value, error_buf, error_buf_size))
  576. goto fail;
  577. break;
  578. /* f64.const */
  579. case INIT_EXPR_TYPE_F64_CONST:
  580. CHECK_BUF(p, p_end, 8);
  581. p_float = (uint8 *)&cur_value.f64;
  582. for (i = 0; i < sizeof(float64); i++)
  583. *p_float++ = *p++;
  584. if (!push_const_expr_stack(
  585. &const_expr_ctx, flag, VALUE_TYPE_F64,
  586. #if WASM_ENABLE_GC != 0
  587. NULL, 0,
  588. #endif
  589. &cur_value, error_buf, error_buf_size))
  590. goto fail;
  591. break;
  592. #if WASM_ENABLE_SIMD != 0
  593. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  594. /* v128.const */
  595. case INIT_EXPR_TYPE_V128_CONST:
  596. {
  597. uint64 high, low;
  598. CHECK_BUF(p, p_end, 1);
  599. (void)read_uint8(p);
  600. CHECK_BUF(p, p_end, 16);
  601. wasm_runtime_read_v128(p, &high, &low);
  602. p += 16;
  603. cur_value.v128.i64x2[0] = high;
  604. cur_value.v128.i64x2[1] = low;
  605. if (!push_const_expr_stack(
  606. &const_expr_ctx, flag, VALUE_TYPE_V128,
  607. #if WASM_ENABLE_GC != 0
  608. NULL, 0,
  609. #endif
  610. &cur_value, error_buf, error_buf_size))
  611. goto fail;
  612. #if WASM_ENABLE_WAMR_COMPILER != 0
  613. /* If any init_expr is v128.const, mark SIMD used */
  614. module->is_simd_used = true;
  615. #endif
  616. break;
  617. }
  618. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  619. #endif /* end of WASM_ENABLE_SIMD */
  620. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  621. /* ref.func */
  622. case INIT_EXPR_TYPE_FUNCREF_CONST:
  623. {
  624. uint32 func_idx;
  625. read_leb_uint32(p, p_end, func_idx);
  626. cur_value.ref_index = func_idx;
  627. if (!check_function_index(module, func_idx, error_buf,
  628. error_buf_size)) {
  629. goto fail;
  630. }
  631. #if WASM_ENABLE_GC == 0
  632. if (!push_const_expr_stack(&const_expr_ctx, flag,
  633. VALUE_TYPE_FUNCREF, &cur_value,
  634. error_buf, error_buf_size))
  635. goto fail;
  636. #else
  637. if (func_idx < module->import_function_count) {
  638. type_idx =
  639. module->import_functions[func_idx].u.function.type_idx;
  640. }
  641. else {
  642. type_idx = module
  643. ->functions[func_idx
  644. - module->import_function_count]
  645. ->type_idx;
  646. }
  647. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  648. false, type_idx);
  649. if (!push_const_expr_stack(&const_expr_ctx, flag,
  650. cur_ref_type.ref_type, &cur_ref_type,
  651. 0, &cur_value, error_buf,
  652. error_buf_size))
  653. goto fail;
  654. #endif
  655. #if WASM_ENABLE_WAMR_COMPILER != 0
  656. module->is_ref_types_used = true;
  657. #endif
  658. break;
  659. }
  660. /* ref.null */
  661. case INIT_EXPR_TYPE_REFNULL_CONST:
  662. {
  663. uint8 type1;
  664. CHECK_BUF(p, p_end, 1);
  665. type1 = read_uint8(p);
  666. #if WASM_ENABLE_GC == 0
  667. cur_value.ref_index = NULL_REF;
  668. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  669. &cur_value, error_buf,
  670. error_buf_size))
  671. goto fail;
  672. #else
  673. cur_value.gc_obj = NULL_REF;
  674. if (!is_byte_a_type(type1)) {
  675. p--;
  676. read_leb_uint32(p, p_end, type_idx);
  677. if (!check_type_index(module, module->type_count, type_idx,
  678. error_buf, error_buf_size))
  679. goto fail;
  680. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  681. true, type_idx);
  682. if (!push_const_expr_stack(&const_expr_ctx, flag,
  683. cur_ref_type.ref_type,
  684. &cur_ref_type, 0, &cur_value,
  685. error_buf, error_buf_size))
  686. goto fail;
  687. }
  688. else {
  689. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  690. NULL, 0, &cur_value, error_buf,
  691. error_buf_size))
  692. goto fail;
  693. }
  694. #endif
  695. #if WASM_ENABLE_WAMR_COMPILER != 0
  696. module->is_ref_types_used = true;
  697. #endif
  698. break;
  699. }
  700. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  701. /* get_global */
  702. case INIT_EXPR_TYPE_GET_GLOBAL:
  703. {
  704. uint32 global_idx;
  705. uint8 global_type;
  706. read_leb_uint32(p, p_end, cur_value.global_index);
  707. global_idx = cur_value.global_index;
  708. /*
  709. * Currently, constant expressions occurring as initializers
  710. * of globals are further constrained in that contained
  711. * global.get instructions are
  712. * only allowed to refer to imported globals.
  713. *
  714. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  715. */
  716. if (global_idx >= module->import_global_count
  717. /* make spec test happy */
  718. #if WASM_ENABLE_GC != 0
  719. + module->global_count
  720. #endif
  721. ) {
  722. set_error_buf_v(error_buf, error_buf_size,
  723. "unknown global %u", global_idx);
  724. goto fail;
  725. }
  726. if (
  727. /* make spec test happy */
  728. #if WASM_ENABLE_GC != 0
  729. global_idx < module->import_global_count &&
  730. #endif
  731. module->import_globals[global_idx]
  732. .u.global.type.is_mutable) {
  733. set_error_buf_v(error_buf, error_buf_size,
  734. "constant expression required");
  735. goto fail;
  736. }
  737. if (global_idx < module->import_global_count) {
  738. global_type = module->import_globals[global_idx]
  739. .u.global.type.val_type;
  740. #if WASM_ENABLE_GC != 0
  741. if (wasm_is_type_multi_byte_type(global_type)) {
  742. WASMRefType *global_ref_type =
  743. module->import_globals[global_idx]
  744. .u.global.ref_type;
  745. bh_memcpy_s(&cur_ref_type,
  746. wasm_reftype_struct_size(global_ref_type),
  747. global_ref_type,
  748. wasm_reftype_struct_size(global_ref_type));
  749. }
  750. #endif
  751. }
  752. else {
  753. global_type =
  754. module
  755. ->globals[global_idx - module->import_global_count]
  756. .type.val_type;
  757. #if WASM_ENABLE_GC != 0
  758. if (wasm_is_type_multi_byte_type(global_type)) {
  759. WASMRefType *global_ref_type =
  760. module
  761. ->globals[global_idx
  762. - module->import_global_count]
  763. .ref_type;
  764. bh_memcpy_s(&cur_ref_type,
  765. wasm_reftype_struct_size(global_ref_type),
  766. global_ref_type,
  767. wasm_reftype_struct_size(global_ref_type));
  768. }
  769. #endif
  770. }
  771. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  772. #if WASM_ENABLE_GC != 0
  773. &cur_ref_type, 0,
  774. #endif
  775. &cur_value, error_buf,
  776. error_buf_size))
  777. goto fail;
  778. break;
  779. }
  780. #if WASM_ENABLE_GC != 0
  781. /* struct.new and array.new */
  782. case WASM_OP_GC_PREFIX:
  783. {
  784. read_leb_uint32(p, p_end, opcode1);
  785. switch (opcode1) {
  786. case WASM_OP_STRUCT_NEW:
  787. {
  788. WASMStructType *struct_type;
  789. WASMStructNewInitValues *struct_init_values = NULL;
  790. uint32 field_count;
  791. read_leb_uint32(p, p_end, type_idx);
  792. if (!check_type_index(module, module->type_count,
  793. type_idx, error_buf,
  794. error_buf_size)) {
  795. goto fail;
  796. }
  797. struct_type = (WASMStructType *)module->types[type_idx];
  798. if (struct_type->base_type.type_flag
  799. != WASM_TYPE_STRUCT) {
  800. set_error_buf(error_buf, error_buf_size,
  801. "unkown struct type");
  802. goto fail;
  803. }
  804. field_count = struct_type->field_count;
  805. if (!(struct_init_values = loader_malloc(
  806. offsetof(WASMStructNewInitValues, fields)
  807. + (uint64)field_count * sizeof(WASMValue),
  808. error_buf, error_buf_size))) {
  809. goto fail;
  810. }
  811. struct_init_values->type_idx = type_idx;
  812. struct_init_values->count = field_count;
  813. for (i = field_count; i > 0; i--) {
  814. WASMRefType *field_ref_type = NULL;
  815. uint32 field_idx = i - 1;
  816. uint8 field_type =
  817. struct_type->fields[field_idx].field_type;
  818. if (wasm_is_type_multi_byte_type(field_type)) {
  819. field_ref_type = wasm_reftype_map_find(
  820. struct_type->ref_type_maps,
  821. struct_type->ref_type_map_count, field_idx);
  822. }
  823. if (is_packed_type(field_type)) {
  824. field_type = VALUE_TYPE_I32;
  825. }
  826. if (!pop_const_expr_stack(
  827. &const_expr_ctx, NULL, field_type,
  828. field_ref_type, NULL,
  829. &struct_init_values->fields[field_idx],
  830. error_buf, error_buf_size)) {
  831. destroy_init_expr_data_recursive(
  832. module, struct_init_values);
  833. goto fail;
  834. }
  835. }
  836. cur_value.data = struct_init_values;
  837. wasm_set_refheaptype_typeidx(
  838. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  839. if (!push_const_expr_stack(
  840. &const_expr_ctx, flag, cur_ref_type.ref_type,
  841. &cur_ref_type, (uint8)opcode1, &cur_value,
  842. error_buf, error_buf_size)) {
  843. destroy_init_expr_data_recursive(
  844. module, struct_init_values);
  845. goto fail;
  846. }
  847. break;
  848. }
  849. case WASM_OP_STRUCT_NEW_DEFAULT:
  850. {
  851. read_leb_uint32(p, p_end, cur_value.type_index);
  852. type_idx = cur_value.type_index;
  853. if (!check_type_index(module, module->type_count,
  854. type_idx, error_buf,
  855. error_buf_size)) {
  856. goto fail;
  857. }
  858. if (module->types[type_idx]->type_flag
  859. != WASM_TYPE_STRUCT) {
  860. set_error_buf(error_buf, error_buf_size,
  861. "unkown struct type");
  862. goto fail;
  863. }
  864. cur_value.type_index = type_idx;
  865. cur_value.data = NULL;
  866. wasm_set_refheaptype_typeidx(
  867. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  868. if (!push_const_expr_stack(
  869. &const_expr_ctx, flag, cur_ref_type.ref_type,
  870. &cur_ref_type, (uint8)opcode1, &cur_value,
  871. error_buf, error_buf_size)) {
  872. goto fail;
  873. }
  874. break;
  875. }
  876. case WASM_OP_ARRAY_NEW:
  877. case WASM_OP_ARRAY_NEW_DEFAULT:
  878. case WASM_OP_ARRAY_NEW_FIXED:
  879. {
  880. WASMArrayNewInitValues *array_init_values = NULL;
  881. WASMArrayType *array_type = NULL;
  882. WASMRefType *elem_ref_type = NULL;
  883. uint64 total_size;
  884. uint8 elem_type;
  885. read_leb_uint32(p, p_end, cur_value.type_index);
  886. type_idx = cur_value.type_index;
  887. if (!check_type_index(module, module->type_count,
  888. type_idx, error_buf,
  889. error_buf_size)) {
  890. goto fail;
  891. }
  892. array_type = (WASMArrayType *)module->types[type_idx];
  893. if (array_type->base_type.type_flag
  894. != WASM_TYPE_ARRAY) {
  895. set_error_buf(error_buf, error_buf_size,
  896. "unkown array type");
  897. goto fail;
  898. }
  899. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  900. elem_type = array_type->elem_type;
  901. if (wasm_is_type_multi_byte_type(elem_type)) {
  902. elem_ref_type = array_type->elem_ref_type;
  903. }
  904. if (is_packed_type(elem_type)) {
  905. elem_type = VALUE_TYPE_I32;
  906. }
  907. if (opcode1 == WASM_OP_ARRAY_NEW) {
  908. WASMValue len_val;
  909. if (!(array_init_values = loader_malloc(
  910. sizeof(WASMArrayNewInitValues),
  911. error_buf, error_buf_size))) {
  912. goto fail;
  913. }
  914. array_init_values->type_idx = type_idx;
  915. if (!pop_const_expr_stack(
  916. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  917. NULL, NULL, &len_val, error_buf,
  918. error_buf_size)) {
  919. destroy_init_expr_data_recursive(
  920. module, array_init_values);
  921. goto fail;
  922. }
  923. array_init_values->length = len_val.i32;
  924. if (!pop_const_expr_stack(
  925. &const_expr_ctx, NULL, elem_type,
  926. elem_ref_type, NULL,
  927. &array_init_values->elem_data[0],
  928. error_buf, error_buf_size)) {
  929. destroy_init_expr_data_recursive(
  930. module, array_init_values);
  931. goto fail;
  932. }
  933. cur_value.data = array_init_values;
  934. }
  935. else {
  936. /* WASM_OP_ARRAY_NEW_FIXED */
  937. uint32 len;
  938. read_leb_uint32(p, p_end, len);
  939. total_size =
  940. (uint64)offsetof(WASMArrayNewInitValues,
  941. elem_data)
  942. + (uint64)sizeof(WASMValue) * len;
  943. if (!(array_init_values =
  944. loader_malloc(total_size, error_buf,
  945. error_buf_size))) {
  946. goto fail;
  947. }
  948. array_init_values->type_idx = type_idx;
  949. array_init_values->length = len;
  950. for (i = len; i > 0; i--) {
  951. if (!pop_const_expr_stack(
  952. &const_expr_ctx, NULL, elem_type,
  953. elem_ref_type, NULL,
  954. &array_init_values
  955. ->elem_data[i - 1],
  956. error_buf, error_buf_size)) {
  957. destroy_init_expr_data_recursive(
  958. module, array_init_values);
  959. goto fail;
  960. }
  961. }
  962. cur_value.data = array_init_values;
  963. }
  964. }
  965. else {
  966. /* WASM_OP_ARRAY_NEW_DEFAULT */
  967. WASMValue len_val;
  968. uint32 len;
  969. /* POP(i32) */
  970. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  971. VALUE_TYPE_I32, NULL,
  972. NULL, &len_val, error_buf,
  973. error_buf_size)) {
  974. goto fail;
  975. }
  976. len = len_val.i32;
  977. cur_value.array_new_default.type_index = type_idx;
  978. cur_value.array_new_default.length = len;
  979. }
  980. wasm_set_refheaptype_typeidx(
  981. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  982. if (!push_const_expr_stack(
  983. &const_expr_ctx, flag, cur_ref_type.ref_type,
  984. &cur_ref_type, (uint8)opcode1, &cur_value,
  985. error_buf, error_buf_size)) {
  986. if (array_init_values) {
  987. destroy_init_expr_data_recursive(
  988. module, array_init_values);
  989. }
  990. goto fail;
  991. }
  992. break;
  993. }
  994. case WASM_OP_ANY_CONVERT_EXTERN:
  995. {
  996. set_error_buf(error_buf, error_buf_size,
  997. "unsupported constant expression of "
  998. "extern.internalize");
  999. goto fail;
  1000. }
  1001. case WASM_OP_EXTERN_CONVERT_ANY:
  1002. {
  1003. set_error_buf(error_buf, error_buf_size,
  1004. "unsupported constant expression of "
  1005. "extern.externalize");
  1006. goto fail;
  1007. }
  1008. case WASM_OP_REF_I31:
  1009. {
  1010. /* POP(i32) */
  1011. if (!pop_const_expr_stack(
  1012. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1013. NULL, &cur_value, error_buf, error_buf_size)) {
  1014. goto fail;
  1015. }
  1016. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1017. false, HEAP_TYPE_I31);
  1018. if (!push_const_expr_stack(
  1019. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1020. &cur_ref_type, (uint8)opcode1, &cur_value,
  1021. error_buf, error_buf_size)) {
  1022. goto fail;
  1023. }
  1024. break;
  1025. }
  1026. default:
  1027. set_error_buf(
  1028. error_buf, error_buf_size,
  1029. "type mismatch or constant expression required");
  1030. goto fail;
  1031. }
  1032. break;
  1033. }
  1034. #endif /* end of WASM_ENABLE_GC != 0 */
  1035. default:
  1036. {
  1037. set_error_buf(error_buf, error_buf_size,
  1038. "illegal opcode "
  1039. "or constant expression required "
  1040. "or type mismatch");
  1041. goto fail;
  1042. }
  1043. }
  1044. CHECK_BUF(p, p_end, 1);
  1045. flag = read_uint8(p);
  1046. }
  1047. /* There should be only one value left on the init value stack */
  1048. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1049. #if WASM_ENABLE_GC != 0
  1050. ref_type, &opcode,
  1051. #endif
  1052. &cur_value, error_buf, error_buf_size)) {
  1053. goto fail;
  1054. }
  1055. if (const_expr_ctx.sp != 0) {
  1056. set_error_buf(error_buf, error_buf_size,
  1057. "type mismatch: illegal constant opcode sequence");
  1058. goto fail;
  1059. }
  1060. init_expr->init_expr_type = flag;
  1061. init_expr->u = cur_value;
  1062. #if WASM_ENABLE_GC != 0
  1063. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1064. switch (opcode) {
  1065. case WASM_OP_STRUCT_NEW:
  1066. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1067. break;
  1068. case WASM_OP_STRUCT_NEW_DEFAULT:
  1069. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1070. break;
  1071. case WASM_OP_ARRAY_NEW:
  1072. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1073. break;
  1074. case WASM_OP_ARRAY_NEW_DEFAULT:
  1075. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1076. break;
  1077. case WASM_OP_ARRAY_NEW_FIXED:
  1078. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1079. break;
  1080. case WASM_OP_REF_I31:
  1081. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1082. break;
  1083. default:
  1084. bh_assert(0);
  1085. break;
  1086. }
  1087. }
  1088. #endif /* end of WASM_ENABLE_GC != 0 */
  1089. *p_buf = p;
  1090. destroy_const_expr_stack(&const_expr_ctx);
  1091. return true;
  1092. fail:
  1093. destroy_const_expr_stack(&const_expr_ctx);
  1094. return false;
  1095. }
  1096. static bool
  1097. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1098. {
  1099. if (mutable >= 2) {
  1100. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1101. return false;
  1102. }
  1103. return true;
  1104. }
  1105. #if WASM_ENABLE_GC != 0
  1106. static void
  1107. destroy_func_type(WASMFuncType *type)
  1108. {
  1109. /* Destroy the reference type hash set */
  1110. if (type->ref_type_maps)
  1111. wasm_runtime_free(type->ref_type_maps);
  1112. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1113. && WASM_ENABLE_LAZY_JIT != 0
  1114. if (type->call_to_llvm_jit_from_fast_jit)
  1115. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1116. #endif
  1117. /* Free the type */
  1118. wasm_runtime_free(type);
  1119. }
  1120. static void
  1121. destroy_struct_type(WASMStructType *type)
  1122. {
  1123. if (type->ref_type_maps)
  1124. wasm_runtime_free(type->ref_type_maps);
  1125. wasm_runtime_free(type);
  1126. }
  1127. static void
  1128. destroy_array_type(WASMArrayType *type)
  1129. {
  1130. wasm_runtime_free(type);
  1131. }
  1132. static void
  1133. destroy_wasm_type(WASMType *type)
  1134. {
  1135. if (type->ref_count > 1) {
  1136. /* The type is referenced by other types
  1137. of current wasm module */
  1138. type->ref_count--;
  1139. return;
  1140. }
  1141. if (type->type_flag == WASM_TYPE_FUNC)
  1142. destroy_func_type((WASMFuncType *)type);
  1143. else if (type->type_flag == WASM_TYPE_STRUCT)
  1144. destroy_struct_type((WASMStructType *)type);
  1145. else if (type->type_flag == WASM_TYPE_ARRAY)
  1146. destroy_array_type((WASMArrayType *)type);
  1147. else {
  1148. bh_assert(0);
  1149. }
  1150. }
  1151. /* Resolve (ref null ht) or (ref ht) */
  1152. static bool
  1153. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1154. WASMModule *module, uint32 type_count, bool nullable,
  1155. WASMRefType *ref_type, char *error_buf,
  1156. uint32 error_buf_size)
  1157. {
  1158. const uint8 *p = *p_buf, *p_end = buf_end;
  1159. ref_type->ref_type =
  1160. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1161. ref_type->ref_ht_common.nullable = nullable;
  1162. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1163. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1164. /* heap type is (type i), i : typeidx, >= 0 */
  1165. if (!check_type_index(module, type_count,
  1166. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1167. error_buf_size)) {
  1168. return false;
  1169. }
  1170. }
  1171. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1172. /* heap type is func, extern, any, eq, i31 or data */
  1173. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1174. return false;
  1175. }
  1176. *p_buf = p;
  1177. return true;
  1178. fail:
  1179. return false;
  1180. }
  1181. static bool
  1182. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1183. WASMModule *module, uint32 type_count,
  1184. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1185. bool allow_packed_type, char *error_buf,
  1186. uint32 error_buf_size)
  1187. {
  1188. const uint8 *p = *p_buf, *p_end = buf_end;
  1189. uint8 type;
  1190. memset(ref_type, 0, sizeof(WASMRefType));
  1191. CHECK_BUF(p, p_end, 1);
  1192. type = read_uint8(p);
  1193. if (wasm_is_reftype_htref_nullable(type)) {
  1194. /* (ref null ht) */
  1195. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1196. ref_type, error_buf, error_buf_size))
  1197. return false;
  1198. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1199. *p_need_ref_type_map = true;
  1200. else {
  1201. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1202. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1203. multi-byte type to one-byte type to reduce the footprint and
  1204. the complexity of type equal/subtype checking */
  1205. ref_type->ref_type =
  1206. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1207. *p_need_ref_type_map = false;
  1208. }
  1209. }
  1210. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1211. /* (ref ht) */
  1212. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1213. ref_type, error_buf, error_buf_size))
  1214. return false;
  1215. *p_need_ref_type_map = true;
  1216. #if WASM_ENABLE_STRINGREF != 0
  1217. /* covert (ref string) to stringref */
  1218. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1219. ref_type->ref_type =
  1220. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1221. *p_need_ref_type_map = false;
  1222. }
  1223. #endif
  1224. }
  1225. else {
  1226. /* type which can be represented by one byte */
  1227. if (!is_valid_value_type_for_interpreter(type)
  1228. && !(allow_packed_type && is_packed_type(type))) {
  1229. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1230. return false;
  1231. }
  1232. ref_type->ref_type = type;
  1233. *p_need_ref_type_map = false;
  1234. #if WASM_ENABLE_WAMR_COMPILER != 0
  1235. /* If any value's type is v128, mark the module as SIMD used */
  1236. if (type == VALUE_TYPE_V128)
  1237. module->is_simd_used = true;
  1238. #endif
  1239. }
  1240. *p_buf = p;
  1241. return true;
  1242. fail:
  1243. return false;
  1244. }
  1245. static WASMRefType *
  1246. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1247. char *error_buf, uint32 error_buf_size)
  1248. {
  1249. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1250. if (!ret) {
  1251. set_error_buf(error_buf, error_buf_size,
  1252. "insert ref type to hash set failed");
  1253. }
  1254. return ret;
  1255. }
  1256. static bool
  1257. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1258. uint32 type_count, uint32 type_idx, char *error_buf,
  1259. uint32 error_buf_size)
  1260. {
  1261. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1262. uint32 param_count, result_count, i, j = 0;
  1263. uint32 param_cell_num, ret_cell_num;
  1264. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1265. uint64 total_size;
  1266. bool need_ref_type_map;
  1267. WASMRefType ref_type;
  1268. WASMFuncType *type = NULL;
  1269. /* Parse first time to resolve param count, result count and
  1270. ref type map count */
  1271. read_leb_uint32(p, p_end, param_count);
  1272. p_org = p;
  1273. for (i = 0; i < param_count; i++) {
  1274. if (!resolve_value_type(&p, p_end, module, type_count,
  1275. &need_ref_type_map, &ref_type, false, error_buf,
  1276. error_buf_size)) {
  1277. return false;
  1278. }
  1279. if (need_ref_type_map)
  1280. ref_type_map_count++;
  1281. }
  1282. read_leb_uint32(p, p_end, result_count);
  1283. for (i = 0; i < result_count; i++) {
  1284. if (!resolve_value_type(&p, p_end, module, type_count,
  1285. &need_ref_type_map, &ref_type, false, error_buf,
  1286. error_buf_size)) {
  1287. return false;
  1288. }
  1289. if (need_ref_type_map) {
  1290. ref_type_map_count++;
  1291. result_ref_type_map_count++;
  1292. }
  1293. }
  1294. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1295. "ref type map count: %d",
  1296. type_idx, param_count, result_count, ref_type_map_count);
  1297. /* Parse second time to resolve param types, result types and
  1298. ref type map info */
  1299. p = p_org;
  1300. total_size = offsetof(WASMFuncType, types)
  1301. + sizeof(uint8) * (uint64)(param_count + result_count);
  1302. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1303. return false;
  1304. }
  1305. if (ref_type_map_count > 0) {
  1306. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1307. if (!(type->ref_type_maps =
  1308. loader_malloc(total_size, error_buf, error_buf_size))) {
  1309. goto fail;
  1310. }
  1311. }
  1312. type->base_type.type_flag = WASM_TYPE_FUNC;
  1313. type->param_count = param_count;
  1314. type->result_count = result_count;
  1315. type->ref_type_map_count = ref_type_map_count;
  1316. if (ref_type_map_count > 0) {
  1317. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1318. - result_ref_type_map_count;
  1319. }
  1320. for (i = 0; i < param_count; i++) {
  1321. if (!resolve_value_type(&p, p_end, module, type_count,
  1322. &need_ref_type_map, &ref_type, false, error_buf,
  1323. error_buf_size)) {
  1324. goto fail;
  1325. }
  1326. type->types[i] = ref_type.ref_type;
  1327. if (need_ref_type_map) {
  1328. type->ref_type_maps[j].index = i;
  1329. if (!(type->ref_type_maps[j++].ref_type =
  1330. reftype_set_insert(module->ref_type_set, &ref_type,
  1331. error_buf, error_buf_size))) {
  1332. goto fail;
  1333. }
  1334. }
  1335. }
  1336. read_leb_uint32(p, p_end, result_count);
  1337. for (i = 0; i < result_count; i++) {
  1338. if (!resolve_value_type(&p, p_end, module, type_count,
  1339. &need_ref_type_map, &ref_type, false, error_buf,
  1340. error_buf_size)) {
  1341. goto fail;
  1342. }
  1343. type->types[param_count + i] = ref_type.ref_type;
  1344. if (need_ref_type_map) {
  1345. type->ref_type_maps[j].index = param_count + i;
  1346. if (!(type->ref_type_maps[j++].ref_type =
  1347. reftype_set_insert(module->ref_type_set, &ref_type,
  1348. error_buf, error_buf_size))) {
  1349. goto fail;
  1350. }
  1351. }
  1352. }
  1353. bh_assert(j == type->ref_type_map_count);
  1354. #if TRACE_WASM_LOADER != 0
  1355. os_printf("type %d = ", type_idx);
  1356. wasm_dump_func_type(type);
  1357. #endif
  1358. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1359. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1360. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1361. set_error_buf(error_buf, error_buf_size,
  1362. "param count or result count too large");
  1363. goto fail;
  1364. }
  1365. type->param_cell_num = (uint16)param_cell_num;
  1366. type->ret_cell_num = (uint16)ret_cell_num;
  1367. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1368. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1369. #endif
  1370. #if WASM_ENABLE_WAMR_COMPILER != 0
  1371. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1372. if (type->types[i] == VALUE_TYPE_V128)
  1373. module->is_simd_used = true;
  1374. }
  1375. #endif
  1376. *p_buf = p;
  1377. module->types[type_idx] = (WASMType *)type;
  1378. return true;
  1379. fail:
  1380. if (type)
  1381. destroy_func_type(type);
  1382. return false;
  1383. }
  1384. static bool
  1385. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1386. WASMModule *module, uint32 type_count, uint32 type_idx,
  1387. char *error_buf, uint32 error_buf_size)
  1388. {
  1389. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1390. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1391. uint32 i, j = 0, offset;
  1392. uint16 *reference_table;
  1393. uint64 total_size;
  1394. uint8 mutable;
  1395. bool need_ref_type_map;
  1396. WASMRefType ref_type;
  1397. WASMStructType *type = NULL;
  1398. /* Parse first time to resolve field count and ref type map count */
  1399. read_leb_uint32(p, p_end, field_count);
  1400. p_org = p;
  1401. for (i = 0; i < field_count; i++) {
  1402. if (!resolve_value_type(&p, p_end, module, type_count,
  1403. &need_ref_type_map, &ref_type, true, error_buf,
  1404. error_buf_size)) {
  1405. return false;
  1406. }
  1407. if (need_ref_type_map)
  1408. ref_type_map_count++;
  1409. if (wasm_is_type_reftype(ref_type.ref_type))
  1410. ref_field_count++;
  1411. CHECK_BUF(p, p_end, 1);
  1412. mutable = read_uint8(p);
  1413. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1414. return false;
  1415. }
  1416. }
  1417. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1418. type_idx, field_count, ref_type_map_count);
  1419. /* Parse second time to resolve field types and ref type map info */
  1420. p = p_org;
  1421. total_size = offsetof(WASMStructType, fields)
  1422. + sizeof(WASMStructFieldType) * (uint64)field_count
  1423. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1424. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1425. return false;
  1426. }
  1427. if (ref_type_map_count > 0) {
  1428. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1429. if (!(type->ref_type_maps =
  1430. loader_malloc(total_size, error_buf, error_buf_size))) {
  1431. goto fail;
  1432. }
  1433. }
  1434. type->reference_table = reference_table =
  1435. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1436. + sizeof(WASMStructFieldType) * field_count);
  1437. *reference_table++ = ref_field_count;
  1438. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1439. type->field_count = field_count;
  1440. type->ref_type_map_count = ref_type_map_count;
  1441. offset = (uint32)offsetof(WASMStructObject, field_data);
  1442. for (i = 0; i < field_count; i++) {
  1443. if (!resolve_value_type(&p, p_end, module, type_count,
  1444. &need_ref_type_map, &ref_type, true, error_buf,
  1445. error_buf_size)) {
  1446. goto fail;
  1447. }
  1448. type->fields[i].field_type = ref_type.ref_type;
  1449. if (need_ref_type_map) {
  1450. type->ref_type_maps[j].index = i;
  1451. if (!(type->ref_type_maps[j++].ref_type =
  1452. reftype_set_insert(module->ref_type_set, &ref_type,
  1453. error_buf, error_buf_size))) {
  1454. goto fail;
  1455. }
  1456. }
  1457. CHECK_BUF(p, p_end, 1);
  1458. type->fields[i].field_flags = read_uint8(p);
  1459. type->fields[i].field_size =
  1460. (uint8)wasm_reftype_size(ref_type.ref_type);
  1461. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1462. || defined(BUILD_TARGET_X86_32))
  1463. if (type->fields[i].field_size == 2)
  1464. offset = align_uint(offset, 2);
  1465. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1466. offset = align_uint(offset, 4);
  1467. #endif
  1468. type->fields[i].field_offset = offset;
  1469. if (wasm_is_type_reftype(ref_type.ref_type))
  1470. *reference_table++ = offset;
  1471. offset += type->fields[i].field_size;
  1472. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1473. type->fields[i].field_flags, type->fields[i].field_type);
  1474. }
  1475. type->total_size = offset;
  1476. bh_assert(j == type->ref_type_map_count);
  1477. #if TRACE_WASM_LOADER != 0
  1478. os_printf("type %d = ", type_idx);
  1479. wasm_dump_struct_type(type);
  1480. #endif
  1481. *p_buf = p;
  1482. module->types[type_idx] = (WASMType *)type;
  1483. return true;
  1484. fail:
  1485. if (type)
  1486. destroy_struct_type(type);
  1487. return false;
  1488. }
  1489. static bool
  1490. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1491. WASMModule *module, uint32 type_count, uint32 type_idx,
  1492. char *error_buf, uint32 error_buf_size)
  1493. {
  1494. const uint8 *p = *p_buf, *p_end = buf_end;
  1495. uint8 mutable;
  1496. bool need_ref_type_map;
  1497. WASMRefType ref_type;
  1498. WASMArrayType *type = NULL;
  1499. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1500. &ref_type, true, error_buf, error_buf_size)) {
  1501. return false;
  1502. }
  1503. CHECK_BUF(p, p_end, 1);
  1504. mutable = read_uint8(p);
  1505. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1506. return false;
  1507. }
  1508. LOG_VERBOSE("type %u: array", type_idx);
  1509. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1510. error_buf_size))) {
  1511. return false;
  1512. }
  1513. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1514. type->elem_flags = mutable;
  1515. type->elem_type = ref_type.ref_type;
  1516. if (need_ref_type_map) {
  1517. if (!(type->elem_ref_type =
  1518. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1519. error_buf_size))) {
  1520. goto fail;
  1521. }
  1522. }
  1523. #if TRACE_WASM_LOADER != 0
  1524. os_printf("type %d = ", type_idx);
  1525. wasm_dump_array_type(type);
  1526. #endif
  1527. *p_buf = p;
  1528. module->types[type_idx] = (WASMType *)type;
  1529. return true;
  1530. fail:
  1531. if (type)
  1532. destroy_array_type(type);
  1533. return false;
  1534. }
  1535. static bool
  1536. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1537. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1538. {
  1539. if (heap_type >= 0) {
  1540. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1541. error_buf_size)) {
  1542. return false;
  1543. }
  1544. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1545. heap_type);
  1546. }
  1547. else {
  1548. if (!wasm_is_valid_heap_type(heap_type)) {
  1549. set_error_buf(error_buf, error_buf_size, "unknown type");
  1550. return false;
  1551. }
  1552. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1553. heap_type);
  1554. if (nullable) {
  1555. /* For (ref null func/extern/any/eq/i31/data),
  1556. they are same as
  1557. funcref/externref/anyref/eqref/i31ref/dataref,
  1558. we convert the multi-byte type to one-byte
  1559. type to reduce the footprint and the
  1560. complexity of type equal/subtype checking */
  1561. ref_type->ref_type =
  1562. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1563. }
  1564. }
  1565. return true;
  1566. }
  1567. static void
  1568. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1569. WASMRefType *ref_type2)
  1570. {
  1571. /**
  1572. * The difference rt1 ∖ rt2 between two reference types is defined as
  1573. * follows:
  1574. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1575. * (ref ht2) = (ref null?1 ht1)
  1576. */
  1577. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1578. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1579. ref_type1, wasm_reftype_struct_size(ref_type1));
  1580. }
  1581. else {
  1582. ref_type_diff->ref_type = ref_type1->ref_type;
  1583. }
  1584. if (ref_type2->ref_ht_common.nullable) {
  1585. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1586. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1587. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1588. (int32)ref_type_diff->ref_type - 0x80);
  1589. }
  1590. else {
  1591. ref_type_diff->ref_ht_typeidx.nullable = false;
  1592. }
  1593. }
  1594. }
  1595. #else /* else of WASM_ENABLE_GC != 0 */
  1596. static void
  1597. destroy_wasm_type(WASMType *type)
  1598. {
  1599. if (type->ref_count > 1) {
  1600. /* The type is referenced by other types
  1601. of current wasm module */
  1602. type->ref_count--;
  1603. return;
  1604. }
  1605. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1606. && WASM_ENABLE_LAZY_JIT != 0
  1607. if (type->call_to_llvm_jit_from_fast_jit)
  1608. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1609. #endif
  1610. wasm_runtime_free(type);
  1611. }
  1612. #endif /* end of WASM_ENABLE_GC != 0 */
  1613. static bool
  1614. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1615. char *error_buf, uint32 error_buf_size)
  1616. {
  1617. const uint8 *p = buf, *p_end = buf_end;
  1618. uint32 type_count, i;
  1619. uint64 total_size;
  1620. uint8 flag;
  1621. #if WASM_ENABLE_GC != 0
  1622. uint32 processed_type_count = 0;
  1623. #endif
  1624. read_leb_uint32(p, p_end, type_count);
  1625. if (type_count) {
  1626. module->type_count = type_count;
  1627. total_size = sizeof(WASMType *) * (uint64)type_count;
  1628. if (!(module->types =
  1629. loader_malloc(total_size, error_buf, error_buf_size))) {
  1630. return false;
  1631. }
  1632. #if WASM_ENABLE_GC == 0
  1633. for (i = 0; i < type_count; i++) {
  1634. WASMFuncType *type;
  1635. const uint8 *p_org;
  1636. uint32 param_count, result_count, j;
  1637. uint32 param_cell_num, ret_cell_num;
  1638. CHECK_BUF(p, p_end, 1);
  1639. flag = read_uint8(p);
  1640. if (flag != 0x60) {
  1641. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1642. return false;
  1643. }
  1644. read_leb_uint32(p, p_end, param_count);
  1645. /* Resolve param count and result count firstly */
  1646. p_org = p;
  1647. CHECK_BUF(p, p_end, param_count);
  1648. p += param_count;
  1649. read_leb_uint32(p, p_end, result_count);
  1650. CHECK_BUF(p, p_end, result_count);
  1651. p = p_org;
  1652. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1653. set_error_buf(error_buf, error_buf_size,
  1654. "param count or result count too large");
  1655. return false;
  1656. }
  1657. total_size = offsetof(WASMFuncType, types)
  1658. + sizeof(uint8) * (uint64)(param_count + result_count);
  1659. if (!(type = module->types[i] =
  1660. loader_malloc(total_size, error_buf, error_buf_size))) {
  1661. return false;
  1662. }
  1663. /* Resolve param types and result types */
  1664. type->ref_count = 1;
  1665. type->param_count = (uint16)param_count;
  1666. type->result_count = (uint16)result_count;
  1667. for (j = 0; j < param_count; j++) {
  1668. CHECK_BUF(p, p_end, 1);
  1669. type->types[j] = read_uint8(p);
  1670. }
  1671. read_leb_uint32(p, p_end, result_count);
  1672. for (j = 0; j < result_count; j++) {
  1673. CHECK_BUF(p, p_end, 1);
  1674. type->types[param_count + j] = read_uint8(p);
  1675. }
  1676. for (j = 0; j < param_count + result_count; j++) {
  1677. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1678. set_error_buf(error_buf, error_buf_size,
  1679. "unknown value type");
  1680. return false;
  1681. }
  1682. }
  1683. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1684. ret_cell_num =
  1685. wasm_get_cell_num(type->types + param_count, result_count);
  1686. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1687. set_error_buf(error_buf, error_buf_size,
  1688. "param count or result count too large");
  1689. return false;
  1690. }
  1691. type->param_cell_num = (uint16)param_cell_num;
  1692. type->ret_cell_num = (uint16)ret_cell_num;
  1693. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1694. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1695. #endif
  1696. #if WASM_ENABLE_WAMR_COMPILER != 0
  1697. for (j = 0; j < type->param_count + type->result_count; j++) {
  1698. if (type->types[j] == VALUE_TYPE_V128)
  1699. module->is_simd_used = true;
  1700. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1701. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1702. module->is_ref_types_used = true;
  1703. }
  1704. #endif
  1705. /* If there is already a same type created, use it instead */
  1706. for (j = 0; j < i; j++) {
  1707. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1708. if (module->types[j]->ref_count == UINT16_MAX) {
  1709. set_error_buf(error_buf, error_buf_size,
  1710. "wasm type's ref count too large");
  1711. return false;
  1712. }
  1713. destroy_wasm_type(type);
  1714. module->types[i] = module->types[j];
  1715. module->types[j]->ref_count++;
  1716. break;
  1717. }
  1718. }
  1719. }
  1720. #else /* else of WASM_ENABLE_GC == 0 */
  1721. for (i = 0; i < type_count; i++) {
  1722. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1723. uint32 rec_count = 1, j;
  1724. bool is_sub_final = true;
  1725. CHECK_BUF(p, p_end, 1);
  1726. flag = read_uint8(p);
  1727. if (flag == DEFINED_TYPE_REC) {
  1728. read_leb_uint32(p, p_end, rec_count);
  1729. if (rec_count > 1) {
  1730. uint64 new_total_size;
  1731. /* integer overflow */
  1732. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1733. set_error_buf(error_buf, error_buf_size,
  1734. "recursive type count too large");
  1735. return false;
  1736. }
  1737. module->type_count += rec_count - 1;
  1738. new_total_size =
  1739. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1740. if (new_total_size > UINT32_MAX) {
  1741. set_error_buf(error_buf, error_buf_size,
  1742. "allocate memory failed");
  1743. return false;
  1744. }
  1745. MEM_REALLOC(module->types, (uint32)total_size,
  1746. (uint32)new_total_size);
  1747. total_size = new_total_size;
  1748. }
  1749. LOG_VERBOSE("Processing rec group [%d-%d]",
  1750. processed_type_count,
  1751. processed_type_count + rec_count - 1);
  1752. }
  1753. else {
  1754. p--;
  1755. }
  1756. for (j = 0; j < rec_count; j++) {
  1757. WASMType *cur_type = NULL;
  1758. CHECK_BUF(p, p_end, 1);
  1759. flag = read_uint8(p);
  1760. parent_type_idx = -1;
  1761. if (flag == DEFINED_TYPE_SUB
  1762. || flag == DEFINED_TYPE_SUB_FINAL) {
  1763. read_leb_uint32(p, p_end, super_type_count);
  1764. if (super_type_count > 1) {
  1765. set_error_buf(error_buf, error_buf_size,
  1766. "super type count too large");
  1767. return false;
  1768. }
  1769. if (super_type_count > 0) {
  1770. read_leb_uint32(p, p_end, parent_type_idx);
  1771. if (parent_type_idx >= processed_type_count + j) {
  1772. set_error_buf_v(error_buf, error_buf_size,
  1773. "unknown type %d", parent_type_idx);
  1774. return false;
  1775. }
  1776. if (module->types[parent_type_idx]->is_sub_final) {
  1777. set_error_buf(error_buf, error_buf_size,
  1778. "sub type can not inherit from "
  1779. "a final super type");
  1780. return false;
  1781. }
  1782. }
  1783. if (flag == DEFINED_TYPE_SUB)
  1784. is_sub_final = false;
  1785. CHECK_BUF(p, p_end, 1);
  1786. flag = read_uint8(p);
  1787. }
  1788. if (flag == DEFINED_TYPE_FUNC) {
  1789. if (!resolve_func_type(&p, buf_end, module,
  1790. processed_type_count + rec_count,
  1791. processed_type_count + j, error_buf,
  1792. error_buf_size)) {
  1793. return false;
  1794. }
  1795. }
  1796. else if (flag == DEFINED_TYPE_STRUCT) {
  1797. if (!resolve_struct_type(&p, buf_end, module,
  1798. processed_type_count + rec_count,
  1799. processed_type_count + j,
  1800. error_buf, error_buf_size)) {
  1801. return false;
  1802. }
  1803. }
  1804. else if (flag == DEFINED_TYPE_ARRAY) {
  1805. if (!resolve_array_type(&p, buf_end, module,
  1806. processed_type_count + rec_count,
  1807. processed_type_count + j, error_buf,
  1808. error_buf_size)) {
  1809. return false;
  1810. }
  1811. }
  1812. else {
  1813. set_error_buf(error_buf, error_buf_size,
  1814. "invalid type flag");
  1815. return false;
  1816. }
  1817. cur_type = module->types[processed_type_count + j];
  1818. cur_type->ref_count = 1;
  1819. cur_type->parent_type_idx = parent_type_idx;
  1820. cur_type->is_sub_final = is_sub_final;
  1821. cur_type->rec_count = rec_count;
  1822. cur_type->rec_idx = j;
  1823. cur_type->rec_begin_type_idx = processed_type_count;
  1824. }
  1825. /* resolve subtyping relationship in current rec group */
  1826. for (j = 0; j < rec_count; j++) {
  1827. WASMType *cur_type = module->types[processed_type_count + j];
  1828. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1829. WASMType *parent_type =
  1830. module->types[cur_type->parent_type_idx];
  1831. cur_type->parent_type = parent_type;
  1832. cur_type->root_type = parent_type->root_type;
  1833. if (parent_type->inherit_depth == UINT16_MAX) {
  1834. set_error_buf(error_buf, error_buf_size,
  1835. "parent type's inherit depth too large");
  1836. return false;
  1837. }
  1838. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1839. }
  1840. else {
  1841. cur_type->parent_type = NULL;
  1842. cur_type->root_type = cur_type;
  1843. cur_type->inherit_depth = 0;
  1844. }
  1845. }
  1846. for (j = 0; j < rec_count; j++) {
  1847. WASMType *cur_type = module->types[processed_type_count + j];
  1848. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1849. WASMType *parent_type =
  1850. module->types[cur_type->parent_type_idx];
  1851. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1852. module->types,
  1853. module->type_count)) {
  1854. set_error_buf(error_buf, error_buf_size,
  1855. "sub type does not match super type");
  1856. return false;
  1857. }
  1858. }
  1859. }
  1860. /* If there is already an equivalence type or a group of equivalence
  1861. recursive types created, use it or them instead */
  1862. for (j = 0; j < processed_type_count;) {
  1863. WASMType *src_type = module->types[j];
  1864. WASMType *cur_type = module->types[processed_type_count];
  1865. uint32 k, src_rec_count;
  1866. src_rec_count = src_type->rec_count;
  1867. if (src_rec_count != rec_count) {
  1868. /* no type equivalence */
  1869. j += src_rec_count;
  1870. continue;
  1871. }
  1872. for (k = 0; k < rec_count; k++) {
  1873. src_type = module->types[j + k];
  1874. cur_type = module->types[processed_type_count + k];
  1875. if (!wasm_type_equal(src_type, cur_type, module->types,
  1876. module->type_count)) {
  1877. break;
  1878. }
  1879. }
  1880. if (k < rec_count) {
  1881. /* no type equivalence */
  1882. j += src_rec_count;
  1883. continue;
  1884. }
  1885. /* type equivalence */
  1886. for (k = 0; k < rec_count; k++) {
  1887. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1888. set_error_buf(error_buf, error_buf_size,
  1889. "wasm type's ref count too large");
  1890. return false;
  1891. }
  1892. destroy_wasm_type(module->types[processed_type_count + k]);
  1893. module->types[processed_type_count + k] =
  1894. module->types[j + k];
  1895. module->types[j + k]->ref_count++;
  1896. }
  1897. break;
  1898. }
  1899. if (rec_count > 1) {
  1900. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1901. processed_type_count,
  1902. processed_type_count + rec_count - 1);
  1903. }
  1904. processed_type_count += rec_count;
  1905. }
  1906. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1907. * module->type_count,
  1908. error_buf, error_buf_size))) {
  1909. return false;
  1910. }
  1911. #endif /* end of WASM_ENABLE_GC == 0 */
  1912. }
  1913. if (p != p_end) {
  1914. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1915. return false;
  1916. }
  1917. LOG_VERBOSE("Load type section success.\n");
  1918. return true;
  1919. fail:
  1920. return false;
  1921. }
  1922. static void
  1923. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1924. {
  1925. uint32 default_max_size;
  1926. if (UINT32_MAX / 2 > init_size)
  1927. default_max_size = init_size * 2;
  1928. else
  1929. default_max_size = UINT32_MAX;
  1930. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1931. default_max_size = WASM_TABLE_MAX_SIZE;
  1932. if (max_size_flag) {
  1933. /* module defines the table limitation */
  1934. bh_assert(init_size <= *max_size);
  1935. if (init_size < *max_size) {
  1936. *max_size =
  1937. *max_size < default_max_size ? *max_size : default_max_size;
  1938. }
  1939. }
  1940. else {
  1941. /* partial defined table limitation, gives a default value */
  1942. *max_size = default_max_size;
  1943. }
  1944. }
  1945. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1946. /**
  1947. * Find export item of a module with export info:
  1948. * module name, field name and export kind
  1949. */
  1950. static WASMExport *
  1951. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1952. const char *field_name, uint8 export_kind,
  1953. char *error_buf, uint32 error_buf_size)
  1954. {
  1955. WASMExport *export =
  1956. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1957. export_kind, error_buf, error_buf_size);
  1958. return export;
  1959. }
  1960. #endif
  1961. #if WASM_ENABLE_MULTI_MODULE != 0
  1962. static WASMFunction *
  1963. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1964. const WASMFuncType *expected_function_type,
  1965. char *error_buf, uint32 error_buf_size)
  1966. {
  1967. WASMModuleCommon *module_reg;
  1968. WASMFunction *function = NULL;
  1969. WASMExport *export = NULL;
  1970. WASMModule *module = NULL;
  1971. WASMFuncType *target_function_type = NULL;
  1972. module_reg = wasm_runtime_find_module_registered(module_name);
  1973. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1974. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  1975. function_name);
  1976. set_error_buf(error_buf, error_buf_size, "unknown import");
  1977. return NULL;
  1978. }
  1979. module = (WASMModule *)module_reg;
  1980. export =
  1981. wasm_loader_find_export(module, module_name, function_name,
  1982. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  1983. if (!export) {
  1984. return NULL;
  1985. }
  1986. /* resolve function type and function */
  1987. if (export->index < module->import_function_count) {
  1988. target_function_type =
  1989. module->import_functions[export->index].u.function.func_type;
  1990. function = module->import_functions[export->index]
  1991. .u.function.import_func_linked;
  1992. }
  1993. else {
  1994. target_function_type =
  1995. module->functions[export->index - module->import_function_count]
  1996. ->func_type;
  1997. function =
  1998. module->functions[export->index - module->import_function_count];
  1999. }
  2000. /* check function type */
  2001. if (!wasm_type_equal((WASMType *)expected_function_type,
  2002. (WASMType *)target_function_type, module->types,
  2003. module->type_count)) {
  2004. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2005. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2006. return NULL;
  2007. }
  2008. return function;
  2009. }
  2010. static WASMTable *
  2011. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2012. uint32 init_size, uint32 max_size, char *error_buf,
  2013. uint32 error_buf_size)
  2014. {
  2015. WASMModuleCommon *module_reg;
  2016. WASMTable *table = NULL;
  2017. WASMExport *export = NULL;
  2018. WASMModule *module = NULL;
  2019. module_reg = wasm_runtime_find_module_registered(module_name);
  2020. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2021. LOG_DEBUG("can not find a module named %s for table", module_name);
  2022. set_error_buf(error_buf, error_buf_size, "unknown import");
  2023. return NULL;
  2024. }
  2025. module = (WASMModule *)module_reg;
  2026. export =
  2027. wasm_loader_find_export(module, module_name, table_name,
  2028. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2029. if (!export) {
  2030. return NULL;
  2031. }
  2032. /* resolve table and check the init/max size */
  2033. if (export->index < module->import_table_count) {
  2034. table =
  2035. module->import_tables[export->index].u.table.import_table_linked;
  2036. }
  2037. else {
  2038. table = &(module->tables[export->index - module->import_table_count]);
  2039. }
  2040. if (table->table_type.init_size < init_size
  2041. || table->table_type.max_size > max_size) {
  2042. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2043. module_name, table_name, table->table_type.init_size,
  2044. table->table_type.max_size, init_size, max_size);
  2045. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2046. return NULL;
  2047. }
  2048. return table;
  2049. }
  2050. static WASMMemory *
  2051. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2052. uint32 init_page_count, uint32 max_page_count,
  2053. char *error_buf, uint32 error_buf_size)
  2054. {
  2055. WASMModuleCommon *module_reg;
  2056. WASMMemory *memory = NULL;
  2057. WASMExport *export = NULL;
  2058. WASMModule *module = NULL;
  2059. module_reg = wasm_runtime_find_module_registered(module_name);
  2060. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2061. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2062. set_error_buf(error_buf, error_buf_size, "unknown import");
  2063. return NULL;
  2064. }
  2065. module = (WASMModule *)module_reg;
  2066. export =
  2067. wasm_loader_find_export(module, module_name, memory_name,
  2068. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2069. if (!export) {
  2070. return NULL;
  2071. }
  2072. /* resolve memory and check the init/max page count */
  2073. if (export->index < module->import_memory_count) {
  2074. memory = module->import_memories[export->index]
  2075. .u.memory.import_memory_linked;
  2076. }
  2077. else {
  2078. memory =
  2079. &(module->memories[export->index - module->import_memory_count]);
  2080. }
  2081. if (memory->init_page_count < init_page_count
  2082. || memory->max_page_count > max_page_count) {
  2083. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2084. module_name, memory_name, memory->init_page_count,
  2085. memory->max_page_count, init_page_count, max_page_count);
  2086. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2087. return NULL;
  2088. }
  2089. return memory;
  2090. }
  2091. static WASMGlobal *
  2092. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2093. uint8 type, bool is_mutable, char *error_buf,
  2094. uint32 error_buf_size)
  2095. {
  2096. WASMModuleCommon *module_reg;
  2097. WASMGlobal *global = NULL;
  2098. WASMExport *export = NULL;
  2099. WASMModule *module = NULL;
  2100. module_reg = wasm_runtime_find_module_registered(module_name);
  2101. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2102. LOG_DEBUG("can not find a module named %s for global", module_name);
  2103. set_error_buf(error_buf, error_buf_size, "unknown import");
  2104. return NULL;
  2105. }
  2106. module = (WASMModule *)module_reg;
  2107. export =
  2108. wasm_loader_find_export(module, module_name, global_name,
  2109. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2110. if (!export) {
  2111. return NULL;
  2112. }
  2113. /* resolve and check the global */
  2114. if (export->index < module->import_global_count) {
  2115. global =
  2116. module->import_globals[export->index].u.global.import_global_linked;
  2117. }
  2118. else {
  2119. global =
  2120. &(module->globals[export->index - module->import_global_count]);
  2121. }
  2122. if (global->type.val_type != type
  2123. || global->type.is_mutable != is_mutable) {
  2124. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2125. module_name, global_name, global->type.val_type,
  2126. global->type.is_mutable, type, is_mutable);
  2127. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2128. return NULL;
  2129. }
  2130. return global;
  2131. }
  2132. #if WASM_ENABLE_TAGS != 0
  2133. static WASMTag *
  2134. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2135. const WASMType *expected_tag_type,
  2136. uint32 *linked_tag_index, char *error_buf,
  2137. uint32 error_buf_size)
  2138. {
  2139. WASMModuleCommon *module_reg;
  2140. WASMTag *tag = NULL;
  2141. WASMExport *export = NULL;
  2142. WASMModule *module = NULL;
  2143. module_reg = wasm_runtime_find_module_registered(module_name);
  2144. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2145. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2146. tag_name);
  2147. set_error_buf(error_buf, error_buf_size, "unknown import");
  2148. return NULL;
  2149. }
  2150. module = (WASMModule *)module_reg;
  2151. export =
  2152. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2153. error_buf, error_buf_size);
  2154. if (!export) {
  2155. return NULL;
  2156. }
  2157. /* resolve tag type and tag */
  2158. if (export->index < module->import_tag_count) {
  2159. /* importing an imported tag from the submodule */
  2160. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2161. }
  2162. else {
  2163. /* importing an section tag from the submodule */
  2164. tag = module->tags[export->index - module->import_tag_count];
  2165. }
  2166. /* check function type */
  2167. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2168. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2169. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2170. return NULL;
  2171. }
  2172. if (linked_tag_index != NULL) {
  2173. *linked_tag_index = export->index;
  2174. }
  2175. return tag;
  2176. }
  2177. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2178. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2179. static bool
  2180. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2181. const WASMModule *parent_module,
  2182. const char *sub_module_name, const char *function_name,
  2183. WASMFunctionImport *function, char *error_buf,
  2184. uint32 error_buf_size)
  2185. {
  2186. const uint8 *p = *p_buf, *p_end = buf_end;
  2187. uint32 declare_type_index = 0;
  2188. WASMFuncType *declare_func_type = NULL;
  2189. WASMFunction *linked_func = NULL;
  2190. #if WASM_ENABLE_MULTI_MODULE != 0
  2191. WASMModule *sub_module = NULL;
  2192. bool is_built_in_module = false;
  2193. #endif
  2194. const char *linked_signature = NULL;
  2195. void *linked_attachment = NULL;
  2196. bool linked_call_conv_raw = false;
  2197. bool is_native_symbol = false;
  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. declare_func_type =
  2212. (WASMFuncType *)parent_module->types[declare_type_index];
  2213. /* lookup registered native symbols first */
  2214. linked_func = wasm_native_resolve_symbol(
  2215. sub_module_name, function_name, declare_func_type, &linked_signature,
  2216. &linked_attachment, &linked_call_conv_raw);
  2217. if (linked_func) {
  2218. is_native_symbol = true;
  2219. }
  2220. #if WASM_ENABLE_MULTI_MODULE != 0
  2221. else {
  2222. if (!(is_built_in_module =
  2223. wasm_runtime_is_built_in_module(sub_module_name))) {
  2224. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2225. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2226. error_buf_size);
  2227. }
  2228. if (is_built_in_module || sub_module)
  2229. linked_func = wasm_loader_resolve_function(
  2230. sub_module_name, function_name, declare_func_type, error_buf,
  2231. error_buf_size);
  2232. }
  2233. #endif
  2234. function->module_name = (char *)sub_module_name;
  2235. function->field_name = (char *)function_name;
  2236. function->func_type = declare_func_type;
  2237. /* func_ptr_linked is for native registered symbol */
  2238. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2239. function->signature = linked_signature;
  2240. function->attachment = linked_attachment;
  2241. function->call_conv_raw = linked_call_conv_raw;
  2242. #if WASM_ENABLE_MULTI_MODULE != 0
  2243. function->import_module = is_native_symbol ? NULL : sub_module;
  2244. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2245. #endif
  2246. return true;
  2247. fail:
  2248. return false;
  2249. }
  2250. static bool
  2251. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2252. uint32 error_buf_size)
  2253. {
  2254. if (max_size < init_size) {
  2255. set_error_buf(error_buf, error_buf_size,
  2256. "size minimum must not be greater than maximum");
  2257. return false;
  2258. }
  2259. return true;
  2260. }
  2261. static bool
  2262. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2263. WASMModule *parent_module, const char *sub_module_name,
  2264. const char *table_name, WASMTableImport *table,
  2265. char *error_buf, uint32 error_buf_size)
  2266. {
  2267. const uint8 *p = *p_buf, *p_end = buf_end;
  2268. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2269. declare_init_size = 0, declare_max_size = 0;
  2270. #if WASM_ENABLE_MULTI_MODULE != 0
  2271. WASMModule *sub_module = NULL;
  2272. WASMTable *linked_table = NULL;
  2273. #endif
  2274. #if WASM_ENABLE_GC != 0
  2275. WASMRefType ref_type;
  2276. bool need_ref_type_map;
  2277. #endif
  2278. #if WASM_ENABLE_GC == 0
  2279. CHECK_BUF(p, p_end, 1);
  2280. /* 0x70 or 0x6F */
  2281. declare_elem_type = read_uint8(p);
  2282. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2283. #if WASM_ENABLE_REF_TYPES != 0
  2284. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2285. #endif
  2286. ) {
  2287. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2288. return false;
  2289. }
  2290. #else /* else of WASM_ENABLE_GC == 0 */
  2291. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2292. &need_ref_type_map, &ref_type, false, error_buf,
  2293. error_buf_size)) {
  2294. return false;
  2295. }
  2296. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2297. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2298. return false;
  2299. }
  2300. declare_elem_type = ref_type.ref_type;
  2301. if (need_ref_type_map) {
  2302. if (!(table->table_type.elem_ref_type =
  2303. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2304. error_buf, error_buf_size))) {
  2305. return false;
  2306. }
  2307. }
  2308. #if TRACE_WASM_LOADER != 0
  2309. os_printf("import table type: ");
  2310. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2311. os_printf("\n");
  2312. #endif
  2313. #endif /* end of WASM_ENABLE_GC == 0 */
  2314. read_leb_uint32(p, p_end, declare_max_size_flag);
  2315. if (declare_max_size_flag > 1) {
  2316. set_error_buf(error_buf, error_buf_size, "integer too large");
  2317. return false;
  2318. }
  2319. read_leb_uint32(p, p_end, declare_init_size);
  2320. if (declare_max_size_flag) {
  2321. read_leb_uint32(p, p_end, declare_max_size);
  2322. if (!check_table_max_size(declare_init_size, declare_max_size,
  2323. error_buf, error_buf_size))
  2324. return false;
  2325. }
  2326. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2327. &declare_max_size);
  2328. *p_buf = p;
  2329. #if WASM_ENABLE_MULTI_MODULE != 0
  2330. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2331. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2332. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2333. error_buf_size);
  2334. if (sub_module) {
  2335. linked_table = wasm_loader_resolve_table(
  2336. sub_module_name, table_name, declare_init_size,
  2337. declare_max_size, error_buf, error_buf_size);
  2338. if (linked_table) {
  2339. /* reset with linked table limit */
  2340. declare_elem_type = linked_table->table_type.elem_type;
  2341. declare_init_size = linked_table->table_type.init_size;
  2342. declare_max_size = linked_table->table_type.max_size;
  2343. declare_max_size_flag = linked_table->table_type.flags;
  2344. table->import_table_linked = linked_table;
  2345. table->import_module = sub_module;
  2346. }
  2347. }
  2348. }
  2349. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2350. /* (table (export "table") 10 20 funcref) */
  2351. /* we need this section working in wamrc */
  2352. if (!strcmp("spectest", sub_module_name)) {
  2353. const uint32 spectest_table_init_size = 10;
  2354. const uint32 spectest_table_max_size = 20;
  2355. if (strcmp("table", table_name)) {
  2356. set_error_buf(error_buf, error_buf_size,
  2357. "incompatible import type or unknown import");
  2358. return false;
  2359. }
  2360. if (declare_init_size > spectest_table_init_size
  2361. || declare_max_size < spectest_table_max_size) {
  2362. set_error_buf(error_buf, error_buf_size,
  2363. "incompatible import type");
  2364. return false;
  2365. }
  2366. declare_init_size = spectest_table_init_size;
  2367. declare_max_size = spectest_table_max_size;
  2368. }
  2369. /* now we believe all declaration are ok */
  2370. table->table_type.elem_type = declare_elem_type;
  2371. table->table_type.init_size = declare_init_size;
  2372. table->table_type.flags = declare_max_size_flag;
  2373. table->table_type.max_size = declare_max_size;
  2374. #if WASM_ENABLE_WAMR_COMPILER != 0
  2375. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2376. parent_module->is_ref_types_used = true;
  2377. #endif
  2378. (void)parent_module;
  2379. return true;
  2380. fail:
  2381. return false;
  2382. }
  2383. static bool
  2384. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2385. uint32 error_buf_size)
  2386. {
  2387. uint32 default_max_size =
  2388. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2389. if (!is_memory64 && init_size > default_max_size) {
  2390. set_error_buf(error_buf, error_buf_size,
  2391. "memory size must be at most 65536 pages (4GiB)");
  2392. return false;
  2393. }
  2394. #if WASM_ENABLE_MEMORY64 != 0
  2395. else if (is_memory64 && init_size > default_max_size) {
  2396. set_error_buf(
  2397. error_buf, error_buf_size,
  2398. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2399. return false;
  2400. }
  2401. #endif
  2402. return true;
  2403. }
  2404. static bool
  2405. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2406. char *error_buf, uint32 error_buf_size)
  2407. {
  2408. uint32 default_max_size =
  2409. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2410. if (max_size < init_size) {
  2411. set_error_buf(error_buf, error_buf_size,
  2412. "size minimum must not be greater than maximum");
  2413. return false;
  2414. }
  2415. if (!is_memory64 && max_size > default_max_size) {
  2416. set_error_buf(error_buf, error_buf_size,
  2417. "memory size must be at most 65536 pages (4GiB)");
  2418. return false;
  2419. }
  2420. #if WASM_ENABLE_MEMORY64 != 0
  2421. else if (is_memory64 && max_size > default_max_size) {
  2422. set_error_buf(
  2423. error_buf, error_buf_size,
  2424. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2425. return false;
  2426. }
  2427. #endif
  2428. return true;
  2429. }
  2430. static bool
  2431. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2432. WASMModule *parent_module, const char *sub_module_name,
  2433. const char *memory_name, WASMMemoryImport *memory,
  2434. char *error_buf, uint32 error_buf_size)
  2435. {
  2436. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2437. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2438. uint32 pool_size = wasm_runtime_memory_pool_size();
  2439. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2440. / DEFAULT_NUM_BYTES_PER_PAGE;
  2441. #else
  2442. uint32 max_page_count;
  2443. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2444. uint32 mem_flag = 0;
  2445. bool is_memory64 = false;
  2446. uint32 declare_init_page_count = 0;
  2447. uint32 declare_max_page_count = 0;
  2448. #if WASM_ENABLE_MULTI_MODULE != 0
  2449. WASMModule *sub_module = NULL;
  2450. WASMMemory *linked_memory = NULL;
  2451. #endif
  2452. p_org = p;
  2453. read_leb_uint32(p, p_end, mem_flag);
  2454. is_memory64 = mem_flag & MEMORY64_FLAG;
  2455. if (p - p_org > 1) {
  2456. LOG_VERBOSE("integer representation too long");
  2457. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2458. return false;
  2459. }
  2460. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2461. return false;
  2462. }
  2463. read_leb_uint32(p, p_end, declare_init_page_count);
  2464. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2465. error_buf_size)) {
  2466. return false;
  2467. }
  2468. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2469. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2470. #endif
  2471. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2472. read_leb_uint32(p, p_end, declare_max_page_count);
  2473. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2474. declare_max_page_count, error_buf,
  2475. error_buf_size)) {
  2476. return false;
  2477. }
  2478. if (declare_max_page_count > max_page_count) {
  2479. declare_max_page_count = max_page_count;
  2480. }
  2481. }
  2482. else {
  2483. /* Limit the maximum memory size to max_page_count */
  2484. declare_max_page_count = max_page_count;
  2485. }
  2486. #if WASM_ENABLE_MULTI_MODULE != 0
  2487. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2488. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2489. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2490. error_buf_size);
  2491. if (sub_module) {
  2492. linked_memory = wasm_loader_resolve_memory(
  2493. sub_module_name, memory_name, declare_init_page_count,
  2494. declare_max_page_count, error_buf, error_buf_size);
  2495. if (linked_memory) {
  2496. /**
  2497. * reset with linked memory limit
  2498. */
  2499. memory->import_module = sub_module;
  2500. memory->import_memory_linked = linked_memory;
  2501. declare_init_page_count = linked_memory->init_page_count;
  2502. declare_max_page_count = linked_memory->max_page_count;
  2503. }
  2504. }
  2505. }
  2506. #endif
  2507. /* (memory (export "memory") 1 2) */
  2508. if (!strcmp("spectest", sub_module_name)) {
  2509. uint32 spectest_memory_init_page = 1;
  2510. uint32 spectest_memory_max_page = 2;
  2511. if (strcmp("memory", memory_name)) {
  2512. set_error_buf(error_buf, error_buf_size,
  2513. "incompatible import type or unknown import");
  2514. return false;
  2515. }
  2516. if (declare_init_page_count > spectest_memory_init_page
  2517. || declare_max_page_count < spectest_memory_max_page) {
  2518. set_error_buf(error_buf, error_buf_size,
  2519. "incompatible import type");
  2520. return false;
  2521. }
  2522. declare_init_page_count = spectest_memory_init_page;
  2523. declare_max_page_count = spectest_memory_max_page;
  2524. }
  2525. #if WASM_ENABLE_WASI_TEST != 0
  2526. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2527. else if (!strcmp("foo", sub_module_name)) {
  2528. uint32 spectest_memory_init_page = 1;
  2529. uint32 spectest_memory_max_page = 1;
  2530. if (strcmp("bar", memory_name)) {
  2531. set_error_buf(error_buf, error_buf_size,
  2532. "incompatible import type or unknown import");
  2533. return false;
  2534. }
  2535. if (declare_init_page_count > spectest_memory_init_page
  2536. || declare_max_page_count < spectest_memory_max_page) {
  2537. set_error_buf(error_buf, error_buf_size,
  2538. "incompatible import type");
  2539. return false;
  2540. }
  2541. declare_init_page_count = spectest_memory_init_page;
  2542. declare_max_page_count = spectest_memory_max_page;
  2543. }
  2544. #endif
  2545. /* now we believe all declaration are ok */
  2546. memory->mem_type.flags = mem_flag;
  2547. memory->mem_type.init_page_count = declare_init_page_count;
  2548. memory->mem_type.max_page_count = declare_max_page_count;
  2549. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2550. *p_buf = p;
  2551. (void)parent_module;
  2552. return true;
  2553. fail:
  2554. return false;
  2555. }
  2556. #if WASM_ENABLE_TAGS != 0
  2557. static bool
  2558. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2559. const WASMModule *parent_module, /* this module ! */
  2560. const char *sub_module_name, const char *tag_name,
  2561. WASMTagImport *tag, /* structure to fill */
  2562. char *error_buf, uint32 error_buf_size)
  2563. {
  2564. /* attribute and type of the import statement */
  2565. uint8 declare_tag_attribute;
  2566. uint32 declare_type_index;
  2567. const uint8 *p = *p_buf, *p_end = buf_end;
  2568. #if WASM_ENABLE_MULTI_MODULE != 0
  2569. WASMModule *sub_module = NULL;
  2570. #endif
  2571. /* get the one byte attribute */
  2572. CHECK_BUF(p, p_end, 1);
  2573. declare_tag_attribute = read_uint8(p);
  2574. if (declare_tag_attribute != 0) {
  2575. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2576. goto fail;
  2577. }
  2578. /* get type */
  2579. read_leb_uint32(p, p_end, declare_type_index);
  2580. /* compare against module->types */
  2581. if (declare_type_index >= parent_module->type_count) {
  2582. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2583. goto fail;
  2584. }
  2585. WASMFuncType *declare_tag_type =
  2586. (WASMFuncType *)parent_module->types[declare_type_index];
  2587. /* check, that the type of the declared tag returns void */
  2588. if (declare_tag_type->result_count != 0) {
  2589. set_error_buf(error_buf, error_buf_size,
  2590. "tag type signature does not return void");
  2591. goto fail;
  2592. }
  2593. #if WASM_ENABLE_MULTI_MODULE != 0
  2594. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2595. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2596. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2597. error_buf_size);
  2598. if (sub_module) {
  2599. /* wasm_loader_resolve_tag checks, that the imported tag
  2600. * and the declared tag have the same type
  2601. */
  2602. uint32 linked_tag_index = 0;
  2603. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2604. sub_module_name, tag_name, declare_tag_type,
  2605. &linked_tag_index /* out */, error_buf, error_buf_size);
  2606. if (linked_tag) {
  2607. tag->import_module = sub_module;
  2608. tag->import_tag_linked = linked_tag;
  2609. tag->import_tag_index_linked = linked_tag_index;
  2610. }
  2611. }
  2612. }
  2613. #endif
  2614. /* store to module tag declarations */
  2615. tag->attribute = declare_tag_attribute;
  2616. tag->type = declare_type_index;
  2617. tag->module_name = (char *)sub_module_name;
  2618. tag->field_name = (char *)tag_name;
  2619. tag->tag_type = declare_tag_type;
  2620. *p_buf = p;
  2621. (void)parent_module;
  2622. LOG_VERBOSE("Load tag import success\n");
  2623. return true;
  2624. fail:
  2625. return false;
  2626. }
  2627. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2628. static bool
  2629. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2630. WASMModule *parent_module, char *sub_module_name,
  2631. char *global_name, WASMGlobalImport *global, char *error_buf,
  2632. uint32 error_buf_size)
  2633. {
  2634. const uint8 *p = *p_buf, *p_end = buf_end;
  2635. uint8 declare_type = 0;
  2636. uint8 declare_mutable = 0;
  2637. #if WASM_ENABLE_MULTI_MODULE != 0
  2638. WASMModule *sub_module = NULL;
  2639. WASMGlobal *linked_global = NULL;
  2640. #endif
  2641. #if WASM_ENABLE_GC != 0
  2642. WASMRefType ref_type;
  2643. bool need_ref_type_map;
  2644. #endif
  2645. bool ret = false;
  2646. #if WASM_ENABLE_GC == 0
  2647. CHECK_BUF(p, p_end, 2);
  2648. /* global type */
  2649. declare_type = read_uint8(p);
  2650. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2651. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2652. return false;
  2653. }
  2654. declare_mutable = read_uint8(p);
  2655. #else
  2656. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2657. &need_ref_type_map, &ref_type, false, error_buf,
  2658. error_buf_size)) {
  2659. return false;
  2660. }
  2661. declare_type = ref_type.ref_type;
  2662. if (need_ref_type_map) {
  2663. if (!(global->ref_type =
  2664. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2665. error_buf, error_buf_size))) {
  2666. return false;
  2667. }
  2668. }
  2669. #if TRACE_WASM_LOADER != 0
  2670. os_printf("import global type: ");
  2671. wasm_dump_value_type(declare_type, global->ref_type);
  2672. os_printf("\n");
  2673. #endif
  2674. CHECK_BUF(p, p_end, 1);
  2675. declare_mutable = read_uint8(p);
  2676. #endif /* end of WASM_ENABLE_GC == 0 */
  2677. *p_buf = p;
  2678. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2679. return false;
  2680. }
  2681. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2682. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2683. global);
  2684. if (ret) {
  2685. if (global->type.val_type != declare_type
  2686. || global->type.is_mutable != declare_mutable) {
  2687. set_error_buf(error_buf, error_buf_size,
  2688. "incompatible import type");
  2689. return false;
  2690. }
  2691. global->is_linked = true;
  2692. }
  2693. #endif
  2694. #if WASM_ENABLE_MULTI_MODULE != 0
  2695. if (!global->is_linked
  2696. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2697. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2698. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2699. error_buf_size);
  2700. if (sub_module) {
  2701. /* check sub modules */
  2702. linked_global = wasm_loader_resolve_global(
  2703. sub_module_name, global_name, declare_type, declare_mutable,
  2704. error_buf, error_buf_size);
  2705. if (linked_global) {
  2706. global->import_module = sub_module;
  2707. global->import_global_linked = linked_global;
  2708. global->is_linked = true;
  2709. }
  2710. }
  2711. }
  2712. #endif
  2713. global->module_name = sub_module_name;
  2714. global->field_name = global_name;
  2715. global->type.val_type = declare_type;
  2716. global->type.is_mutable = (declare_mutable == 1);
  2717. #if WASM_ENABLE_WAMR_COMPILER != 0
  2718. if (global->type.val_type == VALUE_TYPE_V128)
  2719. parent_module->is_simd_used = true;
  2720. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2721. parent_module->is_ref_types_used = true;
  2722. #endif
  2723. (void)parent_module;
  2724. (void)ret;
  2725. return true;
  2726. fail:
  2727. return false;
  2728. }
  2729. static bool
  2730. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2731. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2732. {
  2733. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2734. #if WASM_ENABLE_GC != 0
  2735. WASMRefType ref_type;
  2736. bool need_ref_type_map;
  2737. #endif
  2738. #if WASM_ENABLE_GC == 0
  2739. CHECK_BUF(p, p_end, 1);
  2740. /* 0x70 or 0x6F */
  2741. table->table_type.elem_type = read_uint8(p);
  2742. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2743. #if WASM_ENABLE_REF_TYPES != 0
  2744. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2745. #endif
  2746. ) {
  2747. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2748. return false;
  2749. }
  2750. #else /* else of WASM_ENABLE_GC == 0 */
  2751. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2752. &need_ref_type_map, &ref_type, false, error_buf,
  2753. error_buf_size)) {
  2754. return false;
  2755. }
  2756. table->table_type.elem_type = ref_type.ref_type;
  2757. if (need_ref_type_map) {
  2758. if (!(table->table_type.elem_ref_type =
  2759. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2760. error_buf_size))) {
  2761. return false;
  2762. }
  2763. }
  2764. #if TRACE_WASM_LOADER != 0
  2765. os_printf("table type: ");
  2766. wasm_dump_value_type(table->table_type.elem_type,
  2767. table->table_type.elem_ref_type);
  2768. os_printf("\n");
  2769. #endif
  2770. #endif /* end of WASM_ENABLE_GC == 0 */
  2771. p_org = p;
  2772. read_leb_uint32(p, p_end, table->table_type.flags);
  2773. #if WASM_ENABLE_SHARED_MEMORY == 0
  2774. if (p - p_org > 1) {
  2775. set_error_buf(error_buf, error_buf_size,
  2776. "integer representation too long");
  2777. return false;
  2778. }
  2779. if (table->table_type.flags > 1) {
  2780. set_error_buf(error_buf, error_buf_size, "integer too large");
  2781. return false;
  2782. }
  2783. #else
  2784. if (p - p_org > 1) {
  2785. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2786. return false;
  2787. }
  2788. if (table->table_type.flags == 2) {
  2789. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2790. return false;
  2791. }
  2792. if (table->table_type.flags > 1) {
  2793. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2794. return false;
  2795. }
  2796. #endif
  2797. read_leb_uint32(p, p_end, table->table_type.init_size);
  2798. if (table->table_type.flags) {
  2799. read_leb_uint32(p, p_end, table->table_type.max_size);
  2800. if (!check_table_max_size(table->table_type.init_size,
  2801. table->table_type.max_size, error_buf,
  2802. error_buf_size))
  2803. return false;
  2804. }
  2805. adjust_table_max_size(table->table_type.init_size, table->table_type.flags,
  2806. &table->table_type.max_size);
  2807. #if WASM_ENABLE_WAMR_COMPILER != 0
  2808. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2809. module->is_ref_types_used = true;
  2810. #endif
  2811. *p_buf = p;
  2812. return true;
  2813. fail:
  2814. return false;
  2815. }
  2816. static bool
  2817. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2818. char *error_buf, uint32 error_buf_size)
  2819. {
  2820. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2821. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2822. uint32 pool_size = wasm_runtime_memory_pool_size();
  2823. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2824. / DEFAULT_NUM_BYTES_PER_PAGE;
  2825. #else
  2826. uint32 max_page_count;
  2827. #endif
  2828. bool is_memory64 = false;
  2829. p_org = p;
  2830. read_leb_uint32(p, p_end, memory->flags);
  2831. is_memory64 = memory->flags & MEMORY64_FLAG;
  2832. if (p - p_org > 1) {
  2833. LOG_VERBOSE("integer representation too long");
  2834. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2835. return false;
  2836. }
  2837. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2838. false)) {
  2839. return false;
  2840. }
  2841. read_leb_uint32(p, p_end, memory->init_page_count);
  2842. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2843. error_buf_size))
  2844. return false;
  2845. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2846. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2847. #endif
  2848. if (memory->flags & 1) {
  2849. read_leb_uint32(p, p_end, memory->max_page_count);
  2850. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2851. memory->max_page_count, error_buf,
  2852. error_buf_size))
  2853. return false;
  2854. if (memory->max_page_count > max_page_count)
  2855. memory->max_page_count = max_page_count;
  2856. }
  2857. else {
  2858. /* Limit the maximum memory size to max_page_count */
  2859. memory->max_page_count = max_page_count;
  2860. }
  2861. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2862. *p_buf = p;
  2863. return true;
  2864. fail:
  2865. return false;
  2866. }
  2867. static bool
  2868. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2869. bool is_load_from_file_buf, char *error_buf,
  2870. uint32 error_buf_size)
  2871. {
  2872. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2873. uint32 import_count, name_len, type_index, i, u32, flags;
  2874. uint64 total_size;
  2875. WASMImport *import;
  2876. WASMImport *import_functions = NULL, *import_tables = NULL;
  2877. WASMImport *import_memories = NULL, *import_globals = NULL;
  2878. #if WASM_ENABLE_TAGS != 0
  2879. WASMImport *import_tags = NULL;
  2880. #endif
  2881. char *sub_module_name, *field_name;
  2882. uint8 u8, kind, global_type;
  2883. read_leb_uint32(p, p_end, import_count);
  2884. if (import_count) {
  2885. module->import_count = import_count;
  2886. total_size = sizeof(WASMImport) * (uint64)import_count;
  2887. if (!(module->imports =
  2888. loader_malloc(total_size, error_buf, error_buf_size))) {
  2889. return false;
  2890. }
  2891. p_old = p;
  2892. /* Scan firstly to get import count of each type */
  2893. for (i = 0; i < import_count; i++) {
  2894. /* module name */
  2895. read_leb_uint32(p, p_end, name_len);
  2896. CHECK_BUF(p, p_end, name_len);
  2897. p += name_len;
  2898. /* field name */
  2899. read_leb_uint32(p, p_end, name_len);
  2900. CHECK_BUF(p, p_end, name_len);
  2901. p += name_len;
  2902. CHECK_BUF(p, p_end, 1);
  2903. /* 0x00/0x01/0x02/0x03/0x04 */
  2904. kind = read_uint8(p);
  2905. switch (kind) {
  2906. case IMPORT_KIND_FUNC: /* import function */
  2907. read_leb_uint32(p, p_end, type_index);
  2908. module->import_function_count++;
  2909. break;
  2910. case IMPORT_KIND_TABLE: /* import table */
  2911. CHECK_BUF(p, p_end, 1);
  2912. /* 0x70 */
  2913. u8 = read_uint8(p);
  2914. read_leb_uint32(p, p_end, flags);
  2915. read_leb_uint32(p, p_end, u32);
  2916. if (flags & 1)
  2917. read_leb_uint32(p, p_end, u32);
  2918. module->import_table_count++;
  2919. if (module->import_table_count > 1) {
  2920. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2921. set_error_buf(error_buf, error_buf_size,
  2922. "multiple tables");
  2923. return false;
  2924. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2925. module->is_ref_types_used = true;
  2926. #endif
  2927. }
  2928. break;
  2929. case IMPORT_KIND_MEMORY: /* import memory */
  2930. read_leb_uint32(p, p_end, flags);
  2931. read_leb_uint32(p, p_end, u32);
  2932. if (flags & 1)
  2933. read_leb_uint32(p, p_end, u32);
  2934. module->import_memory_count++;
  2935. if (module->import_memory_count > 1) {
  2936. set_error_buf(error_buf, error_buf_size,
  2937. "multiple memories");
  2938. return false;
  2939. }
  2940. break;
  2941. #if WASM_ENABLE_TAGS != 0
  2942. case IMPORT_KIND_TAG: /* import tags */
  2943. /* it only counts the number of tags to import */
  2944. module->import_tag_count++;
  2945. CHECK_BUF(p, p_end, 1);
  2946. u8 = read_uint8(p);
  2947. read_leb_uint32(p, p_end, type_index);
  2948. break;
  2949. #endif
  2950. case IMPORT_KIND_GLOBAL: /* import global */
  2951. #if WASM_ENABLE_GC != 0
  2952. /* valtype */
  2953. CHECK_BUF(p, p_end, 1);
  2954. global_type = read_uint8(p);
  2955. if (wasm_is_type_multi_byte_type(global_type)) {
  2956. int32 heap_type;
  2957. read_leb_int32(p, p_end, heap_type);
  2958. (void)heap_type;
  2959. }
  2960. /* mutability */
  2961. CHECK_BUF(p, p_end, 1);
  2962. p += 1;
  2963. #else
  2964. CHECK_BUF(p, p_end, 2);
  2965. p += 2;
  2966. #endif
  2967. (void)global_type;
  2968. module->import_global_count++;
  2969. break;
  2970. default:
  2971. set_error_buf(error_buf, error_buf_size,
  2972. "invalid import kind");
  2973. return false;
  2974. }
  2975. }
  2976. if (module->import_function_count)
  2977. import_functions = module->import_functions = module->imports;
  2978. if (module->import_table_count)
  2979. import_tables = module->import_tables =
  2980. module->imports + module->import_function_count;
  2981. if (module->import_memory_count)
  2982. import_memories = module->import_memories =
  2983. module->imports + module->import_function_count
  2984. + module->import_table_count;
  2985. #if WASM_ENABLE_TAGS != 0
  2986. if (module->import_tag_count)
  2987. import_tags = module->import_tags =
  2988. module->imports + module->import_function_count
  2989. + module->import_table_count + module->import_memory_count;
  2990. if (module->import_global_count)
  2991. import_globals = module->import_globals =
  2992. module->imports + module->import_function_count
  2993. + module->import_table_count + module->import_memory_count
  2994. + module->import_tag_count;
  2995. #else
  2996. if (module->import_global_count)
  2997. import_globals = module->import_globals =
  2998. module->imports + module->import_function_count
  2999. + module->import_table_count + module->import_memory_count;
  3000. #endif
  3001. p = p_old;
  3002. /* Scan again to resolve the data */
  3003. for (i = 0; i < import_count; i++) {
  3004. /* load module name */
  3005. read_leb_uint32(p, p_end, name_len);
  3006. CHECK_BUF(p, p_end, name_len);
  3007. if (!(sub_module_name = wasm_const_str_list_insert(
  3008. p, name_len, module, is_load_from_file_buf, error_buf,
  3009. error_buf_size))) {
  3010. return false;
  3011. }
  3012. p += name_len;
  3013. /* load field name */
  3014. read_leb_uint32(p, p_end, name_len);
  3015. CHECK_BUF(p, p_end, name_len);
  3016. if (!(field_name = wasm_const_str_list_insert(
  3017. p, name_len, module, is_load_from_file_buf, error_buf,
  3018. error_buf_size))) {
  3019. return false;
  3020. }
  3021. p += name_len;
  3022. CHECK_BUF(p, p_end, 1);
  3023. /* 0x00/0x01/0x02/0x03/0x4 */
  3024. kind = read_uint8(p);
  3025. switch (kind) {
  3026. case IMPORT_KIND_FUNC: /* import function */
  3027. bh_assert(import_functions);
  3028. import = import_functions++;
  3029. if (!load_function_import(
  3030. &p, p_end, module, sub_module_name, field_name,
  3031. &import->u.function, error_buf, error_buf_size)) {
  3032. return false;
  3033. }
  3034. break;
  3035. case IMPORT_KIND_TABLE: /* import table */
  3036. bh_assert(import_tables);
  3037. import = import_tables++;
  3038. if (!load_table_import(&p, p_end, module, sub_module_name,
  3039. field_name, &import->u.table,
  3040. error_buf, error_buf_size)) {
  3041. LOG_DEBUG("can not import such a table (%s,%s)",
  3042. sub_module_name, field_name);
  3043. return false;
  3044. }
  3045. break;
  3046. case IMPORT_KIND_MEMORY: /* import memory */
  3047. bh_assert(import_memories);
  3048. import = import_memories++;
  3049. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3050. field_name, &import->u.memory,
  3051. error_buf, error_buf_size)) {
  3052. return false;
  3053. }
  3054. break;
  3055. #if WASM_ENABLE_TAGS != 0
  3056. case IMPORT_KIND_TAG:
  3057. bh_assert(import_tags);
  3058. import = import_tags++;
  3059. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3060. field_name, &import->u.tag, error_buf,
  3061. error_buf_size)) {
  3062. return false;
  3063. }
  3064. break;
  3065. #endif
  3066. case IMPORT_KIND_GLOBAL: /* import global */
  3067. bh_assert(import_globals);
  3068. import = import_globals++;
  3069. if (!load_global_import(&p, p_end, module, sub_module_name,
  3070. field_name, &import->u.global,
  3071. error_buf, error_buf_size)) {
  3072. return false;
  3073. }
  3074. break;
  3075. default:
  3076. set_error_buf(error_buf, error_buf_size,
  3077. "invalid import kind");
  3078. return false;
  3079. }
  3080. import->kind = kind;
  3081. import->u.names.module_name = sub_module_name;
  3082. import->u.names.field_name = field_name;
  3083. }
  3084. #if WASM_ENABLE_LIBC_WASI != 0
  3085. import = module->import_functions;
  3086. for (i = 0; i < module->import_function_count; i++, import++) {
  3087. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3088. || !strcmp(import->u.names.module_name,
  3089. "wasi_snapshot_preview1")) {
  3090. module->import_wasi_api = true;
  3091. break;
  3092. }
  3093. }
  3094. #endif
  3095. }
  3096. if (p != p_end) {
  3097. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3098. return false;
  3099. }
  3100. LOG_VERBOSE("Load import section success.\n");
  3101. (void)u8;
  3102. (void)u32;
  3103. (void)type_index;
  3104. return true;
  3105. fail:
  3106. return false;
  3107. }
  3108. static bool
  3109. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3110. uint32 error_buf_size)
  3111. {
  3112. WASMFuncType *param_type = func->func_type;
  3113. uint32 param_count = param_type->param_count;
  3114. uint8 *param_types = param_type->types;
  3115. uint32 local_count = func->local_count;
  3116. uint8 *local_types = func->local_types;
  3117. uint32 i, local_offset = 0;
  3118. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3119. /*
  3120. * Only allocate memory when total_size is not 0,
  3121. * or the return value of malloc(0) might be NULL on some platforms,
  3122. * which causes wasm loader return false.
  3123. */
  3124. if (total_size > 0
  3125. && !(func->local_offsets =
  3126. loader_malloc(total_size, error_buf, error_buf_size))) {
  3127. return false;
  3128. }
  3129. for (i = 0; i < param_count; i++) {
  3130. func->local_offsets[i] = (uint16)local_offset;
  3131. local_offset += wasm_value_type_cell_num(param_types[i]);
  3132. }
  3133. for (i = 0; i < local_count; i++) {
  3134. func->local_offsets[param_count + i] = (uint16)local_offset;
  3135. local_offset += wasm_value_type_cell_num(local_types[i]);
  3136. }
  3137. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3138. return true;
  3139. }
  3140. static bool
  3141. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3142. const uint8 *buf_code, const uint8 *buf_code_end,
  3143. WASMModule *module, char *error_buf,
  3144. uint32 error_buf_size)
  3145. {
  3146. const uint8 *p = buf, *p_end = buf_end;
  3147. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3148. uint32 func_count;
  3149. uint64 total_size;
  3150. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3151. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3152. uint8 type;
  3153. WASMFunction *func;
  3154. #if WASM_ENABLE_GC != 0
  3155. bool need_ref_type_map;
  3156. WASMRefType ref_type;
  3157. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3158. #endif
  3159. read_leb_uint32(p, p_end, func_count);
  3160. if (buf_code)
  3161. read_leb_uint32(p_code, buf_code_end, code_count);
  3162. if (func_count != code_count) {
  3163. set_error_buf(error_buf, error_buf_size,
  3164. "function and code section have inconsistent lengths or "
  3165. "unexpected end");
  3166. return false;
  3167. }
  3168. if (is_indices_overflow(module->import_function_count, func_count,
  3169. error_buf, error_buf_size))
  3170. return false;
  3171. if (func_count) {
  3172. module->function_count = func_count;
  3173. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3174. if (!(module->functions =
  3175. loader_malloc(total_size, error_buf, error_buf_size))) {
  3176. return false;
  3177. }
  3178. for (i = 0; i < func_count; i++) {
  3179. /* Resolve function type */
  3180. read_leb_uint32(p, p_end, type_index);
  3181. if (type_index >= module->type_count) {
  3182. set_error_buf(error_buf, error_buf_size, "unknown type");
  3183. return false;
  3184. }
  3185. #if WASM_ENABLE_GC != 0
  3186. type_index_org = type_index;
  3187. #endif
  3188. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3189. && WASM_ENABLE_GC == 0
  3190. type_index = wasm_get_smallest_type_idx(
  3191. module->types, module->type_count, type_index);
  3192. #endif
  3193. read_leb_uint32(p_code, buf_code_end, code_size);
  3194. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3195. set_error_buf(error_buf, error_buf_size,
  3196. "invalid function code size");
  3197. return false;
  3198. }
  3199. /* Resolve local set count */
  3200. p_code_end = p_code + code_size;
  3201. local_count = 0;
  3202. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3203. p_code_save = p_code;
  3204. #if WASM_ENABLE_GC != 0
  3205. ref_type_map_count = 0;
  3206. #endif
  3207. /* Calculate total local count */
  3208. for (j = 0; j < local_set_count; j++) {
  3209. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3210. if (sub_local_count > UINT32_MAX - local_count) {
  3211. set_error_buf(error_buf, error_buf_size, "too many locals");
  3212. return false;
  3213. }
  3214. #if WASM_ENABLE_GC == 0
  3215. CHECK_BUF(p_code, buf_code_end, 1);
  3216. /* 0x7F/0x7E/0x7D/0x7C */
  3217. type = read_uint8(p_code);
  3218. local_count += sub_local_count;
  3219. #if WASM_ENABLE_WAMR_COMPILER != 0
  3220. /* If any value's type is v128, mark the module as SIMD used */
  3221. if (type == VALUE_TYPE_V128)
  3222. module->is_simd_used = true;
  3223. #endif
  3224. #else
  3225. if (!resolve_value_type(&p_code, buf_code_end, module,
  3226. module->type_count, &need_ref_type_map,
  3227. &ref_type, false, error_buf,
  3228. error_buf_size)) {
  3229. return false;
  3230. }
  3231. local_count += sub_local_count;
  3232. if (need_ref_type_map)
  3233. ref_type_map_count += sub_local_count;
  3234. #endif
  3235. }
  3236. /* Alloc memory, layout: function structure + local types */
  3237. code_size = (uint32)(p_code_end - p_code);
  3238. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3239. if (!(func = module->functions[i] =
  3240. loader_malloc(total_size, error_buf, error_buf_size))) {
  3241. return false;
  3242. }
  3243. #if WASM_ENABLE_GC != 0
  3244. if (ref_type_map_count > 0) {
  3245. total_size =
  3246. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3247. if (!(func->local_ref_type_maps = loader_malloc(
  3248. total_size, error_buf, error_buf_size))) {
  3249. return false;
  3250. }
  3251. func->local_ref_type_map_count = ref_type_map_count;
  3252. }
  3253. #endif
  3254. /* Set function type, local count, code size and code body */
  3255. func->func_type = (WASMFuncType *)module->types[type_index];
  3256. func->local_count = local_count;
  3257. if (local_count > 0)
  3258. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3259. func->code_size = code_size;
  3260. /*
  3261. * we shall make a copy of code body [p_code, p_code + code_size]
  3262. * when we are worrying about inappropriate releasing behaviour.
  3263. * all code bodies are actually in a buffer which user allocates in
  3264. * his embedding environment and we don't have power on them.
  3265. * it will be like:
  3266. * code_body_cp = malloc(code_size);
  3267. * memcpy(code_body_cp, p_code, code_size);
  3268. * func->code = code_body_cp;
  3269. */
  3270. func->code = (uint8 *)p_code;
  3271. #if WASM_ENABLE_GC != 0
  3272. func->type_idx = type_index_org;
  3273. #endif
  3274. #if WASM_ENABLE_GC != 0
  3275. t = 0;
  3276. #endif
  3277. /* Load each local type */
  3278. p_code = p_code_save;
  3279. local_type_index = 0;
  3280. for (j = 0; j < local_set_count; j++) {
  3281. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3282. /* Note: sub_local_count is allowed to be 0 */
  3283. if (local_type_index > UINT32_MAX - sub_local_count
  3284. || local_type_index + sub_local_count > local_count) {
  3285. set_error_buf(error_buf, error_buf_size,
  3286. "invalid local count");
  3287. return false;
  3288. }
  3289. #if WASM_ENABLE_GC == 0
  3290. CHECK_BUF(p_code, buf_code_end, 1);
  3291. /* 0x7F/0x7E/0x7D/0x7C */
  3292. type = read_uint8(p_code);
  3293. if (!is_valid_value_type_for_interpreter(type)) {
  3294. if (type == VALUE_TYPE_V128)
  3295. set_error_buf(error_buf, error_buf_size,
  3296. "v128 value type requires simd feature");
  3297. else if (type == VALUE_TYPE_FUNCREF
  3298. || type == VALUE_TYPE_EXTERNREF)
  3299. set_error_buf(error_buf, error_buf_size,
  3300. "ref value type requires "
  3301. "reference types feature");
  3302. else
  3303. set_error_buf_v(error_buf, error_buf_size,
  3304. "invalid local type 0x%02X", type);
  3305. return false;
  3306. }
  3307. #else
  3308. if (!resolve_value_type(&p_code, buf_code_end, module,
  3309. module->type_count, &need_ref_type_map,
  3310. &ref_type, false, error_buf,
  3311. error_buf_size)) {
  3312. return false;
  3313. }
  3314. if (need_ref_type_map) {
  3315. WASMRefType *ref_type_tmp;
  3316. if (!(ref_type_tmp = reftype_set_insert(
  3317. module->ref_type_set, &ref_type, error_buf,
  3318. error_buf_size))) {
  3319. return false;
  3320. }
  3321. for (k = 0; k < sub_local_count; k++) {
  3322. func->local_ref_type_maps[t + k].ref_type =
  3323. ref_type_tmp;
  3324. func->local_ref_type_maps[t + k].index =
  3325. local_type_index + k;
  3326. }
  3327. t += sub_local_count;
  3328. }
  3329. type = ref_type.ref_type;
  3330. #endif
  3331. for (k = 0; k < sub_local_count; k++) {
  3332. func->local_types[local_type_index++] = type;
  3333. }
  3334. #if WASM_ENABLE_WAMR_COMPILER != 0
  3335. if (type == VALUE_TYPE_V128)
  3336. module->is_simd_used = true;
  3337. else if (type == VALUE_TYPE_FUNCREF
  3338. || type == VALUE_TYPE_EXTERNREF)
  3339. module->is_ref_types_used = true;
  3340. #endif
  3341. }
  3342. bh_assert(local_type_index == func->local_count);
  3343. #if WASM_ENABLE_GC != 0
  3344. bh_assert(t == func->local_ref_type_map_count);
  3345. #if TRACE_WASM_LOADER != 0
  3346. os_printf("func %u, local types: [", i);
  3347. k = 0;
  3348. for (j = 0; j < func->local_count; j++) {
  3349. WASMRefType *ref_type_tmp = NULL;
  3350. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3351. bh_assert(j == func->local_ref_type_maps[k].index);
  3352. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3353. }
  3354. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3355. if (j < func->local_count - 1)
  3356. os_printf(" ");
  3357. }
  3358. os_printf("]\n");
  3359. #endif
  3360. #endif
  3361. func->param_cell_num = func->func_type->param_cell_num;
  3362. func->ret_cell_num = func->func_type->ret_cell_num;
  3363. local_cell_num =
  3364. wasm_get_cell_num(func->local_types, func->local_count);
  3365. if (local_cell_num > UINT16_MAX) {
  3366. set_error_buf(error_buf, error_buf_size,
  3367. "local count too large");
  3368. return false;
  3369. }
  3370. func->local_cell_num = (uint16)local_cell_num;
  3371. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3372. return false;
  3373. p_code = p_code_end;
  3374. }
  3375. }
  3376. if (p != p_end) {
  3377. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3378. return false;
  3379. }
  3380. LOG_VERBOSE("Load function section success.\n");
  3381. return true;
  3382. fail:
  3383. return false;
  3384. }
  3385. static bool
  3386. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3387. char *error_buf, uint32 error_buf_size)
  3388. {
  3389. const uint8 *p = buf, *p_end = buf_end;
  3390. uint32 table_count, i;
  3391. uint64 total_size;
  3392. WASMTable *table;
  3393. read_leb_uint32(p, p_end, table_count);
  3394. if (module->import_table_count + table_count > 1) {
  3395. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3396. /* a total of one table is allowed */
  3397. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3398. return false;
  3399. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3400. module->is_ref_types_used = true;
  3401. #endif
  3402. }
  3403. if (table_count) {
  3404. module->table_count = table_count;
  3405. total_size = sizeof(WASMTable) * (uint64)table_count;
  3406. if (!(module->tables =
  3407. loader_malloc(total_size, error_buf, error_buf_size))) {
  3408. return false;
  3409. }
  3410. /* load each table */
  3411. table = module->tables;
  3412. for (i = 0; i < table_count; i++, table++) {
  3413. #if WASM_ENABLE_GC != 0
  3414. uint8 flag;
  3415. bool has_init = false;
  3416. CHECK_BUF(buf, buf_end, 1);
  3417. flag = read_uint8(p);
  3418. if (flag == TABLE_INIT_EXPR_FLAG) {
  3419. CHECK_BUF(buf, buf_end, 1);
  3420. flag = read_uint8(p);
  3421. if (flag != 0x00) {
  3422. set_error_buf(error_buf, error_buf_size,
  3423. "invalid leading bytes for table");
  3424. return false;
  3425. }
  3426. has_init = true;
  3427. }
  3428. else {
  3429. p--;
  3430. }
  3431. #endif /* end of WASM_ENABLE_GC != 0 */
  3432. if (!load_table(&p, p_end, module, table, error_buf,
  3433. error_buf_size))
  3434. return false;
  3435. #if WASM_ENABLE_GC != 0
  3436. if (has_init) {
  3437. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3438. table->table_type.elem_type,
  3439. table->table_type.elem_ref_type, error_buf,
  3440. error_buf_size))
  3441. return false;
  3442. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3443. && table->init_expr.init_expr_type
  3444. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3445. set_error_buf(
  3446. error_buf, error_buf_size,
  3447. "unsupported initializer expression for table");
  3448. return false;
  3449. }
  3450. }
  3451. else {
  3452. if (wasm_is_reftype_htref_non_nullable(
  3453. table->table_type.elem_type)) {
  3454. set_error_buf(
  3455. error_buf, error_buf_size,
  3456. "type mismatch: non-nullable table without init expr");
  3457. return false;
  3458. }
  3459. }
  3460. #endif /* end of WASM_ENABLE_GC != 0 */
  3461. #if WASM_ENABLE_WAMR_COMPILER != 0
  3462. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3463. module->is_ref_types_used = true;
  3464. #endif
  3465. }
  3466. }
  3467. if (p != p_end) {
  3468. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3469. return false;
  3470. }
  3471. LOG_VERBOSE("Load table section success.\n");
  3472. return true;
  3473. fail:
  3474. return false;
  3475. }
  3476. static bool
  3477. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3478. char *error_buf, uint32 error_buf_size)
  3479. {
  3480. const uint8 *p = buf, *p_end = buf_end;
  3481. uint32 memory_count, i;
  3482. uint64 total_size;
  3483. WASMMemory *memory;
  3484. read_leb_uint32(p, p_end, memory_count);
  3485. /* a total of one memory is allowed */
  3486. if (module->import_memory_count + memory_count > 1) {
  3487. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3488. return false;
  3489. }
  3490. if (memory_count) {
  3491. module->memory_count = memory_count;
  3492. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3493. if (!(module->memories =
  3494. loader_malloc(total_size, error_buf, error_buf_size))) {
  3495. return false;
  3496. }
  3497. /* load each memory */
  3498. memory = module->memories;
  3499. for (i = 0; i < memory_count; i++, memory++)
  3500. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3501. return false;
  3502. }
  3503. if (p != p_end) {
  3504. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3505. return false;
  3506. }
  3507. LOG_VERBOSE("Load memory section success.\n");
  3508. return true;
  3509. fail:
  3510. return false;
  3511. }
  3512. static bool
  3513. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3514. char *error_buf, uint32 error_buf_size)
  3515. {
  3516. const uint8 *p = buf, *p_end = buf_end;
  3517. uint32 global_count, i;
  3518. uint64 total_size;
  3519. WASMGlobal *global;
  3520. uint8 mutable;
  3521. #if WASM_ENABLE_GC != 0
  3522. bool need_ref_type_map;
  3523. WASMRefType ref_type;
  3524. #endif
  3525. read_leb_uint32(p, p_end, global_count);
  3526. if (is_indices_overflow(module->import_global_count, global_count,
  3527. error_buf, error_buf_size))
  3528. return false;
  3529. module->global_count = 0;
  3530. if (global_count) {
  3531. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3532. if (!(module->globals =
  3533. loader_malloc(total_size, error_buf, error_buf_size))) {
  3534. return false;
  3535. }
  3536. global = module->globals;
  3537. for (i = 0; i < global_count; i++, global++) {
  3538. #if WASM_ENABLE_GC == 0
  3539. CHECK_BUF(p, p_end, 2);
  3540. /* global type */
  3541. global->type.val_type = read_uint8(p);
  3542. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3543. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3544. return false;
  3545. }
  3546. mutable = read_uint8(p);
  3547. #else
  3548. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3549. &need_ref_type_map, &ref_type, false,
  3550. error_buf, error_buf_size)) {
  3551. return false;
  3552. }
  3553. global->type.val_type = ref_type.ref_type;
  3554. CHECK_BUF(p, p_end, 1);
  3555. mutable = read_uint8(p);
  3556. #endif /* end of WASM_ENABLE_GC */
  3557. #if WASM_ENABLE_WAMR_COMPILER != 0
  3558. if (global->type.val_type == VALUE_TYPE_V128)
  3559. module->is_simd_used = true;
  3560. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3561. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3562. module->is_ref_types_used = true;
  3563. #endif
  3564. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3565. return false;
  3566. }
  3567. global->type.is_mutable = mutable ? true : false;
  3568. /* initialize expression */
  3569. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3570. global->type.val_type,
  3571. #if WASM_ENABLE_GC == 0
  3572. NULL,
  3573. #else
  3574. &ref_type,
  3575. #endif
  3576. error_buf, error_buf_size))
  3577. return false;
  3578. #if WASM_ENABLE_GC != 0
  3579. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3580. uint8 global_type;
  3581. WASMRefType *global_ref_type;
  3582. uint32 global_idx = global->init_expr.u.global_index;
  3583. if (global->init_expr.u.global_index
  3584. >= module->import_global_count + i) {
  3585. set_error_buf(error_buf, error_buf_size, "unknown global");
  3586. return false;
  3587. }
  3588. if (global_idx < module->import_global_count) {
  3589. global_type = module->import_globals[global_idx]
  3590. .u.global.type.val_type;
  3591. global_ref_type =
  3592. module->import_globals[global_idx].u.global.ref_type;
  3593. }
  3594. else {
  3595. global_type =
  3596. module
  3597. ->globals[global_idx - module->import_global_count]
  3598. .type.val_type;
  3599. global_ref_type =
  3600. module
  3601. ->globals[global_idx - module->import_global_count]
  3602. .ref_type;
  3603. }
  3604. if (!wasm_reftype_is_subtype_of(
  3605. global_type, global_ref_type, global->type.val_type,
  3606. global->ref_type, module->types, module->type_count)) {
  3607. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3608. return false;
  3609. }
  3610. }
  3611. if (need_ref_type_map) {
  3612. if (!(global->ref_type =
  3613. reftype_set_insert(module->ref_type_set, &ref_type,
  3614. error_buf, error_buf_size))) {
  3615. return false;
  3616. }
  3617. }
  3618. #if TRACE_WASM_LOADER != 0
  3619. os_printf("global type: ");
  3620. wasm_dump_value_type(global->type, global->ref_type);
  3621. os_printf("\n");
  3622. #endif
  3623. #endif
  3624. module->global_count++;
  3625. }
  3626. bh_assert(module->global_count == global_count);
  3627. }
  3628. if (p != p_end) {
  3629. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3630. return false;
  3631. }
  3632. LOG_VERBOSE("Load global section success.\n");
  3633. return true;
  3634. fail:
  3635. return false;
  3636. }
  3637. static bool
  3638. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3639. bool is_load_from_file_buf, char *error_buf,
  3640. uint32 error_buf_size)
  3641. {
  3642. const uint8 *p = buf, *p_end = buf_end;
  3643. uint32 export_count, i, j, index;
  3644. uint64 total_size;
  3645. uint32 str_len;
  3646. WASMExport *export;
  3647. const char *name;
  3648. read_leb_uint32(p, p_end, export_count);
  3649. if (export_count) {
  3650. module->export_count = export_count;
  3651. total_size = sizeof(WASMExport) * (uint64)export_count;
  3652. if (!(module->exports =
  3653. loader_malloc(total_size, error_buf, error_buf_size))) {
  3654. return false;
  3655. }
  3656. export = module->exports;
  3657. for (i = 0; i < export_count; i++, export ++) {
  3658. #if WASM_ENABLE_THREAD_MGR == 0
  3659. if (p == p_end) {
  3660. /* export section with inconsistent count:
  3661. n export declared, but less than n given */
  3662. set_error_buf(error_buf, error_buf_size,
  3663. "length out of bounds");
  3664. return false;
  3665. }
  3666. #endif
  3667. read_leb_uint32(p, p_end, str_len);
  3668. CHECK_BUF(p, p_end, str_len);
  3669. for (j = 0; j < i; j++) {
  3670. name = module->exports[j].name;
  3671. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3672. set_error_buf(error_buf, error_buf_size,
  3673. "duplicate export name");
  3674. return false;
  3675. }
  3676. }
  3677. if (!(export->name = wasm_const_str_list_insert(
  3678. p, str_len, module, is_load_from_file_buf, error_buf,
  3679. error_buf_size))) {
  3680. return false;
  3681. }
  3682. p += str_len;
  3683. CHECK_BUF(p, p_end, 1);
  3684. export->kind = read_uint8(p);
  3685. read_leb_uint32(p, p_end, index);
  3686. export->index = index;
  3687. switch (export->kind) {
  3688. /* function index */
  3689. case EXPORT_KIND_FUNC:
  3690. if (index >= module->function_count
  3691. + module->import_function_count) {
  3692. set_error_buf(error_buf, error_buf_size,
  3693. "unknown function");
  3694. return false;
  3695. }
  3696. #if WASM_ENABLE_SIMD != 0
  3697. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3698. /* TODO: check func type, if it has v128 param or result,
  3699. report error */
  3700. #endif
  3701. #endif
  3702. break;
  3703. /* table index */
  3704. case EXPORT_KIND_TABLE:
  3705. if (index
  3706. >= module->table_count + module->import_table_count) {
  3707. set_error_buf(error_buf, error_buf_size,
  3708. "unknown table");
  3709. return false;
  3710. }
  3711. break;
  3712. /* memory index */
  3713. case EXPORT_KIND_MEMORY:
  3714. if (index
  3715. >= module->memory_count + module->import_memory_count) {
  3716. set_error_buf(error_buf, error_buf_size,
  3717. "unknown memory");
  3718. return false;
  3719. }
  3720. break;
  3721. #if WASM_ENABLE_TAGS != 0
  3722. /* export tag */
  3723. case EXPORT_KIND_TAG:
  3724. if (index >= module->tag_count + module->import_tag_count) {
  3725. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3726. return false;
  3727. }
  3728. break;
  3729. #endif
  3730. /* global index */
  3731. case EXPORT_KIND_GLOBAL:
  3732. if (index
  3733. >= module->global_count + module->import_global_count) {
  3734. set_error_buf(error_buf, error_buf_size,
  3735. "unknown global");
  3736. return false;
  3737. }
  3738. break;
  3739. default:
  3740. set_error_buf(error_buf, error_buf_size,
  3741. "invalid export kind");
  3742. return false;
  3743. }
  3744. }
  3745. }
  3746. if (p != p_end) {
  3747. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3748. return false;
  3749. }
  3750. LOG_VERBOSE("Load export section success.\n");
  3751. return true;
  3752. fail:
  3753. return false;
  3754. }
  3755. static bool
  3756. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3757. uint32 error_buf_size)
  3758. {
  3759. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3760. if (table_index != 0) {
  3761. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3762. return false;
  3763. }
  3764. #endif
  3765. if (table_index >= module->import_table_count + module->table_count) {
  3766. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3767. table_index);
  3768. return false;
  3769. }
  3770. return true;
  3771. }
  3772. static bool
  3773. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3774. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3775. {
  3776. const uint8 *p = *p_buf, *p_end = buf_end;
  3777. uint32 table_index;
  3778. read_leb_uint32(p, p_end, table_index);
  3779. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3780. return false;
  3781. }
  3782. *p_table_index = table_index;
  3783. *p_buf = p;
  3784. return true;
  3785. fail:
  3786. return false;
  3787. }
  3788. /* Element segments must match element type of table */
  3789. static bool
  3790. check_table_elem_type(WASMModule *module, uint32 table_index,
  3791. uint32 type_from_elem_seg, char *error_buf,
  3792. uint32 error_buf_size)
  3793. {
  3794. uint32 table_declared_elem_type;
  3795. if (table_index < module->import_table_count)
  3796. table_declared_elem_type =
  3797. module->import_tables[table_index].u.table.table_type.elem_type;
  3798. else
  3799. table_declared_elem_type =
  3800. (module->tables + table_index)->table_type.elem_type;
  3801. if (table_declared_elem_type == type_from_elem_seg)
  3802. return true;
  3803. #if WASM_ENABLE_GC != 0
  3804. /*
  3805. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3806. */
  3807. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3808. return true;
  3809. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3810. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3811. return true;
  3812. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3813. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3814. return true;
  3815. #endif
  3816. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3817. return false;
  3818. }
  3819. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3820. static bool
  3821. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3822. uint32 *p_elem_type,
  3823. #if WASM_ENABLE_GC != 0
  3824. WASMRefType **p_elem_ref_type,
  3825. #endif
  3826. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3827. {
  3828. const uint8 *p = *p_buf, *p_end = buf_end;
  3829. uint8 elem_type;
  3830. #if WASM_ENABLE_GC != 0
  3831. WASMRefType elem_ref_type;
  3832. bool need_ref_type_map;
  3833. #endif
  3834. CHECK_BUF(p, p_end, 1);
  3835. elem_type = read_uint8(p);
  3836. if (elemkind_zero) {
  3837. if (elem_type != 0) {
  3838. set_error_buf(error_buf, error_buf_size,
  3839. "invalid reference type or unknown type");
  3840. return false;
  3841. }
  3842. else {
  3843. *p_elem_type = VALUE_TYPE_FUNCREF;
  3844. *p_buf = p;
  3845. return true;
  3846. }
  3847. }
  3848. #if WASM_ENABLE_GC == 0
  3849. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3850. set_error_buf(error_buf, error_buf_size,
  3851. "invalid reference type or unknown type");
  3852. return false;
  3853. }
  3854. *p_elem_type = elem_type;
  3855. #else
  3856. p--;
  3857. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3858. module->type_count, &need_ref_type_map,
  3859. &elem_ref_type, false, error_buf, error_buf_size)) {
  3860. return false;
  3861. }
  3862. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3863. set_error_buf(error_buf, error_buf_size,
  3864. "invalid reference type or unknown type");
  3865. return false;
  3866. }
  3867. *p_elem_type = elem_ref_type.ref_type;
  3868. if (need_ref_type_map) {
  3869. if (!(*p_elem_ref_type =
  3870. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3871. error_buf, error_buf_size))) {
  3872. return false;
  3873. }
  3874. }
  3875. #endif
  3876. *p_buf = p;
  3877. return true;
  3878. fail:
  3879. return false;
  3880. }
  3881. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3882. static bool
  3883. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3884. WASMModule *module, WASMTableSeg *table_segment,
  3885. char *error_buf, uint32 error_buf_size)
  3886. {
  3887. const uint8 *p = *p_buf, *p_end = buf_end;
  3888. uint32 function_count, function_index = 0, i;
  3889. uint64 total_size;
  3890. read_leb_uint32(p, p_end, function_count);
  3891. table_segment->value_count = function_count;
  3892. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3893. if (total_size > 0
  3894. && !(table_segment->init_values =
  3895. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3896. error_buf_size))) {
  3897. return false;
  3898. }
  3899. for (i = 0; i < function_count; i++) {
  3900. InitializerExpression *init_expr = &table_segment->init_values[i];
  3901. read_leb_uint32(p, p_end, function_index);
  3902. if (!check_function_index(module, function_index, error_buf,
  3903. error_buf_size)) {
  3904. return false;
  3905. }
  3906. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3907. init_expr->u.ref_index = function_index;
  3908. }
  3909. *p_buf = p;
  3910. return true;
  3911. fail:
  3912. return false;
  3913. }
  3914. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3915. static bool
  3916. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3917. WASMModule *module, WASMTableSeg *table_segment,
  3918. char *error_buf, uint32 error_buf_size)
  3919. {
  3920. const uint8 *p = *p_buf, *p_end = buf_end;
  3921. uint32 ref_count, i;
  3922. uint64 total_size;
  3923. read_leb_uint32(p, p_end, ref_count);
  3924. table_segment->value_count = ref_count;
  3925. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3926. if (total_size > 0
  3927. && !(table_segment->init_values =
  3928. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3929. error_buf_size))) {
  3930. return false;
  3931. }
  3932. for (i = 0; i < ref_count; i++) {
  3933. InitializerExpression *init_expr = &table_segment->init_values[i];
  3934. if (!load_init_expr(module, &p, p_end, init_expr,
  3935. table_segment->elem_type,
  3936. #if WASM_ENABLE_GC == 0
  3937. NULL,
  3938. #else
  3939. table_segment->elem_ref_type,
  3940. #endif
  3941. error_buf, error_buf_size))
  3942. return false;
  3943. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3944. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3945. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3946. && init_expr->init_expr_type
  3947. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3948. }
  3949. *p_buf = p;
  3950. return true;
  3951. fail:
  3952. return false;
  3953. }
  3954. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3955. static bool
  3956. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3957. WASMModule *module, char *error_buf,
  3958. uint32 error_buf_size)
  3959. {
  3960. const uint8 *p = buf, *p_end = buf_end;
  3961. uint32 table_segment_count, i;
  3962. uint64 total_size;
  3963. WASMTableSeg *table_segment;
  3964. read_leb_uint32(p, p_end, table_segment_count);
  3965. if (table_segment_count) {
  3966. module->table_seg_count = table_segment_count;
  3967. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3968. if (!(module->table_segments =
  3969. loader_malloc(total_size, error_buf, error_buf_size))) {
  3970. return false;
  3971. }
  3972. table_segment = module->table_segments;
  3973. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3974. if (p >= p_end) {
  3975. set_error_buf(error_buf, error_buf_size,
  3976. "invalid value type or "
  3977. "invalid elements segment kind");
  3978. return false;
  3979. }
  3980. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3981. read_leb_uint32(p, p_end, table_segment->mode);
  3982. /* last three bits */
  3983. table_segment->mode = table_segment->mode & 0x07;
  3984. switch (table_segment->mode) {
  3985. /* elemkind/elemtype + active */
  3986. case 0:
  3987. case 4:
  3988. {
  3989. #if WASM_ENABLE_GC != 0
  3990. if (table_segment->mode == 0) {
  3991. /* vec(funcidx), set elem type to (ref func) */
  3992. WASMRefType elem_ref_type = { 0 };
  3993. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3994. wasm_set_refheaptype_common(
  3995. &elem_ref_type.ref_ht_common, false,
  3996. HEAP_TYPE_FUNC);
  3997. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3998. module->ref_type_set, &elem_ref_type,
  3999. error_buf, error_buf_size)))
  4000. return false;
  4001. }
  4002. else {
  4003. /* vec(expr), set elem type to funcref */
  4004. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4005. }
  4006. #else
  4007. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4008. #endif
  4009. table_segment->table_index = 0;
  4010. if (!check_table_index(module, table_segment->table_index,
  4011. error_buf, error_buf_size))
  4012. return false;
  4013. if (!load_init_expr(
  4014. module, &p, p_end, &table_segment->base_offset,
  4015. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4016. return false;
  4017. if (table_segment->mode == 0) {
  4018. /* vec(funcidx) */
  4019. if (!load_func_index_vec(&p, p_end, module,
  4020. table_segment, error_buf,
  4021. error_buf_size))
  4022. return false;
  4023. }
  4024. else {
  4025. /* vec(expr) */
  4026. if (!load_init_expr_vec(&p, p_end, module,
  4027. table_segment, error_buf,
  4028. error_buf_size))
  4029. return false;
  4030. }
  4031. if (!check_table_elem_type(module,
  4032. table_segment->table_index,
  4033. table_segment->elem_type,
  4034. error_buf, error_buf_size))
  4035. return false;
  4036. break;
  4037. }
  4038. /* elemkind + passive/declarative */
  4039. case 1:
  4040. case 3:
  4041. if (!load_elem_type(module, &p, p_end,
  4042. &table_segment->elem_type,
  4043. #if WASM_ENABLE_GC != 0
  4044. &table_segment->elem_ref_type,
  4045. #endif
  4046. true, error_buf, error_buf_size))
  4047. return false;
  4048. /* vec(funcidx) */
  4049. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4050. error_buf, error_buf_size))
  4051. return false;
  4052. break;
  4053. /* elemkind/elemtype + table_idx + active */
  4054. case 2:
  4055. case 6:
  4056. if (!load_table_index(&p, p_end, module,
  4057. &table_segment->table_index,
  4058. error_buf, error_buf_size))
  4059. return false;
  4060. if (!load_init_expr(
  4061. module, &p, p_end, &table_segment->base_offset,
  4062. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4063. return false;
  4064. if (!load_elem_type(module, &p, p_end,
  4065. &table_segment->elem_type,
  4066. #if WASM_ENABLE_GC != 0
  4067. &table_segment->elem_ref_type,
  4068. #endif
  4069. table_segment->mode == 2 ? true : false,
  4070. error_buf, error_buf_size))
  4071. return false;
  4072. if (table_segment->mode == 2) {
  4073. /* vec(funcidx) */
  4074. if (!load_func_index_vec(&p, p_end, module,
  4075. table_segment, error_buf,
  4076. error_buf_size))
  4077. return false;
  4078. }
  4079. else {
  4080. /* vec(expr) */
  4081. if (!load_init_expr_vec(&p, p_end, module,
  4082. table_segment, error_buf,
  4083. error_buf_size))
  4084. return false;
  4085. }
  4086. if (!check_table_elem_type(module,
  4087. table_segment->table_index,
  4088. table_segment->elem_type,
  4089. error_buf, error_buf_size))
  4090. return false;
  4091. break;
  4092. case 5:
  4093. case 7:
  4094. if (!load_elem_type(module, &p, p_end,
  4095. &table_segment->elem_type,
  4096. #if WASM_ENABLE_GC != 0
  4097. &table_segment->elem_ref_type,
  4098. #endif
  4099. false, error_buf, error_buf_size))
  4100. return false;
  4101. /* vec(expr) */
  4102. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4103. error_buf, error_buf_size))
  4104. return false;
  4105. break;
  4106. default:
  4107. set_error_buf(error_buf, error_buf_size,
  4108. "unknown element segment kind");
  4109. return false;
  4110. }
  4111. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4112. /*
  4113. * like: 00 41 05 0b 04 00 01 00 01
  4114. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4115. */
  4116. if (!load_table_index(&p, p_end, module,
  4117. &table_segment->table_index, error_buf,
  4118. error_buf_size))
  4119. return false;
  4120. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4121. VALUE_TYPE_I32, NULL, error_buf,
  4122. error_buf_size))
  4123. return false;
  4124. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4125. error_buf, error_buf_size))
  4126. return false;
  4127. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4128. if (!check_table_elem_type(module, table_segment->table_index,
  4129. table_segment->elem_type, error_buf,
  4130. error_buf_size))
  4131. return false;
  4132. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4133. #if WASM_ENABLE_WAMR_COMPILER != 0
  4134. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4135. module->is_ref_types_used = true;
  4136. #endif
  4137. }
  4138. }
  4139. if (p != p_end) {
  4140. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4141. return false;
  4142. }
  4143. LOG_VERBOSE("Load table segment section success.\n");
  4144. return true;
  4145. fail:
  4146. return false;
  4147. }
  4148. static bool
  4149. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4150. WASMModule *module,
  4151. #if WASM_ENABLE_BULK_MEMORY != 0
  4152. bool has_datacount_section,
  4153. #endif
  4154. bool clone_data_seg, char *error_buf,
  4155. uint32 error_buf_size)
  4156. {
  4157. const uint8 *p = buf, *p_end = buf_end;
  4158. uint32 data_seg_count, i, mem_index, data_seg_len;
  4159. uint64 total_size;
  4160. WASMDataSeg *dataseg;
  4161. InitializerExpression init_expr;
  4162. #if WASM_ENABLE_BULK_MEMORY != 0
  4163. bool is_passive = false;
  4164. uint32 mem_flag;
  4165. #endif
  4166. uint8 mem_offset_type = VALUE_TYPE_I32;
  4167. read_leb_uint32(p, p_end, data_seg_count);
  4168. #if WASM_ENABLE_BULK_MEMORY != 0
  4169. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4170. set_error_buf(error_buf, error_buf_size,
  4171. "data count and data section have inconsistent lengths");
  4172. return false;
  4173. }
  4174. #endif
  4175. if (data_seg_count) {
  4176. module->data_seg_count = data_seg_count;
  4177. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4178. if (!(module->data_segments =
  4179. loader_malloc(total_size, error_buf, error_buf_size))) {
  4180. return false;
  4181. }
  4182. for (i = 0; i < data_seg_count; i++) {
  4183. read_leb_uint32(p, p_end, mem_index);
  4184. #if WASM_ENABLE_BULK_MEMORY != 0
  4185. is_passive = false;
  4186. mem_flag = mem_index & 0x03;
  4187. switch (mem_flag) {
  4188. case 0x01:
  4189. is_passive = true;
  4190. #if WASM_ENABLE_WAMR_COMPILER != 0
  4191. module->is_bulk_memory_used = true;
  4192. #endif
  4193. break;
  4194. case 0x00:
  4195. /* no memory index, treat index as 0 */
  4196. mem_index = 0;
  4197. goto check_mem_index;
  4198. case 0x02:
  4199. /* read following memory index */
  4200. read_leb_uint32(p, p_end, mem_index);
  4201. #if WASM_ENABLE_WAMR_COMPILER != 0
  4202. module->is_bulk_memory_used = true;
  4203. #endif
  4204. check_mem_index:
  4205. if (mem_index
  4206. >= module->import_memory_count + module->memory_count) {
  4207. set_error_buf_v(error_buf, error_buf_size,
  4208. "unknown memory %d", mem_index);
  4209. return false;
  4210. }
  4211. break;
  4212. case 0x03:
  4213. default:
  4214. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4215. return false;
  4216. break;
  4217. }
  4218. #else
  4219. if (mem_index
  4220. >= module->import_memory_count + module->memory_count) {
  4221. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4222. mem_index);
  4223. return false;
  4224. }
  4225. #endif /* WASM_ENABLE_BULK_MEMORY */
  4226. #if WASM_ENABLE_BULK_MEMORY != 0
  4227. if (!is_passive)
  4228. #endif
  4229. {
  4230. #if WASM_ENABLE_MEMORY64 != 0
  4231. /* This memory_flag is from memory instead of data segment */
  4232. uint8 memory_flag;
  4233. if (module->import_memory_count > 0) {
  4234. memory_flag = module->import_memories[mem_index]
  4235. .u.memory.mem_type.flags;
  4236. }
  4237. else {
  4238. memory_flag =
  4239. module
  4240. ->memories[mem_index - module->import_memory_count]
  4241. .flags;
  4242. }
  4243. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4244. : VALUE_TYPE_I32;
  4245. #else
  4246. mem_offset_type = VALUE_TYPE_I32;
  4247. #endif
  4248. }
  4249. #if WASM_ENABLE_BULK_MEMORY != 0
  4250. if (!is_passive)
  4251. #endif
  4252. if (!load_init_expr(module, &p, p_end, &init_expr,
  4253. mem_offset_type, NULL, error_buf,
  4254. error_buf_size))
  4255. return false;
  4256. read_leb_uint32(p, p_end, data_seg_len);
  4257. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4258. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4259. return false;
  4260. }
  4261. #if WASM_ENABLE_BULK_MEMORY != 0
  4262. dataseg->is_passive = is_passive;
  4263. if (!is_passive)
  4264. #endif
  4265. {
  4266. bh_memcpy_s(&dataseg->base_offset,
  4267. sizeof(InitializerExpression), &init_expr,
  4268. sizeof(InitializerExpression));
  4269. dataseg->memory_index = mem_index;
  4270. }
  4271. dataseg->data_length = data_seg_len;
  4272. CHECK_BUF(p, p_end, data_seg_len);
  4273. if (clone_data_seg) {
  4274. if (!(dataseg->data = loader_malloc(
  4275. dataseg->data_length, error_buf, error_buf_size))) {
  4276. return false;
  4277. }
  4278. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4279. data_seg_len);
  4280. }
  4281. else {
  4282. dataseg->data = (uint8 *)p;
  4283. }
  4284. dataseg->is_data_cloned = clone_data_seg;
  4285. p += data_seg_len;
  4286. }
  4287. }
  4288. if (p != p_end) {
  4289. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4290. return false;
  4291. }
  4292. LOG_VERBOSE("Load data segment section success.\n");
  4293. return true;
  4294. fail:
  4295. return false;
  4296. }
  4297. #if WASM_ENABLE_BULK_MEMORY != 0
  4298. static bool
  4299. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4300. WASMModule *module, char *error_buf,
  4301. uint32 error_buf_size)
  4302. {
  4303. const uint8 *p = buf, *p_end = buf_end;
  4304. uint32 data_seg_count1 = 0;
  4305. read_leb_uint32(p, p_end, data_seg_count1);
  4306. module->data_seg_count1 = data_seg_count1;
  4307. if (p != p_end) {
  4308. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4309. return false;
  4310. }
  4311. #if WASM_ENABLE_WAMR_COMPILER != 0
  4312. module->is_bulk_memory_used = true;
  4313. #endif
  4314. LOG_VERBOSE("Load datacount section success.\n");
  4315. return true;
  4316. fail:
  4317. return false;
  4318. }
  4319. #endif
  4320. #if WASM_ENABLE_TAGS != 0
  4321. static bool
  4322. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4323. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4324. uint32 error_buf_size)
  4325. {
  4326. (void)buf_code;
  4327. (void)buf_code_end;
  4328. const uint8 *p = buf, *p_end = buf_end;
  4329. size_t total_size = 0;
  4330. /* number of tags defined in the section */
  4331. uint32 section_tag_count = 0;
  4332. uint8 tag_attribute;
  4333. uint32 tag_type;
  4334. WASMTag *tag = NULL;
  4335. /* get tag count */
  4336. read_leb_uint32(p, p_end, section_tag_count);
  4337. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4338. error_buf, error_buf_size))
  4339. return false;
  4340. module->tag_count = section_tag_count;
  4341. if (section_tag_count) {
  4342. total_size = sizeof(WASMTag *) * module->tag_count;
  4343. if (!(module->tags =
  4344. loader_malloc(total_size, error_buf, error_buf_size))) {
  4345. return false;
  4346. }
  4347. /* load each tag, imported tags precede the tags */
  4348. uint32 tag_index;
  4349. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4350. /* get the one byte attribute */
  4351. CHECK_BUF(p, p_end, 1);
  4352. tag_attribute = read_uint8(p);
  4353. /* get type */
  4354. read_leb_uint32(p, p_end, tag_type);
  4355. /* compare against module->types */
  4356. if (tag_type >= module->type_count) {
  4357. set_error_buf(error_buf, error_buf_size, "unknown type");
  4358. return false;
  4359. }
  4360. /* get return type (must be 0) */
  4361. /* check, that the type of the referred tag returns void */
  4362. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4363. if (func_type->result_count != 0) {
  4364. set_error_buf(error_buf, error_buf_size,
  4365. "non-empty tag result type");
  4366. goto fail;
  4367. }
  4368. if (!(tag = module->tags[tag_index] = loader_malloc(
  4369. sizeof(WASMTag), error_buf, error_buf_size))) {
  4370. return false;
  4371. }
  4372. /* store to module tag declarations */
  4373. tag->attribute = tag_attribute;
  4374. tag->type = tag_type;
  4375. tag->tag_type = func_type;
  4376. }
  4377. }
  4378. if (p != p_end) {
  4379. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4380. return false;
  4381. }
  4382. LOG_VERBOSE("Load tag section success.\n");
  4383. return true;
  4384. fail:
  4385. return false;
  4386. }
  4387. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4388. static bool
  4389. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4390. const uint8 *buf_func_end, WASMModule *module,
  4391. char *error_buf, uint32 error_buf_size)
  4392. {
  4393. const uint8 *p = buf, *p_end = buf_end;
  4394. const uint8 *p_func = buf_func;
  4395. uint32 func_count = 0, code_count;
  4396. /* code has been loaded in function section, so pass it here, just check
  4397. * whether function and code section have inconsistent lengths */
  4398. read_leb_uint32(p, p_end, code_count);
  4399. if (buf_func)
  4400. read_leb_uint32(p_func, buf_func_end, func_count);
  4401. if (func_count != code_count) {
  4402. set_error_buf(error_buf, error_buf_size,
  4403. "function and code section have inconsistent lengths");
  4404. return false;
  4405. }
  4406. LOG_VERBOSE("Load code segment section success.\n");
  4407. (void)module;
  4408. return true;
  4409. fail:
  4410. return false;
  4411. }
  4412. static bool
  4413. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4414. char *error_buf, uint32 error_buf_size)
  4415. {
  4416. const uint8 *p = buf, *p_end = buf_end;
  4417. WASMFuncType *type;
  4418. uint32 start_function;
  4419. read_leb_uint32(p, p_end, start_function);
  4420. if (start_function
  4421. >= module->function_count + module->import_function_count) {
  4422. set_error_buf(error_buf, error_buf_size, "unknown function");
  4423. return false;
  4424. }
  4425. if (start_function < module->import_function_count)
  4426. type = module->import_functions[start_function].u.function.func_type;
  4427. else
  4428. type = module->functions[start_function - module->import_function_count]
  4429. ->func_type;
  4430. if (type->param_count != 0 || type->result_count != 0) {
  4431. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4432. return false;
  4433. }
  4434. module->start_function = start_function;
  4435. if (p != p_end) {
  4436. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4437. return false;
  4438. }
  4439. LOG_VERBOSE("Load start section success.\n");
  4440. return true;
  4441. fail:
  4442. return false;
  4443. }
  4444. #if WASM_ENABLE_STRINGREF != 0
  4445. static bool
  4446. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4447. WASMModule *module, bool is_load_from_file_buf,
  4448. char *error_buf, uint32 error_buf_size)
  4449. {
  4450. const uint8 *p = buf, *p_end = buf_end;
  4451. int32 deferred_count, immediate_count, string_length, i;
  4452. uint64 total_size;
  4453. read_leb_uint32(p, p_end, deferred_count);
  4454. read_leb_uint32(p, p_end, immediate_count);
  4455. /* proposal set deferred_count for future extension */
  4456. if (deferred_count != 0) {
  4457. goto fail;
  4458. }
  4459. if (immediate_count > 0) {
  4460. total_size = sizeof(char *) * (uint64)immediate_count;
  4461. if (!(module->string_literal_ptrs =
  4462. loader_malloc(total_size, error_buf, error_buf_size))) {
  4463. goto fail;
  4464. }
  4465. module->string_literal_count = immediate_count;
  4466. total_size = sizeof(uint32) * (uint64)immediate_count;
  4467. if (!(module->string_literal_lengths =
  4468. loader_malloc(total_size, error_buf, error_buf_size))) {
  4469. goto fail;
  4470. }
  4471. for (i = 0; i < immediate_count; i++) {
  4472. read_leb_uint32(p, p_end, string_length);
  4473. CHECK_BUF(p, p_end, string_length);
  4474. module->string_literal_ptrs[i] = p;
  4475. module->string_literal_lengths[i] = string_length;
  4476. p += string_length;
  4477. }
  4478. }
  4479. if (p != p_end) {
  4480. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4481. goto fail;
  4482. }
  4483. LOG_VERBOSE("Load stringref section success.\n");
  4484. return true;
  4485. fail:
  4486. return false;
  4487. }
  4488. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4489. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4490. static bool
  4491. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4492. bool is_load_from_file_buf, char *error_buf,
  4493. uint32 error_buf_size)
  4494. {
  4495. const uint8 *p = buf, *p_end = buf_end;
  4496. uint32 name_type, subsection_size;
  4497. uint32 previous_name_type = 0;
  4498. uint32 num_func_name;
  4499. uint32 func_index;
  4500. uint32 previous_func_index = ~0U;
  4501. uint32 func_name_len;
  4502. uint32 name_index;
  4503. int i = 0;
  4504. if (p >= p_end) {
  4505. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4506. return false;
  4507. }
  4508. while (p < p_end) {
  4509. read_leb_uint32(p, p_end, name_type);
  4510. if (i != 0) {
  4511. if (name_type == previous_name_type) {
  4512. set_error_buf(error_buf, error_buf_size,
  4513. "duplicate sub-section");
  4514. return false;
  4515. }
  4516. if (name_type < previous_name_type) {
  4517. set_error_buf(error_buf, error_buf_size,
  4518. "out-of-order sub-section");
  4519. return false;
  4520. }
  4521. }
  4522. previous_name_type = name_type;
  4523. read_leb_uint32(p, p_end, subsection_size);
  4524. CHECK_BUF(p, p_end, subsection_size);
  4525. switch (name_type) {
  4526. case SUB_SECTION_TYPE_FUNC:
  4527. if (subsection_size) {
  4528. read_leb_uint32(p, p_end, num_func_name);
  4529. for (name_index = 0; name_index < num_func_name;
  4530. name_index++) {
  4531. read_leb_uint32(p, p_end, func_index);
  4532. if (func_index == previous_func_index) {
  4533. set_error_buf(error_buf, error_buf_size,
  4534. "duplicate function name");
  4535. return false;
  4536. }
  4537. if (func_index < previous_func_index
  4538. && previous_func_index != ~0U) {
  4539. set_error_buf(error_buf, error_buf_size,
  4540. "out-of-order function index ");
  4541. return false;
  4542. }
  4543. previous_func_index = func_index;
  4544. read_leb_uint32(p, p_end, func_name_len);
  4545. CHECK_BUF(p, p_end, func_name_len);
  4546. /* Skip the import functions */
  4547. if (func_index >= module->import_function_count) {
  4548. func_index -= module->import_function_count;
  4549. if (func_index >= module->function_count) {
  4550. set_error_buf(error_buf, error_buf_size,
  4551. "out-of-range function index");
  4552. return false;
  4553. }
  4554. if (!(module->functions[func_index]->field_name =
  4555. wasm_const_str_list_insert(
  4556. p, func_name_len, module,
  4557. #if WASM_ENABLE_WAMR_COMPILER != 0
  4558. false,
  4559. #else
  4560. is_load_from_file_buf,
  4561. #endif
  4562. error_buf, error_buf_size))) {
  4563. return false;
  4564. }
  4565. }
  4566. p += func_name_len;
  4567. }
  4568. }
  4569. break;
  4570. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4571. */
  4572. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4573. default:
  4574. p = p + subsection_size;
  4575. break;
  4576. }
  4577. i++;
  4578. }
  4579. return true;
  4580. fail:
  4581. return false;
  4582. }
  4583. #endif
  4584. static bool
  4585. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4586. bool is_load_from_file_buf, char *error_buf,
  4587. uint32 error_buf_size)
  4588. {
  4589. const uint8 *p = buf, *p_end = buf_end;
  4590. char section_name[32];
  4591. uint32 name_len, buffer_len;
  4592. if (p >= p_end) {
  4593. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4594. return false;
  4595. }
  4596. read_leb_uint32(p, p_end, name_len);
  4597. if (p + name_len > p_end) {
  4598. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4599. return false;
  4600. }
  4601. if (!wasm_check_utf8_str(p, name_len)) {
  4602. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4603. return false;
  4604. }
  4605. buffer_len = sizeof(section_name);
  4606. memset(section_name, 0, buffer_len);
  4607. if (name_len < buffer_len) {
  4608. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4609. }
  4610. else {
  4611. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4612. memset(section_name + buffer_len - 4, '.', 3);
  4613. }
  4614. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4615. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4616. module->name_section_buf = buf;
  4617. module->name_section_buf_end = buf_end;
  4618. p += name_len;
  4619. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4620. error_buf, error_buf_size)) {
  4621. return false;
  4622. }
  4623. LOG_VERBOSE("Load custom name section success.");
  4624. }
  4625. #endif
  4626. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4627. {
  4628. WASMCustomSection *section =
  4629. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4630. if (!section) {
  4631. return false;
  4632. }
  4633. section->name_addr = (char *)p;
  4634. section->name_len = name_len;
  4635. section->content_addr = (uint8 *)(p + name_len);
  4636. section->content_len = (uint32)(p_end - p - name_len);
  4637. section->next = module->custom_section_list;
  4638. module->custom_section_list = section;
  4639. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4640. return true;
  4641. }
  4642. #endif
  4643. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4644. (void)is_load_from_file_buf;
  4645. (void)module;
  4646. return true;
  4647. fail:
  4648. return false;
  4649. }
  4650. static void
  4651. calculate_global_data_offset(WASMModule *module)
  4652. {
  4653. uint32 i, data_offset;
  4654. data_offset = 0;
  4655. for (i = 0; i < module->import_global_count; i++) {
  4656. WASMGlobalImport *import_global =
  4657. &((module->import_globals + i)->u.global);
  4658. #if WASM_ENABLE_FAST_JIT != 0
  4659. import_global->data_offset = data_offset;
  4660. #endif
  4661. data_offset += wasm_value_type_size(import_global->type.val_type);
  4662. }
  4663. for (i = 0; i < module->global_count; i++) {
  4664. WASMGlobal *global = module->globals + i;
  4665. #if WASM_ENABLE_FAST_JIT != 0
  4666. global->data_offset = data_offset;
  4667. #endif
  4668. data_offset += wasm_value_type_size(global->type.val_type);
  4669. }
  4670. module->global_data_size = data_offset;
  4671. }
  4672. #if WASM_ENABLE_FAST_JIT != 0
  4673. static bool
  4674. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4675. uint32 error_buf_size)
  4676. {
  4677. #if WASM_ENABLE_LAZY_JIT != 0
  4678. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4679. #endif
  4680. uint32 i;
  4681. if (!module->function_count)
  4682. return true;
  4683. if (!(module->fast_jit_func_ptrs =
  4684. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4685. error_buf_size))) {
  4686. return false;
  4687. }
  4688. #if WASM_ENABLE_LAZY_JIT != 0
  4689. for (i = 0; i < module->function_count; i++) {
  4690. module->fast_jit_func_ptrs[i] =
  4691. jit_globals->compile_fast_jit_and_then_call;
  4692. }
  4693. #endif
  4694. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4695. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4696. set_error_buf(error_buf, error_buf_size,
  4697. "init fast jit thread lock failed");
  4698. return false;
  4699. }
  4700. module->fast_jit_thread_locks_inited[i] = true;
  4701. }
  4702. return true;
  4703. }
  4704. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4705. #if WASM_ENABLE_JIT != 0
  4706. static bool
  4707. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4708. uint32 error_buf_size)
  4709. {
  4710. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4711. AOTCompOption option = { 0 };
  4712. char *aot_last_error;
  4713. uint64 size;
  4714. #if WASM_ENABLE_GC != 0
  4715. bool gc_enabled = true;
  4716. #else
  4717. bool gc_enabled = false;
  4718. #endif
  4719. if (module->function_count == 0)
  4720. return true;
  4721. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4722. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4723. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4724. return false;
  4725. }
  4726. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4727. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4728. os_mutex_destroy(&module->tierup_wait_lock);
  4729. return false;
  4730. }
  4731. module->tierup_wait_lock_inited = true;
  4732. #endif
  4733. size = sizeof(void *) * (uint64)module->function_count
  4734. + sizeof(bool) * (uint64)module->function_count;
  4735. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4736. return false;
  4737. }
  4738. module->func_ptrs_compiled =
  4739. (bool *)((uint8 *)module->func_ptrs
  4740. + sizeof(void *) * module->function_count);
  4741. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4742. if (!module->comp_data) {
  4743. aot_last_error = aot_get_last_error();
  4744. bh_assert(aot_last_error != NULL);
  4745. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4746. return false;
  4747. }
  4748. option.is_jit_mode = true;
  4749. option.opt_level = llvm_jit_options->opt_level;
  4750. option.size_level = llvm_jit_options->size_level;
  4751. option.segue_flags = llvm_jit_options->segue_flags;
  4752. option.quick_invoke_c_api_import =
  4753. llvm_jit_options->quick_invoke_c_api_import;
  4754. #if WASM_ENABLE_BULK_MEMORY != 0
  4755. option.enable_bulk_memory = true;
  4756. #endif
  4757. #if WASM_ENABLE_THREAD_MGR != 0
  4758. option.enable_thread_mgr = true;
  4759. #endif
  4760. #if WASM_ENABLE_TAIL_CALL != 0
  4761. option.enable_tail_call = true;
  4762. #endif
  4763. #if WASM_ENABLE_SIMD != 0
  4764. option.enable_simd = true;
  4765. #endif
  4766. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4767. option.enable_ref_types = true;
  4768. #elif WASM_ENABLE_GC != 0
  4769. option.enable_gc = true;
  4770. #endif
  4771. option.enable_aux_stack_check = true;
  4772. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4773. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4774. option.enable_aux_stack_frame = true;
  4775. #endif
  4776. #if WASM_ENABLE_PERF_PROFILING != 0
  4777. option.enable_perf_profiling = true;
  4778. #endif
  4779. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4780. option.enable_memory_profiling = true;
  4781. option.enable_stack_estimation = true;
  4782. #endif
  4783. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4784. if (!module->comp_ctx) {
  4785. aot_last_error = aot_get_last_error();
  4786. bh_assert(aot_last_error != NULL);
  4787. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4788. return false;
  4789. }
  4790. return true;
  4791. }
  4792. static bool
  4793. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4794. uint32 error_buf_size)
  4795. {
  4796. char *aot_last_error;
  4797. uint32 i;
  4798. if (module->function_count == 0)
  4799. return true;
  4800. if (!aot_compile_wasm(module->comp_ctx)) {
  4801. aot_last_error = aot_get_last_error();
  4802. bh_assert(aot_last_error != NULL);
  4803. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4804. return false;
  4805. }
  4806. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4807. if (module->orcjit_stop_compiling)
  4808. return false;
  4809. #endif
  4810. bh_print_time("Begin to lookup llvm jit functions");
  4811. for (i = 0; i < module->function_count; i++) {
  4812. LLVMOrcJITTargetAddress func_addr = 0;
  4813. LLVMErrorRef error;
  4814. char func_name[48];
  4815. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4816. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4817. func_name);
  4818. if (error != LLVMErrorSuccess) {
  4819. char *err_msg = LLVMGetErrorMessage(error);
  4820. set_error_buf_v(error_buf, error_buf_size,
  4821. "failed to compile llvm jit function: %s", err_msg);
  4822. LLVMDisposeErrorMessage(err_msg);
  4823. return false;
  4824. }
  4825. /**
  4826. * No need to lock the func_ptr[func_idx] here as it is basic
  4827. * data type, the load/store for it can be finished by one cpu
  4828. * instruction, and there can be only one cpu instruction
  4829. * loading/storing at the same time.
  4830. */
  4831. module->func_ptrs[i] = (void *)func_addr;
  4832. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4833. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4834. if (module->orcjit_stop_compiling)
  4835. return false;
  4836. #endif
  4837. }
  4838. bh_print_time("End lookup llvm jit functions");
  4839. return true;
  4840. }
  4841. #endif /* end of WASM_ENABLE_JIT != 0 */
  4842. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4843. && WASM_ENABLE_LAZY_JIT != 0
  4844. static void *
  4845. init_llvm_jit_functions_stage2_callback(void *arg)
  4846. {
  4847. WASMModule *module = (WASMModule *)arg;
  4848. char error_buf[128];
  4849. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4850. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4851. module->orcjit_stop_compiling = true;
  4852. return NULL;
  4853. }
  4854. os_mutex_lock(&module->tierup_wait_lock);
  4855. module->llvm_jit_inited = true;
  4856. os_cond_broadcast(&module->tierup_wait_cond);
  4857. os_mutex_unlock(&module->tierup_wait_lock);
  4858. return NULL;
  4859. }
  4860. #endif
  4861. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4862. /* The callback function to compile jit functions */
  4863. static void *
  4864. orcjit_thread_callback(void *arg)
  4865. {
  4866. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4867. #if WASM_ENABLE_JIT != 0
  4868. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4869. #endif
  4870. WASMModule *module = thread_arg->module;
  4871. uint32 group_idx = thread_arg->group_idx;
  4872. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4873. uint32 func_count = module->function_count;
  4874. uint32 i;
  4875. #if WASM_ENABLE_FAST_JIT != 0
  4876. /* Compile fast jit functions of this group */
  4877. for (i = group_idx; i < func_count; i += group_stride) {
  4878. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4879. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4880. break;
  4881. }
  4882. if (module->orcjit_stop_compiling) {
  4883. return NULL;
  4884. }
  4885. }
  4886. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4887. os_mutex_lock(&module->tierup_wait_lock);
  4888. module->fast_jit_ready_groups++;
  4889. os_mutex_unlock(&module->tierup_wait_lock);
  4890. #endif
  4891. #endif
  4892. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4893. && WASM_ENABLE_LAZY_JIT != 0
  4894. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4895. for (i = group_idx; i < func_count;
  4896. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4897. uint32 j;
  4898. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4899. if (i + j * group_stride < func_count) {
  4900. if (!jit_compiler_set_call_to_fast_jit(
  4901. module,
  4902. i + j * group_stride + module->import_function_count)) {
  4903. LOG_ERROR(
  4904. "failed to compile call_to_fast_jit for func %u\n",
  4905. i + j * group_stride + module->import_function_count);
  4906. module->orcjit_stop_compiling = true;
  4907. return NULL;
  4908. }
  4909. }
  4910. if (module->orcjit_stop_compiling) {
  4911. return NULL;
  4912. }
  4913. }
  4914. }
  4915. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4916. fast jit functions are compiled */
  4917. os_mutex_lock(&module->tierup_wait_lock);
  4918. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4919. && module->fast_jit_ready_groups >= group_stride)) {
  4920. os_cond_reltimedwait(&module->tierup_wait_cond,
  4921. &module->tierup_wait_lock, 10000);
  4922. if (module->orcjit_stop_compiling) {
  4923. /* init_llvm_jit_functions_stage2 failed */
  4924. os_mutex_unlock(&module->tierup_wait_lock);
  4925. return NULL;
  4926. }
  4927. }
  4928. os_mutex_unlock(&module->tierup_wait_lock);
  4929. #endif
  4930. #if WASM_ENABLE_JIT != 0
  4931. /* Compile llvm jit functions of this group */
  4932. for (i = group_idx; i < func_count;
  4933. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4934. LLVMOrcJITTargetAddress func_addr = 0;
  4935. LLVMErrorRef error;
  4936. char func_name[48];
  4937. typedef void (*F)(void);
  4938. union {
  4939. F f;
  4940. void *v;
  4941. } u;
  4942. uint32 j;
  4943. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4944. "_wrapper");
  4945. LOG_DEBUG("compile llvm jit func %s", func_name);
  4946. error =
  4947. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4948. if (error != LLVMErrorSuccess) {
  4949. char *err_msg = LLVMGetErrorMessage(error);
  4950. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4951. LLVMDisposeErrorMessage(err_msg);
  4952. break;
  4953. }
  4954. /* Call the jit wrapper function to trigger its compilation, so as
  4955. to compile the actual jit functions, since we add the latter to
  4956. function list in the PartitionFunction callback */
  4957. u.v = (void *)func_addr;
  4958. u.f();
  4959. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4960. if (i + j * group_stride < func_count) {
  4961. module->func_ptrs_compiled[i + j * group_stride] = true;
  4962. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4963. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4964. i + j * group_stride);
  4965. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4966. func_name);
  4967. if (error != LLVMErrorSuccess) {
  4968. char *err_msg = LLVMGetErrorMessage(error);
  4969. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4970. err_msg);
  4971. LLVMDisposeErrorMessage(err_msg);
  4972. /* Ignore current llvm jit func, as its func ptr is
  4973. previous set to call_to_fast_jit, which also works */
  4974. continue;
  4975. }
  4976. jit_compiler_set_llvm_jit_func_ptr(
  4977. module,
  4978. i + j * group_stride + module->import_function_count,
  4979. (void *)func_addr);
  4980. /* Try to switch to call this llvm jit function instead of
  4981. fast jit function from fast jit jitted code */
  4982. jit_compiler_set_call_to_llvm_jit(
  4983. module,
  4984. i + j * group_stride + module->import_function_count);
  4985. #endif
  4986. }
  4987. }
  4988. if (module->orcjit_stop_compiling) {
  4989. break;
  4990. }
  4991. }
  4992. #endif
  4993. return NULL;
  4994. }
  4995. static void
  4996. orcjit_stop_compile_threads(WASMModule *module)
  4997. {
  4998. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4999. / sizeof(OrcJitThreadArg));
  5000. module->orcjit_stop_compiling = true;
  5001. for (i = 0; i < thread_num; i++) {
  5002. if (module->orcjit_threads[i])
  5003. os_thread_join(module->orcjit_threads[i], NULL);
  5004. }
  5005. }
  5006. static bool
  5007. compile_jit_functions(WASMModule *module, char *error_buf,
  5008. uint32 error_buf_size)
  5009. {
  5010. uint32 thread_num =
  5011. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5012. uint32 i, j;
  5013. bh_print_time("Begin to compile jit functions");
  5014. /* Create threads to compile the jit functions */
  5015. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5016. #if WASM_ENABLE_JIT != 0
  5017. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5018. #endif
  5019. module->orcjit_thread_args[i].module = module;
  5020. module->orcjit_thread_args[i].group_idx = i;
  5021. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5022. (void *)&module->orcjit_thread_args[i],
  5023. APP_THREAD_STACK_SIZE_DEFAULT)
  5024. != 0) {
  5025. set_error_buf(error_buf, error_buf_size,
  5026. "create orcjit compile thread failed");
  5027. /* Terminate the threads created */
  5028. module->orcjit_stop_compiling = true;
  5029. for (j = 0; j < i; j++) {
  5030. os_thread_join(module->orcjit_threads[j], NULL);
  5031. }
  5032. return false;
  5033. }
  5034. }
  5035. #if WASM_ENABLE_LAZY_JIT == 0
  5036. /* Wait until all jit functions are compiled for eager mode */
  5037. for (i = 0; i < thread_num; i++) {
  5038. if (module->orcjit_threads[i])
  5039. os_thread_join(module->orcjit_threads[i], NULL);
  5040. }
  5041. #if WASM_ENABLE_FAST_JIT != 0
  5042. /* Ensure all the fast-jit functions are compiled */
  5043. for (i = 0; i < module->function_count; i++) {
  5044. if (!jit_compiler_is_compiled(module,
  5045. i + module->import_function_count)) {
  5046. set_error_buf(error_buf, error_buf_size,
  5047. "failed to compile fast jit function");
  5048. return false;
  5049. }
  5050. }
  5051. #endif
  5052. #if WASM_ENABLE_JIT != 0
  5053. /* Ensure all the llvm-jit functions are compiled */
  5054. for (i = 0; i < module->function_count; i++) {
  5055. if (!module->func_ptrs_compiled[i]) {
  5056. set_error_buf(error_buf, error_buf_size,
  5057. "failed to compile llvm jit function");
  5058. return false;
  5059. }
  5060. }
  5061. #endif
  5062. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5063. bh_print_time("End compile jit functions");
  5064. return true;
  5065. }
  5066. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5067. static bool
  5068. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5069. uint32 cur_func_idx, char *error_buf,
  5070. uint32 error_buf_size);
  5071. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5072. void **
  5073. wasm_interp_get_handle_table();
  5074. static void **handle_table;
  5075. #endif
  5076. static bool
  5077. load_from_sections(WASMModule *module, WASMSection *sections,
  5078. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5079. char *error_buf, uint32 error_buf_size)
  5080. {
  5081. WASMExport *export;
  5082. WASMSection *section = sections;
  5083. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5084. *buf_func = NULL, *buf_func_end = NULL;
  5085. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5086. WASMGlobal *aux_stack_top_global = NULL, *global;
  5087. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5088. aux_stack_top = (uint64)-1LL;
  5089. uint32 global_index, func_index, i;
  5090. uint32 aux_data_end_global_index = (uint32)-1;
  5091. uint32 aux_heap_base_global_index = (uint32)-1;
  5092. WASMFuncType *func_type;
  5093. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5094. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5095. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5096. #if WASM_ENABLE_BULK_MEMORY != 0
  5097. bool has_datacount_section = false;
  5098. #endif
  5099. /* Find code and function sections if have */
  5100. while (section) {
  5101. if (section->section_type == SECTION_TYPE_CODE) {
  5102. buf_code = section->section_body;
  5103. buf_code_end = buf_code + section->section_body_size;
  5104. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5105. module->buf_code = (uint8 *)buf_code;
  5106. module->buf_code_size = section->section_body_size;
  5107. #endif
  5108. }
  5109. else if (section->section_type == SECTION_TYPE_FUNC) {
  5110. buf_func = section->section_body;
  5111. buf_func_end = buf_func + section->section_body_size;
  5112. }
  5113. section = section->next;
  5114. }
  5115. section = sections;
  5116. while (section) {
  5117. buf = section->section_body;
  5118. buf_end = buf + section->section_body_size;
  5119. switch (section->section_type) {
  5120. case SECTION_TYPE_USER:
  5121. /* unsupported user section, ignore it. */
  5122. if (!load_user_section(buf, buf_end, module,
  5123. reuse_const_strings, error_buf,
  5124. error_buf_size))
  5125. return false;
  5126. break;
  5127. case SECTION_TYPE_TYPE:
  5128. if (!load_type_section(buf, buf_end, module, error_buf,
  5129. error_buf_size))
  5130. return false;
  5131. break;
  5132. case SECTION_TYPE_IMPORT:
  5133. if (!load_import_section(buf, buf_end, module,
  5134. reuse_const_strings, error_buf,
  5135. error_buf_size))
  5136. return false;
  5137. break;
  5138. case SECTION_TYPE_FUNC:
  5139. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5140. module, error_buf, error_buf_size))
  5141. return false;
  5142. break;
  5143. case SECTION_TYPE_TABLE:
  5144. if (!load_table_section(buf, buf_end, module, error_buf,
  5145. error_buf_size))
  5146. return false;
  5147. break;
  5148. case SECTION_TYPE_MEMORY:
  5149. if (!load_memory_section(buf, buf_end, module, error_buf,
  5150. error_buf_size))
  5151. return false;
  5152. break;
  5153. #if WASM_ENABLE_TAGS != 0
  5154. case SECTION_TYPE_TAG:
  5155. /* load tag declaration section */
  5156. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5157. module, error_buf, error_buf_size))
  5158. return false;
  5159. break;
  5160. #endif
  5161. case SECTION_TYPE_GLOBAL:
  5162. if (!load_global_section(buf, buf_end, module, error_buf,
  5163. error_buf_size))
  5164. return false;
  5165. break;
  5166. case SECTION_TYPE_EXPORT:
  5167. if (!load_export_section(buf, buf_end, module,
  5168. reuse_const_strings, error_buf,
  5169. error_buf_size))
  5170. return false;
  5171. break;
  5172. case SECTION_TYPE_START:
  5173. if (!load_start_section(buf, buf_end, module, error_buf,
  5174. error_buf_size))
  5175. return false;
  5176. break;
  5177. case SECTION_TYPE_ELEM:
  5178. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5179. error_buf_size))
  5180. return false;
  5181. break;
  5182. case SECTION_TYPE_CODE:
  5183. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5184. module, error_buf, error_buf_size))
  5185. return false;
  5186. break;
  5187. case SECTION_TYPE_DATA:
  5188. if (!load_data_segment_section(buf, buf_end, module,
  5189. #if WASM_ENABLE_BULK_MEMORY != 0
  5190. has_datacount_section,
  5191. #endif
  5192. clone_data_seg, error_buf,
  5193. error_buf_size))
  5194. return false;
  5195. break;
  5196. #if WASM_ENABLE_BULK_MEMORY != 0
  5197. case SECTION_TYPE_DATACOUNT:
  5198. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5199. error_buf_size))
  5200. return false;
  5201. has_datacount_section = true;
  5202. break;
  5203. #endif
  5204. #if WASM_ENABLE_STRINGREF != 0
  5205. case SECTION_TYPE_STRINGREF:
  5206. if (!load_stringref_section(buf, buf_end, module,
  5207. reuse_const_strings, error_buf,
  5208. error_buf_size))
  5209. return false;
  5210. break;
  5211. #endif
  5212. default:
  5213. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5214. return false;
  5215. }
  5216. section = section->next;
  5217. }
  5218. module->aux_data_end_global_index = (uint32)-1;
  5219. module->aux_heap_base_global_index = (uint32)-1;
  5220. module->aux_stack_top_global_index = (uint32)-1;
  5221. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5222. export = module->exports;
  5223. for (i = 0; i < module->export_count; i++, export ++) {
  5224. if (export->kind == EXPORT_KIND_GLOBAL) {
  5225. if (!strcmp(export->name, "__heap_base")) {
  5226. global_index = export->index - module->import_global_count;
  5227. global = module->globals + global_index;
  5228. if (global->type.val_type == VALUE_TYPE_I32
  5229. && !global->type.is_mutable
  5230. && global->init_expr.init_expr_type
  5231. == INIT_EXPR_TYPE_I32_CONST) {
  5232. aux_heap_base_global = global;
  5233. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5234. aux_heap_base_global_index = export->index;
  5235. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5236. aux_heap_base);
  5237. }
  5238. }
  5239. else if (!strcmp(export->name, "__data_end")) {
  5240. global_index = export->index - module->import_global_count;
  5241. global = module->globals + global_index;
  5242. if (global->type.val_type == VALUE_TYPE_I32
  5243. && !global->type.is_mutable
  5244. && global->init_expr.init_expr_type
  5245. == INIT_EXPR_TYPE_I32_CONST) {
  5246. aux_data_end_global = global;
  5247. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5248. aux_data_end_global_index = export->index;
  5249. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5250. aux_data_end);
  5251. aux_data_end = align_uint64(aux_data_end, 16);
  5252. }
  5253. }
  5254. /* For module compiled with -pthread option, the global is:
  5255. [0] stack_top <-- 0
  5256. [1] tls_pointer
  5257. [2] tls_size
  5258. [3] data_end <-- 3
  5259. [4] global_base
  5260. [5] heap_base <-- 5
  5261. [6] dso_handle
  5262. For module compiled without -pthread option:
  5263. [0] stack_top <-- 0
  5264. [1] data_end <-- 1
  5265. [2] global_base
  5266. [3] heap_base <-- 3
  5267. [4] dso_handle
  5268. */
  5269. if (aux_data_end_global && aux_heap_base_global
  5270. && aux_data_end <= aux_heap_base) {
  5271. module->aux_data_end_global_index = aux_data_end_global_index;
  5272. module->aux_data_end = aux_data_end;
  5273. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5274. module->aux_heap_base = aux_heap_base;
  5275. /* Resolve aux stack top global */
  5276. for (global_index = 0; global_index < module->global_count;
  5277. global_index++) {
  5278. global = module->globals + global_index;
  5279. if (global->type.is_mutable /* heap_base and data_end is
  5280. not mutable */
  5281. && global->type.val_type == VALUE_TYPE_I32
  5282. && global->init_expr.init_expr_type
  5283. == INIT_EXPR_TYPE_I32_CONST
  5284. && (uint64)(uint32)global->init_expr.u.i32
  5285. <= aux_heap_base) {
  5286. aux_stack_top_global = global;
  5287. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5288. module->aux_stack_top_global_index =
  5289. module->import_global_count + global_index;
  5290. module->aux_stack_bottom = aux_stack_top;
  5291. module->aux_stack_size =
  5292. aux_stack_top > aux_data_end
  5293. ? (uint32)(aux_stack_top - aux_data_end)
  5294. : (uint32)aux_stack_top;
  5295. LOG_VERBOSE(
  5296. "Found aux stack top global, value: %" PRIu64 ", "
  5297. "global index: %d, stack size: %d",
  5298. aux_stack_top, global_index,
  5299. module->aux_stack_size);
  5300. break;
  5301. }
  5302. }
  5303. if (!aux_stack_top_global) {
  5304. /* Auxiliary stack global isn't found, it must be unused
  5305. in the wasm app, as if it is used, the global must be
  5306. defined. Here we set it to __heap_base global and set
  5307. its size to 0. */
  5308. aux_stack_top_global = aux_heap_base_global;
  5309. aux_stack_top = aux_heap_base;
  5310. module->aux_stack_top_global_index =
  5311. module->aux_heap_base_global_index;
  5312. module->aux_stack_bottom = aux_stack_top;
  5313. module->aux_stack_size = 0;
  5314. }
  5315. break;
  5316. }
  5317. }
  5318. }
  5319. module->malloc_function = (uint32)-1;
  5320. module->free_function = (uint32)-1;
  5321. module->retain_function = (uint32)-1;
  5322. /* Resolve malloc/free function exported by wasm module */
  5323. #if WASM_ENABLE_MEMORY64 != 0
  5324. if (has_module_memory64(module))
  5325. malloc_free_io_type = VALUE_TYPE_I64;
  5326. #endif
  5327. export = module->exports;
  5328. for (i = 0; i < module->export_count; i++, export ++) {
  5329. if (export->kind == EXPORT_KIND_FUNC) {
  5330. if (!strcmp(export->name, "malloc")
  5331. && export->index >= module->import_function_count) {
  5332. func_index = export->index - module->import_function_count;
  5333. func_type = module->functions[func_index]->func_type;
  5334. if (func_type->param_count == 1 && func_type->result_count == 1
  5335. && func_type->types[0] == malloc_free_io_type
  5336. && func_type->types[1] == malloc_free_io_type) {
  5337. bh_assert(module->malloc_function == (uint32)-1);
  5338. module->malloc_function = export->index;
  5339. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5340. export->name, export->index);
  5341. }
  5342. }
  5343. else if (!strcmp(export->name, "__new")
  5344. && export->index >= module->import_function_count) {
  5345. /* __new && __pin for AssemblyScript */
  5346. func_index = export->index - module->import_function_count;
  5347. func_type = module->functions[func_index]->func_type;
  5348. if (func_type->param_count == 2 && func_type->result_count == 1
  5349. && func_type->types[0] == malloc_free_io_type
  5350. && func_type->types[1] == VALUE_TYPE_I32
  5351. && func_type->types[2] == malloc_free_io_type) {
  5352. uint32 j;
  5353. WASMExport *export_tmp;
  5354. bh_assert(module->malloc_function == (uint32)-1);
  5355. module->malloc_function = export->index;
  5356. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5357. export->name, export->index);
  5358. /* resolve retain function.
  5359. If not found, reset malloc function index */
  5360. export_tmp = module->exports;
  5361. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5362. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5363. && (!strcmp(export_tmp->name, "__retain")
  5364. || (!strcmp(export_tmp->name, "__pin")))
  5365. && (export_tmp->index
  5366. >= module->import_function_count)) {
  5367. func_index = export_tmp->index
  5368. - module->import_function_count;
  5369. func_type =
  5370. module->functions[func_index]->func_type;
  5371. if (func_type->param_count == 1
  5372. && func_type->result_count == 1
  5373. && func_type->types[0] == malloc_free_io_type
  5374. && func_type->types[1] == malloc_free_io_type) {
  5375. bh_assert(module->retain_function
  5376. == (uint32)-1);
  5377. module->retain_function = export_tmp->index;
  5378. LOG_VERBOSE("Found retain function, name: %s, "
  5379. "index: %u",
  5380. export_tmp->name,
  5381. export_tmp->index);
  5382. break;
  5383. }
  5384. }
  5385. }
  5386. if (j == module->export_count) {
  5387. module->malloc_function = (uint32)-1;
  5388. LOG_VERBOSE("Can't find retain function,"
  5389. "reset malloc function index to -1");
  5390. }
  5391. }
  5392. }
  5393. else if (((!strcmp(export->name, "free"))
  5394. || (!strcmp(export->name, "__release"))
  5395. || (!strcmp(export->name, "__unpin")))
  5396. && export->index >= module->import_function_count) {
  5397. func_index = export->index - module->import_function_count;
  5398. func_type = module->functions[func_index]->func_type;
  5399. if (func_type->param_count == 1 && func_type->result_count == 0
  5400. && func_type->types[0] == malloc_free_io_type) {
  5401. bh_assert(module->free_function == (uint32)-1);
  5402. module->free_function = export->index;
  5403. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5404. export->name, export->index);
  5405. }
  5406. }
  5407. }
  5408. }
  5409. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5410. handle_table = wasm_interp_get_handle_table();
  5411. #endif
  5412. for (i = 0; i < module->function_count; i++) {
  5413. WASMFunction *func = module->functions[i];
  5414. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5415. error_buf_size)) {
  5416. return false;
  5417. }
  5418. if (i == module->function_count - 1
  5419. && func->code + func->code_size != buf_code_end) {
  5420. set_error_buf(error_buf, error_buf_size,
  5421. "code section size mismatch");
  5422. return false;
  5423. }
  5424. }
  5425. if (!module->possible_memory_grow) {
  5426. WASMMemoryImport *memory_import;
  5427. WASMMemory *memory;
  5428. if (aux_data_end_global && aux_heap_base_global
  5429. && aux_stack_top_global) {
  5430. uint64 init_memory_size;
  5431. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5432. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5433. * valid range of uint32 */
  5434. if (shrunk_memory_size <= UINT32_MAX) {
  5435. if (module->import_memory_count) {
  5436. memory_import = &module->import_memories[0].u.memory;
  5437. init_memory_size =
  5438. (uint64)memory_import->mem_type.num_bytes_per_page
  5439. * memory_import->mem_type.init_page_count;
  5440. if (shrunk_memory_size <= init_memory_size) {
  5441. /* Reset memory info to decrease memory usage */
  5442. memory_import->mem_type.num_bytes_per_page =
  5443. (uint32)shrunk_memory_size;
  5444. memory_import->mem_type.init_page_count = 1;
  5445. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5446. shrunk_memory_size);
  5447. }
  5448. }
  5449. if (module->memory_count) {
  5450. memory = &module->memories[0];
  5451. init_memory_size = (uint64)memory->num_bytes_per_page
  5452. * memory->init_page_count;
  5453. if (shrunk_memory_size <= init_memory_size) {
  5454. /* Reset memory info to decrease memory usage */
  5455. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5456. memory->init_page_count = 1;
  5457. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5458. shrunk_memory_size);
  5459. }
  5460. }
  5461. }
  5462. }
  5463. #if WASM_ENABLE_MULTI_MODULE == 0
  5464. if (module->import_memory_count) {
  5465. memory_import = &module->import_memories[0].u.memory;
  5466. /* Only resize the memory to one big page if num_bytes_per_page is
  5467. * in valid range of uint32 */
  5468. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5469. memory_import->mem_type.num_bytes_per_page *=
  5470. memory_import->mem_type.init_page_count;
  5471. if (memory_import->mem_type.init_page_count > 0)
  5472. memory_import->mem_type.init_page_count =
  5473. memory_import->mem_type.max_page_count = 1;
  5474. else
  5475. memory_import->mem_type.init_page_count =
  5476. memory_import->mem_type.max_page_count = 0;
  5477. }
  5478. }
  5479. if (module->memory_count) {
  5480. memory = &module->memories[0];
  5481. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5482. * valid range of uint32 */
  5483. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5484. memory->num_bytes_per_page *= memory->init_page_count;
  5485. if (memory->init_page_count > 0)
  5486. memory->init_page_count = memory->max_page_count = 1;
  5487. else
  5488. memory->init_page_count = memory->max_page_count = 0;
  5489. }
  5490. }
  5491. #endif
  5492. }
  5493. calculate_global_data_offset(module);
  5494. #if WASM_ENABLE_FAST_JIT != 0
  5495. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5496. return false;
  5497. }
  5498. #endif
  5499. #if WASM_ENABLE_JIT != 0
  5500. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5501. return false;
  5502. }
  5503. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5504. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5505. return false;
  5506. }
  5507. #else
  5508. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5509. thread fast jit execution, since applying llvm optimizations in
  5510. aot_compile_wasm may cost a lot of time.
  5511. Create thread with enough native stack to apply llvm optimizations */
  5512. if (os_thread_create(&module->llvm_jit_init_thread,
  5513. init_llvm_jit_functions_stage2_callback,
  5514. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5515. != 0) {
  5516. set_error_buf(error_buf, error_buf_size,
  5517. "create orcjit compile thread failed");
  5518. return false;
  5519. }
  5520. #endif
  5521. #endif
  5522. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5523. /* Create threads to compile the jit functions */
  5524. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5525. return false;
  5526. }
  5527. #endif
  5528. #if WASM_ENABLE_MEMORY_TRACING != 0
  5529. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5530. #endif
  5531. return true;
  5532. }
  5533. static WASMModule *
  5534. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5535. {
  5536. WASMModule *module =
  5537. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5538. bh_list_status ret;
  5539. if (!module) {
  5540. return NULL;
  5541. }
  5542. module->module_type = Wasm_Module_Bytecode;
  5543. /* Set start_function to -1, means no start function */
  5544. module->start_function = (uint32)-1;
  5545. module->name = name;
  5546. module->is_binary_freeable = false;
  5547. #if WASM_ENABLE_FAST_INTERP == 0
  5548. module->br_table_cache_list = &module->br_table_cache_list_head;
  5549. ret = bh_list_init(module->br_table_cache_list);
  5550. bh_assert(ret == BH_LIST_SUCCESS);
  5551. #endif
  5552. #if WASM_ENABLE_MULTI_MODULE != 0
  5553. module->import_module_list = &module->import_module_list_head;
  5554. ret = bh_list_init(module->import_module_list);
  5555. bh_assert(ret == BH_LIST_SUCCESS);
  5556. #endif
  5557. #if WASM_ENABLE_DEBUG_INTERP != 0
  5558. ret = bh_list_init(&module->fast_opcode_list);
  5559. bh_assert(ret == BH_LIST_SUCCESS);
  5560. #endif
  5561. #if WASM_ENABLE_GC != 0
  5562. if (!(module->ref_type_set =
  5563. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5564. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5565. goto fail1;
  5566. }
  5567. if (os_mutex_init(&module->rtt_type_lock)) {
  5568. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5569. goto fail2;
  5570. }
  5571. #endif
  5572. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5573. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5574. && WASM_ENABLE_LAZY_JIT != 0)
  5575. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5576. set_error_buf(error_buf, error_buf_size,
  5577. "init instance list lock failed");
  5578. goto fail3;
  5579. }
  5580. #endif
  5581. (void)ret;
  5582. return module;
  5583. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5584. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5585. && WASM_ENABLE_LAZY_JIT != 0)
  5586. fail3:
  5587. #endif
  5588. #if WASM_ENABLE_GC != 0
  5589. os_mutex_destroy(&module->rtt_type_lock);
  5590. fail2:
  5591. bh_hash_map_destroy(module->ref_type_set);
  5592. fail1:
  5593. #endif
  5594. wasm_runtime_free(module);
  5595. return NULL;
  5596. }
  5597. #if WASM_ENABLE_DEBUG_INTERP != 0
  5598. static bool
  5599. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5600. uint32 error_buf_size)
  5601. {
  5602. WASMFastOPCodeNode *fast_op =
  5603. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5604. if (fast_op) {
  5605. fast_op->offset = pos - module->load_addr;
  5606. fast_op->orig_op = orig_op;
  5607. bh_list_insert(&module->fast_opcode_list, fast_op);
  5608. }
  5609. return fast_op ? true : false;
  5610. }
  5611. #endif
  5612. WASMModule *
  5613. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5614. uint32 error_buf_size)
  5615. {
  5616. WASMModule *module = create_module("", error_buf, error_buf_size);
  5617. if (!module)
  5618. return NULL;
  5619. if (!load_from_sections(module, section_list, false, true, error_buf,
  5620. error_buf_size)) {
  5621. wasm_loader_unload(module);
  5622. return NULL;
  5623. }
  5624. LOG_VERBOSE("Load module from sections success.\n");
  5625. return module;
  5626. }
  5627. static void
  5628. destroy_sections(WASMSection *section_list)
  5629. {
  5630. WASMSection *section = section_list, *next;
  5631. while (section) {
  5632. next = section->next;
  5633. wasm_runtime_free(section);
  5634. section = next;
  5635. }
  5636. }
  5637. /* clang-format off */
  5638. static uint8 section_ids[] = {
  5639. SECTION_TYPE_USER,
  5640. SECTION_TYPE_TYPE,
  5641. SECTION_TYPE_IMPORT,
  5642. SECTION_TYPE_FUNC,
  5643. SECTION_TYPE_TABLE,
  5644. SECTION_TYPE_MEMORY,
  5645. #if WASM_ENABLE_TAGS != 0
  5646. SECTION_TYPE_TAG,
  5647. #endif
  5648. #if WASM_ENABLE_STRINGREF != 0
  5649. /* must immediately precede the global section,
  5650. or where the global section would be */
  5651. SECTION_TYPE_STRINGREF,
  5652. #endif
  5653. SECTION_TYPE_GLOBAL,
  5654. SECTION_TYPE_EXPORT,
  5655. SECTION_TYPE_START,
  5656. SECTION_TYPE_ELEM,
  5657. #if WASM_ENABLE_BULK_MEMORY != 0
  5658. SECTION_TYPE_DATACOUNT,
  5659. #endif
  5660. SECTION_TYPE_CODE,
  5661. SECTION_TYPE_DATA
  5662. };
  5663. /* clang-format on */
  5664. static uint8
  5665. get_section_index(uint8 section_type)
  5666. {
  5667. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5668. for (uint8 i = 0; i < max_id; i++) {
  5669. if (section_type == section_ids[i])
  5670. return i;
  5671. }
  5672. return (uint8)-1;
  5673. }
  5674. static bool
  5675. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5676. char *error_buf, uint32 error_buf_size)
  5677. {
  5678. WASMSection *section_list_end = NULL, *section;
  5679. const uint8 *p = buf, *p_end = buf + size;
  5680. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5681. uint32 section_size;
  5682. bh_assert(!*p_section_list);
  5683. p += 8;
  5684. while (p < p_end) {
  5685. CHECK_BUF(p, p_end, 1);
  5686. section_type = read_uint8(p);
  5687. section_index = get_section_index(section_type);
  5688. if (section_index != (uint8)-1) {
  5689. if (section_type != SECTION_TYPE_USER) {
  5690. /* Custom sections may be inserted at any place,
  5691. while other sections must occur at most once
  5692. and in prescribed order. */
  5693. if (last_section_index != (uint8)-1
  5694. && (section_index <= last_section_index)) {
  5695. set_error_buf(error_buf, error_buf_size,
  5696. "unexpected content after last section or "
  5697. "junk after last section");
  5698. return false;
  5699. }
  5700. last_section_index = section_index;
  5701. }
  5702. read_leb_uint32(p, p_end, section_size);
  5703. CHECK_BUF1(p, p_end, section_size);
  5704. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5705. error_buf_size))) {
  5706. return false;
  5707. }
  5708. section->section_type = section_type;
  5709. section->section_body = (uint8 *)p;
  5710. section->section_body_size = section_size;
  5711. if (!section_list_end)
  5712. *p_section_list = section_list_end = section;
  5713. else {
  5714. section_list_end->next = section;
  5715. section_list_end = section;
  5716. }
  5717. p += section_size;
  5718. }
  5719. else {
  5720. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5721. return false;
  5722. }
  5723. }
  5724. return true;
  5725. fail:
  5726. return false;
  5727. }
  5728. static void
  5729. exchange32(uint8 *p_data)
  5730. {
  5731. uint8 value = *p_data;
  5732. *p_data = *(p_data + 3);
  5733. *(p_data + 3) = value;
  5734. value = *(p_data + 1);
  5735. *(p_data + 1) = *(p_data + 2);
  5736. *(p_data + 2) = value;
  5737. }
  5738. static union {
  5739. int a;
  5740. char b;
  5741. } __ue = { .a = 1 };
  5742. #define is_little_endian() (__ue.b == 1)
  5743. static bool
  5744. load(const uint8 *buf, uint32 size, WASMModule *module,
  5745. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5746. {
  5747. const uint8 *buf_end = buf + size;
  5748. const uint8 *p = buf, *p_end = buf_end;
  5749. uint32 magic_number, version;
  5750. WASMSection *section_list = NULL;
  5751. CHECK_BUF1(p, p_end, sizeof(uint32));
  5752. magic_number = read_uint32(p);
  5753. if (!is_little_endian())
  5754. exchange32((uint8 *)&magic_number);
  5755. if (magic_number != WASM_MAGIC_NUMBER) {
  5756. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5757. return false;
  5758. }
  5759. CHECK_BUF1(p, p_end, sizeof(uint32));
  5760. version = read_uint32(p);
  5761. if (!is_little_endian())
  5762. exchange32((uint8 *)&version);
  5763. if (version != WASM_CURRENT_VERSION) {
  5764. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5765. return false;
  5766. }
  5767. module->package_version = version;
  5768. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5769. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5770. error_buf, error_buf_size)) {
  5771. destroy_sections(section_list);
  5772. return false;
  5773. }
  5774. destroy_sections(section_list);
  5775. return true;
  5776. fail:
  5777. return false;
  5778. }
  5779. #if WASM_ENABLE_LIBC_WASI != 0
  5780. /**
  5781. * refer to
  5782. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5783. */
  5784. static bool
  5785. check_wasi_abi_compatibility(const WASMModule *module,
  5786. #if WASM_ENABLE_MULTI_MODULE != 0
  5787. bool main_module,
  5788. #endif
  5789. char *error_buf, uint32 error_buf_size)
  5790. {
  5791. /**
  5792. * be careful with:
  5793. * wasi compatible modules(command/reactor) which don't import any wasi
  5794. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5795. * reactor can depend on nothing. At the same time, each has its own entry
  5796. * point.
  5797. *
  5798. * observations:
  5799. * - clang always injects `_start` into a command
  5800. * - clang always injects `_initialize` into a reactor
  5801. * - `iwasm -f` allows to run a function in the reactor
  5802. *
  5803. * strong assumptions:
  5804. * - no one will define either `_start` or `_initialize` on purpose
  5805. * - `_start` should always be `void _start(void)`
  5806. * - `_initialize` should always be `void _initialize(void)`
  5807. *
  5808. */
  5809. /* clang-format off */
  5810. /**
  5811. *
  5812. * | | import_wasi_api True | | import_wasi_api False | |
  5813. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5814. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5815. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5816. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5817. */
  5818. /* clang-format on */
  5819. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5820. uint32 import_function_count = module->import_function_count;
  5821. WASMFuncType *func_type;
  5822. /* (func (export "_start") (...) */
  5823. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5824. error_buf, error_buf_size);
  5825. if (start) {
  5826. if (start->index < import_function_count) {
  5827. set_error_buf(
  5828. error_buf, error_buf_size,
  5829. "the builtin _start function can not be an import function");
  5830. return false;
  5831. }
  5832. func_type =
  5833. module->functions[start->index - import_function_count]->func_type;
  5834. if (func_type->param_count || func_type->result_count) {
  5835. set_error_buf(error_buf, error_buf_size,
  5836. "the signature of builtin _start function is wrong");
  5837. return false;
  5838. }
  5839. }
  5840. else {
  5841. /* (func (export "_initialize") (...) */
  5842. initialize =
  5843. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5844. error_buf, error_buf_size);
  5845. if (initialize) {
  5846. if (initialize->index < import_function_count) {
  5847. set_error_buf(error_buf, error_buf_size,
  5848. "the builtin _initialize function can not be an "
  5849. "import function");
  5850. return false;
  5851. }
  5852. func_type =
  5853. module->functions[initialize->index - import_function_count]
  5854. ->func_type;
  5855. if (func_type->param_count || func_type->result_count) {
  5856. set_error_buf(
  5857. error_buf, error_buf_size,
  5858. "the signature of builtin _initialize function is wrong");
  5859. return false;
  5860. }
  5861. }
  5862. }
  5863. /* filter out non-wasi compatible modules */
  5864. if (!module->import_wasi_api && !start && !initialize) {
  5865. return true;
  5866. }
  5867. /* should have one at least */
  5868. if (module->import_wasi_api && !start && !initialize) {
  5869. LOG_WARNING("warning: a module with WASI apis should be either "
  5870. "a command or a reactor");
  5871. }
  5872. /*
  5873. * there is at least one of `_start` and `_initialize` in below cases.
  5874. * according to the assumption, they should be all wasi compatible
  5875. */
  5876. #if WASM_ENABLE_MULTI_MODULE != 0
  5877. /* filter out commands (with `_start`) cases */
  5878. if (start && !main_module) {
  5879. set_error_buf(
  5880. error_buf, error_buf_size,
  5881. "a command (with _start function) can not be a sub-module");
  5882. return false;
  5883. }
  5884. #endif
  5885. /*
  5886. * it is ok a reactor acts as a main module,
  5887. * so skip the check about (with `_initialize`)
  5888. */
  5889. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5890. error_buf, error_buf_size);
  5891. if (!memory
  5892. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5893. /*
  5894. * with wasi-threads, it's still an open question if a memory
  5895. * should be exported.
  5896. *
  5897. * https://github.com/WebAssembly/wasi-threads/issues/22
  5898. * https://github.com/WebAssembly/WASI/issues/502
  5899. *
  5900. * Note: this code assumes the number of memories is at most 1.
  5901. */
  5902. && module->import_memory_count == 0
  5903. #endif
  5904. ) {
  5905. set_error_buf(error_buf, error_buf_size,
  5906. "a module with WASI apis must export memory by default");
  5907. return false;
  5908. }
  5909. return true;
  5910. }
  5911. #endif
  5912. WASMModule *
  5913. wasm_loader_load(uint8 *buf, uint32 size,
  5914. #if WASM_ENABLE_MULTI_MODULE != 0
  5915. bool main_module,
  5916. #endif
  5917. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5918. {
  5919. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5920. if (!module) {
  5921. return NULL;
  5922. }
  5923. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5924. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5925. module->load_addr = (uint8 *)buf;
  5926. module->load_size = size;
  5927. #endif
  5928. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  5929. error_buf_size)) {
  5930. goto fail;
  5931. }
  5932. #if WASM_ENABLE_LIBC_WASI != 0
  5933. /* Check the WASI application ABI */
  5934. if (!check_wasi_abi_compatibility(module,
  5935. #if WASM_ENABLE_MULTI_MODULE != 0
  5936. main_module,
  5937. #endif
  5938. error_buf, error_buf_size)) {
  5939. goto fail;
  5940. }
  5941. #endif
  5942. LOG_VERBOSE("Load module success.\n");
  5943. return module;
  5944. fail:
  5945. wasm_loader_unload(module);
  5946. return NULL;
  5947. }
  5948. void
  5949. wasm_loader_unload(WASMModule *module)
  5950. {
  5951. uint32 i;
  5952. if (!module)
  5953. return;
  5954. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5955. && WASM_ENABLE_LAZY_JIT != 0
  5956. module->orcjit_stop_compiling = true;
  5957. if (module->llvm_jit_init_thread)
  5958. os_thread_join(module->llvm_jit_init_thread, NULL);
  5959. #endif
  5960. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5961. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5962. module internal data after they were freed */
  5963. orcjit_stop_compile_threads(module);
  5964. #endif
  5965. #if WASM_ENABLE_JIT != 0
  5966. if (module->func_ptrs)
  5967. wasm_runtime_free(module->func_ptrs);
  5968. if (module->comp_ctx)
  5969. aot_destroy_comp_context(module->comp_ctx);
  5970. if (module->comp_data)
  5971. aot_destroy_comp_data(module->comp_data);
  5972. #endif
  5973. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5974. && WASM_ENABLE_LAZY_JIT != 0
  5975. if (module->tierup_wait_lock_inited) {
  5976. os_mutex_destroy(&module->tierup_wait_lock);
  5977. os_cond_destroy(&module->tierup_wait_cond);
  5978. }
  5979. #endif
  5980. if (module->imports)
  5981. wasm_runtime_free(module->imports);
  5982. if (module->functions) {
  5983. for (i = 0; i < module->function_count; i++) {
  5984. if (module->functions[i]) {
  5985. if (module->functions[i]->local_offsets)
  5986. wasm_runtime_free(module->functions[i]->local_offsets);
  5987. #if WASM_ENABLE_FAST_INTERP != 0
  5988. if (module->functions[i]->code_compiled)
  5989. wasm_runtime_free(module->functions[i]->code_compiled);
  5990. if (module->functions[i]->consts)
  5991. wasm_runtime_free(module->functions[i]->consts);
  5992. #endif
  5993. #if WASM_ENABLE_FAST_JIT != 0
  5994. if (module->functions[i]->fast_jit_jitted_code) {
  5995. jit_code_cache_free(
  5996. module->functions[i]->fast_jit_jitted_code);
  5997. }
  5998. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5999. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6000. jit_code_cache_free(
  6001. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6002. }
  6003. #endif
  6004. #endif
  6005. #if WASM_ENABLE_GC != 0
  6006. if (module->functions[i]->local_ref_type_maps) {
  6007. wasm_runtime_free(
  6008. module->functions[i]->local_ref_type_maps);
  6009. }
  6010. #endif
  6011. wasm_runtime_free(module->functions[i]);
  6012. }
  6013. }
  6014. wasm_runtime_free(module->functions);
  6015. }
  6016. if (module->tables) {
  6017. #if WASM_ENABLE_GC != 0
  6018. for (i = 0; i < module->table_count; i++) {
  6019. destroy_init_expr(module, &module->tables[i].init_expr);
  6020. }
  6021. #endif
  6022. wasm_runtime_free(module->tables);
  6023. }
  6024. if (module->memories)
  6025. wasm_runtime_free(module->memories);
  6026. if (module->globals) {
  6027. #if WASM_ENABLE_GC != 0
  6028. for (i = 0; i < module->global_count; i++) {
  6029. destroy_init_expr(module, &module->globals[i].init_expr);
  6030. }
  6031. #endif
  6032. wasm_runtime_free(module->globals);
  6033. }
  6034. #if WASM_ENABLE_TAGS != 0
  6035. if (module->tags) {
  6036. for (i = 0; i < module->tag_count; i++) {
  6037. if (module->tags[i])
  6038. wasm_runtime_free(module->tags[i]);
  6039. }
  6040. wasm_runtime_free(module->tags);
  6041. }
  6042. #endif
  6043. if (module->exports)
  6044. wasm_runtime_free(module->exports);
  6045. if (module->table_segments) {
  6046. for (i = 0; i < module->table_seg_count; i++) {
  6047. if (module->table_segments[i].init_values) {
  6048. #if WASM_ENABLE_GC != 0
  6049. uint32 j;
  6050. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6051. destroy_init_expr(
  6052. module, &module->table_segments[i].init_values[j]);
  6053. }
  6054. #endif
  6055. wasm_runtime_free(module->table_segments[i].init_values);
  6056. }
  6057. }
  6058. wasm_runtime_free(module->table_segments);
  6059. }
  6060. if (module->data_segments) {
  6061. for (i = 0; i < module->data_seg_count; i++) {
  6062. if (module->data_segments[i]) {
  6063. if (module->data_segments[i]->is_data_cloned)
  6064. wasm_runtime_free(module->data_segments[i]->data);
  6065. wasm_runtime_free(module->data_segments[i]);
  6066. }
  6067. }
  6068. wasm_runtime_free(module->data_segments);
  6069. }
  6070. if (module->types) {
  6071. for (i = 0; i < module->type_count; i++) {
  6072. if (module->types[i])
  6073. destroy_wasm_type(module->types[i]);
  6074. }
  6075. wasm_runtime_free(module->types);
  6076. }
  6077. if (module->const_str_list) {
  6078. StringNode *node = module->const_str_list, *node_next;
  6079. while (node) {
  6080. node_next = node->next;
  6081. wasm_runtime_free(node);
  6082. node = node_next;
  6083. }
  6084. }
  6085. #if WASM_ENABLE_STRINGREF != 0
  6086. if (module->string_literal_ptrs) {
  6087. wasm_runtime_free((void *)module->string_literal_ptrs);
  6088. }
  6089. if (module->string_literal_lengths) {
  6090. wasm_runtime_free(module->string_literal_lengths);
  6091. }
  6092. #endif
  6093. #if WASM_ENABLE_FAST_INTERP == 0
  6094. if (module->br_table_cache_list) {
  6095. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6096. BrTableCache *node_next;
  6097. while (node) {
  6098. node_next = bh_list_elem_next(node);
  6099. wasm_runtime_free(node);
  6100. node = node_next;
  6101. }
  6102. }
  6103. #endif
  6104. #if WASM_ENABLE_MULTI_MODULE != 0
  6105. /* just release the sub module list */
  6106. if (module->import_module_list) {
  6107. WASMRegisteredModule *node =
  6108. bh_list_first_elem(module->import_module_list);
  6109. while (node) {
  6110. WASMRegisteredModule *next = bh_list_elem_next(node);
  6111. bh_list_remove(module->import_module_list, node);
  6112. /*
  6113. * unload(sub_module) will be triggered during runtime_destroy().
  6114. * every module in the global module list will be unloaded one by
  6115. * one. so don't worry.
  6116. */
  6117. wasm_runtime_free(node);
  6118. /*
  6119. * the module file reading buffer will be released
  6120. * in runtime_destroy()
  6121. */
  6122. node = next;
  6123. }
  6124. }
  6125. #endif
  6126. #if WASM_ENABLE_DEBUG_INTERP != 0
  6127. WASMFastOPCodeNode *fast_opcode =
  6128. bh_list_first_elem(&module->fast_opcode_list);
  6129. while (fast_opcode) {
  6130. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6131. wasm_runtime_free(fast_opcode);
  6132. fast_opcode = next;
  6133. }
  6134. #endif
  6135. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6136. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6137. && WASM_ENABLE_LAZY_JIT != 0)
  6138. os_mutex_destroy(&module->instance_list_lock);
  6139. #endif
  6140. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6141. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6142. #endif
  6143. #if WASM_ENABLE_FAST_JIT != 0
  6144. if (module->fast_jit_func_ptrs) {
  6145. wasm_runtime_free(module->fast_jit_func_ptrs);
  6146. }
  6147. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6148. if (module->fast_jit_thread_locks_inited[i]) {
  6149. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6150. }
  6151. }
  6152. #endif
  6153. #if WASM_ENABLE_GC != 0
  6154. os_mutex_destroy(&module->rtt_type_lock);
  6155. bh_hash_map_destroy(module->ref_type_set);
  6156. if (module->rtt_types) {
  6157. for (i = 0; i < module->type_count; i++) {
  6158. if (module->rtt_types[i])
  6159. wasm_runtime_free(module->rtt_types[i]);
  6160. }
  6161. wasm_runtime_free(module->rtt_types);
  6162. }
  6163. #if WASM_ENABLE_STRINGREF != 0
  6164. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6165. if (module->stringref_rtts[i])
  6166. wasm_runtime_free(module->stringref_rtts[i]);
  6167. }
  6168. #endif
  6169. #endif
  6170. wasm_runtime_free(module);
  6171. }
  6172. bool
  6173. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6174. const uint8 *start_addr, const uint8 *code_end_addr,
  6175. uint8 label_type, uint8 **p_else_addr,
  6176. uint8 **p_end_addr)
  6177. {
  6178. const uint8 *p = start_addr, *p_end = code_end_addr;
  6179. uint8 *else_addr = NULL;
  6180. char error_buf[128];
  6181. uint32 block_nested_depth = 1, count, i, j, t;
  6182. uint32 error_buf_size = sizeof(error_buf);
  6183. uint8 opcode, u8;
  6184. BlockAddr block_stack[16] = { { 0 } }, *block;
  6185. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6186. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6187. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6188. if (block[j].start_addr == start_addr) {
  6189. /* Cache hit */
  6190. *p_else_addr = block[j].else_addr;
  6191. *p_end_addr = block[j].end_addr;
  6192. return true;
  6193. }
  6194. }
  6195. /* Cache unhit */
  6196. block_stack[0].start_addr = start_addr;
  6197. while (p < code_end_addr) {
  6198. opcode = *p++;
  6199. #if WASM_ENABLE_DEBUG_INTERP != 0
  6200. op_break_retry:
  6201. #endif
  6202. switch (opcode) {
  6203. case WASM_OP_UNREACHABLE:
  6204. case WASM_OP_NOP:
  6205. break;
  6206. #if WASM_ENABLE_EXCE_HANDLING != 0
  6207. case WASM_OP_TRY:
  6208. u8 = read_uint8(p);
  6209. if (block_nested_depth
  6210. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6211. block_stack[block_nested_depth].start_addr = p;
  6212. block_stack[block_nested_depth].else_addr = NULL;
  6213. }
  6214. block_nested_depth++;
  6215. break;
  6216. case EXT_OP_TRY:
  6217. skip_leb_uint32(p, p_end);
  6218. if (block_nested_depth
  6219. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6220. block_stack[block_nested_depth].start_addr = p;
  6221. block_stack[block_nested_depth].else_addr = NULL;
  6222. }
  6223. block_nested_depth++;
  6224. break;
  6225. case WASM_OP_CATCH:
  6226. if (block_nested_depth == 1) {
  6227. *p_end_addr = (uint8 *)(p - 1);
  6228. /* stop search and return the address of the catch block */
  6229. return true;
  6230. }
  6231. break;
  6232. case WASM_OP_CATCH_ALL:
  6233. if (block_nested_depth == 1) {
  6234. *p_end_addr = (uint8 *)(p - 1);
  6235. /* stop search and return the address of the catch_all block
  6236. */
  6237. return true;
  6238. }
  6239. break;
  6240. case WASM_OP_THROW:
  6241. /* skip tag_index */
  6242. skip_leb(p);
  6243. break;
  6244. case WASM_OP_RETHROW:
  6245. /* skip depth */
  6246. skip_leb(p);
  6247. break;
  6248. case WASM_OP_DELEGATE:
  6249. if (block_nested_depth == 1) {
  6250. *p_end_addr = (uint8 *)(p - 1);
  6251. return true;
  6252. }
  6253. else {
  6254. skip_leb(p);
  6255. /* the DELEGATE opcode ends the tryblock, */
  6256. block_nested_depth--;
  6257. if (block_nested_depth
  6258. < sizeof(block_stack) / sizeof(BlockAddr))
  6259. block_stack[block_nested_depth].end_addr =
  6260. (uint8 *)(p - 1);
  6261. }
  6262. break;
  6263. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6264. case WASM_OP_BLOCK:
  6265. case WASM_OP_LOOP:
  6266. case WASM_OP_IF:
  6267. {
  6268. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6269. u8 = read_uint8(p);
  6270. if (is_byte_a_type(u8)) {
  6271. #if WASM_ENABLE_GC != 0
  6272. if (wasm_is_type_multi_byte_type(u8)) {
  6273. /* the possible extra bytes of GC ref type have been
  6274. modified to OP_NOP, no need to resolve them again */
  6275. }
  6276. #endif
  6277. }
  6278. else {
  6279. p--;
  6280. /* block type */
  6281. skip_leb_int32(p, p_end);
  6282. }
  6283. if (block_nested_depth
  6284. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6285. block_stack[block_nested_depth].start_addr = p;
  6286. block_stack[block_nested_depth].else_addr = NULL;
  6287. }
  6288. block_nested_depth++;
  6289. break;
  6290. }
  6291. case EXT_OP_BLOCK:
  6292. case EXT_OP_LOOP:
  6293. case EXT_OP_IF:
  6294. /* block type */
  6295. skip_leb_int32(p, p_end);
  6296. if (block_nested_depth
  6297. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6298. block_stack[block_nested_depth].start_addr = p;
  6299. block_stack[block_nested_depth].else_addr = NULL;
  6300. }
  6301. block_nested_depth++;
  6302. break;
  6303. case WASM_OP_ELSE:
  6304. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6305. else_addr = (uint8 *)(p - 1);
  6306. if (block_nested_depth - 1
  6307. < sizeof(block_stack) / sizeof(BlockAddr))
  6308. block_stack[block_nested_depth - 1].else_addr =
  6309. (uint8 *)(p - 1);
  6310. break;
  6311. case WASM_OP_END:
  6312. if (block_nested_depth == 1) {
  6313. if (label_type == LABEL_TYPE_IF)
  6314. *p_else_addr = else_addr;
  6315. *p_end_addr = (uint8 *)(p - 1);
  6316. block_stack[0].end_addr = (uint8 *)(p - 1);
  6317. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6318. t++) {
  6319. start_addr = block_stack[t].start_addr;
  6320. if (start_addr) {
  6321. i = ((uintptr_t)start_addr)
  6322. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6323. block =
  6324. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6325. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6326. if (!block[j].start_addr)
  6327. break;
  6328. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6329. memmove(block + 1, block,
  6330. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6331. * sizeof(BlockAddr));
  6332. j = 0;
  6333. }
  6334. block[j].start_addr = block_stack[t].start_addr;
  6335. block[j].else_addr = block_stack[t].else_addr;
  6336. block[j].end_addr = block_stack[t].end_addr;
  6337. }
  6338. else
  6339. break;
  6340. }
  6341. return true;
  6342. }
  6343. else {
  6344. block_nested_depth--;
  6345. if (block_nested_depth
  6346. < sizeof(block_stack) / sizeof(BlockAddr))
  6347. block_stack[block_nested_depth].end_addr =
  6348. (uint8 *)(p - 1);
  6349. }
  6350. break;
  6351. case WASM_OP_BR:
  6352. case WASM_OP_BR_IF:
  6353. skip_leb_uint32(p, p_end); /* labelidx */
  6354. break;
  6355. case WASM_OP_BR_TABLE:
  6356. read_leb_uint32(p, p_end, count); /* label num */
  6357. #if WASM_ENABLE_FAST_INTERP != 0
  6358. for (i = 0; i <= count; i++) /* labelidxs */
  6359. skip_leb_uint32(p, p_end);
  6360. #else
  6361. p += count + 1;
  6362. while (*p == WASM_OP_NOP)
  6363. p++;
  6364. #endif
  6365. break;
  6366. #if WASM_ENABLE_FAST_INTERP == 0
  6367. case EXT_OP_BR_TABLE_CACHE:
  6368. read_leb_uint32(p, p_end, count); /* label num */
  6369. while (*p == WASM_OP_NOP)
  6370. p++;
  6371. break;
  6372. #endif
  6373. case WASM_OP_RETURN:
  6374. break;
  6375. case WASM_OP_CALL:
  6376. #if WASM_ENABLE_TAIL_CALL != 0
  6377. case WASM_OP_RETURN_CALL:
  6378. #endif
  6379. skip_leb_uint32(p, p_end); /* funcidx */
  6380. break;
  6381. case WASM_OP_CALL_INDIRECT:
  6382. #if WASM_ENABLE_TAIL_CALL != 0
  6383. case WASM_OP_RETURN_CALL_INDIRECT:
  6384. #endif
  6385. skip_leb_uint32(p, p_end); /* typeidx */
  6386. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6387. u8 = read_uint8(p); /* 0x00 */
  6388. #else
  6389. skip_leb_uint32(p, p_end); /* tableidx */
  6390. #endif
  6391. break;
  6392. #if WASM_ENABLE_GC != 0
  6393. case WASM_OP_CALL_REF:
  6394. case WASM_OP_RETURN_CALL_REF:
  6395. skip_leb_uint32(p, p_end); /* typeidx */
  6396. break;
  6397. #endif
  6398. case WASM_OP_DROP:
  6399. case WASM_OP_SELECT:
  6400. case WASM_OP_DROP_64:
  6401. case WASM_OP_SELECT_64:
  6402. break;
  6403. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6404. case WASM_OP_SELECT_T:
  6405. {
  6406. skip_leb_uint32(p, p_end); /* vec length */
  6407. u8 = read_uint8(p); /* typeidx */
  6408. /* the possible extra bytes of GC ref type have been
  6409. modified to OP_NOP, no need to resolve them again */
  6410. break;
  6411. }
  6412. case WASM_OP_TABLE_GET:
  6413. case WASM_OP_TABLE_SET:
  6414. skip_leb_uint32(p, p_end); /* table index */
  6415. break;
  6416. case WASM_OP_REF_NULL:
  6417. {
  6418. u8 = read_uint8(p); /* type */
  6419. if (is_byte_a_type(u8)) {
  6420. #if WASM_ENABLE_GC != 0
  6421. if (wasm_is_type_multi_byte_type(u8)) {
  6422. /* the possible extra bytes of GC ref type have been
  6423. modified to OP_NOP, no need to resolve them again */
  6424. }
  6425. #endif
  6426. }
  6427. else {
  6428. p--;
  6429. skip_leb_uint32(p, p_end);
  6430. }
  6431. break;
  6432. }
  6433. case WASM_OP_REF_IS_NULL:
  6434. break;
  6435. case WASM_OP_REF_FUNC:
  6436. skip_leb_uint32(p, p_end); /* func index */
  6437. break;
  6438. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6439. #if WASM_ENABLE_GC != 0
  6440. case WASM_OP_REF_AS_NON_NULL:
  6441. case WASM_OP_REF_EQ:
  6442. break;
  6443. case WASM_OP_BR_ON_NULL:
  6444. case WASM_OP_BR_ON_NON_NULL:
  6445. skip_leb_uint32(p, p_end); /* label index */
  6446. break;
  6447. #endif /* end of WASM_ENABLE_GC != 0 */
  6448. case WASM_OP_GET_LOCAL:
  6449. case WASM_OP_SET_LOCAL:
  6450. case WASM_OP_TEE_LOCAL:
  6451. case WASM_OP_GET_GLOBAL:
  6452. case WASM_OP_SET_GLOBAL:
  6453. case WASM_OP_GET_GLOBAL_64:
  6454. case WASM_OP_SET_GLOBAL_64:
  6455. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6456. skip_leb_uint32(p, p_end); /* local index */
  6457. break;
  6458. case EXT_OP_GET_LOCAL_FAST:
  6459. case EXT_OP_SET_LOCAL_FAST:
  6460. case EXT_OP_TEE_LOCAL_FAST:
  6461. CHECK_BUF(p, p_end, 1);
  6462. p++;
  6463. break;
  6464. case WASM_OP_I32_LOAD:
  6465. case WASM_OP_I64_LOAD:
  6466. case WASM_OP_F32_LOAD:
  6467. case WASM_OP_F64_LOAD:
  6468. case WASM_OP_I32_LOAD8_S:
  6469. case WASM_OP_I32_LOAD8_U:
  6470. case WASM_OP_I32_LOAD16_S:
  6471. case WASM_OP_I32_LOAD16_U:
  6472. case WASM_OP_I64_LOAD8_S:
  6473. case WASM_OP_I64_LOAD8_U:
  6474. case WASM_OP_I64_LOAD16_S:
  6475. case WASM_OP_I64_LOAD16_U:
  6476. case WASM_OP_I64_LOAD32_S:
  6477. case WASM_OP_I64_LOAD32_U:
  6478. case WASM_OP_I32_STORE:
  6479. case WASM_OP_I64_STORE:
  6480. case WASM_OP_F32_STORE:
  6481. case WASM_OP_F64_STORE:
  6482. case WASM_OP_I32_STORE8:
  6483. case WASM_OP_I32_STORE16:
  6484. case WASM_OP_I64_STORE8:
  6485. case WASM_OP_I64_STORE16:
  6486. case WASM_OP_I64_STORE32:
  6487. skip_leb_uint32(p, p_end); /* align */
  6488. skip_leb_mem_offset(p, p_end); /* offset */
  6489. break;
  6490. case WASM_OP_MEMORY_SIZE:
  6491. case WASM_OP_MEMORY_GROW:
  6492. skip_leb_uint32(p, p_end); /* 0x00 */
  6493. break;
  6494. case WASM_OP_I32_CONST:
  6495. skip_leb_int32(p, p_end);
  6496. break;
  6497. case WASM_OP_I64_CONST:
  6498. skip_leb_int64(p, p_end);
  6499. break;
  6500. case WASM_OP_F32_CONST:
  6501. p += sizeof(float32);
  6502. break;
  6503. case WASM_OP_F64_CONST:
  6504. p += sizeof(float64);
  6505. break;
  6506. case WASM_OP_I32_EQZ:
  6507. case WASM_OP_I32_EQ:
  6508. case WASM_OP_I32_NE:
  6509. case WASM_OP_I32_LT_S:
  6510. case WASM_OP_I32_LT_U:
  6511. case WASM_OP_I32_GT_S:
  6512. case WASM_OP_I32_GT_U:
  6513. case WASM_OP_I32_LE_S:
  6514. case WASM_OP_I32_LE_U:
  6515. case WASM_OP_I32_GE_S:
  6516. case WASM_OP_I32_GE_U:
  6517. case WASM_OP_I64_EQZ:
  6518. case WASM_OP_I64_EQ:
  6519. case WASM_OP_I64_NE:
  6520. case WASM_OP_I64_LT_S:
  6521. case WASM_OP_I64_LT_U:
  6522. case WASM_OP_I64_GT_S:
  6523. case WASM_OP_I64_GT_U:
  6524. case WASM_OP_I64_LE_S:
  6525. case WASM_OP_I64_LE_U:
  6526. case WASM_OP_I64_GE_S:
  6527. case WASM_OP_I64_GE_U:
  6528. case WASM_OP_F32_EQ:
  6529. case WASM_OP_F32_NE:
  6530. case WASM_OP_F32_LT:
  6531. case WASM_OP_F32_GT:
  6532. case WASM_OP_F32_LE:
  6533. case WASM_OP_F32_GE:
  6534. case WASM_OP_F64_EQ:
  6535. case WASM_OP_F64_NE:
  6536. case WASM_OP_F64_LT:
  6537. case WASM_OP_F64_GT:
  6538. case WASM_OP_F64_LE:
  6539. case WASM_OP_F64_GE:
  6540. case WASM_OP_I32_CLZ:
  6541. case WASM_OP_I32_CTZ:
  6542. case WASM_OP_I32_POPCNT:
  6543. case WASM_OP_I32_ADD:
  6544. case WASM_OP_I32_SUB:
  6545. case WASM_OP_I32_MUL:
  6546. case WASM_OP_I32_DIV_S:
  6547. case WASM_OP_I32_DIV_U:
  6548. case WASM_OP_I32_REM_S:
  6549. case WASM_OP_I32_REM_U:
  6550. case WASM_OP_I32_AND:
  6551. case WASM_OP_I32_OR:
  6552. case WASM_OP_I32_XOR:
  6553. case WASM_OP_I32_SHL:
  6554. case WASM_OP_I32_SHR_S:
  6555. case WASM_OP_I32_SHR_U:
  6556. case WASM_OP_I32_ROTL:
  6557. case WASM_OP_I32_ROTR:
  6558. case WASM_OP_I64_CLZ:
  6559. case WASM_OP_I64_CTZ:
  6560. case WASM_OP_I64_POPCNT:
  6561. case WASM_OP_I64_ADD:
  6562. case WASM_OP_I64_SUB:
  6563. case WASM_OP_I64_MUL:
  6564. case WASM_OP_I64_DIV_S:
  6565. case WASM_OP_I64_DIV_U:
  6566. case WASM_OP_I64_REM_S:
  6567. case WASM_OP_I64_REM_U:
  6568. case WASM_OP_I64_AND:
  6569. case WASM_OP_I64_OR:
  6570. case WASM_OP_I64_XOR:
  6571. case WASM_OP_I64_SHL:
  6572. case WASM_OP_I64_SHR_S:
  6573. case WASM_OP_I64_SHR_U:
  6574. case WASM_OP_I64_ROTL:
  6575. case WASM_OP_I64_ROTR:
  6576. case WASM_OP_F32_ABS:
  6577. case WASM_OP_F32_NEG:
  6578. case WASM_OP_F32_CEIL:
  6579. case WASM_OP_F32_FLOOR:
  6580. case WASM_OP_F32_TRUNC:
  6581. case WASM_OP_F32_NEAREST:
  6582. case WASM_OP_F32_SQRT:
  6583. case WASM_OP_F32_ADD:
  6584. case WASM_OP_F32_SUB:
  6585. case WASM_OP_F32_MUL:
  6586. case WASM_OP_F32_DIV:
  6587. case WASM_OP_F32_MIN:
  6588. case WASM_OP_F32_MAX:
  6589. case WASM_OP_F32_COPYSIGN:
  6590. case WASM_OP_F64_ABS:
  6591. case WASM_OP_F64_NEG:
  6592. case WASM_OP_F64_CEIL:
  6593. case WASM_OP_F64_FLOOR:
  6594. case WASM_OP_F64_TRUNC:
  6595. case WASM_OP_F64_NEAREST:
  6596. case WASM_OP_F64_SQRT:
  6597. case WASM_OP_F64_ADD:
  6598. case WASM_OP_F64_SUB:
  6599. case WASM_OP_F64_MUL:
  6600. case WASM_OP_F64_DIV:
  6601. case WASM_OP_F64_MIN:
  6602. case WASM_OP_F64_MAX:
  6603. case WASM_OP_F64_COPYSIGN:
  6604. case WASM_OP_I32_WRAP_I64:
  6605. case WASM_OP_I32_TRUNC_S_F32:
  6606. case WASM_OP_I32_TRUNC_U_F32:
  6607. case WASM_OP_I32_TRUNC_S_F64:
  6608. case WASM_OP_I32_TRUNC_U_F64:
  6609. case WASM_OP_I64_EXTEND_S_I32:
  6610. case WASM_OP_I64_EXTEND_U_I32:
  6611. case WASM_OP_I64_TRUNC_S_F32:
  6612. case WASM_OP_I64_TRUNC_U_F32:
  6613. case WASM_OP_I64_TRUNC_S_F64:
  6614. case WASM_OP_I64_TRUNC_U_F64:
  6615. case WASM_OP_F32_CONVERT_S_I32:
  6616. case WASM_OP_F32_CONVERT_U_I32:
  6617. case WASM_OP_F32_CONVERT_S_I64:
  6618. case WASM_OP_F32_CONVERT_U_I64:
  6619. case WASM_OP_F32_DEMOTE_F64:
  6620. case WASM_OP_F64_CONVERT_S_I32:
  6621. case WASM_OP_F64_CONVERT_U_I32:
  6622. case WASM_OP_F64_CONVERT_S_I64:
  6623. case WASM_OP_F64_CONVERT_U_I64:
  6624. case WASM_OP_F64_PROMOTE_F32:
  6625. case WASM_OP_I32_REINTERPRET_F32:
  6626. case WASM_OP_I64_REINTERPRET_F64:
  6627. case WASM_OP_F32_REINTERPRET_I32:
  6628. case WASM_OP_F64_REINTERPRET_I64:
  6629. case WASM_OP_I32_EXTEND8_S:
  6630. case WASM_OP_I32_EXTEND16_S:
  6631. case WASM_OP_I64_EXTEND8_S:
  6632. case WASM_OP_I64_EXTEND16_S:
  6633. case WASM_OP_I64_EXTEND32_S:
  6634. break;
  6635. #if WASM_ENABLE_GC != 0
  6636. case WASM_OP_GC_PREFIX:
  6637. {
  6638. uint32 opcode1;
  6639. read_leb_uint32(p, p_end, opcode1);
  6640. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6641. is no larger than UINT8_MAX */
  6642. opcode = (uint8)opcode1;
  6643. switch (opcode) {
  6644. case WASM_OP_STRUCT_NEW:
  6645. case WASM_OP_STRUCT_NEW_DEFAULT:
  6646. skip_leb_uint32(p, p_end); /* typeidx */
  6647. break;
  6648. case WASM_OP_STRUCT_GET:
  6649. case WASM_OP_STRUCT_GET_S:
  6650. case WASM_OP_STRUCT_GET_U:
  6651. case WASM_OP_STRUCT_SET:
  6652. skip_leb_uint32(p, p_end); /* typeidx */
  6653. skip_leb_uint32(p, p_end); /* fieldidx */
  6654. break;
  6655. case WASM_OP_ARRAY_NEW:
  6656. case WASM_OP_ARRAY_NEW_DEFAULT:
  6657. case WASM_OP_ARRAY_GET:
  6658. case WASM_OP_ARRAY_GET_S:
  6659. case WASM_OP_ARRAY_GET_U:
  6660. case WASM_OP_ARRAY_SET:
  6661. case WASM_OP_ARRAY_FILL:
  6662. skip_leb_uint32(p, p_end); /* typeidx */
  6663. break;
  6664. case WASM_OP_ARRAY_COPY:
  6665. skip_leb_uint32(p, p_end); /* typeidx1 */
  6666. skip_leb_uint32(p, p_end); /* typeidx2 */
  6667. break;
  6668. case WASM_OP_ARRAY_LEN:
  6669. break;
  6670. case WASM_OP_ARRAY_NEW_FIXED:
  6671. case WASM_OP_ARRAY_NEW_DATA:
  6672. case WASM_OP_ARRAY_NEW_ELEM:
  6673. skip_leb_uint32(p, p_end); /* typeidx */
  6674. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6675. break;
  6676. case WASM_OP_REF_I31:
  6677. case WASM_OP_I31_GET_S:
  6678. case WASM_OP_I31_GET_U:
  6679. break;
  6680. case WASM_OP_REF_TEST:
  6681. case WASM_OP_REF_CAST:
  6682. case WASM_OP_REF_TEST_NULLABLE:
  6683. case WASM_OP_REF_CAST_NULLABLE:
  6684. skip_leb_int32(p, p_end); /* heaptype */
  6685. break;
  6686. case WASM_OP_BR_ON_CAST:
  6687. case WASM_OP_BR_ON_CAST_FAIL:
  6688. p += sizeof(uint8); /* castflag */
  6689. skip_leb_uint32(p, p_end); /* labelidx */
  6690. skip_leb_int32(p, p_end); /* heaptype */
  6691. skip_leb_int32(p, p_end); /* heaptype2 */
  6692. break;
  6693. case WASM_OP_ANY_CONVERT_EXTERN:
  6694. case WASM_OP_EXTERN_CONVERT_ANY:
  6695. break;
  6696. #if WASM_ENABLE_STRINGREF != 0
  6697. case WASM_OP_STRING_NEW_UTF8:
  6698. case WASM_OP_STRING_NEW_WTF16:
  6699. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6700. case WASM_OP_STRING_NEW_WTF8:
  6701. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6702. break;
  6703. case WASM_OP_STRING_CONST:
  6704. skip_leb_int32(p, p_end); /* contents */
  6705. break;
  6706. case WASM_OP_STRING_MEASURE_UTF8:
  6707. case WASM_OP_STRING_MEASURE_WTF8:
  6708. case WASM_OP_STRING_MEASURE_WTF16:
  6709. break;
  6710. case WASM_OP_STRING_ENCODE_UTF8:
  6711. case WASM_OP_STRING_ENCODE_WTF16:
  6712. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6713. case WASM_OP_STRING_ENCODE_WTF8:
  6714. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6715. break;
  6716. case WASM_OP_STRING_CONCAT:
  6717. case WASM_OP_STRING_EQ:
  6718. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6719. case WASM_OP_STRING_AS_WTF8:
  6720. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6721. break;
  6722. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6723. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6724. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6725. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6726. break;
  6727. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6728. case WASM_OP_STRING_AS_WTF16:
  6729. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6730. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6731. break;
  6732. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6733. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6734. break;
  6735. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6736. case WASM_OP_STRING_AS_ITER:
  6737. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6738. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6739. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6740. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6741. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6742. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6743. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6744. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6745. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6746. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6747. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6748. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6749. break;
  6750. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6751. default:
  6752. return false;
  6753. }
  6754. break;
  6755. }
  6756. #endif /* end of WASM_ENABLE_GC != 0 */
  6757. case WASM_OP_MISC_PREFIX:
  6758. {
  6759. uint32 opcode1;
  6760. read_leb_uint32(p, p_end, opcode1);
  6761. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6762. is no larger than UINT8_MAX */
  6763. opcode = (uint8)opcode1;
  6764. switch (opcode) {
  6765. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6766. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6767. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6768. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6769. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6770. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6771. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6772. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6773. break;
  6774. #if WASM_ENABLE_BULK_MEMORY != 0
  6775. case WASM_OP_MEMORY_INIT:
  6776. skip_leb_uint32(p, p_end);
  6777. /* skip memory idx */
  6778. p++;
  6779. break;
  6780. case WASM_OP_DATA_DROP:
  6781. skip_leb_uint32(p, p_end);
  6782. break;
  6783. case WASM_OP_MEMORY_COPY:
  6784. /* skip two memory idx */
  6785. p += 2;
  6786. break;
  6787. case WASM_OP_MEMORY_FILL:
  6788. /* skip memory idx */
  6789. p++;
  6790. break;
  6791. #endif /* WASM_ENABLE_BULK_MEMORY */
  6792. #if WASM_ENABLE_REF_TYPES != 0
  6793. case WASM_OP_TABLE_INIT:
  6794. case WASM_OP_TABLE_COPY:
  6795. /* tableidx */
  6796. skip_leb_uint32(p, p_end);
  6797. /* elemidx */
  6798. skip_leb_uint32(p, p_end);
  6799. break;
  6800. case WASM_OP_ELEM_DROP:
  6801. /* elemidx */
  6802. skip_leb_uint32(p, p_end);
  6803. break;
  6804. case WASM_OP_TABLE_SIZE:
  6805. case WASM_OP_TABLE_GROW:
  6806. case WASM_OP_TABLE_FILL:
  6807. skip_leb_uint32(p, p_end); /* table idx */
  6808. break;
  6809. #endif /* WASM_ENABLE_REF_TYPES */
  6810. default:
  6811. return false;
  6812. }
  6813. break;
  6814. }
  6815. #if WASM_ENABLE_SIMD != 0
  6816. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6817. case WASM_OP_SIMD_PREFIX:
  6818. {
  6819. uint32 opcode1;
  6820. read_leb_uint32(p, p_end, opcode1);
  6821. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6822. is no larger than UINT8_MAX */
  6823. opcode = (uint8)opcode1;
  6824. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6825. */
  6826. switch (opcode) {
  6827. case SIMD_v128_load:
  6828. case SIMD_v128_load8x8_s:
  6829. case SIMD_v128_load8x8_u:
  6830. case SIMD_v128_load16x4_s:
  6831. case SIMD_v128_load16x4_u:
  6832. case SIMD_v128_load32x2_s:
  6833. case SIMD_v128_load32x2_u:
  6834. case SIMD_v128_load8_splat:
  6835. case SIMD_v128_load16_splat:
  6836. case SIMD_v128_load32_splat:
  6837. case SIMD_v128_load64_splat:
  6838. case SIMD_v128_store:
  6839. /* memarg align */
  6840. skip_leb_uint32(p, p_end);
  6841. /* memarg offset */
  6842. skip_leb_mem_offset(p, p_end);
  6843. break;
  6844. case SIMD_v128_const:
  6845. case SIMD_v8x16_shuffle:
  6846. /* immByte[16] immLaneId[16] */
  6847. CHECK_BUF1(p, p_end, 16);
  6848. p += 16;
  6849. break;
  6850. case SIMD_i8x16_extract_lane_s:
  6851. case SIMD_i8x16_extract_lane_u:
  6852. case SIMD_i8x16_replace_lane:
  6853. case SIMD_i16x8_extract_lane_s:
  6854. case SIMD_i16x8_extract_lane_u:
  6855. case SIMD_i16x8_replace_lane:
  6856. case SIMD_i32x4_extract_lane:
  6857. case SIMD_i32x4_replace_lane:
  6858. case SIMD_i64x2_extract_lane:
  6859. case SIMD_i64x2_replace_lane:
  6860. case SIMD_f32x4_extract_lane:
  6861. case SIMD_f32x4_replace_lane:
  6862. case SIMD_f64x2_extract_lane:
  6863. case SIMD_f64x2_replace_lane:
  6864. /* ImmLaneId */
  6865. CHECK_BUF(p, p_end, 1);
  6866. p++;
  6867. break;
  6868. case SIMD_v128_load8_lane:
  6869. case SIMD_v128_load16_lane:
  6870. case SIMD_v128_load32_lane:
  6871. case SIMD_v128_load64_lane:
  6872. case SIMD_v128_store8_lane:
  6873. case SIMD_v128_store16_lane:
  6874. case SIMD_v128_store32_lane:
  6875. case SIMD_v128_store64_lane:
  6876. /* memarg align */
  6877. skip_leb_uint32(p, p_end);
  6878. /* memarg offset */
  6879. skip_leb_mem_offset(p, p_end);
  6880. /* ImmLaneId */
  6881. CHECK_BUF(p, p_end, 1);
  6882. p++;
  6883. break;
  6884. case SIMD_v128_load32_zero:
  6885. case SIMD_v128_load64_zero:
  6886. /* memarg align */
  6887. skip_leb_uint32(p, p_end);
  6888. /* memarg offset */
  6889. skip_leb_mem_offset(p, p_end);
  6890. break;
  6891. default:
  6892. /*
  6893. * since latest SIMD specific used almost every value
  6894. * from 0x00 to 0xff, the default branch will present
  6895. * all opcodes without imm
  6896. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6897. */
  6898. break;
  6899. }
  6900. break;
  6901. }
  6902. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6903. #endif /* end of WASM_ENABLE_SIMD */
  6904. #if WASM_ENABLE_SHARED_MEMORY != 0
  6905. case WASM_OP_ATOMIC_PREFIX:
  6906. {
  6907. /* TODO: memory64 offset type changes */
  6908. uint32 opcode1;
  6909. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6910. read_leb_uint32(p, p_end, opcode1);
  6911. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6912. is no larger than UINT8_MAX */
  6913. opcode = (uint8)opcode1;
  6914. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6915. skip_leb_uint32(p, p_end); /* align */
  6916. skip_leb_mem_offset(p, p_end); /* offset */
  6917. }
  6918. else {
  6919. /* atomic.fence doesn't have memarg */
  6920. p++;
  6921. }
  6922. break;
  6923. }
  6924. #endif
  6925. #if WASM_ENABLE_DEBUG_INTERP != 0
  6926. case DEBUG_OP_BREAK:
  6927. {
  6928. WASMDebugInstance *debug_instance =
  6929. wasm_exec_env_get_instance(exec_env);
  6930. char original_opcode[1];
  6931. uint64 size = 1;
  6932. WASMModuleInstance *module_inst =
  6933. (WASMModuleInstance *)exec_env->module_inst;
  6934. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6935. ? (p - 1) - module_inst->module->load_addr
  6936. : ~0;
  6937. if (debug_instance) {
  6938. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6939. original_opcode, &size)
  6940. && size == 1) {
  6941. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6942. "with %02x: ",
  6943. original_opcode[0]);
  6944. opcode = original_opcode[0];
  6945. goto op_break_retry;
  6946. }
  6947. }
  6948. break;
  6949. }
  6950. #endif
  6951. default:
  6952. return false;
  6953. }
  6954. }
  6955. (void)u8;
  6956. (void)exec_env;
  6957. return false;
  6958. fail:
  6959. return false;
  6960. }
  6961. #if WASM_ENABLE_FAST_INTERP != 0
  6962. #if WASM_DEBUG_PREPROCESSOR != 0
  6963. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6964. #else
  6965. #define LOG_OP(...) (void)0
  6966. #endif
  6967. #define PATCH_ELSE 0
  6968. #define PATCH_END 1
  6969. typedef struct BranchBlockPatch {
  6970. struct BranchBlockPatch *next;
  6971. uint8 patch_type;
  6972. uint8 *code_compiled;
  6973. } BranchBlockPatch;
  6974. #endif
  6975. typedef struct BranchBlock {
  6976. uint8 label_type;
  6977. BlockType block_type;
  6978. uint8 *start_addr;
  6979. uint8 *else_addr;
  6980. uint8 *end_addr;
  6981. uint32 stack_cell_num;
  6982. #if WASM_ENABLE_GC != 0
  6983. uint32 reftype_map_num;
  6984. /* Indicate which local is used inside current block, used to validate
  6985. * local.get with non-nullable ref types */
  6986. uint8 *local_use_mask;
  6987. uint32 local_use_mask_size;
  6988. #endif
  6989. #if WASM_ENABLE_FAST_INTERP != 0
  6990. uint16 dynamic_offset;
  6991. uint8 *code_compiled;
  6992. BranchBlockPatch *patch_list;
  6993. /* This is used to save params frame_offset of of if block */
  6994. int16 *param_frame_offsets;
  6995. /* This is used to recover the dynamic offset for else branch,
  6996. * and also to remember the start offset of dynamic space which
  6997. * stores the block arguments for loop block, so we can use it
  6998. * to copy the stack operands to the loop block's arguments in
  6999. * wasm_loader_emit_br_info for opcode br. */
  7000. uint16 start_dynamic_offset;
  7001. #endif
  7002. /* Indicate the operand stack is in polymorphic state.
  7003. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7004. * to polymorphic state until the block's 'end' opcode is processed.
  7005. * If stack is in polymorphic state and stack is empty, instruction can
  7006. * pop any type of value directly without decreasing stack top pointer
  7007. * and stack cell num. */
  7008. bool is_stack_polymorphic;
  7009. } BranchBlock;
  7010. typedef struct WASMLoaderContext {
  7011. /* frame ref stack */
  7012. uint8 *frame_ref;
  7013. uint8 *frame_ref_bottom;
  7014. uint8 *frame_ref_boundary;
  7015. uint32 frame_ref_size;
  7016. uint32 stack_cell_num;
  7017. uint32 max_stack_cell_num;
  7018. #if WASM_ENABLE_GC != 0
  7019. /* frame reftype map stack */
  7020. WASMRefTypeMap *frame_reftype_map;
  7021. WASMRefTypeMap *frame_reftype_map_bottom;
  7022. WASMRefTypeMap *frame_reftype_map_boundary;
  7023. uint32 frame_reftype_map_size;
  7024. uint32 reftype_map_num;
  7025. uint32 max_reftype_map_num;
  7026. /* Current module */
  7027. WASMModule *module;
  7028. /* Current module's ref_type_set */
  7029. HashMap *ref_type_set;
  7030. /* Always point to local variable ref_type of
  7031. wasm_loader_prepare_bytecode */
  7032. WASMRefType *ref_type_tmp;
  7033. #endif
  7034. /* frame csp stack */
  7035. BranchBlock *frame_csp;
  7036. BranchBlock *frame_csp_bottom;
  7037. BranchBlock *frame_csp_boundary;
  7038. uint32 frame_csp_size;
  7039. uint32 csp_num;
  7040. uint32 max_csp_num;
  7041. #if WASM_ENABLE_FAST_INTERP != 0
  7042. /* frame offset stack */
  7043. int16 *frame_offset;
  7044. int16 *frame_offset_bottom;
  7045. int16 *frame_offset_boundary;
  7046. uint32 frame_offset_size;
  7047. int16 dynamic_offset;
  7048. int16 start_dynamic_offset;
  7049. int16 max_dynamic_offset;
  7050. /* preserved local offset */
  7051. int16 preserved_local_offset;
  7052. /* const buffer */
  7053. uint8 *const_buf;
  7054. uint16 num_const;
  7055. uint16 const_cell_num;
  7056. uint32 const_buf_size;
  7057. /* processed code */
  7058. uint8 *p_code_compiled;
  7059. uint8 *p_code_compiled_end;
  7060. uint32 code_compiled_size;
  7061. /* If the last opcode will be dropped, the peak memory usage will be larger
  7062. * than the final code_compiled_size, we record the peak size to ensure
  7063. * there will not be invalid memory access during second traverse */
  7064. uint32 code_compiled_peak_size;
  7065. #endif
  7066. } WASMLoaderContext;
  7067. typedef struct Const {
  7068. WASMValue value;
  7069. uint16 slot_index;
  7070. uint8 value_type;
  7071. } Const;
  7072. #define CHECK_CSP_PUSH() \
  7073. do { \
  7074. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7075. MEM_REALLOC( \
  7076. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7077. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7078. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7079. ctx->frame_csp_boundary = \
  7080. ctx->frame_csp_bottom \
  7081. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7082. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7083. } \
  7084. } while (0)
  7085. #define CHECK_CSP_POP() \
  7086. do { \
  7087. if (ctx->csp_num < 1) { \
  7088. set_error_buf(error_buf, error_buf_size, \
  7089. "type mismatch: " \
  7090. "expect data but block stack was empty"); \
  7091. goto fail; \
  7092. } \
  7093. } while (0)
  7094. #if WASM_ENABLE_FAST_INTERP != 0
  7095. static bool
  7096. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7097. uint32 error_buf_size)
  7098. {
  7099. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7100. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7101. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7102. ctx->frame_offset_size + 16);
  7103. ctx->frame_offset_size += 16;
  7104. ctx->frame_offset_boundary =
  7105. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7106. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7107. }
  7108. return true;
  7109. fail:
  7110. return false;
  7111. }
  7112. static bool
  7113. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7114. {
  7115. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7116. return false;
  7117. return true;
  7118. }
  7119. static void
  7120. free_label_patch_list(BranchBlock *frame_csp)
  7121. {
  7122. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7123. BranchBlockPatch *next;
  7124. while (label_patch != NULL) {
  7125. next = label_patch->next;
  7126. wasm_runtime_free(label_patch);
  7127. label_patch = next;
  7128. }
  7129. frame_csp->patch_list = NULL;
  7130. }
  7131. static void
  7132. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7133. {
  7134. BranchBlock *tmp_csp = frame_csp;
  7135. uint32 i;
  7136. for (i = 0; i < csp_num; i++) {
  7137. free_label_patch_list(tmp_csp);
  7138. tmp_csp++;
  7139. }
  7140. }
  7141. static void
  7142. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7143. {
  7144. BranchBlock *tmp_csp = frame_csp;
  7145. uint32 i;
  7146. for (i = 0; i < csp_num; i++) {
  7147. if (tmp_csp->param_frame_offsets)
  7148. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7149. tmp_csp++;
  7150. }
  7151. }
  7152. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7153. #if WASM_ENABLE_GC != 0
  7154. static bool
  7155. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7156. char *error_buf, uint32 error_buf_size)
  7157. {
  7158. BranchBlock *current_csp = ctx->frame_csp - 1;
  7159. uint32 local_mask_size;
  7160. if (local_count == 0) {
  7161. current_csp->local_use_mask_size = 0;
  7162. return true;
  7163. }
  7164. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7165. * else branch, we don't need to allocate memory again */
  7166. if (!current_csp->local_use_mask) {
  7167. local_mask_size = (local_count + 7) / sizeof(uint8);
  7168. if (!(current_csp->local_use_mask =
  7169. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7170. return false;
  7171. }
  7172. current_csp->local_use_mask_size = local_mask_size;
  7173. }
  7174. else {
  7175. local_mask_size = current_csp->local_use_mask_size;
  7176. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7177. }
  7178. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7179. /* For non-function blocks, inherit the use status from parent block */
  7180. BranchBlock *parent_csp = current_csp - 1;
  7181. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7182. bh_assert(parent_csp->local_use_mask);
  7183. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7184. parent_csp->local_use_mask, local_mask_size);
  7185. }
  7186. return true;
  7187. }
  7188. static void
  7189. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7190. {
  7191. BranchBlock *current_csp = ctx->frame_csp - 1;
  7192. bh_assert(current_csp->local_use_mask
  7193. || current_csp->local_use_mask_size == 0);
  7194. if (current_csp->local_use_mask) {
  7195. wasm_runtime_free(current_csp->local_use_mask);
  7196. }
  7197. current_csp->local_use_mask = NULL;
  7198. current_csp->local_use_mask_size = 0;
  7199. }
  7200. static void
  7201. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7202. {
  7203. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7204. uint32 i;
  7205. for (i = 0; i < ctx->csp_num; i++) {
  7206. if (tmp_csp->local_use_mask) {
  7207. wasm_runtime_free(tmp_csp->local_use_mask);
  7208. tmp_csp->local_use_mask = NULL;
  7209. tmp_csp->local_use_mask_size = 0;
  7210. }
  7211. tmp_csp++;
  7212. }
  7213. }
  7214. static void
  7215. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7216. {
  7217. BranchBlock *current_csp = ctx->frame_csp - 1;
  7218. uint32 byte_offset = index / sizeof(uint8);
  7219. uint32 bit_offset = index % sizeof(uint8);
  7220. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7221. bh_assert(current_csp->local_use_mask);
  7222. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7223. }
  7224. static bool
  7225. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7226. {
  7227. BranchBlock *current_csp = ctx->frame_csp - 1;
  7228. uint32 byte_offset = index / sizeof(uint8);
  7229. uint32 bit_offset = index % sizeof(uint8);
  7230. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7231. bh_assert(current_csp->local_use_mask);
  7232. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7233. ? true
  7234. : false;
  7235. }
  7236. #endif /* end of WASM_ENABLE_GC != 0 */
  7237. static void
  7238. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7239. {
  7240. if (ctx) {
  7241. if (ctx->frame_ref_bottom)
  7242. wasm_runtime_free(ctx->frame_ref_bottom);
  7243. #if WASM_ENABLE_GC != 0
  7244. if (ctx->frame_reftype_map_bottom)
  7245. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7246. #endif
  7247. if (ctx->frame_csp_bottom) {
  7248. #if WASM_ENABLE_FAST_INTERP != 0
  7249. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7250. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7251. ctx->csp_num);
  7252. #endif
  7253. #if WASM_ENABLE_GC != 0
  7254. wasm_loader_clean_all_local_use_masks(ctx);
  7255. #endif
  7256. wasm_runtime_free(ctx->frame_csp_bottom);
  7257. }
  7258. #if WASM_ENABLE_FAST_INTERP != 0
  7259. if (ctx->frame_offset_bottom)
  7260. wasm_runtime_free(ctx->frame_offset_bottom);
  7261. if (ctx->const_buf)
  7262. wasm_runtime_free(ctx->const_buf);
  7263. #endif
  7264. wasm_runtime_free(ctx);
  7265. }
  7266. }
  7267. static WASMLoaderContext *
  7268. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7269. {
  7270. WASMLoaderContext *loader_ctx =
  7271. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7272. if (!loader_ctx)
  7273. return NULL;
  7274. loader_ctx->frame_ref_size = 32;
  7275. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7276. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7277. goto fail;
  7278. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7279. #if WASM_ENABLE_GC != 0
  7280. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7281. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7282. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7283. error_buf_size)))
  7284. goto fail;
  7285. loader_ctx->frame_reftype_map_boundary =
  7286. loader_ctx->frame_reftype_map_bottom + 16;
  7287. #endif
  7288. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7289. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7290. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7291. goto fail;
  7292. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7293. #if WASM_ENABLE_EXCE_HANDLING != 0
  7294. func->exception_handler_count = 0;
  7295. #endif
  7296. #if WASM_ENABLE_FAST_INTERP != 0
  7297. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7298. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7299. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7300. error_buf_size)))
  7301. goto fail;
  7302. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7303. loader_ctx->num_const = 0;
  7304. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7305. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7306. error_buf, error_buf_size)))
  7307. goto fail;
  7308. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7309. set_error_buf(error_buf, error_buf_size,
  7310. "fast interpreter offset overflow");
  7311. goto fail;
  7312. }
  7313. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7314. loader_ctx->max_dynamic_offset =
  7315. func->param_cell_num + func->local_cell_num;
  7316. #endif
  7317. return loader_ctx;
  7318. fail:
  7319. wasm_loader_ctx_destroy(loader_ctx);
  7320. return NULL;
  7321. }
  7322. static bool
  7323. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7324. uint32 error_buf_size)
  7325. {
  7326. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7327. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7328. /* Increase the frame ref stack */
  7329. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7330. ctx->frame_ref_size + 16);
  7331. ctx->frame_ref_size += 16;
  7332. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7333. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7334. }
  7335. #if WASM_ENABLE_GC != 0
  7336. if (wasm_is_type_multi_byte_type(type)
  7337. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7338. /* Increase the frame reftype map stack */
  7339. bh_assert(
  7340. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7341. * sizeof(WASMRefTypeMap))
  7342. == ctx->frame_reftype_map_size);
  7343. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7344. ctx->frame_reftype_map_size
  7345. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7346. ctx->frame_reftype_map =
  7347. ctx->frame_reftype_map_bottom
  7348. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7349. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7350. ctx->frame_reftype_map_boundary =
  7351. ctx->frame_reftype_map_bottom
  7352. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7353. }
  7354. #endif
  7355. return true;
  7356. fail:
  7357. return false;
  7358. }
  7359. static bool
  7360. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7361. uint32 error_buf_size)
  7362. {
  7363. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7364. uint32 i;
  7365. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7366. return false;
  7367. #if WASM_ENABLE_GC != 0
  7368. if (wasm_is_type_multi_byte_type(type)) {
  7369. WASMRefType *ref_type;
  7370. if (!(ref_type =
  7371. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7372. error_buf, error_buf_size))) {
  7373. return false;
  7374. }
  7375. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7376. /* Increase the frame reftype map stack */
  7377. bh_assert((uint32)((ctx->frame_reftype_map
  7378. - ctx->frame_reftype_map_bottom)
  7379. * sizeof(WASMRefTypeMap))
  7380. == ctx->frame_reftype_map_size);
  7381. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7382. ctx->frame_reftype_map_size,
  7383. ctx->frame_reftype_map_size
  7384. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7385. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7386. + ctx->frame_reftype_map_size
  7387. / ((uint32)sizeof(WASMRefTypeMap));
  7388. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7389. ctx->frame_reftype_map_boundary =
  7390. ctx->frame_reftype_map_bottom
  7391. + ctx->frame_reftype_map_size
  7392. / ((uint32)sizeof(WASMRefTypeMap));
  7393. }
  7394. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7395. ctx->frame_reftype_map->ref_type = ref_type;
  7396. ctx->frame_reftype_map++;
  7397. ctx->reftype_map_num++;
  7398. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7399. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7400. }
  7401. #endif
  7402. for (i = 0; i < type_cell_num; i++)
  7403. *ctx->frame_ref++ = type;
  7404. ctx->stack_cell_num += type_cell_num;
  7405. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7406. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7407. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7408. set_error_buf(error_buf, error_buf_size,
  7409. "operand stack depth limit exceeded");
  7410. return false;
  7411. }
  7412. }
  7413. return true;
  7414. #if WASM_ENABLE_GC != 0
  7415. fail:
  7416. return false;
  7417. #endif
  7418. }
  7419. static bool
  7420. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7421. int32 stack_cell_num,
  7422. #if WASM_ENABLE_GC != 0
  7423. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7424. #endif
  7425. uint8 type,
  7426. #if WASM_ENABLE_GC != 0
  7427. WASMRefType *ref_type,
  7428. #endif
  7429. char *error_buf, uint32 error_buf_size)
  7430. {
  7431. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7432. #if WASM_ENABLE_GC != 0
  7433. WASMRefType *frame_reftype = NULL;
  7434. #endif
  7435. if (stack_cell_num < type_cell_num) {
  7436. set_error_buf(error_buf, error_buf_size,
  7437. "type mismatch: expect data but stack was empty");
  7438. return false;
  7439. }
  7440. #if WASM_ENABLE_GC == 0
  7441. for (i = 0; i < type_cell_num; i++) {
  7442. if (*(frame_ref - 1 - i) != type) {
  7443. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7444. "type mismatch: expect ", type2str(type),
  7445. " but got other");
  7446. return false;
  7447. }
  7448. }
  7449. #else
  7450. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7451. bh_assert(reftype_map_num > 0);
  7452. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7453. }
  7454. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7455. ref_type, ctx->module->types,
  7456. ctx->module->type_count)) {
  7457. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7458. "type mismatch: expect ", type2str(type),
  7459. " but got other");
  7460. return false;
  7461. }
  7462. for (i = 0; i < type_cell_num - 1; i++) {
  7463. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7464. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7465. "type mismatch: expect ", type2str(type),
  7466. " but got other");
  7467. return false;
  7468. }
  7469. }
  7470. #endif
  7471. return true;
  7472. }
  7473. static bool
  7474. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7475. uint32 error_buf_size)
  7476. {
  7477. int32 block_stack_cell_num =
  7478. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7479. #if WASM_ENABLE_GC != 0
  7480. int32 reftype_map_num =
  7481. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7482. #endif
  7483. if (block_stack_cell_num > 0) {
  7484. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7485. /* the stack top is a value of any type, return success */
  7486. return true;
  7487. }
  7488. #if WASM_ENABLE_GC != 0
  7489. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7490. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7491. WASMRefType *stack_top_ref_type = NULL;
  7492. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7493. bh_assert(reftype_map_num > 0);
  7494. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7495. }
  7496. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7497. ctx->ref_type_tmp, ctx->module->types,
  7498. ctx->module->type_count)) {
  7499. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7500. uint32 ref_type_struct_size =
  7501. wasm_reftype_struct_size(stack_top_ref_type);
  7502. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7503. stack_top_ref_type, ref_type_struct_size);
  7504. }
  7505. return true;
  7506. }
  7507. }
  7508. #endif
  7509. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7510. #if WASM_ENABLE_GC != 0
  7511. ctx->frame_reftype_map, reftype_map_num,
  7512. #endif
  7513. type,
  7514. #if WASM_ENABLE_GC != 0
  7515. ctx->ref_type_tmp,
  7516. #endif
  7517. error_buf, error_buf_size)) {
  7518. return false;
  7519. }
  7520. return true;
  7521. }
  7522. static bool
  7523. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7524. uint32 error_buf_size)
  7525. {
  7526. BranchBlock *cur_block = ctx->frame_csp - 1;
  7527. int32 available_stack_cell =
  7528. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7529. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7530. /* Directly return success if current block is in stack
  7531. polymorphic state while stack is empty. */
  7532. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7533. return true;
  7534. if (type == VALUE_TYPE_VOID)
  7535. return true;
  7536. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7537. return false;
  7538. bh_assert(available_stack_cell > 0);
  7539. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7540. type = VALUE_TYPE_ANY;
  7541. cell_num_to_pop = 1;
  7542. }
  7543. ctx->frame_ref -= cell_num_to_pop;
  7544. ctx->stack_cell_num -= cell_num_to_pop;
  7545. #if WASM_ENABLE_GC != 0
  7546. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7547. ctx->frame_reftype_map--;
  7548. ctx->reftype_map_num--;
  7549. }
  7550. #endif
  7551. return true;
  7552. }
  7553. #if WASM_ENABLE_GC != 0
  7554. /* Get the stack top element of current block */
  7555. static bool
  7556. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7557. WASMRefType **p_ref_type, char *error_buf,
  7558. uint32 error_buf_size)
  7559. {
  7560. BranchBlock *cur_block = ctx->frame_csp - 1;
  7561. int32 available_stack_cell =
  7562. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7563. if (available_stack_cell <= 0) {
  7564. /* Directly return success if current block is in stack
  7565. polymorphic state while stack is empty. */
  7566. if (cur_block->is_stack_polymorphic) {
  7567. *p_type = VALUE_TYPE_ANY;
  7568. return true;
  7569. }
  7570. else {
  7571. set_error_buf(
  7572. error_buf, error_buf_size,
  7573. "type mismatch: expect data but block stack was empty");
  7574. return false;
  7575. }
  7576. }
  7577. *p_type = *(ctx->frame_ref - 1);
  7578. if (wasm_is_type_multi_byte_type(*p_type)) {
  7579. int32 available_reftype_map =
  7580. (int32)(ctx->reftype_map_num
  7581. - (ctx->frame_csp - 1)->reftype_map_num);
  7582. bh_assert(available_reftype_map > 0);
  7583. (void)available_reftype_map;
  7584. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7585. }
  7586. return true;
  7587. }
  7588. #if WASM_ENABLE_FAST_INTERP != 0
  7589. static bool
  7590. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7591. char *error_buf, uint32 error_buf_size);
  7592. #endif
  7593. /* Check whether the stack top elem is a heap object, and if yes,
  7594. pop and return it */
  7595. static bool
  7596. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7597. WASMRefType *ref_ht_ret, char *error_buf,
  7598. uint32 error_buf_size)
  7599. {
  7600. uint8 type = 0;
  7601. WASMRefType *ref_type = NULL;
  7602. /* Get stack top element */
  7603. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7604. error_buf_size)) {
  7605. return false;
  7606. }
  7607. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7608. /* stack top isn't a ref type */
  7609. && !wasm_is_type_reftype(type)) {
  7610. set_error_buf(error_buf, error_buf_size,
  7611. "type mismatch: expect heap object but got others");
  7612. return false;
  7613. }
  7614. /* POP stack top */
  7615. if (wasm_is_type_multi_byte_type(type)) {
  7616. bh_assert(ref_type);
  7617. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7618. wasm_reftype_struct_size(ref_type));
  7619. }
  7620. #if WASM_ENABLE_FAST_INTERP != 0
  7621. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7622. error_buf_size)) {
  7623. return false;
  7624. }
  7625. #else
  7626. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7627. return false;
  7628. }
  7629. #endif
  7630. if (p_type)
  7631. *p_type = type;
  7632. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7633. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7634. wasm_reftype_struct_size(ref_type));
  7635. }
  7636. return true;
  7637. }
  7638. /* Check whether the stack top elem is subtype of (ref null ht),
  7639. and if yes, pop it and return the converted (ref ht) */
  7640. static bool
  7641. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7642. WASMRefType *ref_ht_ret, char *error_buf,
  7643. uint32 error_buf_size)
  7644. {
  7645. uint8 type = 0;
  7646. WASMRefType ref_type = { 0 };
  7647. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7648. error_buf_size)) {
  7649. return false;
  7650. }
  7651. /* Convert to related (ref ht) and return */
  7652. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7653. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7654. */
  7655. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7656. HEAP_TYPE_ARRAY
  7657. + (type - REF_TYPE_ARRAYREF));
  7658. type = ref_ht_ret->ref_type;
  7659. }
  7660. else if (wasm_is_reftype_htref_nullable(type)
  7661. || wasm_is_reftype_htref_non_nullable(type)) {
  7662. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7663. wasm_reftype_struct_size(&ref_type));
  7664. /* Convert to (ref ht) */
  7665. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7666. ref_ht_ret->ref_ht_common.nullable = false;
  7667. type = ref_ht_ret->ref_type;
  7668. }
  7669. *p_type = type;
  7670. return true;
  7671. }
  7672. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7673. and if yes, pop it and return the type_idx */
  7674. static bool
  7675. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7676. uint32 *p_type_idx, char *error_buf,
  7677. uint32 error_buf_size)
  7678. {
  7679. uint8 type = 0;
  7680. int32 type_idx = -1;
  7681. WASMRefType *ref_type = NULL;
  7682. /* Get stack top element */
  7683. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7684. error_buf_size)) {
  7685. return false;
  7686. }
  7687. if (type != VALUE_TYPE_ANY) {
  7688. /* stack top isn't (ref null $t) */
  7689. if (!((wasm_is_reftype_htref_nullable(type)
  7690. || wasm_is_reftype_htref_non_nullable(type))
  7691. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7692. set_error_buf(error_buf, error_buf_size,
  7693. "type mismatch: expect (ref null $t) but got others");
  7694. return false;
  7695. }
  7696. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7697. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7698. wasm_reftype_struct_size(ref_type));
  7699. }
  7700. /* POP stack top */
  7701. #if WASM_ENABLE_FAST_INTERP != 0
  7702. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7703. error_buf_size)) {
  7704. return false;
  7705. }
  7706. #else
  7707. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7708. return false;
  7709. }
  7710. #endif
  7711. /* Convert to type_idx and return */
  7712. *p_type = type;
  7713. if (type != VALUE_TYPE_ANY)
  7714. *p_type_idx = (uint32)type_idx;
  7715. return true;
  7716. }
  7717. #endif /* WASM_ENABLE_GC != 0 */
  7718. #if WASM_ENABLE_FAST_INTERP == 0
  7719. static bool
  7720. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7721. uint8 type_push, uint8 type_pop, char *error_buf,
  7722. uint32 error_buf_size)
  7723. {
  7724. for (int i = 0; i < pop_cnt; i++) {
  7725. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7726. error_buf_size))
  7727. return false;
  7728. }
  7729. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7730. return false;
  7731. return true;
  7732. }
  7733. #endif
  7734. static bool
  7735. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7736. BlockType block_type, uint8 *start_addr,
  7737. char *error_buf, uint32 error_buf_size)
  7738. {
  7739. CHECK_CSP_PUSH();
  7740. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7741. ctx->frame_csp->label_type = label_type;
  7742. ctx->frame_csp->block_type = block_type;
  7743. ctx->frame_csp->start_addr = start_addr;
  7744. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7745. #if WASM_ENABLE_GC != 0
  7746. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7747. #endif
  7748. #if WASM_ENABLE_FAST_INTERP != 0
  7749. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7750. ctx->frame_csp->patch_list = NULL;
  7751. #endif
  7752. ctx->frame_csp++;
  7753. ctx->csp_num++;
  7754. if (ctx->csp_num > ctx->max_csp_num) {
  7755. ctx->max_csp_num = ctx->csp_num;
  7756. if (ctx->max_csp_num > UINT16_MAX) {
  7757. set_error_buf(error_buf, error_buf_size,
  7758. "label stack depth limit exceeded");
  7759. return false;
  7760. }
  7761. }
  7762. return true;
  7763. fail:
  7764. return false;
  7765. }
  7766. static bool
  7767. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7768. uint32 error_buf_size)
  7769. {
  7770. CHECK_CSP_POP();
  7771. #if WASM_ENABLE_FAST_INTERP != 0
  7772. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7773. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7774. #endif
  7775. ctx->frame_csp--;
  7776. ctx->csp_num--;
  7777. return true;
  7778. fail:
  7779. return false;
  7780. }
  7781. #if WASM_ENABLE_FAST_INTERP != 0
  7782. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7783. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7784. #define emit_label(opcode) \
  7785. do { \
  7786. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7787. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7788. } while (0)
  7789. #define skip_label() \
  7790. do { \
  7791. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7792. LOG_OP("\ndelete last op\n"); \
  7793. } while (0)
  7794. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7795. #if UINTPTR_MAX == UINT64_MAX
  7796. #define emit_label(opcode) \
  7797. do { \
  7798. int32 offset = \
  7799. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7800. /* emit int32 relative offset in 64-bit target */ \
  7801. wasm_loader_emit_uint32(loader_ctx, offset); \
  7802. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7803. } while (0)
  7804. #else
  7805. #define emit_label(opcode) \
  7806. do { \
  7807. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7808. /* emit uint32 label address in 32-bit target */ \
  7809. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7810. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7811. } while (0)
  7812. #endif
  7813. #define skip_label() \
  7814. do { \
  7815. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7816. LOG_OP("\ndelete last op\n"); \
  7817. } while (0)
  7818. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7819. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7820. #define emit_label(opcode) \
  7821. do { \
  7822. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7823. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7824. } while (0)
  7825. #define skip_label() \
  7826. do { \
  7827. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7828. LOG_OP("\ndelete last op\n"); \
  7829. } while (0)
  7830. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7831. #define emit_empty_label_addr_and_frame_ip(type) \
  7832. do { \
  7833. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7834. loader_ctx->p_code_compiled, error_buf, \
  7835. error_buf_size)) \
  7836. goto fail; \
  7837. /* label address, to be patched */ \
  7838. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7839. } while (0)
  7840. #define emit_br_info(frame_csp, is_br) \
  7841. do { \
  7842. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7843. error_buf_size)) \
  7844. goto fail; \
  7845. } while (0)
  7846. #define LAST_OP_OUTPUT_I32() \
  7847. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7848. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7849. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7850. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7851. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7852. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7853. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7854. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7855. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7856. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7857. || (last_op == EXT_OP_COPY_STACK_TOP)
  7858. #define LAST_OP_OUTPUT_I64() \
  7859. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7860. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7861. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7862. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7863. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7864. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7865. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7866. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7867. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7868. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7869. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7870. #define GET_CONST_OFFSET(type, val) \
  7871. do { \
  7872. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7873. &operand_offset, error_buf, \
  7874. error_buf_size))) \
  7875. goto fail; \
  7876. } while (0)
  7877. #define GET_CONST_F32_OFFSET(type, fval) \
  7878. do { \
  7879. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7880. &operand_offset, error_buf, \
  7881. error_buf_size))) \
  7882. goto fail; \
  7883. } while (0)
  7884. #define GET_CONST_F64_OFFSET(type, fval) \
  7885. do { \
  7886. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7887. &operand_offset, error_buf, \
  7888. error_buf_size))) \
  7889. goto fail; \
  7890. } while (0)
  7891. #define emit_operand(ctx, offset) \
  7892. do { \
  7893. wasm_loader_emit_int16(ctx, offset); \
  7894. LOG_OP("%d\t", offset); \
  7895. } while (0)
  7896. #define emit_byte(ctx, byte) \
  7897. do { \
  7898. wasm_loader_emit_uint8(ctx, byte); \
  7899. LOG_OP("%d\t", byte); \
  7900. } while (0)
  7901. #define emit_uint32(ctx, value) \
  7902. do { \
  7903. wasm_loader_emit_uint32(ctx, value); \
  7904. LOG_OP("%d\t", value); \
  7905. } while (0)
  7906. #define emit_uint64(ctx, value) \
  7907. do { \
  7908. wasm_loader_emit_const(ctx, &value, false); \
  7909. LOG_OP("%lld\t", value); \
  7910. } while (0)
  7911. #define emit_float32(ctx, value) \
  7912. do { \
  7913. wasm_loader_emit_const(ctx, &value, true); \
  7914. LOG_OP("%f\t", value); \
  7915. } while (0)
  7916. #define emit_float64(ctx, value) \
  7917. do { \
  7918. wasm_loader_emit_const(ctx, &value, false); \
  7919. LOG_OP("%f\t", value); \
  7920. } while (0)
  7921. static bool
  7922. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7923. {
  7924. if (!(ctx->p_code_compiled =
  7925. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7926. return false;
  7927. ctx->p_code_compiled_end =
  7928. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7929. /* clean up frame ref */
  7930. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7931. ctx->frame_ref = ctx->frame_ref_bottom;
  7932. ctx->stack_cell_num = 0;
  7933. #if WASM_ENABLE_GC != 0
  7934. /* clean up reftype map */
  7935. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7936. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7937. ctx->reftype_map_num = 0;
  7938. #endif
  7939. /* clean up frame csp */
  7940. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7941. ctx->frame_csp = ctx->frame_csp_bottom;
  7942. ctx->csp_num = 0;
  7943. ctx->max_csp_num = 0;
  7944. /* clean up frame offset */
  7945. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7946. ctx->frame_offset = ctx->frame_offset_bottom;
  7947. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7948. /* init preserved local offsets */
  7949. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7950. /* const buf is reserved */
  7951. return true;
  7952. }
  7953. static void
  7954. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7955. {
  7956. ctx->code_compiled_size += size;
  7957. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7958. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7959. }
  7960. }
  7961. static void
  7962. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7963. {
  7964. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7965. if (ctx->p_code_compiled) {
  7966. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7967. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7968. #endif
  7969. bh_memcpy_s(ctx->p_code_compiled,
  7970. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7971. value, size);
  7972. ctx->p_code_compiled += size;
  7973. }
  7974. else {
  7975. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7976. bh_assert((ctx->code_compiled_size & 1) == 0);
  7977. #endif
  7978. increase_compiled_code_space(ctx, size);
  7979. }
  7980. }
  7981. static void
  7982. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7983. {
  7984. if (ctx->p_code_compiled) {
  7985. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7986. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7987. #endif
  7988. STORE_U32(ctx->p_code_compiled, value);
  7989. ctx->p_code_compiled += sizeof(uint32);
  7990. }
  7991. else {
  7992. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7993. bh_assert((ctx->code_compiled_size & 1) == 0);
  7994. #endif
  7995. increase_compiled_code_space(ctx, sizeof(uint32));
  7996. }
  7997. }
  7998. static void
  7999. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8000. {
  8001. if (ctx->p_code_compiled) {
  8002. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8003. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8004. #endif
  8005. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8006. ctx->p_code_compiled += sizeof(int16);
  8007. }
  8008. else {
  8009. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8010. bh_assert((ctx->code_compiled_size & 1) == 0);
  8011. #endif
  8012. increase_compiled_code_space(ctx, sizeof(uint16));
  8013. }
  8014. }
  8015. static void
  8016. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8017. {
  8018. if (ctx->p_code_compiled) {
  8019. *(ctx->p_code_compiled) = value;
  8020. ctx->p_code_compiled += sizeof(uint8);
  8021. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8022. ctx->p_code_compiled++;
  8023. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8024. #endif
  8025. }
  8026. else {
  8027. increase_compiled_code_space(ctx, sizeof(uint8));
  8028. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8029. increase_compiled_code_space(ctx, sizeof(uint8));
  8030. bh_assert((ctx->code_compiled_size & 1) == 0);
  8031. #endif
  8032. }
  8033. }
  8034. static void
  8035. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8036. {
  8037. if (ctx->p_code_compiled) {
  8038. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8039. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8040. #endif
  8041. STORE_PTR(ctx->p_code_compiled, value);
  8042. ctx->p_code_compiled += sizeof(void *);
  8043. }
  8044. else {
  8045. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8046. bh_assert((ctx->code_compiled_size & 1) == 0);
  8047. #endif
  8048. increase_compiled_code_space(ctx, sizeof(void *));
  8049. }
  8050. }
  8051. static void
  8052. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8053. {
  8054. if (ctx->p_code_compiled) {
  8055. ctx->p_code_compiled -= size;
  8056. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8057. if (size == sizeof(uint8)) {
  8058. ctx->p_code_compiled--;
  8059. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8060. }
  8061. #endif
  8062. }
  8063. else {
  8064. ctx->code_compiled_size -= size;
  8065. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8066. if (size == sizeof(uint8)) {
  8067. ctx->code_compiled_size--;
  8068. bh_assert((ctx->code_compiled_size & 1) == 0);
  8069. }
  8070. #endif
  8071. }
  8072. }
  8073. static bool
  8074. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8075. uint32 local_index, uint32 local_type,
  8076. bool *preserved, char *error_buf,
  8077. uint32 error_buf_size)
  8078. {
  8079. uint32 i = 0;
  8080. int16 preserved_offset = (int16)local_index;
  8081. *preserved = false;
  8082. while (i < loader_ctx->stack_cell_num) {
  8083. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8084. /* move previous local into dynamic space before a set/tee_local opcode
  8085. */
  8086. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8087. if (!(*preserved)) {
  8088. *preserved = true;
  8089. skip_label();
  8090. preserved_offset = loader_ctx->preserved_local_offset;
  8091. if (loader_ctx->p_code_compiled) {
  8092. bh_assert(preserved_offset != (int16)local_index);
  8093. }
  8094. if (is_32bit_type(local_type)) {
  8095. /* Only increase preserve offset in the second traversal */
  8096. if (loader_ctx->p_code_compiled)
  8097. loader_ctx->preserved_local_offset++;
  8098. emit_label(EXT_OP_COPY_STACK_TOP);
  8099. }
  8100. else {
  8101. if (loader_ctx->p_code_compiled)
  8102. loader_ctx->preserved_local_offset += 2;
  8103. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8104. }
  8105. emit_operand(loader_ctx, local_index);
  8106. emit_operand(loader_ctx, preserved_offset);
  8107. emit_label(opcode);
  8108. }
  8109. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8110. }
  8111. if (is_32bit_type(cur_type))
  8112. i++;
  8113. else
  8114. i += 2;
  8115. }
  8116. (void)error_buf;
  8117. (void)error_buf_size;
  8118. return true;
  8119. }
  8120. static bool
  8121. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8122. char *error_buf, uint32 error_buf_size)
  8123. {
  8124. uint32 i = 0;
  8125. bool preserve_local;
  8126. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8127. blocks will not influence the value of these locals */
  8128. while (i < loader_ctx->stack_cell_num) {
  8129. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8130. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8131. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8132. && (cur_offset >= 0)) {
  8133. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8134. cur_type, &preserve_local,
  8135. error_buf, error_buf_size)))
  8136. return false;
  8137. }
  8138. if (is_32bit_type(cur_type)) {
  8139. i++;
  8140. }
  8141. else {
  8142. i += 2;
  8143. }
  8144. }
  8145. return true;
  8146. }
  8147. static bool
  8148. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8149. uint8 *p_code_compiled, char *error_buf,
  8150. uint32 error_buf_size)
  8151. {
  8152. BranchBlockPatch *patch =
  8153. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8154. if (!patch) {
  8155. return false;
  8156. }
  8157. patch->patch_type = patch_type;
  8158. patch->code_compiled = p_code_compiled;
  8159. if (!frame_csp->patch_list) {
  8160. frame_csp->patch_list = patch;
  8161. patch->next = NULL;
  8162. }
  8163. else {
  8164. patch->next = frame_csp->patch_list;
  8165. frame_csp->patch_list = patch;
  8166. }
  8167. return true;
  8168. }
  8169. static void
  8170. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8171. {
  8172. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8173. BranchBlockPatch *node = frame_csp->patch_list;
  8174. BranchBlockPatch *node_prev = NULL, *node_next;
  8175. if (!ctx->p_code_compiled)
  8176. return;
  8177. while (node) {
  8178. node_next = node->next;
  8179. if (node->patch_type == patch_type) {
  8180. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8181. if (node_prev == NULL) {
  8182. frame_csp->patch_list = node_next;
  8183. }
  8184. else {
  8185. node_prev->next = node_next;
  8186. }
  8187. wasm_runtime_free(node);
  8188. }
  8189. else {
  8190. node_prev = node;
  8191. }
  8192. node = node_next;
  8193. }
  8194. }
  8195. static bool
  8196. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8197. bool is_br, char *error_buf, uint32 error_buf_size)
  8198. {
  8199. /* br info layout:
  8200. * a) arity of target block
  8201. * b) total cell num of arity values
  8202. * c) each arity value's cell num
  8203. * d) each arity value's src frame offset
  8204. * e) each arity values's dst dynamic offset
  8205. * f) branch target address
  8206. *
  8207. * Note: b-e are omitted when arity is 0 so that
  8208. * interpreter can recover the br info quickly.
  8209. */
  8210. BlockType *block_type = &frame_csp->block_type;
  8211. uint8 *types = NULL, cell;
  8212. #if WASM_ENABLE_GC != 0
  8213. WASMRefTypeMap *reftype_maps;
  8214. uint32 reftype_map_count;
  8215. #endif
  8216. uint32 arity = 0;
  8217. int32 i;
  8218. int16 *frame_offset = ctx->frame_offset;
  8219. uint16 dynamic_offset;
  8220. /* Note: loop's arity is different from if and block. loop's arity is
  8221. * its parameter count while if and block arity is result count.
  8222. */
  8223. #if WASM_ENABLE_GC == 0
  8224. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8225. arity = block_type_get_param_types(block_type, &types);
  8226. else
  8227. arity = block_type_get_result_types(block_type, &types);
  8228. #else
  8229. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8230. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8231. &reftype_map_count);
  8232. else
  8233. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8234. &reftype_map_count);
  8235. #endif
  8236. /* Part a */
  8237. emit_uint32(ctx, arity);
  8238. if (arity) {
  8239. /* Part b */
  8240. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8241. /* Part c */
  8242. for (i = (int32)arity - 1; i >= 0; i--) {
  8243. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8244. emit_byte(ctx, cell);
  8245. }
  8246. /* Part d */
  8247. for (i = (int32)arity - 1; i >= 0; i--) {
  8248. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8249. frame_offset -= cell;
  8250. emit_operand(ctx, *(int16 *)(frame_offset));
  8251. }
  8252. /* Part e */
  8253. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8254. /* Use start_dynamic_offset which was set in
  8255. copy_params_to_dynamic_space */
  8256. dynamic_offset = frame_csp->start_dynamic_offset
  8257. + wasm_get_cell_num(types, arity);
  8258. else
  8259. dynamic_offset =
  8260. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8261. if (is_br)
  8262. ctx->dynamic_offset = dynamic_offset;
  8263. for (i = (int32)arity - 1; i >= 0; i--) {
  8264. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8265. dynamic_offset -= cell;
  8266. emit_operand(ctx, dynamic_offset);
  8267. }
  8268. }
  8269. /* Part f */
  8270. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8271. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8272. }
  8273. else {
  8274. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8275. error_buf, error_buf_size))
  8276. return false;
  8277. /* label address, to be patched */
  8278. wasm_loader_emit_ptr(ctx, NULL);
  8279. }
  8280. return true;
  8281. }
  8282. static bool
  8283. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8284. bool disable_emit, int16 operand_offset,
  8285. char *error_buf, uint32 error_buf_size)
  8286. {
  8287. uint32 cell_num_to_push, i;
  8288. if (type == VALUE_TYPE_VOID)
  8289. return true;
  8290. /* only check memory overflow in first traverse */
  8291. if (ctx->p_code_compiled == NULL) {
  8292. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8293. return false;
  8294. }
  8295. if (disable_emit)
  8296. *(ctx->frame_offset)++ = operand_offset;
  8297. else {
  8298. emit_operand(ctx, ctx->dynamic_offset);
  8299. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8300. ctx->dynamic_offset++;
  8301. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8302. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8303. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8304. goto fail;
  8305. }
  8306. }
  8307. }
  8308. if (is_32bit_type(type))
  8309. return true;
  8310. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8311. for (i = 0; i < cell_num_to_push; i++) {
  8312. if (ctx->p_code_compiled == NULL) {
  8313. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8314. return false;
  8315. }
  8316. ctx->frame_offset++;
  8317. if (!disable_emit) {
  8318. ctx->dynamic_offset++;
  8319. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8320. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8321. if (ctx->max_dynamic_offset >= INT16_MAX)
  8322. goto fail;
  8323. }
  8324. }
  8325. }
  8326. return true;
  8327. fail:
  8328. set_error_buf(error_buf, error_buf_size,
  8329. "fast interpreter offset overflow");
  8330. return false;
  8331. }
  8332. /* This function should be in front of wasm_loader_pop_frame_ref
  8333. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8334. will be modified by wasm_loader_pop_frame_ref */
  8335. static bool
  8336. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8337. char *error_buf, uint32 error_buf_size)
  8338. {
  8339. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8340. then current block is the function block */
  8341. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8342. BranchBlock *cur_block = ctx->frame_csp - depth;
  8343. int32 available_stack_cell =
  8344. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8345. uint32 cell_num_to_pop;
  8346. /* Directly return success if current block is in stack
  8347. polymorphic state while stack is empty. */
  8348. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8349. return true;
  8350. if (type == VALUE_TYPE_VOID)
  8351. return true;
  8352. /* Change type to ANY when the stack top is ANY, so as to avoid
  8353. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8354. two offsets */
  8355. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8356. type = VALUE_TYPE_ANY;
  8357. cell_num_to_pop = wasm_value_type_cell_num(type);
  8358. /* Check the offset stack bottom to ensure the frame offset
  8359. stack will not go underflow. But we don't thrown error
  8360. and return true here, because the error msg should be
  8361. given in wasm_loader_pop_frame_ref */
  8362. if (!check_offset_pop(ctx, cell_num_to_pop))
  8363. return true;
  8364. ctx->frame_offset -= cell_num_to_pop;
  8365. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8366. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8367. ctx->dynamic_offset -= cell_num_to_pop;
  8368. emit_operand(ctx, *(ctx->frame_offset));
  8369. (void)error_buf;
  8370. (void)error_buf_size;
  8371. return true;
  8372. }
  8373. static bool
  8374. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8375. bool disable_emit, int16 operand_offset,
  8376. char *error_buf, uint32 error_buf_size)
  8377. {
  8378. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8379. error_buf, error_buf_size)))
  8380. return false;
  8381. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8382. return false;
  8383. return true;
  8384. }
  8385. static bool
  8386. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8387. char *error_buf, uint32 error_buf_size)
  8388. {
  8389. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8390. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8391. return false;
  8392. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8393. return false;
  8394. return true;
  8395. }
  8396. static bool
  8397. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8398. uint8 type_push, uint8 type_pop,
  8399. bool disable_emit, int16 operand_offset,
  8400. char *error_buf, uint32 error_buf_size)
  8401. {
  8402. uint8 i;
  8403. for (i = 0; i < pop_cnt; i++) {
  8404. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8405. error_buf_size))
  8406. return false;
  8407. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8408. error_buf_size))
  8409. return false;
  8410. }
  8411. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8412. operand_offset, error_buf,
  8413. error_buf_size))
  8414. return false;
  8415. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8416. return false;
  8417. return true;
  8418. }
  8419. static bool
  8420. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8421. int16 *offset, char *error_buf,
  8422. uint32 error_buf_size)
  8423. {
  8424. int8 bytes_to_increase;
  8425. int16 operand_offset = 0;
  8426. Const *c;
  8427. /* Search existing constant */
  8428. for (c = (Const *)ctx->const_buf;
  8429. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8430. /* TODO: handle v128 type? */
  8431. if ((type == c->value_type)
  8432. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8433. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8434. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8435. || (type == VALUE_TYPE_FUNCREF
  8436. && *(int32 *)value == c->value.i32)
  8437. || (type == VALUE_TYPE_EXTERNREF
  8438. && *(int32 *)value == c->value.i32)
  8439. #endif
  8440. || (type == VALUE_TYPE_F64
  8441. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8442. || (type == VALUE_TYPE_F32
  8443. && (0
  8444. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8445. operand_offset = c->slot_index;
  8446. break;
  8447. }
  8448. if (is_32bit_type(c->value_type))
  8449. operand_offset += 1;
  8450. else
  8451. operand_offset += 2;
  8452. }
  8453. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8454. /* New constant, append to the const buffer */
  8455. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8456. bytes_to_increase = 2;
  8457. }
  8458. else {
  8459. bytes_to_increase = 1;
  8460. }
  8461. /* The max cell num of const buffer is 32768 since the valid index range
  8462. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8463. * full */
  8464. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8465. *offset = 0;
  8466. return true;
  8467. }
  8468. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8469. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8470. ctx->const_buf_size + 4 * sizeof(Const));
  8471. ctx->const_buf_size += 4 * sizeof(Const);
  8472. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8473. }
  8474. c->value_type = type;
  8475. switch (type) {
  8476. case VALUE_TYPE_F64:
  8477. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8478. sizeof(float64));
  8479. ctx->const_cell_num += 2;
  8480. /* The const buf will be reversed, we use the second cell */
  8481. /* of the i64/f64 const so the final offset is correct */
  8482. operand_offset++;
  8483. break;
  8484. case VALUE_TYPE_I64:
  8485. c->value.i64 = *(int64 *)value;
  8486. ctx->const_cell_num += 2;
  8487. operand_offset++;
  8488. break;
  8489. case VALUE_TYPE_F32:
  8490. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8491. sizeof(float32));
  8492. ctx->const_cell_num++;
  8493. break;
  8494. case VALUE_TYPE_I32:
  8495. c->value.i32 = *(int32 *)value;
  8496. ctx->const_cell_num++;
  8497. break;
  8498. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8499. case VALUE_TYPE_EXTERNREF:
  8500. case VALUE_TYPE_FUNCREF:
  8501. c->value.i32 = *(int32 *)value;
  8502. ctx->const_cell_num++;
  8503. break;
  8504. #endif
  8505. default:
  8506. break;
  8507. }
  8508. c->slot_index = operand_offset;
  8509. ctx->num_const++;
  8510. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8511. (int64)c->value.i64);
  8512. }
  8513. /* use negative index for const */
  8514. operand_offset = -(operand_offset + 1);
  8515. *offset = operand_offset;
  8516. return true;
  8517. fail:
  8518. return false;
  8519. }
  8520. /*
  8521. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8522. -- Mostly used for the binary / compare operation
  8523. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8524. -- Mostly used in block / control instructions
  8525. The POP will always emit the offset on the top of the frame_offset stack
  8526. PUSH can be used in two ways:
  8527. 1. directly PUSH:
  8528. PUSH_XXX();
  8529. will allocate a dynamic space and emit
  8530. 2. silent PUSH:
  8531. operand_offset = xxx; disable_emit = true;
  8532. PUSH_XXX();
  8533. only push the frame_offset stack, no emit
  8534. */
  8535. #define TEMPLATE_PUSH(Type) \
  8536. do { \
  8537. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8538. disable_emit, operand_offset, \
  8539. error_buf, error_buf_size)) \
  8540. goto fail; \
  8541. } while (0)
  8542. #define TEMPLATE_PUSH_REF(Type) \
  8543. do { \
  8544. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8545. operand_offset, error_buf, \
  8546. error_buf_size)) \
  8547. goto fail; \
  8548. } while (0)
  8549. #define TEMPLATE_POP(Type) \
  8550. do { \
  8551. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8552. error_buf, error_buf_size)) \
  8553. goto fail; \
  8554. } while (0)
  8555. #define TEMPLATE_POP_REF(Type) \
  8556. do { \
  8557. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8558. error_buf_size)) \
  8559. goto fail; \
  8560. } while (0)
  8561. #define PUSH_OFFSET_TYPE(type) \
  8562. do { \
  8563. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8564. operand_offset, error_buf, \
  8565. error_buf_size))) \
  8566. goto fail; \
  8567. } while (0)
  8568. #define POP_OFFSET_TYPE(type) \
  8569. do { \
  8570. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8571. error_buf_size))) \
  8572. goto fail; \
  8573. } while (0)
  8574. #define POP_AND_PUSH(type_pop, type_push) \
  8575. do { \
  8576. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8577. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8578. operand_offset, error_buf, error_buf_size))) \
  8579. goto fail; \
  8580. } while (0)
  8581. /* type of POPs should be the same */
  8582. #define POP2_AND_PUSH(type_pop, type_push) \
  8583. do { \
  8584. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8585. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8586. operand_offset, error_buf, error_buf_size))) \
  8587. goto fail; \
  8588. } while (0)
  8589. #else /* WASM_ENABLE_FAST_INTERP */
  8590. #define TEMPLATE_PUSH(Type) \
  8591. do { \
  8592. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8593. error_buf, error_buf_size))) \
  8594. goto fail; \
  8595. } while (0)
  8596. #define TEMPLATE_PUSH_REF(Type) \
  8597. do { \
  8598. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8599. error_buf_size))) \
  8600. goto fail; \
  8601. } while (0)
  8602. #define TEMPLATE_POP(Type) \
  8603. do { \
  8604. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8605. error_buf, error_buf_size))) \
  8606. goto fail; \
  8607. } while (0)
  8608. #define TEMPLATE_POP_REF(Type) \
  8609. do { \
  8610. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8611. error_buf_size))) \
  8612. goto fail; \
  8613. } while (0)
  8614. #define POP_AND_PUSH(type_pop, type_push) \
  8615. do { \
  8616. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8617. type_pop, error_buf, \
  8618. error_buf_size))) \
  8619. goto fail; \
  8620. } while (0)
  8621. /* type of POPs should be the same */
  8622. #define POP2_AND_PUSH(type_pop, type_push) \
  8623. do { \
  8624. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8625. type_pop, error_buf, \
  8626. error_buf_size))) \
  8627. goto fail; \
  8628. } while (0)
  8629. #endif /* WASM_ENABLE_FAST_INTERP */
  8630. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8631. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8632. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8633. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8634. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8635. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8636. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8637. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8638. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8639. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8640. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8641. #define POP_I32() TEMPLATE_POP(I32)
  8642. #define POP_F32() TEMPLATE_POP(F32)
  8643. #define POP_I64() TEMPLATE_POP(I64)
  8644. #define POP_F64() TEMPLATE_POP(F64)
  8645. #define POP_V128() TEMPLATE_POP(V128)
  8646. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8647. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8648. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8649. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8650. #if WASM_ENABLE_FAST_INTERP != 0
  8651. static bool
  8652. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8653. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8654. {
  8655. int16 operand_offset = 0;
  8656. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8657. : loader_ctx->frame_csp;
  8658. BlockType *block_type = &block->block_type;
  8659. uint8 *return_types = NULL;
  8660. #if WASM_ENABLE_GC != 0
  8661. WASMRefTypeMap *reftype_maps = NULL;
  8662. uint32 reftype_map_count;
  8663. #endif
  8664. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8665. int32 i = 0;
  8666. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8667. *frame_offset_org = NULL;
  8668. #if WASM_ENABLE_GC == 0
  8669. return_count = block_type_get_result_types(block_type, &return_types);
  8670. #else
  8671. return_count = block_type_get_result_types(
  8672. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8673. #endif
  8674. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8675. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8676. if (return_count == 1) {
  8677. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8678. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8679. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8680. /* insert op_copy before else opcode */
  8681. if (opcode == WASM_OP_ELSE)
  8682. skip_label();
  8683. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8684. : EXT_OP_COPY_STACK_TOP_I64);
  8685. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8686. emit_operand(loader_ctx, block->dynamic_offset);
  8687. if (opcode == WASM_OP_ELSE) {
  8688. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8689. }
  8690. else {
  8691. loader_ctx->frame_offset -= cell;
  8692. loader_ctx->dynamic_offset = block->dynamic_offset;
  8693. PUSH_OFFSET_TYPE(return_types[0]);
  8694. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8695. }
  8696. if (opcode == WASM_OP_ELSE)
  8697. emit_label(opcode);
  8698. }
  8699. return true;
  8700. }
  8701. /* Copy stack top values to block's results which are in dynamic space.
  8702. * The instruction format:
  8703. * Part a: values count
  8704. * Part b: all values total cell num
  8705. * Part c: each value's cell_num, src offset and dst offset
  8706. * Part d: each value's src offset and dst offset
  8707. * Part e: each value's dst offset
  8708. */
  8709. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8710. dynamic_offset = dynamic_offset_org =
  8711. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8712. /* First traversal to get the count of values needed to be copied. */
  8713. for (i = (int32)return_count - 1; i >= 0; i--) {
  8714. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8715. frame_offset -= cells;
  8716. dynamic_offset -= cells;
  8717. if (dynamic_offset != *frame_offset) {
  8718. value_count++;
  8719. total_cel_num += cells;
  8720. }
  8721. }
  8722. if (value_count) {
  8723. uint32 j = 0;
  8724. uint8 *emit_data = NULL, *cells = NULL;
  8725. int16 *src_offsets = NULL;
  8726. uint16 *dst_offsets = NULL;
  8727. uint64 size =
  8728. (uint64)value_count
  8729. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8730. /* Allocate memory for the emit data */
  8731. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8732. return false;
  8733. cells = emit_data;
  8734. src_offsets = (int16 *)(cells + value_count);
  8735. dst_offsets = (uint16 *)(src_offsets + value_count);
  8736. /* insert op_copy before else opcode */
  8737. if (opcode == WASM_OP_ELSE)
  8738. skip_label();
  8739. emit_label(EXT_OP_COPY_STACK_VALUES);
  8740. /* Part a) */
  8741. emit_uint32(loader_ctx, value_count);
  8742. /* Part b) */
  8743. emit_uint32(loader_ctx, total_cel_num);
  8744. /* Second traversal to get each value's cell num, src offset and dst
  8745. * offset. */
  8746. frame_offset = frame_offset_org;
  8747. dynamic_offset = dynamic_offset_org;
  8748. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8749. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8750. frame_offset -= cell;
  8751. dynamic_offset -= cell;
  8752. if (dynamic_offset != *frame_offset) {
  8753. /* cell num */
  8754. cells[j] = cell;
  8755. /* src offset */
  8756. src_offsets[j] = *frame_offset;
  8757. /* dst offset */
  8758. dst_offsets[j] = dynamic_offset;
  8759. j++;
  8760. }
  8761. if (opcode == WASM_OP_ELSE) {
  8762. *frame_offset = dynamic_offset;
  8763. }
  8764. else {
  8765. loader_ctx->frame_offset = frame_offset;
  8766. loader_ctx->dynamic_offset = dynamic_offset;
  8767. PUSH_OFFSET_TYPE(return_types[i]);
  8768. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8769. loader_ctx->frame_offset = frame_offset_org;
  8770. loader_ctx->dynamic_offset = dynamic_offset_org;
  8771. }
  8772. }
  8773. bh_assert(j == value_count);
  8774. /* Emit the cells, src_offsets and dst_offsets */
  8775. for (j = 0; j < value_count; j++)
  8776. emit_byte(loader_ctx, cells[j]);
  8777. for (j = 0; j < value_count; j++)
  8778. emit_operand(loader_ctx, src_offsets[j]);
  8779. for (j = 0; j < value_count; j++)
  8780. emit_operand(loader_ctx, dst_offsets[j]);
  8781. if (opcode == WASM_OP_ELSE)
  8782. emit_label(opcode);
  8783. wasm_runtime_free(emit_data);
  8784. }
  8785. return true;
  8786. fail:
  8787. return false;
  8788. }
  8789. #endif /* WASM_ENABLE_FAST_INTERP */
  8790. #define RESERVE_BLOCK_RET() \
  8791. do { \
  8792. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8793. error_buf_size)) \
  8794. goto fail; \
  8795. } while (0)
  8796. #define PUSH_TYPE(type) \
  8797. do { \
  8798. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8799. error_buf_size))) \
  8800. goto fail; \
  8801. } while (0)
  8802. #define POP_TYPE(type) \
  8803. do { \
  8804. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8805. error_buf_size))) \
  8806. goto fail; \
  8807. } while (0)
  8808. #if WASM_ENABLE_GC == 0
  8809. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8810. do { \
  8811. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8812. _start_addr, error_buf, \
  8813. error_buf_size)) \
  8814. goto fail; \
  8815. } while (0)
  8816. #define POP_CSP() \
  8817. do { \
  8818. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8819. goto fail; \
  8820. } while (0)
  8821. #else
  8822. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8823. do { \
  8824. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8825. _start_addr, error_buf, \
  8826. error_buf_size)) \
  8827. goto fail; \
  8828. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8829. error_buf, error_buf_size)) { \
  8830. goto fail; \
  8831. } \
  8832. } while (0)
  8833. #define POP_CSP() \
  8834. do { \
  8835. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8836. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8837. goto fail; \
  8838. } while (0)
  8839. #endif /* end of WASM_ENABLE_GC == 0 */
  8840. #if WASM_ENABLE_GC == 0
  8841. #define GET_LOCAL_REFTYPE() (void)0
  8842. #else
  8843. #define GET_LOCAL_REFTYPE() \
  8844. do { \
  8845. if (wasm_is_type_multi_byte_type(local_type)) { \
  8846. WASMRefType *_ref_type; \
  8847. if (local_idx < param_count) \
  8848. _ref_type = wasm_reftype_map_find( \
  8849. param_reftype_maps, param_reftype_map_count, local_idx); \
  8850. else \
  8851. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8852. local_reftype_map_count, \
  8853. local_idx - param_count); \
  8854. bh_assert(_ref_type); \
  8855. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8856. wasm_reftype_struct_size(_ref_type)); \
  8857. } \
  8858. } while (0)
  8859. #endif /* end of WASM_ENABLE_GC == 0 */
  8860. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8861. do { \
  8862. read_leb_uint32(p, p_end, local_idx); \
  8863. if (local_idx >= param_count + local_count) { \
  8864. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8865. goto fail; \
  8866. } \
  8867. local_type = local_idx < param_count \
  8868. ? param_types[local_idx] \
  8869. : local_types[local_idx - param_count]; \
  8870. local_offset = local_offsets[local_idx]; \
  8871. GET_LOCAL_REFTYPE(); \
  8872. } while (0)
  8873. static bool
  8874. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8875. {
  8876. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8877. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8878. return false;
  8879. }
  8880. return true;
  8881. }
  8882. #define CHECK_MEMORY() \
  8883. do { \
  8884. if (!check_memory(module, error_buf, error_buf_size)) \
  8885. goto fail; \
  8886. } while (0)
  8887. static bool
  8888. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8889. uint32 error_buf_size)
  8890. {
  8891. uint8 mem_access_aligns[] = {
  8892. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8893. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8894. };
  8895. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8896. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8897. set_error_buf(error_buf, error_buf_size,
  8898. "alignment must not be larger than natural");
  8899. return false;
  8900. }
  8901. return true;
  8902. }
  8903. #if WASM_ENABLE_SIMD != 0
  8904. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8905. static bool
  8906. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8907. uint32 error_buf_size)
  8908. {
  8909. uint8 mem_access_aligns[] = {
  8910. 4, /* load */
  8911. 3, 3, 3, 3, 3, 3, /* load and extend */
  8912. 0, 1, 2, 3, /* load and splat */
  8913. 4, /* store */
  8914. };
  8915. uint8 mem_access_aligns_load_lane[] = {
  8916. 0, 1, 2, 3, /* load lane */
  8917. 0, 1, 2, 3, /* store lane */
  8918. 2, 3 /* store zero */
  8919. };
  8920. if (!((opcode <= SIMD_v128_store)
  8921. || (SIMD_v128_load8_lane <= opcode
  8922. && opcode <= SIMD_v128_load64_zero))) {
  8923. set_error_buf(error_buf, error_buf_size,
  8924. "the opcode doesn't include memarg");
  8925. return false;
  8926. }
  8927. if ((opcode <= SIMD_v128_store
  8928. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8929. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8930. && align > mem_access_aligns_load_lane[opcode
  8931. - SIMD_v128_load8_lane])) {
  8932. set_error_buf(error_buf, error_buf_size,
  8933. "alignment must not be larger than natural");
  8934. return false;
  8935. }
  8936. return true;
  8937. }
  8938. static bool
  8939. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8940. uint32 error_buf_size)
  8941. {
  8942. switch (opcode) {
  8943. case SIMD_i8x16_extract_lane_s:
  8944. case SIMD_i8x16_extract_lane_u:
  8945. case SIMD_i8x16_replace_lane:
  8946. if (lane >= 16) {
  8947. goto fail;
  8948. }
  8949. break;
  8950. case SIMD_i16x8_extract_lane_s:
  8951. case SIMD_i16x8_extract_lane_u:
  8952. case SIMD_i16x8_replace_lane:
  8953. if (lane >= 8) {
  8954. goto fail;
  8955. }
  8956. break;
  8957. case SIMD_i32x4_extract_lane:
  8958. case SIMD_i32x4_replace_lane:
  8959. case SIMD_f32x4_extract_lane:
  8960. case SIMD_f32x4_replace_lane:
  8961. if (lane >= 4) {
  8962. goto fail;
  8963. }
  8964. break;
  8965. case SIMD_i64x2_extract_lane:
  8966. case SIMD_i64x2_replace_lane:
  8967. case SIMD_f64x2_extract_lane:
  8968. case SIMD_f64x2_replace_lane:
  8969. if (lane >= 2) {
  8970. goto fail;
  8971. }
  8972. break;
  8973. case SIMD_v128_load8_lane:
  8974. case SIMD_v128_load16_lane:
  8975. case SIMD_v128_load32_lane:
  8976. case SIMD_v128_load64_lane:
  8977. case SIMD_v128_store8_lane:
  8978. case SIMD_v128_store16_lane:
  8979. case SIMD_v128_store32_lane:
  8980. case SIMD_v128_store64_lane:
  8981. case SIMD_v128_load32_zero:
  8982. case SIMD_v128_load64_zero:
  8983. {
  8984. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8985. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8986. goto fail;
  8987. }
  8988. break;
  8989. }
  8990. default:
  8991. goto fail;
  8992. }
  8993. return true;
  8994. fail:
  8995. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8996. return false;
  8997. }
  8998. static bool
  8999. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9000. {
  9001. uint8 i;
  9002. for (i = 0; i != 16; ++i) {
  9003. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9004. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9005. return false;
  9006. }
  9007. }
  9008. return true;
  9009. }
  9010. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9011. #endif /* end of WASM_ENABLE_SIMD */
  9012. #if WASM_ENABLE_SHARED_MEMORY != 0
  9013. static bool
  9014. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9015. uint32 error_buf_size)
  9016. {
  9017. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9018. uint8 mem_access_aligns[] = {
  9019. 2, 3, 0, 1, 0, 1, 2,
  9020. };
  9021. uint8 expect;
  9022. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9023. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9024. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9025. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9026. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9027. }
  9028. else {
  9029. /* 7 opcodes in every group */
  9030. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9031. }
  9032. if (align != expect) {
  9033. set_error_buf(error_buf, error_buf_size,
  9034. "alignment isn't equal to natural");
  9035. return false;
  9036. }
  9037. return true;
  9038. }
  9039. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9040. static bool
  9041. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9042. char *error_buf, uint32 error_buf_size)
  9043. {
  9044. BranchBlock *target_block, *cur_block;
  9045. BlockType *target_block_type;
  9046. uint8 type, *types = NULL, *frame_ref;
  9047. uint32 arity = 0;
  9048. int32 i, available_stack_cell;
  9049. uint16 cell_num;
  9050. #if WASM_ENABLE_GC != 0
  9051. WASMRefTypeMap *frame_reftype_map;
  9052. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9053. WASMRefType *ref_type;
  9054. uint32 reftype_map_count = 0;
  9055. int32 available_reftype_map;
  9056. bool is_type_multi_byte;
  9057. #endif
  9058. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9059. uint8 *frame_ref_after_popped = NULL;
  9060. uint8 frame_ref_tmp[4] = { 0 };
  9061. uint8 *frame_ref_buf = frame_ref_tmp;
  9062. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9063. #if WASM_ENABLE_GC != 0
  9064. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9065. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9066. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9067. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9068. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9069. #endif
  9070. #if WASM_ENABLE_FAST_INTERP != 0
  9071. int16 *frame_offset_old = loader_ctx->frame_offset;
  9072. int16 *frame_offset_after_popped = NULL;
  9073. int16 frame_offset_tmp[4] = { 0 };
  9074. int16 *frame_offset_buf = frame_offset_tmp;
  9075. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9076. #endif
  9077. bool ret = false;
  9078. bh_assert(loader_ctx->csp_num > 0);
  9079. if (loader_ctx->csp_num - 1 < depth) {
  9080. set_error_buf(error_buf, error_buf_size,
  9081. "unknown label, "
  9082. "unexpected end of section or function");
  9083. return false;
  9084. }
  9085. cur_block = loader_ctx->frame_csp - 1;
  9086. target_block = loader_ctx->frame_csp - (depth + 1);
  9087. target_block_type = &target_block->block_type;
  9088. frame_ref = loader_ctx->frame_ref;
  9089. #if WASM_ENABLE_GC != 0
  9090. frame_reftype_map = loader_ctx->frame_reftype_map;
  9091. #endif
  9092. /* Note: loop's arity is different from if and block. loop's arity is
  9093. * its parameter count while if and block arity is result count.
  9094. */
  9095. #if WASM_ENABLE_GC == 0
  9096. if (target_block->label_type == LABEL_TYPE_LOOP)
  9097. arity = block_type_get_param_types(target_block_type, &types);
  9098. else
  9099. arity = block_type_get_result_types(target_block_type, &types);
  9100. #else
  9101. if (target_block->label_type == LABEL_TYPE_LOOP)
  9102. arity = block_type_get_param_types(target_block_type, &types,
  9103. &reftype_maps, &reftype_map_count);
  9104. else
  9105. arity = block_type_get_result_types(target_block_type, &types,
  9106. &reftype_maps, &reftype_map_count);
  9107. #endif
  9108. /* If the stack is in polymorphic state, just clear the stack
  9109. * and then re-push the values to make the stack top values
  9110. * match block type. */
  9111. if (cur_block->is_stack_polymorphic) {
  9112. #if WASM_ENABLE_GC != 0
  9113. int32 j = reftype_map_count - 1;
  9114. #endif
  9115. for (i = (int32)arity - 1; i >= 0; i--) {
  9116. #if WASM_ENABLE_GC != 0
  9117. if (wasm_is_type_multi_byte_type(types[i])) {
  9118. bh_assert(reftype_maps[j].index == i);
  9119. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9120. reftype_maps[j].ref_type,
  9121. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9122. j--;
  9123. }
  9124. #endif
  9125. #if WASM_ENABLE_FAST_INTERP != 0
  9126. POP_OFFSET_TYPE(types[i]);
  9127. #endif
  9128. POP_TYPE(types[i]);
  9129. }
  9130. /* Backup stack data since it may be changed in the below
  9131. push operations, and the stack data may be used when
  9132. checking other target blocks of opcode br_table */
  9133. if (opcode == WASM_OP_BR_TABLE) {
  9134. uint64 total_size;
  9135. frame_ref_after_popped = loader_ctx->frame_ref;
  9136. total_size = (uint64)sizeof(uint8)
  9137. * (frame_ref_old - frame_ref_after_popped);
  9138. if (total_size > sizeof(frame_ref_tmp)
  9139. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9140. error_buf_size))) {
  9141. goto fail;
  9142. }
  9143. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9144. frame_ref_after_popped, (uint32)total_size);
  9145. #if WASM_ENABLE_GC != 0
  9146. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9147. total_size =
  9148. (uint64)sizeof(WASMRefTypeMap)
  9149. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9150. if (total_size > sizeof(frame_reftype_map_tmp)
  9151. && !(frame_reftype_map_buf = loader_malloc(
  9152. total_size, error_buf, error_buf_size))) {
  9153. goto fail;
  9154. }
  9155. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9156. frame_reftype_map_after_popped, (uint32)total_size);
  9157. #endif
  9158. #if WASM_ENABLE_FAST_INTERP != 0
  9159. frame_offset_after_popped = loader_ctx->frame_offset;
  9160. total_size = (uint64)sizeof(int16)
  9161. * (frame_offset_old - frame_offset_after_popped);
  9162. if (total_size > sizeof(frame_offset_tmp)
  9163. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9164. error_buf_size))) {
  9165. goto fail;
  9166. }
  9167. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9168. frame_offset_after_popped, (uint32)total_size);
  9169. #endif
  9170. }
  9171. #if WASM_ENABLE_GC != 0
  9172. j = 0;
  9173. #endif
  9174. for (i = 0; i < (int32)arity; i++) {
  9175. #if WASM_ENABLE_GC != 0
  9176. if (wasm_is_type_multi_byte_type(types[i])) {
  9177. bh_assert(reftype_maps[j].index == i);
  9178. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9179. reftype_maps[j].ref_type,
  9180. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9181. j++;
  9182. }
  9183. #endif
  9184. #if WASM_ENABLE_FAST_INTERP != 0
  9185. bool disable_emit = true;
  9186. int16 operand_offset = 0;
  9187. PUSH_OFFSET_TYPE(types[i]);
  9188. #endif
  9189. PUSH_TYPE(types[i]);
  9190. }
  9191. #if WASM_ENABLE_FAST_INTERP != 0
  9192. emit_br_info(target_block, opcode == WASM_OP_BR);
  9193. #endif
  9194. /* Restore the stack data, note that frame_ref_bottom,
  9195. frame_reftype_map_bottom, frame_offset_bottom may be
  9196. re-allocated in the above push operations */
  9197. if (opcode == WASM_OP_BR_TABLE) {
  9198. uint32 total_size;
  9199. /* The stack operand num should not be smaller than before
  9200. after pop and push operations */
  9201. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9202. loader_ctx->stack_cell_num = stack_cell_num_old;
  9203. loader_ctx->frame_ref =
  9204. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9205. total_size = (uint32)(sizeof(uint8)
  9206. * (frame_ref_old - frame_ref_after_popped));
  9207. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9208. frame_ref_buf, total_size);
  9209. #if WASM_ENABLE_GC != 0
  9210. /* The stack operand num should not be smaller than before
  9211. after pop and push operations */
  9212. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9213. loader_ctx->reftype_map_num = reftype_map_num_old;
  9214. loader_ctx->frame_reftype_map =
  9215. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9216. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9217. * (frame_reftype_map_old
  9218. - frame_reftype_map_after_popped));
  9219. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9220. total_size, frame_reftype_map_buf, total_size);
  9221. #endif
  9222. #if WASM_ENABLE_FAST_INTERP != 0
  9223. loader_ctx->frame_offset =
  9224. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9225. total_size =
  9226. (uint32)(sizeof(int16)
  9227. * (frame_offset_old - frame_offset_after_popped));
  9228. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9229. total_size, frame_offset_buf, total_size);
  9230. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9231. #endif
  9232. }
  9233. ret = true;
  9234. goto cleanup_and_return;
  9235. }
  9236. available_stack_cell =
  9237. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9238. #if WASM_ENABLE_GC != 0
  9239. available_reftype_map =
  9240. (int32)(loader_ctx->reftype_map_num
  9241. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9242. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9243. #endif
  9244. /* Check stack top values match target block type */
  9245. for (i = (int32)arity - 1; i >= 0; i--) {
  9246. type = types[i];
  9247. #if WASM_ENABLE_GC != 0
  9248. ref_type = NULL;
  9249. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9250. if (is_type_multi_byte) {
  9251. bh_assert(reftype_map);
  9252. ref_type = reftype_map->ref_type;
  9253. }
  9254. #endif
  9255. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9256. break;
  9257. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9258. #if WASM_ENABLE_GC != 0
  9259. frame_reftype_map, available_reftype_map,
  9260. #endif
  9261. type,
  9262. #if WASM_ENABLE_GC != 0
  9263. ref_type,
  9264. #endif
  9265. error_buf, error_buf_size)) {
  9266. goto fail;
  9267. }
  9268. cell_num = wasm_value_type_cell_num(types[i]);
  9269. frame_ref -= cell_num;
  9270. available_stack_cell -= cell_num;
  9271. #if WASM_ENABLE_GC != 0
  9272. if (is_type_multi_byte) {
  9273. frame_reftype_map--;
  9274. available_reftype_map--;
  9275. reftype_map--;
  9276. }
  9277. #endif
  9278. }
  9279. #if WASM_ENABLE_FAST_INTERP != 0
  9280. emit_br_info(target_block, opcode == WASM_OP_BR);
  9281. #endif
  9282. ret = true;
  9283. cleanup_and_return:
  9284. fail:
  9285. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9286. wasm_runtime_free(frame_ref_buf);
  9287. #if WASM_ENABLE_GC != 0
  9288. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9289. wasm_runtime_free(frame_reftype_map_buf);
  9290. #endif
  9291. #if WASM_ENABLE_FAST_INTERP != 0
  9292. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9293. wasm_runtime_free(frame_offset_buf);
  9294. #endif
  9295. return ret;
  9296. }
  9297. static BranchBlock *
  9298. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9299. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9300. {
  9301. uint8 *p = *p_buf, *p_end = buf_end;
  9302. BranchBlock *frame_csp_tmp;
  9303. uint32 depth;
  9304. read_leb_uint32(p, p_end, depth);
  9305. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9306. error_buf_size)) {
  9307. goto fail;
  9308. }
  9309. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9310. *p_buf = p;
  9311. return frame_csp_tmp;
  9312. fail:
  9313. return NULL;
  9314. }
  9315. #if WASM_ENABLE_EXCE_HANDLING != 0
  9316. static BranchBlock *
  9317. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9318. uint8 *buf_end, char *error_buf,
  9319. uint32 error_buf_size)
  9320. {
  9321. uint8 *p = *p_buf, *p_end = buf_end;
  9322. BranchBlock *frame_csp_tmp;
  9323. uint32 depth;
  9324. read_leb_uint32(p, p_end, depth);
  9325. /*
  9326. * Note: "delegate 0" means the surrounding block, not the
  9327. * try-delegate block itself.
  9328. *
  9329. * Note: the caller hasn't popped the try-delegate frame yet.
  9330. */
  9331. bh_assert(loader_ctx->csp_num > 0);
  9332. if (loader_ctx->csp_num - 1 <= depth) {
  9333. #if WASM_ENABLE_SPEC_TEST == 0
  9334. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9335. #else
  9336. set_error_buf(error_buf, error_buf_size, "unknown label");
  9337. #endif
  9338. goto fail;
  9339. }
  9340. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9341. #if WASM_ENABLE_FAST_INTERP != 0
  9342. emit_br_info(frame_csp_tmp, false);
  9343. #endif
  9344. *p_buf = p;
  9345. return frame_csp_tmp;
  9346. fail:
  9347. return NULL;
  9348. }
  9349. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9350. static bool
  9351. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9352. char *error_buf, uint32 error_buf_size)
  9353. {
  9354. BlockType *block_type = &block->block_type;
  9355. uint8 *return_types = NULL;
  9356. uint32 return_count = 0;
  9357. int32 available_stack_cell, return_cell_num, i;
  9358. uint8 *frame_ref = NULL;
  9359. #if WASM_ENABLE_GC != 0
  9360. WASMRefTypeMap *frame_reftype_map;
  9361. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9362. WASMRefType *ref_type;
  9363. uint32 param_count, return_reftype_map_count = 0;
  9364. int32 available_reftype_map =
  9365. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9366. #endif
  9367. available_stack_cell =
  9368. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9369. #if WASM_ENABLE_GC == 0
  9370. return_count = block_type_get_result_types(block_type, &return_types);
  9371. #else
  9372. return_count = block_type_get_result_types(block_type, &return_types,
  9373. &return_reftype_maps,
  9374. &return_reftype_map_count);
  9375. param_count =
  9376. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9377. (void)param_count;
  9378. #endif
  9379. return_cell_num =
  9380. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9381. /* If the stack is in polymorphic state, just clear the stack
  9382. * and then re-push the values to make the stack top values
  9383. * match block type. */
  9384. if (block->is_stack_polymorphic) {
  9385. #if WASM_ENABLE_GC != 0
  9386. int32 j = return_reftype_map_count - 1;
  9387. #endif
  9388. for (i = (int32)return_count - 1; i >= 0; i--) {
  9389. #if WASM_ENABLE_GC != 0
  9390. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9391. bh_assert(return_reftype_maps[j].index == i + param_count);
  9392. bh_memcpy_s(
  9393. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9394. return_reftype_maps[j].ref_type,
  9395. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9396. j--;
  9397. }
  9398. #endif
  9399. #if WASM_ENABLE_FAST_INTERP != 0
  9400. POP_OFFSET_TYPE(return_types[i]);
  9401. #endif
  9402. POP_TYPE(return_types[i]);
  9403. }
  9404. /* Check stack is empty */
  9405. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9406. set_error_buf(
  9407. error_buf, error_buf_size,
  9408. "type mismatch: stack size does not match block type");
  9409. goto fail;
  9410. }
  9411. #if WASM_ENABLE_GC != 0
  9412. j = 0;
  9413. #endif
  9414. for (i = 0; i < (int32)return_count; i++) {
  9415. #if WASM_ENABLE_GC != 0
  9416. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9417. bh_assert(return_reftype_maps[j].index == i + param_count);
  9418. bh_memcpy_s(
  9419. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9420. return_reftype_maps[j].ref_type,
  9421. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9422. j++;
  9423. }
  9424. #endif
  9425. #if WASM_ENABLE_FAST_INTERP != 0
  9426. bool disable_emit = true;
  9427. int16 operand_offset = 0;
  9428. PUSH_OFFSET_TYPE(return_types[i]);
  9429. #endif
  9430. PUSH_TYPE(return_types[i]);
  9431. }
  9432. return true;
  9433. }
  9434. if (available_stack_cell != return_cell_num) {
  9435. #if WASM_ENABLE_EXCE_HANDLING != 0
  9436. /* testspec: this error message format is expected by try_catch.wast */
  9437. snprintf(
  9438. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9439. block->label_type == LABEL_TYPE_TRY
  9440. || (block->label_type == LABEL_TYPE_CATCH
  9441. && return_cell_num > 0)
  9442. ? "instruction"
  9443. : "block",
  9444. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9445. " but stack has ",
  9446. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9447. : "");
  9448. goto fail;
  9449. #else
  9450. set_error_buf(error_buf, error_buf_size,
  9451. "type mismatch: stack size does not match block type");
  9452. goto fail;
  9453. #endif
  9454. }
  9455. /* Check stack values match return types */
  9456. frame_ref = loader_ctx->frame_ref;
  9457. #if WASM_ENABLE_GC != 0
  9458. frame_reftype_map = loader_ctx->frame_reftype_map;
  9459. return_reftype_map =
  9460. return_reftype_map_count
  9461. ? return_reftype_maps + return_reftype_map_count - 1
  9462. : NULL;
  9463. #endif
  9464. for (i = (int32)return_count - 1; i >= 0; i--) {
  9465. uint8 type = return_types[i];
  9466. #if WASM_ENABLE_GC != 0
  9467. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9468. ref_type = NULL;
  9469. if (is_type_multi_byte) {
  9470. bh_assert(return_reftype_map);
  9471. ref_type = return_reftype_map->ref_type;
  9472. }
  9473. #endif
  9474. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9475. #if WASM_ENABLE_GC != 0
  9476. frame_reftype_map, available_reftype_map,
  9477. #endif
  9478. type,
  9479. #if WASM_ENABLE_GC != 0
  9480. ref_type,
  9481. #endif
  9482. error_buf, error_buf_size))
  9483. return false;
  9484. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9485. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9486. #if WASM_ENABLE_GC != 0
  9487. if (is_type_multi_byte) {
  9488. frame_reftype_map--;
  9489. available_reftype_map--;
  9490. return_reftype_map--;
  9491. }
  9492. #endif
  9493. }
  9494. return true;
  9495. fail:
  9496. return false;
  9497. }
  9498. #if WASM_ENABLE_FAST_INTERP != 0
  9499. /* Copy parameters to dynamic space.
  9500. * 1) POP original parameter out;
  9501. * 2) Push and copy original values to dynamic space.
  9502. * The copy instruction format:
  9503. * Part a: param count
  9504. * Part b: all param total cell num
  9505. * Part c: each param's cell_num, src offset and dst offset
  9506. * Part d: each param's src offset
  9507. * Part e: each param's dst offset
  9508. */
  9509. static bool
  9510. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9511. uint32 error_buf_size)
  9512. {
  9513. bool ret = false;
  9514. int16 *frame_offset = NULL;
  9515. uint8 *cells = NULL, cell;
  9516. int16 *src_offsets = NULL;
  9517. uint8 *emit_data = NULL;
  9518. uint32 i;
  9519. BranchBlock *block = loader_ctx->frame_csp - 1;
  9520. BlockType *block_type = &block->block_type;
  9521. WASMFuncType *wasm_type = block_type->u.type;
  9522. uint32 param_count = block_type->u.type->param_count;
  9523. int16 condition_offset = 0;
  9524. bool disable_emit = false;
  9525. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9526. int16 operand_offset = 0;
  9527. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9528. bh_assert(size > 0);
  9529. /* For if block, we also need copy the condition operand offset. */
  9530. if (is_if_block)
  9531. size += sizeof(*cells) + sizeof(*src_offsets);
  9532. /* Allocate memory for the emit data */
  9533. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9534. return false;
  9535. cells = emit_data;
  9536. src_offsets = (int16 *)(cells + param_count);
  9537. if (is_if_block)
  9538. condition_offset = *loader_ctx->frame_offset;
  9539. /* POP original parameter out */
  9540. for (i = 0; i < param_count; i++) {
  9541. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9542. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9543. }
  9544. frame_offset = loader_ctx->frame_offset;
  9545. /* Get each param's cell num and src offset */
  9546. for (i = 0; i < param_count; i++) {
  9547. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9548. cells[i] = cell;
  9549. src_offsets[i] = *frame_offset;
  9550. frame_offset += cell;
  9551. }
  9552. /* emit copy instruction */
  9553. emit_label(EXT_OP_COPY_STACK_VALUES);
  9554. /* Part a) */
  9555. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9556. /* Part b) */
  9557. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9558. : wasm_type->param_cell_num);
  9559. /* Part c) */
  9560. for (i = 0; i < param_count; i++)
  9561. emit_byte(loader_ctx, cells[i]);
  9562. if (is_if_block)
  9563. emit_byte(loader_ctx, 1);
  9564. /* Part d) */
  9565. for (i = 0; i < param_count; i++)
  9566. emit_operand(loader_ctx, src_offsets[i]);
  9567. if (is_if_block)
  9568. emit_operand(loader_ctx, condition_offset);
  9569. /* Since the start offset to save the block's params and
  9570. * the start offset to save the block's results may be
  9571. * different, we remember the dynamic offset for loop block
  9572. * so that we can use it to copy the stack operands to the
  9573. * loop block's params in wasm_loader_emit_br_info. */
  9574. if (block->label_type == LABEL_TYPE_LOOP)
  9575. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9576. /* Part e) */
  9577. /* Push to dynamic space. The push will emit the dst offset. */
  9578. for (i = 0; i < param_count; i++)
  9579. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9580. if (is_if_block)
  9581. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9582. ret = true;
  9583. fail:
  9584. /* Free the emit data */
  9585. wasm_runtime_free(emit_data);
  9586. return ret;
  9587. }
  9588. #endif
  9589. #if WASM_ENABLE_GC == 0
  9590. #define RESET_REFTYPE_MAP_STACK() (void)0
  9591. #else
  9592. #define RESET_REFTYPE_MAP_STACK() \
  9593. do { \
  9594. loader_ctx->reftype_map_num = \
  9595. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9596. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9597. + loader_ctx->reftype_map_num; \
  9598. } while (0)
  9599. #endif
  9600. /* reset the stack to the state of before entering the last block */
  9601. #if WASM_ENABLE_FAST_INTERP != 0
  9602. #define RESET_STACK() \
  9603. do { \
  9604. loader_ctx->stack_cell_num = \
  9605. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9606. loader_ctx->frame_ref = \
  9607. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9608. loader_ctx->frame_offset = \
  9609. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9610. RESET_REFTYPE_MAP_STACK(); \
  9611. } while (0)
  9612. #else
  9613. #define RESET_STACK() \
  9614. do { \
  9615. loader_ctx->stack_cell_num = \
  9616. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9617. loader_ctx->frame_ref = \
  9618. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9619. RESET_REFTYPE_MAP_STACK(); \
  9620. } while (0)
  9621. #endif
  9622. /* set current block's stack polymorphic state */
  9623. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9624. do { \
  9625. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9626. _cur_block->is_stack_polymorphic = flag; \
  9627. } while (0)
  9628. #define BLOCK_HAS_PARAM(block_type) \
  9629. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9630. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9631. do { \
  9632. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9633. error_buf_size))) { \
  9634. goto fail; \
  9635. } \
  9636. } while (0)
  9637. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9638. static bool
  9639. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9640. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9641. uint32 error_buf_size)
  9642. {
  9643. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9644. return false;
  9645. }
  9646. if (table_idx < module->import_table_count) {
  9647. if (p_elem_type)
  9648. *p_elem_type =
  9649. module->import_tables[table_idx].u.table.table_type.elem_type;
  9650. #if WASM_ENABLE_GC != 0
  9651. if (p_ref_type)
  9652. *((WASMRefType **)p_ref_type) =
  9653. module->import_tables[table_idx]
  9654. .u.table.table_type.elem_ref_type;
  9655. #endif
  9656. }
  9657. else {
  9658. if (p_elem_type)
  9659. *p_elem_type =
  9660. module->tables[module->import_table_count + table_idx]
  9661. .table_type.elem_type;
  9662. #if WASM_ENABLE_GC != 0
  9663. if (p_ref_type)
  9664. *((WASMRefType **)p_ref_type) =
  9665. module->tables[module->import_table_count + table_idx]
  9666. .table_type.elem_ref_type;
  9667. #endif
  9668. }
  9669. return true;
  9670. }
  9671. static bool
  9672. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9673. uint8 *p_elem_type, void **p_elem_ref_type,
  9674. char *error_buf, uint32 error_buf_size)
  9675. {
  9676. if (table_seg_idx >= module->table_seg_count) {
  9677. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9678. table_seg_idx);
  9679. return false;
  9680. }
  9681. if (p_elem_type) {
  9682. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9683. }
  9684. #if WASM_ENABLE_GC != 0
  9685. if (p_elem_ref_type)
  9686. *((WASMRefType **)p_elem_ref_type) =
  9687. module->table_segments[table_seg_idx].elem_ref_type;
  9688. #endif
  9689. return true;
  9690. }
  9691. #endif
  9692. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9693. const uint8 *
  9694. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9695. uint32 *len)
  9696. {
  9697. WASMCustomSection *section = module->custom_section_list;
  9698. while (section) {
  9699. if ((section->name_len == strlen(name))
  9700. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9701. if (len) {
  9702. *len = section->content_len;
  9703. }
  9704. return section->content_addr;
  9705. }
  9706. section = section->next;
  9707. }
  9708. return NULL;
  9709. }
  9710. #endif
  9711. #if 0
  9712. #define HANDLE_OPCODE(opcode) #opcode
  9713. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9714. #undef HANDLE_OPCODE
  9715. #endif
  9716. static bool
  9717. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9718. uint32 cur_func_idx, char *error_buf,
  9719. uint32 error_buf_size)
  9720. {
  9721. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9722. uint32 param_count, local_count, global_count;
  9723. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9724. BlockType func_block_type;
  9725. uint16 *local_offsets, local_offset;
  9726. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9727. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9728. mem_offset_t mem_offset;
  9729. int32 i32_const = 0;
  9730. int64 i64_const;
  9731. uint8 opcode;
  9732. bool return_value = false;
  9733. WASMLoaderContext *loader_ctx;
  9734. BranchBlock *frame_csp_tmp;
  9735. #if WASM_ENABLE_GC != 0
  9736. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9737. uint32 param_reftype_map_count, local_reftype_map_count;
  9738. int32 heap_type;
  9739. WASMRefType wasm_ref_type = { 0 };
  9740. bool need_ref_type_map;
  9741. #endif
  9742. #if WASM_ENABLE_FAST_INTERP != 0
  9743. uint8 *func_const_end, *func_const = NULL;
  9744. int16 operand_offset = 0;
  9745. uint8 last_op = 0;
  9746. bool disable_emit, preserve_local = false, if_condition_available = true;
  9747. float32 f32_const;
  9748. float64 f64_const;
  9749. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9750. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9751. #endif
  9752. #if WASM_ENABLE_MEMORY64 != 0
  9753. bool is_memory64 = has_module_memory64(module);
  9754. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9755. #else
  9756. mem_offset_type = VALUE_TYPE_I32;
  9757. #endif
  9758. global_count = module->import_global_count + module->global_count;
  9759. param_count = func->func_type->param_count;
  9760. param_types = func->func_type->types;
  9761. func_block_type.is_value_type = false;
  9762. func_block_type.u.type = func->func_type;
  9763. local_count = func->local_count;
  9764. local_types = func->local_types;
  9765. local_offsets = func->local_offsets;
  9766. #if WASM_ENABLE_GC != 0
  9767. param_reftype_maps = func->func_type->ref_type_maps;
  9768. param_reftype_map_count = func->func_type->ref_type_map_count;
  9769. local_reftype_maps = func->local_ref_type_maps;
  9770. local_reftype_map_count = func->local_ref_type_map_count;
  9771. #endif
  9772. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9773. goto fail;
  9774. }
  9775. #if WASM_ENABLE_GC != 0
  9776. loader_ctx->module = module;
  9777. loader_ctx->ref_type_set = module->ref_type_set;
  9778. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9779. #endif
  9780. #if WASM_ENABLE_FAST_INTERP != 0
  9781. /* For the first traverse, the initial value of preserved_local_offset has
  9782. * not been determined, we use the INT16_MAX to represent that a slot has
  9783. * been copied to preserve space. For second traverse, this field will be
  9784. * set to the appropriate value in wasm_loader_ctx_reinit.
  9785. * This is for Issue #1230,
  9786. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9787. * drop opcodes need to know which slots are preserved, so those slots will
  9788. * not be treated as dynamically allocated slots */
  9789. loader_ctx->preserved_local_offset = INT16_MAX;
  9790. re_scan:
  9791. if (loader_ctx->code_compiled_size > 0) {
  9792. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9793. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9794. goto fail;
  9795. }
  9796. p = func->code;
  9797. func->code_compiled = loader_ctx->p_code_compiled;
  9798. func->code_compiled_size = loader_ctx->code_compiled_size;
  9799. }
  9800. #endif
  9801. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9802. while (p < p_end) {
  9803. opcode = *p++;
  9804. #if WASM_ENABLE_FAST_INTERP != 0
  9805. p_org = p;
  9806. disable_emit = false;
  9807. emit_label(opcode);
  9808. #endif
  9809. switch (opcode) {
  9810. case WASM_OP_UNREACHABLE:
  9811. RESET_STACK();
  9812. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9813. break;
  9814. case WASM_OP_NOP:
  9815. #if WASM_ENABLE_FAST_INTERP != 0
  9816. skip_label();
  9817. #endif
  9818. break;
  9819. case WASM_OP_IF:
  9820. {
  9821. #if WASM_ENABLE_FAST_INTERP != 0
  9822. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9823. int32 available_stack_cell =
  9824. (int32)(loader_ctx->stack_cell_num
  9825. - parent_block->stack_cell_num);
  9826. if (available_stack_cell <= 0
  9827. && parent_block->is_stack_polymorphic)
  9828. if_condition_available = false;
  9829. else
  9830. if_condition_available = true;
  9831. PRESERVE_LOCAL_FOR_BLOCK();
  9832. #endif
  9833. #if WASM_ENABLE_GC == 0
  9834. POP_I32();
  9835. #endif
  9836. goto handle_op_block_and_loop;
  9837. }
  9838. case WASM_OP_BLOCK:
  9839. case WASM_OP_LOOP:
  9840. #if WASM_ENABLE_EXCE_HANDLING != 0
  9841. case WASM_OP_TRY:
  9842. if (opcode == WASM_OP_TRY) {
  9843. /*
  9844. * keep track of exception handlers to account for
  9845. * memory allocation
  9846. */
  9847. func->exception_handler_count++;
  9848. /*
  9849. * try is a block
  9850. * do nothing special, but execution continues to
  9851. * to handle_op_block_and_loop,
  9852. * and that be pushes the csp
  9853. */
  9854. }
  9855. #endif
  9856. #if WASM_ENABLE_FAST_INTERP != 0
  9857. PRESERVE_LOCAL_FOR_BLOCK();
  9858. #endif
  9859. handle_op_block_and_loop:
  9860. {
  9861. uint8 value_type;
  9862. BlockType block_type;
  9863. #if WASM_ENABLE_FAST_INTERP != 0
  9864. uint32 available_params = 0;
  9865. #endif
  9866. CHECK_BUF(p, p_end, 1);
  9867. value_type = read_uint8(p);
  9868. if (is_byte_a_type(value_type)) {
  9869. /* If the first byte is one of these special values:
  9870. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9871. * the single return value. */
  9872. block_type.is_value_type = true;
  9873. block_type.u.value_type.type = value_type;
  9874. #if WASM_ENABLE_WAMR_COMPILER != 0
  9875. if (value_type == VALUE_TYPE_V128)
  9876. module->is_simd_used = true;
  9877. else if (value_type == VALUE_TYPE_FUNCREF
  9878. || value_type == VALUE_TYPE_EXTERNREF)
  9879. module->is_ref_types_used = true;
  9880. #endif
  9881. #if WASM_ENABLE_GC != 0
  9882. if (value_type != VALUE_TYPE_VOID) {
  9883. p_org = p;
  9884. p--;
  9885. if (!resolve_value_type((const uint8 **)&p, p_end,
  9886. module, module->type_count,
  9887. &need_ref_type_map,
  9888. &wasm_ref_type, false,
  9889. error_buf, error_buf_size)) {
  9890. goto fail;
  9891. }
  9892. if (need_ref_type_map) {
  9893. block_type.u.value_type.ref_type_map.index = 0;
  9894. if (!(block_type.u.value_type.ref_type_map
  9895. .ref_type = reftype_set_insert(
  9896. module->ref_type_set, &wasm_ref_type,
  9897. error_buf, error_buf_size))) {
  9898. goto fail;
  9899. }
  9900. }
  9901. /* Set again as the type might be changed, e.g.
  9902. (ref null any) to anyref */
  9903. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9904. #if WASM_ENABLE_FAST_INTERP == 0
  9905. while (p_org < p) {
  9906. #if WASM_ENABLE_DEBUG_INTERP != 0
  9907. if (!record_fast_op(module, p_org, *p_org,
  9908. error_buf, error_buf_size)) {
  9909. goto fail;
  9910. }
  9911. #endif
  9912. /* Ignore extra bytes for interpreter */
  9913. *p_org++ = WASM_OP_NOP;
  9914. }
  9915. #endif
  9916. }
  9917. #endif /* end of WASM_ENABLE_GC != 0 */
  9918. }
  9919. else {
  9920. int32 type_index;
  9921. /* Resolve the leb128 encoded type index as block type */
  9922. p--;
  9923. p_org = p - 1;
  9924. read_leb_int32(p, p_end, type_index);
  9925. if ((uint32)type_index >= module->type_count) {
  9926. set_error_buf(error_buf, error_buf_size,
  9927. "unknown type");
  9928. goto fail;
  9929. }
  9930. block_type.is_value_type = false;
  9931. block_type.u.type =
  9932. (WASMFuncType *)module->types[type_index];
  9933. #if WASM_ENABLE_FAST_INTERP == 0
  9934. /* If block use type index as block type, change the opcode
  9935. * to new extended opcode so that interpreter can resolve
  9936. * the block quickly.
  9937. */
  9938. #if WASM_ENABLE_DEBUG_INTERP != 0
  9939. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9940. error_buf_size)) {
  9941. goto fail;
  9942. }
  9943. #endif
  9944. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9945. #endif
  9946. }
  9947. #if WASM_ENABLE_GC != 0
  9948. if (opcode == WASM_OP_IF) {
  9949. POP_I32();
  9950. }
  9951. #endif
  9952. /* Pop block parameters from stack */
  9953. if (BLOCK_HAS_PARAM(block_type)) {
  9954. WASMFuncType *wasm_type = block_type.u.type;
  9955. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9956. #if WASM_ENABLE_FAST_INTERP != 0
  9957. uint32 cell_num;
  9958. available_params = block_type.u.type->param_count;
  9959. #endif
  9960. for (i = 0; i < block_type.u.type->param_count; i++) {
  9961. int32 available_stack_cell =
  9962. (int32)(loader_ctx->stack_cell_num
  9963. - cur_block->stack_cell_num);
  9964. if (available_stack_cell <= 0
  9965. && cur_block->is_stack_polymorphic) {
  9966. #if WASM_ENABLE_FAST_INTERP != 0
  9967. available_params = i;
  9968. #endif
  9969. break;
  9970. }
  9971. POP_TYPE(
  9972. wasm_type->types[wasm_type->param_count - i - 1]);
  9973. #if WASM_ENABLE_FAST_INTERP != 0
  9974. /* decrease the frame_offset pointer accordingly to keep
  9975. * consistent with frame_ref stack */
  9976. cell_num = wasm_value_type_cell_num(
  9977. wasm_type->types[wasm_type->param_count - i - 1]);
  9978. loader_ctx->frame_offset -= cell_num;
  9979. #endif
  9980. }
  9981. }
  9982. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9983. block_type, p);
  9984. /* Pass parameters to block */
  9985. if (BLOCK_HAS_PARAM(block_type)) {
  9986. WASMFuncType *func_type = block_type.u.type;
  9987. #if WASM_ENABLE_GC != 0
  9988. WASMRefType *ref_type;
  9989. uint32 j = 0;
  9990. #endif
  9991. for (i = 0; i < func_type->param_count; i++) {
  9992. #if WASM_ENABLE_FAST_INTERP != 0
  9993. uint32 cell_num =
  9994. wasm_value_type_cell_num(func_type->types[i]);
  9995. if (i >= available_params) {
  9996. /* If there isn't enough data on stack, push a dummy
  9997. * offset to keep the stack consistent with
  9998. * frame_ref.
  9999. * Since the stack is already in polymorphic state,
  10000. * the opcode will not be executed, so the dummy
  10001. * offset won't cause any error */
  10002. uint32 n;
  10003. for (n = 0; n < cell_num; n++) {
  10004. if (loader_ctx->p_code_compiled == NULL) {
  10005. if (!check_offset_push(loader_ctx,
  10006. error_buf,
  10007. error_buf_size))
  10008. goto fail;
  10009. }
  10010. *loader_ctx->frame_offset++ = 0;
  10011. }
  10012. }
  10013. else {
  10014. loader_ctx->frame_offset += cell_num;
  10015. }
  10016. #endif
  10017. #if WASM_ENABLE_GC != 0
  10018. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10019. bh_assert(func_type->ref_type_maps[j].index == i);
  10020. ref_type = func_type->ref_type_maps[j].ref_type;
  10021. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10022. ref_type,
  10023. wasm_reftype_struct_size(ref_type));
  10024. j++;
  10025. }
  10026. #endif
  10027. PUSH_TYPE(func_type->types[i]);
  10028. }
  10029. }
  10030. #if WASM_ENABLE_FAST_INTERP != 0
  10031. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10032. skip_label();
  10033. if (BLOCK_HAS_PARAM(block_type)) {
  10034. /* Make sure params are in dynamic space */
  10035. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10036. error_buf_size))
  10037. goto fail;
  10038. }
  10039. if (opcode == WASM_OP_LOOP) {
  10040. (loader_ctx->frame_csp - 1)->code_compiled =
  10041. loader_ctx->p_code_compiled;
  10042. }
  10043. }
  10044. #if WASM_ENABLE_EXCE_HANDLING != 0
  10045. else if (opcode == WASM_OP_TRY) {
  10046. skip_label();
  10047. }
  10048. #endif
  10049. else if (opcode == WASM_OP_IF) {
  10050. BranchBlock *block = loader_ctx->frame_csp - 1;
  10051. /* If block has parameters, we should make sure they are in
  10052. * dynamic space. Otherwise, when else branch is missing,
  10053. * the later opcode may consume incorrect operand offset.
  10054. * Spec case:
  10055. * (func (export "params-id") (param i32) (result i32)
  10056. * (i32.const 1)
  10057. * (i32.const 2)
  10058. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10059. * (then)) (i32.add)
  10060. * )
  10061. *
  10062. * So we should emit a copy instruction before the if.
  10063. *
  10064. * And we also need to save the parameter offsets and
  10065. * recover them before entering else branch.
  10066. *
  10067. */
  10068. if (BLOCK_HAS_PARAM(block_type)) {
  10069. uint64 size;
  10070. /* In polymorphic state, there may be no if condition on
  10071. * the stack, so the offset may not emitted */
  10072. if (if_condition_available) {
  10073. /* skip the if condition operand offset */
  10074. wasm_loader_emit_backspace(loader_ctx,
  10075. sizeof(int16));
  10076. }
  10077. /* skip the if label */
  10078. skip_label();
  10079. /* Emit a copy instruction */
  10080. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10081. error_buf_size))
  10082. goto fail;
  10083. /* Emit the if instruction */
  10084. emit_label(opcode);
  10085. /* Emit the new condition operand offset */
  10086. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10087. /* Save top param_count values of frame_offset stack, so
  10088. * that we can recover it before executing else branch
  10089. */
  10090. size = sizeof(int16)
  10091. * (uint64)block_type.u.type->param_cell_num;
  10092. if (!(block->param_frame_offsets = loader_malloc(
  10093. size, error_buf, error_buf_size)))
  10094. goto fail;
  10095. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10096. loader_ctx->frame_offset
  10097. - size / sizeof(int16),
  10098. (uint32)size);
  10099. }
  10100. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10101. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10102. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10103. }
  10104. #endif
  10105. break;
  10106. }
  10107. #if WASM_ENABLE_EXCE_HANDLING != 0
  10108. case WASM_OP_THROW:
  10109. {
  10110. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10111. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10112. uint8 label_type = cur_block->label_type;
  10113. uint32 tag_index = 0;
  10114. read_leb_int32(p, p_end, tag_index);
  10115. /* check validity of tag_index against module->tag_count */
  10116. /* check tag index is within the tag index space */
  10117. if (tag_index >= module->import_tag_count + module->tag_count) {
  10118. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10119. tag_index);
  10120. goto fail;
  10121. }
  10122. /* the tag_type is stored in either the WASMTag (section tags)
  10123. * or WASMTagImport (import tag) */
  10124. WASMFuncType *tag_type = NULL;
  10125. if (tag_index < module->import_tag_count) {
  10126. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10127. }
  10128. else {
  10129. tag_type =
  10130. module->tags[tag_index - module->import_tag_count]
  10131. ->tag_type;
  10132. }
  10133. if (tag_type->result_count != 0) {
  10134. set_error_buf(error_buf, error_buf_size,
  10135. "tag type signature does not return void");
  10136. goto fail;
  10137. }
  10138. int32 available_stack_cell =
  10139. (int32)(loader_ctx->stack_cell_num
  10140. - cur_block->stack_cell_num);
  10141. int32 tti;
  10142. /* Check stack values match return types by comparing tag param
  10143. * types with stack cells */
  10144. uint8 *frame_ref = loader_ctx->frame_ref;
  10145. #if WASM_ENABLE_GC != 0
  10146. WASMRefTypeMap *frame_reftype_map =
  10147. loader_ctx->frame_reftype_map;
  10148. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10149. /* Temporarily set these values since they may be used in
  10150. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10151. param_reftype_maps = tag_type->ref_type_maps;
  10152. /* For tag_type function, it shouldn't have result_count = 0 */
  10153. param_reftype_map_count = tag_type->ref_type_map_count;
  10154. param_count = tag_type->param_count;
  10155. #endif
  10156. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10157. #if WASM_ENABLE_GC != 0
  10158. local_type = tag_type->types[tti];
  10159. local_idx = tti;
  10160. /* Get the wasm_ref_type if the local_type is multibyte
  10161. type */
  10162. GET_LOCAL_REFTYPE();
  10163. #endif
  10164. if (!check_stack_top_values(
  10165. loader_ctx, frame_ref, available_stack_cell,
  10166. #if WASM_ENABLE_GC != 0
  10167. frame_reftype_map, frame_reftype_map_num,
  10168. #endif
  10169. tag_type->types[tti],
  10170. #if WASM_ENABLE_GC != 0
  10171. &wasm_ref_type,
  10172. #endif
  10173. error_buf, error_buf_size)) {
  10174. snprintf(error_buf, error_buf_size,
  10175. "type mismatch: instruction requires [%s] but "
  10176. "stack has [%s]",
  10177. tag_type->param_count > 0
  10178. ? type2str(tag_type->types[tti])
  10179. : "",
  10180. available_stack_cell > 0
  10181. ? type2str(*(loader_ctx->frame_ref - 1))
  10182. : "");
  10183. goto fail;
  10184. }
  10185. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10186. available_stack_cell -=
  10187. wasm_value_type_cell_num(tag_type->types[tti]);
  10188. }
  10189. #if WASM_ENABLE_GC != 0
  10190. /* Restore the values */
  10191. param_reftype_maps = func->func_type->ref_type_maps;
  10192. param_reftype_map_count = func->func_type->ref_type_map_count;
  10193. param_count = func->func_type->param_count;
  10194. #endif
  10195. /* throw is stack polymorphic */
  10196. (void)label_type;
  10197. RESET_STACK();
  10198. break;
  10199. }
  10200. case WASM_OP_RETHROW:
  10201. {
  10202. /* must be done before checking branch block */
  10203. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10204. /* check the target catching block: LABEL_TYPE_CATCH */
  10205. if (!(frame_csp_tmp =
  10206. check_branch_block(loader_ctx, &p, p_end, opcode,
  10207. error_buf, error_buf_size)))
  10208. goto fail;
  10209. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10210. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10211. /* trap according to spectest (rethrow.wast) */
  10212. set_error_buf(error_buf, error_buf_size,
  10213. "invalid rethrow label");
  10214. goto fail;
  10215. }
  10216. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10217. uint8 label_type = cur_block->label_type;
  10218. (void)label_type;
  10219. /* rethrow is stack polymorphic */
  10220. RESET_STACK();
  10221. break;
  10222. }
  10223. case WASM_OP_DELEGATE:
  10224. {
  10225. /* check target block is valid */
  10226. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10227. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10228. goto fail;
  10229. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10230. uint8 label_type = cur_block->label_type;
  10231. (void)label_type;
  10232. /* DELEGATE ends the block */
  10233. POP_CSP();
  10234. break;
  10235. }
  10236. case WASM_OP_CATCH:
  10237. {
  10238. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10239. uint8 label_type = cur_block->label_type;
  10240. uint32 tag_index = 0;
  10241. read_leb_int32(p, p_end, tag_index);
  10242. /* check validity of tag_index against module->tag_count */
  10243. /* check tag index is within the tag index space */
  10244. if (tag_index >= module->import_tag_count + module->tag_count) {
  10245. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10246. __FUNCTION__);
  10247. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10248. goto fail;
  10249. }
  10250. /* the tag_type is stored in either the WASMTag (section tags)
  10251. * or WASMTagImport (import tag) */
  10252. WASMFuncType *func_type = NULL;
  10253. if (tag_index < module->import_tag_count) {
  10254. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10255. }
  10256. else {
  10257. func_type =
  10258. module->tags[tag_index - module->import_tag_count]
  10259. ->tag_type;
  10260. }
  10261. if (func_type->result_count != 0) {
  10262. set_error_buf(error_buf, error_buf_size,
  10263. "tag type signature does not return void");
  10264. goto fail;
  10265. }
  10266. /* check validity of current label (expect LABEL_TYPE_TRY or
  10267. * LABEL_TYPE_CATCH) */
  10268. if ((LABEL_TYPE_CATCH != label_type)
  10269. && (LABEL_TYPE_TRY != label_type)) {
  10270. set_error_buf(error_buf, error_buf_size,
  10271. "Unexpected block sequence encountered.");
  10272. goto fail;
  10273. }
  10274. /*
  10275. * replace frame_csp by LABEL_TYPE_CATCH
  10276. */
  10277. cur_block->label_type = LABEL_TYPE_CATCH;
  10278. /* RESET_STACK removes the values pushed in TRY or pervious
  10279. * CATCH Blocks */
  10280. RESET_STACK();
  10281. #if WASM_ENABLE_GC != 0
  10282. WASMRefType *ref_type;
  10283. uint32 j = 0;
  10284. #endif
  10285. /* push types on the stack according to caught type */
  10286. for (i = 0; i < func_type->param_count; i++) {
  10287. #if WASM_ENABLE_GC != 0
  10288. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10289. bh_assert(func_type->ref_type_maps[j].index == i);
  10290. ref_type = func_type->ref_type_maps[j].ref_type;
  10291. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10292. ref_type,
  10293. wasm_reftype_struct_size(ref_type));
  10294. j++;
  10295. }
  10296. #endif
  10297. PUSH_TYPE(func_type->types[i]);
  10298. }
  10299. break;
  10300. }
  10301. case WASM_OP_CATCH_ALL:
  10302. {
  10303. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10304. /* expecting a TRY or CATCH, anything else will be considered an
  10305. * error */
  10306. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10307. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10308. set_error_buf(error_buf, error_buf_size,
  10309. "Unexpected block sequence encountered.");
  10310. goto fail;
  10311. }
  10312. /* no immediates */
  10313. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10314. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10315. /* RESET_STACK removes the values pushed in TRY or pervious
  10316. * CATCH Blocks */
  10317. RESET_STACK();
  10318. /* catch_all has no tagtype and therefore no parameters */
  10319. break;
  10320. }
  10321. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10322. case WASM_OP_ELSE:
  10323. handle_op_else:
  10324. {
  10325. BranchBlock *block = NULL;
  10326. BlockType block_type;
  10327. if (loader_ctx->csp_num < 2
  10328. /* the matched if isn't found */
  10329. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10330. /* duplicated else is found */
  10331. || (loader_ctx->frame_csp - 1)->else_addr) {
  10332. set_error_buf(
  10333. error_buf, error_buf_size,
  10334. "opcode else found without matched opcode if");
  10335. goto fail;
  10336. }
  10337. block = loader_ctx->frame_csp - 1;
  10338. /* check whether if branch's stack matches its result type */
  10339. if (!check_block_stack(loader_ctx, block, error_buf,
  10340. error_buf_size))
  10341. goto fail;
  10342. block->else_addr = p - 1;
  10343. block_type = block->block_type;
  10344. #if WASM_ENABLE_GC != 0
  10345. if (!wasm_loader_init_local_use_masks(
  10346. loader_ctx, local_count, error_buf, error_buf_size)) {
  10347. goto fail;
  10348. }
  10349. #endif
  10350. #if WASM_ENABLE_FAST_INTERP != 0
  10351. /* if the result of if branch is in local or const area, add a
  10352. * copy op */
  10353. RESERVE_BLOCK_RET();
  10354. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10355. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10356. #endif
  10357. RESET_STACK();
  10358. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10359. /* Pass parameters to if-false branch */
  10360. if (BLOCK_HAS_PARAM(block_type)) {
  10361. for (i = 0; i < block_type.u.type->param_count; i++)
  10362. PUSH_TYPE(block_type.u.type->types[i]);
  10363. }
  10364. #if WASM_ENABLE_FAST_INTERP != 0
  10365. /* Recover top param_count values of frame_offset stack */
  10366. if (BLOCK_HAS_PARAM((block_type))) {
  10367. uint32 size;
  10368. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10369. bh_memcpy_s(loader_ctx->frame_offset, size,
  10370. block->param_frame_offsets, size);
  10371. loader_ctx->frame_offset += (size / sizeof(int16));
  10372. }
  10373. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10374. #endif
  10375. break;
  10376. }
  10377. case WASM_OP_END:
  10378. {
  10379. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10380. /* check whether block stack matches its result type */
  10381. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10382. error_buf_size))
  10383. goto fail;
  10384. /* if there is no else branch, make a virtual else opcode for
  10385. easier integrity check and to copy the correct results to
  10386. the block return address for fast-interp mode:
  10387. change if block from `if ... end` to `if ... else end` */
  10388. if (cur_block->label_type == LABEL_TYPE_IF
  10389. && !cur_block->else_addr) {
  10390. opcode = WASM_OP_ELSE;
  10391. p--;
  10392. #if WASM_ENABLE_FAST_INTERP != 0
  10393. p_org = p;
  10394. skip_label();
  10395. disable_emit = false;
  10396. emit_label(opcode);
  10397. #endif
  10398. goto handle_op_else;
  10399. }
  10400. POP_CSP();
  10401. #if WASM_ENABLE_FAST_INTERP != 0
  10402. skip_label();
  10403. /* copy the result to the block return address */
  10404. RESERVE_BLOCK_RET();
  10405. apply_label_patch(loader_ctx, 0, PATCH_END);
  10406. free_label_patch_list(loader_ctx->frame_csp);
  10407. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10408. int32 idx;
  10409. uint8 ret_type;
  10410. emit_label(WASM_OP_RETURN);
  10411. for (idx = (int32)func->func_type->result_count - 1;
  10412. idx >= 0; idx--) {
  10413. ret_type = *(func->func_type->types
  10414. + func->func_type->param_count + idx);
  10415. POP_OFFSET_TYPE(ret_type);
  10416. }
  10417. }
  10418. #endif
  10419. if (loader_ctx->csp_num > 0) {
  10420. loader_ctx->frame_csp->end_addr = p - 1;
  10421. }
  10422. else {
  10423. /* end of function block, function will return */
  10424. if (p < p_end) {
  10425. set_error_buf(error_buf, error_buf_size,
  10426. "section size mismatch");
  10427. goto fail;
  10428. }
  10429. }
  10430. break;
  10431. }
  10432. case WASM_OP_BR:
  10433. {
  10434. if (!(frame_csp_tmp =
  10435. check_branch_block(loader_ctx, &p, p_end, opcode,
  10436. error_buf, error_buf_size)))
  10437. goto fail;
  10438. RESET_STACK();
  10439. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10440. break;
  10441. }
  10442. case WASM_OP_BR_IF:
  10443. {
  10444. POP_I32();
  10445. if (!(frame_csp_tmp =
  10446. check_branch_block(loader_ctx, &p, p_end, opcode,
  10447. error_buf, error_buf_size)))
  10448. goto fail;
  10449. break;
  10450. }
  10451. case WASM_OP_BR_TABLE:
  10452. {
  10453. uint32 depth = 0, default_arity, arity = 0;
  10454. BranchBlock *target_block;
  10455. BlockType *target_block_type;
  10456. #if WASM_ENABLE_FAST_INTERP == 0
  10457. BrTableCache *br_table_cache = NULL;
  10458. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10459. uint32 j;
  10460. #endif
  10461. read_leb_uint32(p, p_end, count);
  10462. #if WASM_ENABLE_FAST_INTERP != 0
  10463. emit_uint32(loader_ctx, count);
  10464. #endif
  10465. POP_I32();
  10466. /* Get each depth and check it */
  10467. p_org = p;
  10468. for (i = 0; i <= count; i++) {
  10469. read_leb_uint32(p, p_end, depth);
  10470. bh_assert(loader_ctx->csp_num > 0);
  10471. if (loader_ctx->csp_num - 1 < depth) {
  10472. set_error_buf(error_buf, error_buf_size,
  10473. "unknown label, "
  10474. "unexpected end of section or function");
  10475. goto fail;
  10476. }
  10477. }
  10478. p = p_org;
  10479. /* Get the default block's arity */
  10480. target_block = loader_ctx->frame_csp - (depth + 1);
  10481. target_block_type = &target_block->block_type;
  10482. default_arity = block_type_get_arity(target_block_type,
  10483. target_block->label_type);
  10484. #if WASM_ENABLE_FAST_INTERP == 0
  10485. p_depth_begin = p_depth = p;
  10486. #endif
  10487. for (i = 0; i <= count; i++) {
  10488. p_org = p;
  10489. read_leb_uint32(p, p_end, depth);
  10490. p = p_org;
  10491. /* Get the target block's arity and check it */
  10492. target_block = loader_ctx->frame_csp - (depth + 1);
  10493. target_block_type = &target_block->block_type;
  10494. arity = block_type_get_arity(target_block_type,
  10495. target_block->label_type);
  10496. if (arity != default_arity) {
  10497. set_error_buf(error_buf, error_buf_size,
  10498. "type mismatch: br_table targets must "
  10499. "all use same result type");
  10500. goto fail;
  10501. }
  10502. if (!(frame_csp_tmp =
  10503. check_branch_block(loader_ctx, &p, p_end, opcode,
  10504. error_buf, error_buf_size))) {
  10505. goto fail;
  10506. }
  10507. #if WASM_ENABLE_FAST_INTERP == 0
  10508. if (br_table_cache) {
  10509. br_table_cache->br_depths[i] = depth;
  10510. }
  10511. else {
  10512. if (depth > 255) {
  10513. /* The depth cannot be stored in one byte,
  10514. create br_table cache to store each depth */
  10515. #if WASM_ENABLE_DEBUG_INTERP != 0
  10516. if (!record_fast_op(module, p_opcode, *p_opcode,
  10517. error_buf, error_buf_size)) {
  10518. goto fail;
  10519. }
  10520. #endif
  10521. if (!(br_table_cache = loader_malloc(
  10522. offsetof(BrTableCache, br_depths)
  10523. + sizeof(uint32)
  10524. * (uint64)(count + 1),
  10525. error_buf, error_buf_size))) {
  10526. goto fail;
  10527. }
  10528. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10529. br_table_cache->br_table_op_addr = p_opcode;
  10530. br_table_cache->br_count = count;
  10531. /* Copy previous depths which are one byte */
  10532. for (j = 0; j < i; j++) {
  10533. br_table_cache->br_depths[j] = p_depth_begin[j];
  10534. }
  10535. br_table_cache->br_depths[i] = depth;
  10536. bh_list_insert(module->br_table_cache_list,
  10537. br_table_cache);
  10538. }
  10539. else {
  10540. /* The depth can be stored in one byte, use the
  10541. byte of the leb to store it */
  10542. *p_depth++ = (uint8)depth;
  10543. }
  10544. }
  10545. #endif
  10546. }
  10547. #if WASM_ENABLE_FAST_INTERP == 0
  10548. /* Set the tailing bytes to nop */
  10549. if (br_table_cache)
  10550. p_depth = p_depth_begin;
  10551. while (p_depth < p)
  10552. *p_depth++ = WASM_OP_NOP;
  10553. #endif
  10554. RESET_STACK();
  10555. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10556. break;
  10557. }
  10558. case WASM_OP_RETURN:
  10559. {
  10560. WASMFuncType *func_type = func->func_type;
  10561. int32 idx;
  10562. uint8 ret_type;
  10563. #if WASM_ENABLE_GC != 0
  10564. uint32 j = func_type->ref_type_map_count - 1;
  10565. #endif
  10566. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10567. idx--) {
  10568. ret_type =
  10569. *(func_type->types + func_type->param_count + idx);
  10570. #if WASM_ENABLE_GC != 0
  10571. if (wasm_is_type_multi_byte_type(ret_type)) {
  10572. WASMRefType *ref_type =
  10573. func_type->ref_type_maps[j].ref_type;
  10574. bh_assert(func_type->ref_type_maps[j].index
  10575. == func_type->param_count + idx);
  10576. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10577. ref_type,
  10578. wasm_reftype_struct_size(ref_type));
  10579. j--;
  10580. }
  10581. #endif
  10582. #if WASM_ENABLE_FAST_INTERP != 0
  10583. /* emit the offset after return opcode */
  10584. POP_OFFSET_TYPE(ret_type);
  10585. #endif
  10586. POP_TYPE(ret_type);
  10587. }
  10588. RESET_STACK();
  10589. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10590. break;
  10591. }
  10592. case WASM_OP_CALL:
  10593. #if WASM_ENABLE_TAIL_CALL != 0
  10594. case WASM_OP_RETURN_CALL:
  10595. #endif
  10596. #if WASM_ENABLE_GC != 0
  10597. case WASM_OP_CALL_REF:
  10598. case WASM_OP_RETURN_CALL_REF:
  10599. #endif
  10600. {
  10601. WASMFuncType *func_type;
  10602. uint8 type;
  10603. int32 idx;
  10604. #if WASM_ENABLE_GC != 0
  10605. WASMRefType *ref_type;
  10606. uint32 type_idx1;
  10607. int32 j;
  10608. #endif
  10609. #if WASM_ENABLE_GC != 0
  10610. if (opcode == WASM_OP_CALL_REF
  10611. || opcode == WASM_OP_RETURN_CALL_REF) {
  10612. read_leb_uint32(p, p_end, type_idx1);
  10613. if (!check_type_index(module, module->type_count, type_idx1,
  10614. error_buf, error_buf_size)) {
  10615. goto fail;
  10616. }
  10617. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10618. set_error_buf(error_buf, error_buf_size,
  10619. "unkown function type");
  10620. goto fail;
  10621. }
  10622. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10623. &type_idx, error_buf,
  10624. error_buf_size)) {
  10625. goto fail;
  10626. }
  10627. if (type == VALUE_TYPE_ANY) {
  10628. type_idx = type_idx1;
  10629. }
  10630. if (!check_type_index(module, module->type_count, type_idx,
  10631. error_buf, error_buf_size)) {
  10632. goto fail;
  10633. }
  10634. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10635. set_error_buf(error_buf, error_buf_size,
  10636. "unkown function type");
  10637. goto fail;
  10638. }
  10639. if (!wasm_func_type_is_super_of(
  10640. (WASMFuncType *)module->types[type_idx1],
  10641. (WASMFuncType *)module->types[type_idx])) {
  10642. set_error_buf(error_buf, error_buf_size,
  10643. "function type mismatch");
  10644. goto fail;
  10645. }
  10646. func_type = (WASMFuncType *)module->types[type_idx];
  10647. }
  10648. else
  10649. #endif
  10650. {
  10651. read_leb_uint32(p, p_end, func_idx);
  10652. #if WASM_ENABLE_FAST_INTERP != 0
  10653. /* we need to emit func_idx before arguments */
  10654. emit_uint32(loader_ctx, func_idx);
  10655. #endif
  10656. if (!check_function_index(module, func_idx, error_buf,
  10657. error_buf_size)) {
  10658. goto fail;
  10659. }
  10660. if (func_idx < module->import_function_count)
  10661. func_type = module->import_functions[func_idx]
  10662. .u.function.func_type;
  10663. else
  10664. func_type =
  10665. module
  10666. ->functions[func_idx
  10667. - module->import_function_count]
  10668. ->func_type;
  10669. }
  10670. if (func_type->param_count > 0) {
  10671. #if WASM_ENABLE_GC != 0
  10672. j = (int32)(func_type->result_ref_type_maps
  10673. - func_type->ref_type_maps - 1);
  10674. #endif
  10675. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10676. idx--) {
  10677. #if WASM_ENABLE_GC != 0
  10678. if (wasm_is_type_multi_byte_type(
  10679. func_type->types[idx])) {
  10680. ref_type = func_type->ref_type_maps[j].ref_type;
  10681. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10682. ref_type,
  10683. wasm_reftype_struct_size(ref_type));
  10684. j--;
  10685. }
  10686. #endif
  10687. #if WASM_ENABLE_FAST_INTERP != 0
  10688. POP_OFFSET_TYPE(func_type->types[idx]);
  10689. #endif
  10690. POP_TYPE(func_type->types[idx]);
  10691. }
  10692. }
  10693. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10694. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10695. #endif
  10696. #if WASM_ENABLE_GC != 0
  10697. j = (int32)(func_type->result_ref_type_maps
  10698. - func_type->ref_type_maps);
  10699. #endif
  10700. for (i = 0; i < func_type->result_count; i++) {
  10701. #if WASM_ENABLE_GC != 0
  10702. if (wasm_is_type_multi_byte_type(
  10703. func_type->types[func_type->param_count + i])) {
  10704. ref_type = func_type->ref_type_maps[j].ref_type;
  10705. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10706. ref_type,
  10707. wasm_reftype_struct_size(ref_type));
  10708. j++;
  10709. }
  10710. #endif
  10711. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10712. #if WASM_ENABLE_FAST_INTERP != 0
  10713. /* Here we emit each return value's dynamic_offset. But
  10714. * in fact these offsets are continuous, so interpreter
  10715. * only need to get the first return value's offset.
  10716. */
  10717. PUSH_OFFSET_TYPE(
  10718. func_type->types[func_type->param_count + i]);
  10719. #endif
  10720. }
  10721. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10722. }
  10723. else {
  10724. #if WASM_ENABLE_GC == 0
  10725. if (func_type->result_count
  10726. != func->func_type->result_count) {
  10727. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10728. "type mismatch: expect ",
  10729. func->func_type->result_count,
  10730. " return values but got other");
  10731. goto fail;
  10732. }
  10733. for (i = 0; i < func_type->result_count; i++) {
  10734. type = func->func_type
  10735. ->types[func->func_type->param_count + i];
  10736. if (func_type->types[func_type->param_count + i]
  10737. != type) {
  10738. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10739. "type mismatch: expect ",
  10740. type2str(type), " but got other");
  10741. goto fail;
  10742. }
  10743. }
  10744. #else
  10745. if (!wasm_func_type_result_is_subtype_of(
  10746. func_type, func->func_type, module->types,
  10747. module->type_count)) {
  10748. set_error_buf(
  10749. error_buf, error_buf_size,
  10750. "type mismatch: invalid func result types");
  10751. goto fail;
  10752. }
  10753. #endif
  10754. RESET_STACK();
  10755. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10756. }
  10757. #endif
  10758. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10759. || WASM_ENABLE_WAMR_COMPILER != 0
  10760. func->has_op_func_call = true;
  10761. #endif
  10762. (void)type;
  10763. break;
  10764. }
  10765. /*
  10766. * if disable reference type: call_indirect typeidx, 0x00
  10767. * if enable reference type: call_indirect typeidx, tableidx
  10768. */
  10769. case WASM_OP_CALL_INDIRECT:
  10770. #if WASM_ENABLE_TAIL_CALL != 0
  10771. case WASM_OP_RETURN_CALL_INDIRECT:
  10772. #endif
  10773. {
  10774. int32 idx;
  10775. WASMFuncType *func_type;
  10776. read_leb_uint32(p, p_end, type_idx);
  10777. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10778. read_leb_uint32(p, p_end, table_idx);
  10779. #else
  10780. CHECK_BUF(p, p_end, 1);
  10781. table_idx = read_uint8(p);
  10782. #endif
  10783. if (!check_table_index(module, table_idx, error_buf,
  10784. error_buf_size)) {
  10785. goto fail;
  10786. }
  10787. #if WASM_ENABLE_FAST_INTERP != 0
  10788. /* we need to emit before arguments */
  10789. #if WASM_ENABLE_TAIL_CALL != 0
  10790. emit_byte(loader_ctx, opcode);
  10791. #endif
  10792. emit_uint32(loader_ctx, type_idx);
  10793. emit_uint32(loader_ctx, table_idx);
  10794. #endif
  10795. /* skip elem idx */
  10796. POP_I32();
  10797. if (type_idx >= module->type_count) {
  10798. set_error_buf(error_buf, error_buf_size, "unknown type");
  10799. goto fail;
  10800. }
  10801. func_type = (WASMFuncType *)module->types[type_idx];
  10802. if (func_type->param_count > 0) {
  10803. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10804. idx--) {
  10805. #if WASM_ENABLE_FAST_INTERP != 0
  10806. POP_OFFSET_TYPE(func_type->types[idx]);
  10807. #endif
  10808. POP_TYPE(func_type->types[idx]);
  10809. }
  10810. }
  10811. #if WASM_ENABLE_TAIL_CALL != 0
  10812. if (opcode == WASM_OP_CALL_INDIRECT) {
  10813. #endif
  10814. for (i = 0; i < func_type->result_count; i++) {
  10815. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10816. #if WASM_ENABLE_FAST_INTERP != 0
  10817. PUSH_OFFSET_TYPE(
  10818. func_type->types[func_type->param_count + i]);
  10819. #endif
  10820. }
  10821. #if WASM_ENABLE_TAIL_CALL != 0
  10822. }
  10823. else {
  10824. uint8 type;
  10825. if (func_type->result_count
  10826. != func->func_type->result_count) {
  10827. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10828. "type mismatch: expect ",
  10829. func->func_type->result_count,
  10830. " return values but got other");
  10831. goto fail;
  10832. }
  10833. for (i = 0; i < func_type->result_count; i++) {
  10834. type = func->func_type
  10835. ->types[func->func_type->param_count + i];
  10836. if (func_type->types[func_type->param_count + i]
  10837. != type) {
  10838. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10839. "type mismatch: expect ",
  10840. type2str(type), " but got other");
  10841. goto fail;
  10842. }
  10843. }
  10844. RESET_STACK();
  10845. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10846. }
  10847. #endif
  10848. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10849. || WASM_ENABLE_WAMR_COMPILER != 0
  10850. func->has_op_func_call = true;
  10851. #endif
  10852. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10853. func->has_op_call_indirect = true;
  10854. #endif
  10855. break;
  10856. }
  10857. case WASM_OP_DROP:
  10858. {
  10859. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10860. int32 available_stack_cell =
  10861. (int32)(loader_ctx->stack_cell_num
  10862. - cur_block->stack_cell_num);
  10863. if (available_stack_cell <= 0
  10864. && !cur_block->is_stack_polymorphic) {
  10865. set_error_buf(error_buf, error_buf_size,
  10866. "type mismatch, opcode drop was found "
  10867. "but stack was empty");
  10868. goto fail;
  10869. }
  10870. if (available_stack_cell > 0) {
  10871. #if WASM_ENABLE_GC != 0
  10872. if (wasm_is_type_multi_byte_type(
  10873. *(loader_ctx->frame_ref - 1))) {
  10874. bh_assert((int32)(loader_ctx->reftype_map_num
  10875. - cur_block->reftype_map_num)
  10876. > 0);
  10877. loader_ctx->frame_reftype_map--;
  10878. loader_ctx->reftype_map_num--;
  10879. }
  10880. #endif
  10881. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10882. loader_ctx->frame_ref--;
  10883. loader_ctx->stack_cell_num--;
  10884. #if WASM_ENABLE_FAST_INTERP != 0
  10885. skip_label();
  10886. loader_ctx->frame_offset--;
  10887. if ((*(loader_ctx->frame_offset)
  10888. > loader_ctx->start_dynamic_offset)
  10889. && (*(loader_ctx->frame_offset)
  10890. < loader_ctx->max_dynamic_offset))
  10891. loader_ctx->dynamic_offset--;
  10892. #endif
  10893. }
  10894. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10895. loader_ctx->frame_ref -= 2;
  10896. loader_ctx->stack_cell_num -= 2;
  10897. #if WASM_ENABLE_FAST_INTERP == 0
  10898. *(p - 1) = WASM_OP_DROP_64;
  10899. #endif
  10900. #if WASM_ENABLE_FAST_INTERP != 0
  10901. skip_label();
  10902. loader_ctx->frame_offset -= 2;
  10903. if ((*(loader_ctx->frame_offset)
  10904. > loader_ctx->start_dynamic_offset)
  10905. && (*(loader_ctx->frame_offset)
  10906. < loader_ctx->max_dynamic_offset))
  10907. loader_ctx->dynamic_offset -= 2;
  10908. #endif
  10909. }
  10910. #if WASM_ENABLE_SIMD != 0
  10911. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10912. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10913. loader_ctx->frame_ref -= 4;
  10914. loader_ctx->stack_cell_num -= 4;
  10915. }
  10916. #endif
  10917. #endif
  10918. else {
  10919. set_error_buf(error_buf, error_buf_size,
  10920. "type mismatch");
  10921. goto fail;
  10922. }
  10923. }
  10924. else {
  10925. #if WASM_ENABLE_FAST_INTERP != 0
  10926. skip_label();
  10927. #endif
  10928. }
  10929. break;
  10930. }
  10931. case WASM_OP_SELECT:
  10932. {
  10933. uint8 ref_type;
  10934. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10935. int32 available_stack_cell;
  10936. #if WASM_ENABLE_FAST_INTERP != 0
  10937. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10938. #endif
  10939. POP_I32();
  10940. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10941. - cur_block->stack_cell_num);
  10942. if (available_stack_cell <= 0
  10943. && !cur_block->is_stack_polymorphic) {
  10944. set_error_buf(error_buf, error_buf_size,
  10945. "type mismatch or invalid result arity, "
  10946. "opcode select was found "
  10947. "but stack was empty");
  10948. goto fail;
  10949. }
  10950. if (available_stack_cell > 0) {
  10951. switch (*(loader_ctx->frame_ref - 1)) {
  10952. case VALUE_TYPE_I32:
  10953. case VALUE_TYPE_F32:
  10954. case VALUE_TYPE_ANY:
  10955. break;
  10956. case VALUE_TYPE_I64:
  10957. case VALUE_TYPE_F64:
  10958. #if WASM_ENABLE_FAST_INTERP == 0
  10959. *(p - 1) = WASM_OP_SELECT_64;
  10960. #endif
  10961. #if WASM_ENABLE_FAST_INTERP != 0
  10962. if (loader_ctx->p_code_compiled) {
  10963. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10964. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10965. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10966. *(void **)(p_code_compiled_tmp
  10967. - sizeof(void *)) =
  10968. handle_table[opcode_tmp];
  10969. #else
  10970. #if UINTPTR_MAX == UINT64_MAX
  10971. /* emit int32 relative offset in 64-bit target
  10972. */
  10973. int32 offset =
  10974. (int32)((uint8 *)handle_table[opcode_tmp]
  10975. - (uint8 *)handle_table[0]);
  10976. *(int32 *)(p_code_compiled_tmp
  10977. - sizeof(int32)) = offset;
  10978. #else
  10979. /* emit uint32 label address in 32-bit target */
  10980. *(uint32 *)(p_code_compiled_tmp
  10981. - sizeof(uint32)) =
  10982. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10983. #endif
  10984. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10985. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10986. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10987. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10988. #else
  10989. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10990. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10991. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10992. }
  10993. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10994. break;
  10995. #if WASM_ENABLE_SIMD != 0
  10996. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10997. case VALUE_TYPE_V128:
  10998. break;
  10999. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11000. #endif /* WASM_ENABLE_SIMD != 0 */
  11001. default:
  11002. {
  11003. set_error_buf(error_buf, error_buf_size,
  11004. "type mismatch");
  11005. goto fail;
  11006. }
  11007. }
  11008. ref_type = *(loader_ctx->frame_ref - 1);
  11009. #if WASM_ENABLE_FAST_INTERP != 0
  11010. POP_OFFSET_TYPE(ref_type);
  11011. POP_TYPE(ref_type);
  11012. POP_OFFSET_TYPE(ref_type);
  11013. POP_TYPE(ref_type);
  11014. PUSH_OFFSET_TYPE(ref_type);
  11015. PUSH_TYPE(ref_type);
  11016. #else
  11017. POP2_AND_PUSH(ref_type, ref_type);
  11018. #endif
  11019. }
  11020. else {
  11021. #if WASM_ENABLE_FAST_INTERP != 0
  11022. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11023. #endif
  11024. PUSH_TYPE(VALUE_TYPE_ANY);
  11025. }
  11026. break;
  11027. }
  11028. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11029. case WASM_OP_SELECT_T:
  11030. {
  11031. uint8 vec_len, type;
  11032. #if WASM_ENABLE_GC != 0
  11033. WASMRefType *ref_type = NULL;
  11034. #endif
  11035. #if WASM_ENABLE_FAST_INTERP != 0
  11036. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11037. #endif
  11038. read_leb_uint32(p, p_end, vec_len);
  11039. if (vec_len != 1) {
  11040. /* typed select must have exactly one result */
  11041. set_error_buf(error_buf, error_buf_size,
  11042. "invalid result arity");
  11043. goto fail;
  11044. }
  11045. #if WASM_ENABLE_GC == 0
  11046. CHECK_BUF(p, p_end, 1);
  11047. type = read_uint8(p);
  11048. if (!is_valid_value_type_for_interpreter(type)) {
  11049. set_error_buf(error_buf, error_buf_size,
  11050. "unknown value type");
  11051. goto fail;
  11052. }
  11053. #else
  11054. p_org = p + 1;
  11055. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11056. module->type_count, &need_ref_type_map,
  11057. &wasm_ref_type, false, error_buf,
  11058. error_buf_size)) {
  11059. goto fail;
  11060. }
  11061. type = wasm_ref_type.ref_type;
  11062. if (need_ref_type_map) {
  11063. if (!(ref_type = reftype_set_insert(
  11064. module->ref_type_set, &wasm_ref_type, error_buf,
  11065. error_buf_size))) {
  11066. goto fail;
  11067. }
  11068. }
  11069. #if WASM_ENABLE_FAST_INTERP == 0
  11070. while (p_org < p) {
  11071. #if WASM_ENABLE_DEBUG_INTERP != 0
  11072. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11073. error_buf_size)) {
  11074. goto fail;
  11075. }
  11076. #endif
  11077. /* Ignore extra bytes for interpreter */
  11078. *p_org++ = WASM_OP_NOP;
  11079. }
  11080. #endif
  11081. #endif /* end of WASM_ENABLE_GC == 0 */
  11082. POP_I32();
  11083. #if WASM_ENABLE_FAST_INTERP != 0
  11084. if (loader_ctx->p_code_compiled) {
  11085. uint8 opcode_tmp = WASM_OP_SELECT;
  11086. if (type == VALUE_TYPE_V128) {
  11087. #if (WASM_ENABLE_SIMD == 0) \
  11088. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11089. set_error_buf(error_buf, error_buf_size,
  11090. "SIMD v128 type isn't supported");
  11091. goto fail;
  11092. #endif
  11093. }
  11094. else {
  11095. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11096. opcode_tmp = WASM_OP_SELECT_64;
  11097. #if WASM_ENABLE_GC != 0
  11098. if (wasm_is_type_reftype(type))
  11099. opcode_tmp = WASM_OP_SELECT_T;
  11100. #endif
  11101. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11102. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11103. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11104. handle_table[opcode_tmp];
  11105. #else
  11106. #if UINTPTR_MAX == UINT64_MAX
  11107. /* emit int32 relative offset in 64-bit target */
  11108. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11109. - (uint8 *)handle_table[0]);
  11110. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11111. offset;
  11112. #else
  11113. /* emit uint32 label address in 32-bit target */
  11114. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11115. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11116. #endif
  11117. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11118. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11119. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11120. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11121. #else
  11122. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11123. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11124. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11125. }
  11126. }
  11127. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11128. POP_REF(type);
  11129. #if WASM_ENABLE_GC != 0
  11130. if (need_ref_type_map) {
  11131. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11132. wasm_reftype_struct_size(ref_type));
  11133. }
  11134. #endif
  11135. POP_REF(type);
  11136. #if WASM_ENABLE_GC != 0
  11137. if (need_ref_type_map) {
  11138. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11139. wasm_reftype_struct_size(ref_type));
  11140. }
  11141. #endif
  11142. PUSH_REF(type);
  11143. #if WASM_ENABLE_WAMR_COMPILER != 0
  11144. module->is_ref_types_used = true;
  11145. #endif
  11146. (void)vec_len;
  11147. break;
  11148. }
  11149. /* table.get x. tables[x]. [i32] -> [t] */
  11150. /* table.set x. tables[x]. [i32 t] -> [] */
  11151. case WASM_OP_TABLE_GET:
  11152. case WASM_OP_TABLE_SET:
  11153. {
  11154. uint8 decl_ref_type;
  11155. #if WASM_ENABLE_GC != 0
  11156. WASMRefType *ref_type;
  11157. #endif
  11158. read_leb_uint32(p, p_end, table_idx);
  11159. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11160. #if WASM_ENABLE_GC != 0
  11161. (void **)&ref_type,
  11162. #else
  11163. NULL,
  11164. #endif
  11165. error_buf, error_buf_size))
  11166. goto fail;
  11167. #if WASM_ENABLE_GC != 0
  11168. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11169. bh_assert(ref_type);
  11170. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11171. ref_type, wasm_reftype_struct_size(ref_type));
  11172. }
  11173. #endif
  11174. #if WASM_ENABLE_FAST_INTERP != 0
  11175. emit_uint32(loader_ctx, table_idx);
  11176. #endif
  11177. if (opcode == WASM_OP_TABLE_GET) {
  11178. POP_I32();
  11179. #if WASM_ENABLE_FAST_INTERP != 0
  11180. PUSH_OFFSET_TYPE(decl_ref_type);
  11181. #endif
  11182. PUSH_TYPE(decl_ref_type);
  11183. }
  11184. else {
  11185. #if WASM_ENABLE_FAST_INTERP != 0
  11186. POP_OFFSET_TYPE(decl_ref_type);
  11187. #endif
  11188. POP_TYPE(decl_ref_type);
  11189. POP_I32();
  11190. }
  11191. #if WASM_ENABLE_WAMR_COMPILER != 0
  11192. module->is_ref_types_used = true;
  11193. #endif
  11194. break;
  11195. }
  11196. case WASM_OP_REF_NULL:
  11197. {
  11198. uint8 ref_type;
  11199. #if WASM_ENABLE_GC == 0
  11200. CHECK_BUF(p, p_end, 1);
  11201. ref_type = read_uint8(p);
  11202. if (ref_type != VALUE_TYPE_FUNCREF
  11203. && ref_type != VALUE_TYPE_EXTERNREF) {
  11204. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11205. goto fail;
  11206. }
  11207. #else
  11208. read_leb_int32(p, p_end, heap_type);
  11209. if (heap_type >= 0) {
  11210. if (!check_type_index(module, module->type_count, heap_type,
  11211. error_buf, error_buf_size)) {
  11212. goto fail;
  11213. }
  11214. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11215. true, heap_type);
  11216. ref_type = wasm_ref_type.ref_type;
  11217. }
  11218. else {
  11219. if (!wasm_is_valid_heap_type(heap_type)) {
  11220. set_error_buf(error_buf, error_buf_size,
  11221. "unknown type");
  11222. goto fail;
  11223. }
  11224. ref_type = (uint8)((int32)0x80 + heap_type);
  11225. }
  11226. #endif /* end of WASM_ENABLE_GC == 0 */
  11227. #if WASM_ENABLE_FAST_INTERP != 0
  11228. PUSH_OFFSET_TYPE(ref_type);
  11229. #endif
  11230. PUSH_TYPE(ref_type);
  11231. #if WASM_ENABLE_WAMR_COMPILER != 0
  11232. module->is_ref_types_used = true;
  11233. #endif
  11234. break;
  11235. }
  11236. case WASM_OP_REF_IS_NULL:
  11237. {
  11238. #if WASM_ENABLE_GC == 0
  11239. #if WASM_ENABLE_FAST_INTERP != 0
  11240. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11241. int32 block_stack_cell_num =
  11242. (int32)(loader_ctx->stack_cell_num
  11243. - cur_block->stack_cell_num);
  11244. if (block_stack_cell_num <= 0) {
  11245. if (!cur_block->is_stack_polymorphic) {
  11246. set_error_buf(
  11247. error_buf, error_buf_size,
  11248. "type mismatch: expect data but stack was empty");
  11249. goto fail;
  11250. }
  11251. }
  11252. else {
  11253. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11254. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11255. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11256. if (!wasm_loader_pop_frame_ref_offset(
  11257. loader_ctx, *(loader_ctx->frame_ref - 1),
  11258. error_buf, error_buf_size)) {
  11259. goto fail;
  11260. }
  11261. }
  11262. else {
  11263. set_error_buf(error_buf, error_buf_size,
  11264. "type mismatch");
  11265. goto fail;
  11266. }
  11267. }
  11268. #else
  11269. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11270. error_buf, error_buf_size)
  11271. && !wasm_loader_pop_frame_ref(loader_ctx,
  11272. VALUE_TYPE_EXTERNREF,
  11273. error_buf, error_buf_size)) {
  11274. goto fail;
  11275. }
  11276. #endif
  11277. #else /* else of WASM_ENABLE_GC == 0 */
  11278. uint8 type;
  11279. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11280. error_buf, error_buf_size)) {
  11281. goto fail;
  11282. }
  11283. #endif
  11284. PUSH_I32();
  11285. #if WASM_ENABLE_WAMR_COMPILER != 0
  11286. module->is_ref_types_used = true;
  11287. #endif
  11288. break;
  11289. }
  11290. case WASM_OP_REF_FUNC:
  11291. {
  11292. read_leb_uint32(p, p_end, func_idx);
  11293. if (!check_function_index(module, func_idx, error_buf,
  11294. error_buf_size)) {
  11295. goto fail;
  11296. }
  11297. /* Refer to a forward-declared function:
  11298. the function must be an import, exported, or present in
  11299. a table elem segment or global initializer to be used as
  11300. the operand to ref.func */
  11301. if (func_idx >= module->import_function_count) {
  11302. WASMTableSeg *table_seg = module->table_segments;
  11303. bool func_declared = false;
  11304. uint32 j;
  11305. for (i = 0; i < module->global_count; i++) {
  11306. if (module->globals[i].type.val_type
  11307. == VALUE_TYPE_FUNCREF
  11308. && module->globals[i].init_expr.init_expr_type
  11309. == INIT_EXPR_TYPE_FUNCREF_CONST
  11310. && module->globals[i].init_expr.u.u32 == func_idx) {
  11311. func_declared = true;
  11312. break;
  11313. }
  11314. }
  11315. if (!func_declared) {
  11316. /* Check whether the function is declared in table segs,
  11317. note that it doesn't matter whether the table seg's
  11318. mode is passive, active or declarative. */
  11319. for (i = 0; i < module->table_seg_count;
  11320. i++, table_seg++) {
  11321. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11322. #if WASM_ENABLE_GC != 0
  11323. /* elem type is (ref null? func) or
  11324. (ref null? $t) */
  11325. || ((table_seg->elem_type
  11326. == REF_TYPE_HT_NON_NULLABLE
  11327. || table_seg->elem_type
  11328. == REF_TYPE_HT_NULLABLE)
  11329. && (table_seg->elem_ref_type->ref_ht_common
  11330. .heap_type
  11331. == HEAP_TYPE_FUNC
  11332. || table_seg->elem_ref_type
  11333. ->ref_ht_common.heap_type
  11334. > 0))
  11335. #endif
  11336. ) {
  11337. for (j = 0; j < table_seg->value_count; j++) {
  11338. if (table_seg->init_values[j].u.ref_index
  11339. == func_idx) {
  11340. func_declared = true;
  11341. break;
  11342. }
  11343. }
  11344. }
  11345. }
  11346. }
  11347. if (!func_declared) {
  11348. /* Check whether the function is exported */
  11349. for (i = 0; i < module->export_count; i++) {
  11350. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11351. && module->exports[i].index == func_idx) {
  11352. func_declared = true;
  11353. break;
  11354. }
  11355. }
  11356. }
  11357. if (!func_declared) {
  11358. set_error_buf(error_buf, error_buf_size,
  11359. "undeclared function reference");
  11360. goto fail;
  11361. }
  11362. }
  11363. #if WASM_ENABLE_FAST_INTERP != 0
  11364. emit_uint32(loader_ctx, func_idx);
  11365. #endif
  11366. #if WASM_ENABLE_GC == 0
  11367. PUSH_FUNCREF();
  11368. #else
  11369. if (func_idx < module->import_function_count)
  11370. type_idx =
  11371. module->import_functions[func_idx].u.function.type_idx;
  11372. else
  11373. type_idx = module
  11374. ->functions[func_idx
  11375. - module->import_function_count]
  11376. ->type_idx;
  11377. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11378. false, type_idx);
  11379. PUSH_REF(wasm_ref_type.ref_type);
  11380. #endif
  11381. #if WASM_ENABLE_WAMR_COMPILER != 0
  11382. module->is_ref_types_used = true;
  11383. #endif
  11384. break;
  11385. }
  11386. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11387. #if WASM_ENABLE_GC != 0
  11388. case WASM_OP_REF_AS_NON_NULL:
  11389. case WASM_OP_BR_ON_NULL:
  11390. {
  11391. uint8 type;
  11392. WASMRefType ref_type;
  11393. /* POP (ref null ht) and get the converted (ref ht) */
  11394. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11395. error_buf, error_buf_size)) {
  11396. goto fail;
  11397. }
  11398. if (opcode == WASM_OP_BR_ON_NULL) {
  11399. if (!(frame_csp_tmp =
  11400. check_branch_block(loader_ctx, &p, p_end, opcode,
  11401. error_buf, error_buf_size))) {
  11402. goto fail;
  11403. }
  11404. #if WASM_ENABLE_FAST_INTERP != 0
  11405. disable_emit = true;
  11406. #endif
  11407. }
  11408. /* PUSH the converted (ref ht) */
  11409. if (type != VALUE_TYPE_ANY) {
  11410. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11411. sizeof(WASMRefType));
  11412. }
  11413. PUSH_REF(type);
  11414. break;
  11415. }
  11416. case WASM_OP_BR_ON_NON_NULL:
  11417. {
  11418. uint8 type;
  11419. WASMRefType ref_type;
  11420. uint32 available_stack_cell =
  11421. loader_ctx->stack_cell_num
  11422. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11423. /* POP (ref null ht) and get the converted (ref ht) */
  11424. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11425. error_buf, error_buf_size)) {
  11426. goto fail;
  11427. }
  11428. #if WASM_ENABLE_FAST_INTERP != 0
  11429. disable_emit = true;
  11430. #endif
  11431. /* Temporarily PUSH back (ref ht), check brach block and
  11432. then POP it */
  11433. if (available_stack_cell
  11434. > 0) { /* stack isn't in polymorphic state */
  11435. if (type != VALUE_TYPE_ANY) {
  11436. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11437. &ref_type, sizeof(WASMRefType));
  11438. }
  11439. PUSH_REF(type);
  11440. }
  11441. if (!(frame_csp_tmp =
  11442. check_branch_block(loader_ctx, &p, p_end, opcode,
  11443. error_buf, error_buf_size))) {
  11444. goto fail;
  11445. }
  11446. if (available_stack_cell
  11447. > 0) { /* stack isn't in polymorphic state */
  11448. POP_REF(type);
  11449. #if WASM_ENABLE_FAST_INTERP != 0
  11450. /* Erase the opnd offset emitted by POP_REF() */
  11451. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11452. #endif
  11453. }
  11454. break;
  11455. }
  11456. case WASM_OP_REF_EQ:
  11457. POP_REF(REF_TYPE_EQREF);
  11458. POP_REF(REF_TYPE_EQREF);
  11459. PUSH_I32();
  11460. break;
  11461. #endif /* end of WASM_ENABLE_GC != 0 */
  11462. case WASM_OP_GET_LOCAL:
  11463. {
  11464. p_org = p - 1;
  11465. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11466. PUSH_TYPE(local_type);
  11467. #if WASM_ENABLE_GC != 0
  11468. /* Cannot get a non-nullable and unset local */
  11469. if (local_idx >= param_count
  11470. && wasm_is_reftype_htref_non_nullable(local_type)
  11471. && !wasm_loader_get_local_status(loader_ctx,
  11472. local_idx - param_count)) {
  11473. set_error_buf(error_buf, error_buf_size,
  11474. "uninitialized local");
  11475. goto fail;
  11476. }
  11477. #endif
  11478. #if WASM_ENABLE_FAST_INTERP != 0
  11479. /* Get Local is optimized out */
  11480. skip_label();
  11481. disable_emit = true;
  11482. operand_offset = local_offset;
  11483. PUSH_OFFSET_TYPE(local_type);
  11484. #else
  11485. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11486. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11487. if (local_offset < 0x80
  11488. #if WASM_ENABLE_GC != 0
  11489. && !wasm_is_type_reftype(local_type)
  11490. #endif
  11491. ) {
  11492. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11493. if (is_32bit_type(local_type)) {
  11494. *p_org++ = (uint8)local_offset;
  11495. }
  11496. else {
  11497. *p_org++ = (uint8)(local_offset | 0x80);
  11498. }
  11499. while (p_org < p) {
  11500. *p_org++ = WASM_OP_NOP;
  11501. }
  11502. }
  11503. #endif
  11504. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11505. break;
  11506. }
  11507. case WASM_OP_SET_LOCAL:
  11508. {
  11509. p_org = p - 1;
  11510. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11511. #if WASM_ENABLE_FAST_INTERP != 0
  11512. if (!(preserve_referenced_local(
  11513. loader_ctx, opcode, local_offset, local_type,
  11514. &preserve_local, error_buf, error_buf_size)))
  11515. goto fail;
  11516. if (local_offset < 256
  11517. #if WASM_ENABLE_GC != 0
  11518. && !wasm_is_type_reftype(local_type)
  11519. #endif
  11520. ) {
  11521. skip_label();
  11522. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11523. if (loader_ctx->p_code_compiled)
  11524. STORE_U16(loader_ctx->p_code_compiled - 2,
  11525. local_offset);
  11526. loader_ctx->frame_offset--;
  11527. loader_ctx->dynamic_offset--;
  11528. }
  11529. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11530. if (loader_ctx->p_code_compiled)
  11531. STORE_U16(loader_ctx->p_code_compiled - 2,
  11532. local_offset);
  11533. loader_ctx->frame_offset -= 2;
  11534. loader_ctx->dynamic_offset -= 2;
  11535. }
  11536. else {
  11537. if (is_32bit_type(local_type)) {
  11538. emit_label(EXT_OP_SET_LOCAL_FAST);
  11539. emit_byte(loader_ctx, (uint8)local_offset);
  11540. }
  11541. else {
  11542. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11543. emit_byte(loader_ctx, (uint8)local_offset);
  11544. }
  11545. POP_OFFSET_TYPE(local_type);
  11546. }
  11547. }
  11548. else { /* local index larger than 255, reserve leb */
  11549. emit_uint32(loader_ctx, local_idx);
  11550. POP_OFFSET_TYPE(local_type);
  11551. }
  11552. #else
  11553. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11554. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11555. if (local_offset < 0x80
  11556. #if WASM_ENABLE_GC != 0
  11557. && !wasm_is_type_reftype(local_type)
  11558. #endif
  11559. ) {
  11560. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11561. if (is_32bit_type(local_type)) {
  11562. *p_org++ = (uint8)local_offset;
  11563. }
  11564. else {
  11565. *p_org++ = (uint8)(local_offset | 0x80);
  11566. }
  11567. while (p_org < p) {
  11568. *p_org++ = WASM_OP_NOP;
  11569. }
  11570. }
  11571. #endif
  11572. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11573. #if WASM_ENABLE_GC != 0
  11574. if (local_idx >= param_count) {
  11575. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11576. }
  11577. #endif
  11578. POP_TYPE(local_type);
  11579. break;
  11580. }
  11581. case WASM_OP_TEE_LOCAL:
  11582. {
  11583. p_org = p - 1;
  11584. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11585. #if WASM_ENABLE_FAST_INTERP != 0
  11586. /* If the stack is in polymorphic state, do fake pop and push on
  11587. offset stack to keep the depth of offset stack to be the
  11588. same with ref stack */
  11589. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11590. if (cur_block->is_stack_polymorphic) {
  11591. POP_OFFSET_TYPE(local_type);
  11592. PUSH_OFFSET_TYPE(local_type);
  11593. }
  11594. #endif
  11595. POP_TYPE(local_type);
  11596. PUSH_TYPE(local_type);
  11597. #if WASM_ENABLE_FAST_INTERP != 0
  11598. if (!(preserve_referenced_local(
  11599. loader_ctx, opcode, local_offset, local_type,
  11600. &preserve_local, error_buf, error_buf_size)))
  11601. goto fail;
  11602. if (local_offset < 256
  11603. #if WASM_ENABLE_GC != 0
  11604. && !wasm_is_type_reftype(local_type)
  11605. #endif
  11606. ) {
  11607. skip_label();
  11608. if (is_32bit_type(local_type)) {
  11609. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11610. emit_byte(loader_ctx, (uint8)local_offset);
  11611. }
  11612. else {
  11613. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11614. emit_byte(loader_ctx, (uint8)local_offset);
  11615. }
  11616. }
  11617. else { /* local index larger than 255, reserve leb */
  11618. emit_uint32(loader_ctx, local_idx);
  11619. }
  11620. emit_operand(loader_ctx,
  11621. *(loader_ctx->frame_offset
  11622. - wasm_value_type_cell_num(local_type)));
  11623. #else
  11624. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11625. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11626. if (local_offset < 0x80
  11627. #if WASM_ENABLE_GC != 0
  11628. && !wasm_is_type_reftype(local_type)
  11629. #endif
  11630. ) {
  11631. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11632. if (is_32bit_type(local_type)) {
  11633. *p_org++ = (uint8)local_offset;
  11634. }
  11635. else {
  11636. *p_org++ = (uint8)(local_offset | 0x80);
  11637. }
  11638. while (p_org < p) {
  11639. *p_org++ = WASM_OP_NOP;
  11640. }
  11641. }
  11642. #endif
  11643. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11644. #if WASM_ENABLE_GC != 0
  11645. if (local_idx >= param_count) {
  11646. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11647. }
  11648. #endif
  11649. break;
  11650. }
  11651. case WASM_OP_GET_GLOBAL:
  11652. {
  11653. #if WASM_ENABLE_GC != 0
  11654. WASMRefType *ref_type;
  11655. #endif
  11656. p_org = p - 1;
  11657. read_leb_uint32(p, p_end, global_idx);
  11658. if (global_idx >= global_count) {
  11659. set_error_buf(error_buf, error_buf_size, "unknown global");
  11660. goto fail;
  11661. }
  11662. global_type = global_idx < module->import_global_count
  11663. ? module->import_globals[global_idx]
  11664. .u.global.type.val_type
  11665. : module
  11666. ->globals[global_idx
  11667. - module->import_global_count]
  11668. .type.val_type;
  11669. #if WASM_ENABLE_GC != 0
  11670. ref_type =
  11671. global_idx < module->import_global_count
  11672. ? module->import_globals[global_idx].u.global.ref_type
  11673. : module
  11674. ->globals[global_idx
  11675. - module->import_global_count]
  11676. .ref_type;
  11677. if (wasm_is_type_multi_byte_type(global_type)) {
  11678. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11679. wasm_reftype_struct_size(ref_type));
  11680. }
  11681. #endif
  11682. PUSH_TYPE(global_type);
  11683. #if WASM_ENABLE_FAST_INTERP == 0
  11684. if (global_type == VALUE_TYPE_I64
  11685. || global_type == VALUE_TYPE_F64) {
  11686. #if WASM_ENABLE_DEBUG_INTERP != 0
  11687. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11688. error_buf_size)) {
  11689. goto fail;
  11690. }
  11691. #endif
  11692. *p_org = WASM_OP_GET_GLOBAL_64;
  11693. }
  11694. #else /* else of WASM_ENABLE_FAST_INTERP */
  11695. if (global_type == VALUE_TYPE_I64
  11696. || global_type == VALUE_TYPE_F64) {
  11697. skip_label();
  11698. emit_label(WASM_OP_GET_GLOBAL_64);
  11699. }
  11700. emit_uint32(loader_ctx, global_idx);
  11701. PUSH_OFFSET_TYPE(global_type);
  11702. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11703. break;
  11704. }
  11705. case WASM_OP_SET_GLOBAL:
  11706. {
  11707. bool is_mutable = false;
  11708. #if WASM_ENABLE_GC != 0
  11709. WASMRefType *ref_type;
  11710. #endif
  11711. p_org = p - 1;
  11712. read_leb_uint32(p, p_end, global_idx);
  11713. if (global_idx >= global_count) {
  11714. set_error_buf(error_buf, error_buf_size, "unknown global");
  11715. goto fail;
  11716. }
  11717. is_mutable = global_idx < module->import_global_count
  11718. ? module->import_globals[global_idx]
  11719. .u.global.type.is_mutable
  11720. : module
  11721. ->globals[global_idx
  11722. - module->import_global_count]
  11723. .type.is_mutable;
  11724. if (!is_mutable) {
  11725. #if WASM_ENABLE_GC == 0
  11726. set_error_buf(error_buf, error_buf_size,
  11727. "global is immutable");
  11728. #else
  11729. set_error_buf(error_buf, error_buf_size,
  11730. "immutable global");
  11731. #endif
  11732. goto fail;
  11733. }
  11734. global_type = global_idx < module->import_global_count
  11735. ? module->import_globals[global_idx]
  11736. .u.global.type.val_type
  11737. : module
  11738. ->globals[global_idx
  11739. - module->import_global_count]
  11740. .type.val_type;
  11741. #if WASM_ENABLE_GC != 0
  11742. ref_type =
  11743. global_idx < module->import_global_count
  11744. ? module->import_globals[global_idx].u.global.ref_type
  11745. : module
  11746. ->globals[global_idx
  11747. - module->import_global_count]
  11748. .ref_type;
  11749. if (wasm_is_type_multi_byte_type(global_type)) {
  11750. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11751. wasm_reftype_struct_size(ref_type));
  11752. }
  11753. #endif
  11754. #if WASM_ENABLE_FAST_INTERP == 0
  11755. if (global_type == VALUE_TYPE_I64
  11756. || global_type == VALUE_TYPE_F64) {
  11757. #if WASM_ENABLE_DEBUG_INTERP != 0
  11758. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11759. error_buf_size)) {
  11760. goto fail;
  11761. }
  11762. #endif
  11763. *p_org = WASM_OP_SET_GLOBAL_64;
  11764. }
  11765. else if (module->aux_stack_size > 0
  11766. && global_idx == module->aux_stack_top_global_index) {
  11767. #if WASM_ENABLE_DEBUG_INTERP != 0
  11768. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11769. error_buf_size)) {
  11770. goto fail;
  11771. }
  11772. #endif
  11773. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11774. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11775. func->has_op_set_global_aux_stack = true;
  11776. #endif
  11777. }
  11778. #else /* else of WASM_ENABLE_FAST_INTERP */
  11779. if (global_type == VALUE_TYPE_I64
  11780. || global_type == VALUE_TYPE_F64) {
  11781. skip_label();
  11782. emit_label(WASM_OP_SET_GLOBAL_64);
  11783. }
  11784. else if (module->aux_stack_size > 0
  11785. && global_idx == module->aux_stack_top_global_index) {
  11786. skip_label();
  11787. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11788. }
  11789. emit_uint32(loader_ctx, global_idx);
  11790. POP_OFFSET_TYPE(global_type);
  11791. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11792. POP_TYPE(global_type);
  11793. break;
  11794. }
  11795. /* load */
  11796. case WASM_OP_I32_LOAD:
  11797. case WASM_OP_I32_LOAD8_S:
  11798. case WASM_OP_I32_LOAD8_U:
  11799. case WASM_OP_I32_LOAD16_S:
  11800. case WASM_OP_I32_LOAD16_U:
  11801. case WASM_OP_I64_LOAD:
  11802. case WASM_OP_I64_LOAD8_S:
  11803. case WASM_OP_I64_LOAD8_U:
  11804. case WASM_OP_I64_LOAD16_S:
  11805. case WASM_OP_I64_LOAD16_U:
  11806. case WASM_OP_I64_LOAD32_S:
  11807. case WASM_OP_I64_LOAD32_U:
  11808. case WASM_OP_F32_LOAD:
  11809. case WASM_OP_F64_LOAD:
  11810. /* store */
  11811. case WASM_OP_I32_STORE:
  11812. case WASM_OP_I32_STORE8:
  11813. case WASM_OP_I32_STORE16:
  11814. case WASM_OP_I64_STORE:
  11815. case WASM_OP_I64_STORE8:
  11816. case WASM_OP_I64_STORE16:
  11817. case WASM_OP_I64_STORE32:
  11818. case WASM_OP_F32_STORE:
  11819. case WASM_OP_F64_STORE:
  11820. {
  11821. #if WASM_ENABLE_FAST_INTERP != 0
  11822. /* change F32/F64 into I32/I64 */
  11823. if (opcode == WASM_OP_F32_LOAD) {
  11824. skip_label();
  11825. emit_label(WASM_OP_I32_LOAD);
  11826. }
  11827. else if (opcode == WASM_OP_F64_LOAD) {
  11828. skip_label();
  11829. emit_label(WASM_OP_I64_LOAD);
  11830. }
  11831. else if (opcode == WASM_OP_F32_STORE) {
  11832. skip_label();
  11833. emit_label(WASM_OP_I32_STORE);
  11834. }
  11835. else if (opcode == WASM_OP_F64_STORE) {
  11836. skip_label();
  11837. emit_label(WASM_OP_I64_STORE);
  11838. }
  11839. #endif
  11840. CHECK_MEMORY();
  11841. read_leb_uint32(p, p_end, align); /* align */
  11842. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11843. if (!check_memory_access_align(opcode, align, error_buf,
  11844. error_buf_size)) {
  11845. goto fail;
  11846. }
  11847. #if WASM_ENABLE_FAST_INTERP != 0
  11848. emit_uint32(loader_ctx, mem_offset);
  11849. #endif
  11850. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11851. func->has_memory_operations = true;
  11852. #endif
  11853. switch (opcode) {
  11854. /* load */
  11855. case WASM_OP_I32_LOAD:
  11856. case WASM_OP_I32_LOAD8_S:
  11857. case WASM_OP_I32_LOAD8_U:
  11858. case WASM_OP_I32_LOAD16_S:
  11859. case WASM_OP_I32_LOAD16_U:
  11860. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11861. break;
  11862. case WASM_OP_I64_LOAD:
  11863. case WASM_OP_I64_LOAD8_S:
  11864. case WASM_OP_I64_LOAD8_U:
  11865. case WASM_OP_I64_LOAD16_S:
  11866. case WASM_OP_I64_LOAD16_U:
  11867. case WASM_OP_I64_LOAD32_S:
  11868. case WASM_OP_I64_LOAD32_U:
  11869. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11870. break;
  11871. case WASM_OP_F32_LOAD:
  11872. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11873. break;
  11874. case WASM_OP_F64_LOAD:
  11875. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11876. break;
  11877. /* store */
  11878. case WASM_OP_I32_STORE:
  11879. case WASM_OP_I32_STORE8:
  11880. case WASM_OP_I32_STORE16:
  11881. POP_I32();
  11882. POP_MEM_OFFSET();
  11883. break;
  11884. case WASM_OP_I64_STORE:
  11885. case WASM_OP_I64_STORE8:
  11886. case WASM_OP_I64_STORE16:
  11887. case WASM_OP_I64_STORE32:
  11888. POP_I64();
  11889. POP_MEM_OFFSET();
  11890. break;
  11891. case WASM_OP_F32_STORE:
  11892. POP_F32();
  11893. POP_MEM_OFFSET();
  11894. break;
  11895. case WASM_OP_F64_STORE:
  11896. POP_F64();
  11897. POP_MEM_OFFSET();
  11898. break;
  11899. default:
  11900. break;
  11901. }
  11902. break;
  11903. }
  11904. case WASM_OP_MEMORY_SIZE:
  11905. CHECK_MEMORY();
  11906. /* reserved byte 0x00 */
  11907. if (*p++ != 0x00) {
  11908. set_error_buf(error_buf, error_buf_size,
  11909. "zero byte expected");
  11910. goto fail;
  11911. }
  11912. PUSH_PAGE_COUNT();
  11913. module->possible_memory_grow = true;
  11914. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11915. func->has_memory_operations = true;
  11916. #endif
  11917. break;
  11918. case WASM_OP_MEMORY_GROW:
  11919. CHECK_MEMORY();
  11920. /* reserved byte 0x00 */
  11921. if (*p++ != 0x00) {
  11922. set_error_buf(error_buf, error_buf_size,
  11923. "zero byte expected");
  11924. goto fail;
  11925. }
  11926. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11927. module->possible_memory_grow = true;
  11928. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11929. || WASM_ENABLE_WAMR_COMPILER != 0
  11930. func->has_op_memory_grow = true;
  11931. #endif
  11932. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11933. func->has_memory_operations = true;
  11934. #endif
  11935. break;
  11936. case WASM_OP_I32_CONST:
  11937. read_leb_int32(p, p_end, i32_const);
  11938. #if WASM_ENABLE_FAST_INTERP != 0
  11939. skip_label();
  11940. disable_emit = true;
  11941. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11942. if (operand_offset == 0) {
  11943. disable_emit = false;
  11944. emit_label(WASM_OP_I32_CONST);
  11945. emit_uint32(loader_ctx, i32_const);
  11946. }
  11947. #else
  11948. (void)i32_const;
  11949. #endif
  11950. PUSH_I32();
  11951. break;
  11952. case WASM_OP_I64_CONST:
  11953. read_leb_int64(p, p_end, i64_const);
  11954. #if WASM_ENABLE_FAST_INTERP != 0
  11955. skip_label();
  11956. disable_emit = true;
  11957. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11958. if (operand_offset == 0) {
  11959. disable_emit = false;
  11960. emit_label(WASM_OP_I64_CONST);
  11961. emit_uint64(loader_ctx, i64_const);
  11962. }
  11963. #endif
  11964. PUSH_I64();
  11965. break;
  11966. case WASM_OP_F32_CONST:
  11967. CHECK_BUF(p, p_end, sizeof(float32));
  11968. p += sizeof(float32);
  11969. #if WASM_ENABLE_FAST_INTERP != 0
  11970. skip_label();
  11971. disable_emit = true;
  11972. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11973. sizeof(float32));
  11974. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11975. if (operand_offset == 0) {
  11976. disable_emit = false;
  11977. emit_label(WASM_OP_F32_CONST);
  11978. emit_float32(loader_ctx, f32_const);
  11979. }
  11980. #endif
  11981. PUSH_F32();
  11982. break;
  11983. case WASM_OP_F64_CONST:
  11984. CHECK_BUF(p, p_end, sizeof(float64));
  11985. p += sizeof(float64);
  11986. #if WASM_ENABLE_FAST_INTERP != 0
  11987. skip_label();
  11988. disable_emit = true;
  11989. /* Some MCU may require 8-byte align */
  11990. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11991. sizeof(float64));
  11992. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11993. if (operand_offset == 0) {
  11994. disable_emit = false;
  11995. emit_label(WASM_OP_F64_CONST);
  11996. emit_float64(loader_ctx, f64_const);
  11997. }
  11998. #endif
  11999. PUSH_F64();
  12000. break;
  12001. case WASM_OP_I32_EQZ:
  12002. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12003. break;
  12004. case WASM_OP_I32_EQ:
  12005. case WASM_OP_I32_NE:
  12006. case WASM_OP_I32_LT_S:
  12007. case WASM_OP_I32_LT_U:
  12008. case WASM_OP_I32_GT_S:
  12009. case WASM_OP_I32_GT_U:
  12010. case WASM_OP_I32_LE_S:
  12011. case WASM_OP_I32_LE_U:
  12012. case WASM_OP_I32_GE_S:
  12013. case WASM_OP_I32_GE_U:
  12014. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12015. break;
  12016. case WASM_OP_I64_EQZ:
  12017. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12018. break;
  12019. case WASM_OP_I64_EQ:
  12020. case WASM_OP_I64_NE:
  12021. case WASM_OP_I64_LT_S:
  12022. case WASM_OP_I64_LT_U:
  12023. case WASM_OP_I64_GT_S:
  12024. case WASM_OP_I64_GT_U:
  12025. case WASM_OP_I64_LE_S:
  12026. case WASM_OP_I64_LE_U:
  12027. case WASM_OP_I64_GE_S:
  12028. case WASM_OP_I64_GE_U:
  12029. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12030. break;
  12031. case WASM_OP_F32_EQ:
  12032. case WASM_OP_F32_NE:
  12033. case WASM_OP_F32_LT:
  12034. case WASM_OP_F32_GT:
  12035. case WASM_OP_F32_LE:
  12036. case WASM_OP_F32_GE:
  12037. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12038. break;
  12039. case WASM_OP_F64_EQ:
  12040. case WASM_OP_F64_NE:
  12041. case WASM_OP_F64_LT:
  12042. case WASM_OP_F64_GT:
  12043. case WASM_OP_F64_LE:
  12044. case WASM_OP_F64_GE:
  12045. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12046. break;
  12047. case WASM_OP_I32_CLZ:
  12048. case WASM_OP_I32_CTZ:
  12049. case WASM_OP_I32_POPCNT:
  12050. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12051. break;
  12052. case WASM_OP_I32_ADD:
  12053. case WASM_OP_I32_SUB:
  12054. case WASM_OP_I32_MUL:
  12055. case WASM_OP_I32_DIV_S:
  12056. case WASM_OP_I32_DIV_U:
  12057. case WASM_OP_I32_REM_S:
  12058. case WASM_OP_I32_REM_U:
  12059. case WASM_OP_I32_AND:
  12060. case WASM_OP_I32_OR:
  12061. case WASM_OP_I32_XOR:
  12062. case WASM_OP_I32_SHL:
  12063. case WASM_OP_I32_SHR_S:
  12064. case WASM_OP_I32_SHR_U:
  12065. case WASM_OP_I32_ROTL:
  12066. case WASM_OP_I32_ROTR:
  12067. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12068. break;
  12069. case WASM_OP_I64_CLZ:
  12070. case WASM_OP_I64_CTZ:
  12071. case WASM_OP_I64_POPCNT:
  12072. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12073. break;
  12074. case WASM_OP_I64_ADD:
  12075. case WASM_OP_I64_SUB:
  12076. case WASM_OP_I64_MUL:
  12077. case WASM_OP_I64_DIV_S:
  12078. case WASM_OP_I64_DIV_U:
  12079. case WASM_OP_I64_REM_S:
  12080. case WASM_OP_I64_REM_U:
  12081. case WASM_OP_I64_AND:
  12082. case WASM_OP_I64_OR:
  12083. case WASM_OP_I64_XOR:
  12084. case WASM_OP_I64_SHL:
  12085. case WASM_OP_I64_SHR_S:
  12086. case WASM_OP_I64_SHR_U:
  12087. case WASM_OP_I64_ROTL:
  12088. case WASM_OP_I64_ROTR:
  12089. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12090. break;
  12091. case WASM_OP_F32_ABS:
  12092. case WASM_OP_F32_NEG:
  12093. case WASM_OP_F32_CEIL:
  12094. case WASM_OP_F32_FLOOR:
  12095. case WASM_OP_F32_TRUNC:
  12096. case WASM_OP_F32_NEAREST:
  12097. case WASM_OP_F32_SQRT:
  12098. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12099. break;
  12100. case WASM_OP_F32_ADD:
  12101. case WASM_OP_F32_SUB:
  12102. case WASM_OP_F32_MUL:
  12103. case WASM_OP_F32_DIV:
  12104. case WASM_OP_F32_MIN:
  12105. case WASM_OP_F32_MAX:
  12106. case WASM_OP_F32_COPYSIGN:
  12107. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12108. break;
  12109. case WASM_OP_F64_ABS:
  12110. case WASM_OP_F64_NEG:
  12111. case WASM_OP_F64_CEIL:
  12112. case WASM_OP_F64_FLOOR:
  12113. case WASM_OP_F64_TRUNC:
  12114. case WASM_OP_F64_NEAREST:
  12115. case WASM_OP_F64_SQRT:
  12116. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12117. break;
  12118. case WASM_OP_F64_ADD:
  12119. case WASM_OP_F64_SUB:
  12120. case WASM_OP_F64_MUL:
  12121. case WASM_OP_F64_DIV:
  12122. case WASM_OP_F64_MIN:
  12123. case WASM_OP_F64_MAX:
  12124. case WASM_OP_F64_COPYSIGN:
  12125. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12126. break;
  12127. case WASM_OP_I32_WRAP_I64:
  12128. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12129. break;
  12130. case WASM_OP_I32_TRUNC_S_F32:
  12131. case WASM_OP_I32_TRUNC_U_F32:
  12132. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12133. break;
  12134. case WASM_OP_I32_TRUNC_S_F64:
  12135. case WASM_OP_I32_TRUNC_U_F64:
  12136. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12137. break;
  12138. case WASM_OP_I64_EXTEND_S_I32:
  12139. case WASM_OP_I64_EXTEND_U_I32:
  12140. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12141. break;
  12142. case WASM_OP_I64_TRUNC_S_F32:
  12143. case WASM_OP_I64_TRUNC_U_F32:
  12144. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12145. break;
  12146. case WASM_OP_I64_TRUNC_S_F64:
  12147. case WASM_OP_I64_TRUNC_U_F64:
  12148. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12149. break;
  12150. case WASM_OP_F32_CONVERT_S_I32:
  12151. case WASM_OP_F32_CONVERT_U_I32:
  12152. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12153. break;
  12154. case WASM_OP_F32_CONVERT_S_I64:
  12155. case WASM_OP_F32_CONVERT_U_I64:
  12156. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12157. break;
  12158. case WASM_OP_F32_DEMOTE_F64:
  12159. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12160. break;
  12161. case WASM_OP_F64_CONVERT_S_I32:
  12162. case WASM_OP_F64_CONVERT_U_I32:
  12163. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12164. break;
  12165. case WASM_OP_F64_CONVERT_S_I64:
  12166. case WASM_OP_F64_CONVERT_U_I64:
  12167. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12168. break;
  12169. case WASM_OP_F64_PROMOTE_F32:
  12170. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12171. break;
  12172. case WASM_OP_I32_REINTERPRET_F32:
  12173. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12174. break;
  12175. case WASM_OP_I64_REINTERPRET_F64:
  12176. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12177. break;
  12178. case WASM_OP_F32_REINTERPRET_I32:
  12179. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12180. break;
  12181. case WASM_OP_F64_REINTERPRET_I64:
  12182. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12183. break;
  12184. case WASM_OP_I32_EXTEND8_S:
  12185. case WASM_OP_I32_EXTEND16_S:
  12186. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12187. break;
  12188. case WASM_OP_I64_EXTEND8_S:
  12189. case WASM_OP_I64_EXTEND16_S:
  12190. case WASM_OP_I64_EXTEND32_S:
  12191. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12192. break;
  12193. #if WASM_ENABLE_GC != 0
  12194. case WASM_OP_GC_PREFIX:
  12195. {
  12196. uint32 opcode1;
  12197. read_leb_uint32(p, p_end, opcode1);
  12198. #if WASM_ENABLE_FAST_INTERP != 0
  12199. emit_byte(loader_ctx, ((uint8)opcode1));
  12200. #endif
  12201. switch (opcode1) {
  12202. case WASM_OP_STRUCT_NEW:
  12203. case WASM_OP_STRUCT_NEW_DEFAULT:
  12204. {
  12205. read_leb_uint32(p, p_end, type_idx);
  12206. #if WASM_ENABLE_FAST_INTERP != 0
  12207. emit_uint32(loader_ctx, type_idx);
  12208. #endif
  12209. if (!check_type_index(module, module->type_count,
  12210. type_idx, error_buf,
  12211. error_buf_size)) {
  12212. goto fail;
  12213. }
  12214. if (module->types[type_idx]->type_flag
  12215. != WASM_TYPE_STRUCT) {
  12216. set_error_buf(error_buf, error_buf_size,
  12217. "unkown struct type");
  12218. goto fail;
  12219. }
  12220. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12221. int32 j, k;
  12222. uint8 value_type;
  12223. uint32 ref_type_struct_size;
  12224. WASMStructType *struct_type =
  12225. (WASMStructType *)module->types[type_idx];
  12226. k = struct_type->ref_type_map_count - 1;
  12227. for (j = struct_type->field_count - 1; j >= 0;
  12228. j--) {
  12229. value_type = struct_type->fields[j].field_type;
  12230. if (wasm_is_type_reftype(value_type)) {
  12231. if (wasm_is_type_multi_byte_type(
  12232. value_type)) {
  12233. ref_type_struct_size =
  12234. wasm_reftype_struct_size(
  12235. struct_type->ref_type_maps[k]
  12236. .ref_type);
  12237. bh_memcpy_s(
  12238. &wasm_ref_type,
  12239. (uint32)sizeof(WASMRefType),
  12240. struct_type->ref_type_maps[k]
  12241. .ref_type,
  12242. ref_type_struct_size);
  12243. k--;
  12244. }
  12245. POP_REF(value_type);
  12246. }
  12247. else {
  12248. switch (value_type) {
  12249. case VALUE_TYPE_I32:
  12250. case PACKED_TYPE_I8:
  12251. case PACKED_TYPE_I16:
  12252. POP_I32();
  12253. break;
  12254. case VALUE_TYPE_I64:
  12255. POP_I64();
  12256. break;
  12257. case VALUE_TYPE_F32:
  12258. POP_F32();
  12259. break;
  12260. case VALUE_TYPE_F64:
  12261. POP_F64();
  12262. break;
  12263. default:
  12264. set_error_buf(error_buf,
  12265. error_buf_size,
  12266. "unknown type");
  12267. goto fail;
  12268. }
  12269. }
  12270. }
  12271. }
  12272. /* PUSH struct obj, (ref $t) */
  12273. wasm_set_refheaptype_typeidx(
  12274. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12275. PUSH_REF(wasm_ref_type.ref_type);
  12276. break;
  12277. }
  12278. case WASM_OP_STRUCT_GET:
  12279. case WASM_OP_STRUCT_GET_S:
  12280. case WASM_OP_STRUCT_GET_U:
  12281. case WASM_OP_STRUCT_SET:
  12282. {
  12283. WASMStructType *struct_type;
  12284. WASMRefType *ref_type = NULL;
  12285. uint32 field_idx;
  12286. uint8 field_type;
  12287. read_leb_uint32(p, p_end, type_idx);
  12288. #if WASM_ENABLE_FAST_INTERP != 0
  12289. emit_uint32(loader_ctx, type_idx);
  12290. #endif
  12291. if (!check_type_index(module, module->type_count,
  12292. type_idx, error_buf,
  12293. error_buf_size)) {
  12294. goto fail;
  12295. }
  12296. if (module->types[type_idx]->type_flag
  12297. != WASM_TYPE_STRUCT) {
  12298. set_error_buf(error_buf, error_buf_size,
  12299. "unknown struct type");
  12300. goto fail;
  12301. }
  12302. struct_type = (WASMStructType *)module->types[type_idx];
  12303. read_leb_uint32(p, p_end, field_idx);
  12304. #if WASM_ENABLE_FAST_INTERP != 0
  12305. emit_uint32(loader_ctx, field_idx);
  12306. #endif
  12307. if (field_idx >= struct_type->field_count) {
  12308. set_error_buf(error_buf, error_buf_size,
  12309. "unknown struct field");
  12310. goto fail;
  12311. }
  12312. if (opcode1 == WASM_OP_STRUCT_SET
  12313. && !(struct_type->fields[field_idx].field_flags
  12314. & 1)) {
  12315. set_error_buf(error_buf, error_buf_size,
  12316. "field is immutable");
  12317. goto fail;
  12318. }
  12319. field_type = struct_type->fields[field_idx].field_type;
  12320. if (is_packed_type(field_type)) {
  12321. if (opcode1 == WASM_OP_STRUCT_GET) {
  12322. set_error_buf(error_buf, error_buf_size,
  12323. "type mismatch");
  12324. goto fail;
  12325. }
  12326. else {
  12327. field_type = VALUE_TYPE_I32;
  12328. }
  12329. }
  12330. if (wasm_is_type_multi_byte_type(field_type)) {
  12331. ref_type = wasm_reftype_map_find(
  12332. struct_type->ref_type_maps,
  12333. struct_type->ref_type_map_count, field_idx);
  12334. bh_assert(ref_type);
  12335. }
  12336. if (opcode1 == WASM_OP_STRUCT_SET) {
  12337. /* POP field */
  12338. if (wasm_is_type_multi_byte_type(field_type)) {
  12339. bh_memcpy_s(&wasm_ref_type,
  12340. (uint32)sizeof(WASMRefType),
  12341. ref_type,
  12342. wasm_reftype_struct_size(ref_type));
  12343. }
  12344. POP_REF(field_type);
  12345. /* POP struct obj, (ref null $t) */
  12346. wasm_set_refheaptype_typeidx(
  12347. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12348. POP_REF(wasm_ref_type.ref_type);
  12349. }
  12350. else {
  12351. /* POP struct obj, (ref null $t) */
  12352. wasm_set_refheaptype_typeidx(
  12353. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12354. POP_REF(wasm_ref_type.ref_type);
  12355. /* PUSH field */
  12356. if (wasm_is_type_multi_byte_type(field_type)) {
  12357. bh_memcpy_s(&wasm_ref_type,
  12358. (uint32)sizeof(WASMRefType),
  12359. ref_type,
  12360. wasm_reftype_struct_size(ref_type));
  12361. }
  12362. PUSH_REF(field_type);
  12363. }
  12364. break;
  12365. }
  12366. case WASM_OP_ARRAY_NEW:
  12367. case WASM_OP_ARRAY_NEW_DEFAULT:
  12368. case WASM_OP_ARRAY_NEW_FIXED:
  12369. case WASM_OP_ARRAY_NEW_DATA:
  12370. case WASM_OP_ARRAY_NEW_ELEM:
  12371. {
  12372. WASMArrayType *array_type;
  12373. uint8 elem_type;
  12374. uint32 u32 = 0;
  12375. read_leb_uint32(p, p_end, type_idx);
  12376. #if WASM_ENABLE_FAST_INTERP != 0
  12377. emit_uint32(loader_ctx, type_idx);
  12378. #endif
  12379. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12380. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12381. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12382. read_leb_uint32(p, p_end, u32);
  12383. #if WASM_ENABLE_FAST_INTERP != 0
  12384. emit_uint32(loader_ctx, u32);
  12385. #endif
  12386. }
  12387. if (!check_array_type(module, type_idx, error_buf,
  12388. error_buf_size)) {
  12389. goto fail;
  12390. }
  12391. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12392. /* length */
  12393. POP_I32();
  12394. }
  12395. array_type = (WASMArrayType *)module->types[type_idx];
  12396. elem_type = array_type->elem_type;
  12397. if (opcode1 == WASM_OP_ARRAY_NEW
  12398. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12399. if (wasm_is_type_multi_byte_type(elem_type)) {
  12400. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12401. array_type->elem_ref_type,
  12402. wasm_reftype_struct_size(
  12403. array_type->elem_ref_type));
  12404. }
  12405. if (is_packed_type(elem_type)) {
  12406. elem_type = VALUE_TYPE_I32;
  12407. }
  12408. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12409. uint32 N = u32;
  12410. for (i = 0; i < N; i++) {
  12411. if (wasm_is_type_multi_byte_type(
  12412. elem_type)) {
  12413. bh_memcpy_s(
  12414. &wasm_ref_type, sizeof(WASMRefType),
  12415. array_type->elem_ref_type,
  12416. wasm_reftype_struct_size(
  12417. array_type->elem_ref_type));
  12418. }
  12419. POP_REF(elem_type);
  12420. }
  12421. }
  12422. else
  12423. POP_REF(elem_type);
  12424. }
  12425. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12426. /* offset of data segment */
  12427. POP_I32();
  12428. if (u32 >= module->data_seg_count) {
  12429. set_error_buf(error_buf, error_buf_size,
  12430. "unknown data segment");
  12431. goto fail;
  12432. }
  12433. if (wasm_is_type_reftype(elem_type)) {
  12434. set_error_buf(error_buf, error_buf_size,
  12435. "array elem type mismatch");
  12436. goto fail;
  12437. }
  12438. }
  12439. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12440. WASMTableSeg *table_seg =
  12441. module->table_segments + u32;
  12442. /* offset of element segment */
  12443. POP_I32();
  12444. if (u32 >= module->table_seg_count) {
  12445. set_error_buf(error_buf, error_buf_size,
  12446. "unknown element segment");
  12447. goto fail;
  12448. }
  12449. if (!wasm_reftype_is_subtype_of(
  12450. table_seg->elem_type,
  12451. table_seg->elem_ref_type, elem_type,
  12452. array_type->elem_ref_type, module->types,
  12453. module->type_count)) {
  12454. set_error_buf(error_buf, error_buf_size,
  12455. "array elem type mismatch");
  12456. goto fail;
  12457. }
  12458. }
  12459. /* PUSH array obj, (ref $t) */
  12460. wasm_set_refheaptype_typeidx(
  12461. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12462. PUSH_REF(wasm_ref_type.ref_type);
  12463. break;
  12464. }
  12465. case WASM_OP_ARRAY_GET:
  12466. case WASM_OP_ARRAY_GET_S:
  12467. case WASM_OP_ARRAY_GET_U:
  12468. case WASM_OP_ARRAY_SET:
  12469. {
  12470. uint8 elem_type;
  12471. WASMArrayType *array_type;
  12472. WASMRefType *ref_type = NULL;
  12473. read_leb_uint32(p, p_end, type_idx);
  12474. #if WASM_ENABLE_FAST_INTERP != 0
  12475. emit_uint32(loader_ctx, type_idx);
  12476. #endif
  12477. if (!check_array_type(module, type_idx, error_buf,
  12478. error_buf_size)) {
  12479. goto fail;
  12480. }
  12481. array_type = (WASMArrayType *)module->types[type_idx];
  12482. if (opcode1 == WASM_OP_ARRAY_SET
  12483. && !(array_type->elem_flags & 1)) {
  12484. set_error_buf(error_buf, error_buf_size,
  12485. "array is immutable");
  12486. goto fail;
  12487. }
  12488. elem_type = array_type->elem_type;
  12489. if (is_packed_type(elem_type)) {
  12490. if (opcode1 != WASM_OP_ARRAY_GET_S
  12491. && opcode1 != WASM_OP_ARRAY_GET_U
  12492. && opcode1 != WASM_OP_ARRAY_SET) {
  12493. set_error_buf(error_buf, error_buf_size,
  12494. "type mismatch");
  12495. goto fail;
  12496. }
  12497. else {
  12498. elem_type = VALUE_TYPE_I32;
  12499. }
  12500. }
  12501. ref_type = array_type->elem_ref_type;
  12502. if (opcode1 == WASM_OP_ARRAY_SET) {
  12503. /* POP elem to set */
  12504. if (wasm_is_type_multi_byte_type(elem_type)) {
  12505. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12506. ref_type,
  12507. wasm_reftype_struct_size(ref_type));
  12508. }
  12509. POP_REF(elem_type);
  12510. }
  12511. /* elem idx */
  12512. POP_I32();
  12513. /* POP array obj, (ref null $t) */
  12514. wasm_set_refheaptype_typeidx(
  12515. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12516. POP_REF(wasm_ref_type.ref_type);
  12517. if (opcode1 != WASM_OP_ARRAY_SET) {
  12518. /* PUSH elem */
  12519. if (wasm_is_type_multi_byte_type(elem_type)) {
  12520. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12521. ref_type,
  12522. wasm_reftype_struct_size(ref_type));
  12523. }
  12524. PUSH_REF(elem_type);
  12525. }
  12526. break;
  12527. }
  12528. case WASM_OP_ARRAY_LEN:
  12529. {
  12530. POP_REF(REF_TYPE_ARRAYREF);
  12531. /* length */
  12532. PUSH_I32();
  12533. break;
  12534. }
  12535. case WASM_OP_ARRAY_FILL:
  12536. {
  12537. WASMArrayType *array_type;
  12538. uint8 elem_type;
  12539. /* typeidx */
  12540. read_leb_uint32(p, p_end, type_idx);
  12541. #if WASM_ENABLE_FAST_INTERP != 0
  12542. emit_uint32(loader_ctx, type_idx);
  12543. #endif
  12544. if (!check_array_type(module, type_idx, error_buf,
  12545. error_buf_size)) {
  12546. goto fail;
  12547. }
  12548. array_type = (WASMArrayType *)module->types[type_idx];
  12549. if (!(array_type->elem_flags & 1)) {
  12550. set_error_buf(error_buf, error_buf_size,
  12551. "array is immutable");
  12552. goto fail;
  12553. }
  12554. elem_type = array_type->elem_type;
  12555. if (is_packed_type(elem_type)) {
  12556. elem_type = VALUE_TYPE_I32;
  12557. }
  12558. POP_I32(); /* length */
  12559. #if WASM_ENABLE_FAST_INTERP != 0
  12560. POP_OFFSET_TYPE(elem_type);
  12561. #endif
  12562. POP_TYPE(elem_type);
  12563. POP_I32(); /* start */
  12564. /* POP array obj, (ref null $t) */
  12565. wasm_set_refheaptype_typeidx(
  12566. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12567. POP_REF(wasm_ref_type.ref_type);
  12568. break;
  12569. }
  12570. case WASM_OP_ARRAY_COPY:
  12571. {
  12572. uint32 src_type_idx;
  12573. uint8 src_elem_type, dst_elem_type;
  12574. WASMRefType src_ref_type = { 0 },
  12575. *src_elem_ref_type = NULL;
  12576. WASMRefType dst_ref_type = { 0 },
  12577. *dst_elem_ref_type = NULL;
  12578. WASMArrayType *array_type;
  12579. /* typeidx1 */
  12580. read_leb_uint32(p, p_end, type_idx);
  12581. #if WASM_ENABLE_FAST_INTERP != 0
  12582. emit_uint32(loader_ctx, type_idx);
  12583. #endif
  12584. /* typeidx2 */
  12585. read_leb_uint32(p, p_end, src_type_idx);
  12586. #if WASM_ENABLE_FAST_INTERP != 0
  12587. emit_uint32(loader_ctx, src_type_idx);
  12588. #endif
  12589. if (!check_array_type(module, type_idx, error_buf,
  12590. error_buf_size)) {
  12591. goto fail;
  12592. }
  12593. if (!check_array_type(module, src_type_idx, error_buf,
  12594. error_buf_size)) {
  12595. goto fail;
  12596. }
  12597. POP_I32();
  12598. POP_I32();
  12599. /* POP array obj, (ref null $t) */
  12600. wasm_set_refheaptype_typeidx(
  12601. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12602. POP_REF(wasm_ref_type.ref_type);
  12603. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12604. &wasm_ref_type,
  12605. wasm_reftype_struct_size(&wasm_ref_type));
  12606. POP_I32();
  12607. /* POP array obj, (ref null $t) */
  12608. wasm_set_refheaptype_typeidx(
  12609. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12610. POP_REF(wasm_ref_type.ref_type);
  12611. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12612. &wasm_ref_type,
  12613. wasm_reftype_struct_size(&wasm_ref_type));
  12614. array_type = (WASMArrayType *)module->types[type_idx];
  12615. if (!(array_type->elem_flags & 1)) {
  12616. set_error_buf(error_buf, error_buf_size,
  12617. "destination array is immutable");
  12618. goto fail;
  12619. }
  12620. dst_elem_type = array_type->elem_type;
  12621. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12622. dst_elem_ref_type = array_type->elem_ref_type;
  12623. }
  12624. array_type =
  12625. (WASMArrayType *)module->types[src_type_idx];
  12626. src_elem_type = array_type->elem_type;
  12627. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12628. src_elem_ref_type = array_type->elem_ref_type;
  12629. }
  12630. if (!wasm_reftype_is_subtype_of(
  12631. src_elem_type, src_elem_ref_type, dst_elem_type,
  12632. dst_elem_ref_type, module->types,
  12633. module->type_count)) {
  12634. set_error_buf(error_buf, error_buf_size,
  12635. "array types do not match");
  12636. goto fail;
  12637. }
  12638. break;
  12639. }
  12640. case WASM_OP_REF_I31:
  12641. {
  12642. POP_I32();
  12643. wasm_set_refheaptype_common(
  12644. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12645. PUSH_REF(wasm_ref_type.ref_type);
  12646. break;
  12647. }
  12648. case WASM_OP_I31_GET_S:
  12649. case WASM_OP_I31_GET_U:
  12650. {
  12651. POP_REF(REF_TYPE_I31REF);
  12652. PUSH_I32();
  12653. break;
  12654. }
  12655. case WASM_OP_REF_TEST:
  12656. case WASM_OP_REF_CAST:
  12657. case WASM_OP_REF_TEST_NULLABLE:
  12658. case WASM_OP_REF_CAST_NULLABLE:
  12659. {
  12660. uint8 type;
  12661. read_leb_int32(p, p_end, heap_type);
  12662. #if WASM_ENABLE_FAST_INTERP != 0
  12663. emit_uint32(loader_ctx, (uint32)heap_type);
  12664. #endif
  12665. if (heap_type >= 0) {
  12666. if (!check_type_index(module, module->type_count,
  12667. heap_type, error_buf,
  12668. error_buf_size)) {
  12669. goto fail;
  12670. }
  12671. }
  12672. else {
  12673. if (!wasm_is_valid_heap_type(heap_type)) {
  12674. set_error_buf(error_buf, error_buf_size,
  12675. "unknown type");
  12676. goto fail;
  12677. }
  12678. }
  12679. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12680. &wasm_ref_type, error_buf,
  12681. error_buf_size)) {
  12682. goto fail;
  12683. }
  12684. if (opcode1 == WASM_OP_REF_TEST
  12685. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12686. PUSH_I32();
  12687. else {
  12688. bool nullable =
  12689. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12690. : false;
  12691. if (heap_type >= 0 || !nullable) {
  12692. wasm_set_refheaptype_typeidx(
  12693. &wasm_ref_type.ref_ht_typeidx, nullable,
  12694. heap_type);
  12695. PUSH_REF(wasm_ref_type.ref_type);
  12696. }
  12697. else {
  12698. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12699. }
  12700. }
  12701. break;
  12702. }
  12703. case WASM_OP_BR_ON_CAST:
  12704. case WASM_OP_BR_ON_CAST_FAIL:
  12705. {
  12706. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12707. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12708. uint8 type_tmp, castflags;
  12709. uint32 depth;
  12710. int32 heap_type_dst;
  12711. bool src_nullable, dst_nullable;
  12712. CHECK_BUF(p, p_end, 1);
  12713. castflags = read_uint8(p);
  12714. #if WASM_ENABLE_FAST_INTERP != 0
  12715. /* Emit heap_type firstly */
  12716. emit_byte(loader_ctx, castflags);
  12717. #endif
  12718. p_org = p;
  12719. read_leb_uint32(p, p_end, depth);
  12720. read_leb_int32(p, p_end, heap_type);
  12721. #if WASM_ENABLE_FAST_INTERP != 0
  12722. /* Emit heap_type firstly */
  12723. emit_uint32(loader_ctx, (uint32)heap_type);
  12724. #endif
  12725. read_leb_int32(p, p_end, heap_type_dst);
  12726. #if WASM_ENABLE_FAST_INTERP != 0
  12727. /* Emit heap_type firstly */
  12728. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12729. #endif
  12730. (void)depth;
  12731. /*
  12732. * castflags should be 0~3:
  12733. * 0: (non-null, non-null)
  12734. * 1: (null, non-null)
  12735. * 2: (non-null, null)
  12736. * 3: (null, null)
  12737. */
  12738. if (castflags > 3) {
  12739. set_error_buf(error_buf, error_buf_size,
  12740. "invalid castflags");
  12741. break;
  12742. }
  12743. src_nullable =
  12744. (castflags == 1) || (castflags == 3) ? true : false;
  12745. dst_nullable =
  12746. (castflags == 2) || (castflags == 3) ? true : false;
  12747. /* Pop and backup the stack top's ref type */
  12748. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12749. &ref_type_tmp, error_buf,
  12750. error_buf_size)) {
  12751. goto fail;
  12752. }
  12753. /* The reference type rt1 must be valid */
  12754. if (!init_ref_type(module, &ref_type1, src_nullable,
  12755. heap_type, error_buf,
  12756. error_buf_size)) {
  12757. goto fail;
  12758. }
  12759. /* The reference type rt2 must be valid. */
  12760. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12761. heap_type_dst, error_buf,
  12762. error_buf_size)) {
  12763. goto fail;
  12764. }
  12765. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12766. &ref_type2);
  12767. /* The reference type rt2 must match rt1. */
  12768. if (!wasm_reftype_is_subtype_of(
  12769. ref_type2.ref_type, &ref_type2,
  12770. ref_type1.ref_type, &ref_type1, module->types,
  12771. module->type_count)) {
  12772. set_error_buf(error_buf, error_buf_size,
  12773. "type mismatch");
  12774. goto fail;
  12775. }
  12776. p = p_org;
  12777. /* Push ref type casted for branch block check */
  12778. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12779. /* The reference type rt2 must match rt′. */
  12780. type_tmp = ref_type2.ref_type;
  12781. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12782. bh_memcpy_s(
  12783. &wasm_ref_type,
  12784. wasm_reftype_struct_size(&ref_type2),
  12785. &ref_type2,
  12786. wasm_reftype_struct_size(&ref_type2));
  12787. }
  12788. }
  12789. else {
  12790. /* The reference type rt′1 must match rt′. */
  12791. type_tmp = ref_type_diff.ref_type;
  12792. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12793. bh_memcpy_s(
  12794. &wasm_ref_type,
  12795. wasm_reftype_struct_size(&ref_type_diff),
  12796. &ref_type_diff,
  12797. wasm_reftype_struct_size(&ref_type_diff));
  12798. }
  12799. }
  12800. PUSH_REF(type_tmp);
  12801. if (!(frame_csp_tmp = check_branch_block(
  12802. loader_ctx, &p, p_end, opcode, error_buf,
  12803. error_buf_size))) {
  12804. goto fail;
  12805. }
  12806. /* Ignore heap_types */
  12807. skip_leb_uint32(p, p_end);
  12808. skip_leb_uint32(p, p_end);
  12809. /* Restore the original stack top's ref type */
  12810. POP_REF(type_tmp);
  12811. #if WASM_ENABLE_FAST_INTERP != 0
  12812. /* Erase the opnd offset emitted by POP_REF() */
  12813. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12814. #endif
  12815. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12816. type_tmp = ref_type_diff.ref_type;
  12817. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12818. bh_memcpy_s(
  12819. &wasm_ref_type,
  12820. wasm_reftype_struct_size(&ref_type_diff),
  12821. &ref_type_diff,
  12822. wasm_reftype_struct_size(&ref_type_diff));
  12823. }
  12824. }
  12825. else {
  12826. type_tmp = ref_type2.ref_type;
  12827. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12828. bh_memcpy_s(
  12829. &wasm_ref_type,
  12830. wasm_reftype_struct_size(&ref_type2),
  12831. &ref_type2,
  12832. wasm_reftype_struct_size(&ref_type2));
  12833. }
  12834. }
  12835. PUSH_REF(type_tmp);
  12836. #if WASM_ENABLE_FAST_INTERP != 0
  12837. /* Erase the opnd offset emitted by PUSH_REF() */
  12838. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12839. #endif
  12840. break;
  12841. }
  12842. case WASM_OP_ANY_CONVERT_EXTERN:
  12843. {
  12844. uint8 type;
  12845. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12846. &wasm_ref_type, error_buf,
  12847. error_buf_size)) {
  12848. goto fail;
  12849. }
  12850. if (!(type == REF_TYPE_EXTERNREF
  12851. || (type == REF_TYPE_HT_NON_NULLABLE
  12852. && wasm_ref_type.ref_ht_common.heap_type
  12853. == HEAP_TYPE_EXTERN)
  12854. || type == VALUE_TYPE_ANY)) {
  12855. set_error_buf(error_buf, error_buf_size,
  12856. "type mismatch");
  12857. goto fail;
  12858. }
  12859. if (type == REF_TYPE_EXTERNREF)
  12860. type = REF_TYPE_ANYREF;
  12861. else {
  12862. wasm_ref_type.ref_ht_common.heap_type =
  12863. HEAP_TYPE_ANY;
  12864. }
  12865. PUSH_REF(type);
  12866. break;
  12867. }
  12868. case WASM_OP_EXTERN_CONVERT_ANY:
  12869. {
  12870. uint8 type;
  12871. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12872. &wasm_ref_type, error_buf,
  12873. error_buf_size)) {
  12874. goto fail;
  12875. }
  12876. if (type == REF_TYPE_EXTERNREF
  12877. || ((type == REF_TYPE_HT_NULLABLE
  12878. || type == REF_TYPE_HT_NON_NULLABLE)
  12879. && wasm_ref_type.ref_ht_common.heap_type
  12880. == HEAP_TYPE_EXTERN)) {
  12881. set_error_buf(error_buf, error_buf_size,
  12882. "type mismatch");
  12883. goto fail;
  12884. }
  12885. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12886. /* push (ref null extern) */
  12887. type = REF_TYPE_EXTERNREF;
  12888. }
  12889. else {
  12890. /* push (ref extern) */
  12891. type = REF_TYPE_HT_NON_NULLABLE;
  12892. wasm_set_refheaptype_common(
  12893. &wasm_ref_type.ref_ht_common, false,
  12894. HEAP_TYPE_EXTERN);
  12895. }
  12896. PUSH_REF(type);
  12897. break;
  12898. }
  12899. #if WASM_ENABLE_STRINGREF != 0
  12900. case WASM_OP_STRING_NEW_UTF8:
  12901. case WASM_OP_STRING_NEW_WTF16:
  12902. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12903. case WASM_OP_STRING_NEW_WTF8:
  12904. {
  12905. uint32 memidx;
  12906. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12907. func->has_memory_operations = true;
  12908. #endif
  12909. read_leb_uint32(p, p_end, memidx);
  12910. #if WASM_ENABLE_FAST_INTERP != 0
  12911. emit_uint32(loader_ctx, (uint32)memidx);
  12912. #endif
  12913. POP_I32();
  12914. POP_I32();
  12915. PUSH_REF(REF_TYPE_STRINGREF);
  12916. (void)memidx;
  12917. break;
  12918. }
  12919. case WASM_OP_STRING_CONST:
  12920. {
  12921. uint32 contents;
  12922. read_leb_uint32(p, p_end, contents);
  12923. #if WASM_ENABLE_FAST_INTERP != 0
  12924. emit_uint32(loader_ctx, (uint32)contents);
  12925. #endif
  12926. PUSH_REF(REF_TYPE_STRINGREF);
  12927. (void)contents;
  12928. break;
  12929. }
  12930. case WASM_OP_STRING_MEASURE_UTF8:
  12931. case WASM_OP_STRING_MEASURE_WTF8:
  12932. case WASM_OP_STRING_MEASURE_WTF16:
  12933. {
  12934. POP_STRINGREF();
  12935. PUSH_I32();
  12936. break;
  12937. }
  12938. case WASM_OP_STRING_ENCODE_UTF8:
  12939. case WASM_OP_STRING_ENCODE_WTF16:
  12940. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12941. case WASM_OP_STRING_ENCODE_WTF8:
  12942. {
  12943. uint32 memidx;
  12944. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12945. func->has_memory_operations = true;
  12946. #endif
  12947. read_leb_uint32(p, p_end, memidx);
  12948. #if WASM_ENABLE_FAST_INTERP != 0
  12949. emit_uint32(loader_ctx, (uint32)memidx);
  12950. #endif
  12951. POP_I32();
  12952. POP_STRINGREF();
  12953. PUSH_I32();
  12954. (void)memidx;
  12955. break;
  12956. }
  12957. case WASM_OP_STRING_CONCAT:
  12958. {
  12959. POP_STRINGREF();
  12960. POP_STRINGREF();
  12961. PUSH_REF(REF_TYPE_STRINGREF);
  12962. break;
  12963. }
  12964. case WASM_OP_STRING_EQ:
  12965. {
  12966. POP_STRINGREF();
  12967. POP_STRINGREF();
  12968. PUSH_I32();
  12969. break;
  12970. }
  12971. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12972. {
  12973. POP_STRINGREF();
  12974. PUSH_I32();
  12975. break;
  12976. }
  12977. case WASM_OP_STRING_AS_WTF8:
  12978. {
  12979. POP_STRINGREF();
  12980. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12981. break;
  12982. }
  12983. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12984. {
  12985. POP_I32();
  12986. POP_I32();
  12987. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12988. PUSH_I32();
  12989. break;
  12990. }
  12991. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12992. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12993. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12994. {
  12995. uint32 memidx;
  12996. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12997. func->has_memory_operations = true;
  12998. #endif
  12999. read_leb_uint32(p, p_end, memidx);
  13000. #if WASM_ENABLE_FAST_INTERP != 0
  13001. emit_uint32(loader_ctx, (uint32)memidx);
  13002. #endif
  13003. POP_I32();
  13004. POP_I32();
  13005. POP_I32();
  13006. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13007. PUSH_I32();
  13008. PUSH_I32();
  13009. (void)memidx;
  13010. break;
  13011. }
  13012. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13013. {
  13014. POP_I32();
  13015. POP_I32();
  13016. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13017. PUSH_REF(REF_TYPE_STRINGREF);
  13018. break;
  13019. }
  13020. case WASM_OP_STRING_AS_WTF16:
  13021. {
  13022. POP_STRINGREF();
  13023. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13024. break;
  13025. }
  13026. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13027. {
  13028. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13029. PUSH_I32();
  13030. break;
  13031. }
  13032. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13033. {
  13034. POP_I32();
  13035. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13036. PUSH_I32();
  13037. break;
  13038. }
  13039. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13040. {
  13041. uint32 memidx;
  13042. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13043. func->has_memory_operations = true;
  13044. #endif
  13045. read_leb_uint32(p, p_end, memidx);
  13046. #if WASM_ENABLE_FAST_INTERP != 0
  13047. emit_uint32(loader_ctx, (uint32)memidx);
  13048. #endif
  13049. POP_I32();
  13050. POP_I32();
  13051. POP_I32();
  13052. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13053. PUSH_I32();
  13054. (void)memidx;
  13055. break;
  13056. }
  13057. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13058. {
  13059. POP_I32();
  13060. POP_I32();
  13061. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13062. PUSH_REF(REF_TYPE_STRINGREF);
  13063. break;
  13064. }
  13065. case WASM_OP_STRING_AS_ITER:
  13066. {
  13067. POP_STRINGREF();
  13068. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13069. break;
  13070. }
  13071. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13072. {
  13073. POP_REF(REF_TYPE_STRINGVIEWITER);
  13074. PUSH_I32();
  13075. break;
  13076. }
  13077. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13078. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13079. {
  13080. POP_I32();
  13081. POP_REF(REF_TYPE_STRINGVIEWITER);
  13082. PUSH_I32();
  13083. break;
  13084. }
  13085. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13086. {
  13087. POP_I32();
  13088. POP_REF(REF_TYPE_STRINGVIEWITER);
  13089. PUSH_REF(REF_TYPE_STRINGREF);
  13090. break;
  13091. }
  13092. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13093. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13094. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13095. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13096. {
  13097. POP_I32();
  13098. POP_I32();
  13099. POP_REF(REF_TYPE_ARRAYREF);
  13100. PUSH_REF(REF_TYPE_STRINGREF);
  13101. break;
  13102. }
  13103. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13104. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13105. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13106. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13107. {
  13108. POP_I32();
  13109. POP_REF(REF_TYPE_ARRAYREF);
  13110. POP_STRINGREF();
  13111. PUSH_I32();
  13112. break;
  13113. }
  13114. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13115. default:
  13116. set_error_buf_v(error_buf, error_buf_size,
  13117. "%s %02x %02x", "unsupported opcode",
  13118. 0xfb, opcode1);
  13119. goto fail;
  13120. }
  13121. break;
  13122. }
  13123. #endif /* end of WASM_ENABLE_GC != 0 */
  13124. case WASM_OP_MISC_PREFIX:
  13125. {
  13126. uint32 opcode1;
  13127. read_leb_uint32(p, p_end, opcode1);
  13128. #if WASM_ENABLE_FAST_INTERP != 0
  13129. emit_byte(loader_ctx, ((uint8)opcode1));
  13130. #endif
  13131. switch (opcode1) {
  13132. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13133. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13134. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13135. break;
  13136. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13137. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13138. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13139. break;
  13140. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13141. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13142. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13143. break;
  13144. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13145. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13146. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13147. break;
  13148. #if WASM_ENABLE_BULK_MEMORY != 0
  13149. case WASM_OP_MEMORY_INIT:
  13150. {
  13151. read_leb_uint32(p, p_end, data_seg_idx);
  13152. #if WASM_ENABLE_FAST_INTERP != 0
  13153. emit_uint32(loader_ctx, data_seg_idx);
  13154. #endif
  13155. if (module->import_memory_count == 0
  13156. && module->memory_count == 0)
  13157. goto fail_unknown_memory;
  13158. if (*p++ != 0x00)
  13159. goto fail_zero_byte_expected;
  13160. if (data_seg_idx >= module->data_seg_count) {
  13161. set_error_buf_v(error_buf, error_buf_size,
  13162. "unknown data segment %d",
  13163. data_seg_idx);
  13164. goto fail;
  13165. }
  13166. if (module->data_seg_count1 == 0)
  13167. goto fail_data_cnt_sec_require;
  13168. POP_I32();
  13169. POP_I32();
  13170. POP_MEM_OFFSET();
  13171. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13172. func->has_memory_operations = true;
  13173. #endif
  13174. #if WASM_ENABLE_WAMR_COMPILER != 0
  13175. module->is_bulk_memory_used = true;
  13176. #endif
  13177. break;
  13178. }
  13179. case WASM_OP_DATA_DROP:
  13180. {
  13181. read_leb_uint32(p, p_end, data_seg_idx);
  13182. #if WASM_ENABLE_FAST_INTERP != 0
  13183. emit_uint32(loader_ctx, data_seg_idx);
  13184. #endif
  13185. if (data_seg_idx >= module->data_seg_count) {
  13186. set_error_buf(error_buf, error_buf_size,
  13187. "unknown data segment");
  13188. goto fail;
  13189. }
  13190. if (module->data_seg_count1 == 0)
  13191. goto fail_data_cnt_sec_require;
  13192. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13193. func->has_memory_operations = true;
  13194. #endif
  13195. #if WASM_ENABLE_WAMR_COMPILER != 0
  13196. module->is_bulk_memory_used = true;
  13197. #endif
  13198. break;
  13199. }
  13200. case WASM_OP_MEMORY_COPY:
  13201. {
  13202. CHECK_BUF(p, p_end, sizeof(int16));
  13203. /* both src and dst memory index should be 0 */
  13204. if (*(int16 *)p != 0x0000)
  13205. goto fail_zero_byte_expected;
  13206. p += 2;
  13207. if (module->import_memory_count == 0
  13208. && module->memory_count == 0)
  13209. goto fail_unknown_memory;
  13210. POP_MEM_OFFSET();
  13211. POP_MEM_OFFSET();
  13212. POP_MEM_OFFSET();
  13213. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13214. func->has_memory_operations = true;
  13215. #endif
  13216. #if WASM_ENABLE_WAMR_COMPILER != 0
  13217. module->is_bulk_memory_used = true;
  13218. #endif
  13219. break;
  13220. }
  13221. case WASM_OP_MEMORY_FILL:
  13222. {
  13223. if (*p++ != 0x00) {
  13224. goto fail_zero_byte_expected;
  13225. }
  13226. if (module->import_memory_count == 0
  13227. && module->memory_count == 0) {
  13228. goto fail_unknown_memory;
  13229. }
  13230. POP_MEM_OFFSET();
  13231. POP_I32();
  13232. POP_MEM_OFFSET();
  13233. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13234. func->has_memory_operations = true;
  13235. #endif
  13236. #if WASM_ENABLE_WAMR_COMPILER != 0
  13237. module->is_bulk_memory_used = true;
  13238. #endif
  13239. break;
  13240. }
  13241. fail_zero_byte_expected:
  13242. set_error_buf(error_buf, error_buf_size,
  13243. "zero byte expected");
  13244. goto fail;
  13245. fail_unknown_memory:
  13246. set_error_buf(error_buf, error_buf_size,
  13247. "unknown memory 0");
  13248. goto fail;
  13249. fail_data_cnt_sec_require:
  13250. set_error_buf(error_buf, error_buf_size,
  13251. "data count section required");
  13252. goto fail;
  13253. #endif /* WASM_ENABLE_BULK_MEMORY */
  13254. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13255. case WASM_OP_TABLE_INIT:
  13256. {
  13257. uint8 seg_type = 0, tbl_type = 0;
  13258. #if WASM_ENABLE_GC != 0
  13259. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13260. #endif
  13261. read_leb_uint32(p, p_end, table_seg_idx);
  13262. read_leb_uint32(p, p_end, table_idx);
  13263. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13264. #if WASM_ENABLE_GC != 0
  13265. (void **)&tbl_ref_type,
  13266. #else
  13267. NULL,
  13268. #endif
  13269. error_buf, error_buf_size))
  13270. goto fail;
  13271. if (!get_table_seg_elem_type(module, table_seg_idx,
  13272. &seg_type,
  13273. #if WASM_ENABLE_GC != 0
  13274. (void **)&seg_ref_type,
  13275. #else
  13276. NULL,
  13277. #endif
  13278. error_buf, error_buf_size))
  13279. goto fail;
  13280. #if WASM_ENABLE_GC == 0
  13281. if (seg_type != tbl_type) {
  13282. set_error_buf(error_buf, error_buf_size,
  13283. "type mismatch");
  13284. goto fail;
  13285. }
  13286. #else
  13287. if (!wasm_reftype_is_subtype_of(
  13288. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13289. module->types, module->type_count)) {
  13290. set_error_buf(error_buf, error_buf_size,
  13291. "type mismatch");
  13292. goto fail;
  13293. }
  13294. #endif
  13295. #if WASM_ENABLE_FAST_INTERP != 0
  13296. emit_uint32(loader_ctx, table_seg_idx);
  13297. emit_uint32(loader_ctx, table_idx);
  13298. #endif
  13299. POP_I32();
  13300. POP_I32();
  13301. POP_I32();
  13302. #if WASM_ENABLE_WAMR_COMPILER != 0
  13303. module->is_ref_types_used = true;
  13304. #endif
  13305. break;
  13306. }
  13307. case WASM_OP_ELEM_DROP:
  13308. {
  13309. read_leb_uint32(p, p_end, table_seg_idx);
  13310. if (!get_table_seg_elem_type(module, table_seg_idx,
  13311. NULL, NULL, error_buf,
  13312. error_buf_size))
  13313. goto fail;
  13314. #if WASM_ENABLE_FAST_INTERP != 0
  13315. emit_uint32(loader_ctx, table_seg_idx);
  13316. #endif
  13317. #if WASM_ENABLE_WAMR_COMPILER != 0
  13318. module->is_ref_types_used = true;
  13319. #endif
  13320. break;
  13321. }
  13322. case WASM_OP_TABLE_COPY:
  13323. {
  13324. uint8 src_type, dst_type;
  13325. #if WASM_ENABLE_GC != 0
  13326. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13327. #endif
  13328. uint32 src_tbl_idx, dst_tbl_idx;
  13329. read_leb_uint32(p, p_end, dst_tbl_idx);
  13330. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13331. #if WASM_ENABLE_GC != 0
  13332. (void **)&dst_ref_type,
  13333. #else
  13334. NULL,
  13335. #endif
  13336. error_buf, error_buf_size))
  13337. goto fail;
  13338. read_leb_uint32(p, p_end, src_tbl_idx);
  13339. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13340. #if WASM_ENABLE_GC != 0
  13341. (void **)&src_ref_type,
  13342. #else
  13343. NULL,
  13344. #endif
  13345. error_buf, error_buf_size))
  13346. goto fail;
  13347. #if WASM_ENABLE_GC == 0
  13348. if (src_type != dst_type) {
  13349. set_error_buf(error_buf, error_buf_size,
  13350. "type mismatch");
  13351. goto fail;
  13352. }
  13353. #else
  13354. if (!wasm_reftype_is_subtype_of(
  13355. src_type, src_ref_type, dst_type, dst_ref_type,
  13356. module->types, module->type_count)) {
  13357. set_error_buf(error_buf, error_buf_size,
  13358. "type mismatch");
  13359. goto fail;
  13360. }
  13361. #endif
  13362. #if WASM_ENABLE_FAST_INTERP != 0
  13363. emit_uint32(loader_ctx, dst_tbl_idx);
  13364. emit_uint32(loader_ctx, src_tbl_idx);
  13365. #endif
  13366. POP_I32();
  13367. POP_I32();
  13368. POP_I32();
  13369. #if WASM_ENABLE_WAMR_COMPILER != 0
  13370. module->is_ref_types_used = true;
  13371. #endif
  13372. break;
  13373. }
  13374. case WASM_OP_TABLE_SIZE:
  13375. {
  13376. read_leb_uint32(p, p_end, table_idx);
  13377. /* TODO: shall we create a new function to check
  13378. table idx instead of using below function? */
  13379. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13380. error_buf, error_buf_size))
  13381. goto fail;
  13382. #if WASM_ENABLE_FAST_INTERP != 0
  13383. emit_uint32(loader_ctx, table_idx);
  13384. #endif
  13385. PUSH_I32();
  13386. #if WASM_ENABLE_WAMR_COMPILER != 0
  13387. module->is_ref_types_used = true;
  13388. #endif
  13389. break;
  13390. }
  13391. case WASM_OP_TABLE_GROW:
  13392. case WASM_OP_TABLE_FILL:
  13393. {
  13394. uint8 decl_type;
  13395. #if WASM_ENABLE_GC != 0
  13396. WASMRefType *ref_type = NULL;
  13397. #endif
  13398. read_leb_uint32(p, p_end, table_idx);
  13399. if (!get_table_elem_type(module, table_idx, &decl_type,
  13400. #if WASM_ENABLE_GC != 0
  13401. (void **)&ref_type,
  13402. #else
  13403. NULL,
  13404. #endif
  13405. error_buf, error_buf_size))
  13406. goto fail;
  13407. #if WASM_ENABLE_GC != 0
  13408. if (wasm_is_type_multi_byte_type(decl_type)) {
  13409. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13410. ref_type,
  13411. wasm_reftype_struct_size(ref_type));
  13412. }
  13413. #endif
  13414. if (opcode1 == WASM_OP_TABLE_GROW) {
  13415. if (table_idx < module->import_table_count) {
  13416. module->import_tables[table_idx]
  13417. .u.table.table_type.possible_grow = true;
  13418. }
  13419. else {
  13420. module
  13421. ->tables[table_idx
  13422. - module->import_table_count]
  13423. .table_type.possible_grow = true;
  13424. }
  13425. }
  13426. #if WASM_ENABLE_FAST_INTERP != 0
  13427. emit_uint32(loader_ctx, table_idx);
  13428. #endif
  13429. POP_I32();
  13430. #if WASM_ENABLE_FAST_INTERP != 0
  13431. POP_OFFSET_TYPE(decl_type);
  13432. #endif
  13433. POP_TYPE(decl_type);
  13434. if (opcode1 == WASM_OP_TABLE_GROW)
  13435. PUSH_I32();
  13436. else
  13437. POP_I32();
  13438. #if WASM_ENABLE_WAMR_COMPILER != 0
  13439. module->is_ref_types_used = true;
  13440. #endif
  13441. break;
  13442. }
  13443. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13444. default:
  13445. set_error_buf_v(error_buf, error_buf_size,
  13446. "%s %02x %02x", "unsupported opcode",
  13447. 0xfc, opcode1);
  13448. goto fail;
  13449. }
  13450. break;
  13451. }
  13452. #if WASM_ENABLE_SIMD != 0
  13453. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13454. case WASM_OP_SIMD_PREFIX:
  13455. {
  13456. /* TODO: memory64 offset type changes */
  13457. uint32 opcode1;
  13458. #if WASM_ENABLE_WAMR_COMPILER != 0
  13459. /* Mark the SIMD instruction is used in this module */
  13460. module->is_simd_used = true;
  13461. #endif
  13462. read_leb_uint32(p, p_end, opcode1);
  13463. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13464. */
  13465. switch (opcode1) {
  13466. /* memory instruction */
  13467. case SIMD_v128_load:
  13468. case SIMD_v128_load8x8_s:
  13469. case SIMD_v128_load8x8_u:
  13470. case SIMD_v128_load16x4_s:
  13471. case SIMD_v128_load16x4_u:
  13472. case SIMD_v128_load32x2_s:
  13473. case SIMD_v128_load32x2_u:
  13474. case SIMD_v128_load8_splat:
  13475. case SIMD_v128_load16_splat:
  13476. case SIMD_v128_load32_splat:
  13477. case SIMD_v128_load64_splat:
  13478. {
  13479. CHECK_MEMORY();
  13480. read_leb_uint32(p, p_end, align); /* align */
  13481. if (!check_simd_memory_access_align(
  13482. opcode1, align, error_buf, error_buf_size)) {
  13483. goto fail;
  13484. }
  13485. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13486. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13487. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13488. func->has_memory_operations = true;
  13489. #endif
  13490. break;
  13491. }
  13492. case SIMD_v128_store:
  13493. {
  13494. CHECK_MEMORY();
  13495. read_leb_uint32(p, p_end, align); /* align */
  13496. if (!check_simd_memory_access_align(
  13497. opcode1, align, error_buf, error_buf_size)) {
  13498. goto fail;
  13499. }
  13500. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13501. POP_V128();
  13502. POP_MEM_OFFSET();
  13503. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13504. func->has_memory_operations = true;
  13505. #endif
  13506. break;
  13507. }
  13508. /* basic operation */
  13509. case SIMD_v128_const:
  13510. {
  13511. CHECK_BUF1(p, p_end, 16);
  13512. p += 16;
  13513. PUSH_V128();
  13514. break;
  13515. }
  13516. case SIMD_v8x16_shuffle:
  13517. {
  13518. V128 mask;
  13519. CHECK_BUF1(p, p_end, 16);
  13520. mask = read_i8x16(p, error_buf, error_buf_size);
  13521. p += 16;
  13522. if (!check_simd_shuffle_mask(mask, error_buf,
  13523. error_buf_size)) {
  13524. goto fail;
  13525. }
  13526. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13527. break;
  13528. }
  13529. case SIMD_v8x16_swizzle:
  13530. {
  13531. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13532. break;
  13533. }
  13534. /* splat operation */
  13535. case SIMD_i8x16_splat:
  13536. case SIMD_i16x8_splat:
  13537. case SIMD_i32x4_splat:
  13538. case SIMD_i64x2_splat:
  13539. case SIMD_f32x4_splat:
  13540. case SIMD_f64x2_splat:
  13541. {
  13542. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13543. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13544. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13545. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13546. VALUE_TYPE_V128);
  13547. break;
  13548. }
  13549. /* lane operation */
  13550. case SIMD_i8x16_extract_lane_s:
  13551. case SIMD_i8x16_extract_lane_u:
  13552. case SIMD_i8x16_replace_lane:
  13553. case SIMD_i16x8_extract_lane_s:
  13554. case SIMD_i16x8_extract_lane_u:
  13555. case SIMD_i16x8_replace_lane:
  13556. case SIMD_i32x4_extract_lane:
  13557. case SIMD_i32x4_replace_lane:
  13558. case SIMD_i64x2_extract_lane:
  13559. case SIMD_i64x2_replace_lane:
  13560. case SIMD_f32x4_extract_lane:
  13561. case SIMD_f32x4_replace_lane:
  13562. case SIMD_f64x2_extract_lane:
  13563. case SIMD_f64x2_replace_lane:
  13564. {
  13565. uint8 lane;
  13566. /* clang-format off */
  13567. uint8 replace[] = {
  13568. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13569. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13570. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13571. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13572. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13573. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13574. };
  13575. uint8 push_type[] = {
  13576. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13577. VALUE_TYPE_V128,
  13578. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13579. VALUE_TYPE_V128,
  13580. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13581. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13582. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13583. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13584. };
  13585. /* clang-format on */
  13586. CHECK_BUF(p, p_end, 1);
  13587. lane = read_uint8(p);
  13588. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13589. error_buf_size)) {
  13590. goto fail;
  13591. }
  13592. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13593. if (!(wasm_loader_pop_frame_ref(
  13594. loader_ctx,
  13595. replace[opcode1
  13596. - SIMD_i8x16_extract_lane_s],
  13597. error_buf, error_buf_size)))
  13598. goto fail;
  13599. }
  13600. POP_AND_PUSH(
  13601. VALUE_TYPE_V128,
  13602. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13603. break;
  13604. }
  13605. /* i8x16 compare operation */
  13606. case SIMD_i8x16_eq:
  13607. case SIMD_i8x16_ne:
  13608. case SIMD_i8x16_lt_s:
  13609. case SIMD_i8x16_lt_u:
  13610. case SIMD_i8x16_gt_s:
  13611. case SIMD_i8x16_gt_u:
  13612. case SIMD_i8x16_le_s:
  13613. case SIMD_i8x16_le_u:
  13614. case SIMD_i8x16_ge_s:
  13615. case SIMD_i8x16_ge_u:
  13616. /* i16x8 compare operation */
  13617. case SIMD_i16x8_eq:
  13618. case SIMD_i16x8_ne:
  13619. case SIMD_i16x8_lt_s:
  13620. case SIMD_i16x8_lt_u:
  13621. case SIMD_i16x8_gt_s:
  13622. case SIMD_i16x8_gt_u:
  13623. case SIMD_i16x8_le_s:
  13624. case SIMD_i16x8_le_u:
  13625. case SIMD_i16x8_ge_s:
  13626. case SIMD_i16x8_ge_u:
  13627. /* i32x4 compare operation */
  13628. case SIMD_i32x4_eq:
  13629. case SIMD_i32x4_ne:
  13630. case SIMD_i32x4_lt_s:
  13631. case SIMD_i32x4_lt_u:
  13632. case SIMD_i32x4_gt_s:
  13633. case SIMD_i32x4_gt_u:
  13634. case SIMD_i32x4_le_s:
  13635. case SIMD_i32x4_le_u:
  13636. case SIMD_i32x4_ge_s:
  13637. case SIMD_i32x4_ge_u:
  13638. /* f32x4 compare operation */
  13639. case SIMD_f32x4_eq:
  13640. case SIMD_f32x4_ne:
  13641. case SIMD_f32x4_lt:
  13642. case SIMD_f32x4_gt:
  13643. case SIMD_f32x4_le:
  13644. case SIMD_f32x4_ge:
  13645. /* f64x2 compare operation */
  13646. case SIMD_f64x2_eq:
  13647. case SIMD_f64x2_ne:
  13648. case SIMD_f64x2_lt:
  13649. case SIMD_f64x2_gt:
  13650. case SIMD_f64x2_le:
  13651. case SIMD_f64x2_ge:
  13652. {
  13653. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13654. break;
  13655. }
  13656. /* v128 operation */
  13657. case SIMD_v128_not:
  13658. {
  13659. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13660. break;
  13661. }
  13662. case SIMD_v128_and:
  13663. case SIMD_v128_andnot:
  13664. case SIMD_v128_or:
  13665. case SIMD_v128_xor:
  13666. {
  13667. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13668. break;
  13669. }
  13670. case SIMD_v128_bitselect:
  13671. {
  13672. POP_V128();
  13673. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13674. break;
  13675. }
  13676. case SIMD_v128_any_true:
  13677. {
  13678. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13679. break;
  13680. }
  13681. /* Load Lane Operation */
  13682. case SIMD_v128_load8_lane:
  13683. case SIMD_v128_load16_lane:
  13684. case SIMD_v128_load32_lane:
  13685. case SIMD_v128_load64_lane:
  13686. case SIMD_v128_store8_lane:
  13687. case SIMD_v128_store16_lane:
  13688. case SIMD_v128_store32_lane:
  13689. case SIMD_v128_store64_lane:
  13690. {
  13691. uint8 lane;
  13692. CHECK_MEMORY();
  13693. read_leb_uint32(p, p_end, align); /* align */
  13694. if (!check_simd_memory_access_align(
  13695. opcode1, align, error_buf, error_buf_size)) {
  13696. goto fail;
  13697. }
  13698. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13699. CHECK_BUF(p, p_end, 1);
  13700. lane = read_uint8(p);
  13701. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13702. error_buf_size)) {
  13703. goto fail;
  13704. }
  13705. POP_V128();
  13706. POP_MEM_OFFSET();
  13707. if (opcode1 < SIMD_v128_store8_lane) {
  13708. PUSH_V128();
  13709. }
  13710. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13711. func->has_memory_operations = true;
  13712. #endif
  13713. break;
  13714. }
  13715. case SIMD_v128_load32_zero:
  13716. case SIMD_v128_load64_zero:
  13717. {
  13718. CHECK_MEMORY();
  13719. read_leb_uint32(p, p_end, align); /* align */
  13720. if (!check_simd_memory_access_align(
  13721. opcode1, align, error_buf, error_buf_size)) {
  13722. goto fail;
  13723. }
  13724. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13725. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13726. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13727. func->has_memory_operations = true;
  13728. #endif
  13729. break;
  13730. }
  13731. /* Float conversion */
  13732. case SIMD_f32x4_demote_f64x2_zero:
  13733. case SIMD_f64x2_promote_low_f32x4_zero:
  13734. {
  13735. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13736. break;
  13737. }
  13738. /* i8x16 Operation */
  13739. case SIMD_i8x16_abs:
  13740. case SIMD_i8x16_neg:
  13741. case SIMD_i8x16_popcnt:
  13742. {
  13743. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13744. break;
  13745. }
  13746. case SIMD_i8x16_all_true:
  13747. case SIMD_i8x16_bitmask:
  13748. {
  13749. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13750. break;
  13751. }
  13752. case SIMD_i8x16_narrow_i16x8_s:
  13753. case SIMD_i8x16_narrow_i16x8_u:
  13754. {
  13755. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13756. break;
  13757. }
  13758. case SIMD_f32x4_ceil:
  13759. case SIMD_f32x4_floor:
  13760. case SIMD_f32x4_trunc:
  13761. case SIMD_f32x4_nearest:
  13762. {
  13763. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13764. break;
  13765. }
  13766. case SIMD_i8x16_shl:
  13767. case SIMD_i8x16_shr_s:
  13768. case SIMD_i8x16_shr_u:
  13769. {
  13770. POP_I32();
  13771. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13772. break;
  13773. }
  13774. case SIMD_i8x16_add:
  13775. case SIMD_i8x16_add_sat_s:
  13776. case SIMD_i8x16_add_sat_u:
  13777. case SIMD_i8x16_sub:
  13778. case SIMD_i8x16_sub_sat_s:
  13779. case SIMD_i8x16_sub_sat_u:
  13780. {
  13781. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13782. break;
  13783. }
  13784. case SIMD_f64x2_ceil:
  13785. case SIMD_f64x2_floor:
  13786. {
  13787. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13788. break;
  13789. }
  13790. case SIMD_i8x16_min_s:
  13791. case SIMD_i8x16_min_u:
  13792. case SIMD_i8x16_max_s:
  13793. case SIMD_i8x16_max_u:
  13794. {
  13795. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13796. break;
  13797. }
  13798. case SIMD_f64x2_trunc:
  13799. {
  13800. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13801. break;
  13802. }
  13803. case SIMD_i8x16_avgr_u:
  13804. {
  13805. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13806. break;
  13807. }
  13808. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13809. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13810. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13811. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13812. /* i16x8 operation */
  13813. case SIMD_i16x8_abs:
  13814. case SIMD_i16x8_neg:
  13815. {
  13816. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13817. break;
  13818. }
  13819. case SIMD_i16x8_q15mulr_sat_s:
  13820. {
  13821. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13822. break;
  13823. }
  13824. case SIMD_i16x8_all_true:
  13825. case SIMD_i16x8_bitmask:
  13826. {
  13827. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13828. break;
  13829. }
  13830. case SIMD_i16x8_narrow_i32x4_s:
  13831. case SIMD_i16x8_narrow_i32x4_u:
  13832. {
  13833. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13834. break;
  13835. }
  13836. case SIMD_i16x8_extend_low_i8x16_s:
  13837. case SIMD_i16x8_extend_high_i8x16_s:
  13838. case SIMD_i16x8_extend_low_i8x16_u:
  13839. case SIMD_i16x8_extend_high_i8x16_u:
  13840. {
  13841. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13842. break;
  13843. }
  13844. case SIMD_i16x8_shl:
  13845. case SIMD_i16x8_shr_s:
  13846. case SIMD_i16x8_shr_u:
  13847. {
  13848. POP_I32();
  13849. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13850. break;
  13851. }
  13852. case SIMD_i16x8_add:
  13853. case SIMD_i16x8_add_sat_s:
  13854. case SIMD_i16x8_add_sat_u:
  13855. case SIMD_i16x8_sub:
  13856. case SIMD_i16x8_sub_sat_s:
  13857. case SIMD_i16x8_sub_sat_u:
  13858. {
  13859. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13860. break;
  13861. }
  13862. case SIMD_f64x2_nearest:
  13863. {
  13864. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13865. break;
  13866. }
  13867. case SIMD_i16x8_mul:
  13868. case SIMD_i16x8_min_s:
  13869. case SIMD_i16x8_min_u:
  13870. case SIMD_i16x8_max_s:
  13871. case SIMD_i16x8_max_u:
  13872. case SIMD_i16x8_avgr_u:
  13873. case SIMD_i16x8_extmul_low_i8x16_s:
  13874. case SIMD_i16x8_extmul_high_i8x16_s:
  13875. case SIMD_i16x8_extmul_low_i8x16_u:
  13876. case SIMD_i16x8_extmul_high_i8x16_u:
  13877. {
  13878. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13879. break;
  13880. }
  13881. /* i32x4 operation */
  13882. case SIMD_i32x4_abs:
  13883. case SIMD_i32x4_neg:
  13884. {
  13885. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13886. break;
  13887. }
  13888. case SIMD_i32x4_all_true:
  13889. case SIMD_i32x4_bitmask:
  13890. {
  13891. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13892. break;
  13893. }
  13894. case SIMD_i32x4_extend_low_i16x8_s:
  13895. case SIMD_i32x4_extend_high_i16x8_s:
  13896. case SIMD_i32x4_extend_low_i16x8_u:
  13897. case SIMD_i32x4_extend_high_i16x8_u:
  13898. {
  13899. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13900. break;
  13901. }
  13902. case SIMD_i32x4_shl:
  13903. case SIMD_i32x4_shr_s:
  13904. case SIMD_i32x4_shr_u:
  13905. {
  13906. POP_I32();
  13907. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13908. break;
  13909. }
  13910. case SIMD_i32x4_add:
  13911. case SIMD_i32x4_sub:
  13912. case SIMD_i32x4_mul:
  13913. case SIMD_i32x4_min_s:
  13914. case SIMD_i32x4_min_u:
  13915. case SIMD_i32x4_max_s:
  13916. case SIMD_i32x4_max_u:
  13917. case SIMD_i32x4_dot_i16x8_s:
  13918. case SIMD_i32x4_extmul_low_i16x8_s:
  13919. case SIMD_i32x4_extmul_high_i16x8_s:
  13920. case SIMD_i32x4_extmul_low_i16x8_u:
  13921. case SIMD_i32x4_extmul_high_i16x8_u:
  13922. {
  13923. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13924. break;
  13925. }
  13926. /* i64x2 operation */
  13927. case SIMD_i64x2_abs:
  13928. case SIMD_i64x2_neg:
  13929. {
  13930. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13931. break;
  13932. }
  13933. case SIMD_i64x2_all_true:
  13934. case SIMD_i64x2_bitmask:
  13935. {
  13936. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13937. break;
  13938. }
  13939. case SIMD_i64x2_extend_low_i32x4_s:
  13940. case SIMD_i64x2_extend_high_i32x4_s:
  13941. case SIMD_i64x2_extend_low_i32x4_u:
  13942. case SIMD_i64x2_extend_high_i32x4_u:
  13943. {
  13944. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13945. break;
  13946. }
  13947. case SIMD_i64x2_shl:
  13948. case SIMD_i64x2_shr_s:
  13949. case SIMD_i64x2_shr_u:
  13950. {
  13951. POP_I32();
  13952. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13953. break;
  13954. }
  13955. case SIMD_i64x2_add:
  13956. case SIMD_i64x2_sub:
  13957. case SIMD_i64x2_mul:
  13958. case SIMD_i64x2_eq:
  13959. case SIMD_i64x2_ne:
  13960. case SIMD_i64x2_lt_s:
  13961. case SIMD_i64x2_gt_s:
  13962. case SIMD_i64x2_le_s:
  13963. case SIMD_i64x2_ge_s:
  13964. case SIMD_i64x2_extmul_low_i32x4_s:
  13965. case SIMD_i64x2_extmul_high_i32x4_s:
  13966. case SIMD_i64x2_extmul_low_i32x4_u:
  13967. case SIMD_i64x2_extmul_high_i32x4_u:
  13968. {
  13969. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13970. break;
  13971. }
  13972. /* f32x4 operation */
  13973. case SIMD_f32x4_abs:
  13974. case SIMD_f32x4_neg:
  13975. case SIMD_f32x4_sqrt:
  13976. {
  13977. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13978. break;
  13979. }
  13980. case SIMD_f32x4_add:
  13981. case SIMD_f32x4_sub:
  13982. case SIMD_f32x4_mul:
  13983. case SIMD_f32x4_div:
  13984. case SIMD_f32x4_min:
  13985. case SIMD_f32x4_max:
  13986. case SIMD_f32x4_pmin:
  13987. case SIMD_f32x4_pmax:
  13988. {
  13989. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13990. break;
  13991. }
  13992. /* f64x2 operation */
  13993. case SIMD_f64x2_abs:
  13994. case SIMD_f64x2_neg:
  13995. case SIMD_f64x2_sqrt:
  13996. {
  13997. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13998. break;
  13999. }
  14000. case SIMD_f64x2_add:
  14001. case SIMD_f64x2_sub:
  14002. case SIMD_f64x2_mul:
  14003. case SIMD_f64x2_div:
  14004. case SIMD_f64x2_min:
  14005. case SIMD_f64x2_max:
  14006. case SIMD_f64x2_pmin:
  14007. case SIMD_f64x2_pmax:
  14008. {
  14009. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14010. break;
  14011. }
  14012. case SIMD_i32x4_trunc_sat_f32x4_s:
  14013. case SIMD_i32x4_trunc_sat_f32x4_u:
  14014. case SIMD_f32x4_convert_i32x4_s:
  14015. case SIMD_f32x4_convert_i32x4_u:
  14016. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14017. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14018. case SIMD_f64x2_convert_low_i32x4_s:
  14019. case SIMD_f64x2_convert_low_i32x4_u:
  14020. {
  14021. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14022. break;
  14023. }
  14024. default:
  14025. {
  14026. if (error_buf != NULL) {
  14027. snprintf(error_buf, error_buf_size,
  14028. "WASM module load failed: "
  14029. "invalid opcode 0xfd %02x.",
  14030. opcode1);
  14031. }
  14032. goto fail;
  14033. }
  14034. }
  14035. break;
  14036. }
  14037. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14038. #endif /* end of WASM_ENABLE_SIMD */
  14039. #if WASM_ENABLE_SHARED_MEMORY != 0
  14040. case WASM_OP_ATOMIC_PREFIX:
  14041. {
  14042. uint32 opcode1;
  14043. read_leb_uint32(p, p_end, opcode1);
  14044. #if WASM_ENABLE_FAST_INTERP != 0
  14045. emit_byte(loader_ctx, opcode1);
  14046. #endif
  14047. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14048. CHECK_MEMORY();
  14049. read_leb_uint32(p, p_end, align); /* align */
  14050. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14051. if (!check_memory_align_equal(opcode1, align, error_buf,
  14052. error_buf_size)) {
  14053. goto fail;
  14054. }
  14055. #if WASM_ENABLE_FAST_INTERP != 0
  14056. emit_uint32(loader_ctx, mem_offset);
  14057. #endif
  14058. }
  14059. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14060. func->has_memory_operations = true;
  14061. #endif
  14062. switch (opcode1) {
  14063. case WASM_OP_ATOMIC_NOTIFY:
  14064. POP_I32();
  14065. POP_MEM_OFFSET();
  14066. PUSH_I32();
  14067. break;
  14068. case WASM_OP_ATOMIC_WAIT32:
  14069. POP_I64();
  14070. POP_I32();
  14071. POP_MEM_OFFSET();
  14072. PUSH_I32();
  14073. break;
  14074. case WASM_OP_ATOMIC_WAIT64:
  14075. POP_I64();
  14076. POP_I64();
  14077. POP_MEM_OFFSET();
  14078. PUSH_I32();
  14079. break;
  14080. case WASM_OP_ATOMIC_FENCE:
  14081. /* reserved byte 0x00 */
  14082. if (*p++ != 0x00) {
  14083. set_error_buf(error_buf, error_buf_size,
  14084. "zero byte expected");
  14085. goto fail;
  14086. }
  14087. break;
  14088. case WASM_OP_ATOMIC_I32_LOAD:
  14089. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14090. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14091. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14092. break;
  14093. case WASM_OP_ATOMIC_I32_STORE:
  14094. case WASM_OP_ATOMIC_I32_STORE8:
  14095. case WASM_OP_ATOMIC_I32_STORE16:
  14096. POP_I32();
  14097. POP_MEM_OFFSET();
  14098. break;
  14099. case WASM_OP_ATOMIC_I64_LOAD:
  14100. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14101. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14102. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14103. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14104. break;
  14105. case WASM_OP_ATOMIC_I64_STORE:
  14106. case WASM_OP_ATOMIC_I64_STORE8:
  14107. case WASM_OP_ATOMIC_I64_STORE16:
  14108. case WASM_OP_ATOMIC_I64_STORE32:
  14109. POP_I64();
  14110. POP_MEM_OFFSET();
  14111. break;
  14112. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14113. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14114. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14115. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14116. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14117. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14118. case WASM_OP_ATOMIC_RMW_I32_AND:
  14119. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14120. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14121. case WASM_OP_ATOMIC_RMW_I32_OR:
  14122. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14123. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14124. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14125. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14126. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14127. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14128. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14129. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14130. POP_I32();
  14131. POP_MEM_OFFSET();
  14132. PUSH_I32();
  14133. break;
  14134. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14135. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14136. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14137. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14138. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14139. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14140. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14141. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14142. case WASM_OP_ATOMIC_RMW_I64_AND:
  14143. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14144. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14145. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14146. case WASM_OP_ATOMIC_RMW_I64_OR:
  14147. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14148. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14149. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14150. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14151. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14152. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14153. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14154. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14155. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14156. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14157. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14158. POP_I64();
  14159. POP_MEM_OFFSET();
  14160. PUSH_I64();
  14161. break;
  14162. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14163. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14164. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14165. POP_I32();
  14166. POP_I32();
  14167. POP_MEM_OFFSET();
  14168. PUSH_I32();
  14169. break;
  14170. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14171. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14172. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14173. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14174. POP_I64();
  14175. POP_I64();
  14176. POP_MEM_OFFSET();
  14177. PUSH_I64();
  14178. break;
  14179. default:
  14180. set_error_buf_v(error_buf, error_buf_size,
  14181. "%s %02x %02x", "unsupported opcode",
  14182. 0xfe, opcode1);
  14183. goto fail;
  14184. }
  14185. break;
  14186. }
  14187. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14188. default:
  14189. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14190. "unsupported opcode", opcode);
  14191. goto fail;
  14192. }
  14193. #if WASM_ENABLE_FAST_INTERP != 0
  14194. last_op = opcode;
  14195. #endif
  14196. }
  14197. if (loader_ctx->csp_num > 0) {
  14198. if (cur_func_idx < module->function_count - 1)
  14199. /* Function with missing end marker (between two functions) */
  14200. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14201. else
  14202. /* Function with missing end marker
  14203. (at EOF or end of code sections) */
  14204. set_error_buf(error_buf, error_buf_size,
  14205. "unexpected end of section or function, "
  14206. "or section size mismatch");
  14207. goto fail;
  14208. }
  14209. #if WASM_ENABLE_FAST_INTERP != 0
  14210. if (loader_ctx->p_code_compiled == NULL)
  14211. goto re_scan;
  14212. func->const_cell_num = loader_ctx->const_cell_num;
  14213. if (func->const_cell_num > 0) {
  14214. int32 j;
  14215. if (!(func->consts = func_const = loader_malloc(
  14216. func->const_cell_num * 4, error_buf, error_buf_size)))
  14217. goto fail;
  14218. func_const_end = func->consts + func->const_cell_num * 4;
  14219. /* reverse the const buf */
  14220. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14221. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14222. if (c->value_type == VALUE_TYPE_F64
  14223. || c->value_type == VALUE_TYPE_I64) {
  14224. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14225. &(c->value.f64), (uint32)sizeof(int64));
  14226. func_const += sizeof(int64);
  14227. }
  14228. else {
  14229. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14230. &(c->value.f32), (uint32)sizeof(int32));
  14231. func_const += sizeof(int32);
  14232. }
  14233. }
  14234. }
  14235. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14236. - loader_ctx->start_dynamic_offset + 1;
  14237. #else
  14238. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14239. #endif
  14240. func->max_block_num = loader_ctx->max_csp_num;
  14241. return_value = true;
  14242. fail:
  14243. wasm_loader_ctx_destroy(loader_ctx);
  14244. (void)table_idx;
  14245. (void)table_seg_idx;
  14246. (void)data_seg_idx;
  14247. (void)i64_const;
  14248. (void)local_offset;
  14249. (void)p_org;
  14250. (void)mem_offset;
  14251. (void)align;
  14252. return return_value;
  14253. }