wasm_loader.c 581 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815
  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. if (error_buf != NULL) {
  54. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  55. string);
  56. }
  57. }
  58. #if WASM_ENABLE_MEMORY64 != 0
  59. static void
  60. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  61. {
  62. if (error_buf != NULL) {
  63. snprintf(error_buf, error_buf_size, "offset out of range");
  64. }
  65. }
  66. #endif
  67. static void
  68. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  69. {
  70. va_list args;
  71. char buf[128];
  72. if (error_buf != NULL) {
  73. va_start(args, format);
  74. vsnprintf(buf, sizeof(buf), format, args);
  75. va_end(args);
  76. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  77. }
  78. }
  79. static bool
  80. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  81. char *error_buf, uint32 error_buf_size)
  82. {
  83. if ((uintptr_t)buf + length < (uintptr_t)buf
  84. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  85. set_error_buf(error_buf, error_buf_size,
  86. "unexpected end of section or function");
  87. return false;
  88. }
  89. return true;
  90. }
  91. static bool
  92. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  93. char *error_buf, uint32 error_buf_size)
  94. {
  95. if ((uintptr_t)buf + length < (uintptr_t)buf
  96. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  97. set_error_buf(error_buf, error_buf_size, "unexpected end");
  98. return false;
  99. }
  100. return true;
  101. }
  102. #define CHECK_BUF(buf, buf_end, length) \
  103. do { \
  104. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  105. goto fail; \
  106. } \
  107. } while (0)
  108. #define CHECK_BUF1(buf, buf_end, length) \
  109. do { \
  110. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  111. goto fail; \
  112. } \
  113. } while (0)
  114. #define skip_leb(p) while (*p++ & 0x80)
  115. #define skip_leb_int64(p, p_end) skip_leb(p)
  116. #define skip_leb_uint32(p, p_end) skip_leb(p)
  117. #define skip_leb_int32(p, p_end) skip_leb(p)
  118. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  119. static bool
  120. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  121. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  122. {
  123. const uint8 *buf = *p_buf;
  124. uint64 result = 0;
  125. uint32 shift = 0;
  126. uint32 offset = 0, bcnt = 0;
  127. uint64 byte;
  128. while (true) {
  129. /* uN or SN must not exceed ceil(N/7) bytes */
  130. if (bcnt + 1 > (maxbits + 6) / 7) {
  131. set_error_buf(error_buf, error_buf_size,
  132. "integer representation too long");
  133. return false;
  134. }
  135. CHECK_BUF(buf, buf_end, offset + 1);
  136. byte = buf[offset];
  137. offset += 1;
  138. result |= ((byte & 0x7f) << shift);
  139. shift += 7;
  140. bcnt += 1;
  141. if ((byte & 0x80) == 0) {
  142. break;
  143. }
  144. }
  145. if (!sign && maxbits == 32 && shift >= maxbits) {
  146. /* The top bits set represent values > 32 bits */
  147. if (((uint8)byte) & 0xf0)
  148. goto fail_integer_too_large;
  149. }
  150. else if (sign && maxbits == 32) {
  151. if (shift < maxbits) {
  152. /* Sign extend, second-highest bit is the sign bit */
  153. if ((uint8)byte & 0x40)
  154. result |= (~((uint64)0)) << shift;
  155. }
  156. else {
  157. /* The top bits should be a sign-extension of the sign bit */
  158. bool sign_bit_set = ((uint8)byte) & 0x8;
  159. int top_bits = ((uint8)byte) & 0xf0;
  160. if ((sign_bit_set && top_bits != 0x70)
  161. || (!sign_bit_set && top_bits != 0))
  162. goto fail_integer_too_large;
  163. }
  164. }
  165. else if (sign && maxbits == 64) {
  166. if (shift < maxbits) {
  167. /* Sign extend, second-highest bit is the sign bit */
  168. if ((uint8)byte & 0x40)
  169. result |= (~((uint64)0)) << shift;
  170. }
  171. else {
  172. /* The top bits should be a sign-extension of the sign bit */
  173. bool sign_bit_set = ((uint8)byte) & 0x1;
  174. int top_bits = ((uint8)byte) & 0xfe;
  175. if ((sign_bit_set && top_bits != 0x7e)
  176. || (!sign_bit_set && top_bits != 0))
  177. goto fail_integer_too_large;
  178. }
  179. }
  180. *p_buf += offset;
  181. *p_result = result;
  182. return true;
  183. fail_integer_too_large:
  184. set_error_buf(error_buf, error_buf_size, "integer too large");
  185. fail:
  186. return false;
  187. }
  188. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  189. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  190. #define read_leb_int64(p, p_end, res) \
  191. do { \
  192. uint64 res64; \
  193. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  194. error_buf_size)) \
  195. goto fail; \
  196. res = (int64)res64; \
  197. } while (0)
  198. #if WASM_ENABLE_MEMORY64 != 0
  199. #define read_leb_mem_offset(p, p_end, res) \
  200. do { \
  201. uint64 res64; \
  202. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  203. &res64, error_buf, error_buf_size)) { \
  204. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  205. goto fail; \
  206. } \
  207. res = (mem_offset_t)res64; \
  208. } while (0)
  209. #else
  210. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  211. #endif
  212. #define read_leb_uint32(p, p_end, res) \
  213. do { \
  214. uint64 res64; \
  215. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  216. error_buf_size)) \
  217. goto fail; \
  218. res = (uint32)res64; \
  219. } while (0)
  220. #define read_leb_int32(p, p_end, res) \
  221. do { \
  222. uint64 res64; \
  223. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  224. error_buf_size)) \
  225. goto fail; \
  226. res = (int32)res64; \
  227. } while (0)
  228. static char *
  229. type2str(uint8 type)
  230. {
  231. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  232. #if WASM_ENABLE_GC != 0
  233. char *type_str_ref[] = { "stringview_iter",
  234. "stringview_wtf16",
  235. "(ref null ht)",
  236. "(ref ht)",
  237. "", /* reserved */
  238. "stringview_wtf8",
  239. "stringref",
  240. "", /* reserved */
  241. "", /* reserved */
  242. "arrayref",
  243. "structref",
  244. "i32ref",
  245. "eqref",
  246. "anyref",
  247. "externref",
  248. "funcref",
  249. "nullref",
  250. "nullexternref",
  251. "nullfuncref" };
  252. #endif
  253. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  254. return type_str[type - VALUE_TYPE_V128];
  255. #if WASM_ENABLE_GC != 0
  256. else if (wasm_is_type_reftype(type))
  257. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  258. #endif
  259. else if (type == VALUE_TYPE_FUNCREF)
  260. return "funcref";
  261. else if (type == VALUE_TYPE_EXTERNREF)
  262. return "externref";
  263. else
  264. return "unknown type";
  265. }
  266. static bool
  267. is_32bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I32
  270. || type == VALUE_TYPE_F32
  271. /* the operand stack is in polymorphic state */
  272. || type == VALUE_TYPE_ANY
  273. #if WASM_ENABLE_GC != 0
  274. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  275. #elif WASM_ENABLE_REF_TYPES != 0
  276. /* For reference types, we use uint32 index to represent
  277. the funcref and externref */
  278. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  279. #endif
  280. )
  281. return true;
  282. return false;
  283. }
  284. static bool
  285. is_64bit_type(uint8 type)
  286. {
  287. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  288. #if WASM_ENABLE_GC != 0
  289. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  290. #endif
  291. )
  292. return true;
  293. return false;
  294. }
  295. static bool
  296. is_value_type(uint8 type)
  297. {
  298. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  299. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  300. #if WASM_ENABLE_GC != 0
  301. /* reference types, 0x65 to 0x70 */
  302. || wasm_is_type_reftype(type)
  303. #elif WASM_ENABLE_REF_TYPES != 0
  304. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  305. #endif
  306. #if WASM_ENABLE_SIMD != 0
  307. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  308. || type == VALUE_TYPE_V128 /* 0x7B */
  309. #endif
  310. #endif
  311. )
  312. return true;
  313. return false;
  314. }
  315. #if WASM_ENABLE_GC != 0
  316. static bool
  317. is_packed_type(uint8 type)
  318. {
  319. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  320. }
  321. #endif
  322. static bool
  323. is_byte_a_type(uint8 type)
  324. {
  325. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  326. }
  327. #if WASM_ENABLE_SIMD != 0
  328. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  329. static V128
  330. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  331. {
  332. V128 result;
  333. uint8 i;
  334. for (i = 0; i != 16; ++i) {
  335. result.i8x16[i] = read_uint8(p_buf);
  336. }
  337. return result;
  338. }
  339. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  340. #endif /* end of WASM_ENABLE_SIMD */
  341. static void *
  342. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  343. {
  344. void *mem;
  345. if (size >= WASM_MEM_ALLOC_MAX_SIZE
  346. || !(mem = wasm_runtime_malloc((uint32)size))) {
  347. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  348. return NULL;
  349. }
  350. memset(mem, 0, (uint32)size);
  351. return mem;
  352. }
  353. static void *
  354. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  355. uint32 error_buf_size)
  356. {
  357. uint8 *mem_new;
  358. bh_assert(size_new > size_old);
  359. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  360. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  361. memset(mem_new + size_old, 0, size_new - size_old);
  362. wasm_runtime_free(mem_old);
  363. }
  364. return mem_new;
  365. }
  366. #define MEM_REALLOC(mem, size_old, size_new) \
  367. do { \
  368. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  369. error_buf_size); \
  370. if (!mem_new) \
  371. goto fail; \
  372. mem = mem_new; \
  373. } while (0)
  374. #if WASM_ENABLE_GC != 0
  375. static bool
  376. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  377. char *error_buf, uint32 error_buf_size)
  378. {
  379. if (type_index >= type_count) {
  380. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  381. type_index);
  382. return false;
  383. }
  384. return true;
  385. }
  386. static bool
  387. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  388. uint32 error_buf_size)
  389. {
  390. if (!check_type_index(module, module->type_count, type_index, error_buf,
  391. error_buf_size)) {
  392. return false;
  393. }
  394. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  395. set_error_buf(error_buf, error_buf_size, "unkown array type");
  396. return false;
  397. }
  398. return true;
  399. }
  400. #endif
  401. static bool
  402. check_function_index(const WASMModule *module, uint32 function_index,
  403. char *error_buf, uint32 error_buf_size)
  404. {
  405. if (function_index
  406. >= module->import_function_count + module->function_count) {
  407. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  408. function_index);
  409. return false;
  410. }
  411. return true;
  412. }
  413. typedef struct InitValue {
  414. uint8 type;
  415. uint8 flag;
  416. #if WASM_ENABLE_GC != 0
  417. uint8 gc_opcode;
  418. WASMRefType ref_type;
  419. #endif
  420. WASMValue value;
  421. } InitValue;
  422. typedef struct ConstExprContext {
  423. uint32 sp;
  424. uint32 size;
  425. WASMModule *module;
  426. InitValue *stack;
  427. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  428. } ConstExprContext;
  429. static void
  430. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  431. {
  432. ctx->sp = 0;
  433. ctx->module = module;
  434. ctx->stack = ctx->data;
  435. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  436. }
  437. static bool
  438. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  439. #if WASM_ENABLE_GC != 0
  440. WASMRefType *ref_type, uint8 gc_opcode,
  441. #endif
  442. WASMValue *value, char *error_buf, uint32 error_buf_size)
  443. {
  444. InitValue *cur_value;
  445. if (ctx->sp >= ctx->size) {
  446. if (ctx->stack != ctx->data) {
  447. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  448. (ctx->size + 4) * sizeof(InitValue));
  449. }
  450. else {
  451. if (!(ctx->stack =
  452. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  453. error_buf, error_buf_size))) {
  454. goto fail;
  455. }
  456. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  457. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  458. }
  459. ctx->size += 4;
  460. }
  461. cur_value = &ctx->stack[ctx->sp++];
  462. cur_value->type = type;
  463. cur_value->flag = flag;
  464. cur_value->value = *value;
  465. #if WASM_ENABLE_GC != 0
  466. cur_value->gc_opcode = gc_opcode;
  467. if (wasm_is_type_multi_byte_type(type)) {
  468. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  469. ref_type, wasm_reftype_struct_size(ref_type));
  470. }
  471. #endif
  472. return true;
  473. fail:
  474. return false;
  475. }
  476. #if WASM_ENABLE_GC != 0
  477. static void
  478. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  479. {
  480. WASMStructNewInitValues *struct_init_values =
  481. (WASMStructNewInitValues *)data;
  482. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  483. WASMType *wasm_type;
  484. uint32 i;
  485. if (!data)
  486. return;
  487. wasm_type = module->types[struct_init_values->type_idx];
  488. /* The data can only be type of `WASMStructNewInitValues *`
  489. or `WASMArrayNewInitValues *` */
  490. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  491. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  492. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  493. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  494. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  495. WASMRefType *ref_type;
  496. uint8 field_type;
  497. for (i = 0; i < struct_init_values->count; i++) {
  498. field_type = struct_type->fields[i].field_type;
  499. if (wasm_is_type_multi_byte_type(field_type))
  500. ref_type = ref_type_map->ref_type;
  501. else
  502. ref_type = NULL;
  503. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  504. REF_TYPE_STRUCTREF, NULL,
  505. module->types, module->type_count)
  506. || wasm_reftype_is_subtype_of(
  507. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  508. module->types, module->type_count)) {
  509. destroy_init_expr_data_recursive(
  510. module, struct_init_values->fields[i].data);
  511. }
  512. }
  513. }
  514. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  515. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  516. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  517. uint8 elem_type = array_type->elem_type;
  518. for (i = 0; i < array_init_values->length; i++) {
  519. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  520. REF_TYPE_STRUCTREF, NULL,
  521. module->types, module->type_count)
  522. || wasm_reftype_is_subtype_of(
  523. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  524. module->types, module->type_count)) {
  525. destroy_init_expr_data_recursive(
  526. module, array_init_values->elem_data[i].data);
  527. }
  528. }
  529. }
  530. wasm_runtime_free(data);
  531. }
  532. #endif
  533. static bool
  534. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  535. #if WASM_ENABLE_GC != 0
  536. WASMRefType *ref_type, uint8 *p_gc_opcode,
  537. #endif
  538. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  539. {
  540. InitValue *cur_value;
  541. if (ctx->sp == 0) {
  542. set_error_buf(error_buf, error_buf_size,
  543. "type mismatch: const expr stack underflow");
  544. return false;
  545. }
  546. cur_value = &ctx->stack[--ctx->sp];
  547. #if WASM_ENABLE_GC == 0
  548. if (cur_value->type != type) {
  549. set_error_buf(error_buf, error_buf_size, "type mismatch");
  550. return false;
  551. }
  552. #else
  553. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  554. ref_type, ctx->module->types,
  555. ctx->module->type_count)) {
  556. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  557. "type mismatch: expect ", type2str(type),
  558. " but got other");
  559. goto fail;
  560. }
  561. #endif
  562. if (p_flag)
  563. *p_flag = cur_value->flag;
  564. if (p_value)
  565. *p_value = cur_value->value;
  566. #if WASM_ENABLE_GC != 0
  567. if (p_gc_opcode)
  568. *p_gc_opcode = cur_value->gc_opcode;
  569. #endif
  570. return true;
  571. #if WASM_ENABLE_GC != 0
  572. fail:
  573. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  574. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  575. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  576. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  577. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  578. }
  579. return false;
  580. #endif
  581. }
  582. static void
  583. destroy_const_expr_stack(ConstExprContext *ctx)
  584. {
  585. #if WASM_ENABLE_GC != 0
  586. uint32 i;
  587. for (i = 0; i < ctx->sp; i++) {
  588. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  589. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  590. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  591. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  592. destroy_init_expr_data_recursive(ctx->module,
  593. ctx->stack[i].value.data);
  594. }
  595. }
  596. #endif
  597. if (ctx->stack != ctx->data) {
  598. wasm_runtime_free(ctx->stack);
  599. }
  600. }
  601. #if WASM_ENABLE_GC != 0
  602. static void
  603. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  604. {
  605. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  606. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  607. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  608. destroy_init_expr_data_recursive(module, expr->u.data);
  609. }
  610. }
  611. #endif /* end of WASM_ENABLE_GC != 0 */
  612. static bool
  613. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  614. InitializerExpression *init_expr, uint8 type, void *ref_type,
  615. char *error_buf, uint32 error_buf_size)
  616. {
  617. const uint8 *p = *p_buf, *p_end = buf_end;
  618. uint8 flag, *p_float;
  619. uint32 i;
  620. ConstExprContext const_expr_ctx = { 0 };
  621. WASMValue cur_value;
  622. #if WASM_ENABLE_GC != 0
  623. uint32 opcode1, type_idx;
  624. uint8 opcode;
  625. WASMRefType cur_ref_type = { 0 };
  626. #endif
  627. init_const_expr_stack(&const_expr_ctx, module);
  628. CHECK_BUF(p, p_end, 1);
  629. flag = read_uint8(p);
  630. while (flag != WASM_OP_END) {
  631. switch (flag) {
  632. /* i32.const */
  633. case INIT_EXPR_TYPE_I32_CONST:
  634. read_leb_int32(p, p_end, cur_value.i32);
  635. if (!push_const_expr_stack(
  636. &const_expr_ctx, flag, VALUE_TYPE_I32,
  637. #if WASM_ENABLE_GC != 0
  638. NULL, 0,
  639. #endif
  640. &cur_value, error_buf, error_buf_size))
  641. goto fail;
  642. break;
  643. /* i64.const */
  644. case INIT_EXPR_TYPE_I64_CONST:
  645. read_leb_int64(p, p_end, cur_value.i64);
  646. if (!push_const_expr_stack(
  647. &const_expr_ctx, flag, VALUE_TYPE_I64,
  648. #if WASM_ENABLE_GC != 0
  649. NULL, 0,
  650. #endif
  651. &cur_value, error_buf, error_buf_size))
  652. goto fail;
  653. break;
  654. /* f32.const */
  655. case INIT_EXPR_TYPE_F32_CONST:
  656. CHECK_BUF(p, p_end, 4);
  657. p_float = (uint8 *)&cur_value.f32;
  658. for (i = 0; i < sizeof(float32); i++)
  659. *p_float++ = *p++;
  660. if (!push_const_expr_stack(
  661. &const_expr_ctx, flag, VALUE_TYPE_F32,
  662. #if WASM_ENABLE_GC != 0
  663. NULL, 0,
  664. #endif
  665. &cur_value, error_buf, error_buf_size))
  666. goto fail;
  667. break;
  668. /* f64.const */
  669. case INIT_EXPR_TYPE_F64_CONST:
  670. CHECK_BUF(p, p_end, 8);
  671. p_float = (uint8 *)&cur_value.f64;
  672. for (i = 0; i < sizeof(float64); i++)
  673. *p_float++ = *p++;
  674. if (!push_const_expr_stack(
  675. &const_expr_ctx, flag, VALUE_TYPE_F64,
  676. #if WASM_ENABLE_GC != 0
  677. NULL, 0,
  678. #endif
  679. &cur_value, error_buf, error_buf_size))
  680. goto fail;
  681. break;
  682. #if WASM_ENABLE_SIMD != 0
  683. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  684. /* v128.const */
  685. case INIT_EXPR_TYPE_V128_CONST:
  686. {
  687. uint64 high, low;
  688. CHECK_BUF(p, p_end, 1);
  689. (void)read_uint8(p);
  690. CHECK_BUF(p, p_end, 16);
  691. wasm_runtime_read_v128(p, &high, &low);
  692. p += 16;
  693. cur_value.v128.i64x2[0] = high;
  694. cur_value.v128.i64x2[1] = low;
  695. if (!push_const_expr_stack(
  696. &const_expr_ctx, flag, VALUE_TYPE_V128,
  697. #if WASM_ENABLE_GC != 0
  698. NULL, 0,
  699. #endif
  700. &cur_value, error_buf, error_buf_size))
  701. goto fail;
  702. #if WASM_ENABLE_WAMR_COMPILER != 0
  703. /* If any init_expr is v128.const, mark SIMD used */
  704. module->is_simd_used = true;
  705. #endif
  706. break;
  707. }
  708. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  709. #endif /* end of WASM_ENABLE_SIMD */
  710. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  711. /* ref.func */
  712. case INIT_EXPR_TYPE_FUNCREF_CONST:
  713. {
  714. uint32 func_idx;
  715. read_leb_uint32(p, p_end, func_idx);
  716. cur_value.ref_index = func_idx;
  717. if (!check_function_index(module, func_idx, error_buf,
  718. error_buf_size)) {
  719. goto fail;
  720. }
  721. #if WASM_ENABLE_GC == 0
  722. if (!push_const_expr_stack(&const_expr_ctx, flag,
  723. VALUE_TYPE_FUNCREF, &cur_value,
  724. error_buf, error_buf_size))
  725. goto fail;
  726. #else
  727. if (func_idx < module->import_function_count) {
  728. type_idx =
  729. module->import_functions[func_idx].u.function.type_idx;
  730. }
  731. else {
  732. type_idx = module
  733. ->functions[func_idx
  734. - module->import_function_count]
  735. ->type_idx;
  736. }
  737. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  738. false, type_idx);
  739. if (!push_const_expr_stack(&const_expr_ctx, flag,
  740. cur_ref_type.ref_type, &cur_ref_type,
  741. 0, &cur_value, error_buf,
  742. error_buf_size))
  743. goto fail;
  744. #endif
  745. #if WASM_ENABLE_WAMR_COMPILER != 0
  746. module->is_ref_types_used = true;
  747. #endif
  748. break;
  749. }
  750. /* ref.null */
  751. case INIT_EXPR_TYPE_REFNULL_CONST:
  752. {
  753. uint8 type1;
  754. CHECK_BUF(p, p_end, 1);
  755. type1 = read_uint8(p);
  756. #if WASM_ENABLE_GC == 0
  757. cur_value.ref_index = NULL_REF;
  758. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  759. &cur_value, error_buf,
  760. error_buf_size))
  761. goto fail;
  762. #else
  763. cur_value.gc_obj = NULL_REF;
  764. if (!is_byte_a_type(type1)) {
  765. p--;
  766. read_leb_uint32(p, p_end, type_idx);
  767. if (!check_type_index(module, module->type_count, type_idx,
  768. error_buf, error_buf_size))
  769. goto fail;
  770. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  771. true, type_idx);
  772. if (!push_const_expr_stack(&const_expr_ctx, flag,
  773. cur_ref_type.ref_type,
  774. &cur_ref_type, 0, &cur_value,
  775. error_buf, error_buf_size))
  776. goto fail;
  777. }
  778. else {
  779. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  780. NULL, 0, &cur_value, error_buf,
  781. error_buf_size))
  782. goto fail;
  783. }
  784. #endif
  785. #if WASM_ENABLE_WAMR_COMPILER != 0
  786. module->is_ref_types_used = true;
  787. #endif
  788. break;
  789. }
  790. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  791. /* get_global */
  792. case INIT_EXPR_TYPE_GET_GLOBAL:
  793. {
  794. uint32 global_idx;
  795. uint8 global_type;
  796. read_leb_uint32(p, p_end, cur_value.global_index);
  797. global_idx = cur_value.global_index;
  798. /*
  799. * Currently, constant expressions occurring as initializers
  800. * of globals are further constrained in that contained
  801. * global.get instructions are
  802. * only allowed to refer to imported globals.
  803. *
  804. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  805. */
  806. if (global_idx >= module->import_global_count
  807. /* make spec test happy */
  808. #if WASM_ENABLE_GC != 0
  809. + module->global_count
  810. #endif
  811. ) {
  812. set_error_buf_v(error_buf, error_buf_size,
  813. "unknown global %u", global_idx);
  814. goto fail;
  815. }
  816. if (
  817. /* make spec test happy */
  818. #if WASM_ENABLE_GC != 0
  819. global_idx < module->import_global_count &&
  820. #endif
  821. module->import_globals[global_idx]
  822. .u.global.type.is_mutable) {
  823. set_error_buf_v(error_buf, error_buf_size,
  824. "constant expression required");
  825. goto fail;
  826. }
  827. if (global_idx < module->import_global_count) {
  828. global_type = module->import_globals[global_idx]
  829. .u.global.type.val_type;
  830. #if WASM_ENABLE_GC != 0
  831. if (wasm_is_type_multi_byte_type(global_type)) {
  832. WASMRefType *global_ref_type =
  833. module->import_globals[global_idx]
  834. .u.global.ref_type;
  835. bh_memcpy_s(&cur_ref_type,
  836. wasm_reftype_struct_size(global_ref_type),
  837. global_ref_type,
  838. wasm_reftype_struct_size(global_ref_type));
  839. }
  840. #endif
  841. }
  842. else {
  843. global_type =
  844. module
  845. ->globals[global_idx - module->import_global_count]
  846. .type.val_type;
  847. #if WASM_ENABLE_GC != 0
  848. if (wasm_is_type_multi_byte_type(global_type)) {
  849. WASMRefType *global_ref_type =
  850. module
  851. ->globals[global_idx
  852. - module->import_global_count]
  853. .ref_type;
  854. bh_memcpy_s(&cur_ref_type,
  855. wasm_reftype_struct_size(global_ref_type),
  856. global_ref_type,
  857. wasm_reftype_struct_size(global_ref_type));
  858. }
  859. #endif
  860. }
  861. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  862. #if WASM_ENABLE_GC != 0
  863. &cur_ref_type, 0,
  864. #endif
  865. &cur_value, error_buf,
  866. error_buf_size))
  867. goto fail;
  868. break;
  869. }
  870. #if WASM_ENABLE_GC != 0
  871. /* struct.new and array.new */
  872. case WASM_OP_GC_PREFIX:
  873. {
  874. read_leb_uint32(p, p_end, opcode1);
  875. switch (opcode1) {
  876. case WASM_OP_STRUCT_NEW:
  877. {
  878. WASMStructType *struct_type;
  879. WASMStructNewInitValues *struct_init_values = NULL;
  880. uint32 field_count;
  881. read_leb_uint32(p, p_end, type_idx);
  882. if (!check_type_index(module, module->type_count,
  883. type_idx, error_buf,
  884. error_buf_size)) {
  885. goto fail;
  886. }
  887. struct_type = (WASMStructType *)module->types[type_idx];
  888. if (struct_type->base_type.type_flag
  889. != WASM_TYPE_STRUCT) {
  890. set_error_buf(error_buf, error_buf_size,
  891. "unkown struct type");
  892. goto fail;
  893. }
  894. field_count = struct_type->field_count;
  895. if (!(struct_init_values = loader_malloc(
  896. offsetof(WASMStructNewInitValues, fields)
  897. + (uint64)field_count * sizeof(WASMValue),
  898. error_buf, error_buf_size))) {
  899. goto fail;
  900. }
  901. struct_init_values->type_idx = type_idx;
  902. struct_init_values->count = field_count;
  903. for (i = field_count; i > 0; i--) {
  904. WASMRefType *field_ref_type = NULL;
  905. uint32 field_idx = i - 1;
  906. uint8 field_type =
  907. struct_type->fields[field_idx].field_type;
  908. if (wasm_is_type_multi_byte_type(field_type)) {
  909. field_ref_type = wasm_reftype_map_find(
  910. struct_type->ref_type_maps,
  911. struct_type->ref_type_map_count, field_idx);
  912. }
  913. if (is_packed_type(field_type)) {
  914. field_type = VALUE_TYPE_I32;
  915. }
  916. if (!pop_const_expr_stack(
  917. &const_expr_ctx, NULL, field_type,
  918. field_ref_type, NULL,
  919. &struct_init_values->fields[field_idx],
  920. error_buf, error_buf_size)) {
  921. destroy_init_expr_data_recursive(
  922. module, struct_init_values);
  923. goto fail;
  924. }
  925. }
  926. cur_value.data = struct_init_values;
  927. wasm_set_refheaptype_typeidx(
  928. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  929. if (!push_const_expr_stack(
  930. &const_expr_ctx, flag, cur_ref_type.ref_type,
  931. &cur_ref_type, (uint8)opcode1, &cur_value,
  932. error_buf, error_buf_size)) {
  933. destroy_init_expr_data_recursive(
  934. module, struct_init_values);
  935. goto fail;
  936. }
  937. break;
  938. }
  939. case WASM_OP_STRUCT_NEW_DEFAULT:
  940. {
  941. read_leb_uint32(p, p_end, cur_value.type_index);
  942. type_idx = cur_value.type_index;
  943. if (!check_type_index(module, module->type_count,
  944. type_idx, error_buf,
  945. error_buf_size)) {
  946. goto fail;
  947. }
  948. if (module->types[type_idx]->type_flag
  949. != WASM_TYPE_STRUCT) {
  950. set_error_buf(error_buf, error_buf_size,
  951. "unkown struct type");
  952. goto fail;
  953. }
  954. cur_value.type_index = type_idx;
  955. wasm_set_refheaptype_typeidx(
  956. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  957. if (!push_const_expr_stack(
  958. &const_expr_ctx, flag, cur_ref_type.ref_type,
  959. &cur_ref_type, (uint8)opcode1, &cur_value,
  960. error_buf, error_buf_size)) {
  961. goto fail;
  962. }
  963. break;
  964. }
  965. case WASM_OP_ARRAY_NEW:
  966. case WASM_OP_ARRAY_NEW_DEFAULT:
  967. case WASM_OP_ARRAY_NEW_FIXED:
  968. {
  969. WASMArrayNewInitValues *array_init_values = NULL;
  970. WASMArrayType *array_type = NULL;
  971. WASMRefType *elem_ref_type = NULL;
  972. uint64 total_size;
  973. uint8 elem_type;
  974. read_leb_uint32(p, p_end, cur_value.type_index);
  975. type_idx = cur_value.type_index;
  976. if (!check_type_index(module, module->type_count,
  977. type_idx, error_buf,
  978. error_buf_size)) {
  979. goto fail;
  980. }
  981. array_type = (WASMArrayType *)module->types[type_idx];
  982. if (array_type->base_type.type_flag
  983. != WASM_TYPE_ARRAY) {
  984. set_error_buf(error_buf, error_buf_size,
  985. "unkown array type");
  986. goto fail;
  987. }
  988. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  989. elem_type = array_type->elem_type;
  990. if (wasm_is_type_multi_byte_type(elem_type)) {
  991. elem_ref_type = array_type->elem_ref_type;
  992. }
  993. if (is_packed_type(elem_type)) {
  994. elem_type = VALUE_TYPE_I32;
  995. }
  996. if (opcode1 == WASM_OP_ARRAY_NEW) {
  997. WASMValue len_val;
  998. if (!(array_init_values = loader_malloc(
  999. sizeof(WASMArrayNewInitValues),
  1000. error_buf, error_buf_size))) {
  1001. goto fail;
  1002. }
  1003. array_init_values->type_idx = type_idx;
  1004. if (!pop_const_expr_stack(
  1005. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  1006. NULL, NULL, &len_val, error_buf,
  1007. error_buf_size)) {
  1008. destroy_init_expr_data_recursive(
  1009. module, array_init_values);
  1010. goto fail;
  1011. }
  1012. array_init_values->length = len_val.i32;
  1013. if (!pop_const_expr_stack(
  1014. &const_expr_ctx, NULL, elem_type,
  1015. elem_ref_type, NULL,
  1016. &array_init_values->elem_data[0],
  1017. error_buf, error_buf_size)) {
  1018. destroy_init_expr_data_recursive(
  1019. module, array_init_values);
  1020. goto fail;
  1021. }
  1022. cur_value.data = array_init_values;
  1023. }
  1024. else {
  1025. /* WASM_OP_ARRAY_NEW_FIXED */
  1026. uint32 len;
  1027. read_leb_uint32(p, p_end, len);
  1028. total_size =
  1029. (uint64)offsetof(WASMArrayNewInitValues,
  1030. elem_data)
  1031. + (uint64)sizeof(WASMValue) * len;
  1032. if (!(array_init_values =
  1033. loader_malloc(total_size, error_buf,
  1034. error_buf_size))) {
  1035. goto fail;
  1036. }
  1037. array_init_values->type_idx = type_idx;
  1038. array_init_values->length = len;
  1039. for (i = len; i > 0; i--) {
  1040. if (!pop_const_expr_stack(
  1041. &const_expr_ctx, NULL, elem_type,
  1042. elem_ref_type, NULL,
  1043. &array_init_values
  1044. ->elem_data[i - 1],
  1045. error_buf, error_buf_size)) {
  1046. destroy_init_expr_data_recursive(
  1047. module, array_init_values);
  1048. goto fail;
  1049. }
  1050. }
  1051. cur_value.data = array_init_values;
  1052. }
  1053. }
  1054. else {
  1055. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1056. WASMValue len_val;
  1057. uint32 len;
  1058. /* POP(i32) */
  1059. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1060. VALUE_TYPE_I32, NULL,
  1061. NULL, &len_val, error_buf,
  1062. error_buf_size)) {
  1063. goto fail;
  1064. }
  1065. len = len_val.i32;
  1066. cur_value.array_new_default.type_index = type_idx;
  1067. cur_value.array_new_default.length = len;
  1068. }
  1069. wasm_set_refheaptype_typeidx(
  1070. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1071. if (!push_const_expr_stack(
  1072. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1073. &cur_ref_type, (uint8)opcode1, &cur_value,
  1074. error_buf, error_buf_size)) {
  1075. if (array_init_values) {
  1076. destroy_init_expr_data_recursive(
  1077. module, array_init_values);
  1078. }
  1079. goto fail;
  1080. }
  1081. break;
  1082. }
  1083. case WASM_OP_ANY_CONVERT_EXTERN:
  1084. {
  1085. set_error_buf(error_buf, error_buf_size,
  1086. "unsupported constant expression of "
  1087. "extern.internalize");
  1088. goto fail;
  1089. }
  1090. case WASM_OP_EXTERN_CONVERT_ANY:
  1091. {
  1092. set_error_buf(error_buf, error_buf_size,
  1093. "unsupported constant expression of "
  1094. "extern.externalize");
  1095. goto fail;
  1096. }
  1097. case WASM_OP_REF_I31:
  1098. {
  1099. /* POP(i32) */
  1100. if (!pop_const_expr_stack(
  1101. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1102. NULL, &cur_value, error_buf, error_buf_size)) {
  1103. goto fail;
  1104. }
  1105. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1106. false, HEAP_TYPE_I31);
  1107. if (!push_const_expr_stack(
  1108. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1109. &cur_ref_type, (uint8)opcode1, &cur_value,
  1110. error_buf, error_buf_size)) {
  1111. goto fail;
  1112. }
  1113. break;
  1114. }
  1115. default:
  1116. set_error_buf(
  1117. error_buf, error_buf_size,
  1118. "type mismatch or constant expression required");
  1119. goto fail;
  1120. }
  1121. break;
  1122. }
  1123. #endif /* end of WASM_ENABLE_GC != 0 */
  1124. default:
  1125. {
  1126. set_error_buf(error_buf, error_buf_size,
  1127. "illegal opcode "
  1128. "or constant expression required "
  1129. "or type mismatch");
  1130. goto fail;
  1131. }
  1132. }
  1133. CHECK_BUF(p, p_end, 1);
  1134. flag = read_uint8(p);
  1135. }
  1136. /* There should be only one value left on the init value stack */
  1137. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1138. #if WASM_ENABLE_GC != 0
  1139. ref_type, &opcode,
  1140. #endif
  1141. &cur_value, error_buf, error_buf_size)) {
  1142. goto fail;
  1143. }
  1144. if (const_expr_ctx.sp != 0) {
  1145. set_error_buf(error_buf, error_buf_size,
  1146. "type mismatch: illegal constant opcode sequence");
  1147. goto fail;
  1148. }
  1149. init_expr->init_expr_type = flag;
  1150. init_expr->u = cur_value;
  1151. #if WASM_ENABLE_GC != 0
  1152. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1153. switch (opcode) {
  1154. case WASM_OP_STRUCT_NEW:
  1155. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1156. break;
  1157. case WASM_OP_STRUCT_NEW_DEFAULT:
  1158. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1159. break;
  1160. case WASM_OP_ARRAY_NEW:
  1161. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1162. break;
  1163. case WASM_OP_ARRAY_NEW_DEFAULT:
  1164. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1165. break;
  1166. case WASM_OP_ARRAY_NEW_FIXED:
  1167. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1168. break;
  1169. case WASM_OP_REF_I31:
  1170. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1171. break;
  1172. default:
  1173. bh_assert(0);
  1174. break;
  1175. }
  1176. }
  1177. #endif /* end of WASM_ENABLE_GC != 0 */
  1178. *p_buf = p;
  1179. destroy_const_expr_stack(&const_expr_ctx);
  1180. return true;
  1181. fail:
  1182. destroy_const_expr_stack(&const_expr_ctx);
  1183. return false;
  1184. }
  1185. static bool
  1186. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1187. {
  1188. if (mutable >= 2) {
  1189. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1190. return false;
  1191. }
  1192. return true;
  1193. }
  1194. #if WASM_ENABLE_GC != 0
  1195. static void
  1196. destroy_func_type(WASMFuncType *type)
  1197. {
  1198. /* Destroy the reference type hash set */
  1199. if (type->ref_type_maps)
  1200. wasm_runtime_free(type->ref_type_maps);
  1201. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1202. && WASM_ENABLE_LAZY_JIT != 0
  1203. if (type->call_to_llvm_jit_from_fast_jit)
  1204. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1205. #endif
  1206. /* Free the type */
  1207. wasm_runtime_free(type);
  1208. }
  1209. static void
  1210. destroy_struct_type(WASMStructType *type)
  1211. {
  1212. if (type->ref_type_maps)
  1213. wasm_runtime_free(type->ref_type_maps);
  1214. wasm_runtime_free(type);
  1215. }
  1216. static void
  1217. destroy_array_type(WASMArrayType *type)
  1218. {
  1219. wasm_runtime_free(type);
  1220. }
  1221. static void
  1222. destroy_wasm_type(WASMType *type)
  1223. {
  1224. if (type->ref_count > 1) {
  1225. /* The type is referenced by other types
  1226. of current wasm module */
  1227. type->ref_count--;
  1228. return;
  1229. }
  1230. if (type->type_flag == WASM_TYPE_FUNC)
  1231. destroy_func_type((WASMFuncType *)type);
  1232. else if (type->type_flag == WASM_TYPE_STRUCT)
  1233. destroy_struct_type((WASMStructType *)type);
  1234. else if (type->type_flag == WASM_TYPE_ARRAY)
  1235. destroy_array_type((WASMArrayType *)type);
  1236. else {
  1237. bh_assert(0);
  1238. }
  1239. }
  1240. /* Resolve (ref null ht) or (ref ht) */
  1241. static bool
  1242. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1243. WASMModule *module, uint32 type_count, bool nullable,
  1244. WASMRefType *ref_type, char *error_buf,
  1245. uint32 error_buf_size)
  1246. {
  1247. const uint8 *p = *p_buf, *p_end = buf_end;
  1248. ref_type->ref_type =
  1249. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1250. ref_type->ref_ht_common.nullable = nullable;
  1251. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1252. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1253. /* heap type is (type i), i : typeidx, >= 0 */
  1254. if (!check_type_index(module, type_count,
  1255. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1256. error_buf_size)) {
  1257. return false;
  1258. }
  1259. }
  1260. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1261. /* heap type is func, extern, any, eq, i31 or data */
  1262. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1263. return false;
  1264. }
  1265. *p_buf = p;
  1266. return true;
  1267. fail:
  1268. return false;
  1269. }
  1270. static bool
  1271. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1272. WASMModule *module, uint32 type_count,
  1273. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1274. bool allow_packed_type, char *error_buf,
  1275. uint32 error_buf_size)
  1276. {
  1277. const uint8 *p = *p_buf, *p_end = buf_end;
  1278. uint8 type;
  1279. memset(ref_type, 0, sizeof(WASMRefType));
  1280. CHECK_BUF(p, p_end, 1);
  1281. type = read_uint8(p);
  1282. if (wasm_is_reftype_htref_nullable(type)) {
  1283. /* (ref null ht) */
  1284. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1285. ref_type, error_buf, error_buf_size))
  1286. return false;
  1287. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1288. *p_need_ref_type_map = true;
  1289. else {
  1290. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1291. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1292. multi-byte type to one-byte type to reduce the footprint and
  1293. the complexity of type equal/subtype checking */
  1294. ref_type->ref_type =
  1295. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1296. *p_need_ref_type_map = false;
  1297. }
  1298. }
  1299. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1300. /* (ref ht) */
  1301. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1302. ref_type, error_buf, error_buf_size))
  1303. return false;
  1304. *p_need_ref_type_map = true;
  1305. #if WASM_ENABLE_STRINGREF != 0
  1306. /* covert (ref string) to stringref */
  1307. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1308. ref_type->ref_type =
  1309. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1310. *p_need_ref_type_map = false;
  1311. }
  1312. #endif
  1313. }
  1314. else {
  1315. /* type which can be represented by one byte */
  1316. if (!is_value_type(type)
  1317. && !(allow_packed_type && is_packed_type(type))) {
  1318. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1319. return false;
  1320. }
  1321. ref_type->ref_type = type;
  1322. *p_need_ref_type_map = false;
  1323. #if WASM_ENABLE_WAMR_COMPILER != 0
  1324. /* If any value's type is v128, mark the module as SIMD used */
  1325. if (type == VALUE_TYPE_V128)
  1326. module->is_simd_used = true;
  1327. #endif
  1328. }
  1329. *p_buf = p;
  1330. return true;
  1331. fail:
  1332. return false;
  1333. }
  1334. static WASMRefType *
  1335. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1336. char *error_buf, uint32 error_buf_size)
  1337. {
  1338. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1339. if (!ret) {
  1340. set_error_buf(error_buf, error_buf_size,
  1341. "insert ref type to hash set failed");
  1342. }
  1343. return ret;
  1344. }
  1345. static bool
  1346. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1347. uint32 type_count, uint32 type_idx, char *error_buf,
  1348. uint32 error_buf_size)
  1349. {
  1350. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1351. uint32 param_count, result_count, i, j = 0;
  1352. uint32 param_cell_num, ret_cell_num;
  1353. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1354. uint64 total_size;
  1355. bool need_ref_type_map;
  1356. WASMRefType ref_type;
  1357. WASMFuncType *type = NULL;
  1358. /* Parse first time to resolve param count, result count and
  1359. ref type map count */
  1360. read_leb_uint32(p, p_end, param_count);
  1361. p_org = p;
  1362. for (i = 0; i < param_count; i++) {
  1363. if (!resolve_value_type(&p, p_end, module, type_count,
  1364. &need_ref_type_map, &ref_type, false, error_buf,
  1365. error_buf_size)) {
  1366. return false;
  1367. }
  1368. if (need_ref_type_map)
  1369. ref_type_map_count++;
  1370. }
  1371. read_leb_uint32(p, p_end, result_count);
  1372. for (i = 0; i < result_count; i++) {
  1373. if (!resolve_value_type(&p, p_end, module, type_count,
  1374. &need_ref_type_map, &ref_type, false, error_buf,
  1375. error_buf_size)) {
  1376. return false;
  1377. }
  1378. if (need_ref_type_map) {
  1379. ref_type_map_count++;
  1380. result_ref_type_map_count++;
  1381. }
  1382. }
  1383. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1384. "ref type map count: %d",
  1385. type_idx, param_count, result_count, ref_type_map_count);
  1386. /* Parse second time to resolve param types, result types and
  1387. ref type map info */
  1388. p = p_org;
  1389. total_size = offsetof(WASMFuncType, types)
  1390. + sizeof(uint8) * (uint64)(param_count + result_count);
  1391. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1392. return false;
  1393. }
  1394. if (ref_type_map_count > 0) {
  1395. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1396. if (!(type->ref_type_maps =
  1397. loader_malloc(total_size, error_buf, error_buf_size))) {
  1398. goto fail;
  1399. }
  1400. }
  1401. type->base_type.type_flag = WASM_TYPE_FUNC;
  1402. type->param_count = param_count;
  1403. type->result_count = result_count;
  1404. type->ref_type_map_count = ref_type_map_count;
  1405. if (ref_type_map_count > 0) {
  1406. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1407. - result_ref_type_map_count;
  1408. }
  1409. for (i = 0; i < param_count; i++) {
  1410. if (!resolve_value_type(&p, p_end, module, type_count,
  1411. &need_ref_type_map, &ref_type, false, error_buf,
  1412. error_buf_size)) {
  1413. goto fail;
  1414. }
  1415. type->types[i] = ref_type.ref_type;
  1416. if (need_ref_type_map) {
  1417. type->ref_type_maps[j].index = i;
  1418. if (!(type->ref_type_maps[j++].ref_type =
  1419. reftype_set_insert(module->ref_type_set, &ref_type,
  1420. error_buf, error_buf_size))) {
  1421. goto fail;
  1422. }
  1423. }
  1424. }
  1425. read_leb_uint32(p, p_end, result_count);
  1426. for (i = 0; i < result_count; i++) {
  1427. if (!resolve_value_type(&p, p_end, module, type_count,
  1428. &need_ref_type_map, &ref_type, false, error_buf,
  1429. error_buf_size)) {
  1430. goto fail;
  1431. }
  1432. type->types[param_count + i] = ref_type.ref_type;
  1433. if (need_ref_type_map) {
  1434. type->ref_type_maps[j].index = param_count + i;
  1435. if (!(type->ref_type_maps[j++].ref_type =
  1436. reftype_set_insert(module->ref_type_set, &ref_type,
  1437. error_buf, error_buf_size))) {
  1438. goto fail;
  1439. }
  1440. }
  1441. }
  1442. bh_assert(j == type->ref_type_map_count);
  1443. #if TRACE_WASM_LOADER != 0
  1444. os_printf("type %d = ", type_idx);
  1445. wasm_dump_func_type(type);
  1446. #endif
  1447. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1448. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1449. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1450. set_error_buf(error_buf, error_buf_size,
  1451. "param count or result count too large");
  1452. goto fail;
  1453. }
  1454. type->param_cell_num = (uint16)param_cell_num;
  1455. type->ret_cell_num = (uint16)ret_cell_num;
  1456. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1457. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1458. #endif
  1459. #if WASM_ENABLE_WAMR_COMPILER != 0
  1460. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1461. if (type->types[i] == VALUE_TYPE_V128)
  1462. module->is_simd_used = true;
  1463. }
  1464. #endif
  1465. *p_buf = p;
  1466. module->types[type_idx] = (WASMType *)type;
  1467. return true;
  1468. fail:
  1469. if (type)
  1470. destroy_func_type(type);
  1471. return false;
  1472. }
  1473. static bool
  1474. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1475. WASMModule *module, uint32 type_count, uint32 type_idx,
  1476. char *error_buf, uint32 error_buf_size)
  1477. {
  1478. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1479. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1480. uint32 i, j = 0, offset;
  1481. uint16 *reference_table;
  1482. uint64 total_size;
  1483. uint8 mutable;
  1484. bool need_ref_type_map;
  1485. WASMRefType ref_type;
  1486. WASMStructType *type = NULL;
  1487. /* Parse first time to resolve field count and ref type map count */
  1488. read_leb_uint32(p, p_end, field_count);
  1489. p_org = p;
  1490. for (i = 0; i < field_count; i++) {
  1491. if (!resolve_value_type(&p, p_end, module, type_count,
  1492. &need_ref_type_map, &ref_type, true, error_buf,
  1493. error_buf_size)) {
  1494. return false;
  1495. }
  1496. if (need_ref_type_map)
  1497. ref_type_map_count++;
  1498. if (wasm_is_type_reftype(ref_type.ref_type))
  1499. ref_field_count++;
  1500. CHECK_BUF(p, p_end, 1);
  1501. mutable = read_uint8(p);
  1502. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1503. return false;
  1504. }
  1505. }
  1506. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1507. type_idx, field_count, ref_type_map_count);
  1508. /* Parse second time to resolve field types and ref type map info */
  1509. p = p_org;
  1510. total_size = offsetof(WASMStructType, fields)
  1511. + sizeof(WASMStructFieldType) * (uint64)field_count
  1512. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1513. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1514. return false;
  1515. }
  1516. if (ref_type_map_count > 0) {
  1517. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1518. if (!(type->ref_type_maps =
  1519. loader_malloc(total_size, error_buf, error_buf_size))) {
  1520. goto fail;
  1521. }
  1522. }
  1523. type->reference_table = reference_table =
  1524. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1525. + sizeof(WASMStructFieldType) * field_count);
  1526. *reference_table++ = ref_field_count;
  1527. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1528. type->field_count = field_count;
  1529. type->ref_type_map_count = ref_type_map_count;
  1530. offset = (uint32)offsetof(WASMStructObject, field_data);
  1531. for (i = 0; i < field_count; i++) {
  1532. if (!resolve_value_type(&p, p_end, module, type_count,
  1533. &need_ref_type_map, &ref_type, true, error_buf,
  1534. error_buf_size)) {
  1535. goto fail;
  1536. }
  1537. type->fields[i].field_type = ref_type.ref_type;
  1538. if (need_ref_type_map) {
  1539. type->ref_type_maps[j].index = i;
  1540. if (!(type->ref_type_maps[j++].ref_type =
  1541. reftype_set_insert(module->ref_type_set, &ref_type,
  1542. error_buf, error_buf_size))) {
  1543. goto fail;
  1544. }
  1545. }
  1546. CHECK_BUF(p, p_end, 1);
  1547. type->fields[i].field_flags = read_uint8(p);
  1548. type->fields[i].field_size =
  1549. (uint8)wasm_reftype_size(ref_type.ref_type);
  1550. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1551. || defined(BUILD_TARGET_X86_32))
  1552. if (type->fields[i].field_size == 2)
  1553. offset = align_uint(offset, 2);
  1554. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1555. offset = align_uint(offset, 4);
  1556. #endif
  1557. type->fields[i].field_offset = offset;
  1558. if (wasm_is_type_reftype(ref_type.ref_type))
  1559. *reference_table++ = offset;
  1560. offset += type->fields[i].field_size;
  1561. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1562. type->fields[i].field_flags, type->fields[i].field_type);
  1563. }
  1564. type->total_size = offset;
  1565. bh_assert(j == type->ref_type_map_count);
  1566. #if TRACE_WASM_LOADER != 0
  1567. os_printf("type %d = ", type_idx);
  1568. wasm_dump_struct_type(type);
  1569. #endif
  1570. *p_buf = p;
  1571. module->types[type_idx] = (WASMType *)type;
  1572. return true;
  1573. fail:
  1574. if (type)
  1575. destroy_struct_type(type);
  1576. return false;
  1577. }
  1578. static bool
  1579. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1580. WASMModule *module, uint32 type_count, uint32 type_idx,
  1581. char *error_buf, uint32 error_buf_size)
  1582. {
  1583. const uint8 *p = *p_buf, *p_end = buf_end;
  1584. uint8 mutable;
  1585. bool need_ref_type_map;
  1586. WASMRefType ref_type;
  1587. WASMArrayType *type = NULL;
  1588. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1589. &ref_type, true, error_buf, error_buf_size)) {
  1590. return false;
  1591. }
  1592. CHECK_BUF(p, p_end, 1);
  1593. mutable = read_uint8(p);
  1594. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1595. return false;
  1596. }
  1597. LOG_VERBOSE("type %u: array", type_idx);
  1598. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1599. error_buf_size))) {
  1600. return false;
  1601. }
  1602. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1603. type->elem_flags = mutable;
  1604. type->elem_type = ref_type.ref_type;
  1605. if (need_ref_type_map) {
  1606. if (!(type->elem_ref_type =
  1607. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1608. error_buf_size))) {
  1609. goto fail;
  1610. }
  1611. }
  1612. #if TRACE_WASM_LOADER != 0
  1613. os_printf("type %d = ", type_idx);
  1614. wasm_dump_array_type(type);
  1615. #endif
  1616. *p_buf = p;
  1617. module->types[type_idx] = (WASMType *)type;
  1618. return true;
  1619. fail:
  1620. if (type)
  1621. destroy_array_type(type);
  1622. return false;
  1623. }
  1624. static bool
  1625. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1626. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1627. {
  1628. if (heap_type >= 0) {
  1629. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1630. error_buf_size)) {
  1631. return false;
  1632. }
  1633. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1634. heap_type);
  1635. }
  1636. else {
  1637. if (!wasm_is_valid_heap_type(heap_type)) {
  1638. set_error_buf(error_buf, error_buf_size, "unknown type");
  1639. return false;
  1640. }
  1641. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1642. heap_type);
  1643. if (nullable) {
  1644. /* For (ref null func/extern/any/eq/i31/data),
  1645. they are same as
  1646. funcref/externref/anyref/eqref/i31ref/dataref,
  1647. we convert the multi-byte type to one-byte
  1648. type to reduce the footprint and the
  1649. complexity of type equal/subtype checking */
  1650. ref_type->ref_type =
  1651. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1652. }
  1653. }
  1654. return true;
  1655. }
  1656. static void
  1657. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1658. WASMRefType *ref_type2)
  1659. {
  1660. /**
  1661. * The difference rt1 ∖ rt2 between two reference types is defined as
  1662. * follows:
  1663. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1664. * (ref ht2) = (ref null?1 ht1)
  1665. */
  1666. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1667. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1668. ref_type1, wasm_reftype_struct_size(ref_type1));
  1669. }
  1670. else {
  1671. ref_type_diff->ref_type = ref_type1->ref_type;
  1672. }
  1673. if (ref_type2->ref_ht_common.nullable) {
  1674. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1675. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1676. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1677. (int32)ref_type_diff->ref_type - 0x80);
  1678. }
  1679. else {
  1680. ref_type_diff->ref_ht_typeidx.nullable = false;
  1681. }
  1682. }
  1683. }
  1684. #else /* else of WASM_ENABLE_GC != 0 */
  1685. static void
  1686. destroy_wasm_type(WASMType *type)
  1687. {
  1688. if (type->ref_count > 1) {
  1689. /* The type is referenced by other types
  1690. of current wasm module */
  1691. type->ref_count--;
  1692. return;
  1693. }
  1694. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1695. && WASM_ENABLE_LAZY_JIT != 0
  1696. if (type->call_to_llvm_jit_from_fast_jit)
  1697. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1698. #endif
  1699. wasm_runtime_free(type);
  1700. }
  1701. #endif /* end of WASM_ENABLE_GC != 0 */
  1702. static bool
  1703. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1704. char *error_buf, uint32 error_buf_size)
  1705. {
  1706. const uint8 *p = buf, *p_end = buf_end;
  1707. uint32 type_count, i;
  1708. uint64 total_size;
  1709. uint8 flag;
  1710. #if WASM_ENABLE_GC != 0
  1711. uint32 processed_type_count = 0;
  1712. #endif
  1713. read_leb_uint32(p, p_end, type_count);
  1714. if (type_count) {
  1715. module->type_count = type_count;
  1716. total_size = sizeof(WASMType *) * (uint64)type_count;
  1717. if (!(module->types =
  1718. loader_malloc(total_size, error_buf, error_buf_size))) {
  1719. return false;
  1720. }
  1721. #if WASM_ENABLE_GC == 0
  1722. for (i = 0; i < type_count; i++) {
  1723. WASMFuncType *type;
  1724. const uint8 *p_org;
  1725. uint32 param_count, result_count, j;
  1726. uint32 param_cell_num, ret_cell_num;
  1727. CHECK_BUF(p, p_end, 1);
  1728. flag = read_uint8(p);
  1729. if (flag != 0x60) {
  1730. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1731. return false;
  1732. }
  1733. read_leb_uint32(p, p_end, param_count);
  1734. /* Resolve param count and result count firstly */
  1735. p_org = p;
  1736. CHECK_BUF(p, p_end, param_count);
  1737. p += param_count;
  1738. read_leb_uint32(p, p_end, result_count);
  1739. CHECK_BUF(p, p_end, result_count);
  1740. p = p_org;
  1741. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1742. set_error_buf(error_buf, error_buf_size,
  1743. "param count or result count too large");
  1744. return false;
  1745. }
  1746. total_size = offsetof(WASMFuncType, types)
  1747. + sizeof(uint8) * (uint64)(param_count + result_count);
  1748. if (!(type = module->types[i] =
  1749. loader_malloc(total_size, error_buf, error_buf_size))) {
  1750. return false;
  1751. }
  1752. /* Resolve param types and result types */
  1753. type->ref_count = 1;
  1754. type->param_count = (uint16)param_count;
  1755. type->result_count = (uint16)result_count;
  1756. for (j = 0; j < param_count; j++) {
  1757. CHECK_BUF(p, p_end, 1);
  1758. type->types[j] = read_uint8(p);
  1759. }
  1760. read_leb_uint32(p, p_end, result_count);
  1761. for (j = 0; j < result_count; j++) {
  1762. CHECK_BUF(p, p_end, 1);
  1763. type->types[param_count + j] = read_uint8(p);
  1764. }
  1765. for (j = 0; j < param_count + result_count; j++) {
  1766. if (!is_value_type(type->types[j])) {
  1767. set_error_buf(error_buf, error_buf_size,
  1768. "unknown value type");
  1769. return false;
  1770. }
  1771. }
  1772. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1773. ret_cell_num =
  1774. wasm_get_cell_num(type->types + param_count, result_count);
  1775. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1776. set_error_buf(error_buf, error_buf_size,
  1777. "param count or result count too large");
  1778. return false;
  1779. }
  1780. type->param_cell_num = (uint16)param_cell_num;
  1781. type->ret_cell_num = (uint16)ret_cell_num;
  1782. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1783. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1784. #endif
  1785. #if WASM_ENABLE_WAMR_COMPILER != 0
  1786. for (j = 0; j < type->param_count + type->result_count; j++) {
  1787. if (type->types[j] == VALUE_TYPE_V128)
  1788. module->is_simd_used = true;
  1789. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1790. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1791. module->is_ref_types_used = true;
  1792. }
  1793. #endif
  1794. /* If there is already a same type created, use it instead */
  1795. for (j = 0; j < i; j++) {
  1796. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1797. if (module->types[j]->ref_count == UINT16_MAX) {
  1798. set_error_buf(error_buf, error_buf_size,
  1799. "wasm type's ref count too large");
  1800. return false;
  1801. }
  1802. destroy_wasm_type(type);
  1803. module->types[i] = module->types[j];
  1804. module->types[j]->ref_count++;
  1805. break;
  1806. }
  1807. }
  1808. }
  1809. #else /* else of WASM_ENABLE_GC == 0 */
  1810. for (i = 0; i < type_count; i++) {
  1811. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1812. uint32 rec_count = 1, j;
  1813. bool is_sub_final = true;
  1814. CHECK_BUF(p, p_end, 1);
  1815. flag = read_uint8(p);
  1816. if (flag == DEFINED_TYPE_REC) {
  1817. read_leb_uint32(p, p_end, rec_count);
  1818. if (rec_count > 1) {
  1819. uint64 new_total_size;
  1820. /* integer overflow */
  1821. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1822. set_error_buf(error_buf, error_buf_size,
  1823. "recursive type count too large");
  1824. return false;
  1825. }
  1826. module->type_count += rec_count - 1;
  1827. new_total_size =
  1828. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1829. if (new_total_size > UINT32_MAX) {
  1830. set_error_buf(error_buf, error_buf_size,
  1831. "allocate memory failed");
  1832. return false;
  1833. }
  1834. MEM_REALLOC(module->types, (uint32)total_size,
  1835. (uint32)new_total_size);
  1836. total_size = new_total_size;
  1837. }
  1838. LOG_VERBOSE("Processing rec group [%d-%d]",
  1839. processed_type_count,
  1840. processed_type_count + rec_count - 1);
  1841. }
  1842. else {
  1843. p--;
  1844. }
  1845. for (j = 0; j < rec_count; j++) {
  1846. WASMType *cur_type = NULL;
  1847. CHECK_BUF(p, p_end, 1);
  1848. flag = read_uint8(p);
  1849. parent_type_idx = -1;
  1850. if (flag == DEFINED_TYPE_SUB
  1851. || flag == DEFINED_TYPE_SUB_FINAL) {
  1852. read_leb_uint32(p, p_end, super_type_count);
  1853. if (super_type_count > 1) {
  1854. set_error_buf(error_buf, error_buf_size,
  1855. "super type count too large");
  1856. return false;
  1857. }
  1858. if (super_type_count > 0) {
  1859. read_leb_uint32(p, p_end, parent_type_idx);
  1860. if (parent_type_idx >= processed_type_count + j) {
  1861. set_error_buf_v(error_buf, error_buf_size,
  1862. "unknown type %d", parent_type_idx);
  1863. return false;
  1864. }
  1865. if (module->types[parent_type_idx]->is_sub_final) {
  1866. set_error_buf(error_buf, error_buf_size,
  1867. "sub type can not inherit from "
  1868. "a final super type");
  1869. return false;
  1870. }
  1871. }
  1872. if (flag == DEFINED_TYPE_SUB)
  1873. is_sub_final = false;
  1874. CHECK_BUF(p, p_end, 1);
  1875. flag = read_uint8(p);
  1876. }
  1877. if (flag == DEFINED_TYPE_FUNC) {
  1878. if (!resolve_func_type(&p, buf_end, module,
  1879. processed_type_count + rec_count,
  1880. processed_type_count + j, error_buf,
  1881. error_buf_size)) {
  1882. return false;
  1883. }
  1884. }
  1885. else if (flag == DEFINED_TYPE_STRUCT) {
  1886. if (!resolve_struct_type(&p, buf_end, module,
  1887. processed_type_count + rec_count,
  1888. processed_type_count + j,
  1889. error_buf, error_buf_size)) {
  1890. return false;
  1891. }
  1892. }
  1893. else if (flag == DEFINED_TYPE_ARRAY) {
  1894. if (!resolve_array_type(&p, buf_end, module,
  1895. processed_type_count + rec_count,
  1896. processed_type_count + j, error_buf,
  1897. error_buf_size)) {
  1898. return false;
  1899. }
  1900. }
  1901. else {
  1902. set_error_buf(error_buf, error_buf_size,
  1903. "invalid type flag");
  1904. return false;
  1905. }
  1906. cur_type = module->types[processed_type_count + j];
  1907. cur_type->ref_count = 1;
  1908. cur_type->parent_type_idx = parent_type_idx;
  1909. cur_type->is_sub_final = is_sub_final;
  1910. cur_type->rec_count = rec_count;
  1911. cur_type->rec_idx = j;
  1912. cur_type->rec_begin_type_idx = processed_type_count;
  1913. }
  1914. /* resolve subtyping relationship in current rec group */
  1915. for (j = 0; j < rec_count; j++) {
  1916. WASMType *cur_type = module->types[processed_type_count + j];
  1917. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1918. WASMType *parent_type =
  1919. module->types[cur_type->parent_type_idx];
  1920. cur_type->parent_type = parent_type;
  1921. cur_type->root_type = parent_type->root_type;
  1922. if (parent_type->inherit_depth == UINT16_MAX) {
  1923. set_error_buf(error_buf, error_buf_size,
  1924. "parent type's inherit depth too large");
  1925. return false;
  1926. }
  1927. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1928. }
  1929. else {
  1930. cur_type->parent_type = NULL;
  1931. cur_type->root_type = cur_type;
  1932. cur_type->inherit_depth = 0;
  1933. }
  1934. }
  1935. for (j = 0; j < rec_count; j++) {
  1936. WASMType *cur_type = module->types[processed_type_count + j];
  1937. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1938. WASMType *parent_type =
  1939. module->types[cur_type->parent_type_idx];
  1940. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1941. module->types,
  1942. module->type_count)) {
  1943. set_error_buf(error_buf, error_buf_size,
  1944. "sub type does not match super type");
  1945. return false;
  1946. }
  1947. }
  1948. }
  1949. /* If there is already an equivalence type or a group of equivalence
  1950. recursive types created, use it or them instead */
  1951. for (j = 0; j < processed_type_count;) {
  1952. WASMType *src_type = module->types[j];
  1953. WASMType *cur_type = module->types[processed_type_count];
  1954. uint32 k, src_rec_count;
  1955. src_rec_count = src_type->rec_count;
  1956. if (src_rec_count != rec_count) {
  1957. /* no type equivalence */
  1958. j += src_rec_count;
  1959. continue;
  1960. }
  1961. for (k = 0; k < rec_count; k++) {
  1962. src_type = module->types[j + k];
  1963. cur_type = module->types[processed_type_count + k];
  1964. if (!wasm_type_equal(src_type, cur_type, module->types,
  1965. module->type_count)) {
  1966. break;
  1967. }
  1968. }
  1969. if (k < rec_count) {
  1970. /* no type equivalence */
  1971. j += src_rec_count;
  1972. continue;
  1973. }
  1974. /* type equivalence */
  1975. for (k = 0; k < rec_count; k++) {
  1976. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1977. set_error_buf(error_buf, error_buf_size,
  1978. "wasm type's ref count too large");
  1979. return false;
  1980. }
  1981. destroy_wasm_type(module->types[processed_type_count + k]);
  1982. module->types[processed_type_count + k] =
  1983. module->types[j + k];
  1984. module->types[j + k]->ref_count++;
  1985. }
  1986. break;
  1987. }
  1988. if (rec_count > 1) {
  1989. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1990. processed_type_count,
  1991. processed_type_count + rec_count - 1);
  1992. }
  1993. processed_type_count += rec_count;
  1994. }
  1995. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1996. * module->type_count,
  1997. error_buf, error_buf_size))) {
  1998. return false;
  1999. }
  2000. #endif /* end of WASM_ENABLE_GC == 0 */
  2001. }
  2002. if (p != p_end) {
  2003. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2004. return false;
  2005. }
  2006. LOG_VERBOSE("Load type section success.\n");
  2007. return true;
  2008. fail:
  2009. return false;
  2010. }
  2011. static void
  2012. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  2013. {
  2014. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  2015. ? init_size * 2
  2016. : WASM_TABLE_MAX_SIZE;
  2017. if (max_size_flag) {
  2018. /* module defines the table limitation */
  2019. bh_assert(init_size <= *max_size);
  2020. if (init_size < *max_size) {
  2021. *max_size =
  2022. *max_size < default_max_size ? *max_size : default_max_size;
  2023. }
  2024. }
  2025. else {
  2026. /* partial defined table limitation, gives a default value */
  2027. *max_size = default_max_size;
  2028. }
  2029. }
  2030. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2031. /**
  2032. * Find export item of a module with export info:
  2033. * module name, field name and export kind
  2034. */
  2035. static WASMExport *
  2036. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2037. const char *field_name, uint8 export_kind,
  2038. char *error_buf, uint32 error_buf_size)
  2039. {
  2040. WASMExport *export =
  2041. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2042. export_kind, error_buf, error_buf_size);
  2043. return export;
  2044. }
  2045. #endif
  2046. #if WASM_ENABLE_MULTI_MODULE != 0
  2047. static WASMFunction *
  2048. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2049. const WASMFuncType *expected_function_type,
  2050. char *error_buf, uint32 error_buf_size)
  2051. {
  2052. WASMModuleCommon *module_reg;
  2053. WASMFunction *function = NULL;
  2054. WASMExport *export = NULL;
  2055. WASMModule *module = NULL;
  2056. WASMFuncType *target_function_type = NULL;
  2057. module_reg = wasm_runtime_find_module_registered(module_name);
  2058. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2059. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2060. function_name);
  2061. set_error_buf(error_buf, error_buf_size, "unknown import");
  2062. return NULL;
  2063. }
  2064. module = (WASMModule *)module_reg;
  2065. export =
  2066. wasm_loader_find_export(module, module_name, function_name,
  2067. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2068. if (!export) {
  2069. return NULL;
  2070. }
  2071. /* resolve function type and function */
  2072. if (export->index < module->import_function_count) {
  2073. target_function_type =
  2074. module->import_functions[export->index].u.function.func_type;
  2075. function = module->import_functions[export->index]
  2076. .u.function.import_func_linked;
  2077. }
  2078. else {
  2079. target_function_type =
  2080. module->functions[export->index - module->import_function_count]
  2081. ->func_type;
  2082. function =
  2083. module->functions[export->index - module->import_function_count];
  2084. }
  2085. /* check function type */
  2086. if (!wasm_type_equal((WASMType *)expected_function_type,
  2087. (WASMType *)target_function_type, module->types,
  2088. module->type_count)) {
  2089. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2090. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2091. return NULL;
  2092. }
  2093. return function;
  2094. }
  2095. static WASMTable *
  2096. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2097. uint32 init_size, uint32 max_size, char *error_buf,
  2098. uint32 error_buf_size)
  2099. {
  2100. WASMModuleCommon *module_reg;
  2101. WASMTable *table = NULL;
  2102. WASMExport *export = NULL;
  2103. WASMModule *module = NULL;
  2104. module_reg = wasm_runtime_find_module_registered(module_name);
  2105. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2106. LOG_DEBUG("can not find a module named %s for table", module_name);
  2107. set_error_buf(error_buf, error_buf_size, "unknown import");
  2108. return NULL;
  2109. }
  2110. module = (WASMModule *)module_reg;
  2111. export =
  2112. wasm_loader_find_export(module, module_name, table_name,
  2113. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2114. if (!export) {
  2115. return NULL;
  2116. }
  2117. /* resolve table and check the init/max size */
  2118. if (export->index < module->import_table_count) {
  2119. table =
  2120. module->import_tables[export->index].u.table.import_table_linked;
  2121. }
  2122. else {
  2123. table = &(module->tables[export->index - module->import_table_count]);
  2124. }
  2125. if (table->init_size < init_size || table->max_size > max_size) {
  2126. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2127. module_name, table_name, table->init_size, table->max_size,
  2128. init_size, max_size);
  2129. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2130. return NULL;
  2131. }
  2132. return table;
  2133. }
  2134. static WASMMemory *
  2135. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2136. uint32 init_page_count, uint32 max_page_count,
  2137. char *error_buf, uint32 error_buf_size)
  2138. {
  2139. WASMModuleCommon *module_reg;
  2140. WASMMemory *memory = 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 memory", module_name);
  2146. set_error_buf(error_buf, error_buf_size, "unknown import");
  2147. return NULL;
  2148. }
  2149. module = (WASMModule *)module_reg;
  2150. export =
  2151. wasm_loader_find_export(module, module_name, memory_name,
  2152. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2153. if (!export) {
  2154. return NULL;
  2155. }
  2156. /* resolve memory and check the init/max page count */
  2157. if (export->index < module->import_memory_count) {
  2158. memory = module->import_memories[export->index]
  2159. .u.memory.import_memory_linked;
  2160. }
  2161. else {
  2162. memory =
  2163. &(module->memories[export->index - module->import_memory_count]);
  2164. }
  2165. if (memory->init_page_count < init_page_count
  2166. || memory->max_page_count > max_page_count) {
  2167. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2168. module_name, memory_name, memory->init_page_count,
  2169. memory->max_page_count, init_page_count, max_page_count);
  2170. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2171. return NULL;
  2172. }
  2173. return memory;
  2174. }
  2175. static WASMGlobal *
  2176. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2177. uint8 type, bool is_mutable, char *error_buf,
  2178. uint32 error_buf_size)
  2179. {
  2180. WASMModuleCommon *module_reg;
  2181. WASMGlobal *global = NULL;
  2182. WASMExport *export = NULL;
  2183. WASMModule *module = NULL;
  2184. module_reg = wasm_runtime_find_module_registered(module_name);
  2185. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2186. LOG_DEBUG("can not find a module named %s for global", module_name);
  2187. set_error_buf(error_buf, error_buf_size, "unknown import");
  2188. return NULL;
  2189. }
  2190. module = (WASMModule *)module_reg;
  2191. export =
  2192. wasm_loader_find_export(module, module_name, global_name,
  2193. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2194. if (!export) {
  2195. return NULL;
  2196. }
  2197. /* resolve and check the global */
  2198. if (export->index < module->import_global_count) {
  2199. global =
  2200. module->import_globals[export->index].u.global.import_global_linked;
  2201. }
  2202. else {
  2203. global =
  2204. &(module->globals[export->index - module->import_global_count]);
  2205. }
  2206. if (global->type.val_type != type
  2207. || global->type.is_mutable != is_mutable) {
  2208. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2209. module_name, global_name, global->type.val_type,
  2210. global->type.is_mutable, type, is_mutable);
  2211. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2212. return NULL;
  2213. }
  2214. return global;
  2215. }
  2216. #if WASM_ENABLE_TAGS != 0
  2217. static WASMTag *
  2218. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2219. const WASMType *expected_tag_type,
  2220. uint32 *linked_tag_index, char *error_buf,
  2221. uint32 error_buf_size)
  2222. {
  2223. WASMModuleCommon *module_reg;
  2224. WASMTag *tag = NULL;
  2225. WASMExport *export = NULL;
  2226. WASMModule *module = NULL;
  2227. module_reg = wasm_runtime_find_module_registered(module_name);
  2228. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2229. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2230. tag_name);
  2231. set_error_buf(error_buf, error_buf_size, "unknown import");
  2232. return NULL;
  2233. }
  2234. module = (WASMModule *)module_reg;
  2235. export =
  2236. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2237. error_buf, error_buf_size);
  2238. if (!export) {
  2239. return NULL;
  2240. }
  2241. /* resolve tag type and tag */
  2242. if (export->index < module->import_tag_count) {
  2243. /* importing an imported tag from the submodule */
  2244. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2245. }
  2246. else {
  2247. /* importing an section tag from the submodule */
  2248. tag = module->tags[export->index - module->import_tag_count];
  2249. }
  2250. /* check function type */
  2251. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2252. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2253. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2254. return NULL;
  2255. }
  2256. if (linked_tag_index != NULL) {
  2257. *linked_tag_index = export->index;
  2258. }
  2259. return tag;
  2260. }
  2261. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2262. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2263. static bool
  2264. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2265. const WASMModule *parent_module,
  2266. const char *sub_module_name, const char *function_name,
  2267. WASMFunctionImport *function, char *error_buf,
  2268. uint32 error_buf_size)
  2269. {
  2270. const uint8 *p = *p_buf, *p_end = buf_end;
  2271. uint32 declare_type_index = 0;
  2272. WASMFuncType *declare_func_type = NULL;
  2273. WASMFunction *linked_func = NULL;
  2274. #if WASM_ENABLE_MULTI_MODULE != 0
  2275. WASMModule *sub_module = NULL;
  2276. #endif
  2277. const char *linked_signature = NULL;
  2278. void *linked_attachment = NULL;
  2279. bool linked_call_conv_raw = false;
  2280. bool is_native_symbol = false;
  2281. read_leb_uint32(p, p_end, declare_type_index);
  2282. *p_buf = p;
  2283. if (declare_type_index >= parent_module->type_count) {
  2284. set_error_buf(error_buf, error_buf_size, "unknown type");
  2285. return false;
  2286. }
  2287. #if WASM_ENABLE_GC != 0
  2288. function->type_idx = declare_type_index;
  2289. #endif
  2290. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2291. declare_type_index = wasm_get_smallest_type_idx(
  2292. parent_module->types, parent_module->type_count, declare_type_index);
  2293. #endif
  2294. declare_func_type =
  2295. (WASMFuncType *)parent_module->types[declare_type_index];
  2296. /* lookup registered native symbols first */
  2297. linked_func = wasm_native_resolve_symbol(
  2298. sub_module_name, function_name, declare_func_type, &linked_signature,
  2299. &linked_attachment, &linked_call_conv_raw);
  2300. if (linked_func) {
  2301. is_native_symbol = true;
  2302. }
  2303. #if WASM_ENABLE_MULTI_MODULE != 0
  2304. else {
  2305. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2306. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2307. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2308. error_buf_size);
  2309. if (!sub_module) {
  2310. return false;
  2311. }
  2312. }
  2313. linked_func = wasm_loader_resolve_function(
  2314. sub_module_name, function_name, declare_func_type, error_buf,
  2315. error_buf_size);
  2316. }
  2317. #endif
  2318. function->module_name = (char *)sub_module_name;
  2319. function->field_name = (char *)function_name;
  2320. function->func_type = declare_func_type;
  2321. /* func_ptr_linked is for native registered symbol */
  2322. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2323. function->signature = linked_signature;
  2324. function->attachment = linked_attachment;
  2325. function->call_conv_raw = linked_call_conv_raw;
  2326. #if WASM_ENABLE_MULTI_MODULE != 0
  2327. function->import_module = is_native_symbol ? NULL : sub_module;
  2328. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2329. #endif
  2330. return true;
  2331. fail:
  2332. return false;
  2333. }
  2334. static bool
  2335. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2336. uint32 error_buf_size)
  2337. {
  2338. if (max_size < init_size) {
  2339. set_error_buf(error_buf, error_buf_size,
  2340. "size minimum must not be greater than maximum");
  2341. return false;
  2342. }
  2343. return true;
  2344. }
  2345. static bool
  2346. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2347. WASMModule *parent_module, const char *sub_module_name,
  2348. const char *table_name, WASMTableImport *table,
  2349. char *error_buf, uint32 error_buf_size)
  2350. {
  2351. const uint8 *p = *p_buf, *p_end = buf_end;
  2352. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2353. declare_init_size = 0, declare_max_size = 0;
  2354. #if WASM_ENABLE_MULTI_MODULE != 0
  2355. WASMModule *sub_module = NULL;
  2356. WASMTable *linked_table = NULL;
  2357. #endif
  2358. #if WASM_ENABLE_GC != 0
  2359. WASMRefType ref_type;
  2360. bool need_ref_type_map;
  2361. #endif
  2362. #if WASM_ENABLE_GC == 0
  2363. CHECK_BUF(p, p_end, 1);
  2364. /* 0x70 or 0x6F */
  2365. declare_elem_type = read_uint8(p);
  2366. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2367. #if WASM_ENABLE_REF_TYPES != 0
  2368. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2369. #endif
  2370. ) {
  2371. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2372. return false;
  2373. }
  2374. #else /* else of WASM_ENABLE_GC == 0 */
  2375. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2376. &need_ref_type_map, &ref_type, false, error_buf,
  2377. error_buf_size)) {
  2378. return false;
  2379. }
  2380. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2381. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2382. return false;
  2383. }
  2384. declare_elem_type = ref_type.ref_type;
  2385. if (need_ref_type_map) {
  2386. if (!(table->elem_ref_type =
  2387. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2388. error_buf, error_buf_size))) {
  2389. return false;
  2390. }
  2391. }
  2392. #if TRACE_WASM_LOADER != 0
  2393. os_printf("import table type: ");
  2394. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2395. os_printf("\n");
  2396. #endif
  2397. #endif /* end of WASM_ENABLE_GC == 0 */
  2398. read_leb_uint32(p, p_end, declare_max_size_flag);
  2399. if (declare_max_size_flag > 1) {
  2400. set_error_buf(error_buf, error_buf_size, "integer too large");
  2401. return false;
  2402. }
  2403. read_leb_uint32(p, p_end, declare_init_size);
  2404. if (declare_max_size_flag) {
  2405. read_leb_uint32(p, p_end, declare_max_size);
  2406. if (!check_table_max_size(declare_init_size, declare_max_size,
  2407. error_buf, error_buf_size))
  2408. return false;
  2409. }
  2410. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2411. &declare_max_size);
  2412. *p_buf = p;
  2413. #if WASM_ENABLE_MULTI_MODULE != 0
  2414. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2415. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2416. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2417. error_buf_size);
  2418. if (!sub_module) {
  2419. return false;
  2420. }
  2421. linked_table = wasm_loader_resolve_table(
  2422. sub_module_name, table_name, declare_init_size, declare_max_size,
  2423. error_buf, error_buf_size);
  2424. if (!linked_table) {
  2425. return false;
  2426. }
  2427. /* reset with linked table limit */
  2428. declare_elem_type = linked_table->elem_type;
  2429. declare_init_size = linked_table->init_size;
  2430. declare_max_size = linked_table->max_size;
  2431. declare_max_size_flag = linked_table->flags;
  2432. table->import_table_linked = linked_table;
  2433. table->import_module = sub_module;
  2434. }
  2435. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2436. /* (table (export "table") 10 20 funcref) */
  2437. /* we need this section working in wamrc */
  2438. if (!strcmp("spectest", sub_module_name)) {
  2439. const uint32 spectest_table_init_size = 10;
  2440. const uint32 spectest_table_max_size = 20;
  2441. if (strcmp("table", table_name)) {
  2442. set_error_buf(error_buf, error_buf_size,
  2443. "incompatible import type or unknown import");
  2444. return false;
  2445. }
  2446. if (declare_init_size > spectest_table_init_size
  2447. || declare_max_size < spectest_table_max_size) {
  2448. set_error_buf(error_buf, error_buf_size,
  2449. "incompatible import type");
  2450. return false;
  2451. }
  2452. declare_init_size = spectest_table_init_size;
  2453. declare_max_size = spectest_table_max_size;
  2454. }
  2455. /* now we believe all declaration are ok */
  2456. table->elem_type = declare_elem_type;
  2457. table->init_size = declare_init_size;
  2458. table->flags = declare_max_size_flag;
  2459. table->max_size = declare_max_size;
  2460. #if WASM_ENABLE_WAMR_COMPILER != 0
  2461. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2462. parent_module->is_ref_types_used = true;
  2463. #endif
  2464. (void)parent_module;
  2465. return true;
  2466. fail:
  2467. return false;
  2468. }
  2469. static bool
  2470. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2471. uint32 error_buf_size)
  2472. {
  2473. uint32 default_max_size =
  2474. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2475. if (!is_memory64 && init_size > default_max_size) {
  2476. set_error_buf(error_buf, error_buf_size,
  2477. "memory size must be at most 65536 pages (4GiB)");
  2478. return false;
  2479. }
  2480. #if WASM_ENABLE_MEMORY64 != 0
  2481. else if (is_memory64 && init_size > default_max_size) {
  2482. set_error_buf(
  2483. error_buf, error_buf_size,
  2484. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2485. return false;
  2486. }
  2487. #endif
  2488. return true;
  2489. }
  2490. static bool
  2491. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2492. char *error_buf, uint32 error_buf_size)
  2493. {
  2494. uint32 default_max_size =
  2495. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2496. if (max_size < init_size) {
  2497. set_error_buf(error_buf, error_buf_size,
  2498. "size minimum must not be greater than maximum");
  2499. return false;
  2500. }
  2501. if (!is_memory64 && max_size > default_max_size) {
  2502. set_error_buf(error_buf, error_buf_size,
  2503. "memory size must be at most 65536 pages (4GiB)");
  2504. return false;
  2505. }
  2506. #if WASM_ENABLE_MEMORY64 != 0
  2507. else if (is_memory64 && max_size > default_max_size) {
  2508. set_error_buf(
  2509. error_buf, error_buf_size,
  2510. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2511. return false;
  2512. }
  2513. #endif
  2514. return true;
  2515. }
  2516. static bool
  2517. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2518. WASMModule *parent_module, const char *sub_module_name,
  2519. const char *memory_name, WASMMemoryImport *memory,
  2520. char *error_buf, uint32 error_buf_size)
  2521. {
  2522. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2523. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2524. uint32 pool_size = wasm_runtime_memory_pool_size();
  2525. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2526. / DEFAULT_NUM_BYTES_PER_PAGE;
  2527. #else
  2528. uint32 max_page_count;
  2529. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2530. uint32 mem_flag = 0;
  2531. bool is_memory64 = false;
  2532. uint32 declare_init_page_count = 0;
  2533. uint32 declare_max_page_count = 0;
  2534. #if WASM_ENABLE_MULTI_MODULE != 0
  2535. WASMModule *sub_module = NULL;
  2536. WASMMemory *linked_memory = NULL;
  2537. #endif
  2538. p_org = p;
  2539. read_leb_uint32(p, p_end, mem_flag);
  2540. is_memory64 = mem_flag & MEMORY64_FLAG;
  2541. if (p - p_org > 1) {
  2542. LOG_VERBOSE("integer representation too long");
  2543. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2544. return false;
  2545. }
  2546. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2547. return false;
  2548. }
  2549. read_leb_uint32(p, p_end, declare_init_page_count);
  2550. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2551. error_buf_size)) {
  2552. return false;
  2553. }
  2554. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2555. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2556. #endif
  2557. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2558. read_leb_uint32(p, p_end, declare_max_page_count);
  2559. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2560. declare_max_page_count, error_buf,
  2561. error_buf_size)) {
  2562. return false;
  2563. }
  2564. if (declare_max_page_count > max_page_count) {
  2565. declare_max_page_count = max_page_count;
  2566. }
  2567. }
  2568. else {
  2569. /* Limit the maximum memory size to max_page_count */
  2570. declare_max_page_count = max_page_count;
  2571. }
  2572. #if WASM_ENABLE_MULTI_MODULE != 0
  2573. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2574. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2575. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2576. error_buf_size);
  2577. if (!sub_module) {
  2578. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2579. /* Avoid memory import failure when wasi-threads is enabled
  2580. and the memory is shared */
  2581. if (!(mem_flag & SHARED_MEMORY_FLAG))
  2582. return false;
  2583. #else
  2584. return false;
  2585. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2586. }
  2587. else {
  2588. linked_memory = wasm_loader_resolve_memory(
  2589. sub_module_name, memory_name, declare_init_page_count,
  2590. declare_max_page_count, error_buf, error_buf_size);
  2591. if (!linked_memory) {
  2592. return false;
  2593. }
  2594. /**
  2595. * reset with linked memory limit
  2596. */
  2597. memory->import_module = sub_module;
  2598. memory->import_memory_linked = linked_memory;
  2599. declare_init_page_count = linked_memory->init_page_count;
  2600. declare_max_page_count = linked_memory->max_page_count;
  2601. }
  2602. }
  2603. #endif
  2604. /* (memory (export "memory") 1 2) */
  2605. if (!strcmp("spectest", sub_module_name)) {
  2606. uint32 spectest_memory_init_page = 1;
  2607. uint32 spectest_memory_max_page = 2;
  2608. if (strcmp("memory", memory_name)) {
  2609. set_error_buf(error_buf, error_buf_size,
  2610. "incompatible import type or unknown import");
  2611. return false;
  2612. }
  2613. if (declare_init_page_count > spectest_memory_init_page
  2614. || declare_max_page_count < spectest_memory_max_page) {
  2615. set_error_buf(error_buf, error_buf_size,
  2616. "incompatible import type");
  2617. return false;
  2618. }
  2619. declare_init_page_count = spectest_memory_init_page;
  2620. declare_max_page_count = spectest_memory_max_page;
  2621. }
  2622. /* now we believe all declaration are ok */
  2623. memory->mem_type.flags = mem_flag;
  2624. memory->mem_type.init_page_count = declare_init_page_count;
  2625. memory->mem_type.max_page_count = declare_max_page_count;
  2626. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2627. *p_buf = p;
  2628. (void)parent_module;
  2629. return true;
  2630. fail:
  2631. return false;
  2632. }
  2633. #if WASM_ENABLE_TAGS != 0
  2634. static bool
  2635. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2636. const WASMModule *parent_module, /* this module ! */
  2637. const char *sub_module_name, const char *tag_name,
  2638. WASMTagImport *tag, /* structure to fill */
  2639. char *error_buf, uint32 error_buf_size)
  2640. {
  2641. /* attribute and type of the import statement */
  2642. uint8 declare_tag_attribute;
  2643. uint32 declare_type_index;
  2644. const uint8 *p = *p_buf, *p_end = buf_end;
  2645. #if WASM_ENABLE_MULTI_MODULE != 0
  2646. WASMModule *sub_module = NULL;
  2647. #endif
  2648. /* get the one byte attribute */
  2649. CHECK_BUF(p, p_end, 1);
  2650. declare_tag_attribute = read_uint8(p);
  2651. if (declare_tag_attribute != 0) {
  2652. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2653. goto fail;
  2654. }
  2655. /* get type */
  2656. read_leb_uint32(p, p_end, declare_type_index);
  2657. /* compare against module->types */
  2658. if (declare_type_index >= parent_module->type_count) {
  2659. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2660. goto fail;
  2661. }
  2662. WASMFuncType *declare_tag_type =
  2663. (WASMFuncType *)parent_module->types[declare_type_index];
  2664. /* check, that the type of the declared tag returns void */
  2665. if (declare_tag_type->result_count != 0) {
  2666. set_error_buf(error_buf, error_buf_size,
  2667. "tag type signature does not return void");
  2668. goto fail;
  2669. }
  2670. #if WASM_ENABLE_MULTI_MODULE != 0
  2671. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2672. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2673. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2674. error_buf_size);
  2675. if (!sub_module) {
  2676. return false;
  2677. }
  2678. /* wasm_loader_resolve_tag checks, that the imported tag
  2679. * and the declared tag have the same type
  2680. */
  2681. uint32 linked_tag_index = 0;
  2682. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2683. sub_module_name, tag_name, declare_tag_type,
  2684. &linked_tag_index /* out */, error_buf, error_buf_size);
  2685. if (linked_tag) {
  2686. tag->import_module = sub_module;
  2687. tag->import_tag_linked = linked_tag;
  2688. tag->import_tag_index_linked = linked_tag_index;
  2689. }
  2690. }
  2691. #endif
  2692. /* store to module tag declarations */
  2693. tag->attribute = declare_tag_attribute;
  2694. tag->type = declare_type_index;
  2695. tag->module_name = (char *)sub_module_name;
  2696. tag->field_name = (char *)tag_name;
  2697. tag->tag_type = declare_tag_type;
  2698. *p_buf = p;
  2699. (void)parent_module;
  2700. LOG_VERBOSE("Load tag import success\n");
  2701. return true;
  2702. fail:
  2703. return false;
  2704. }
  2705. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2706. static bool
  2707. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2708. WASMModule *parent_module, char *sub_module_name,
  2709. char *global_name, WASMGlobalImport *global, char *error_buf,
  2710. uint32 error_buf_size)
  2711. {
  2712. const uint8 *p = *p_buf, *p_end = buf_end;
  2713. uint8 declare_type = 0;
  2714. uint8 declare_mutable = 0;
  2715. #if WASM_ENABLE_MULTI_MODULE != 0
  2716. WASMModule *sub_module = NULL;
  2717. WASMGlobal *linked_global = NULL;
  2718. #endif
  2719. #if WASM_ENABLE_GC != 0
  2720. WASMRefType ref_type;
  2721. bool need_ref_type_map;
  2722. #endif
  2723. bool ret = false;
  2724. #if WASM_ENABLE_GC == 0
  2725. CHECK_BUF(p, p_end, 2);
  2726. /* global type */
  2727. declare_type = read_uint8(p);
  2728. if (!is_value_type(declare_type)) {
  2729. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2730. return false;
  2731. }
  2732. declare_mutable = read_uint8(p);
  2733. #else
  2734. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2735. &need_ref_type_map, &ref_type, false, error_buf,
  2736. error_buf_size)) {
  2737. return false;
  2738. }
  2739. declare_type = ref_type.ref_type;
  2740. if (need_ref_type_map) {
  2741. if (!(global->ref_type =
  2742. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2743. error_buf, error_buf_size))) {
  2744. return false;
  2745. }
  2746. }
  2747. #if TRACE_WASM_LOADER != 0
  2748. os_printf("import global type: ");
  2749. wasm_dump_value_type(declare_type, global->ref_type);
  2750. os_printf("\n");
  2751. #endif
  2752. CHECK_BUF(p, p_end, 1);
  2753. declare_mutable = read_uint8(p);
  2754. #endif /* end of WASM_ENABLE_GC == 0 */
  2755. *p_buf = p;
  2756. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2757. return false;
  2758. }
  2759. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2760. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2761. global);
  2762. if (ret) {
  2763. if (global->type.val_type != declare_type
  2764. || global->type.is_mutable != declare_mutable) {
  2765. set_error_buf(error_buf, error_buf_size,
  2766. "incompatible import type");
  2767. return false;
  2768. }
  2769. global->is_linked = true;
  2770. }
  2771. #endif
  2772. #if WASM_ENABLE_MULTI_MODULE != 0
  2773. if (!global->is_linked
  2774. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2775. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2776. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2777. error_buf_size);
  2778. if (!sub_module) {
  2779. return false;
  2780. }
  2781. /* check sub modules */
  2782. linked_global = wasm_loader_resolve_global(
  2783. sub_module_name, global_name, declare_type, declare_mutable,
  2784. error_buf, error_buf_size);
  2785. if (linked_global) {
  2786. global->import_module = sub_module;
  2787. global->import_global_linked = linked_global;
  2788. global->is_linked = true;
  2789. }
  2790. }
  2791. #endif
  2792. global->module_name = sub_module_name;
  2793. global->field_name = global_name;
  2794. global->type.val_type = declare_type;
  2795. global->type.is_mutable = (declare_mutable == 1);
  2796. #if WASM_ENABLE_WAMR_COMPILER != 0
  2797. if (global->type.val_type == VALUE_TYPE_V128)
  2798. parent_module->is_simd_used = true;
  2799. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2800. parent_module->is_ref_types_used = true;
  2801. #endif
  2802. (void)parent_module;
  2803. (void)ret;
  2804. return true;
  2805. fail:
  2806. return false;
  2807. }
  2808. static bool
  2809. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2810. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2811. {
  2812. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2813. #if WASM_ENABLE_GC != 0
  2814. WASMRefType ref_type;
  2815. bool need_ref_type_map;
  2816. #endif
  2817. #if WASM_ENABLE_GC == 0
  2818. CHECK_BUF(p, p_end, 1);
  2819. /* 0x70 or 0x6F */
  2820. table->elem_type = read_uint8(p);
  2821. if (VALUE_TYPE_FUNCREF != table->elem_type
  2822. #if WASM_ENABLE_REF_TYPES != 0
  2823. && VALUE_TYPE_EXTERNREF != table->elem_type
  2824. #endif
  2825. ) {
  2826. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2827. return false;
  2828. }
  2829. #else /* else of WASM_ENABLE_GC == 0 */
  2830. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2831. &need_ref_type_map, &ref_type, false, error_buf,
  2832. error_buf_size)) {
  2833. return false;
  2834. }
  2835. table->elem_type = ref_type.ref_type;
  2836. if (need_ref_type_map) {
  2837. if (!(table->elem_ref_type =
  2838. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2839. error_buf_size))) {
  2840. return false;
  2841. }
  2842. }
  2843. #if TRACE_WASM_LOADER != 0
  2844. os_printf("table type: ");
  2845. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2846. os_printf("\n");
  2847. #endif
  2848. #endif /* end of WASM_ENABLE_GC == 0 */
  2849. p_org = p;
  2850. read_leb_uint32(p, p_end, table->flags);
  2851. #if WASM_ENABLE_SHARED_MEMORY == 0
  2852. if (p - p_org > 1) {
  2853. set_error_buf(error_buf, error_buf_size,
  2854. "integer representation too long");
  2855. return false;
  2856. }
  2857. if (table->flags > 1) {
  2858. set_error_buf(error_buf, error_buf_size, "integer too large");
  2859. return false;
  2860. }
  2861. #else
  2862. if (p - p_org > 1) {
  2863. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2864. return false;
  2865. }
  2866. if (table->flags == 2) {
  2867. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2868. return false;
  2869. }
  2870. if (table->flags > 1) {
  2871. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2872. return false;
  2873. }
  2874. #endif
  2875. read_leb_uint32(p, p_end, table->init_size);
  2876. if (table->flags) {
  2877. read_leb_uint32(p, p_end, table->max_size);
  2878. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2879. error_buf_size))
  2880. return false;
  2881. }
  2882. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2883. #if WASM_ENABLE_WAMR_COMPILER != 0
  2884. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2885. module->is_ref_types_used = true;
  2886. #endif
  2887. *p_buf = p;
  2888. return true;
  2889. fail:
  2890. return false;
  2891. }
  2892. static bool
  2893. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2894. char *error_buf, uint32 error_buf_size)
  2895. {
  2896. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2897. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2898. uint32 pool_size = wasm_runtime_memory_pool_size();
  2899. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2900. / DEFAULT_NUM_BYTES_PER_PAGE;
  2901. #else
  2902. uint32 max_page_count;
  2903. #endif
  2904. bool is_memory64 = false;
  2905. p_org = p;
  2906. read_leb_uint32(p, p_end, memory->flags);
  2907. is_memory64 = memory->flags & MEMORY64_FLAG;
  2908. if (p - p_org > 1) {
  2909. LOG_VERBOSE("integer representation too long");
  2910. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2911. return false;
  2912. }
  2913. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2914. false)) {
  2915. return false;
  2916. }
  2917. read_leb_uint32(p, p_end, memory->init_page_count);
  2918. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2919. error_buf_size))
  2920. return false;
  2921. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2922. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2923. #endif
  2924. if (memory->flags & 1) {
  2925. read_leb_uint32(p, p_end, memory->max_page_count);
  2926. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2927. memory->max_page_count, error_buf,
  2928. error_buf_size))
  2929. return false;
  2930. if (memory->max_page_count > max_page_count)
  2931. memory->max_page_count = max_page_count;
  2932. }
  2933. else {
  2934. /* Limit the maximum memory size to max_page_count */
  2935. memory->max_page_count = max_page_count;
  2936. }
  2937. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2938. *p_buf = p;
  2939. return true;
  2940. fail:
  2941. return false;
  2942. }
  2943. static bool
  2944. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2945. bool is_load_from_file_buf, char *error_buf,
  2946. uint32 error_buf_size)
  2947. {
  2948. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2949. uint32 import_count, name_len, type_index, i, u32, flags;
  2950. uint64 total_size;
  2951. WASMImport *import;
  2952. WASMImport *import_functions = NULL, *import_tables = NULL;
  2953. WASMImport *import_memories = NULL, *import_globals = NULL;
  2954. #if WASM_ENABLE_TAGS != 0
  2955. WASMImport *import_tags = NULL;
  2956. #endif
  2957. char *sub_module_name, *field_name;
  2958. uint8 u8, kind, global_type;
  2959. read_leb_uint32(p, p_end, import_count);
  2960. if (import_count) {
  2961. module->import_count = import_count;
  2962. total_size = sizeof(WASMImport) * (uint64)import_count;
  2963. if (!(module->imports =
  2964. loader_malloc(total_size, error_buf, error_buf_size))) {
  2965. return false;
  2966. }
  2967. p_old = p;
  2968. /* Scan firstly to get import count of each type */
  2969. for (i = 0; i < import_count; i++) {
  2970. /* module name */
  2971. read_leb_uint32(p, p_end, name_len);
  2972. CHECK_BUF(p, p_end, name_len);
  2973. p += name_len;
  2974. /* field name */
  2975. read_leb_uint32(p, p_end, name_len);
  2976. CHECK_BUF(p, p_end, name_len);
  2977. p += name_len;
  2978. CHECK_BUF(p, p_end, 1);
  2979. /* 0x00/0x01/0x02/0x03/0x04 */
  2980. kind = read_uint8(p);
  2981. switch (kind) {
  2982. case IMPORT_KIND_FUNC: /* import function */
  2983. read_leb_uint32(p, p_end, type_index);
  2984. module->import_function_count++;
  2985. break;
  2986. case IMPORT_KIND_TABLE: /* import table */
  2987. CHECK_BUF(p, p_end, 1);
  2988. /* 0x70 */
  2989. u8 = read_uint8(p);
  2990. read_leb_uint32(p, p_end, flags);
  2991. read_leb_uint32(p, p_end, u32);
  2992. if (flags & 1)
  2993. read_leb_uint32(p, p_end, u32);
  2994. module->import_table_count++;
  2995. if (module->import_table_count > 1) {
  2996. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2997. set_error_buf(error_buf, error_buf_size,
  2998. "multiple tables");
  2999. return false;
  3000. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3001. module->is_ref_types_used = true;
  3002. #endif
  3003. }
  3004. break;
  3005. case IMPORT_KIND_MEMORY: /* import memory */
  3006. read_leb_uint32(p, p_end, flags);
  3007. read_leb_uint32(p, p_end, u32);
  3008. if (flags & 1)
  3009. read_leb_uint32(p, p_end, u32);
  3010. module->import_memory_count++;
  3011. if (module->import_memory_count > 1) {
  3012. set_error_buf(error_buf, error_buf_size,
  3013. "multiple memories");
  3014. return false;
  3015. }
  3016. break;
  3017. #if WASM_ENABLE_TAGS != 0
  3018. case IMPORT_KIND_TAG: /* import tags */
  3019. /* it only counts the number of tags to import */
  3020. module->import_tag_count++;
  3021. CHECK_BUF(p, p_end, 1);
  3022. u8 = read_uint8(p);
  3023. read_leb_uint32(p, p_end, type_index);
  3024. break;
  3025. #endif
  3026. case IMPORT_KIND_GLOBAL: /* import global */
  3027. #if WASM_ENABLE_GC != 0
  3028. /* valtype */
  3029. CHECK_BUF(p, p_end, 1);
  3030. global_type = read_uint8(p);
  3031. if (wasm_is_type_multi_byte_type(global_type)) {
  3032. int32 heap_type;
  3033. read_leb_int32(p, p_end, heap_type);
  3034. (void)heap_type;
  3035. }
  3036. /* mutability */
  3037. CHECK_BUF(p, p_end, 1);
  3038. p += 1;
  3039. #else
  3040. CHECK_BUF(p, p_end, 2);
  3041. p += 2;
  3042. #endif
  3043. (void)global_type;
  3044. module->import_global_count++;
  3045. break;
  3046. default:
  3047. set_error_buf(error_buf, error_buf_size,
  3048. "invalid import kind");
  3049. return false;
  3050. }
  3051. }
  3052. if (module->import_function_count)
  3053. import_functions = module->import_functions = module->imports;
  3054. if (module->import_table_count)
  3055. import_tables = module->import_tables =
  3056. module->imports + module->import_function_count;
  3057. if (module->import_memory_count)
  3058. import_memories = module->import_memories =
  3059. module->imports + module->import_function_count
  3060. + module->import_table_count;
  3061. #if WASM_ENABLE_TAGS != 0
  3062. if (module->import_tag_count)
  3063. import_tags = module->import_tags =
  3064. module->imports + module->import_function_count
  3065. + module->import_table_count + module->import_memory_count;
  3066. if (module->import_global_count)
  3067. import_globals = module->import_globals =
  3068. module->imports + module->import_function_count
  3069. + module->import_table_count + module->import_memory_count
  3070. + module->import_tag_count;
  3071. #else
  3072. if (module->import_global_count)
  3073. import_globals = module->import_globals =
  3074. module->imports + module->import_function_count
  3075. + module->import_table_count + module->import_memory_count;
  3076. #endif
  3077. p = p_old;
  3078. /* Scan again to resolve the data */
  3079. for (i = 0; i < import_count; i++) {
  3080. /* load module name */
  3081. read_leb_uint32(p, p_end, name_len);
  3082. CHECK_BUF(p, p_end, name_len);
  3083. if (!(sub_module_name = wasm_const_str_list_insert(
  3084. p, name_len, module, is_load_from_file_buf, error_buf,
  3085. error_buf_size))) {
  3086. return false;
  3087. }
  3088. p += name_len;
  3089. /* load field name */
  3090. read_leb_uint32(p, p_end, name_len);
  3091. CHECK_BUF(p, p_end, name_len);
  3092. if (!(field_name = wasm_const_str_list_insert(
  3093. p, name_len, module, is_load_from_file_buf, error_buf,
  3094. error_buf_size))) {
  3095. return false;
  3096. }
  3097. p += name_len;
  3098. CHECK_BUF(p, p_end, 1);
  3099. /* 0x00/0x01/0x02/0x03/0x4 */
  3100. kind = read_uint8(p);
  3101. switch (kind) {
  3102. case IMPORT_KIND_FUNC: /* import function */
  3103. bh_assert(import_functions);
  3104. import = import_functions++;
  3105. if (!load_function_import(
  3106. &p, p_end, module, sub_module_name, field_name,
  3107. &import->u.function, error_buf, error_buf_size)) {
  3108. return false;
  3109. }
  3110. break;
  3111. case IMPORT_KIND_TABLE: /* import table */
  3112. bh_assert(import_tables);
  3113. import = import_tables++;
  3114. if (!load_table_import(&p, p_end, module, sub_module_name,
  3115. field_name, &import->u.table,
  3116. error_buf, error_buf_size)) {
  3117. LOG_DEBUG("can not import such a table (%s,%s)",
  3118. sub_module_name, field_name);
  3119. return false;
  3120. }
  3121. break;
  3122. case IMPORT_KIND_MEMORY: /* import memory */
  3123. bh_assert(import_memories);
  3124. import = import_memories++;
  3125. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3126. field_name, &import->u.memory,
  3127. error_buf, error_buf_size)) {
  3128. return false;
  3129. }
  3130. break;
  3131. #if WASM_ENABLE_TAGS != 0
  3132. case IMPORT_KIND_TAG:
  3133. bh_assert(import_tags);
  3134. import = import_tags++;
  3135. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3136. field_name, &import->u.tag, error_buf,
  3137. error_buf_size)) {
  3138. return false;
  3139. }
  3140. break;
  3141. #endif
  3142. case IMPORT_KIND_GLOBAL: /* import global */
  3143. bh_assert(import_globals);
  3144. import = import_globals++;
  3145. if (!load_global_import(&p, p_end, module, sub_module_name,
  3146. field_name, &import->u.global,
  3147. error_buf, error_buf_size)) {
  3148. return false;
  3149. }
  3150. break;
  3151. default:
  3152. set_error_buf(error_buf, error_buf_size,
  3153. "invalid import kind");
  3154. return false;
  3155. }
  3156. import->kind = kind;
  3157. import->u.names.module_name = sub_module_name;
  3158. import->u.names.field_name = field_name;
  3159. }
  3160. #if WASM_ENABLE_LIBC_WASI != 0
  3161. import = module->import_functions;
  3162. for (i = 0; i < module->import_function_count; i++, import++) {
  3163. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3164. || !strcmp(import->u.names.module_name,
  3165. "wasi_snapshot_preview1")) {
  3166. module->import_wasi_api = true;
  3167. break;
  3168. }
  3169. }
  3170. #endif
  3171. }
  3172. if (p != p_end) {
  3173. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3174. return false;
  3175. }
  3176. LOG_VERBOSE("Load import section success.\n");
  3177. (void)u8;
  3178. (void)u32;
  3179. (void)type_index;
  3180. return true;
  3181. fail:
  3182. return false;
  3183. }
  3184. static bool
  3185. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3186. uint32 error_buf_size)
  3187. {
  3188. WASMFuncType *param_type = func->func_type;
  3189. uint32 param_count = param_type->param_count;
  3190. uint8 *param_types = param_type->types;
  3191. uint32 local_count = func->local_count;
  3192. uint8 *local_types = func->local_types;
  3193. uint32 i, local_offset = 0;
  3194. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3195. /*
  3196. * Only allocate memory when total_size is not 0,
  3197. * or the return value of malloc(0) might be NULL on some platforms,
  3198. * which causes wasm loader return false.
  3199. */
  3200. if (total_size > 0
  3201. && !(func->local_offsets =
  3202. loader_malloc(total_size, error_buf, error_buf_size))) {
  3203. return false;
  3204. }
  3205. for (i = 0; i < param_count; i++) {
  3206. func->local_offsets[i] = (uint16)local_offset;
  3207. local_offset += wasm_value_type_cell_num(param_types[i]);
  3208. }
  3209. for (i = 0; i < local_count; i++) {
  3210. func->local_offsets[param_count + i] = (uint16)local_offset;
  3211. local_offset += wasm_value_type_cell_num(local_types[i]);
  3212. }
  3213. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3214. return true;
  3215. }
  3216. static bool
  3217. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3218. const uint8 *buf_code, const uint8 *buf_code_end,
  3219. WASMModule *module, char *error_buf,
  3220. uint32 error_buf_size)
  3221. {
  3222. const uint8 *p = buf, *p_end = buf_end;
  3223. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3224. uint32 func_count;
  3225. uint64 total_size;
  3226. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3227. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3228. uint8 type;
  3229. WASMFunction *func;
  3230. #if WASM_ENABLE_GC != 0
  3231. bool need_ref_type_map;
  3232. WASMRefType ref_type;
  3233. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3234. #endif
  3235. read_leb_uint32(p, p_end, func_count);
  3236. if (buf_code)
  3237. read_leb_uint32(p_code, buf_code_end, code_count);
  3238. if (func_count != code_count) {
  3239. set_error_buf(error_buf, error_buf_size,
  3240. "function and code section have inconsistent lengths or "
  3241. "unexpected end");
  3242. return false;
  3243. }
  3244. if (func_count) {
  3245. module->function_count = func_count;
  3246. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3247. if (!(module->functions =
  3248. loader_malloc(total_size, error_buf, error_buf_size))) {
  3249. return false;
  3250. }
  3251. for (i = 0; i < func_count; i++) {
  3252. /* Resolve function type */
  3253. read_leb_uint32(p, p_end, type_index);
  3254. if (type_index >= module->type_count) {
  3255. set_error_buf(error_buf, error_buf_size, "unknown type");
  3256. return false;
  3257. }
  3258. #if WASM_ENABLE_GC != 0
  3259. type_index_org = type_index;
  3260. #endif
  3261. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3262. && WASM_ENABLE_GC == 0
  3263. type_index = wasm_get_smallest_type_idx(
  3264. module->types, module->type_count, type_index);
  3265. #endif
  3266. read_leb_uint32(p_code, buf_code_end, code_size);
  3267. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3268. set_error_buf(error_buf, error_buf_size,
  3269. "invalid function code size");
  3270. return false;
  3271. }
  3272. /* Resolve local set count */
  3273. p_code_end = p_code + code_size;
  3274. local_count = 0;
  3275. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3276. p_code_save = p_code;
  3277. #if WASM_ENABLE_GC != 0
  3278. ref_type_map_count = 0;
  3279. #endif
  3280. /* Calculate total local count */
  3281. for (j = 0; j < local_set_count; j++) {
  3282. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3283. if (sub_local_count > UINT32_MAX - local_count) {
  3284. set_error_buf(error_buf, error_buf_size, "too many locals");
  3285. return false;
  3286. }
  3287. #if WASM_ENABLE_GC == 0
  3288. CHECK_BUF(p_code, buf_code_end, 1);
  3289. /* 0x7F/0x7E/0x7D/0x7C */
  3290. type = read_uint8(p_code);
  3291. local_count += sub_local_count;
  3292. #if WASM_ENABLE_WAMR_COMPILER != 0
  3293. /* If any value's type is v128, mark the module as SIMD used */
  3294. if (type == VALUE_TYPE_V128)
  3295. module->is_simd_used = true;
  3296. #endif
  3297. #else
  3298. if (!resolve_value_type(&p_code, buf_code_end, module,
  3299. module->type_count, &need_ref_type_map,
  3300. &ref_type, false, error_buf,
  3301. error_buf_size)) {
  3302. return false;
  3303. }
  3304. local_count += sub_local_count;
  3305. if (need_ref_type_map)
  3306. ref_type_map_count += sub_local_count;
  3307. #endif
  3308. }
  3309. /* Alloc memory, layout: function structure + local types */
  3310. code_size = (uint32)(p_code_end - p_code);
  3311. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3312. if (!(func = module->functions[i] =
  3313. loader_malloc(total_size, error_buf, error_buf_size))) {
  3314. return false;
  3315. }
  3316. #if WASM_ENABLE_GC != 0
  3317. if (ref_type_map_count > 0) {
  3318. total_size =
  3319. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3320. if (!(func->local_ref_type_maps = loader_malloc(
  3321. total_size, error_buf, error_buf_size))) {
  3322. return false;
  3323. }
  3324. func->local_ref_type_map_count = ref_type_map_count;
  3325. }
  3326. #endif
  3327. /* Set function type, local count, code size and code body */
  3328. func->func_type = (WASMFuncType *)module->types[type_index];
  3329. func->local_count = local_count;
  3330. if (local_count > 0)
  3331. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3332. func->code_size = code_size;
  3333. /*
  3334. * we shall make a copy of code body [p_code, p_code + code_size]
  3335. * when we are worrying about inappropriate releasing behaviour.
  3336. * all code bodies are actually in a buffer which user allocates in
  3337. * his embedding environment and we don't have power on them.
  3338. * it will be like:
  3339. * code_body_cp = malloc(code_size);
  3340. * memcpy(code_body_cp, p_code, code_size);
  3341. * func->code = code_body_cp;
  3342. */
  3343. func->code = (uint8 *)p_code;
  3344. #if WASM_ENABLE_GC != 0
  3345. func->type_idx = type_index_org;
  3346. #endif
  3347. #if WASM_ENABLE_GC != 0
  3348. t = 0;
  3349. #endif
  3350. /* Load each local type */
  3351. p_code = p_code_save;
  3352. local_type_index = 0;
  3353. for (j = 0; j < local_set_count; j++) {
  3354. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3355. /* Note: sub_local_count is allowed to be 0 */
  3356. if (local_type_index > UINT32_MAX - sub_local_count
  3357. || local_type_index + sub_local_count > local_count) {
  3358. set_error_buf(error_buf, error_buf_size,
  3359. "invalid local count");
  3360. return false;
  3361. }
  3362. #if WASM_ENABLE_GC == 0
  3363. CHECK_BUF(p_code, buf_code_end, 1);
  3364. /* 0x7F/0x7E/0x7D/0x7C */
  3365. type = read_uint8(p_code);
  3366. if (!is_value_type(type)) {
  3367. if (type == VALUE_TYPE_V128)
  3368. set_error_buf(error_buf, error_buf_size,
  3369. "v128 value type requires simd feature");
  3370. else if (type == VALUE_TYPE_FUNCREF
  3371. || type == VALUE_TYPE_EXTERNREF)
  3372. set_error_buf(error_buf, error_buf_size,
  3373. "ref value type requires "
  3374. "reference types feature");
  3375. else
  3376. set_error_buf_v(error_buf, error_buf_size,
  3377. "invalid local type 0x%02X", type);
  3378. return false;
  3379. }
  3380. #else
  3381. if (!resolve_value_type(&p_code, buf_code_end, module,
  3382. module->type_count, &need_ref_type_map,
  3383. &ref_type, false, error_buf,
  3384. error_buf_size)) {
  3385. return false;
  3386. }
  3387. if (need_ref_type_map) {
  3388. WASMRefType *ref_type_tmp;
  3389. if (!(ref_type_tmp = reftype_set_insert(
  3390. module->ref_type_set, &ref_type, error_buf,
  3391. error_buf_size))) {
  3392. return false;
  3393. }
  3394. for (k = 0; k < sub_local_count; k++) {
  3395. func->local_ref_type_maps[t + k].ref_type =
  3396. ref_type_tmp;
  3397. func->local_ref_type_maps[t + k].index =
  3398. local_type_index + k;
  3399. }
  3400. t += sub_local_count;
  3401. }
  3402. type = ref_type.ref_type;
  3403. #endif
  3404. for (k = 0; k < sub_local_count; k++) {
  3405. func->local_types[local_type_index++] = type;
  3406. }
  3407. #if WASM_ENABLE_WAMR_COMPILER != 0
  3408. if (type == VALUE_TYPE_V128)
  3409. module->is_simd_used = true;
  3410. else if (type == VALUE_TYPE_FUNCREF
  3411. || type == VALUE_TYPE_EXTERNREF)
  3412. module->is_ref_types_used = true;
  3413. #endif
  3414. }
  3415. bh_assert(local_type_index == func->local_count);
  3416. #if WASM_ENABLE_GC != 0
  3417. bh_assert(t == func->local_ref_type_map_count);
  3418. #if TRACE_WASM_LOADER != 0
  3419. os_printf("func %u, local types: [", i);
  3420. k = 0;
  3421. for (j = 0; j < func->local_count; j++) {
  3422. WASMRefType *ref_type_tmp = NULL;
  3423. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3424. bh_assert(j == func->local_ref_type_maps[k].index);
  3425. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3426. }
  3427. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3428. if (j < func->local_count - 1)
  3429. os_printf(" ");
  3430. }
  3431. os_printf("]\n");
  3432. #endif
  3433. #endif
  3434. func->param_cell_num = func->func_type->param_cell_num;
  3435. func->ret_cell_num = func->func_type->ret_cell_num;
  3436. local_cell_num =
  3437. wasm_get_cell_num(func->local_types, func->local_count);
  3438. if (local_cell_num > UINT16_MAX) {
  3439. set_error_buf(error_buf, error_buf_size,
  3440. "local count too large");
  3441. return false;
  3442. }
  3443. func->local_cell_num = (uint16)local_cell_num;
  3444. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3445. return false;
  3446. p_code = p_code_end;
  3447. }
  3448. }
  3449. if (p != p_end) {
  3450. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3451. return false;
  3452. }
  3453. LOG_VERBOSE("Load function section success.\n");
  3454. return true;
  3455. fail:
  3456. return false;
  3457. }
  3458. static bool
  3459. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3460. char *error_buf, uint32 error_buf_size)
  3461. {
  3462. const uint8 *p = buf, *p_end = buf_end;
  3463. uint32 table_count, i;
  3464. uint64 total_size;
  3465. WASMTable *table;
  3466. read_leb_uint32(p, p_end, table_count);
  3467. if (module->import_table_count + table_count > 1) {
  3468. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3469. /* a total of one table is allowed */
  3470. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3471. return false;
  3472. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3473. module->is_ref_types_used = true;
  3474. #endif
  3475. }
  3476. if (table_count) {
  3477. module->table_count = table_count;
  3478. total_size = sizeof(WASMTable) * (uint64)table_count;
  3479. if (!(module->tables =
  3480. loader_malloc(total_size, error_buf, error_buf_size))) {
  3481. return false;
  3482. }
  3483. /* load each table */
  3484. table = module->tables;
  3485. for (i = 0; i < table_count; i++, table++) {
  3486. #if WASM_ENABLE_GC != 0
  3487. uint8 flag;
  3488. bool has_init = false;
  3489. CHECK_BUF(buf, buf_end, 1);
  3490. flag = read_uint8(p);
  3491. if (flag == TABLE_INIT_EXPR_FLAG) {
  3492. CHECK_BUF(buf, buf_end, 1);
  3493. flag = read_uint8(p);
  3494. if (flag != 0x00) {
  3495. set_error_buf(error_buf, error_buf_size,
  3496. "invalid leading bytes for table");
  3497. return false;
  3498. }
  3499. has_init = true;
  3500. }
  3501. else {
  3502. p--;
  3503. }
  3504. #endif /* end of WASM_ENABLE_GC != 0 */
  3505. if (!load_table(&p, p_end, module, table, error_buf,
  3506. error_buf_size))
  3507. return false;
  3508. #if WASM_ENABLE_GC != 0
  3509. if (has_init) {
  3510. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3511. table->elem_type, table->elem_ref_type,
  3512. error_buf, error_buf_size))
  3513. return false;
  3514. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3515. && table->init_expr.init_expr_type
  3516. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3517. set_error_buf(
  3518. error_buf, error_buf_size,
  3519. "unsupported initializer expression for table");
  3520. return false;
  3521. }
  3522. }
  3523. else {
  3524. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3525. set_error_buf(
  3526. error_buf, error_buf_size,
  3527. "type mismatch: non-nullable table without init expr");
  3528. return false;
  3529. }
  3530. }
  3531. #endif /* end of WASM_ENABLE_GC != 0 */
  3532. #if WASM_ENABLE_WAMR_COMPILER != 0
  3533. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3534. module->is_ref_types_used = true;
  3535. #endif
  3536. }
  3537. }
  3538. if (p != p_end) {
  3539. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3540. return false;
  3541. }
  3542. LOG_VERBOSE("Load table section success.\n");
  3543. return true;
  3544. fail:
  3545. return false;
  3546. }
  3547. static bool
  3548. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3549. char *error_buf, uint32 error_buf_size)
  3550. {
  3551. const uint8 *p = buf, *p_end = buf_end;
  3552. uint32 memory_count, i;
  3553. uint64 total_size;
  3554. WASMMemory *memory;
  3555. read_leb_uint32(p, p_end, memory_count);
  3556. /* a total of one memory is allowed */
  3557. if (module->import_memory_count + memory_count > 1) {
  3558. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3559. return false;
  3560. }
  3561. if (memory_count) {
  3562. module->memory_count = memory_count;
  3563. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3564. if (!(module->memories =
  3565. loader_malloc(total_size, error_buf, error_buf_size))) {
  3566. return false;
  3567. }
  3568. /* load each memory */
  3569. memory = module->memories;
  3570. for (i = 0; i < memory_count; i++, memory++)
  3571. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3572. return false;
  3573. }
  3574. if (p != p_end) {
  3575. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3576. return false;
  3577. }
  3578. LOG_VERBOSE("Load memory section success.\n");
  3579. return true;
  3580. fail:
  3581. return false;
  3582. }
  3583. static bool
  3584. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3585. char *error_buf, uint32 error_buf_size)
  3586. {
  3587. const uint8 *p = buf, *p_end = buf_end;
  3588. uint32 global_count, i;
  3589. uint64 total_size;
  3590. WASMGlobal *global;
  3591. uint8 mutable;
  3592. #if WASM_ENABLE_GC != 0
  3593. bool need_ref_type_map;
  3594. WASMRefType ref_type;
  3595. #endif
  3596. read_leb_uint32(p, p_end, global_count);
  3597. module->global_count = 0;
  3598. if (global_count) {
  3599. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3600. if (!(module->globals =
  3601. loader_malloc(total_size, error_buf, error_buf_size))) {
  3602. return false;
  3603. }
  3604. global = module->globals;
  3605. for (i = 0; i < global_count; i++, global++) {
  3606. #if WASM_ENABLE_GC == 0
  3607. CHECK_BUF(p, p_end, 2);
  3608. /* global type */
  3609. global->type.val_type = read_uint8(p);
  3610. if (!is_value_type(global->type.val_type)) {
  3611. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3612. return false;
  3613. }
  3614. mutable = read_uint8(p);
  3615. #else
  3616. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3617. &need_ref_type_map, &ref_type, false,
  3618. error_buf, error_buf_size)) {
  3619. return false;
  3620. }
  3621. global->type.val_type = ref_type.ref_type;
  3622. CHECK_BUF(p, p_end, 1);
  3623. mutable = read_uint8(p);
  3624. #endif /* end of WASM_ENABLE_GC */
  3625. #if WASM_ENABLE_WAMR_COMPILER != 0
  3626. if (global->type.val_type == VALUE_TYPE_V128)
  3627. module->is_simd_used = true;
  3628. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3629. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3630. module->is_ref_types_used = true;
  3631. #endif
  3632. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3633. return false;
  3634. }
  3635. global->type.is_mutable = mutable ? true : false;
  3636. /* initialize expression */
  3637. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3638. global->type.val_type,
  3639. #if WASM_ENABLE_GC == 0
  3640. NULL,
  3641. #else
  3642. &ref_type,
  3643. #endif
  3644. error_buf, error_buf_size))
  3645. return false;
  3646. #if WASM_ENABLE_GC != 0
  3647. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3648. uint8 global_type;
  3649. WASMRefType *global_ref_type;
  3650. uint32 global_idx = global->init_expr.u.global_index;
  3651. if (global->init_expr.u.global_index
  3652. >= module->import_global_count + i) {
  3653. set_error_buf(error_buf, error_buf_size, "unknown global");
  3654. return false;
  3655. }
  3656. if (global_idx < module->import_global_count) {
  3657. global_type = module->import_globals[global_idx]
  3658. .u.global.type.val_type;
  3659. global_ref_type =
  3660. module->import_globals[global_idx].u.global.ref_type;
  3661. }
  3662. else {
  3663. global_type =
  3664. module
  3665. ->globals[global_idx - module->import_global_count]
  3666. .type.val_type;
  3667. global_ref_type =
  3668. module
  3669. ->globals[global_idx - module->import_global_count]
  3670. .ref_type;
  3671. }
  3672. if (!wasm_reftype_is_subtype_of(
  3673. global_type, global_ref_type, global->type.val_type,
  3674. global->ref_type, module->types, module->type_count)) {
  3675. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3676. return false;
  3677. }
  3678. }
  3679. if (need_ref_type_map) {
  3680. if (!(global->ref_type =
  3681. reftype_set_insert(module->ref_type_set, &ref_type,
  3682. error_buf, error_buf_size))) {
  3683. return false;
  3684. }
  3685. }
  3686. #if TRACE_WASM_LOADER != 0
  3687. os_printf("global type: ");
  3688. wasm_dump_value_type(global->type, global->ref_type);
  3689. os_printf("\n");
  3690. #endif
  3691. #endif
  3692. module->global_count++;
  3693. }
  3694. bh_assert(module->global_count == global_count);
  3695. }
  3696. if (p != p_end) {
  3697. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3698. return false;
  3699. }
  3700. LOG_VERBOSE("Load global section success.\n");
  3701. return true;
  3702. fail:
  3703. return false;
  3704. }
  3705. static bool
  3706. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3707. bool is_load_from_file_buf, char *error_buf,
  3708. uint32 error_buf_size)
  3709. {
  3710. const uint8 *p = buf, *p_end = buf_end;
  3711. uint32 export_count, i, j, index;
  3712. uint64 total_size;
  3713. uint32 str_len;
  3714. WASMExport *export;
  3715. const char *name;
  3716. read_leb_uint32(p, p_end, export_count);
  3717. if (export_count) {
  3718. module->export_count = export_count;
  3719. total_size = sizeof(WASMExport) * (uint64)export_count;
  3720. if (!(module->exports =
  3721. loader_malloc(total_size, error_buf, error_buf_size))) {
  3722. return false;
  3723. }
  3724. export = module->exports;
  3725. for (i = 0; i < export_count; i++, export ++) {
  3726. #if WASM_ENABLE_THREAD_MGR == 0
  3727. if (p == p_end) {
  3728. /* export section with inconsistent count:
  3729. n export declared, but less than n given */
  3730. set_error_buf(error_buf, error_buf_size,
  3731. "length out of bounds");
  3732. return false;
  3733. }
  3734. #endif
  3735. read_leb_uint32(p, p_end, str_len);
  3736. CHECK_BUF(p, p_end, str_len);
  3737. for (j = 0; j < i; j++) {
  3738. name = module->exports[j].name;
  3739. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3740. set_error_buf(error_buf, error_buf_size,
  3741. "duplicate export name");
  3742. return false;
  3743. }
  3744. }
  3745. if (!(export->name = wasm_const_str_list_insert(
  3746. p, str_len, module, is_load_from_file_buf, error_buf,
  3747. error_buf_size))) {
  3748. return false;
  3749. }
  3750. p += str_len;
  3751. CHECK_BUF(p, p_end, 1);
  3752. export->kind = read_uint8(p);
  3753. read_leb_uint32(p, p_end, index);
  3754. export->index = index;
  3755. switch (export->kind) {
  3756. /* function index */
  3757. case EXPORT_KIND_FUNC:
  3758. if (index >= module->function_count
  3759. + module->import_function_count) {
  3760. set_error_buf(error_buf, error_buf_size,
  3761. "unknown function");
  3762. return false;
  3763. }
  3764. #if WASM_ENABLE_SIMD != 0
  3765. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3766. /* TODO: check func type, if it has v128 param or result,
  3767. report error */
  3768. #endif
  3769. #endif
  3770. break;
  3771. /* table index */
  3772. case EXPORT_KIND_TABLE:
  3773. if (index
  3774. >= module->table_count + module->import_table_count) {
  3775. set_error_buf(error_buf, error_buf_size,
  3776. "unknown table");
  3777. return false;
  3778. }
  3779. break;
  3780. /* memory index */
  3781. case EXPORT_KIND_MEMORY:
  3782. if (index
  3783. >= module->memory_count + module->import_memory_count) {
  3784. set_error_buf(error_buf, error_buf_size,
  3785. "unknown memory");
  3786. return false;
  3787. }
  3788. break;
  3789. #if WASM_ENABLE_TAGS != 0
  3790. /* export tag */
  3791. case EXPORT_KIND_TAG:
  3792. if (index >= module->tag_count + module->import_tag_count) {
  3793. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3794. return false;
  3795. }
  3796. break;
  3797. #endif
  3798. /* global index */
  3799. case EXPORT_KIND_GLOBAL:
  3800. if (index
  3801. >= module->global_count + module->import_global_count) {
  3802. set_error_buf(error_buf, error_buf_size,
  3803. "unknown global");
  3804. return false;
  3805. }
  3806. break;
  3807. default:
  3808. set_error_buf(error_buf, error_buf_size,
  3809. "invalid export kind");
  3810. return false;
  3811. }
  3812. }
  3813. }
  3814. if (p != p_end) {
  3815. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3816. return false;
  3817. }
  3818. LOG_VERBOSE("Load export section success.\n");
  3819. return true;
  3820. fail:
  3821. return false;
  3822. }
  3823. static bool
  3824. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3825. uint32 error_buf_size)
  3826. {
  3827. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3828. if (table_index != 0) {
  3829. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3830. return false;
  3831. }
  3832. #endif
  3833. if (table_index >= module->import_table_count + module->table_count) {
  3834. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3835. table_index);
  3836. return false;
  3837. }
  3838. return true;
  3839. }
  3840. static bool
  3841. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3842. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3843. {
  3844. const uint8 *p = *p_buf, *p_end = buf_end;
  3845. uint32 table_index;
  3846. read_leb_uint32(p, p_end, table_index);
  3847. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3848. return false;
  3849. }
  3850. *p_table_index = table_index;
  3851. *p_buf = p;
  3852. return true;
  3853. fail:
  3854. return false;
  3855. }
  3856. /* Element segments must match element type of table */
  3857. static bool
  3858. check_table_elem_type(WASMModule *module, uint32 table_index,
  3859. uint32 type_from_elem_seg, char *error_buf,
  3860. uint32 error_buf_size)
  3861. {
  3862. uint32 table_declared_elem_type;
  3863. if (table_index < module->import_table_count)
  3864. table_declared_elem_type =
  3865. module->import_tables[table_index].u.table.elem_type;
  3866. else
  3867. table_declared_elem_type = (module->tables + table_index)->elem_type;
  3868. if (table_declared_elem_type == type_from_elem_seg)
  3869. return true;
  3870. #if WASM_ENABLE_GC != 0
  3871. /*
  3872. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3873. */
  3874. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3875. return true;
  3876. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3877. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3878. return true;
  3879. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3880. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3881. return true;
  3882. #endif
  3883. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3884. return false;
  3885. }
  3886. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3887. static bool
  3888. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3889. uint32 *p_elem_type,
  3890. #if WASM_ENABLE_GC != 0
  3891. WASMRefType **p_elem_ref_type,
  3892. #endif
  3893. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3894. {
  3895. const uint8 *p = *p_buf, *p_end = buf_end;
  3896. uint8 elem_type;
  3897. #if WASM_ENABLE_GC != 0
  3898. WASMRefType elem_ref_type;
  3899. bool need_ref_type_map;
  3900. #endif
  3901. CHECK_BUF(p, p_end, 1);
  3902. elem_type = read_uint8(p);
  3903. if (elemkind_zero) {
  3904. if (elem_type != 0) {
  3905. set_error_buf(error_buf, error_buf_size,
  3906. "invalid reference type or unknown type");
  3907. return false;
  3908. }
  3909. else {
  3910. *p_elem_type = VALUE_TYPE_FUNCREF;
  3911. *p_buf = p;
  3912. return true;
  3913. }
  3914. }
  3915. #if WASM_ENABLE_GC == 0
  3916. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3917. set_error_buf(error_buf, error_buf_size,
  3918. "invalid reference type or unknown type");
  3919. return false;
  3920. }
  3921. *p_elem_type = elem_type;
  3922. #else
  3923. p--;
  3924. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3925. module->type_count, &need_ref_type_map,
  3926. &elem_ref_type, false, error_buf, error_buf_size)) {
  3927. return false;
  3928. }
  3929. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3930. set_error_buf(error_buf, error_buf_size,
  3931. "invalid reference type or unknown type");
  3932. return false;
  3933. }
  3934. *p_elem_type = elem_ref_type.ref_type;
  3935. if (need_ref_type_map) {
  3936. if (!(*p_elem_ref_type =
  3937. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3938. error_buf, error_buf_size))) {
  3939. return false;
  3940. }
  3941. }
  3942. #endif
  3943. *p_buf = p;
  3944. return true;
  3945. fail:
  3946. return false;
  3947. }
  3948. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3949. static bool
  3950. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3951. WASMModule *module, WASMTableSeg *table_segment,
  3952. char *error_buf, uint32 error_buf_size)
  3953. {
  3954. const uint8 *p = *p_buf, *p_end = buf_end;
  3955. uint32 function_count, function_index = 0, i;
  3956. uint64 total_size;
  3957. read_leb_uint32(p, p_end, function_count);
  3958. table_segment->value_count = function_count;
  3959. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3960. if (total_size > 0
  3961. && !(table_segment->init_values =
  3962. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3963. error_buf_size))) {
  3964. return false;
  3965. }
  3966. for (i = 0; i < function_count; i++) {
  3967. InitializerExpression *init_expr = &table_segment->init_values[i];
  3968. read_leb_uint32(p, p_end, function_index);
  3969. if (!check_function_index(module, function_index, error_buf,
  3970. error_buf_size)) {
  3971. return false;
  3972. }
  3973. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3974. init_expr->u.ref_index = function_index;
  3975. }
  3976. *p_buf = p;
  3977. return true;
  3978. fail:
  3979. return false;
  3980. }
  3981. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3982. static bool
  3983. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3984. WASMModule *module, WASMTableSeg *table_segment,
  3985. char *error_buf, uint32 error_buf_size)
  3986. {
  3987. const uint8 *p = *p_buf, *p_end = buf_end;
  3988. uint32 ref_count, i;
  3989. uint64 total_size;
  3990. read_leb_uint32(p, p_end, ref_count);
  3991. table_segment->value_count = ref_count;
  3992. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3993. if (total_size > 0
  3994. && !(table_segment->init_values =
  3995. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3996. error_buf_size))) {
  3997. return false;
  3998. }
  3999. for (i = 0; i < ref_count; i++) {
  4000. InitializerExpression *init_expr = &table_segment->init_values[i];
  4001. if (!load_init_expr(module, &p, p_end, init_expr,
  4002. table_segment->elem_type,
  4003. #if WASM_ENABLE_GC == 0
  4004. NULL,
  4005. #else
  4006. table_segment->elem_ref_type,
  4007. #endif
  4008. error_buf, error_buf_size))
  4009. return false;
  4010. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4011. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4012. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4013. && init_expr->init_expr_type
  4014. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4015. }
  4016. *p_buf = p;
  4017. return true;
  4018. fail:
  4019. return false;
  4020. }
  4021. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4022. static bool
  4023. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4024. WASMModule *module, char *error_buf,
  4025. uint32 error_buf_size)
  4026. {
  4027. const uint8 *p = buf, *p_end = buf_end;
  4028. uint32 table_segment_count, i;
  4029. uint64 total_size;
  4030. WASMTableSeg *table_segment;
  4031. read_leb_uint32(p, p_end, table_segment_count);
  4032. if (table_segment_count) {
  4033. module->table_seg_count = table_segment_count;
  4034. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4035. if (!(module->table_segments =
  4036. loader_malloc(total_size, error_buf, error_buf_size))) {
  4037. return false;
  4038. }
  4039. table_segment = module->table_segments;
  4040. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4041. if (p >= p_end) {
  4042. set_error_buf(error_buf, error_buf_size,
  4043. "invalid value type or "
  4044. "invalid elements segment kind");
  4045. return false;
  4046. }
  4047. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4048. read_leb_uint32(p, p_end, table_segment->mode);
  4049. /* last three bits */
  4050. table_segment->mode = table_segment->mode & 0x07;
  4051. switch (table_segment->mode) {
  4052. /* elemkind/elemtype + active */
  4053. case 0:
  4054. case 4:
  4055. {
  4056. #if WASM_ENABLE_GC != 0
  4057. if (table_segment->mode == 0) {
  4058. /* vec(funcidx), set elem type to (ref func) */
  4059. WASMRefType elem_ref_type = { 0 };
  4060. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4061. wasm_set_refheaptype_common(
  4062. &elem_ref_type.ref_ht_common, false,
  4063. HEAP_TYPE_FUNC);
  4064. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4065. module->ref_type_set, &elem_ref_type,
  4066. error_buf, error_buf_size)))
  4067. return false;
  4068. }
  4069. else {
  4070. /* vec(expr), set elem type to funcref */
  4071. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4072. }
  4073. #else
  4074. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4075. #endif
  4076. table_segment->table_index = 0;
  4077. if (!check_table_index(module, table_segment->table_index,
  4078. error_buf, error_buf_size))
  4079. return false;
  4080. if (!load_init_expr(
  4081. module, &p, p_end, &table_segment->base_offset,
  4082. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4083. return false;
  4084. if (table_segment->mode == 0) {
  4085. /* vec(funcidx) */
  4086. if (!load_func_index_vec(&p, p_end, module,
  4087. table_segment, error_buf,
  4088. error_buf_size))
  4089. return false;
  4090. }
  4091. else {
  4092. /* vec(expr) */
  4093. if (!load_init_expr_vec(&p, p_end, module,
  4094. table_segment, error_buf,
  4095. error_buf_size))
  4096. return false;
  4097. }
  4098. if (!check_table_elem_type(module,
  4099. table_segment->table_index,
  4100. table_segment->elem_type,
  4101. error_buf, error_buf_size))
  4102. return false;
  4103. break;
  4104. }
  4105. /* elemkind + passive/declarative */
  4106. case 1:
  4107. case 3:
  4108. if (!load_elem_type(module, &p, p_end,
  4109. &table_segment->elem_type,
  4110. #if WASM_ENABLE_GC != 0
  4111. &table_segment->elem_ref_type,
  4112. #endif
  4113. true, error_buf, error_buf_size))
  4114. return false;
  4115. /* vec(funcidx) */
  4116. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4117. error_buf, error_buf_size))
  4118. return false;
  4119. break;
  4120. /* elemkind/elemtype + table_idx + active */
  4121. case 2:
  4122. case 6:
  4123. if (!load_table_index(&p, p_end, module,
  4124. &table_segment->table_index,
  4125. error_buf, error_buf_size))
  4126. return false;
  4127. if (!load_init_expr(
  4128. module, &p, p_end, &table_segment->base_offset,
  4129. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4130. return false;
  4131. if (!load_elem_type(module, &p, p_end,
  4132. &table_segment->elem_type,
  4133. #if WASM_ENABLE_GC != 0
  4134. &table_segment->elem_ref_type,
  4135. #endif
  4136. table_segment->mode == 2 ? true : false,
  4137. error_buf, error_buf_size))
  4138. return false;
  4139. if (table_segment->mode == 2) {
  4140. /* vec(funcidx) */
  4141. if (!load_func_index_vec(&p, p_end, module,
  4142. table_segment, error_buf,
  4143. error_buf_size))
  4144. return false;
  4145. }
  4146. else {
  4147. /* vec(expr) */
  4148. if (!load_init_expr_vec(&p, p_end, module,
  4149. table_segment, error_buf,
  4150. error_buf_size))
  4151. return false;
  4152. }
  4153. if (!check_table_elem_type(module,
  4154. table_segment->table_index,
  4155. table_segment->elem_type,
  4156. error_buf, error_buf_size))
  4157. return false;
  4158. break;
  4159. case 5:
  4160. case 7:
  4161. if (!load_elem_type(module, &p, p_end,
  4162. &table_segment->elem_type,
  4163. #if WASM_ENABLE_GC != 0
  4164. &table_segment->elem_ref_type,
  4165. #endif
  4166. false, error_buf, error_buf_size))
  4167. return false;
  4168. /* vec(expr) */
  4169. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4170. error_buf, error_buf_size))
  4171. return false;
  4172. break;
  4173. default:
  4174. set_error_buf(error_buf, error_buf_size,
  4175. "unknown element segment kind");
  4176. return false;
  4177. }
  4178. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4179. /*
  4180. * like: 00 41 05 0b 04 00 01 00 01
  4181. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4182. */
  4183. if (!load_table_index(&p, p_end, module,
  4184. &table_segment->table_index, error_buf,
  4185. error_buf_size))
  4186. return false;
  4187. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4188. VALUE_TYPE_I32, NULL, error_buf,
  4189. error_buf_size))
  4190. return false;
  4191. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4192. error_buf, error_buf_size))
  4193. return false;
  4194. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4195. if (!check_table_elem_type(module, table_segment->table_index,
  4196. table_segment->elem_type, error_buf,
  4197. error_buf_size))
  4198. return false;
  4199. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4200. #if WASM_ENABLE_WAMR_COMPILER != 0
  4201. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4202. module->is_ref_types_used = true;
  4203. #endif
  4204. }
  4205. }
  4206. if (p != p_end) {
  4207. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4208. return false;
  4209. }
  4210. LOG_VERBOSE("Load table segment section success.\n");
  4211. return true;
  4212. fail:
  4213. return false;
  4214. }
  4215. static bool
  4216. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4217. WASMModule *module,
  4218. #if WASM_ENABLE_BULK_MEMORY != 0
  4219. bool has_datacount_section,
  4220. #endif
  4221. bool clone_data_seg, char *error_buf,
  4222. uint32 error_buf_size)
  4223. {
  4224. const uint8 *p = buf, *p_end = buf_end;
  4225. uint32 data_seg_count, i, mem_index, data_seg_len;
  4226. uint64 total_size;
  4227. WASMDataSeg *dataseg;
  4228. InitializerExpression init_expr;
  4229. #if WASM_ENABLE_BULK_MEMORY != 0
  4230. bool is_passive = false;
  4231. uint32 mem_flag;
  4232. #endif
  4233. uint8 mem_offset_type = VALUE_TYPE_I32;
  4234. read_leb_uint32(p, p_end, data_seg_count);
  4235. #if WASM_ENABLE_BULK_MEMORY != 0
  4236. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4237. set_error_buf(error_buf, error_buf_size,
  4238. "data count and data section have inconsistent lengths");
  4239. return false;
  4240. }
  4241. #endif
  4242. if (data_seg_count) {
  4243. module->data_seg_count = data_seg_count;
  4244. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4245. if (!(module->data_segments =
  4246. loader_malloc(total_size, error_buf, error_buf_size))) {
  4247. return false;
  4248. }
  4249. for (i = 0; i < data_seg_count; i++) {
  4250. read_leb_uint32(p, p_end, mem_index);
  4251. #if WASM_ENABLE_BULK_MEMORY != 0
  4252. is_passive = false;
  4253. mem_flag = mem_index & 0x03;
  4254. switch (mem_flag) {
  4255. case 0x01:
  4256. is_passive = true;
  4257. #if WASM_ENABLE_WAMR_COMPILER != 0
  4258. module->is_bulk_memory_used = true;
  4259. #endif
  4260. break;
  4261. case 0x00:
  4262. /* no memory index, treat index as 0 */
  4263. mem_index = 0;
  4264. goto check_mem_index;
  4265. case 0x02:
  4266. /* read following memory index */
  4267. read_leb_uint32(p, p_end, mem_index);
  4268. #if WASM_ENABLE_WAMR_COMPILER != 0
  4269. module->is_bulk_memory_used = true;
  4270. #endif
  4271. check_mem_index:
  4272. if (mem_index
  4273. >= module->import_memory_count + module->memory_count) {
  4274. set_error_buf_v(error_buf, error_buf_size,
  4275. "unknown memory %d", mem_index);
  4276. return false;
  4277. }
  4278. break;
  4279. case 0x03:
  4280. default:
  4281. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4282. return false;
  4283. break;
  4284. }
  4285. #else
  4286. if (mem_index
  4287. >= module->import_memory_count + module->memory_count) {
  4288. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4289. mem_index);
  4290. return false;
  4291. }
  4292. #endif /* WASM_ENABLE_BULK_MEMORY */
  4293. #if WASM_ENABLE_BULK_MEMORY != 0
  4294. if (!is_passive)
  4295. #endif
  4296. {
  4297. #if WASM_ENABLE_MEMORY64 != 0
  4298. /* This memory_flag is from memory instead of data segment */
  4299. uint8 memory_flag;
  4300. if (module->import_memory_count > 0) {
  4301. memory_flag = module->import_memories[mem_index]
  4302. .u.memory.mem_type.flags;
  4303. }
  4304. else {
  4305. memory_flag =
  4306. module
  4307. ->memories[mem_index - module->import_memory_count]
  4308. .flags;
  4309. }
  4310. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4311. : VALUE_TYPE_I32;
  4312. #else
  4313. mem_offset_type = VALUE_TYPE_I32;
  4314. #endif
  4315. }
  4316. #if WASM_ENABLE_BULK_MEMORY != 0
  4317. if (!is_passive)
  4318. #endif
  4319. if (!load_init_expr(module, &p, p_end, &init_expr,
  4320. mem_offset_type, NULL, error_buf,
  4321. error_buf_size))
  4322. return false;
  4323. read_leb_uint32(p, p_end, data_seg_len);
  4324. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4325. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4326. return false;
  4327. }
  4328. #if WASM_ENABLE_BULK_MEMORY != 0
  4329. dataseg->is_passive = is_passive;
  4330. if (!is_passive)
  4331. #endif
  4332. {
  4333. bh_memcpy_s(&dataseg->base_offset,
  4334. sizeof(InitializerExpression), &init_expr,
  4335. sizeof(InitializerExpression));
  4336. dataseg->memory_index = mem_index;
  4337. }
  4338. dataseg->data_length = data_seg_len;
  4339. CHECK_BUF(p, p_end, data_seg_len);
  4340. if (clone_data_seg) {
  4341. if (!(dataseg->data = loader_malloc(
  4342. dataseg->data_length, error_buf, error_buf_size))) {
  4343. return false;
  4344. }
  4345. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4346. data_seg_len);
  4347. }
  4348. else {
  4349. dataseg->data = (uint8 *)p;
  4350. }
  4351. dataseg->is_data_cloned = clone_data_seg;
  4352. p += data_seg_len;
  4353. }
  4354. }
  4355. if (p != p_end) {
  4356. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4357. return false;
  4358. }
  4359. LOG_VERBOSE("Load data segment section success.\n");
  4360. return true;
  4361. fail:
  4362. return false;
  4363. }
  4364. #if WASM_ENABLE_BULK_MEMORY != 0
  4365. static bool
  4366. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4367. WASMModule *module, char *error_buf,
  4368. uint32 error_buf_size)
  4369. {
  4370. const uint8 *p = buf, *p_end = buf_end;
  4371. uint32 data_seg_count1 = 0;
  4372. read_leb_uint32(p, p_end, data_seg_count1);
  4373. module->data_seg_count1 = data_seg_count1;
  4374. if (p != p_end) {
  4375. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4376. return false;
  4377. }
  4378. #if WASM_ENABLE_WAMR_COMPILER != 0
  4379. module->is_bulk_memory_used = true;
  4380. #endif
  4381. LOG_VERBOSE("Load datacount section success.\n");
  4382. return true;
  4383. fail:
  4384. return false;
  4385. }
  4386. #endif
  4387. #if WASM_ENABLE_TAGS != 0
  4388. static bool
  4389. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4390. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4391. uint32 error_buf_size)
  4392. {
  4393. (void)buf_code;
  4394. (void)buf_code_end;
  4395. const uint8 *p = buf, *p_end = buf_end;
  4396. size_t total_size = 0;
  4397. /* number of tags defined in the section */
  4398. uint32 section_tag_count = 0;
  4399. uint8 tag_attribute;
  4400. uint32 tag_type;
  4401. WASMTag *tag = NULL;
  4402. /* get tag count */
  4403. read_leb_uint32(p, p_end, section_tag_count);
  4404. module->tag_count = section_tag_count;
  4405. if (section_tag_count) {
  4406. total_size = sizeof(WASMTag *) * module->tag_count;
  4407. if (!(module->tags =
  4408. loader_malloc(total_size, error_buf, error_buf_size))) {
  4409. return false;
  4410. }
  4411. /* load each tag, imported tags precede the tags */
  4412. uint32 tag_index;
  4413. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4414. /* get the one byte attribute */
  4415. CHECK_BUF(p, p_end, 1);
  4416. tag_attribute = read_uint8(p);
  4417. /* get type */
  4418. read_leb_uint32(p, p_end, tag_type);
  4419. /* compare against module->types */
  4420. if (tag_type >= module->type_count) {
  4421. set_error_buf(error_buf, error_buf_size, "unknown type");
  4422. return false;
  4423. }
  4424. /* get return type (must be 0) */
  4425. /* check, that the type of the referred tag returns void */
  4426. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4427. if (func_type->result_count != 0) {
  4428. set_error_buf(error_buf, error_buf_size,
  4429. "non-empty tag result type");
  4430. goto fail;
  4431. }
  4432. if (!(tag = module->tags[tag_index] = loader_malloc(
  4433. sizeof(WASMTag), error_buf, error_buf_size))) {
  4434. return false;
  4435. }
  4436. /* store to module tag declarations */
  4437. tag->attribute = tag_attribute;
  4438. tag->type = tag_type;
  4439. tag->tag_type = func_type;
  4440. }
  4441. }
  4442. if (p != p_end) {
  4443. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4444. return false;
  4445. }
  4446. LOG_VERBOSE("Load tag section success.\n");
  4447. return true;
  4448. fail:
  4449. return false;
  4450. }
  4451. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4452. static bool
  4453. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4454. const uint8 *buf_func_end, WASMModule *module,
  4455. char *error_buf, uint32 error_buf_size)
  4456. {
  4457. const uint8 *p = buf, *p_end = buf_end;
  4458. const uint8 *p_func = buf_func;
  4459. uint32 func_count = 0, code_count;
  4460. /* code has been loaded in function section, so pass it here, just check
  4461. * whether function and code section have inconsistent lengths */
  4462. read_leb_uint32(p, p_end, code_count);
  4463. if (buf_func)
  4464. read_leb_uint32(p_func, buf_func_end, func_count);
  4465. if (func_count != code_count) {
  4466. set_error_buf(error_buf, error_buf_size,
  4467. "function and code section have inconsistent lengths");
  4468. return false;
  4469. }
  4470. LOG_VERBOSE("Load code segment section success.\n");
  4471. (void)module;
  4472. return true;
  4473. fail:
  4474. return false;
  4475. }
  4476. static bool
  4477. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4478. char *error_buf, uint32 error_buf_size)
  4479. {
  4480. const uint8 *p = buf, *p_end = buf_end;
  4481. WASMFuncType *type;
  4482. uint32 start_function;
  4483. read_leb_uint32(p, p_end, start_function);
  4484. if (start_function
  4485. >= module->function_count + module->import_function_count) {
  4486. set_error_buf(error_buf, error_buf_size, "unknown function");
  4487. return false;
  4488. }
  4489. if (start_function < module->import_function_count)
  4490. type = module->import_functions[start_function].u.function.func_type;
  4491. else
  4492. type = module->functions[start_function - module->import_function_count]
  4493. ->func_type;
  4494. if (type->param_count != 0 || type->result_count != 0) {
  4495. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4496. return false;
  4497. }
  4498. module->start_function = start_function;
  4499. if (p != p_end) {
  4500. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4501. return false;
  4502. }
  4503. LOG_VERBOSE("Load start section success.\n");
  4504. return true;
  4505. fail:
  4506. return false;
  4507. }
  4508. #if WASM_ENABLE_STRINGREF != 0
  4509. static bool
  4510. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4511. WASMModule *module, bool is_load_from_file_buf,
  4512. char *error_buf, uint32 error_buf_size)
  4513. {
  4514. const uint8 *p = buf, *p_end = buf_end;
  4515. int32 deferred_count, immediate_count, string_length, i;
  4516. uint64 total_size;
  4517. read_leb_uint32(p, p_end, deferred_count);
  4518. read_leb_uint32(p, p_end, immediate_count);
  4519. /* proposal set deferred_count for future extension */
  4520. if (deferred_count != 0) {
  4521. goto fail;
  4522. }
  4523. if (immediate_count > 0) {
  4524. total_size = sizeof(char *) * (uint64)immediate_count;
  4525. if (!(module->string_literal_ptrs =
  4526. loader_malloc(total_size, error_buf, error_buf_size))) {
  4527. goto fail;
  4528. }
  4529. module->string_literal_count = immediate_count;
  4530. total_size = sizeof(uint32) * (uint64)immediate_count;
  4531. if (!(module->string_literal_lengths =
  4532. loader_malloc(total_size, error_buf, error_buf_size))) {
  4533. goto fail;
  4534. }
  4535. for (i = 0; i < immediate_count; i++) {
  4536. read_leb_uint32(p, p_end, string_length);
  4537. CHECK_BUF(p, p_end, string_length);
  4538. module->string_literal_ptrs[i] = p;
  4539. module->string_literal_lengths[i] = string_length;
  4540. p += string_length;
  4541. }
  4542. }
  4543. if (p != p_end) {
  4544. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4545. goto fail;
  4546. }
  4547. LOG_VERBOSE("Load stringref section success.\n");
  4548. return true;
  4549. fail:
  4550. return false;
  4551. }
  4552. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4553. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4554. static bool
  4555. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4556. bool is_load_from_file_buf, char *error_buf,
  4557. uint32 error_buf_size)
  4558. {
  4559. const uint8 *p = buf, *p_end = buf_end;
  4560. uint32 name_type, subsection_size;
  4561. uint32 previous_name_type = 0;
  4562. uint32 num_func_name;
  4563. uint32 func_index;
  4564. uint32 previous_func_index = ~0U;
  4565. uint32 func_name_len;
  4566. uint32 name_index;
  4567. int i = 0;
  4568. if (p >= p_end) {
  4569. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4570. return false;
  4571. }
  4572. while (p < p_end) {
  4573. read_leb_uint32(p, p_end, name_type);
  4574. if (i != 0) {
  4575. if (name_type == previous_name_type) {
  4576. set_error_buf(error_buf, error_buf_size,
  4577. "duplicate sub-section");
  4578. return false;
  4579. }
  4580. if (name_type < previous_name_type) {
  4581. set_error_buf(error_buf, error_buf_size,
  4582. "out-of-order sub-section");
  4583. return false;
  4584. }
  4585. }
  4586. previous_name_type = name_type;
  4587. read_leb_uint32(p, p_end, subsection_size);
  4588. CHECK_BUF(p, p_end, subsection_size);
  4589. switch (name_type) {
  4590. case SUB_SECTION_TYPE_FUNC:
  4591. if (subsection_size) {
  4592. read_leb_uint32(p, p_end, num_func_name);
  4593. for (name_index = 0; name_index < num_func_name;
  4594. name_index++) {
  4595. read_leb_uint32(p, p_end, func_index);
  4596. if (func_index == previous_func_index) {
  4597. set_error_buf(error_buf, error_buf_size,
  4598. "duplicate function name");
  4599. return false;
  4600. }
  4601. if (func_index < previous_func_index
  4602. && previous_func_index != ~0U) {
  4603. set_error_buf(error_buf, error_buf_size,
  4604. "out-of-order function index ");
  4605. return false;
  4606. }
  4607. previous_func_index = func_index;
  4608. read_leb_uint32(p, p_end, func_name_len);
  4609. CHECK_BUF(p, p_end, func_name_len);
  4610. /* Skip the import functions */
  4611. if (func_index >= module->import_function_count) {
  4612. func_index -= module->import_function_count;
  4613. if (func_index >= module->function_count) {
  4614. set_error_buf(error_buf, error_buf_size,
  4615. "out-of-range function index");
  4616. return false;
  4617. }
  4618. if (!(module->functions[func_index]->field_name =
  4619. wasm_const_str_list_insert(
  4620. p, func_name_len, module,
  4621. #if WASM_ENABLE_WAMR_COMPILER != 0
  4622. false,
  4623. #else
  4624. is_load_from_file_buf,
  4625. #endif
  4626. error_buf, error_buf_size))) {
  4627. return false;
  4628. }
  4629. }
  4630. p += func_name_len;
  4631. }
  4632. }
  4633. break;
  4634. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4635. */
  4636. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4637. default:
  4638. p = p + subsection_size;
  4639. break;
  4640. }
  4641. i++;
  4642. }
  4643. return true;
  4644. fail:
  4645. return false;
  4646. }
  4647. #endif
  4648. static bool
  4649. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4650. bool is_load_from_file_buf, char *error_buf,
  4651. uint32 error_buf_size)
  4652. {
  4653. const uint8 *p = buf, *p_end = buf_end;
  4654. char section_name[32];
  4655. uint32 name_len, buffer_len;
  4656. if (p >= p_end) {
  4657. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4658. return false;
  4659. }
  4660. read_leb_uint32(p, p_end, name_len);
  4661. if (p + name_len > p_end) {
  4662. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4663. return false;
  4664. }
  4665. if (!wasm_check_utf8_str(p, name_len)) {
  4666. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4667. return false;
  4668. }
  4669. buffer_len = sizeof(section_name);
  4670. memset(section_name, 0, buffer_len);
  4671. if (name_len < buffer_len) {
  4672. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4673. }
  4674. else {
  4675. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4676. memset(section_name + buffer_len - 4, '.', 3);
  4677. }
  4678. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4679. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4680. module->name_section_buf = buf;
  4681. module->name_section_buf_end = buf_end;
  4682. p += name_len;
  4683. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4684. error_buf, error_buf_size)) {
  4685. return false;
  4686. }
  4687. LOG_VERBOSE("Load custom name section success.");
  4688. }
  4689. #endif
  4690. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4691. {
  4692. WASMCustomSection *section =
  4693. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4694. if (!section) {
  4695. return false;
  4696. }
  4697. section->name_addr = (char *)p;
  4698. section->name_len = name_len;
  4699. section->content_addr = (uint8 *)(p + name_len);
  4700. section->content_len = (uint32)(p_end - p - name_len);
  4701. section->next = module->custom_section_list;
  4702. module->custom_section_list = section;
  4703. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4704. return true;
  4705. }
  4706. #endif
  4707. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4708. (void)is_load_from_file_buf;
  4709. (void)module;
  4710. return true;
  4711. fail:
  4712. return false;
  4713. }
  4714. static void
  4715. calculate_global_data_offset(WASMModule *module)
  4716. {
  4717. uint32 i, data_offset;
  4718. data_offset = 0;
  4719. for (i = 0; i < module->import_global_count; i++) {
  4720. WASMGlobalImport *import_global =
  4721. &((module->import_globals + i)->u.global);
  4722. #if WASM_ENABLE_FAST_JIT != 0
  4723. import_global->data_offset = data_offset;
  4724. #endif
  4725. data_offset += wasm_value_type_size(import_global->type.val_type);
  4726. }
  4727. for (i = 0; i < module->global_count; i++) {
  4728. WASMGlobal *global = module->globals + i;
  4729. #if WASM_ENABLE_FAST_JIT != 0
  4730. global->data_offset = data_offset;
  4731. #endif
  4732. data_offset += wasm_value_type_size(global->type.val_type);
  4733. }
  4734. module->global_data_size = data_offset;
  4735. }
  4736. #if WASM_ENABLE_FAST_JIT != 0
  4737. static bool
  4738. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4739. uint32 error_buf_size)
  4740. {
  4741. #if WASM_ENABLE_LAZY_JIT != 0
  4742. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4743. #endif
  4744. uint32 i;
  4745. if (!module->function_count)
  4746. return true;
  4747. if (!(module->fast_jit_func_ptrs =
  4748. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4749. error_buf_size))) {
  4750. return false;
  4751. }
  4752. #if WASM_ENABLE_LAZY_JIT != 0
  4753. for (i = 0; i < module->function_count; i++) {
  4754. module->fast_jit_func_ptrs[i] =
  4755. jit_globals->compile_fast_jit_and_then_call;
  4756. }
  4757. #endif
  4758. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4759. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4760. set_error_buf(error_buf, error_buf_size,
  4761. "init fast jit thread lock failed");
  4762. return false;
  4763. }
  4764. module->fast_jit_thread_locks_inited[i] = true;
  4765. }
  4766. return true;
  4767. }
  4768. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4769. #if WASM_ENABLE_JIT != 0
  4770. static bool
  4771. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4772. uint32 error_buf_size)
  4773. {
  4774. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4775. AOTCompOption option = { 0 };
  4776. char *aot_last_error;
  4777. uint64 size;
  4778. #if WASM_ENABLE_GC != 0
  4779. bool gc_enabled = true;
  4780. #else
  4781. bool gc_enabled = false;
  4782. #endif
  4783. if (module->function_count == 0)
  4784. return true;
  4785. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4786. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4787. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4788. return false;
  4789. }
  4790. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4791. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4792. os_mutex_destroy(&module->tierup_wait_lock);
  4793. return false;
  4794. }
  4795. module->tierup_wait_lock_inited = true;
  4796. #endif
  4797. size = sizeof(void *) * (uint64)module->function_count
  4798. + sizeof(bool) * (uint64)module->function_count;
  4799. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4800. return false;
  4801. }
  4802. module->func_ptrs_compiled =
  4803. (bool *)((uint8 *)module->func_ptrs
  4804. + sizeof(void *) * module->function_count);
  4805. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4806. if (!module->comp_data) {
  4807. aot_last_error = aot_get_last_error();
  4808. bh_assert(aot_last_error != NULL);
  4809. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4810. return false;
  4811. }
  4812. option.is_jit_mode = true;
  4813. option.opt_level = llvm_jit_options->opt_level;
  4814. option.size_level = llvm_jit_options->size_level;
  4815. option.segue_flags = llvm_jit_options->segue_flags;
  4816. option.quick_invoke_c_api_import =
  4817. llvm_jit_options->quick_invoke_c_api_import;
  4818. #if WASM_ENABLE_BULK_MEMORY != 0
  4819. option.enable_bulk_memory = true;
  4820. #endif
  4821. #if WASM_ENABLE_THREAD_MGR != 0
  4822. option.enable_thread_mgr = true;
  4823. #endif
  4824. #if WASM_ENABLE_TAIL_CALL != 0
  4825. option.enable_tail_call = true;
  4826. #endif
  4827. #if WASM_ENABLE_SIMD != 0
  4828. option.enable_simd = true;
  4829. #endif
  4830. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4831. option.enable_ref_types = true;
  4832. #elif WASM_ENABLE_GC != 0
  4833. option.enable_gc = true;
  4834. #endif
  4835. option.enable_aux_stack_check = true;
  4836. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4837. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4838. option.enable_aux_stack_frame = true;
  4839. #endif
  4840. #if WASM_ENABLE_PERF_PROFILING != 0
  4841. option.enable_perf_profiling = true;
  4842. #endif
  4843. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4844. option.enable_memory_profiling = true;
  4845. option.enable_stack_estimation = true;
  4846. #endif
  4847. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4848. if (!module->comp_ctx) {
  4849. aot_last_error = aot_get_last_error();
  4850. bh_assert(aot_last_error != NULL);
  4851. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4852. return false;
  4853. }
  4854. return true;
  4855. }
  4856. static bool
  4857. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4858. uint32 error_buf_size)
  4859. {
  4860. char *aot_last_error;
  4861. uint32 i;
  4862. if (module->function_count == 0)
  4863. return true;
  4864. if (!aot_compile_wasm(module->comp_ctx)) {
  4865. aot_last_error = aot_get_last_error();
  4866. bh_assert(aot_last_error != NULL);
  4867. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4868. return false;
  4869. }
  4870. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4871. if (module->orcjit_stop_compiling)
  4872. return false;
  4873. #endif
  4874. bh_print_time("Begin to lookup llvm jit functions");
  4875. for (i = 0; i < module->function_count; i++) {
  4876. LLVMOrcJITTargetAddress func_addr = 0;
  4877. LLVMErrorRef error;
  4878. char func_name[48];
  4879. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4880. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4881. func_name);
  4882. if (error != LLVMErrorSuccess) {
  4883. char *err_msg = LLVMGetErrorMessage(error);
  4884. set_error_buf_v(error_buf, error_buf_size,
  4885. "failed to compile llvm jit function: %s", err_msg);
  4886. LLVMDisposeErrorMessage(err_msg);
  4887. return false;
  4888. }
  4889. /**
  4890. * No need to lock the func_ptr[func_idx] here as it is basic
  4891. * data type, the load/store for it can be finished by one cpu
  4892. * instruction, and there can be only one cpu instruction
  4893. * loading/storing at the same time.
  4894. */
  4895. module->func_ptrs[i] = (void *)func_addr;
  4896. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4897. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4898. if (module->orcjit_stop_compiling)
  4899. return false;
  4900. #endif
  4901. }
  4902. bh_print_time("End lookup llvm jit functions");
  4903. return true;
  4904. }
  4905. #endif /* end of WASM_ENABLE_JIT != 0 */
  4906. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4907. && WASM_ENABLE_LAZY_JIT != 0
  4908. static void *
  4909. init_llvm_jit_functions_stage2_callback(void *arg)
  4910. {
  4911. WASMModule *module = (WASMModule *)arg;
  4912. char error_buf[128];
  4913. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4914. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4915. module->orcjit_stop_compiling = true;
  4916. return NULL;
  4917. }
  4918. os_mutex_lock(&module->tierup_wait_lock);
  4919. module->llvm_jit_inited = true;
  4920. os_cond_broadcast(&module->tierup_wait_cond);
  4921. os_mutex_unlock(&module->tierup_wait_lock);
  4922. return NULL;
  4923. }
  4924. #endif
  4925. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4926. /* The callback function to compile jit functions */
  4927. static void *
  4928. orcjit_thread_callback(void *arg)
  4929. {
  4930. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4931. #if WASM_ENABLE_JIT != 0
  4932. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4933. #endif
  4934. WASMModule *module = thread_arg->module;
  4935. uint32 group_idx = thread_arg->group_idx;
  4936. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4937. uint32 func_count = module->function_count;
  4938. uint32 i;
  4939. #if WASM_ENABLE_FAST_JIT != 0
  4940. /* Compile fast jit functions of this group */
  4941. for (i = group_idx; i < func_count; i += group_stride) {
  4942. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4943. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4944. break;
  4945. }
  4946. if (module->orcjit_stop_compiling) {
  4947. return NULL;
  4948. }
  4949. }
  4950. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4951. os_mutex_lock(&module->tierup_wait_lock);
  4952. module->fast_jit_ready_groups++;
  4953. os_mutex_unlock(&module->tierup_wait_lock);
  4954. #endif
  4955. #endif
  4956. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4957. && WASM_ENABLE_LAZY_JIT != 0
  4958. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4959. for (i = group_idx; i < func_count;
  4960. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4961. uint32 j;
  4962. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4963. if (i + j * group_stride < func_count) {
  4964. if (!jit_compiler_set_call_to_fast_jit(
  4965. module,
  4966. i + j * group_stride + module->import_function_count)) {
  4967. LOG_ERROR(
  4968. "failed to compile call_to_fast_jit for func %u\n",
  4969. i + j * group_stride + module->import_function_count);
  4970. module->orcjit_stop_compiling = true;
  4971. return NULL;
  4972. }
  4973. }
  4974. if (module->orcjit_stop_compiling) {
  4975. return NULL;
  4976. }
  4977. }
  4978. }
  4979. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4980. fast jit functions are compiled */
  4981. os_mutex_lock(&module->tierup_wait_lock);
  4982. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4983. && module->fast_jit_ready_groups >= group_stride)) {
  4984. os_cond_reltimedwait(&module->tierup_wait_cond,
  4985. &module->tierup_wait_lock, 10000);
  4986. if (module->orcjit_stop_compiling) {
  4987. /* init_llvm_jit_functions_stage2 failed */
  4988. os_mutex_unlock(&module->tierup_wait_lock);
  4989. return NULL;
  4990. }
  4991. }
  4992. os_mutex_unlock(&module->tierup_wait_lock);
  4993. #endif
  4994. #if WASM_ENABLE_JIT != 0
  4995. /* Compile llvm jit functions of this group */
  4996. for (i = group_idx; i < func_count;
  4997. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4998. LLVMOrcJITTargetAddress func_addr = 0;
  4999. LLVMErrorRef error;
  5000. char func_name[48];
  5001. typedef void (*F)(void);
  5002. union {
  5003. F f;
  5004. void *v;
  5005. } u;
  5006. uint32 j;
  5007. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5008. "_wrapper");
  5009. LOG_DEBUG("compile llvm jit func %s", func_name);
  5010. error =
  5011. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5012. if (error != LLVMErrorSuccess) {
  5013. char *err_msg = LLVMGetErrorMessage(error);
  5014. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5015. LLVMDisposeErrorMessage(err_msg);
  5016. break;
  5017. }
  5018. /* Call the jit wrapper function to trigger its compilation, so as
  5019. to compile the actual jit functions, since we add the latter to
  5020. function list in the PartitionFunction callback */
  5021. u.v = (void *)func_addr;
  5022. u.f();
  5023. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5024. if (i + j * group_stride < func_count) {
  5025. module->func_ptrs_compiled[i + j * group_stride] = true;
  5026. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5027. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5028. i + j * group_stride);
  5029. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5030. func_name);
  5031. if (error != LLVMErrorSuccess) {
  5032. char *err_msg = LLVMGetErrorMessage(error);
  5033. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5034. err_msg);
  5035. LLVMDisposeErrorMessage(err_msg);
  5036. /* Ignore current llvm jit func, as its func ptr is
  5037. previous set to call_to_fast_jit, which also works */
  5038. continue;
  5039. }
  5040. jit_compiler_set_llvm_jit_func_ptr(
  5041. module,
  5042. i + j * group_stride + module->import_function_count,
  5043. (void *)func_addr);
  5044. /* Try to switch to call this llvm jit function instead of
  5045. fast jit function from fast jit jitted code */
  5046. jit_compiler_set_call_to_llvm_jit(
  5047. module,
  5048. i + j * group_stride + module->import_function_count);
  5049. #endif
  5050. }
  5051. }
  5052. if (module->orcjit_stop_compiling) {
  5053. break;
  5054. }
  5055. }
  5056. #endif
  5057. return NULL;
  5058. }
  5059. static void
  5060. orcjit_stop_compile_threads(WASMModule *module)
  5061. {
  5062. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5063. / sizeof(OrcJitThreadArg));
  5064. module->orcjit_stop_compiling = true;
  5065. for (i = 0; i < thread_num; i++) {
  5066. if (module->orcjit_threads[i])
  5067. os_thread_join(module->orcjit_threads[i], NULL);
  5068. }
  5069. }
  5070. static bool
  5071. compile_jit_functions(WASMModule *module, char *error_buf,
  5072. uint32 error_buf_size)
  5073. {
  5074. uint32 thread_num =
  5075. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5076. uint32 i, j;
  5077. bh_print_time("Begin to compile jit functions");
  5078. /* Create threads to compile the jit functions */
  5079. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5080. #if WASM_ENABLE_JIT != 0
  5081. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5082. #endif
  5083. module->orcjit_thread_args[i].module = module;
  5084. module->orcjit_thread_args[i].group_idx = i;
  5085. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5086. (void *)&module->orcjit_thread_args[i],
  5087. APP_THREAD_STACK_SIZE_DEFAULT)
  5088. != 0) {
  5089. set_error_buf(error_buf, error_buf_size,
  5090. "create orcjit compile thread failed");
  5091. /* Terminate the threads created */
  5092. module->orcjit_stop_compiling = true;
  5093. for (j = 0; j < i; j++) {
  5094. os_thread_join(module->orcjit_threads[j], NULL);
  5095. }
  5096. return false;
  5097. }
  5098. }
  5099. #if WASM_ENABLE_LAZY_JIT == 0
  5100. /* Wait until all jit functions are compiled for eager mode */
  5101. for (i = 0; i < thread_num; i++) {
  5102. if (module->orcjit_threads[i])
  5103. os_thread_join(module->orcjit_threads[i], NULL);
  5104. }
  5105. #if WASM_ENABLE_FAST_JIT != 0
  5106. /* Ensure all the fast-jit functions are compiled */
  5107. for (i = 0; i < module->function_count; i++) {
  5108. if (!jit_compiler_is_compiled(module,
  5109. i + module->import_function_count)) {
  5110. set_error_buf(error_buf, error_buf_size,
  5111. "failed to compile fast jit function");
  5112. return false;
  5113. }
  5114. }
  5115. #endif
  5116. #if WASM_ENABLE_JIT != 0
  5117. /* Ensure all the llvm-jit functions are compiled */
  5118. for (i = 0; i < module->function_count; i++) {
  5119. if (!module->func_ptrs_compiled[i]) {
  5120. set_error_buf(error_buf, error_buf_size,
  5121. "failed to compile llvm jit function");
  5122. return false;
  5123. }
  5124. }
  5125. #endif
  5126. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5127. bh_print_time("End compile jit functions");
  5128. return true;
  5129. }
  5130. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5131. static bool
  5132. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5133. uint32 cur_func_idx, char *error_buf,
  5134. uint32 error_buf_size);
  5135. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5136. void **
  5137. wasm_interp_get_handle_table();
  5138. static void **handle_table;
  5139. #endif
  5140. static bool
  5141. load_from_sections(WASMModule *module, WASMSection *sections,
  5142. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5143. char *error_buf, uint32 error_buf_size)
  5144. {
  5145. WASMExport *export;
  5146. WASMSection *section = sections;
  5147. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5148. *buf_func = NULL, *buf_func_end = NULL;
  5149. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5150. WASMGlobal *aux_stack_top_global = NULL, *global;
  5151. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5152. aux_stack_top = (uint64)-1LL;
  5153. uint32 global_index, func_index, i;
  5154. uint32 aux_data_end_global_index = (uint32)-1;
  5155. uint32 aux_heap_base_global_index = (uint32)-1;
  5156. WASMFuncType *func_type;
  5157. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5158. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5159. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5160. #if WASM_ENABLE_BULK_MEMORY != 0
  5161. bool has_datacount_section = false;
  5162. #endif
  5163. /* Find code and function sections if have */
  5164. while (section) {
  5165. if (section->section_type == SECTION_TYPE_CODE) {
  5166. buf_code = section->section_body;
  5167. buf_code_end = buf_code + section->section_body_size;
  5168. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5169. module->buf_code = (uint8 *)buf_code;
  5170. module->buf_code_size = section->section_body_size;
  5171. #endif
  5172. }
  5173. else if (section->section_type == SECTION_TYPE_FUNC) {
  5174. buf_func = section->section_body;
  5175. buf_func_end = buf_func + section->section_body_size;
  5176. }
  5177. section = section->next;
  5178. }
  5179. section = sections;
  5180. while (section) {
  5181. buf = section->section_body;
  5182. buf_end = buf + section->section_body_size;
  5183. switch (section->section_type) {
  5184. case SECTION_TYPE_USER:
  5185. /* unsupported user section, ignore it. */
  5186. if (!load_user_section(buf, buf_end, module,
  5187. reuse_const_strings, error_buf,
  5188. error_buf_size))
  5189. return false;
  5190. break;
  5191. case SECTION_TYPE_TYPE:
  5192. if (!load_type_section(buf, buf_end, module, error_buf,
  5193. error_buf_size))
  5194. return false;
  5195. break;
  5196. case SECTION_TYPE_IMPORT:
  5197. if (!load_import_section(buf, buf_end, module,
  5198. reuse_const_strings, error_buf,
  5199. error_buf_size))
  5200. return false;
  5201. break;
  5202. case SECTION_TYPE_FUNC:
  5203. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5204. module, error_buf, error_buf_size))
  5205. return false;
  5206. break;
  5207. case SECTION_TYPE_TABLE:
  5208. if (!load_table_section(buf, buf_end, module, error_buf,
  5209. error_buf_size))
  5210. return false;
  5211. break;
  5212. case SECTION_TYPE_MEMORY:
  5213. if (!load_memory_section(buf, buf_end, module, error_buf,
  5214. error_buf_size))
  5215. return false;
  5216. break;
  5217. #if WASM_ENABLE_TAGS != 0
  5218. case SECTION_TYPE_TAG:
  5219. /* load tag declaration section */
  5220. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5221. module, error_buf, error_buf_size))
  5222. return false;
  5223. break;
  5224. #endif
  5225. case SECTION_TYPE_GLOBAL:
  5226. if (!load_global_section(buf, buf_end, module, error_buf,
  5227. error_buf_size))
  5228. return false;
  5229. break;
  5230. case SECTION_TYPE_EXPORT:
  5231. if (!load_export_section(buf, buf_end, module,
  5232. reuse_const_strings, error_buf,
  5233. error_buf_size))
  5234. return false;
  5235. break;
  5236. case SECTION_TYPE_START:
  5237. if (!load_start_section(buf, buf_end, module, error_buf,
  5238. error_buf_size))
  5239. return false;
  5240. break;
  5241. case SECTION_TYPE_ELEM:
  5242. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5243. error_buf_size))
  5244. return false;
  5245. break;
  5246. case SECTION_TYPE_CODE:
  5247. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5248. module, error_buf, error_buf_size))
  5249. return false;
  5250. break;
  5251. case SECTION_TYPE_DATA:
  5252. if (!load_data_segment_section(buf, buf_end, module,
  5253. #if WASM_ENABLE_BULK_MEMORY != 0
  5254. has_datacount_section,
  5255. #endif
  5256. clone_data_seg, error_buf,
  5257. error_buf_size))
  5258. return false;
  5259. break;
  5260. #if WASM_ENABLE_BULK_MEMORY != 0
  5261. case SECTION_TYPE_DATACOUNT:
  5262. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5263. error_buf_size))
  5264. return false;
  5265. has_datacount_section = true;
  5266. break;
  5267. #endif
  5268. #if WASM_ENABLE_STRINGREF != 0
  5269. case SECTION_TYPE_STRINGREF:
  5270. if (!load_stringref_section(buf, buf_end, module,
  5271. reuse_const_strings, error_buf,
  5272. error_buf_size))
  5273. return false;
  5274. break;
  5275. #endif
  5276. default:
  5277. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5278. return false;
  5279. }
  5280. section = section->next;
  5281. }
  5282. module->aux_data_end_global_index = (uint32)-1;
  5283. module->aux_heap_base_global_index = (uint32)-1;
  5284. module->aux_stack_top_global_index = (uint32)-1;
  5285. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5286. export = module->exports;
  5287. for (i = 0; i < module->export_count; i++, export ++) {
  5288. if (export->kind == EXPORT_KIND_GLOBAL) {
  5289. if (!strcmp(export->name, "__heap_base")) {
  5290. global_index = export->index - module->import_global_count;
  5291. global = module->globals + global_index;
  5292. if (global->type.val_type == VALUE_TYPE_I32
  5293. && !global->type.is_mutable
  5294. && global->init_expr.init_expr_type
  5295. == INIT_EXPR_TYPE_I32_CONST) {
  5296. aux_heap_base_global = global;
  5297. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5298. aux_heap_base_global_index = export->index;
  5299. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5300. aux_heap_base);
  5301. }
  5302. }
  5303. else if (!strcmp(export->name, "__data_end")) {
  5304. global_index = export->index - module->import_global_count;
  5305. global = module->globals + global_index;
  5306. if (global->type.val_type == VALUE_TYPE_I32
  5307. && !global->type.is_mutable
  5308. && global->init_expr.init_expr_type
  5309. == INIT_EXPR_TYPE_I32_CONST) {
  5310. aux_data_end_global = global;
  5311. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5312. aux_data_end_global_index = export->index;
  5313. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5314. aux_data_end);
  5315. aux_data_end = align_uint64(aux_data_end, 16);
  5316. }
  5317. }
  5318. /* For module compiled with -pthread option, the global is:
  5319. [0] stack_top <-- 0
  5320. [1] tls_pointer
  5321. [2] tls_size
  5322. [3] data_end <-- 3
  5323. [4] global_base
  5324. [5] heap_base <-- 5
  5325. [6] dso_handle
  5326. For module compiled without -pthread option:
  5327. [0] stack_top <-- 0
  5328. [1] data_end <-- 1
  5329. [2] global_base
  5330. [3] heap_base <-- 3
  5331. [4] dso_handle
  5332. */
  5333. if (aux_data_end_global && aux_heap_base_global
  5334. && aux_data_end <= aux_heap_base) {
  5335. module->aux_data_end_global_index = aux_data_end_global_index;
  5336. module->aux_data_end = aux_data_end;
  5337. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5338. module->aux_heap_base = aux_heap_base;
  5339. /* Resolve aux stack top global */
  5340. for (global_index = 0; global_index < module->global_count;
  5341. global_index++) {
  5342. global = module->globals + global_index;
  5343. if (global->type.is_mutable /* heap_base and data_end is
  5344. not mutable */
  5345. && global->type.val_type == VALUE_TYPE_I32
  5346. && global->init_expr.init_expr_type
  5347. == INIT_EXPR_TYPE_I32_CONST
  5348. && (uint64)(uint32)global->init_expr.u.i32
  5349. <= aux_heap_base) {
  5350. aux_stack_top_global = global;
  5351. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5352. module->aux_stack_top_global_index =
  5353. module->import_global_count + global_index;
  5354. module->aux_stack_bottom = aux_stack_top;
  5355. module->aux_stack_size =
  5356. aux_stack_top > aux_data_end
  5357. ? (uint32)(aux_stack_top - aux_data_end)
  5358. : (uint32)aux_stack_top;
  5359. LOG_VERBOSE(
  5360. "Found aux stack top global, value: %" PRIu64 ", "
  5361. "global index: %d, stack size: %d",
  5362. aux_stack_top, global_index,
  5363. module->aux_stack_size);
  5364. break;
  5365. }
  5366. }
  5367. if (!aux_stack_top_global) {
  5368. /* Auxiliary stack global isn't found, it must be unused
  5369. in the wasm app, as if it is used, the global must be
  5370. defined. Here we set it to __heap_base global and set
  5371. its size to 0. */
  5372. aux_stack_top_global = aux_heap_base_global;
  5373. aux_stack_top = aux_heap_base;
  5374. module->aux_stack_top_global_index =
  5375. module->aux_heap_base_global_index;
  5376. module->aux_stack_bottom = aux_stack_top;
  5377. module->aux_stack_size = 0;
  5378. }
  5379. break;
  5380. }
  5381. }
  5382. }
  5383. module->malloc_function = (uint32)-1;
  5384. module->free_function = (uint32)-1;
  5385. module->retain_function = (uint32)-1;
  5386. /* Resolve malloc/free function exported by wasm module */
  5387. #if WASM_ENABLE_MEMORY64 != 0
  5388. if (has_module_memory64(module))
  5389. malloc_free_io_type = VALUE_TYPE_I64;
  5390. #endif
  5391. export = module->exports;
  5392. for (i = 0; i < module->export_count; i++, export ++) {
  5393. if (export->kind == EXPORT_KIND_FUNC) {
  5394. if (!strcmp(export->name, "malloc")
  5395. && export->index >= module->import_function_count) {
  5396. func_index = export->index - module->import_function_count;
  5397. func_type = module->functions[func_index]->func_type;
  5398. if (func_type->param_count == 1 && func_type->result_count == 1
  5399. && func_type->types[0] == malloc_free_io_type
  5400. && func_type->types[1] == malloc_free_io_type) {
  5401. bh_assert(module->malloc_function == (uint32)-1);
  5402. module->malloc_function = export->index;
  5403. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5404. export->name, export->index);
  5405. }
  5406. }
  5407. else if (!strcmp(export->name, "__new")
  5408. && export->index >= module->import_function_count) {
  5409. /* __new && __pin for AssemblyScript */
  5410. func_index = export->index - module->import_function_count;
  5411. func_type = module->functions[func_index]->func_type;
  5412. if (func_type->param_count == 2 && func_type->result_count == 1
  5413. && func_type->types[0] == malloc_free_io_type
  5414. && func_type->types[1] == VALUE_TYPE_I32
  5415. && func_type->types[2] == malloc_free_io_type) {
  5416. uint32 j;
  5417. WASMExport *export_tmp;
  5418. bh_assert(module->malloc_function == (uint32)-1);
  5419. module->malloc_function = export->index;
  5420. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5421. export->name, export->index);
  5422. /* resolve retain function.
  5423. If not found, reset malloc function index */
  5424. export_tmp = module->exports;
  5425. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5426. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5427. && (!strcmp(export_tmp->name, "__retain")
  5428. || (!strcmp(export_tmp->name, "__pin")))
  5429. && (export_tmp->index
  5430. >= module->import_function_count)) {
  5431. func_index = export_tmp->index
  5432. - module->import_function_count;
  5433. func_type =
  5434. module->functions[func_index]->func_type;
  5435. if (func_type->param_count == 1
  5436. && func_type->result_count == 1
  5437. && func_type->types[0] == malloc_free_io_type
  5438. && func_type->types[1] == malloc_free_io_type) {
  5439. bh_assert(module->retain_function
  5440. == (uint32)-1);
  5441. module->retain_function = export_tmp->index;
  5442. LOG_VERBOSE("Found retain function, name: %s, "
  5443. "index: %u",
  5444. export_tmp->name,
  5445. export_tmp->index);
  5446. break;
  5447. }
  5448. }
  5449. }
  5450. if (j == module->export_count) {
  5451. module->malloc_function = (uint32)-1;
  5452. LOG_VERBOSE("Can't find retain function,"
  5453. "reset malloc function index to -1");
  5454. }
  5455. }
  5456. }
  5457. else if (((!strcmp(export->name, "free"))
  5458. || (!strcmp(export->name, "__release"))
  5459. || (!strcmp(export->name, "__unpin")))
  5460. && export->index >= module->import_function_count) {
  5461. func_index = export->index - module->import_function_count;
  5462. func_type = module->functions[func_index]->func_type;
  5463. if (func_type->param_count == 1 && func_type->result_count == 0
  5464. && func_type->types[0] == malloc_free_io_type) {
  5465. bh_assert(module->free_function == (uint32)-1);
  5466. module->free_function = export->index;
  5467. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5468. export->name, export->index);
  5469. }
  5470. }
  5471. }
  5472. }
  5473. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5474. handle_table = wasm_interp_get_handle_table();
  5475. #endif
  5476. for (i = 0; i < module->function_count; i++) {
  5477. WASMFunction *func = module->functions[i];
  5478. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5479. error_buf_size)) {
  5480. return false;
  5481. }
  5482. if (i == module->function_count - 1
  5483. && func->code + func->code_size != buf_code_end) {
  5484. set_error_buf(error_buf, error_buf_size,
  5485. "code section size mismatch");
  5486. return false;
  5487. }
  5488. }
  5489. if (!module->possible_memory_grow) {
  5490. WASMMemoryImport *memory_import;
  5491. WASMMemory *memory;
  5492. if (aux_data_end_global && aux_heap_base_global
  5493. && aux_stack_top_global) {
  5494. uint64 init_memory_size;
  5495. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5496. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5497. * valid range of uint32 */
  5498. if (shrunk_memory_size <= UINT32_MAX) {
  5499. if (module->import_memory_count) {
  5500. memory_import = &module->import_memories[0].u.memory;
  5501. init_memory_size =
  5502. (uint64)memory_import->mem_type.num_bytes_per_page
  5503. * memory_import->mem_type.init_page_count;
  5504. if (shrunk_memory_size <= init_memory_size) {
  5505. /* Reset memory info to decrease memory usage */
  5506. memory_import->mem_type.num_bytes_per_page =
  5507. (uint32)shrunk_memory_size;
  5508. memory_import->mem_type.init_page_count = 1;
  5509. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5510. shrunk_memory_size);
  5511. }
  5512. }
  5513. if (module->memory_count) {
  5514. memory = &module->memories[0];
  5515. init_memory_size = (uint64)memory->num_bytes_per_page
  5516. * memory->init_page_count;
  5517. if (shrunk_memory_size <= init_memory_size) {
  5518. /* Reset memory info to decrease memory usage */
  5519. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5520. memory->init_page_count = 1;
  5521. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5522. shrunk_memory_size);
  5523. }
  5524. }
  5525. }
  5526. }
  5527. #if WASM_ENABLE_MULTI_MODULE == 0
  5528. if (module->import_memory_count) {
  5529. memory_import = &module->import_memories[0].u.memory;
  5530. /* Only resize the memory to one big page if num_bytes_per_page is
  5531. * in valid range of uint32 */
  5532. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5533. memory_import->mem_type.num_bytes_per_page *=
  5534. memory_import->mem_type.init_page_count;
  5535. if (memory_import->mem_type.init_page_count > 0)
  5536. memory_import->mem_type.init_page_count =
  5537. memory_import->mem_type.max_page_count = 1;
  5538. else
  5539. memory_import->mem_type.init_page_count =
  5540. memory_import->mem_type.max_page_count = 0;
  5541. }
  5542. }
  5543. if (module->memory_count) {
  5544. memory = &module->memories[0];
  5545. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5546. * valid range of uint32 */
  5547. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5548. memory->num_bytes_per_page *= memory->init_page_count;
  5549. if (memory->init_page_count > 0)
  5550. memory->init_page_count = memory->max_page_count = 1;
  5551. else
  5552. memory->init_page_count = memory->max_page_count = 0;
  5553. }
  5554. }
  5555. #endif
  5556. }
  5557. calculate_global_data_offset(module);
  5558. #if WASM_ENABLE_FAST_JIT != 0
  5559. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5560. return false;
  5561. }
  5562. #endif
  5563. #if WASM_ENABLE_JIT != 0
  5564. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5565. return false;
  5566. }
  5567. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5568. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5569. return false;
  5570. }
  5571. #else
  5572. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5573. thread fast jit execution, since applying llvm optimizations in
  5574. aot_compile_wasm may cost a lot of time.
  5575. Create thread with enough native stack to apply llvm optimizations */
  5576. if (os_thread_create(&module->llvm_jit_init_thread,
  5577. init_llvm_jit_functions_stage2_callback,
  5578. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5579. != 0) {
  5580. set_error_buf(error_buf, error_buf_size,
  5581. "create orcjit compile thread failed");
  5582. return false;
  5583. }
  5584. #endif
  5585. #endif
  5586. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5587. /* Create threads to compile the jit functions */
  5588. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5589. return false;
  5590. }
  5591. #endif
  5592. #if WASM_ENABLE_MEMORY_TRACING != 0
  5593. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5594. #endif
  5595. return true;
  5596. }
  5597. static WASMModule *
  5598. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5599. {
  5600. WASMModule *module =
  5601. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5602. bh_list_status ret;
  5603. if (!module) {
  5604. return NULL;
  5605. }
  5606. module->module_type = Wasm_Module_Bytecode;
  5607. /* Set start_function to -1, means no start function */
  5608. module->start_function = (uint32)-1;
  5609. module->name = name;
  5610. module->is_binary_freeable = false;
  5611. #if WASM_ENABLE_FAST_INTERP == 0
  5612. module->br_table_cache_list = &module->br_table_cache_list_head;
  5613. ret = bh_list_init(module->br_table_cache_list);
  5614. bh_assert(ret == BH_LIST_SUCCESS);
  5615. #endif
  5616. #if WASM_ENABLE_MULTI_MODULE != 0
  5617. module->import_module_list = &module->import_module_list_head;
  5618. ret = bh_list_init(module->import_module_list);
  5619. bh_assert(ret == BH_LIST_SUCCESS);
  5620. #endif
  5621. #if WASM_ENABLE_DEBUG_INTERP != 0
  5622. ret = bh_list_init(&module->fast_opcode_list);
  5623. bh_assert(ret == BH_LIST_SUCCESS);
  5624. #endif
  5625. #if WASM_ENABLE_GC != 0
  5626. if (!(module->ref_type_set =
  5627. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5628. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5629. goto fail1;
  5630. }
  5631. if (os_mutex_init(&module->rtt_type_lock)) {
  5632. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5633. goto fail2;
  5634. }
  5635. #endif
  5636. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5637. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5638. && WASM_ENABLE_LAZY_JIT != 0)
  5639. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5640. set_error_buf(error_buf, error_buf_size,
  5641. "init instance list lock failed");
  5642. goto fail3;
  5643. }
  5644. #endif
  5645. (void)ret;
  5646. return module;
  5647. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5648. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5649. && WASM_ENABLE_LAZY_JIT != 0)
  5650. fail3:
  5651. #endif
  5652. #if WASM_ENABLE_GC != 0
  5653. os_mutex_destroy(&module->rtt_type_lock);
  5654. fail2:
  5655. bh_hash_map_destroy(module->ref_type_set);
  5656. fail1:
  5657. #endif
  5658. wasm_runtime_free(module);
  5659. return NULL;
  5660. }
  5661. #if WASM_ENABLE_DEBUG_INTERP != 0
  5662. static bool
  5663. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5664. uint32 error_buf_size)
  5665. {
  5666. WASMFastOPCodeNode *fast_op =
  5667. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5668. if (fast_op) {
  5669. fast_op->offset = pos - module->load_addr;
  5670. fast_op->orig_op = orig_op;
  5671. bh_list_insert(&module->fast_opcode_list, fast_op);
  5672. }
  5673. return fast_op ? true : false;
  5674. }
  5675. #endif
  5676. WASMModule *
  5677. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5678. uint32 error_buf_size)
  5679. {
  5680. WASMModule *module = create_module("", error_buf, error_buf_size);
  5681. if (!module)
  5682. return NULL;
  5683. if (!load_from_sections(module, section_list, false, true, error_buf,
  5684. error_buf_size)) {
  5685. wasm_loader_unload(module);
  5686. return NULL;
  5687. }
  5688. LOG_VERBOSE("Load module from sections success.\n");
  5689. return module;
  5690. }
  5691. static void
  5692. destroy_sections(WASMSection *section_list)
  5693. {
  5694. WASMSection *section = section_list, *next;
  5695. while (section) {
  5696. next = section->next;
  5697. wasm_runtime_free(section);
  5698. section = next;
  5699. }
  5700. }
  5701. /* clang-format off */
  5702. static uint8 section_ids[] = {
  5703. SECTION_TYPE_USER,
  5704. SECTION_TYPE_TYPE,
  5705. SECTION_TYPE_IMPORT,
  5706. SECTION_TYPE_FUNC,
  5707. SECTION_TYPE_TABLE,
  5708. SECTION_TYPE_MEMORY,
  5709. #if WASM_ENABLE_TAGS != 0
  5710. SECTION_TYPE_TAG,
  5711. #endif
  5712. #if WASM_ENABLE_STRINGREF != 0
  5713. /* must immediately precede the global section,
  5714. or where the global section would be */
  5715. SECTION_TYPE_STRINGREF,
  5716. #endif
  5717. SECTION_TYPE_GLOBAL,
  5718. SECTION_TYPE_EXPORT,
  5719. SECTION_TYPE_START,
  5720. SECTION_TYPE_ELEM,
  5721. #if WASM_ENABLE_BULK_MEMORY != 0
  5722. SECTION_TYPE_DATACOUNT,
  5723. #endif
  5724. SECTION_TYPE_CODE,
  5725. SECTION_TYPE_DATA
  5726. };
  5727. /* clang-format on */
  5728. static uint8
  5729. get_section_index(uint8 section_type)
  5730. {
  5731. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5732. for (uint8 i = 0; i < max_id; i++) {
  5733. if (section_type == section_ids[i])
  5734. return i;
  5735. }
  5736. return (uint8)-1;
  5737. }
  5738. static bool
  5739. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5740. char *error_buf, uint32 error_buf_size)
  5741. {
  5742. WASMSection *section_list_end = NULL, *section;
  5743. const uint8 *p = buf, *p_end = buf + size;
  5744. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5745. uint32 section_size;
  5746. bh_assert(!*p_section_list);
  5747. p += 8;
  5748. while (p < p_end) {
  5749. CHECK_BUF(p, p_end, 1);
  5750. section_type = read_uint8(p);
  5751. section_index = get_section_index(section_type);
  5752. if (section_index != (uint8)-1) {
  5753. if (section_type != SECTION_TYPE_USER) {
  5754. /* Custom sections may be inserted at any place,
  5755. while other sections must occur at most once
  5756. and in prescribed order. */
  5757. if (last_section_index != (uint8)-1
  5758. && (section_index <= last_section_index)) {
  5759. set_error_buf(error_buf, error_buf_size,
  5760. "unexpected content after last section or "
  5761. "junk after last section");
  5762. return false;
  5763. }
  5764. last_section_index = section_index;
  5765. }
  5766. read_leb_uint32(p, p_end, section_size);
  5767. CHECK_BUF1(p, p_end, section_size);
  5768. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5769. error_buf_size))) {
  5770. return false;
  5771. }
  5772. section->section_type = section_type;
  5773. section->section_body = (uint8 *)p;
  5774. section->section_body_size = section_size;
  5775. if (!section_list_end)
  5776. *p_section_list = section_list_end = section;
  5777. else {
  5778. section_list_end->next = section;
  5779. section_list_end = section;
  5780. }
  5781. p += section_size;
  5782. }
  5783. else {
  5784. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5785. return false;
  5786. }
  5787. }
  5788. return true;
  5789. fail:
  5790. return false;
  5791. }
  5792. static void
  5793. exchange32(uint8 *p_data)
  5794. {
  5795. uint8 value = *p_data;
  5796. *p_data = *(p_data + 3);
  5797. *(p_data + 3) = value;
  5798. value = *(p_data + 1);
  5799. *(p_data + 1) = *(p_data + 2);
  5800. *(p_data + 2) = value;
  5801. }
  5802. static union {
  5803. int a;
  5804. char b;
  5805. } __ue = { .a = 1 };
  5806. #define is_little_endian() (__ue.b == 1)
  5807. static bool
  5808. load(const uint8 *buf, uint32 size, WASMModule *module,
  5809. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5810. {
  5811. const uint8 *buf_end = buf + size;
  5812. const uint8 *p = buf, *p_end = buf_end;
  5813. uint32 magic_number, version;
  5814. WASMSection *section_list = NULL;
  5815. CHECK_BUF1(p, p_end, sizeof(uint32));
  5816. magic_number = read_uint32(p);
  5817. if (!is_little_endian())
  5818. exchange32((uint8 *)&magic_number);
  5819. if (magic_number != WASM_MAGIC_NUMBER) {
  5820. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5821. return false;
  5822. }
  5823. CHECK_BUF1(p, p_end, sizeof(uint32));
  5824. version = read_uint32(p);
  5825. if (!is_little_endian())
  5826. exchange32((uint8 *)&version);
  5827. if (version != WASM_CURRENT_VERSION) {
  5828. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5829. return false;
  5830. }
  5831. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5832. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5833. error_buf, error_buf_size)) {
  5834. destroy_sections(section_list);
  5835. return false;
  5836. }
  5837. destroy_sections(section_list);
  5838. return true;
  5839. fail:
  5840. return false;
  5841. }
  5842. #if WASM_ENABLE_LIBC_WASI != 0
  5843. /**
  5844. * refer to
  5845. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5846. */
  5847. static bool
  5848. check_wasi_abi_compatibility(const WASMModule *module,
  5849. #if WASM_ENABLE_MULTI_MODULE != 0
  5850. bool main_module,
  5851. #endif
  5852. char *error_buf, uint32 error_buf_size)
  5853. {
  5854. /**
  5855. * be careful with:
  5856. * wasi compatible modules(command/reactor) which don't import any wasi
  5857. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5858. * reactor can depend on nothing. At the same time, each has its own entry
  5859. * point.
  5860. *
  5861. * observations:
  5862. * - clang always injects `_start` into a command
  5863. * - clang always injects `_initialize` into a reactor
  5864. * - `iwasm -f` allows to run a function in the reactor
  5865. *
  5866. * strong assumptions:
  5867. * - no one will define either `_start` or `_initialize` on purpose
  5868. * - `_start` should always be `void _start(void)`
  5869. * - `_initialize` should always be `void _initialize(void)`
  5870. *
  5871. */
  5872. /* clang-format off */
  5873. /**
  5874. *
  5875. * | | import_wasi_api True | | import_wasi_api False | |
  5876. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5877. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5878. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5879. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5880. */
  5881. /* clang-format on */
  5882. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5883. uint32 import_function_count = module->import_function_count;
  5884. WASMFuncType *func_type;
  5885. /* (func (export "_start") (...) */
  5886. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5887. error_buf, error_buf_size);
  5888. if (start) {
  5889. if (start->index < import_function_count) {
  5890. set_error_buf(
  5891. error_buf, error_buf_size,
  5892. "the builtin _start function can not be an import function");
  5893. return false;
  5894. }
  5895. func_type =
  5896. module->functions[start->index - import_function_count]->func_type;
  5897. if (func_type->param_count || func_type->result_count) {
  5898. set_error_buf(error_buf, error_buf_size,
  5899. "the signature of builtin _start function is wrong");
  5900. return false;
  5901. }
  5902. }
  5903. else {
  5904. /* (func (export "_initialize") (...) */
  5905. initialize =
  5906. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5907. error_buf, error_buf_size);
  5908. if (initialize) {
  5909. if (initialize->index < import_function_count) {
  5910. set_error_buf(error_buf, error_buf_size,
  5911. "the builtin _initialize function can not be an "
  5912. "import function");
  5913. return false;
  5914. }
  5915. func_type =
  5916. module->functions[initialize->index - import_function_count]
  5917. ->func_type;
  5918. if (func_type->param_count || func_type->result_count) {
  5919. set_error_buf(
  5920. error_buf, error_buf_size,
  5921. "the signature of builtin _initialize function is wrong");
  5922. return false;
  5923. }
  5924. }
  5925. }
  5926. /* filter out non-wasi compatible modules */
  5927. if (!module->import_wasi_api && !start && !initialize) {
  5928. return true;
  5929. }
  5930. /* should have one at least */
  5931. if (module->import_wasi_api && !start && !initialize) {
  5932. LOG_WARNING("warning: a module with WASI apis should be either "
  5933. "a command or a reactor");
  5934. }
  5935. /*
  5936. * there is at least one of `_start` and `_initialize` in below cases.
  5937. * according to the assumption, they should be all wasi compatible
  5938. */
  5939. #if WASM_ENABLE_MULTI_MODULE != 0
  5940. /* filter out commands (with `_start`) cases */
  5941. if (start && !main_module) {
  5942. set_error_buf(
  5943. error_buf, error_buf_size,
  5944. "a command (with _start function) can not be a sub-module");
  5945. return false;
  5946. }
  5947. #endif
  5948. /*
  5949. * it is ok a reactor acts as a main module,
  5950. * so skip the check about (with `_initialize`)
  5951. */
  5952. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5953. error_buf, error_buf_size);
  5954. if (!memory
  5955. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5956. /*
  5957. * with wasi-threads, it's still an open question if a memory
  5958. * should be exported.
  5959. *
  5960. * https://github.com/WebAssembly/wasi-threads/issues/22
  5961. * https://github.com/WebAssembly/WASI/issues/502
  5962. *
  5963. * Note: this code assumes the number of memories is at most 1.
  5964. */
  5965. && module->import_memory_count == 0
  5966. #endif
  5967. ) {
  5968. set_error_buf(error_buf, error_buf_size,
  5969. "a module with WASI apis must export memory by default");
  5970. return false;
  5971. }
  5972. return true;
  5973. }
  5974. #endif
  5975. WASMModule *
  5976. wasm_loader_load(uint8 *buf, uint32 size,
  5977. #if WASM_ENABLE_MULTI_MODULE != 0
  5978. bool main_module,
  5979. #endif
  5980. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5981. {
  5982. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5983. if (!module) {
  5984. return NULL;
  5985. }
  5986. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5987. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5988. module->load_addr = (uint8 *)buf;
  5989. module->load_size = size;
  5990. #endif
  5991. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  5992. error_buf_size)) {
  5993. goto fail;
  5994. }
  5995. #if WASM_ENABLE_LIBC_WASI != 0
  5996. /* Check the WASI application ABI */
  5997. if (!check_wasi_abi_compatibility(module,
  5998. #if WASM_ENABLE_MULTI_MODULE != 0
  5999. main_module,
  6000. #endif
  6001. error_buf, error_buf_size)) {
  6002. goto fail;
  6003. }
  6004. #endif
  6005. LOG_VERBOSE("Load module success.\n");
  6006. return module;
  6007. fail:
  6008. wasm_loader_unload(module);
  6009. return NULL;
  6010. }
  6011. void
  6012. wasm_loader_unload(WASMModule *module)
  6013. {
  6014. uint32 i;
  6015. if (!module)
  6016. return;
  6017. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6018. && WASM_ENABLE_LAZY_JIT != 0
  6019. module->orcjit_stop_compiling = true;
  6020. if (module->llvm_jit_init_thread)
  6021. os_thread_join(module->llvm_jit_init_thread, NULL);
  6022. #endif
  6023. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6024. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6025. module internal data after they were freed */
  6026. orcjit_stop_compile_threads(module);
  6027. #endif
  6028. #if WASM_ENABLE_JIT != 0
  6029. if (module->func_ptrs)
  6030. wasm_runtime_free(module->func_ptrs);
  6031. if (module->comp_ctx)
  6032. aot_destroy_comp_context(module->comp_ctx);
  6033. if (module->comp_data)
  6034. aot_destroy_comp_data(module->comp_data);
  6035. #endif
  6036. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6037. && WASM_ENABLE_LAZY_JIT != 0
  6038. if (module->tierup_wait_lock_inited) {
  6039. os_mutex_destroy(&module->tierup_wait_lock);
  6040. os_cond_destroy(&module->tierup_wait_cond);
  6041. }
  6042. #endif
  6043. if (module->imports)
  6044. wasm_runtime_free(module->imports);
  6045. if (module->functions) {
  6046. for (i = 0; i < module->function_count; i++) {
  6047. if (module->functions[i]) {
  6048. if (module->functions[i]->local_offsets)
  6049. wasm_runtime_free(module->functions[i]->local_offsets);
  6050. #if WASM_ENABLE_FAST_INTERP != 0
  6051. if (module->functions[i]->code_compiled)
  6052. wasm_runtime_free(module->functions[i]->code_compiled);
  6053. if (module->functions[i]->consts)
  6054. wasm_runtime_free(module->functions[i]->consts);
  6055. #endif
  6056. #if WASM_ENABLE_FAST_JIT != 0
  6057. if (module->functions[i]->fast_jit_jitted_code) {
  6058. jit_code_cache_free(
  6059. module->functions[i]->fast_jit_jitted_code);
  6060. }
  6061. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6062. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6063. jit_code_cache_free(
  6064. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6065. }
  6066. #endif
  6067. #endif
  6068. #if WASM_ENABLE_GC != 0
  6069. if (module->functions[i]->local_ref_type_maps) {
  6070. wasm_runtime_free(
  6071. module->functions[i]->local_ref_type_maps);
  6072. }
  6073. #endif
  6074. wasm_runtime_free(module->functions[i]);
  6075. }
  6076. }
  6077. wasm_runtime_free(module->functions);
  6078. }
  6079. if (module->tables) {
  6080. #if WASM_ENABLE_GC != 0
  6081. for (i = 0; i < module->table_count; i++) {
  6082. destroy_init_expr(module, &module->tables[i].init_expr);
  6083. }
  6084. #endif
  6085. wasm_runtime_free(module->tables);
  6086. }
  6087. if (module->memories)
  6088. wasm_runtime_free(module->memories);
  6089. if (module->globals) {
  6090. #if WASM_ENABLE_GC != 0
  6091. for (i = 0; i < module->global_count; i++) {
  6092. destroy_init_expr(module, &module->globals[i].init_expr);
  6093. }
  6094. #endif
  6095. wasm_runtime_free(module->globals);
  6096. }
  6097. #if WASM_ENABLE_TAGS != 0
  6098. if (module->tags) {
  6099. for (i = 0; i < module->tag_count; i++) {
  6100. if (module->tags[i])
  6101. wasm_runtime_free(module->tags[i]);
  6102. }
  6103. wasm_runtime_free(module->tags);
  6104. }
  6105. #endif
  6106. if (module->exports)
  6107. wasm_runtime_free(module->exports);
  6108. if (module->table_segments) {
  6109. for (i = 0; i < module->table_seg_count; i++) {
  6110. if (module->table_segments[i].init_values) {
  6111. #if WASM_ENABLE_GC != 0
  6112. uint32 j;
  6113. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6114. destroy_init_expr(
  6115. module, &module->table_segments[i].init_values[j]);
  6116. }
  6117. #endif
  6118. wasm_runtime_free(module->table_segments[i].init_values);
  6119. }
  6120. }
  6121. wasm_runtime_free(module->table_segments);
  6122. }
  6123. if (module->data_segments) {
  6124. for (i = 0; i < module->data_seg_count; i++) {
  6125. if (module->data_segments[i]) {
  6126. if (module->data_segments[i]->is_data_cloned)
  6127. wasm_runtime_free(module->data_segments[i]->data);
  6128. wasm_runtime_free(module->data_segments[i]);
  6129. }
  6130. }
  6131. wasm_runtime_free(module->data_segments);
  6132. }
  6133. if (module->types) {
  6134. for (i = 0; i < module->type_count; i++) {
  6135. if (module->types[i])
  6136. destroy_wasm_type(module->types[i]);
  6137. }
  6138. wasm_runtime_free(module->types);
  6139. }
  6140. if (module->const_str_list) {
  6141. StringNode *node = module->const_str_list, *node_next;
  6142. while (node) {
  6143. node_next = node->next;
  6144. wasm_runtime_free(node);
  6145. node = node_next;
  6146. }
  6147. }
  6148. #if WASM_ENABLE_STRINGREF != 0
  6149. if (module->string_literal_ptrs) {
  6150. wasm_runtime_free((void *)module->string_literal_ptrs);
  6151. }
  6152. if (module->string_literal_lengths) {
  6153. wasm_runtime_free(module->string_literal_lengths);
  6154. }
  6155. #endif
  6156. #if WASM_ENABLE_FAST_INTERP == 0
  6157. if (module->br_table_cache_list) {
  6158. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6159. BrTableCache *node_next;
  6160. while (node) {
  6161. node_next = bh_list_elem_next(node);
  6162. wasm_runtime_free(node);
  6163. node = node_next;
  6164. }
  6165. }
  6166. #endif
  6167. #if WASM_ENABLE_MULTI_MODULE != 0
  6168. /* just release the sub module list */
  6169. if (module->import_module_list) {
  6170. WASMRegisteredModule *node =
  6171. bh_list_first_elem(module->import_module_list);
  6172. while (node) {
  6173. WASMRegisteredModule *next = bh_list_elem_next(node);
  6174. bh_list_remove(module->import_module_list, node);
  6175. /*
  6176. * unload(sub_module) will be triggered during runtime_destroy().
  6177. * every module in the global module list will be unloaded one by
  6178. * one. so don't worry.
  6179. */
  6180. wasm_runtime_free(node);
  6181. /*
  6182. * the module file reading buffer will be released
  6183. * in runtime_destroy()
  6184. */
  6185. node = next;
  6186. }
  6187. }
  6188. #endif
  6189. #if WASM_ENABLE_DEBUG_INTERP != 0
  6190. WASMFastOPCodeNode *fast_opcode =
  6191. bh_list_first_elem(&module->fast_opcode_list);
  6192. while (fast_opcode) {
  6193. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6194. wasm_runtime_free(fast_opcode);
  6195. fast_opcode = next;
  6196. }
  6197. #endif
  6198. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6199. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6200. && WASM_ENABLE_LAZY_JIT != 0)
  6201. os_mutex_destroy(&module->instance_list_lock);
  6202. #endif
  6203. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6204. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6205. #endif
  6206. #if WASM_ENABLE_FAST_JIT != 0
  6207. if (module->fast_jit_func_ptrs) {
  6208. wasm_runtime_free(module->fast_jit_func_ptrs);
  6209. }
  6210. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6211. if (module->fast_jit_thread_locks_inited[i]) {
  6212. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6213. }
  6214. }
  6215. #endif
  6216. #if WASM_ENABLE_GC != 0
  6217. os_mutex_destroy(&module->rtt_type_lock);
  6218. bh_hash_map_destroy(module->ref_type_set);
  6219. if (module->rtt_types) {
  6220. for (i = 0; i < module->type_count; i++) {
  6221. if (module->rtt_types[i])
  6222. wasm_runtime_free(module->rtt_types[i]);
  6223. }
  6224. wasm_runtime_free(module->rtt_types);
  6225. }
  6226. #if WASM_ENABLE_STRINGREF != 0
  6227. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6228. if (module->stringref_rtts[i])
  6229. wasm_runtime_free(module->stringref_rtts[i]);
  6230. }
  6231. #endif
  6232. #endif
  6233. wasm_runtime_free(module);
  6234. }
  6235. bool
  6236. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6237. const uint8 *start_addr, const uint8 *code_end_addr,
  6238. uint8 label_type, uint8 **p_else_addr,
  6239. uint8 **p_end_addr)
  6240. {
  6241. const uint8 *p = start_addr, *p_end = code_end_addr;
  6242. uint8 *else_addr = NULL;
  6243. char error_buf[128];
  6244. uint32 block_nested_depth = 1, count, i, j, t;
  6245. uint32 error_buf_size = sizeof(error_buf);
  6246. uint8 opcode, u8;
  6247. BlockAddr block_stack[16] = { { 0 } }, *block;
  6248. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6249. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6250. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6251. if (block[j].start_addr == start_addr) {
  6252. /* Cache hit */
  6253. *p_else_addr = block[j].else_addr;
  6254. *p_end_addr = block[j].end_addr;
  6255. return true;
  6256. }
  6257. }
  6258. /* Cache unhit */
  6259. block_stack[0].start_addr = start_addr;
  6260. while (p < code_end_addr) {
  6261. opcode = *p++;
  6262. #if WASM_ENABLE_DEBUG_INTERP != 0
  6263. op_break_retry:
  6264. #endif
  6265. switch (opcode) {
  6266. case WASM_OP_UNREACHABLE:
  6267. case WASM_OP_NOP:
  6268. break;
  6269. #if WASM_ENABLE_EXCE_HANDLING != 0
  6270. case WASM_OP_TRY:
  6271. u8 = read_uint8(p);
  6272. if (block_nested_depth
  6273. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6274. block_stack[block_nested_depth].start_addr = p;
  6275. block_stack[block_nested_depth].else_addr = NULL;
  6276. }
  6277. block_nested_depth++;
  6278. break;
  6279. case EXT_OP_TRY:
  6280. skip_leb_uint32(p, p_end);
  6281. if (block_nested_depth
  6282. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6283. block_stack[block_nested_depth].start_addr = p;
  6284. block_stack[block_nested_depth].else_addr = NULL;
  6285. }
  6286. block_nested_depth++;
  6287. break;
  6288. case WASM_OP_CATCH:
  6289. if (block_nested_depth == 1) {
  6290. *p_end_addr = (uint8 *)(p - 1);
  6291. /* stop search and return the address of the catch block */
  6292. return true;
  6293. }
  6294. break;
  6295. case WASM_OP_CATCH_ALL:
  6296. if (block_nested_depth == 1) {
  6297. *p_end_addr = (uint8 *)(p - 1);
  6298. /* stop search and return the address of the catch_all block
  6299. */
  6300. return true;
  6301. }
  6302. break;
  6303. case WASM_OP_THROW:
  6304. /* skip tag_index */
  6305. skip_leb(p);
  6306. break;
  6307. case WASM_OP_RETHROW:
  6308. /* skip depth */
  6309. skip_leb(p);
  6310. break;
  6311. case WASM_OP_DELEGATE:
  6312. if (block_nested_depth == 1) {
  6313. *p_end_addr = (uint8 *)(p - 1);
  6314. return true;
  6315. }
  6316. else {
  6317. skip_leb(p);
  6318. /* the DELEGATE opcode ends the tryblock, */
  6319. block_nested_depth--;
  6320. if (block_nested_depth
  6321. < sizeof(block_stack) / sizeof(BlockAddr))
  6322. block_stack[block_nested_depth].end_addr =
  6323. (uint8 *)(p - 1);
  6324. }
  6325. break;
  6326. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6327. case WASM_OP_BLOCK:
  6328. case WASM_OP_LOOP:
  6329. case WASM_OP_IF:
  6330. {
  6331. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6332. u8 = read_uint8(p);
  6333. if (is_byte_a_type(u8)) {
  6334. #if WASM_ENABLE_GC != 0
  6335. if (wasm_is_type_multi_byte_type(u8)) {
  6336. /* the possible extra bytes of GC ref type have been
  6337. modified to OP_NOP, no need to resolve them again */
  6338. }
  6339. #endif
  6340. }
  6341. else {
  6342. p--;
  6343. /* block type */
  6344. skip_leb_int32(p, p_end);
  6345. }
  6346. if (block_nested_depth
  6347. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6348. block_stack[block_nested_depth].start_addr = p;
  6349. block_stack[block_nested_depth].else_addr = NULL;
  6350. }
  6351. block_nested_depth++;
  6352. break;
  6353. }
  6354. case EXT_OP_BLOCK:
  6355. case EXT_OP_LOOP:
  6356. case EXT_OP_IF:
  6357. /* block type */
  6358. skip_leb_int32(p, p_end);
  6359. if (block_nested_depth
  6360. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6361. block_stack[block_nested_depth].start_addr = p;
  6362. block_stack[block_nested_depth].else_addr = NULL;
  6363. }
  6364. block_nested_depth++;
  6365. break;
  6366. case WASM_OP_ELSE:
  6367. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6368. else_addr = (uint8 *)(p - 1);
  6369. if (block_nested_depth - 1
  6370. < sizeof(block_stack) / sizeof(BlockAddr))
  6371. block_stack[block_nested_depth - 1].else_addr =
  6372. (uint8 *)(p - 1);
  6373. break;
  6374. case WASM_OP_END:
  6375. if (block_nested_depth == 1) {
  6376. if (label_type == LABEL_TYPE_IF)
  6377. *p_else_addr = else_addr;
  6378. *p_end_addr = (uint8 *)(p - 1);
  6379. block_stack[0].end_addr = (uint8 *)(p - 1);
  6380. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6381. t++) {
  6382. start_addr = block_stack[t].start_addr;
  6383. if (start_addr) {
  6384. i = ((uintptr_t)start_addr)
  6385. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6386. block =
  6387. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6388. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6389. if (!block[j].start_addr)
  6390. break;
  6391. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6392. memmove(block + 1, block,
  6393. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6394. * sizeof(BlockAddr));
  6395. j = 0;
  6396. }
  6397. block[j].start_addr = block_stack[t].start_addr;
  6398. block[j].else_addr = block_stack[t].else_addr;
  6399. block[j].end_addr = block_stack[t].end_addr;
  6400. }
  6401. else
  6402. break;
  6403. }
  6404. return true;
  6405. }
  6406. else {
  6407. block_nested_depth--;
  6408. if (block_nested_depth
  6409. < sizeof(block_stack) / sizeof(BlockAddr))
  6410. block_stack[block_nested_depth].end_addr =
  6411. (uint8 *)(p - 1);
  6412. }
  6413. break;
  6414. case WASM_OP_BR:
  6415. case WASM_OP_BR_IF:
  6416. skip_leb_uint32(p, p_end); /* labelidx */
  6417. break;
  6418. case WASM_OP_BR_TABLE:
  6419. read_leb_uint32(p, p_end, count); /* label num */
  6420. #if WASM_ENABLE_FAST_INTERP != 0
  6421. for (i = 0; i <= count; i++) /* labelidxs */
  6422. skip_leb_uint32(p, p_end);
  6423. #else
  6424. p += count + 1;
  6425. while (*p == WASM_OP_NOP)
  6426. p++;
  6427. #endif
  6428. break;
  6429. #if WASM_ENABLE_FAST_INTERP == 0
  6430. case EXT_OP_BR_TABLE_CACHE:
  6431. read_leb_uint32(p, p_end, count); /* label num */
  6432. while (*p == WASM_OP_NOP)
  6433. p++;
  6434. break;
  6435. #endif
  6436. case WASM_OP_RETURN:
  6437. break;
  6438. case WASM_OP_CALL:
  6439. #if WASM_ENABLE_TAIL_CALL != 0
  6440. case WASM_OP_RETURN_CALL:
  6441. #endif
  6442. skip_leb_uint32(p, p_end); /* funcidx */
  6443. break;
  6444. case WASM_OP_CALL_INDIRECT:
  6445. #if WASM_ENABLE_TAIL_CALL != 0
  6446. case WASM_OP_RETURN_CALL_INDIRECT:
  6447. #endif
  6448. skip_leb_uint32(p, p_end); /* typeidx */
  6449. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6450. u8 = read_uint8(p); /* 0x00 */
  6451. #else
  6452. skip_leb_uint32(p, p_end); /* tableidx */
  6453. #endif
  6454. break;
  6455. #if WASM_ENABLE_GC != 0
  6456. case WASM_OP_CALL_REF:
  6457. case WASM_OP_RETURN_CALL_REF:
  6458. skip_leb_uint32(p, p_end); /* typeidx */
  6459. break;
  6460. #endif
  6461. case WASM_OP_DROP:
  6462. case WASM_OP_SELECT:
  6463. case WASM_OP_DROP_64:
  6464. case WASM_OP_SELECT_64:
  6465. break;
  6466. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6467. case WASM_OP_SELECT_T:
  6468. {
  6469. skip_leb_uint32(p, p_end); /* vec length */
  6470. u8 = read_uint8(p); /* typeidx */
  6471. /* the possible extra bytes of GC ref type have been
  6472. modified to OP_NOP, no need to resolve them again */
  6473. break;
  6474. }
  6475. case WASM_OP_TABLE_GET:
  6476. case WASM_OP_TABLE_SET:
  6477. skip_leb_uint32(p, p_end); /* table index */
  6478. break;
  6479. case WASM_OP_REF_NULL:
  6480. {
  6481. u8 = read_uint8(p); /* type */
  6482. if (is_byte_a_type(u8)) {
  6483. #if WASM_ENABLE_GC != 0
  6484. if (wasm_is_type_multi_byte_type(u8)) {
  6485. /* the possible extra bytes of GC ref type have been
  6486. modified to OP_NOP, no need to resolve them again */
  6487. }
  6488. #endif
  6489. }
  6490. else {
  6491. p--;
  6492. skip_leb_uint32(p, p_end);
  6493. }
  6494. break;
  6495. }
  6496. case WASM_OP_REF_IS_NULL:
  6497. break;
  6498. case WASM_OP_REF_FUNC:
  6499. skip_leb_uint32(p, p_end); /* func index */
  6500. break;
  6501. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6502. #if WASM_ENABLE_GC != 0
  6503. case WASM_OP_REF_AS_NON_NULL:
  6504. case WASM_OP_REF_EQ:
  6505. break;
  6506. case WASM_OP_BR_ON_NULL:
  6507. case WASM_OP_BR_ON_NON_NULL:
  6508. skip_leb_uint32(p, p_end); /* label index */
  6509. break;
  6510. #endif /* end of WASM_ENABLE_GC != 0 */
  6511. case WASM_OP_GET_LOCAL:
  6512. case WASM_OP_SET_LOCAL:
  6513. case WASM_OP_TEE_LOCAL:
  6514. case WASM_OP_GET_GLOBAL:
  6515. case WASM_OP_SET_GLOBAL:
  6516. case WASM_OP_GET_GLOBAL_64:
  6517. case WASM_OP_SET_GLOBAL_64:
  6518. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6519. skip_leb_uint32(p, p_end); /* local index */
  6520. break;
  6521. case EXT_OP_GET_LOCAL_FAST:
  6522. case EXT_OP_SET_LOCAL_FAST:
  6523. case EXT_OP_TEE_LOCAL_FAST:
  6524. CHECK_BUF(p, p_end, 1);
  6525. p++;
  6526. break;
  6527. case WASM_OP_I32_LOAD:
  6528. case WASM_OP_I64_LOAD:
  6529. case WASM_OP_F32_LOAD:
  6530. case WASM_OP_F64_LOAD:
  6531. case WASM_OP_I32_LOAD8_S:
  6532. case WASM_OP_I32_LOAD8_U:
  6533. case WASM_OP_I32_LOAD16_S:
  6534. case WASM_OP_I32_LOAD16_U:
  6535. case WASM_OP_I64_LOAD8_S:
  6536. case WASM_OP_I64_LOAD8_U:
  6537. case WASM_OP_I64_LOAD16_S:
  6538. case WASM_OP_I64_LOAD16_U:
  6539. case WASM_OP_I64_LOAD32_S:
  6540. case WASM_OP_I64_LOAD32_U:
  6541. case WASM_OP_I32_STORE:
  6542. case WASM_OP_I64_STORE:
  6543. case WASM_OP_F32_STORE:
  6544. case WASM_OP_F64_STORE:
  6545. case WASM_OP_I32_STORE8:
  6546. case WASM_OP_I32_STORE16:
  6547. case WASM_OP_I64_STORE8:
  6548. case WASM_OP_I64_STORE16:
  6549. case WASM_OP_I64_STORE32:
  6550. skip_leb_uint32(p, p_end); /* align */
  6551. skip_leb_mem_offset(p, p_end); /* offset */
  6552. break;
  6553. case WASM_OP_MEMORY_SIZE:
  6554. case WASM_OP_MEMORY_GROW:
  6555. skip_leb_uint32(p, p_end); /* 0x00 */
  6556. break;
  6557. case WASM_OP_I32_CONST:
  6558. skip_leb_int32(p, p_end);
  6559. break;
  6560. case WASM_OP_I64_CONST:
  6561. skip_leb_int64(p, p_end);
  6562. break;
  6563. case WASM_OP_F32_CONST:
  6564. p += sizeof(float32);
  6565. break;
  6566. case WASM_OP_F64_CONST:
  6567. p += sizeof(float64);
  6568. break;
  6569. case WASM_OP_I32_EQZ:
  6570. case WASM_OP_I32_EQ:
  6571. case WASM_OP_I32_NE:
  6572. case WASM_OP_I32_LT_S:
  6573. case WASM_OP_I32_LT_U:
  6574. case WASM_OP_I32_GT_S:
  6575. case WASM_OP_I32_GT_U:
  6576. case WASM_OP_I32_LE_S:
  6577. case WASM_OP_I32_LE_U:
  6578. case WASM_OP_I32_GE_S:
  6579. case WASM_OP_I32_GE_U:
  6580. case WASM_OP_I64_EQZ:
  6581. case WASM_OP_I64_EQ:
  6582. case WASM_OP_I64_NE:
  6583. case WASM_OP_I64_LT_S:
  6584. case WASM_OP_I64_LT_U:
  6585. case WASM_OP_I64_GT_S:
  6586. case WASM_OP_I64_GT_U:
  6587. case WASM_OP_I64_LE_S:
  6588. case WASM_OP_I64_LE_U:
  6589. case WASM_OP_I64_GE_S:
  6590. case WASM_OP_I64_GE_U:
  6591. case WASM_OP_F32_EQ:
  6592. case WASM_OP_F32_NE:
  6593. case WASM_OP_F32_LT:
  6594. case WASM_OP_F32_GT:
  6595. case WASM_OP_F32_LE:
  6596. case WASM_OP_F32_GE:
  6597. case WASM_OP_F64_EQ:
  6598. case WASM_OP_F64_NE:
  6599. case WASM_OP_F64_LT:
  6600. case WASM_OP_F64_GT:
  6601. case WASM_OP_F64_LE:
  6602. case WASM_OP_F64_GE:
  6603. case WASM_OP_I32_CLZ:
  6604. case WASM_OP_I32_CTZ:
  6605. case WASM_OP_I32_POPCNT:
  6606. case WASM_OP_I32_ADD:
  6607. case WASM_OP_I32_SUB:
  6608. case WASM_OP_I32_MUL:
  6609. case WASM_OP_I32_DIV_S:
  6610. case WASM_OP_I32_DIV_U:
  6611. case WASM_OP_I32_REM_S:
  6612. case WASM_OP_I32_REM_U:
  6613. case WASM_OP_I32_AND:
  6614. case WASM_OP_I32_OR:
  6615. case WASM_OP_I32_XOR:
  6616. case WASM_OP_I32_SHL:
  6617. case WASM_OP_I32_SHR_S:
  6618. case WASM_OP_I32_SHR_U:
  6619. case WASM_OP_I32_ROTL:
  6620. case WASM_OP_I32_ROTR:
  6621. case WASM_OP_I64_CLZ:
  6622. case WASM_OP_I64_CTZ:
  6623. case WASM_OP_I64_POPCNT:
  6624. case WASM_OP_I64_ADD:
  6625. case WASM_OP_I64_SUB:
  6626. case WASM_OP_I64_MUL:
  6627. case WASM_OP_I64_DIV_S:
  6628. case WASM_OP_I64_DIV_U:
  6629. case WASM_OP_I64_REM_S:
  6630. case WASM_OP_I64_REM_U:
  6631. case WASM_OP_I64_AND:
  6632. case WASM_OP_I64_OR:
  6633. case WASM_OP_I64_XOR:
  6634. case WASM_OP_I64_SHL:
  6635. case WASM_OP_I64_SHR_S:
  6636. case WASM_OP_I64_SHR_U:
  6637. case WASM_OP_I64_ROTL:
  6638. case WASM_OP_I64_ROTR:
  6639. case WASM_OP_F32_ABS:
  6640. case WASM_OP_F32_NEG:
  6641. case WASM_OP_F32_CEIL:
  6642. case WASM_OP_F32_FLOOR:
  6643. case WASM_OP_F32_TRUNC:
  6644. case WASM_OP_F32_NEAREST:
  6645. case WASM_OP_F32_SQRT:
  6646. case WASM_OP_F32_ADD:
  6647. case WASM_OP_F32_SUB:
  6648. case WASM_OP_F32_MUL:
  6649. case WASM_OP_F32_DIV:
  6650. case WASM_OP_F32_MIN:
  6651. case WASM_OP_F32_MAX:
  6652. case WASM_OP_F32_COPYSIGN:
  6653. case WASM_OP_F64_ABS:
  6654. case WASM_OP_F64_NEG:
  6655. case WASM_OP_F64_CEIL:
  6656. case WASM_OP_F64_FLOOR:
  6657. case WASM_OP_F64_TRUNC:
  6658. case WASM_OP_F64_NEAREST:
  6659. case WASM_OP_F64_SQRT:
  6660. case WASM_OP_F64_ADD:
  6661. case WASM_OP_F64_SUB:
  6662. case WASM_OP_F64_MUL:
  6663. case WASM_OP_F64_DIV:
  6664. case WASM_OP_F64_MIN:
  6665. case WASM_OP_F64_MAX:
  6666. case WASM_OP_F64_COPYSIGN:
  6667. case WASM_OP_I32_WRAP_I64:
  6668. case WASM_OP_I32_TRUNC_S_F32:
  6669. case WASM_OP_I32_TRUNC_U_F32:
  6670. case WASM_OP_I32_TRUNC_S_F64:
  6671. case WASM_OP_I32_TRUNC_U_F64:
  6672. case WASM_OP_I64_EXTEND_S_I32:
  6673. case WASM_OP_I64_EXTEND_U_I32:
  6674. case WASM_OP_I64_TRUNC_S_F32:
  6675. case WASM_OP_I64_TRUNC_U_F32:
  6676. case WASM_OP_I64_TRUNC_S_F64:
  6677. case WASM_OP_I64_TRUNC_U_F64:
  6678. case WASM_OP_F32_CONVERT_S_I32:
  6679. case WASM_OP_F32_CONVERT_U_I32:
  6680. case WASM_OP_F32_CONVERT_S_I64:
  6681. case WASM_OP_F32_CONVERT_U_I64:
  6682. case WASM_OP_F32_DEMOTE_F64:
  6683. case WASM_OP_F64_CONVERT_S_I32:
  6684. case WASM_OP_F64_CONVERT_U_I32:
  6685. case WASM_OP_F64_CONVERT_S_I64:
  6686. case WASM_OP_F64_CONVERT_U_I64:
  6687. case WASM_OP_F64_PROMOTE_F32:
  6688. case WASM_OP_I32_REINTERPRET_F32:
  6689. case WASM_OP_I64_REINTERPRET_F64:
  6690. case WASM_OP_F32_REINTERPRET_I32:
  6691. case WASM_OP_F64_REINTERPRET_I64:
  6692. case WASM_OP_I32_EXTEND8_S:
  6693. case WASM_OP_I32_EXTEND16_S:
  6694. case WASM_OP_I64_EXTEND8_S:
  6695. case WASM_OP_I64_EXTEND16_S:
  6696. case WASM_OP_I64_EXTEND32_S:
  6697. break;
  6698. #if WASM_ENABLE_GC != 0
  6699. case WASM_OP_GC_PREFIX:
  6700. {
  6701. uint32 opcode1;
  6702. read_leb_uint32(p, p_end, opcode1);
  6703. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6704. is no larger than UINT8_MAX */
  6705. opcode = (uint8)opcode1;
  6706. switch (opcode) {
  6707. case WASM_OP_STRUCT_NEW:
  6708. case WASM_OP_STRUCT_NEW_DEFAULT:
  6709. skip_leb_uint32(p, p_end); /* typeidx */
  6710. break;
  6711. case WASM_OP_STRUCT_GET:
  6712. case WASM_OP_STRUCT_GET_S:
  6713. case WASM_OP_STRUCT_GET_U:
  6714. case WASM_OP_STRUCT_SET:
  6715. skip_leb_uint32(p, p_end); /* typeidx */
  6716. skip_leb_uint32(p, p_end); /* fieldidx */
  6717. break;
  6718. case WASM_OP_ARRAY_NEW:
  6719. case WASM_OP_ARRAY_NEW_DEFAULT:
  6720. case WASM_OP_ARRAY_GET:
  6721. case WASM_OP_ARRAY_GET_S:
  6722. case WASM_OP_ARRAY_GET_U:
  6723. case WASM_OP_ARRAY_SET:
  6724. case WASM_OP_ARRAY_FILL:
  6725. skip_leb_uint32(p, p_end); /* typeidx */
  6726. break;
  6727. case WASM_OP_ARRAY_COPY:
  6728. skip_leb_uint32(p, p_end); /* typeidx1 */
  6729. skip_leb_uint32(p, p_end); /* typeidx2 */
  6730. break;
  6731. case WASM_OP_ARRAY_LEN:
  6732. break;
  6733. case WASM_OP_ARRAY_NEW_FIXED:
  6734. case WASM_OP_ARRAY_NEW_DATA:
  6735. case WASM_OP_ARRAY_NEW_ELEM:
  6736. skip_leb_uint32(p, p_end); /* typeidx */
  6737. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6738. break;
  6739. case WASM_OP_REF_I31:
  6740. case WASM_OP_I31_GET_S:
  6741. case WASM_OP_I31_GET_U:
  6742. break;
  6743. case WASM_OP_REF_TEST:
  6744. case WASM_OP_REF_CAST:
  6745. case WASM_OP_REF_TEST_NULLABLE:
  6746. case WASM_OP_REF_CAST_NULLABLE:
  6747. skip_leb_int32(p, p_end); /* heaptype */
  6748. break;
  6749. case WASM_OP_BR_ON_CAST:
  6750. case WASM_OP_BR_ON_CAST_FAIL:
  6751. p += sizeof(uint8); /* castflag */
  6752. skip_leb_uint32(p, p_end); /* labelidx */
  6753. skip_leb_int32(p, p_end); /* heaptype */
  6754. skip_leb_int32(p, p_end); /* heaptype2 */
  6755. break;
  6756. case WASM_OP_ANY_CONVERT_EXTERN:
  6757. case WASM_OP_EXTERN_CONVERT_ANY:
  6758. break;
  6759. #if WASM_ENABLE_STRINGREF != 0
  6760. case WASM_OP_STRING_NEW_UTF8:
  6761. case WASM_OP_STRING_NEW_WTF16:
  6762. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6763. case WASM_OP_STRING_NEW_WTF8:
  6764. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6765. break;
  6766. case WASM_OP_STRING_CONST:
  6767. skip_leb_int32(p, p_end); /* contents */
  6768. break;
  6769. case WASM_OP_STRING_MEASURE_UTF8:
  6770. case WASM_OP_STRING_MEASURE_WTF8:
  6771. case WASM_OP_STRING_MEASURE_WTF16:
  6772. break;
  6773. case WASM_OP_STRING_ENCODE_UTF8:
  6774. case WASM_OP_STRING_ENCODE_WTF16:
  6775. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6776. case WASM_OP_STRING_ENCODE_WTF8:
  6777. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6778. break;
  6779. case WASM_OP_STRING_CONCAT:
  6780. case WASM_OP_STRING_EQ:
  6781. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6782. case WASM_OP_STRING_AS_WTF8:
  6783. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6784. break;
  6785. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6786. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6787. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6788. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6789. break;
  6790. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6791. case WASM_OP_STRING_AS_WTF16:
  6792. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6793. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6794. break;
  6795. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6796. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6797. break;
  6798. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6799. case WASM_OP_STRING_AS_ITER:
  6800. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6801. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6802. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6803. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6804. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6805. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6806. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6807. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6808. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6809. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6810. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6811. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6812. break;
  6813. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6814. default:
  6815. return false;
  6816. }
  6817. break;
  6818. }
  6819. #endif /* end of WASM_ENABLE_GC != 0 */
  6820. case WASM_OP_MISC_PREFIX:
  6821. {
  6822. uint32 opcode1;
  6823. read_leb_uint32(p, p_end, opcode1);
  6824. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6825. is no larger than UINT8_MAX */
  6826. opcode = (uint8)opcode1;
  6827. switch (opcode) {
  6828. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6829. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6830. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6831. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6832. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6833. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6834. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6835. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6836. break;
  6837. #if WASM_ENABLE_BULK_MEMORY != 0
  6838. case WASM_OP_MEMORY_INIT:
  6839. skip_leb_uint32(p, p_end);
  6840. /* skip memory idx */
  6841. p++;
  6842. break;
  6843. case WASM_OP_DATA_DROP:
  6844. skip_leb_uint32(p, p_end);
  6845. break;
  6846. case WASM_OP_MEMORY_COPY:
  6847. /* skip two memory idx */
  6848. p += 2;
  6849. break;
  6850. case WASM_OP_MEMORY_FILL:
  6851. /* skip memory idx */
  6852. p++;
  6853. break;
  6854. #endif /* WASM_ENABLE_BULK_MEMORY */
  6855. #if WASM_ENABLE_REF_TYPES != 0
  6856. case WASM_OP_TABLE_INIT:
  6857. case WASM_OP_TABLE_COPY:
  6858. /* tableidx */
  6859. skip_leb_uint32(p, p_end);
  6860. /* elemidx */
  6861. skip_leb_uint32(p, p_end);
  6862. break;
  6863. case WASM_OP_ELEM_DROP:
  6864. /* elemidx */
  6865. skip_leb_uint32(p, p_end);
  6866. break;
  6867. case WASM_OP_TABLE_SIZE:
  6868. case WASM_OP_TABLE_GROW:
  6869. case WASM_OP_TABLE_FILL:
  6870. skip_leb_uint32(p, p_end); /* table idx */
  6871. break;
  6872. #endif /* WASM_ENABLE_REF_TYPES */
  6873. default:
  6874. return false;
  6875. }
  6876. break;
  6877. }
  6878. #if WASM_ENABLE_SIMD != 0
  6879. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6880. case WASM_OP_SIMD_PREFIX:
  6881. {
  6882. uint32 opcode1;
  6883. read_leb_uint32(p, p_end, opcode1);
  6884. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6885. is no larger than UINT8_MAX */
  6886. opcode = (uint8)opcode1;
  6887. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6888. */
  6889. switch (opcode) {
  6890. case SIMD_v128_load:
  6891. case SIMD_v128_load8x8_s:
  6892. case SIMD_v128_load8x8_u:
  6893. case SIMD_v128_load16x4_s:
  6894. case SIMD_v128_load16x4_u:
  6895. case SIMD_v128_load32x2_s:
  6896. case SIMD_v128_load32x2_u:
  6897. case SIMD_v128_load8_splat:
  6898. case SIMD_v128_load16_splat:
  6899. case SIMD_v128_load32_splat:
  6900. case SIMD_v128_load64_splat:
  6901. case SIMD_v128_store:
  6902. /* memarg align */
  6903. skip_leb_uint32(p, p_end);
  6904. /* memarg offset */
  6905. skip_leb_mem_offset(p, p_end);
  6906. break;
  6907. case SIMD_v128_const:
  6908. case SIMD_v8x16_shuffle:
  6909. /* immByte[16] immLaneId[16] */
  6910. CHECK_BUF1(p, p_end, 16);
  6911. p += 16;
  6912. break;
  6913. case SIMD_i8x16_extract_lane_s:
  6914. case SIMD_i8x16_extract_lane_u:
  6915. case SIMD_i8x16_replace_lane:
  6916. case SIMD_i16x8_extract_lane_s:
  6917. case SIMD_i16x8_extract_lane_u:
  6918. case SIMD_i16x8_replace_lane:
  6919. case SIMD_i32x4_extract_lane:
  6920. case SIMD_i32x4_replace_lane:
  6921. case SIMD_i64x2_extract_lane:
  6922. case SIMD_i64x2_replace_lane:
  6923. case SIMD_f32x4_extract_lane:
  6924. case SIMD_f32x4_replace_lane:
  6925. case SIMD_f64x2_extract_lane:
  6926. case SIMD_f64x2_replace_lane:
  6927. /* ImmLaneId */
  6928. CHECK_BUF(p, p_end, 1);
  6929. p++;
  6930. break;
  6931. case SIMD_v128_load8_lane:
  6932. case SIMD_v128_load16_lane:
  6933. case SIMD_v128_load32_lane:
  6934. case SIMD_v128_load64_lane:
  6935. case SIMD_v128_store8_lane:
  6936. case SIMD_v128_store16_lane:
  6937. case SIMD_v128_store32_lane:
  6938. case SIMD_v128_store64_lane:
  6939. /* memarg align */
  6940. skip_leb_uint32(p, p_end);
  6941. /* memarg offset */
  6942. skip_leb_mem_offset(p, p_end);
  6943. /* ImmLaneId */
  6944. CHECK_BUF(p, p_end, 1);
  6945. p++;
  6946. break;
  6947. case SIMD_v128_load32_zero:
  6948. case SIMD_v128_load64_zero:
  6949. /* memarg align */
  6950. skip_leb_uint32(p, p_end);
  6951. /* memarg offset */
  6952. skip_leb_mem_offset(p, p_end);
  6953. break;
  6954. default:
  6955. /*
  6956. * since latest SIMD specific used almost every value
  6957. * from 0x00 to 0xff, the default branch will present
  6958. * all opcodes without imm
  6959. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6960. */
  6961. break;
  6962. }
  6963. break;
  6964. }
  6965. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6966. #endif /* end of WASM_ENABLE_SIMD */
  6967. #if WASM_ENABLE_SHARED_MEMORY != 0
  6968. case WASM_OP_ATOMIC_PREFIX:
  6969. {
  6970. /* TODO: memory64 offset type changes */
  6971. uint32 opcode1;
  6972. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6973. read_leb_uint32(p, p_end, opcode1);
  6974. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6975. is no larger than UINT8_MAX */
  6976. opcode = (uint8)opcode1;
  6977. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6978. skip_leb_uint32(p, p_end); /* align */
  6979. skip_leb_mem_offset(p, p_end); /* offset */
  6980. }
  6981. else {
  6982. /* atomic.fence doesn't have memarg */
  6983. p++;
  6984. }
  6985. break;
  6986. }
  6987. #endif
  6988. #if WASM_ENABLE_DEBUG_INTERP != 0
  6989. case DEBUG_OP_BREAK:
  6990. {
  6991. WASMDebugInstance *debug_instance =
  6992. wasm_exec_env_get_instance(exec_env);
  6993. char original_opcode[1];
  6994. uint64 size = 1;
  6995. WASMModuleInstance *module_inst =
  6996. (WASMModuleInstance *)exec_env->module_inst;
  6997. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6998. ? (p - 1) - module_inst->module->load_addr
  6999. : ~0;
  7000. if (debug_instance) {
  7001. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7002. original_opcode, &size)
  7003. && size == 1) {
  7004. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7005. "with %02x: ",
  7006. original_opcode[0]);
  7007. opcode = original_opcode[0];
  7008. goto op_break_retry;
  7009. }
  7010. }
  7011. break;
  7012. }
  7013. #endif
  7014. default:
  7015. return false;
  7016. }
  7017. }
  7018. (void)u8;
  7019. (void)exec_env;
  7020. return false;
  7021. fail:
  7022. return false;
  7023. }
  7024. #if WASM_ENABLE_FAST_INTERP != 0
  7025. #if WASM_DEBUG_PREPROCESSOR != 0
  7026. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7027. #else
  7028. #define LOG_OP(...) (void)0
  7029. #endif
  7030. #define PATCH_ELSE 0
  7031. #define PATCH_END 1
  7032. typedef struct BranchBlockPatch {
  7033. struct BranchBlockPatch *next;
  7034. uint8 patch_type;
  7035. uint8 *code_compiled;
  7036. } BranchBlockPatch;
  7037. #endif
  7038. typedef struct BranchBlock {
  7039. uint8 label_type;
  7040. BlockType block_type;
  7041. uint8 *start_addr;
  7042. uint8 *else_addr;
  7043. uint8 *end_addr;
  7044. uint32 stack_cell_num;
  7045. #if WASM_ENABLE_GC != 0
  7046. uint32 reftype_map_num;
  7047. /* Indicate which local is used inside current block, used to validate
  7048. * local.get with non-nullable ref types */
  7049. uint8 *local_use_mask;
  7050. uint32 local_use_mask_size;
  7051. #endif
  7052. #if WASM_ENABLE_FAST_INTERP != 0
  7053. uint16 dynamic_offset;
  7054. uint8 *code_compiled;
  7055. BranchBlockPatch *patch_list;
  7056. /* This is used to save params frame_offset of of if block */
  7057. int16 *param_frame_offsets;
  7058. /* This is used to recover the dynamic offset for else branch,
  7059. * and also to remember the start offset of dynamic space which
  7060. * stores the block arguments for loop block, so we can use it
  7061. * to copy the stack operands to the loop block's arguments in
  7062. * wasm_loader_emit_br_info for opcode br. */
  7063. uint16 start_dynamic_offset;
  7064. #endif
  7065. /* Indicate the operand stack is in polymorphic state.
  7066. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7067. * to polymorphic state until the block's 'end' opcode is processed.
  7068. * If stack is in polymorphic state and stack is empty, instruction can
  7069. * pop any type of value directly without decreasing stack top pointer
  7070. * and stack cell num. */
  7071. bool is_stack_polymorphic;
  7072. } BranchBlock;
  7073. typedef struct WASMLoaderContext {
  7074. /* frame ref stack */
  7075. uint8 *frame_ref;
  7076. uint8 *frame_ref_bottom;
  7077. uint8 *frame_ref_boundary;
  7078. uint32 frame_ref_size;
  7079. uint32 stack_cell_num;
  7080. uint32 max_stack_cell_num;
  7081. #if WASM_ENABLE_GC != 0
  7082. /* frame reftype map stack */
  7083. WASMRefTypeMap *frame_reftype_map;
  7084. WASMRefTypeMap *frame_reftype_map_bottom;
  7085. WASMRefTypeMap *frame_reftype_map_boundary;
  7086. uint32 frame_reftype_map_size;
  7087. uint32 reftype_map_num;
  7088. uint32 max_reftype_map_num;
  7089. /* Current module */
  7090. WASMModule *module;
  7091. /* Current module's ref_type_set */
  7092. HashMap *ref_type_set;
  7093. /* Always point to local variable ref_type of
  7094. wasm_loader_prepare_bytecode */
  7095. WASMRefType *ref_type_tmp;
  7096. #endif
  7097. /* frame csp stack */
  7098. BranchBlock *frame_csp;
  7099. BranchBlock *frame_csp_bottom;
  7100. BranchBlock *frame_csp_boundary;
  7101. uint32 frame_csp_size;
  7102. uint32 csp_num;
  7103. uint32 max_csp_num;
  7104. #if WASM_ENABLE_FAST_INTERP != 0
  7105. /* frame offset stack */
  7106. int16 *frame_offset;
  7107. int16 *frame_offset_bottom;
  7108. int16 *frame_offset_boundary;
  7109. uint32 frame_offset_size;
  7110. int16 dynamic_offset;
  7111. int16 start_dynamic_offset;
  7112. int16 max_dynamic_offset;
  7113. /* preserved local offset */
  7114. int16 preserved_local_offset;
  7115. /* const buffer */
  7116. uint8 *const_buf;
  7117. uint16 num_const;
  7118. uint16 const_cell_num;
  7119. uint32 const_buf_size;
  7120. /* processed code */
  7121. uint8 *p_code_compiled;
  7122. uint8 *p_code_compiled_end;
  7123. uint32 code_compiled_size;
  7124. /* If the last opcode will be dropped, the peak memory usage will be larger
  7125. * than the final code_compiled_size, we record the peak size to ensure
  7126. * there will not be invalid memory access during second traverse */
  7127. uint32 code_compiled_peak_size;
  7128. #endif
  7129. } WASMLoaderContext;
  7130. typedef struct Const {
  7131. WASMValue value;
  7132. uint16 slot_index;
  7133. uint8 value_type;
  7134. } Const;
  7135. #define CHECK_CSP_PUSH() \
  7136. do { \
  7137. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7138. MEM_REALLOC( \
  7139. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7140. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7141. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7142. ctx->frame_csp_boundary = \
  7143. ctx->frame_csp_bottom \
  7144. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7145. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7146. } \
  7147. } while (0)
  7148. #define CHECK_CSP_POP() \
  7149. do { \
  7150. if (ctx->csp_num < 1) { \
  7151. set_error_buf(error_buf, error_buf_size, \
  7152. "type mismatch: " \
  7153. "expect data but block stack was empty"); \
  7154. goto fail; \
  7155. } \
  7156. } while (0)
  7157. #if WASM_ENABLE_FAST_INTERP != 0
  7158. static bool
  7159. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7160. uint32 error_buf_size)
  7161. {
  7162. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7163. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7164. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7165. ctx->frame_offset_size + 16);
  7166. ctx->frame_offset_size += 16;
  7167. ctx->frame_offset_boundary =
  7168. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7169. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7170. }
  7171. return true;
  7172. fail:
  7173. return false;
  7174. }
  7175. static bool
  7176. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7177. {
  7178. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7179. return false;
  7180. return true;
  7181. }
  7182. static void
  7183. free_label_patch_list(BranchBlock *frame_csp)
  7184. {
  7185. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7186. BranchBlockPatch *next;
  7187. while (label_patch != NULL) {
  7188. next = label_patch->next;
  7189. wasm_runtime_free(label_patch);
  7190. label_patch = next;
  7191. }
  7192. frame_csp->patch_list = NULL;
  7193. }
  7194. static void
  7195. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7196. {
  7197. BranchBlock *tmp_csp = frame_csp;
  7198. uint32 i;
  7199. for (i = 0; i < csp_num; i++) {
  7200. free_label_patch_list(tmp_csp);
  7201. tmp_csp++;
  7202. }
  7203. }
  7204. static void
  7205. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7206. {
  7207. BranchBlock *tmp_csp = frame_csp;
  7208. uint32 i;
  7209. for (i = 0; i < csp_num; i++) {
  7210. if (tmp_csp->param_frame_offsets)
  7211. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7212. tmp_csp++;
  7213. }
  7214. }
  7215. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7216. #if WASM_ENABLE_GC != 0
  7217. static bool
  7218. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7219. char *error_buf, uint32 error_buf_size)
  7220. {
  7221. BranchBlock *current_csp = ctx->frame_csp - 1;
  7222. uint32 local_mask_size;
  7223. if (local_count == 0) {
  7224. current_csp->local_use_mask_size = 0;
  7225. return true;
  7226. }
  7227. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7228. * else branch, we don't need to allocate memory again */
  7229. if (!current_csp->local_use_mask) {
  7230. local_mask_size = (local_count + 7) / sizeof(uint8);
  7231. if (!(current_csp->local_use_mask =
  7232. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7233. return false;
  7234. }
  7235. current_csp->local_use_mask_size = local_mask_size;
  7236. }
  7237. else {
  7238. local_mask_size = current_csp->local_use_mask_size;
  7239. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7240. }
  7241. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7242. /* For non-function blocks, inherit the use status from parent block */
  7243. BranchBlock *parent_csp = current_csp - 1;
  7244. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7245. bh_assert(parent_csp->local_use_mask);
  7246. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7247. parent_csp->local_use_mask, local_mask_size);
  7248. }
  7249. return true;
  7250. }
  7251. static void
  7252. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7253. {
  7254. BranchBlock *current_csp = ctx->frame_csp - 1;
  7255. bh_assert(current_csp->local_use_mask
  7256. || current_csp->local_use_mask_size == 0);
  7257. if (current_csp->local_use_mask) {
  7258. wasm_runtime_free(current_csp->local_use_mask);
  7259. }
  7260. current_csp->local_use_mask = NULL;
  7261. current_csp->local_use_mask_size = 0;
  7262. }
  7263. static void
  7264. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7265. {
  7266. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7267. uint32 i;
  7268. for (i = 0; i < ctx->csp_num; i++) {
  7269. if (tmp_csp->local_use_mask) {
  7270. wasm_runtime_free(tmp_csp->local_use_mask);
  7271. tmp_csp->local_use_mask = NULL;
  7272. tmp_csp->local_use_mask_size = 0;
  7273. }
  7274. tmp_csp++;
  7275. }
  7276. }
  7277. static void
  7278. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7279. {
  7280. BranchBlock *current_csp = ctx->frame_csp - 1;
  7281. uint32 byte_offset = index / sizeof(uint8);
  7282. uint32 bit_offset = index % sizeof(uint8);
  7283. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7284. bh_assert(current_csp->local_use_mask);
  7285. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7286. }
  7287. static bool
  7288. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7289. {
  7290. BranchBlock *current_csp = ctx->frame_csp - 1;
  7291. uint32 byte_offset = index / sizeof(uint8);
  7292. uint32 bit_offset = index % sizeof(uint8);
  7293. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7294. bh_assert(current_csp->local_use_mask);
  7295. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7296. ? true
  7297. : false;
  7298. }
  7299. #endif /* end of WASM_ENABLE_GC != 0 */
  7300. static void
  7301. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7302. {
  7303. if (ctx) {
  7304. if (ctx->frame_ref_bottom)
  7305. wasm_runtime_free(ctx->frame_ref_bottom);
  7306. #if WASM_ENABLE_GC != 0
  7307. if (ctx->frame_reftype_map_bottom)
  7308. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7309. #endif
  7310. if (ctx->frame_csp_bottom) {
  7311. #if WASM_ENABLE_FAST_INTERP != 0
  7312. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7313. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7314. ctx->csp_num);
  7315. #endif
  7316. #if WASM_ENABLE_GC != 0
  7317. wasm_loader_clean_all_local_use_masks(ctx);
  7318. #endif
  7319. wasm_runtime_free(ctx->frame_csp_bottom);
  7320. }
  7321. #if WASM_ENABLE_FAST_INTERP != 0
  7322. if (ctx->frame_offset_bottom)
  7323. wasm_runtime_free(ctx->frame_offset_bottom);
  7324. if (ctx->const_buf)
  7325. wasm_runtime_free(ctx->const_buf);
  7326. #endif
  7327. wasm_runtime_free(ctx);
  7328. }
  7329. }
  7330. static WASMLoaderContext *
  7331. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7332. {
  7333. WASMLoaderContext *loader_ctx =
  7334. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7335. if (!loader_ctx)
  7336. return NULL;
  7337. loader_ctx->frame_ref_size = 32;
  7338. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7339. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7340. goto fail;
  7341. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7342. #if WASM_ENABLE_GC != 0
  7343. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7344. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7345. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7346. error_buf_size)))
  7347. goto fail;
  7348. loader_ctx->frame_reftype_map_boundary =
  7349. loader_ctx->frame_reftype_map_bottom + 16;
  7350. #endif
  7351. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7352. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7353. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7354. goto fail;
  7355. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7356. #if WASM_ENABLE_EXCE_HANDLING != 0
  7357. func->exception_handler_count = 0;
  7358. #endif
  7359. #if WASM_ENABLE_FAST_INTERP != 0
  7360. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7361. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7362. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7363. error_buf_size)))
  7364. goto fail;
  7365. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7366. loader_ctx->num_const = 0;
  7367. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7368. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7369. error_buf, error_buf_size)))
  7370. goto fail;
  7371. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7372. set_error_buf(error_buf, error_buf_size,
  7373. "fast interpreter offset overflow");
  7374. goto fail;
  7375. }
  7376. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7377. loader_ctx->max_dynamic_offset =
  7378. func->param_cell_num + func->local_cell_num;
  7379. #endif
  7380. return loader_ctx;
  7381. fail:
  7382. wasm_loader_ctx_destroy(loader_ctx);
  7383. return NULL;
  7384. }
  7385. static bool
  7386. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7387. uint32 error_buf_size)
  7388. {
  7389. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7390. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7391. /* Increase the frame ref stack */
  7392. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7393. ctx->frame_ref_size + 16);
  7394. ctx->frame_ref_size += 16;
  7395. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7396. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7397. }
  7398. #if WASM_ENABLE_GC != 0
  7399. if (wasm_is_type_multi_byte_type(type)
  7400. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7401. /* Increase the frame reftype map stack */
  7402. bh_assert(
  7403. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7404. * sizeof(WASMRefTypeMap))
  7405. == ctx->frame_reftype_map_size);
  7406. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7407. ctx->frame_reftype_map_size
  7408. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7409. ctx->frame_reftype_map =
  7410. ctx->frame_reftype_map_bottom
  7411. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7412. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7413. ctx->frame_reftype_map_boundary =
  7414. ctx->frame_reftype_map_bottom
  7415. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7416. }
  7417. #endif
  7418. return true;
  7419. fail:
  7420. return false;
  7421. }
  7422. static bool
  7423. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7424. uint32 error_buf_size)
  7425. {
  7426. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7427. uint32 i;
  7428. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7429. return false;
  7430. #if WASM_ENABLE_GC != 0
  7431. if (wasm_is_type_multi_byte_type(type)) {
  7432. WASMRefType *ref_type;
  7433. if (!(ref_type =
  7434. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7435. error_buf, error_buf_size))) {
  7436. return false;
  7437. }
  7438. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7439. /* Increase the frame reftype map stack */
  7440. bh_assert((uint32)((ctx->frame_reftype_map
  7441. - ctx->frame_reftype_map_bottom)
  7442. * sizeof(WASMRefTypeMap))
  7443. == ctx->frame_reftype_map_size);
  7444. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7445. ctx->frame_reftype_map_size,
  7446. ctx->frame_reftype_map_size
  7447. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7448. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7449. + ctx->frame_reftype_map_size
  7450. / ((uint32)sizeof(WASMRefTypeMap));
  7451. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7452. ctx->frame_reftype_map_boundary =
  7453. ctx->frame_reftype_map_bottom
  7454. + ctx->frame_reftype_map_size
  7455. / ((uint32)sizeof(WASMRefTypeMap));
  7456. }
  7457. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7458. ctx->frame_reftype_map->ref_type = ref_type;
  7459. ctx->frame_reftype_map++;
  7460. ctx->reftype_map_num++;
  7461. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7462. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7463. }
  7464. #endif
  7465. for (i = 0; i < type_cell_num; i++)
  7466. *ctx->frame_ref++ = type;
  7467. ctx->stack_cell_num += type_cell_num;
  7468. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7469. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7470. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7471. set_error_buf(error_buf, error_buf_size,
  7472. "operand stack depth limit exceeded");
  7473. return false;
  7474. }
  7475. }
  7476. return true;
  7477. #if WASM_ENABLE_GC != 0
  7478. fail:
  7479. return false;
  7480. #endif
  7481. }
  7482. static bool
  7483. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7484. int32 stack_cell_num,
  7485. #if WASM_ENABLE_GC != 0
  7486. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7487. #endif
  7488. uint8 type,
  7489. #if WASM_ENABLE_GC != 0
  7490. WASMRefType *ref_type,
  7491. #endif
  7492. char *error_buf, uint32 error_buf_size)
  7493. {
  7494. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7495. #if WASM_ENABLE_GC != 0
  7496. WASMRefType *frame_reftype = NULL;
  7497. #endif
  7498. if (stack_cell_num < type_cell_num) {
  7499. set_error_buf(error_buf, error_buf_size,
  7500. "type mismatch: expect data but stack was empty");
  7501. return false;
  7502. }
  7503. #if WASM_ENABLE_GC == 0
  7504. for (i = 0; i < type_cell_num; i++) {
  7505. if (*(frame_ref - 1 - i) != type) {
  7506. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7507. "type mismatch: expect ", type2str(type),
  7508. " but got other");
  7509. return false;
  7510. }
  7511. }
  7512. #else
  7513. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7514. bh_assert(reftype_map_num > 0);
  7515. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7516. }
  7517. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7518. ref_type, ctx->module->types,
  7519. ctx->module->type_count)) {
  7520. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7521. "type mismatch: expect ", type2str(type),
  7522. " but got other");
  7523. return false;
  7524. }
  7525. for (i = 0; i < type_cell_num - 1; i++) {
  7526. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7527. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7528. "type mismatch: expect ", type2str(type),
  7529. " but got other");
  7530. return false;
  7531. }
  7532. }
  7533. #endif
  7534. return true;
  7535. }
  7536. static bool
  7537. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7538. uint32 error_buf_size)
  7539. {
  7540. int32 block_stack_cell_num =
  7541. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7542. #if WASM_ENABLE_GC != 0
  7543. int32 reftype_map_num =
  7544. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7545. #endif
  7546. if (block_stack_cell_num > 0) {
  7547. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7548. /* the stack top is a value of any type, return success */
  7549. return true;
  7550. }
  7551. #if WASM_ENABLE_GC != 0
  7552. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7553. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7554. WASMRefType *stack_top_ref_type = NULL;
  7555. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7556. bh_assert(reftype_map_num > 0);
  7557. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7558. }
  7559. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7560. ctx->ref_type_tmp, ctx->module->types,
  7561. ctx->module->type_count)) {
  7562. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7563. uint32 ref_type_struct_size =
  7564. wasm_reftype_struct_size(stack_top_ref_type);
  7565. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7566. stack_top_ref_type, ref_type_struct_size);
  7567. }
  7568. return true;
  7569. }
  7570. }
  7571. #endif
  7572. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7573. #if WASM_ENABLE_GC != 0
  7574. ctx->frame_reftype_map, reftype_map_num,
  7575. #endif
  7576. type,
  7577. #if WASM_ENABLE_GC != 0
  7578. ctx->ref_type_tmp,
  7579. #endif
  7580. error_buf, error_buf_size)) {
  7581. return false;
  7582. }
  7583. return true;
  7584. }
  7585. static bool
  7586. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7587. uint32 error_buf_size)
  7588. {
  7589. BranchBlock *cur_block = ctx->frame_csp - 1;
  7590. int32 available_stack_cell =
  7591. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7592. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7593. /* Directly return success if current block is in stack
  7594. polymorphic state while stack is empty. */
  7595. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7596. return true;
  7597. if (type == VALUE_TYPE_VOID)
  7598. return true;
  7599. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7600. return false;
  7601. bh_assert(available_stack_cell > 0);
  7602. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7603. type = VALUE_TYPE_ANY;
  7604. cell_num_to_pop = 1;
  7605. }
  7606. ctx->frame_ref -= cell_num_to_pop;
  7607. ctx->stack_cell_num -= cell_num_to_pop;
  7608. #if WASM_ENABLE_GC != 0
  7609. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7610. ctx->frame_reftype_map--;
  7611. ctx->reftype_map_num--;
  7612. }
  7613. #endif
  7614. return true;
  7615. }
  7616. #if WASM_ENABLE_GC != 0
  7617. /* Get the stack top element of current block */
  7618. static bool
  7619. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7620. WASMRefType **p_ref_type, char *error_buf,
  7621. uint32 error_buf_size)
  7622. {
  7623. BranchBlock *cur_block = ctx->frame_csp - 1;
  7624. int32 available_stack_cell =
  7625. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7626. if (available_stack_cell <= 0) {
  7627. /* Directly return success if current block is in stack
  7628. polymorphic state while stack is empty. */
  7629. if (cur_block->is_stack_polymorphic) {
  7630. *p_type = VALUE_TYPE_ANY;
  7631. return true;
  7632. }
  7633. else {
  7634. set_error_buf(
  7635. error_buf, error_buf_size,
  7636. "type mismatch: expect data but block stack was empty");
  7637. return false;
  7638. }
  7639. }
  7640. *p_type = *(ctx->frame_ref - 1);
  7641. if (wasm_is_type_multi_byte_type(*p_type)) {
  7642. int32 available_reftype_map =
  7643. (int32)(ctx->reftype_map_num
  7644. - (ctx->frame_csp - 1)->reftype_map_num);
  7645. bh_assert(available_reftype_map > 0);
  7646. (void)available_reftype_map;
  7647. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7648. }
  7649. return true;
  7650. }
  7651. #if WASM_ENABLE_FAST_INTERP != 0
  7652. static bool
  7653. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7654. char *error_buf, uint32 error_buf_size);
  7655. #endif
  7656. /* Check whether the stack top elem is a heap object, and if yes,
  7657. pop and return it */
  7658. static bool
  7659. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7660. WASMRefType *ref_ht_ret, char *error_buf,
  7661. uint32 error_buf_size)
  7662. {
  7663. uint8 type = 0;
  7664. WASMRefType *ref_type = NULL;
  7665. /* Get stack top element */
  7666. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7667. error_buf_size)) {
  7668. return false;
  7669. }
  7670. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7671. /* stack top isn't a ref type */
  7672. && !wasm_is_type_reftype(type)) {
  7673. set_error_buf(error_buf, error_buf_size,
  7674. "type mismatch: expect heap object but got others");
  7675. return false;
  7676. }
  7677. /* POP stack top */
  7678. if (wasm_is_type_multi_byte_type(type)) {
  7679. bh_assert(ref_type);
  7680. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7681. wasm_reftype_struct_size(ref_type));
  7682. }
  7683. #if WASM_ENABLE_FAST_INTERP != 0
  7684. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7685. error_buf_size)) {
  7686. return false;
  7687. }
  7688. #else
  7689. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7690. return false;
  7691. }
  7692. #endif
  7693. if (p_type)
  7694. *p_type = type;
  7695. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7696. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7697. wasm_reftype_struct_size(ref_type));
  7698. }
  7699. return true;
  7700. }
  7701. /* Check whether the stack top elem is subtype of (ref null ht),
  7702. and if yes, pop it and return the converted (ref ht) */
  7703. static bool
  7704. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7705. WASMRefType *ref_ht_ret, char *error_buf,
  7706. uint32 error_buf_size)
  7707. {
  7708. uint8 type = 0;
  7709. WASMRefType ref_type = { 0 };
  7710. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7711. error_buf_size)) {
  7712. return false;
  7713. }
  7714. /* Convert to related (ref ht) and return */
  7715. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7716. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7717. */
  7718. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7719. HEAP_TYPE_ARRAY
  7720. + (type - REF_TYPE_ARRAYREF));
  7721. type = ref_ht_ret->ref_type;
  7722. }
  7723. else if (wasm_is_reftype_htref_nullable(type)
  7724. || wasm_is_reftype_htref_non_nullable(type)) {
  7725. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7726. wasm_reftype_struct_size(&ref_type));
  7727. /* Convert to (ref ht) */
  7728. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7729. ref_ht_ret->ref_ht_common.nullable = false;
  7730. type = ref_ht_ret->ref_type;
  7731. }
  7732. *p_type = type;
  7733. return true;
  7734. }
  7735. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7736. and if yes, pop it and return the type_idx */
  7737. static bool
  7738. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7739. uint32 *p_type_idx, char *error_buf,
  7740. uint32 error_buf_size)
  7741. {
  7742. uint8 type = 0;
  7743. int32 type_idx = -1;
  7744. WASMRefType *ref_type = NULL;
  7745. /* Get stack top element */
  7746. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7747. error_buf_size)) {
  7748. return false;
  7749. }
  7750. if (type != VALUE_TYPE_ANY) {
  7751. /* stack top isn't (ref null $t) */
  7752. if (!((wasm_is_reftype_htref_nullable(type)
  7753. || wasm_is_reftype_htref_non_nullable(type))
  7754. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7755. set_error_buf(error_buf, error_buf_size,
  7756. "type mismatch: expect (ref null $t) but got others");
  7757. return false;
  7758. }
  7759. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7760. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7761. wasm_reftype_struct_size(ref_type));
  7762. }
  7763. /* POP stack top */
  7764. #if WASM_ENABLE_FAST_INTERP != 0
  7765. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7766. error_buf_size)) {
  7767. return false;
  7768. }
  7769. #else
  7770. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7771. return false;
  7772. }
  7773. #endif
  7774. /* Convert to type_idx and return */
  7775. *p_type = type;
  7776. if (type != VALUE_TYPE_ANY)
  7777. *p_type_idx = (uint32)type_idx;
  7778. return true;
  7779. }
  7780. #endif /* WASM_ENABLE_GC != 0 */
  7781. #if WASM_ENABLE_FAST_INTERP == 0
  7782. static bool
  7783. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7784. uint8 type_push, uint8 type_pop, char *error_buf,
  7785. uint32 error_buf_size)
  7786. {
  7787. for (int i = 0; i < pop_cnt; i++) {
  7788. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7789. error_buf_size))
  7790. return false;
  7791. }
  7792. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7793. return false;
  7794. return true;
  7795. }
  7796. #endif
  7797. static bool
  7798. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7799. BlockType block_type, uint8 *start_addr,
  7800. char *error_buf, uint32 error_buf_size)
  7801. {
  7802. CHECK_CSP_PUSH();
  7803. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7804. ctx->frame_csp->label_type = label_type;
  7805. ctx->frame_csp->block_type = block_type;
  7806. ctx->frame_csp->start_addr = start_addr;
  7807. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7808. #if WASM_ENABLE_GC != 0
  7809. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7810. #endif
  7811. #if WASM_ENABLE_FAST_INTERP != 0
  7812. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7813. ctx->frame_csp->patch_list = NULL;
  7814. #endif
  7815. ctx->frame_csp++;
  7816. ctx->csp_num++;
  7817. if (ctx->csp_num > ctx->max_csp_num) {
  7818. ctx->max_csp_num = ctx->csp_num;
  7819. if (ctx->max_csp_num > UINT16_MAX) {
  7820. set_error_buf(error_buf, error_buf_size,
  7821. "label stack depth limit exceeded");
  7822. return false;
  7823. }
  7824. }
  7825. return true;
  7826. fail:
  7827. return false;
  7828. }
  7829. static bool
  7830. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7831. uint32 error_buf_size)
  7832. {
  7833. CHECK_CSP_POP();
  7834. #if WASM_ENABLE_FAST_INTERP != 0
  7835. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7836. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7837. #endif
  7838. ctx->frame_csp--;
  7839. ctx->csp_num--;
  7840. return true;
  7841. fail:
  7842. return false;
  7843. }
  7844. #if WASM_ENABLE_FAST_INTERP != 0
  7845. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7846. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7847. #define emit_label(opcode) \
  7848. do { \
  7849. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7850. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7851. } while (0)
  7852. #define skip_label() \
  7853. do { \
  7854. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7855. LOG_OP("\ndelete last op\n"); \
  7856. } while (0)
  7857. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7858. #if UINTPTR_MAX == UINT64_MAX
  7859. #define emit_label(opcode) \
  7860. do { \
  7861. int32 offset = \
  7862. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7863. /* emit int32 relative offset in 64-bit target */ \
  7864. wasm_loader_emit_uint32(loader_ctx, offset); \
  7865. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7866. } while (0)
  7867. #else
  7868. #define emit_label(opcode) \
  7869. do { \
  7870. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7871. /* emit uint32 label address in 32-bit target */ \
  7872. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7873. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7874. } while (0)
  7875. #endif
  7876. #define skip_label() \
  7877. do { \
  7878. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7879. LOG_OP("\ndelete last op\n"); \
  7880. } while (0)
  7881. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7882. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7883. #define emit_label(opcode) \
  7884. do { \
  7885. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7886. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7887. } while (0)
  7888. #define skip_label() \
  7889. do { \
  7890. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7891. LOG_OP("\ndelete last op\n"); \
  7892. } while (0)
  7893. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7894. #define emit_empty_label_addr_and_frame_ip(type) \
  7895. do { \
  7896. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7897. loader_ctx->p_code_compiled, error_buf, \
  7898. error_buf_size)) \
  7899. goto fail; \
  7900. /* label address, to be patched */ \
  7901. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7902. } while (0)
  7903. #define emit_br_info(frame_csp, is_br) \
  7904. do { \
  7905. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7906. error_buf_size)) \
  7907. goto fail; \
  7908. } while (0)
  7909. #define LAST_OP_OUTPUT_I32() \
  7910. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7911. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7912. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7913. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7914. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7915. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7916. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7917. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7918. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7919. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7920. || (last_op == EXT_OP_COPY_STACK_TOP)
  7921. #define LAST_OP_OUTPUT_I64() \
  7922. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7923. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7924. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7925. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7926. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7927. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7928. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7929. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7930. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7931. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7932. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7933. #define GET_CONST_OFFSET(type, val) \
  7934. do { \
  7935. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7936. &operand_offset, error_buf, \
  7937. error_buf_size))) \
  7938. goto fail; \
  7939. } while (0)
  7940. #define GET_CONST_F32_OFFSET(type, fval) \
  7941. do { \
  7942. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7943. &operand_offset, error_buf, \
  7944. error_buf_size))) \
  7945. goto fail; \
  7946. } while (0)
  7947. #define GET_CONST_F64_OFFSET(type, fval) \
  7948. do { \
  7949. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7950. &operand_offset, error_buf, \
  7951. error_buf_size))) \
  7952. goto fail; \
  7953. } while (0)
  7954. #define emit_operand(ctx, offset) \
  7955. do { \
  7956. wasm_loader_emit_int16(ctx, offset); \
  7957. LOG_OP("%d\t", offset); \
  7958. } while (0)
  7959. #define emit_byte(ctx, byte) \
  7960. do { \
  7961. wasm_loader_emit_uint8(ctx, byte); \
  7962. LOG_OP("%d\t", byte); \
  7963. } while (0)
  7964. #define emit_uint32(ctx, value) \
  7965. do { \
  7966. wasm_loader_emit_uint32(ctx, value); \
  7967. LOG_OP("%d\t", value); \
  7968. } while (0)
  7969. #define emit_uint64(ctx, value) \
  7970. do { \
  7971. wasm_loader_emit_const(ctx, &value, false); \
  7972. LOG_OP("%lld\t", value); \
  7973. } while (0)
  7974. #define emit_float32(ctx, value) \
  7975. do { \
  7976. wasm_loader_emit_const(ctx, &value, true); \
  7977. LOG_OP("%f\t", value); \
  7978. } while (0)
  7979. #define emit_float64(ctx, value) \
  7980. do { \
  7981. wasm_loader_emit_const(ctx, &value, false); \
  7982. LOG_OP("%f\t", value); \
  7983. } while (0)
  7984. static bool
  7985. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7986. {
  7987. if (!(ctx->p_code_compiled =
  7988. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7989. return false;
  7990. ctx->p_code_compiled_end =
  7991. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7992. /* clean up frame ref */
  7993. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7994. ctx->frame_ref = ctx->frame_ref_bottom;
  7995. ctx->stack_cell_num = 0;
  7996. #if WASM_ENABLE_GC != 0
  7997. /* clean up reftype map */
  7998. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7999. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8000. ctx->reftype_map_num = 0;
  8001. #endif
  8002. /* clean up frame csp */
  8003. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8004. ctx->frame_csp = ctx->frame_csp_bottom;
  8005. ctx->csp_num = 0;
  8006. ctx->max_csp_num = 0;
  8007. /* clean up frame offset */
  8008. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8009. ctx->frame_offset = ctx->frame_offset_bottom;
  8010. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8011. /* init preserved local offsets */
  8012. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8013. /* const buf is reserved */
  8014. return true;
  8015. }
  8016. static void
  8017. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8018. {
  8019. ctx->code_compiled_size += size;
  8020. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8021. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8022. }
  8023. }
  8024. static void
  8025. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8026. {
  8027. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8028. if (ctx->p_code_compiled) {
  8029. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8030. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8031. #endif
  8032. bh_memcpy_s(ctx->p_code_compiled,
  8033. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8034. value, size);
  8035. ctx->p_code_compiled += size;
  8036. }
  8037. else {
  8038. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8039. bh_assert((ctx->code_compiled_size & 1) == 0);
  8040. #endif
  8041. increase_compiled_code_space(ctx, size);
  8042. }
  8043. }
  8044. static void
  8045. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8046. {
  8047. if (ctx->p_code_compiled) {
  8048. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8049. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8050. #endif
  8051. STORE_U32(ctx->p_code_compiled, value);
  8052. ctx->p_code_compiled += sizeof(uint32);
  8053. }
  8054. else {
  8055. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8056. bh_assert((ctx->code_compiled_size & 1) == 0);
  8057. #endif
  8058. increase_compiled_code_space(ctx, sizeof(uint32));
  8059. }
  8060. }
  8061. static void
  8062. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8063. {
  8064. if (ctx->p_code_compiled) {
  8065. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8066. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8067. #endif
  8068. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8069. ctx->p_code_compiled += sizeof(int16);
  8070. }
  8071. else {
  8072. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8073. bh_assert((ctx->code_compiled_size & 1) == 0);
  8074. #endif
  8075. increase_compiled_code_space(ctx, sizeof(uint16));
  8076. }
  8077. }
  8078. static void
  8079. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8080. {
  8081. if (ctx->p_code_compiled) {
  8082. *(ctx->p_code_compiled) = value;
  8083. ctx->p_code_compiled += sizeof(uint8);
  8084. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8085. ctx->p_code_compiled++;
  8086. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8087. #endif
  8088. }
  8089. else {
  8090. increase_compiled_code_space(ctx, sizeof(uint8));
  8091. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8092. increase_compiled_code_space(ctx, sizeof(uint8));
  8093. bh_assert((ctx->code_compiled_size & 1) == 0);
  8094. #endif
  8095. }
  8096. }
  8097. static void
  8098. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8099. {
  8100. if (ctx->p_code_compiled) {
  8101. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8102. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8103. #endif
  8104. STORE_PTR(ctx->p_code_compiled, value);
  8105. ctx->p_code_compiled += sizeof(void *);
  8106. }
  8107. else {
  8108. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8109. bh_assert((ctx->code_compiled_size & 1) == 0);
  8110. #endif
  8111. increase_compiled_code_space(ctx, sizeof(void *));
  8112. }
  8113. }
  8114. static void
  8115. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8116. {
  8117. if (ctx->p_code_compiled) {
  8118. ctx->p_code_compiled -= size;
  8119. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8120. if (size == sizeof(uint8)) {
  8121. ctx->p_code_compiled--;
  8122. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8123. }
  8124. #endif
  8125. }
  8126. else {
  8127. ctx->code_compiled_size -= size;
  8128. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8129. if (size == sizeof(uint8)) {
  8130. ctx->code_compiled_size--;
  8131. bh_assert((ctx->code_compiled_size & 1) == 0);
  8132. }
  8133. #endif
  8134. }
  8135. }
  8136. static bool
  8137. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8138. uint32 local_index, uint32 local_type,
  8139. bool *preserved, char *error_buf,
  8140. uint32 error_buf_size)
  8141. {
  8142. uint32 i = 0;
  8143. int16 preserved_offset = (int16)local_index;
  8144. *preserved = false;
  8145. while (i < loader_ctx->stack_cell_num) {
  8146. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8147. /* move previous local into dynamic space before a set/tee_local opcode
  8148. */
  8149. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8150. if (!(*preserved)) {
  8151. *preserved = true;
  8152. skip_label();
  8153. preserved_offset = loader_ctx->preserved_local_offset;
  8154. if (loader_ctx->p_code_compiled) {
  8155. bh_assert(preserved_offset != (int16)local_index);
  8156. }
  8157. if (is_32bit_type(local_type)) {
  8158. /* Only increase preserve offset in the second traversal */
  8159. if (loader_ctx->p_code_compiled)
  8160. loader_ctx->preserved_local_offset++;
  8161. emit_label(EXT_OP_COPY_STACK_TOP);
  8162. }
  8163. else {
  8164. if (loader_ctx->p_code_compiled)
  8165. loader_ctx->preserved_local_offset += 2;
  8166. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8167. }
  8168. emit_operand(loader_ctx, local_index);
  8169. emit_operand(loader_ctx, preserved_offset);
  8170. emit_label(opcode);
  8171. }
  8172. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8173. }
  8174. if (is_32bit_type(cur_type))
  8175. i++;
  8176. else
  8177. i += 2;
  8178. }
  8179. (void)error_buf;
  8180. (void)error_buf_size;
  8181. return true;
  8182. }
  8183. static bool
  8184. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8185. char *error_buf, uint32 error_buf_size)
  8186. {
  8187. uint32 i = 0;
  8188. bool preserve_local;
  8189. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8190. blocks will not influence the value of these locals */
  8191. while (i < loader_ctx->stack_cell_num) {
  8192. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8193. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8194. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8195. && (cur_offset >= 0)) {
  8196. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8197. cur_type, &preserve_local,
  8198. error_buf, error_buf_size)))
  8199. return false;
  8200. }
  8201. if (is_32bit_type(cur_type)) {
  8202. i++;
  8203. }
  8204. else {
  8205. i += 2;
  8206. }
  8207. }
  8208. return true;
  8209. }
  8210. static bool
  8211. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8212. uint8 *p_code_compiled, char *error_buf,
  8213. uint32 error_buf_size)
  8214. {
  8215. BranchBlockPatch *patch =
  8216. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8217. if (!patch) {
  8218. return false;
  8219. }
  8220. patch->patch_type = patch_type;
  8221. patch->code_compiled = p_code_compiled;
  8222. if (!frame_csp->patch_list) {
  8223. frame_csp->patch_list = patch;
  8224. patch->next = NULL;
  8225. }
  8226. else {
  8227. patch->next = frame_csp->patch_list;
  8228. frame_csp->patch_list = patch;
  8229. }
  8230. return true;
  8231. }
  8232. static void
  8233. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8234. {
  8235. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8236. BranchBlockPatch *node = frame_csp->patch_list;
  8237. BranchBlockPatch *node_prev = NULL, *node_next;
  8238. if (!ctx->p_code_compiled)
  8239. return;
  8240. while (node) {
  8241. node_next = node->next;
  8242. if (node->patch_type == patch_type) {
  8243. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8244. if (node_prev == NULL) {
  8245. frame_csp->patch_list = node_next;
  8246. }
  8247. else {
  8248. node_prev->next = node_next;
  8249. }
  8250. wasm_runtime_free(node);
  8251. }
  8252. else {
  8253. node_prev = node;
  8254. }
  8255. node = node_next;
  8256. }
  8257. }
  8258. static bool
  8259. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8260. bool is_br, char *error_buf, uint32 error_buf_size)
  8261. {
  8262. /* br info layout:
  8263. * a) arity of target block
  8264. * b) total cell num of arity values
  8265. * c) each arity value's cell num
  8266. * d) each arity value's src frame offset
  8267. * e) each arity values's dst dynamic offset
  8268. * f) branch target address
  8269. *
  8270. * Note: b-e are omitted when arity is 0 so that
  8271. * interpreter can recover the br info quickly.
  8272. */
  8273. BlockType *block_type = &frame_csp->block_type;
  8274. uint8 *types = NULL, cell;
  8275. #if WASM_ENABLE_GC != 0
  8276. WASMRefTypeMap *reftype_maps;
  8277. uint32 reftype_map_count;
  8278. #endif
  8279. uint32 arity = 0;
  8280. int32 i;
  8281. int16 *frame_offset = ctx->frame_offset;
  8282. uint16 dynamic_offset;
  8283. /* Note: loop's arity is different from if and block. loop's arity is
  8284. * its parameter count while if and block arity is result count.
  8285. */
  8286. #if WASM_ENABLE_GC == 0
  8287. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8288. arity = block_type_get_param_types(block_type, &types);
  8289. else
  8290. arity = block_type_get_result_types(block_type, &types);
  8291. #else
  8292. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8293. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8294. &reftype_map_count);
  8295. else
  8296. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8297. &reftype_map_count);
  8298. #endif
  8299. /* Part a */
  8300. emit_uint32(ctx, arity);
  8301. if (arity) {
  8302. /* Part b */
  8303. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8304. /* Part c */
  8305. for (i = (int32)arity - 1; i >= 0; i--) {
  8306. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8307. emit_byte(ctx, cell);
  8308. }
  8309. /* Part d */
  8310. for (i = (int32)arity - 1; i >= 0; i--) {
  8311. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8312. frame_offset -= cell;
  8313. emit_operand(ctx, *(int16 *)(frame_offset));
  8314. }
  8315. /* Part e */
  8316. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8317. /* Use start_dynamic_offset which was set in
  8318. copy_params_to_dynamic_space */
  8319. dynamic_offset = frame_csp->start_dynamic_offset
  8320. + wasm_get_cell_num(types, arity);
  8321. else
  8322. dynamic_offset =
  8323. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8324. if (is_br)
  8325. ctx->dynamic_offset = dynamic_offset;
  8326. for (i = (int32)arity - 1; i >= 0; i--) {
  8327. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8328. dynamic_offset -= cell;
  8329. emit_operand(ctx, dynamic_offset);
  8330. }
  8331. }
  8332. /* Part f */
  8333. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8334. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8335. }
  8336. else {
  8337. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8338. error_buf, error_buf_size))
  8339. return false;
  8340. /* label address, to be patched */
  8341. wasm_loader_emit_ptr(ctx, NULL);
  8342. }
  8343. return true;
  8344. }
  8345. static bool
  8346. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8347. bool disable_emit, int16 operand_offset,
  8348. char *error_buf, uint32 error_buf_size)
  8349. {
  8350. if (type == VALUE_TYPE_VOID)
  8351. return true;
  8352. /* only check memory overflow in first traverse */
  8353. if (ctx->p_code_compiled == NULL) {
  8354. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8355. return false;
  8356. }
  8357. if (disable_emit)
  8358. *(ctx->frame_offset)++ = operand_offset;
  8359. else {
  8360. emit_operand(ctx, ctx->dynamic_offset);
  8361. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8362. ctx->dynamic_offset++;
  8363. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8364. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8365. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8366. goto fail;
  8367. }
  8368. }
  8369. }
  8370. if (is_32bit_type(type))
  8371. return true;
  8372. if (ctx->p_code_compiled == NULL) {
  8373. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8374. return false;
  8375. }
  8376. ctx->frame_offset++;
  8377. if (!disable_emit) {
  8378. ctx->dynamic_offset++;
  8379. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8380. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8381. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8382. goto fail;
  8383. }
  8384. }
  8385. }
  8386. return true;
  8387. fail:
  8388. set_error_buf(error_buf, error_buf_size,
  8389. "fast interpreter offset overflow");
  8390. return false;
  8391. }
  8392. /* This function should be in front of wasm_loader_pop_frame_ref
  8393. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8394. will be modified by wasm_loader_pop_frame_ref */
  8395. static bool
  8396. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8397. char *error_buf, uint32 error_buf_size)
  8398. {
  8399. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8400. then current block is the function block */
  8401. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8402. BranchBlock *cur_block = ctx->frame_csp - depth;
  8403. int32 available_stack_cell =
  8404. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8405. /* Directly return success if current block is in stack
  8406. * polymorphic state while stack is empty. */
  8407. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8408. return true;
  8409. if (type == VALUE_TYPE_VOID)
  8410. return true;
  8411. if (is_32bit_type(type)) {
  8412. /* Check the offset stack bottom to ensure the frame offset
  8413. stack will not go underflow. But we don't thrown error
  8414. and return true here, because the error msg should be
  8415. given in wasm_loader_pop_frame_ref */
  8416. if (!check_offset_pop(ctx, 1))
  8417. return true;
  8418. ctx->frame_offset -= 1;
  8419. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8420. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8421. ctx->dynamic_offset -= 1;
  8422. }
  8423. else {
  8424. if (!check_offset_pop(ctx, 2))
  8425. return true;
  8426. ctx->frame_offset -= 2;
  8427. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8428. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8429. ctx->dynamic_offset -= 2;
  8430. }
  8431. emit_operand(ctx, *(ctx->frame_offset));
  8432. (void)error_buf;
  8433. (void)error_buf_size;
  8434. return true;
  8435. }
  8436. static bool
  8437. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8438. bool disable_emit, int16 operand_offset,
  8439. char *error_buf, uint32 error_buf_size)
  8440. {
  8441. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8442. error_buf, error_buf_size)))
  8443. return false;
  8444. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8445. return false;
  8446. return true;
  8447. }
  8448. static bool
  8449. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8450. char *error_buf, uint32 error_buf_size)
  8451. {
  8452. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8453. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8454. return false;
  8455. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8456. return false;
  8457. return true;
  8458. }
  8459. static bool
  8460. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8461. uint8 type_push, uint8 type_pop,
  8462. bool disable_emit, int16 operand_offset,
  8463. char *error_buf, uint32 error_buf_size)
  8464. {
  8465. uint8 i;
  8466. for (i = 0; i < pop_cnt; i++) {
  8467. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8468. error_buf_size))
  8469. return false;
  8470. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8471. error_buf_size))
  8472. return false;
  8473. }
  8474. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8475. operand_offset, error_buf,
  8476. error_buf_size))
  8477. return false;
  8478. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8479. return false;
  8480. return true;
  8481. }
  8482. static bool
  8483. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8484. int16 *offset, char *error_buf,
  8485. uint32 error_buf_size)
  8486. {
  8487. int8 bytes_to_increase;
  8488. int16 operand_offset = 0;
  8489. Const *c;
  8490. /* Search existing constant */
  8491. for (c = (Const *)ctx->const_buf;
  8492. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8493. /* TODO: handle v128 type? */
  8494. if ((type == c->value_type)
  8495. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8496. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8497. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8498. || (type == VALUE_TYPE_FUNCREF
  8499. && *(int32 *)value == c->value.i32)
  8500. || (type == VALUE_TYPE_EXTERNREF
  8501. && *(int32 *)value == c->value.i32)
  8502. #endif
  8503. || (type == VALUE_TYPE_F64
  8504. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8505. || (type == VALUE_TYPE_F32
  8506. && (0
  8507. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8508. operand_offset = c->slot_index;
  8509. break;
  8510. }
  8511. if (is_32bit_type(c->value_type))
  8512. operand_offset += 1;
  8513. else
  8514. operand_offset += 2;
  8515. }
  8516. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8517. /* New constant, append to the const buffer */
  8518. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8519. bytes_to_increase = 2;
  8520. }
  8521. else {
  8522. bytes_to_increase = 1;
  8523. }
  8524. /* The max cell num of const buffer is 32768 since the valid index range
  8525. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8526. * full */
  8527. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8528. *offset = 0;
  8529. return true;
  8530. }
  8531. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8532. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8533. ctx->const_buf_size + 4 * sizeof(Const));
  8534. ctx->const_buf_size += 4 * sizeof(Const);
  8535. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8536. }
  8537. c->value_type = type;
  8538. switch (type) {
  8539. case VALUE_TYPE_F64:
  8540. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8541. sizeof(float64));
  8542. ctx->const_cell_num += 2;
  8543. /* The const buf will be reversed, we use the second cell */
  8544. /* of the i64/f64 const so the final offset is correct */
  8545. operand_offset++;
  8546. break;
  8547. case VALUE_TYPE_I64:
  8548. c->value.i64 = *(int64 *)value;
  8549. ctx->const_cell_num += 2;
  8550. operand_offset++;
  8551. break;
  8552. case VALUE_TYPE_F32:
  8553. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8554. sizeof(float32));
  8555. ctx->const_cell_num++;
  8556. break;
  8557. case VALUE_TYPE_I32:
  8558. c->value.i32 = *(int32 *)value;
  8559. ctx->const_cell_num++;
  8560. break;
  8561. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8562. case VALUE_TYPE_EXTERNREF:
  8563. case VALUE_TYPE_FUNCREF:
  8564. c->value.i32 = *(int32 *)value;
  8565. ctx->const_cell_num++;
  8566. break;
  8567. #endif
  8568. default:
  8569. break;
  8570. }
  8571. c->slot_index = operand_offset;
  8572. ctx->num_const++;
  8573. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8574. (int64)c->value.i64);
  8575. }
  8576. /* use negative index for const */
  8577. operand_offset = -(operand_offset + 1);
  8578. *offset = operand_offset;
  8579. return true;
  8580. fail:
  8581. return false;
  8582. }
  8583. /*
  8584. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8585. -- Mostly used for the binary / compare operation
  8586. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8587. -- Mostly used in block / control instructions
  8588. The POP will always emit the offset on the top of the frame_offset stack
  8589. PUSH can be used in two ways:
  8590. 1. directly PUSH:
  8591. PUSH_XXX();
  8592. will allocate a dynamic space and emit
  8593. 2. silent PUSH:
  8594. operand_offset = xxx; disable_emit = true;
  8595. PUSH_XXX();
  8596. only push the frame_offset stack, no emit
  8597. */
  8598. #define TEMPLATE_PUSH(Type) \
  8599. do { \
  8600. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8601. disable_emit, operand_offset, \
  8602. error_buf, error_buf_size)) \
  8603. goto fail; \
  8604. } while (0)
  8605. #define TEMPLATE_PUSH_REF(Type) \
  8606. do { \
  8607. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8608. operand_offset, error_buf, \
  8609. error_buf_size)) \
  8610. goto fail; \
  8611. } while (0)
  8612. #define TEMPLATE_POP(Type) \
  8613. do { \
  8614. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8615. error_buf, error_buf_size)) \
  8616. goto fail; \
  8617. } while (0)
  8618. #define TEMPLATE_POP_REF(Type) \
  8619. do { \
  8620. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8621. error_buf_size)) \
  8622. goto fail; \
  8623. } while (0)
  8624. #define PUSH_OFFSET_TYPE(type) \
  8625. do { \
  8626. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8627. operand_offset, error_buf, \
  8628. error_buf_size))) \
  8629. goto fail; \
  8630. } while (0)
  8631. #define POP_OFFSET_TYPE(type) \
  8632. do { \
  8633. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8634. error_buf_size))) \
  8635. goto fail; \
  8636. } while (0)
  8637. #define POP_AND_PUSH(type_pop, type_push) \
  8638. do { \
  8639. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8640. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8641. operand_offset, error_buf, error_buf_size))) \
  8642. goto fail; \
  8643. } while (0)
  8644. /* type of POPs should be the same */
  8645. #define POP2_AND_PUSH(type_pop, type_push) \
  8646. do { \
  8647. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8648. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8649. operand_offset, error_buf, error_buf_size))) \
  8650. goto fail; \
  8651. } while (0)
  8652. #else /* WASM_ENABLE_FAST_INTERP */
  8653. #define TEMPLATE_PUSH(Type) \
  8654. do { \
  8655. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8656. error_buf, error_buf_size))) \
  8657. goto fail; \
  8658. } while (0)
  8659. #define TEMPLATE_PUSH_REF(Type) \
  8660. do { \
  8661. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8662. error_buf_size))) \
  8663. goto fail; \
  8664. } while (0)
  8665. #define TEMPLATE_POP(Type) \
  8666. do { \
  8667. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8668. error_buf, error_buf_size))) \
  8669. goto fail; \
  8670. } while (0)
  8671. #define TEMPLATE_POP_REF(Type) \
  8672. do { \
  8673. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8674. error_buf_size))) \
  8675. goto fail; \
  8676. } while (0)
  8677. #define POP_AND_PUSH(type_pop, type_push) \
  8678. do { \
  8679. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8680. type_pop, error_buf, \
  8681. error_buf_size))) \
  8682. goto fail; \
  8683. } while (0)
  8684. /* type of POPs should be the same */
  8685. #define POP2_AND_PUSH(type_pop, type_push) \
  8686. do { \
  8687. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8688. type_pop, error_buf, \
  8689. error_buf_size))) \
  8690. goto fail; \
  8691. } while (0)
  8692. #endif /* WASM_ENABLE_FAST_INTERP */
  8693. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8694. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8695. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8696. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8697. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8698. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8699. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8700. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8701. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8702. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8703. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8704. #define POP_I32() TEMPLATE_POP(I32)
  8705. #define POP_F32() TEMPLATE_POP(F32)
  8706. #define POP_I64() TEMPLATE_POP(I64)
  8707. #define POP_F64() TEMPLATE_POP(F64)
  8708. #define POP_V128() TEMPLATE_POP(V128)
  8709. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8710. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8711. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8712. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8713. #if WASM_ENABLE_FAST_INTERP != 0
  8714. static bool
  8715. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8716. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8717. {
  8718. int16 operand_offset = 0;
  8719. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8720. : loader_ctx->frame_csp;
  8721. BlockType *block_type = &block->block_type;
  8722. uint8 *return_types = NULL;
  8723. #if WASM_ENABLE_GC != 0
  8724. WASMRefTypeMap *reftype_maps = NULL;
  8725. uint32 reftype_map_count;
  8726. #endif
  8727. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8728. int32 i = 0;
  8729. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8730. *frame_offset_org = NULL;
  8731. #if WASM_ENABLE_GC == 0
  8732. return_count = block_type_get_result_types(block_type, &return_types);
  8733. #else
  8734. return_count = block_type_get_result_types(
  8735. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8736. #endif
  8737. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8738. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8739. if (return_count == 1) {
  8740. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8741. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8742. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8743. /* insert op_copy before else opcode */
  8744. if (opcode == WASM_OP_ELSE)
  8745. skip_label();
  8746. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8747. : EXT_OP_COPY_STACK_TOP_I64);
  8748. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8749. emit_operand(loader_ctx, block->dynamic_offset);
  8750. if (opcode == WASM_OP_ELSE) {
  8751. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8752. }
  8753. else {
  8754. loader_ctx->frame_offset -= cell;
  8755. loader_ctx->dynamic_offset = block->dynamic_offset;
  8756. PUSH_OFFSET_TYPE(return_types[0]);
  8757. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8758. }
  8759. if (opcode == WASM_OP_ELSE)
  8760. emit_label(opcode);
  8761. }
  8762. return true;
  8763. }
  8764. /* Copy stack top values to block's results which are in dynamic space.
  8765. * The instruction format:
  8766. * Part a: values count
  8767. * Part b: all values total cell num
  8768. * Part c: each value's cell_num, src offset and dst offset
  8769. * Part d: each value's src offset and dst offset
  8770. * Part e: each value's dst offset
  8771. */
  8772. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8773. dynamic_offset = dynamic_offset_org =
  8774. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8775. /* First traversal to get the count of values needed to be copied. */
  8776. for (i = (int32)return_count - 1; i >= 0; i--) {
  8777. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8778. frame_offset -= cells;
  8779. dynamic_offset -= cells;
  8780. if (dynamic_offset != *frame_offset) {
  8781. value_count++;
  8782. total_cel_num += cells;
  8783. }
  8784. }
  8785. if (value_count) {
  8786. uint32 j = 0;
  8787. uint8 *emit_data = NULL, *cells = NULL;
  8788. int16 *src_offsets = NULL;
  8789. uint16 *dst_offsets = NULL;
  8790. uint64 size =
  8791. (uint64)value_count
  8792. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8793. /* Allocate memory for the emit data */
  8794. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8795. return false;
  8796. cells = emit_data;
  8797. src_offsets = (int16 *)(cells + value_count);
  8798. dst_offsets = (uint16 *)(src_offsets + value_count);
  8799. /* insert op_copy before else opcode */
  8800. if (opcode == WASM_OP_ELSE)
  8801. skip_label();
  8802. emit_label(EXT_OP_COPY_STACK_VALUES);
  8803. /* Part a) */
  8804. emit_uint32(loader_ctx, value_count);
  8805. /* Part b) */
  8806. emit_uint32(loader_ctx, total_cel_num);
  8807. /* Second traversal to get each value's cell num, src offset and dst
  8808. * offset. */
  8809. frame_offset = frame_offset_org;
  8810. dynamic_offset = dynamic_offset_org;
  8811. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8812. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8813. frame_offset -= cell;
  8814. dynamic_offset -= cell;
  8815. if (dynamic_offset != *frame_offset) {
  8816. /* cell num */
  8817. cells[j] = cell;
  8818. /* src offset */
  8819. src_offsets[j] = *frame_offset;
  8820. /* dst offset */
  8821. dst_offsets[j] = dynamic_offset;
  8822. j++;
  8823. }
  8824. if (opcode == WASM_OP_ELSE) {
  8825. *frame_offset = dynamic_offset;
  8826. }
  8827. else {
  8828. loader_ctx->frame_offset = frame_offset;
  8829. loader_ctx->dynamic_offset = dynamic_offset;
  8830. PUSH_OFFSET_TYPE(return_types[i]);
  8831. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8832. loader_ctx->frame_offset = frame_offset_org;
  8833. loader_ctx->dynamic_offset = dynamic_offset_org;
  8834. }
  8835. }
  8836. bh_assert(j == value_count);
  8837. /* Emit the cells, src_offsets and dst_offsets */
  8838. for (j = 0; j < value_count; j++)
  8839. emit_byte(loader_ctx, cells[j]);
  8840. for (j = 0; j < value_count; j++)
  8841. emit_operand(loader_ctx, src_offsets[j]);
  8842. for (j = 0; j < value_count; j++)
  8843. emit_operand(loader_ctx, dst_offsets[j]);
  8844. if (opcode == WASM_OP_ELSE)
  8845. emit_label(opcode);
  8846. wasm_runtime_free(emit_data);
  8847. }
  8848. return true;
  8849. fail:
  8850. return false;
  8851. }
  8852. #endif /* WASM_ENABLE_FAST_INTERP */
  8853. #define RESERVE_BLOCK_RET() \
  8854. do { \
  8855. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8856. error_buf_size)) \
  8857. goto fail; \
  8858. } while (0)
  8859. #define PUSH_TYPE(type) \
  8860. do { \
  8861. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8862. error_buf_size))) \
  8863. goto fail; \
  8864. } while (0)
  8865. #define POP_TYPE(type) \
  8866. do { \
  8867. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8868. error_buf_size))) \
  8869. goto fail; \
  8870. } while (0)
  8871. #if WASM_ENABLE_GC == 0
  8872. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8873. do { \
  8874. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8875. _start_addr, error_buf, \
  8876. error_buf_size)) \
  8877. goto fail; \
  8878. } while (0)
  8879. #define POP_CSP() \
  8880. do { \
  8881. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8882. goto fail; \
  8883. } while (0)
  8884. #else
  8885. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8886. do { \
  8887. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8888. _start_addr, error_buf, \
  8889. error_buf_size)) \
  8890. goto fail; \
  8891. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8892. error_buf, error_buf_size)) { \
  8893. goto fail; \
  8894. } \
  8895. } while (0)
  8896. #define POP_CSP() \
  8897. do { \
  8898. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8899. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8900. goto fail; \
  8901. } while (0)
  8902. #endif /* end of WASM_ENABLE_GC == 0 */
  8903. #if WASM_ENABLE_GC == 0
  8904. #define GET_LOCAL_REFTYPE() (void)0
  8905. #else
  8906. #define GET_LOCAL_REFTYPE() \
  8907. do { \
  8908. if (wasm_is_type_multi_byte_type(local_type)) { \
  8909. WASMRefType *_ref_type; \
  8910. if (local_idx < param_count) \
  8911. _ref_type = wasm_reftype_map_find( \
  8912. param_reftype_maps, param_reftype_map_count, local_idx); \
  8913. else \
  8914. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8915. local_reftype_map_count, \
  8916. local_idx - param_count); \
  8917. bh_assert(_ref_type); \
  8918. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8919. wasm_reftype_struct_size(_ref_type)); \
  8920. } \
  8921. } while (0)
  8922. #endif /* end of WASM_ENABLE_GC == 0 */
  8923. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8924. do { \
  8925. read_leb_uint32(p, p_end, local_idx); \
  8926. if (local_idx >= param_count + local_count) { \
  8927. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8928. goto fail; \
  8929. } \
  8930. local_type = local_idx < param_count \
  8931. ? param_types[local_idx] \
  8932. : local_types[local_idx - param_count]; \
  8933. local_offset = local_offsets[local_idx]; \
  8934. GET_LOCAL_REFTYPE(); \
  8935. } while (0)
  8936. static bool
  8937. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8938. {
  8939. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8940. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8941. return false;
  8942. }
  8943. return true;
  8944. }
  8945. #define CHECK_MEMORY() \
  8946. do { \
  8947. if (!check_memory(module, error_buf, error_buf_size)) \
  8948. goto fail; \
  8949. } while (0)
  8950. static bool
  8951. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8952. uint32 error_buf_size)
  8953. {
  8954. uint8 mem_access_aligns[] = {
  8955. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8956. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8957. };
  8958. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8959. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8960. set_error_buf(error_buf, error_buf_size,
  8961. "alignment must not be larger than natural");
  8962. return false;
  8963. }
  8964. return true;
  8965. }
  8966. #if WASM_ENABLE_SIMD != 0
  8967. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8968. static bool
  8969. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8970. uint32 error_buf_size)
  8971. {
  8972. uint8 mem_access_aligns[] = {
  8973. 4, /* load */
  8974. 3, 3, 3, 3, 3, 3, /* load and extend */
  8975. 0, 1, 2, 3, /* load and splat */
  8976. 4, /* store */
  8977. };
  8978. uint8 mem_access_aligns_load_lane[] = {
  8979. 0, 1, 2, 3, /* load lane */
  8980. 0, 1, 2, 3, /* store lane */
  8981. 2, 3 /* store zero */
  8982. };
  8983. if (!((opcode <= SIMD_v128_store)
  8984. || (SIMD_v128_load8_lane <= opcode
  8985. && opcode <= SIMD_v128_load64_zero))) {
  8986. set_error_buf(error_buf, error_buf_size,
  8987. "the opcode doesn't include memarg");
  8988. return false;
  8989. }
  8990. if ((opcode <= SIMD_v128_store
  8991. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8992. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8993. && align > mem_access_aligns_load_lane[opcode
  8994. - SIMD_v128_load8_lane])) {
  8995. set_error_buf(error_buf, error_buf_size,
  8996. "alignment must not be larger than natural");
  8997. return false;
  8998. }
  8999. return true;
  9000. }
  9001. static bool
  9002. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9003. uint32 error_buf_size)
  9004. {
  9005. switch (opcode) {
  9006. case SIMD_i8x16_extract_lane_s:
  9007. case SIMD_i8x16_extract_lane_u:
  9008. case SIMD_i8x16_replace_lane:
  9009. if (lane >= 16) {
  9010. goto fail;
  9011. }
  9012. break;
  9013. case SIMD_i16x8_extract_lane_s:
  9014. case SIMD_i16x8_extract_lane_u:
  9015. case SIMD_i16x8_replace_lane:
  9016. if (lane >= 8) {
  9017. goto fail;
  9018. }
  9019. break;
  9020. case SIMD_i32x4_extract_lane:
  9021. case SIMD_i32x4_replace_lane:
  9022. case SIMD_f32x4_extract_lane:
  9023. case SIMD_f32x4_replace_lane:
  9024. if (lane >= 4) {
  9025. goto fail;
  9026. }
  9027. break;
  9028. case SIMD_i64x2_extract_lane:
  9029. case SIMD_i64x2_replace_lane:
  9030. case SIMD_f64x2_extract_lane:
  9031. case SIMD_f64x2_replace_lane:
  9032. if (lane >= 2) {
  9033. goto fail;
  9034. }
  9035. break;
  9036. case SIMD_v128_load8_lane:
  9037. case SIMD_v128_load16_lane:
  9038. case SIMD_v128_load32_lane:
  9039. case SIMD_v128_load64_lane:
  9040. case SIMD_v128_store8_lane:
  9041. case SIMD_v128_store16_lane:
  9042. case SIMD_v128_store32_lane:
  9043. case SIMD_v128_store64_lane:
  9044. case SIMD_v128_load32_zero:
  9045. case SIMD_v128_load64_zero:
  9046. {
  9047. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9048. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9049. goto fail;
  9050. }
  9051. break;
  9052. }
  9053. default:
  9054. goto fail;
  9055. }
  9056. return true;
  9057. fail:
  9058. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9059. return false;
  9060. }
  9061. static bool
  9062. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9063. {
  9064. uint8 i;
  9065. for (i = 0; i != 16; ++i) {
  9066. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9067. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9068. return false;
  9069. }
  9070. }
  9071. return true;
  9072. }
  9073. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9074. #endif /* end of WASM_ENABLE_SIMD */
  9075. #if WASM_ENABLE_SHARED_MEMORY != 0
  9076. static bool
  9077. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9078. uint32 error_buf_size)
  9079. {
  9080. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9081. uint8 mem_access_aligns[] = {
  9082. 2, 3, 0, 1, 0, 1, 2,
  9083. };
  9084. uint8 expect;
  9085. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9086. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9087. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9088. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9089. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9090. }
  9091. else {
  9092. /* 7 opcodes in every group */
  9093. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9094. }
  9095. if (align != expect) {
  9096. set_error_buf(error_buf, error_buf_size,
  9097. "alignment isn't equal to natural");
  9098. return false;
  9099. }
  9100. return true;
  9101. }
  9102. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9103. static bool
  9104. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9105. char *error_buf, uint32 error_buf_size)
  9106. {
  9107. BranchBlock *target_block, *cur_block;
  9108. BlockType *target_block_type;
  9109. uint8 type, *types = NULL, *frame_ref;
  9110. uint32 arity = 0;
  9111. int32 i, available_stack_cell;
  9112. uint16 cell_num;
  9113. #if WASM_ENABLE_GC != 0
  9114. WASMRefTypeMap *frame_reftype_map;
  9115. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9116. WASMRefType *ref_type;
  9117. uint32 reftype_map_count = 0;
  9118. int32 available_reftype_map;
  9119. bool is_type_multi_byte;
  9120. #endif
  9121. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9122. uint8 *frame_ref_after_popped = NULL;
  9123. uint8 frame_ref_tmp[4] = { 0 };
  9124. uint8 *frame_ref_buf = frame_ref_tmp;
  9125. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9126. #if WASM_ENABLE_GC != 0
  9127. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9128. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9129. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9130. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9131. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9132. #endif
  9133. #if WASM_ENABLE_FAST_INTERP != 0
  9134. int16 *frame_offset_old = loader_ctx->frame_offset;
  9135. int16 *frame_offset_after_popped = NULL;
  9136. int16 frame_offset_tmp[4] = { 0 };
  9137. int16 *frame_offset_buf = frame_offset_tmp;
  9138. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9139. #endif
  9140. bool ret = false;
  9141. bh_assert(loader_ctx->csp_num > 0);
  9142. if (loader_ctx->csp_num - 1 < depth) {
  9143. set_error_buf(error_buf, error_buf_size,
  9144. "unknown label, "
  9145. "unexpected end of section or function");
  9146. return false;
  9147. }
  9148. cur_block = loader_ctx->frame_csp - 1;
  9149. target_block = loader_ctx->frame_csp - (depth + 1);
  9150. target_block_type = &target_block->block_type;
  9151. frame_ref = loader_ctx->frame_ref;
  9152. #if WASM_ENABLE_GC != 0
  9153. frame_reftype_map = loader_ctx->frame_reftype_map;
  9154. #endif
  9155. /* Note: loop's arity is different from if and block. loop's arity is
  9156. * its parameter count while if and block arity is result count.
  9157. */
  9158. #if WASM_ENABLE_GC == 0
  9159. if (target_block->label_type == LABEL_TYPE_LOOP)
  9160. arity = block_type_get_param_types(target_block_type, &types);
  9161. else
  9162. arity = block_type_get_result_types(target_block_type, &types);
  9163. #else
  9164. if (target_block->label_type == LABEL_TYPE_LOOP)
  9165. arity = block_type_get_param_types(target_block_type, &types,
  9166. &reftype_maps, &reftype_map_count);
  9167. else
  9168. arity = block_type_get_result_types(target_block_type, &types,
  9169. &reftype_maps, &reftype_map_count);
  9170. #endif
  9171. /* If the stack is in polymorphic state, just clear the stack
  9172. * and then re-push the values to make the stack top values
  9173. * match block type. */
  9174. if (cur_block->is_stack_polymorphic) {
  9175. #if WASM_ENABLE_GC != 0
  9176. int32 j = reftype_map_count - 1;
  9177. #endif
  9178. for (i = (int32)arity - 1; i >= 0; i--) {
  9179. #if WASM_ENABLE_GC != 0
  9180. if (wasm_is_type_multi_byte_type(types[i])) {
  9181. bh_assert(reftype_maps[j].index == i);
  9182. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9183. reftype_maps[j].ref_type,
  9184. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9185. j--;
  9186. }
  9187. #endif
  9188. #if WASM_ENABLE_FAST_INTERP != 0
  9189. POP_OFFSET_TYPE(types[i]);
  9190. #endif
  9191. POP_TYPE(types[i]);
  9192. }
  9193. /* Backup stack data since it may be changed in the below
  9194. push operations, and the stack data may be used when
  9195. checking other target blocks of opcode br_table */
  9196. if (opcode == WASM_OP_BR_TABLE) {
  9197. uint64 total_size;
  9198. frame_ref_after_popped = loader_ctx->frame_ref;
  9199. total_size = (uint64)sizeof(uint8)
  9200. * (frame_ref_old - frame_ref_after_popped);
  9201. if (total_size > sizeof(frame_ref_tmp)
  9202. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9203. error_buf_size))) {
  9204. goto fail;
  9205. }
  9206. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9207. frame_ref_after_popped, (uint32)total_size);
  9208. #if WASM_ENABLE_GC != 0
  9209. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9210. total_size =
  9211. (uint64)sizeof(WASMRefTypeMap)
  9212. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9213. if (total_size > sizeof(frame_reftype_map_tmp)
  9214. && !(frame_reftype_map_buf = loader_malloc(
  9215. total_size, error_buf, error_buf_size))) {
  9216. goto fail;
  9217. }
  9218. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9219. frame_reftype_map_after_popped, (uint32)total_size);
  9220. #endif
  9221. #if WASM_ENABLE_FAST_INTERP != 0
  9222. frame_offset_after_popped = loader_ctx->frame_offset;
  9223. total_size = (uint64)sizeof(int16)
  9224. * (frame_offset_old - frame_offset_after_popped);
  9225. if (total_size > sizeof(frame_offset_tmp)
  9226. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9227. error_buf_size))) {
  9228. goto fail;
  9229. }
  9230. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9231. frame_offset_after_popped, (uint32)total_size);
  9232. #endif
  9233. }
  9234. #if WASM_ENABLE_GC != 0
  9235. j = 0;
  9236. #endif
  9237. for (i = 0; i < (int32)arity; i++) {
  9238. #if WASM_ENABLE_GC != 0
  9239. if (wasm_is_type_multi_byte_type(types[i])) {
  9240. bh_assert(reftype_maps[j].index == i);
  9241. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9242. reftype_maps[j].ref_type,
  9243. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9244. j++;
  9245. }
  9246. #endif
  9247. #if WASM_ENABLE_FAST_INTERP != 0
  9248. bool disable_emit = true;
  9249. int16 operand_offset = 0;
  9250. PUSH_OFFSET_TYPE(types[i]);
  9251. #endif
  9252. PUSH_TYPE(types[i]);
  9253. }
  9254. #if WASM_ENABLE_FAST_INTERP != 0
  9255. emit_br_info(target_block, opcode == WASM_OP_BR);
  9256. #endif
  9257. /* Restore the stack data, note that frame_ref_bottom,
  9258. frame_reftype_map_bottom, frame_offset_bottom may be
  9259. re-allocated in the above push operations */
  9260. if (opcode == WASM_OP_BR_TABLE) {
  9261. uint32 total_size;
  9262. /* The stack operand num should not be smaller than before
  9263. after pop and push operations */
  9264. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9265. loader_ctx->stack_cell_num = stack_cell_num_old;
  9266. loader_ctx->frame_ref =
  9267. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9268. total_size = (uint32)(sizeof(uint8)
  9269. * (frame_ref_old - frame_ref_after_popped));
  9270. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9271. frame_ref_buf, total_size);
  9272. #if WASM_ENABLE_GC != 0
  9273. /* The stack operand num should not be smaller than before
  9274. after pop and push operations */
  9275. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9276. loader_ctx->reftype_map_num = reftype_map_num_old;
  9277. loader_ctx->frame_reftype_map =
  9278. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9279. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9280. * (frame_reftype_map_old
  9281. - frame_reftype_map_after_popped));
  9282. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9283. total_size, frame_reftype_map_buf, total_size);
  9284. #endif
  9285. #if WASM_ENABLE_FAST_INTERP != 0
  9286. loader_ctx->frame_offset =
  9287. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9288. total_size =
  9289. (uint32)(sizeof(int16)
  9290. * (frame_offset_old - frame_offset_after_popped));
  9291. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9292. total_size, frame_offset_buf, total_size);
  9293. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9294. #endif
  9295. }
  9296. ret = true;
  9297. goto cleanup_and_return;
  9298. }
  9299. available_stack_cell =
  9300. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9301. #if WASM_ENABLE_GC != 0
  9302. available_reftype_map =
  9303. (int32)(loader_ctx->reftype_map_num
  9304. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9305. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9306. #endif
  9307. /* Check stack top values match target block type */
  9308. for (i = (int32)arity - 1; i >= 0; i--) {
  9309. type = types[i];
  9310. #if WASM_ENABLE_GC != 0
  9311. ref_type = NULL;
  9312. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9313. if (is_type_multi_byte) {
  9314. bh_assert(reftype_map);
  9315. ref_type = reftype_map->ref_type;
  9316. }
  9317. #endif
  9318. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9319. break;
  9320. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9321. #if WASM_ENABLE_GC != 0
  9322. frame_reftype_map, available_reftype_map,
  9323. #endif
  9324. type,
  9325. #if WASM_ENABLE_GC != 0
  9326. ref_type,
  9327. #endif
  9328. error_buf, error_buf_size)) {
  9329. goto fail;
  9330. }
  9331. cell_num = wasm_value_type_cell_num(types[i]);
  9332. frame_ref -= cell_num;
  9333. available_stack_cell -= cell_num;
  9334. #if WASM_ENABLE_GC != 0
  9335. if (is_type_multi_byte) {
  9336. frame_reftype_map--;
  9337. available_reftype_map--;
  9338. reftype_map--;
  9339. }
  9340. #endif
  9341. }
  9342. #if WASM_ENABLE_FAST_INTERP != 0
  9343. emit_br_info(target_block, opcode == WASM_OP_BR);
  9344. #endif
  9345. ret = true;
  9346. cleanup_and_return:
  9347. fail:
  9348. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9349. wasm_runtime_free(frame_ref_buf);
  9350. #if WASM_ENABLE_GC != 0
  9351. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9352. wasm_runtime_free(frame_reftype_map_buf);
  9353. #endif
  9354. #if WASM_ENABLE_FAST_INTERP != 0
  9355. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9356. wasm_runtime_free(frame_offset_buf);
  9357. #endif
  9358. return ret;
  9359. }
  9360. static BranchBlock *
  9361. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9362. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9363. {
  9364. uint8 *p = *p_buf, *p_end = buf_end;
  9365. BranchBlock *frame_csp_tmp;
  9366. uint32 depth;
  9367. read_leb_uint32(p, p_end, depth);
  9368. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9369. error_buf_size)) {
  9370. goto fail;
  9371. }
  9372. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9373. *p_buf = p;
  9374. return frame_csp_tmp;
  9375. fail:
  9376. return NULL;
  9377. }
  9378. #if WASM_ENABLE_EXCE_HANDLING != 0
  9379. static BranchBlock *
  9380. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9381. uint8 *buf_end, char *error_buf,
  9382. uint32 error_buf_size)
  9383. {
  9384. uint8 *p = *p_buf, *p_end = buf_end;
  9385. BranchBlock *frame_csp_tmp;
  9386. uint32 depth;
  9387. read_leb_uint32(p, p_end, depth);
  9388. /*
  9389. * Note: "delegate 0" means the surrounding block, not the
  9390. * try-delegate block itself.
  9391. *
  9392. * Note: the caller hasn't popped the try-delegate frame yet.
  9393. */
  9394. bh_assert(loader_ctx->csp_num > 0);
  9395. if (loader_ctx->csp_num - 1 <= depth) {
  9396. #if WASM_ENABLE_SPEC_TEST == 0
  9397. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9398. #else
  9399. set_error_buf(error_buf, error_buf_size, "unknown label");
  9400. #endif
  9401. goto fail;
  9402. }
  9403. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9404. #if WASM_ENABLE_FAST_INTERP != 0
  9405. emit_br_info(frame_csp_tmp, false);
  9406. #endif
  9407. *p_buf = p;
  9408. return frame_csp_tmp;
  9409. fail:
  9410. return NULL;
  9411. }
  9412. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9413. static bool
  9414. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9415. char *error_buf, uint32 error_buf_size)
  9416. {
  9417. BlockType *block_type = &block->block_type;
  9418. uint8 *return_types = NULL;
  9419. uint32 return_count = 0;
  9420. int32 available_stack_cell, return_cell_num, i;
  9421. uint8 *frame_ref = NULL;
  9422. #if WASM_ENABLE_GC != 0
  9423. WASMRefTypeMap *frame_reftype_map;
  9424. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9425. WASMRefType *ref_type;
  9426. uint32 param_count, return_reftype_map_count = 0;
  9427. int32 available_reftype_map =
  9428. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9429. #endif
  9430. available_stack_cell =
  9431. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9432. #if WASM_ENABLE_GC == 0
  9433. return_count = block_type_get_result_types(block_type, &return_types);
  9434. #else
  9435. return_count = block_type_get_result_types(block_type, &return_types,
  9436. &return_reftype_maps,
  9437. &return_reftype_map_count);
  9438. param_count =
  9439. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9440. (void)param_count;
  9441. #endif
  9442. return_cell_num =
  9443. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9444. /* If the stack is in polymorphic state, just clear the stack
  9445. * and then re-push the values to make the stack top values
  9446. * match block type. */
  9447. if (block->is_stack_polymorphic) {
  9448. #if WASM_ENABLE_GC != 0
  9449. int32 j = return_reftype_map_count - 1;
  9450. #endif
  9451. for (i = (int32)return_count - 1; i >= 0; i--) {
  9452. #if WASM_ENABLE_GC != 0
  9453. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9454. bh_assert(return_reftype_maps[j].index == i + param_count);
  9455. bh_memcpy_s(
  9456. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9457. return_reftype_maps[j].ref_type,
  9458. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9459. j--;
  9460. }
  9461. #endif
  9462. #if WASM_ENABLE_FAST_INTERP != 0
  9463. POP_OFFSET_TYPE(return_types[i]);
  9464. #endif
  9465. POP_TYPE(return_types[i]);
  9466. }
  9467. /* Check stack is empty */
  9468. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9469. set_error_buf(
  9470. error_buf, error_buf_size,
  9471. "type mismatch: stack size does not match block type");
  9472. goto fail;
  9473. }
  9474. #if WASM_ENABLE_GC != 0
  9475. j = 0;
  9476. #endif
  9477. for (i = 0; i < (int32)return_count; i++) {
  9478. #if WASM_ENABLE_GC != 0
  9479. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9480. bh_assert(return_reftype_maps[j].index == i + param_count);
  9481. bh_memcpy_s(
  9482. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9483. return_reftype_maps[j].ref_type,
  9484. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9485. j++;
  9486. }
  9487. #endif
  9488. #if WASM_ENABLE_FAST_INTERP != 0
  9489. bool disable_emit = true;
  9490. int16 operand_offset = 0;
  9491. PUSH_OFFSET_TYPE(return_types[i]);
  9492. #endif
  9493. PUSH_TYPE(return_types[i]);
  9494. }
  9495. return true;
  9496. }
  9497. if (available_stack_cell != return_cell_num) {
  9498. #if WASM_ENABLE_EXCE_HANDLING != 0
  9499. /* testspec: this error message format is expected by try_catch.wast */
  9500. snprintf(
  9501. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9502. block->label_type == LABEL_TYPE_TRY
  9503. || (block->label_type == LABEL_TYPE_CATCH
  9504. && return_cell_num > 0)
  9505. ? "instruction"
  9506. : "block",
  9507. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9508. " but stack has ",
  9509. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9510. : "");
  9511. goto fail;
  9512. #else
  9513. set_error_buf(error_buf, error_buf_size,
  9514. "type mismatch: stack size does not match block type");
  9515. goto fail;
  9516. #endif
  9517. }
  9518. /* Check stack values match return types */
  9519. frame_ref = loader_ctx->frame_ref;
  9520. #if WASM_ENABLE_GC != 0
  9521. frame_reftype_map = loader_ctx->frame_reftype_map;
  9522. return_reftype_map =
  9523. return_reftype_map_count
  9524. ? return_reftype_maps + return_reftype_map_count - 1
  9525. : NULL;
  9526. #endif
  9527. for (i = (int32)return_count - 1; i >= 0; i--) {
  9528. uint8 type = return_types[i];
  9529. #if WASM_ENABLE_GC != 0
  9530. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9531. ref_type = NULL;
  9532. if (is_type_multi_byte) {
  9533. bh_assert(return_reftype_map);
  9534. ref_type = return_reftype_map->ref_type;
  9535. }
  9536. #endif
  9537. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9538. #if WASM_ENABLE_GC != 0
  9539. frame_reftype_map, available_reftype_map,
  9540. #endif
  9541. type,
  9542. #if WASM_ENABLE_GC != 0
  9543. ref_type,
  9544. #endif
  9545. error_buf, error_buf_size))
  9546. return false;
  9547. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9548. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9549. #if WASM_ENABLE_GC != 0
  9550. if (is_type_multi_byte) {
  9551. frame_reftype_map--;
  9552. available_reftype_map--;
  9553. return_reftype_map--;
  9554. }
  9555. #endif
  9556. }
  9557. return true;
  9558. fail:
  9559. return false;
  9560. }
  9561. #if WASM_ENABLE_FAST_INTERP != 0
  9562. /* Copy parameters to dynamic space.
  9563. * 1) POP original parameter out;
  9564. * 2) Push and copy original values to dynamic space.
  9565. * The copy instruction format:
  9566. * Part a: param count
  9567. * Part b: all param total cell num
  9568. * Part c: each param's cell_num, src offset and dst offset
  9569. * Part d: each param's src offset
  9570. * Part e: each param's dst offset
  9571. */
  9572. static bool
  9573. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9574. uint32 error_buf_size)
  9575. {
  9576. bool ret = false;
  9577. int16 *frame_offset = NULL;
  9578. uint8 *cells = NULL, cell;
  9579. int16 *src_offsets = NULL;
  9580. uint8 *emit_data = NULL;
  9581. uint32 i;
  9582. BranchBlock *block = loader_ctx->frame_csp - 1;
  9583. BlockType *block_type = &block->block_type;
  9584. WASMFuncType *wasm_type = block_type->u.type;
  9585. uint32 param_count = block_type->u.type->param_count;
  9586. int16 condition_offset = 0;
  9587. bool disable_emit = false;
  9588. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9589. int16 operand_offset = 0;
  9590. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9591. bh_assert(size > 0);
  9592. /* For if block, we also need copy the condition operand offset. */
  9593. if (is_if_block)
  9594. size += sizeof(*cells) + sizeof(*src_offsets);
  9595. /* Allocate memory for the emit data */
  9596. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9597. return false;
  9598. cells = emit_data;
  9599. src_offsets = (int16 *)(cells + param_count);
  9600. if (is_if_block)
  9601. condition_offset = *loader_ctx->frame_offset;
  9602. /* POP original parameter out */
  9603. for (i = 0; i < param_count; i++) {
  9604. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9605. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9606. }
  9607. frame_offset = loader_ctx->frame_offset;
  9608. /* Get each param's cell num and src offset */
  9609. for (i = 0; i < param_count; i++) {
  9610. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9611. cells[i] = cell;
  9612. src_offsets[i] = *frame_offset;
  9613. frame_offset += cell;
  9614. }
  9615. /* emit copy instruction */
  9616. emit_label(EXT_OP_COPY_STACK_VALUES);
  9617. /* Part a) */
  9618. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9619. /* Part b) */
  9620. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9621. : wasm_type->param_cell_num);
  9622. /* Part c) */
  9623. for (i = 0; i < param_count; i++)
  9624. emit_byte(loader_ctx, cells[i]);
  9625. if (is_if_block)
  9626. emit_byte(loader_ctx, 1);
  9627. /* Part d) */
  9628. for (i = 0; i < param_count; i++)
  9629. emit_operand(loader_ctx, src_offsets[i]);
  9630. if (is_if_block)
  9631. emit_operand(loader_ctx, condition_offset);
  9632. /* Since the start offset to save the block's params and
  9633. * the start offset to save the block's results may be
  9634. * different, we remember the dynamic offset for loop block
  9635. * so that we can use it to copy the stack operands to the
  9636. * loop block's params in wasm_loader_emit_br_info. */
  9637. if (block->label_type == LABEL_TYPE_LOOP)
  9638. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9639. /* Part e) */
  9640. /* Push to dynamic space. The push will emit the dst offset. */
  9641. for (i = 0; i < param_count; i++)
  9642. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9643. if (is_if_block)
  9644. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9645. ret = true;
  9646. fail:
  9647. /* Free the emit data */
  9648. wasm_runtime_free(emit_data);
  9649. return ret;
  9650. }
  9651. #endif
  9652. #if WASM_ENABLE_GC == 0
  9653. #define RESET_REFTYPE_MAP_STACK() (void)0
  9654. #else
  9655. #define RESET_REFTYPE_MAP_STACK() \
  9656. do { \
  9657. loader_ctx->reftype_map_num = \
  9658. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9659. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9660. + loader_ctx->reftype_map_num; \
  9661. } while (0)
  9662. #endif
  9663. /* reset the stack to the state of before entering the last block */
  9664. #if WASM_ENABLE_FAST_INTERP != 0
  9665. #define RESET_STACK() \
  9666. do { \
  9667. loader_ctx->stack_cell_num = \
  9668. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9669. loader_ctx->frame_ref = \
  9670. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9671. loader_ctx->frame_offset = \
  9672. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9673. RESET_REFTYPE_MAP_STACK(); \
  9674. } while (0)
  9675. #else
  9676. #define RESET_STACK() \
  9677. do { \
  9678. loader_ctx->stack_cell_num = \
  9679. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9680. loader_ctx->frame_ref = \
  9681. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9682. RESET_REFTYPE_MAP_STACK(); \
  9683. } while (0)
  9684. #endif
  9685. /* set current block's stack polymorphic state */
  9686. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9687. do { \
  9688. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9689. _cur_block->is_stack_polymorphic = flag; \
  9690. } while (0)
  9691. #define BLOCK_HAS_PARAM(block_type) \
  9692. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9693. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9694. do { \
  9695. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9696. error_buf_size))) { \
  9697. goto fail; \
  9698. } \
  9699. } while (0)
  9700. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9701. static bool
  9702. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9703. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9704. uint32 error_buf_size)
  9705. {
  9706. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9707. return false;
  9708. }
  9709. if (table_idx < module->import_table_count) {
  9710. if (p_elem_type)
  9711. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9712. #if WASM_ENABLE_GC != 0
  9713. if (p_ref_type)
  9714. *((WASMRefType **)p_ref_type) =
  9715. module->import_tables[table_idx].u.table.elem_ref_type;
  9716. #endif
  9717. }
  9718. else {
  9719. if (p_elem_type)
  9720. *p_elem_type =
  9721. module->tables[module->import_table_count + table_idx]
  9722. .elem_type;
  9723. #if WASM_ENABLE_GC != 0
  9724. if (p_ref_type)
  9725. *((WASMRefType **)p_ref_type) =
  9726. module->tables[module->import_table_count + table_idx]
  9727. .elem_ref_type;
  9728. #endif
  9729. }
  9730. return true;
  9731. }
  9732. static bool
  9733. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9734. uint8 *p_elem_type, void **p_elem_ref_type,
  9735. char *error_buf, uint32 error_buf_size)
  9736. {
  9737. if (table_seg_idx >= module->table_seg_count) {
  9738. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9739. table_seg_idx);
  9740. return false;
  9741. }
  9742. if (p_elem_type) {
  9743. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9744. }
  9745. #if WASM_ENABLE_GC != 0
  9746. if (p_elem_ref_type)
  9747. *((WASMRefType **)p_elem_ref_type) =
  9748. module->table_segments[table_seg_idx].elem_ref_type;
  9749. #endif
  9750. return true;
  9751. }
  9752. #endif
  9753. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9754. const uint8 *
  9755. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9756. uint32 *len)
  9757. {
  9758. WASMCustomSection *section = module->custom_section_list;
  9759. while (section) {
  9760. if ((section->name_len == strlen(name))
  9761. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9762. if (len) {
  9763. *len = section->content_len;
  9764. }
  9765. return section->content_addr;
  9766. }
  9767. section = section->next;
  9768. }
  9769. return NULL;
  9770. }
  9771. #endif
  9772. static bool
  9773. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9774. uint32 cur_func_idx, char *error_buf,
  9775. uint32 error_buf_size)
  9776. {
  9777. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9778. uint32 param_count, local_count, global_count;
  9779. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9780. BlockType func_block_type;
  9781. uint16 *local_offsets, local_offset;
  9782. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9783. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9784. mem_offset_t mem_offset;
  9785. int32 i32_const = 0;
  9786. int64 i64_const;
  9787. uint8 opcode;
  9788. bool return_value = false;
  9789. WASMLoaderContext *loader_ctx;
  9790. BranchBlock *frame_csp_tmp;
  9791. #if WASM_ENABLE_GC != 0
  9792. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9793. uint32 param_reftype_map_count, local_reftype_map_count;
  9794. int32 heap_type;
  9795. WASMRefType wasm_ref_type = { 0 };
  9796. bool need_ref_type_map;
  9797. #endif
  9798. #if WASM_ENABLE_FAST_INTERP != 0
  9799. uint8 *func_const_end, *func_const = NULL;
  9800. int16 operand_offset = 0;
  9801. uint8 last_op = 0;
  9802. bool disable_emit, preserve_local = false, if_condition_available = true;
  9803. float32 f32_const;
  9804. float64 f64_const;
  9805. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9806. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9807. #endif
  9808. #if WASM_ENABLE_MEMORY64 != 0
  9809. bool is_memory64 = has_module_memory64(module);
  9810. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9811. #else
  9812. mem_offset_type = VALUE_TYPE_I32;
  9813. #endif
  9814. global_count = module->import_global_count + module->global_count;
  9815. param_count = func->func_type->param_count;
  9816. param_types = func->func_type->types;
  9817. func_block_type.is_value_type = false;
  9818. func_block_type.u.type = func->func_type;
  9819. local_count = func->local_count;
  9820. local_types = func->local_types;
  9821. local_offsets = func->local_offsets;
  9822. #if WASM_ENABLE_GC != 0
  9823. param_reftype_maps = func->func_type->ref_type_maps;
  9824. param_reftype_map_count = func->func_type->ref_type_map_count;
  9825. local_reftype_maps = func->local_ref_type_maps;
  9826. local_reftype_map_count = func->local_ref_type_map_count;
  9827. #endif
  9828. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9829. goto fail;
  9830. }
  9831. #if WASM_ENABLE_GC != 0
  9832. loader_ctx->module = module;
  9833. loader_ctx->ref_type_set = module->ref_type_set;
  9834. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9835. #endif
  9836. #if WASM_ENABLE_FAST_INTERP != 0
  9837. /* For the first traverse, the initial value of preserved_local_offset has
  9838. * not been determined, we use the INT16_MAX to represent that a slot has
  9839. * been copied to preserve space. For second traverse, this field will be
  9840. * set to the appropriate value in wasm_loader_ctx_reinit.
  9841. * This is for Issue #1230,
  9842. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9843. * drop opcodes need to know which slots are preserved, so those slots will
  9844. * not be treated as dynamically allocated slots */
  9845. loader_ctx->preserved_local_offset = INT16_MAX;
  9846. re_scan:
  9847. if (loader_ctx->code_compiled_size > 0) {
  9848. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9849. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9850. goto fail;
  9851. }
  9852. p = func->code;
  9853. func->code_compiled = loader_ctx->p_code_compiled;
  9854. func->code_compiled_size = loader_ctx->code_compiled_size;
  9855. }
  9856. #endif
  9857. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9858. while (p < p_end) {
  9859. opcode = *p++;
  9860. #if WASM_ENABLE_FAST_INTERP != 0
  9861. p_org = p;
  9862. disable_emit = false;
  9863. emit_label(opcode);
  9864. #endif
  9865. switch (opcode) {
  9866. case WASM_OP_UNREACHABLE:
  9867. RESET_STACK();
  9868. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9869. break;
  9870. case WASM_OP_NOP:
  9871. #if WASM_ENABLE_FAST_INTERP != 0
  9872. skip_label();
  9873. #endif
  9874. break;
  9875. case WASM_OP_IF:
  9876. {
  9877. #if WASM_ENABLE_FAST_INTERP != 0
  9878. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9879. int32 available_stack_cell =
  9880. (int32)(loader_ctx->stack_cell_num
  9881. - parent_block->stack_cell_num);
  9882. if (available_stack_cell <= 0
  9883. && parent_block->is_stack_polymorphic)
  9884. if_condition_available = false;
  9885. else
  9886. if_condition_available = true;
  9887. PRESERVE_LOCAL_FOR_BLOCK();
  9888. #endif
  9889. #if WASM_ENABLE_GC == 0
  9890. POP_I32();
  9891. #endif
  9892. goto handle_op_block_and_loop;
  9893. }
  9894. case WASM_OP_BLOCK:
  9895. case WASM_OP_LOOP:
  9896. #if WASM_ENABLE_EXCE_HANDLING != 0
  9897. case WASM_OP_TRY:
  9898. if (opcode == WASM_OP_TRY) {
  9899. /*
  9900. * keep track of exception handlers to account for
  9901. * memory allocation
  9902. */
  9903. func->exception_handler_count++;
  9904. /*
  9905. * try is a block
  9906. * do nothing special, but execution continues to
  9907. * to handle_op_block_and_loop,
  9908. * and that be pushes the csp
  9909. */
  9910. }
  9911. #endif
  9912. #if WASM_ENABLE_FAST_INTERP != 0
  9913. PRESERVE_LOCAL_FOR_BLOCK();
  9914. #endif
  9915. handle_op_block_and_loop:
  9916. {
  9917. uint8 value_type;
  9918. BlockType block_type;
  9919. #if WASM_ENABLE_FAST_INTERP != 0
  9920. uint32 available_params = 0;
  9921. #endif
  9922. CHECK_BUF(p, p_end, 1);
  9923. value_type = read_uint8(p);
  9924. if (is_byte_a_type(value_type)) {
  9925. /* If the first byte is one of these special values:
  9926. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9927. * the single return value. */
  9928. block_type.is_value_type = true;
  9929. block_type.u.value_type.type = value_type;
  9930. #if WASM_ENABLE_WAMR_COMPILER != 0
  9931. if (value_type == VALUE_TYPE_V128)
  9932. module->is_simd_used = true;
  9933. else if (value_type == VALUE_TYPE_FUNCREF
  9934. || value_type == VALUE_TYPE_EXTERNREF)
  9935. module->is_ref_types_used = true;
  9936. #endif
  9937. #if WASM_ENABLE_GC != 0
  9938. if (value_type != VALUE_TYPE_VOID) {
  9939. p_org = p;
  9940. p--;
  9941. if (!resolve_value_type((const uint8 **)&p, p_end,
  9942. module, module->type_count,
  9943. &need_ref_type_map,
  9944. &wasm_ref_type, false,
  9945. error_buf, error_buf_size)) {
  9946. goto fail;
  9947. }
  9948. if (need_ref_type_map) {
  9949. block_type.u.value_type.ref_type_map.index = 0;
  9950. if (!(block_type.u.value_type.ref_type_map
  9951. .ref_type = reftype_set_insert(
  9952. module->ref_type_set, &wasm_ref_type,
  9953. error_buf, error_buf_size))) {
  9954. goto fail;
  9955. }
  9956. }
  9957. /* Set again as the type might be changed, e.g.
  9958. (ref null any) to anyref */
  9959. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9960. #if WASM_ENABLE_FAST_INTERP == 0
  9961. while (p_org < p) {
  9962. #if WASM_ENABLE_DEBUG_INTERP != 0
  9963. if (!record_fast_op(module, p_org, *p_org,
  9964. error_buf, error_buf_size)) {
  9965. goto fail;
  9966. }
  9967. #endif
  9968. /* Ignore extra bytes for interpreter */
  9969. *p_org++ = WASM_OP_NOP;
  9970. }
  9971. #endif
  9972. }
  9973. #endif /* end of WASM_ENABLE_GC != 0 */
  9974. }
  9975. else {
  9976. int32 type_index;
  9977. /* Resolve the leb128 encoded type index as block type */
  9978. p--;
  9979. p_org = p - 1;
  9980. read_leb_int32(p, p_end, type_index);
  9981. if ((uint32)type_index >= module->type_count) {
  9982. set_error_buf(error_buf, error_buf_size,
  9983. "unknown type");
  9984. goto fail;
  9985. }
  9986. block_type.is_value_type = false;
  9987. block_type.u.type =
  9988. (WASMFuncType *)module->types[type_index];
  9989. #if WASM_ENABLE_FAST_INTERP == 0
  9990. /* If block use type index as block type, change the opcode
  9991. * to new extended opcode so that interpreter can resolve
  9992. * the block quickly.
  9993. */
  9994. #if WASM_ENABLE_DEBUG_INTERP != 0
  9995. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9996. error_buf_size)) {
  9997. goto fail;
  9998. }
  9999. #endif
  10000. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10001. #endif
  10002. }
  10003. #if WASM_ENABLE_GC != 0
  10004. if (opcode == WASM_OP_IF) {
  10005. POP_I32();
  10006. }
  10007. #endif
  10008. /* Pop block parameters from stack */
  10009. if (BLOCK_HAS_PARAM(block_type)) {
  10010. WASMFuncType *wasm_type = block_type.u.type;
  10011. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10012. #if WASM_ENABLE_FAST_INTERP != 0
  10013. uint32 cell_num;
  10014. available_params = block_type.u.type->param_count;
  10015. #endif
  10016. for (i = 0; i < block_type.u.type->param_count; i++) {
  10017. int32 available_stack_cell =
  10018. (int32)(loader_ctx->stack_cell_num
  10019. - cur_block->stack_cell_num);
  10020. if (available_stack_cell <= 0
  10021. && cur_block->is_stack_polymorphic) {
  10022. #if WASM_ENABLE_FAST_INTERP != 0
  10023. available_params = i;
  10024. #endif
  10025. break;
  10026. }
  10027. POP_TYPE(
  10028. wasm_type->types[wasm_type->param_count - i - 1]);
  10029. #if WASM_ENABLE_FAST_INTERP != 0
  10030. /* decrease the frame_offset pointer accordingly to keep
  10031. * consistent with frame_ref stack */
  10032. cell_num = wasm_value_type_cell_num(
  10033. wasm_type->types[wasm_type->param_count - i - 1]);
  10034. loader_ctx->frame_offset -= cell_num;
  10035. #endif
  10036. }
  10037. }
  10038. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10039. block_type, p);
  10040. /* Pass parameters to block */
  10041. if (BLOCK_HAS_PARAM(block_type)) {
  10042. for (i = 0; i < block_type.u.type->param_count; i++) {
  10043. #if WASM_ENABLE_FAST_INTERP != 0
  10044. uint32 cell_num = wasm_value_type_cell_num(
  10045. block_type.u.type->types[i]);
  10046. if (i >= available_params) {
  10047. /* If there isn't enough data on stack, push a dummy
  10048. * offset to keep the stack consistent with
  10049. * frame_ref.
  10050. * Since the stack is already in polymorphic state,
  10051. * the opcode will not be executed, so the dummy
  10052. * offset won't cause any error */
  10053. uint32 n;
  10054. for (n = 0; n < cell_num; n++) {
  10055. if (loader_ctx->p_code_compiled == NULL) {
  10056. if (!check_offset_push(loader_ctx,
  10057. error_buf,
  10058. error_buf_size))
  10059. goto fail;
  10060. }
  10061. *loader_ctx->frame_offset++ = 0;
  10062. }
  10063. }
  10064. else {
  10065. loader_ctx->frame_offset += cell_num;
  10066. }
  10067. #endif
  10068. PUSH_TYPE(block_type.u.type->types[i]);
  10069. }
  10070. }
  10071. #if WASM_ENABLE_FAST_INTERP != 0
  10072. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10073. skip_label();
  10074. if (BLOCK_HAS_PARAM(block_type)) {
  10075. /* Make sure params are in dynamic space */
  10076. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10077. error_buf_size))
  10078. goto fail;
  10079. }
  10080. if (opcode == WASM_OP_LOOP) {
  10081. (loader_ctx->frame_csp - 1)->code_compiled =
  10082. loader_ctx->p_code_compiled;
  10083. }
  10084. }
  10085. #if WASM_ENABLE_EXCE_HANDLING != 0
  10086. else if (opcode == WASM_OP_TRY) {
  10087. skip_label();
  10088. }
  10089. #endif
  10090. else if (opcode == WASM_OP_IF) {
  10091. BranchBlock *block = loader_ctx->frame_csp - 1;
  10092. /* If block has parameters, we should make sure they are in
  10093. * dynamic space. Otherwise, when else branch is missing,
  10094. * the later opcode may consume incorrect operand offset.
  10095. * Spec case:
  10096. * (func (export "params-id") (param i32) (result i32)
  10097. * (i32.const 1)
  10098. * (i32.const 2)
  10099. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10100. * (then)) (i32.add)
  10101. * )
  10102. *
  10103. * So we should emit a copy instruction before the if.
  10104. *
  10105. * And we also need to save the parameter offsets and
  10106. * recover them before entering else branch.
  10107. *
  10108. */
  10109. if (BLOCK_HAS_PARAM(block_type)) {
  10110. uint64 size;
  10111. /* In polymorphic state, there may be no if condition on
  10112. * the stack, so the offset may not emitted */
  10113. if (if_condition_available) {
  10114. /* skip the if condition operand offset */
  10115. wasm_loader_emit_backspace(loader_ctx,
  10116. sizeof(int16));
  10117. }
  10118. /* skip the if label */
  10119. skip_label();
  10120. /* Emit a copy instruction */
  10121. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10122. error_buf_size))
  10123. goto fail;
  10124. /* Emit the if instruction */
  10125. emit_label(opcode);
  10126. /* Emit the new condition operand offset */
  10127. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10128. /* Save top param_count values of frame_offset stack, so
  10129. * that we can recover it before executing else branch
  10130. */
  10131. size = sizeof(int16)
  10132. * (uint64)block_type.u.type->param_cell_num;
  10133. if (!(block->param_frame_offsets = loader_malloc(
  10134. size, error_buf, error_buf_size)))
  10135. goto fail;
  10136. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10137. loader_ctx->frame_offset
  10138. - size / sizeof(int16),
  10139. (uint32)size);
  10140. }
  10141. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10142. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10143. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10144. }
  10145. #endif
  10146. break;
  10147. }
  10148. #if WASM_ENABLE_EXCE_HANDLING != 0
  10149. case WASM_OP_THROW:
  10150. {
  10151. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10152. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10153. uint8 label_type = cur_block->label_type;
  10154. uint32 tag_index = 0;
  10155. read_leb_int32(p, p_end, tag_index);
  10156. /* check validity of tag_index against module->tag_count */
  10157. /* check tag index is within the tag index space */
  10158. if (tag_index >= module->import_tag_count + module->tag_count) {
  10159. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10160. tag_index);
  10161. goto fail;
  10162. }
  10163. /* the tag_type is stored in either the WASMTag (section tags)
  10164. * or WASMTagImport (import tag) */
  10165. WASMFuncType *tag_type = NULL;
  10166. if (tag_index < module->import_tag_count) {
  10167. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10168. }
  10169. else {
  10170. tag_type =
  10171. module->tags[tag_index - module->import_tag_count]
  10172. ->tag_type;
  10173. }
  10174. if (tag_type->result_count != 0) {
  10175. set_error_buf(error_buf, error_buf_size,
  10176. "tag type signature does not return void");
  10177. goto fail;
  10178. }
  10179. int32 available_stack_cell =
  10180. (int32)(loader_ctx->stack_cell_num
  10181. - cur_block->stack_cell_num);
  10182. /* Check stack values match return types by comparing tag param
  10183. * types with stack cells */
  10184. uint8 *frame_ref = loader_ctx->frame_ref;
  10185. #if WASM_ENABLE_GC != 0
  10186. WASMRefTypeMap *frame_reftype_map =
  10187. loader_ctx->frame_reftype_map;
  10188. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10189. param_reftype_maps = tag_type->ref_type_maps;
  10190. /* For tag_type function, it shouldn't have result_count = 0 */
  10191. param_reftype_map_count = tag_type->ref_type_map_count;
  10192. param_count = (int32)tag_type->param_count;
  10193. #endif
  10194. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  10195. tti--) {
  10196. #if WASM_ENABLE_GC != 0
  10197. local_type = tag_type->types[tti];
  10198. local_idx = tti;
  10199. /* Get the wasm_ref_type if the local_type is multibyte
  10200. * type */
  10201. GET_LOCAL_REFTYPE();
  10202. #endif
  10203. if (!check_stack_top_values(
  10204. loader_ctx, frame_ref, available_stack_cell,
  10205. #if WASM_ENABLE_GC != 0
  10206. frame_reftype_map, frame_reftype_map_num,
  10207. #endif
  10208. tag_type->types[tti],
  10209. #if WASM_ENABLE_GC != 0
  10210. &wasm_ref_type,
  10211. #endif
  10212. error_buf, error_buf_size)) {
  10213. snprintf(error_buf, error_buf_size,
  10214. "type mismatch: instruction requires [%s] but "
  10215. "stack has [%s]",
  10216. tag_type->param_count > 0
  10217. ? type2str(tag_type->types[tti])
  10218. : "",
  10219. available_stack_cell > 0
  10220. ? type2str(*(loader_ctx->frame_ref - 1))
  10221. : "");
  10222. goto fail;
  10223. }
  10224. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10225. available_stack_cell -=
  10226. wasm_value_type_cell_num(tag_type->types[tti]);
  10227. }
  10228. /* throw is stack polymorphic */
  10229. (void)label_type;
  10230. RESET_STACK();
  10231. break;
  10232. }
  10233. case WASM_OP_RETHROW:
  10234. {
  10235. /* must be done before checking branch block */
  10236. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10237. /* check the target catching block: LABEL_TYPE_CATCH */
  10238. if (!(frame_csp_tmp =
  10239. check_branch_block(loader_ctx, &p, p_end, opcode,
  10240. error_buf, error_buf_size)))
  10241. goto fail;
  10242. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10243. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10244. /* trap according to spectest (rethrow.wast) */
  10245. set_error_buf(error_buf, error_buf_size,
  10246. "invalid rethrow label");
  10247. goto fail;
  10248. }
  10249. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10250. uint8 label_type = cur_block->label_type;
  10251. (void)label_type;
  10252. /* rethrow is stack polymorphic */
  10253. RESET_STACK();
  10254. break;
  10255. }
  10256. case WASM_OP_DELEGATE:
  10257. {
  10258. /* check target block is valid */
  10259. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10260. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10261. goto fail;
  10262. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10263. uint8 label_type = cur_block->label_type;
  10264. (void)label_type;
  10265. /* DELEGATE ends the block */
  10266. POP_CSP();
  10267. break;
  10268. }
  10269. case WASM_OP_CATCH:
  10270. {
  10271. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10272. uint8 label_type = cur_block->label_type;
  10273. uint32 tag_index = 0;
  10274. read_leb_int32(p, p_end, tag_index);
  10275. /* check validity of tag_index against module->tag_count */
  10276. /* check tag index is within the tag index space */
  10277. if (tag_index >= module->import_tag_count + module->tag_count) {
  10278. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10279. __FUNCTION__);
  10280. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10281. goto fail;
  10282. }
  10283. /* the tag_type is stored in either the WASMTag (section tags)
  10284. * or WASMTagImport (import tag) */
  10285. WASMFuncType *func_type = NULL;
  10286. if (tag_index < module->import_tag_count) {
  10287. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10288. }
  10289. else {
  10290. func_type =
  10291. module->tags[tag_index - module->import_tag_count]
  10292. ->tag_type;
  10293. }
  10294. if (func_type->result_count != 0) {
  10295. set_error_buf(error_buf, error_buf_size,
  10296. "tag type signature does not return void");
  10297. goto fail;
  10298. }
  10299. /* check validity of current label (expect LABEL_TYPE_TRY or
  10300. * LABEL_TYPE_CATCH) */
  10301. if ((LABEL_TYPE_CATCH != label_type)
  10302. && (LABEL_TYPE_TRY != label_type)) {
  10303. set_error_buf(error_buf, error_buf_size,
  10304. "Unexpected block sequence encountered.");
  10305. goto fail;
  10306. }
  10307. BlockType new_block_type;
  10308. new_block_type.is_value_type = false;
  10309. new_block_type.u.type = func_type;
  10310. /*
  10311. * replace frame_csp by LABEL_TYPE_CATCH
  10312. */
  10313. cur_block->label_type = LABEL_TYPE_CATCH;
  10314. /* RESET_STACK removes the values pushed in TRY or pervious
  10315. * CATCH Blocks */
  10316. RESET_STACK();
  10317. /* push types on the stack according to caught type */
  10318. if (BLOCK_HAS_PARAM(new_block_type)) {
  10319. for (i = 0; i < new_block_type.u.type->param_count; i++)
  10320. PUSH_TYPE(new_block_type.u.type->types[i]);
  10321. }
  10322. break;
  10323. }
  10324. case WASM_OP_CATCH_ALL:
  10325. {
  10326. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10327. /* expecting a TRY or CATCH, anything else will be considered an
  10328. * error */
  10329. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10330. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10331. set_error_buf(error_buf, error_buf_size,
  10332. "Unexpected block sequence encountered.");
  10333. goto fail;
  10334. }
  10335. /* no immediates */
  10336. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10337. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10338. /* RESET_STACK removes the values pushed in TRY or pervious
  10339. * CATCH Blocks */
  10340. RESET_STACK();
  10341. /* catch_all has no tagtype and therefore no parameters */
  10342. break;
  10343. }
  10344. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10345. case WASM_OP_ELSE:
  10346. handle_op_else:
  10347. {
  10348. BranchBlock *block = NULL;
  10349. BlockType block_type;
  10350. if (loader_ctx->csp_num < 2
  10351. /* the matched if isn't found */
  10352. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10353. /* duplicated else is found */
  10354. || (loader_ctx->frame_csp - 1)->else_addr) {
  10355. set_error_buf(
  10356. error_buf, error_buf_size,
  10357. "opcode else found without matched opcode if");
  10358. goto fail;
  10359. }
  10360. block = loader_ctx->frame_csp - 1;
  10361. /* check whether if branch's stack matches its result type */
  10362. if (!check_block_stack(loader_ctx, block, error_buf,
  10363. error_buf_size))
  10364. goto fail;
  10365. block->else_addr = p - 1;
  10366. block_type = block->block_type;
  10367. #if WASM_ENABLE_GC != 0
  10368. if (!wasm_loader_init_local_use_masks(
  10369. loader_ctx, local_count, error_buf, error_buf_size)) {
  10370. goto fail;
  10371. }
  10372. #endif
  10373. #if WASM_ENABLE_FAST_INTERP != 0
  10374. /* if the result of if branch is in local or const area, add a
  10375. * copy op */
  10376. RESERVE_BLOCK_RET();
  10377. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10378. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10379. #endif
  10380. RESET_STACK();
  10381. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10382. /* Pass parameters to if-false branch */
  10383. if (BLOCK_HAS_PARAM(block_type)) {
  10384. for (i = 0; i < block_type.u.type->param_count; i++)
  10385. PUSH_TYPE(block_type.u.type->types[i]);
  10386. }
  10387. #if WASM_ENABLE_FAST_INTERP != 0
  10388. /* Recover top param_count values of frame_offset stack */
  10389. if (BLOCK_HAS_PARAM((block_type))) {
  10390. uint32 size;
  10391. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10392. bh_memcpy_s(loader_ctx->frame_offset, size,
  10393. block->param_frame_offsets, size);
  10394. loader_ctx->frame_offset += (size / sizeof(int16));
  10395. }
  10396. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10397. #endif
  10398. break;
  10399. }
  10400. case WASM_OP_END:
  10401. {
  10402. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10403. /* check whether block stack matches its result type */
  10404. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10405. error_buf_size))
  10406. goto fail;
  10407. /* if there is no else branch, make a virtual else opcode for
  10408. easier integrity check and to copy the correct results to
  10409. the block return address for fast-interp mode:
  10410. change if block from `if ... end` to `if ... else end` */
  10411. if (cur_block->label_type == LABEL_TYPE_IF
  10412. && !cur_block->else_addr) {
  10413. opcode = WASM_OP_ELSE;
  10414. p--;
  10415. #if WASM_ENABLE_FAST_INTERP != 0
  10416. p_org = p;
  10417. skip_label();
  10418. disable_emit = false;
  10419. emit_label(opcode);
  10420. #endif
  10421. goto handle_op_else;
  10422. }
  10423. POP_CSP();
  10424. #if WASM_ENABLE_FAST_INTERP != 0
  10425. skip_label();
  10426. /* copy the result to the block return address */
  10427. RESERVE_BLOCK_RET();
  10428. apply_label_patch(loader_ctx, 0, PATCH_END);
  10429. free_label_patch_list(loader_ctx->frame_csp);
  10430. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10431. int32 idx;
  10432. uint8 ret_type;
  10433. emit_label(WASM_OP_RETURN);
  10434. for (idx = (int32)func->func_type->result_count - 1;
  10435. idx >= 0; idx--) {
  10436. ret_type = *(func->func_type->types
  10437. + func->func_type->param_count + idx);
  10438. POP_OFFSET_TYPE(ret_type);
  10439. }
  10440. }
  10441. #endif
  10442. if (loader_ctx->csp_num > 0) {
  10443. loader_ctx->frame_csp->end_addr = p - 1;
  10444. }
  10445. else {
  10446. /* end of function block, function will return */
  10447. if (p < p_end) {
  10448. set_error_buf(error_buf, error_buf_size,
  10449. "section size mismatch");
  10450. goto fail;
  10451. }
  10452. }
  10453. break;
  10454. }
  10455. case WASM_OP_BR:
  10456. {
  10457. if (!(frame_csp_tmp =
  10458. check_branch_block(loader_ctx, &p, p_end, opcode,
  10459. error_buf, error_buf_size)))
  10460. goto fail;
  10461. RESET_STACK();
  10462. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10463. break;
  10464. }
  10465. case WASM_OP_BR_IF:
  10466. {
  10467. POP_I32();
  10468. if (!(frame_csp_tmp =
  10469. check_branch_block(loader_ctx, &p, p_end, opcode,
  10470. error_buf, error_buf_size)))
  10471. goto fail;
  10472. break;
  10473. }
  10474. case WASM_OP_BR_TABLE:
  10475. {
  10476. uint32 depth = 0, default_arity, arity = 0;
  10477. BranchBlock *target_block;
  10478. BlockType *target_block_type;
  10479. #if WASM_ENABLE_FAST_INTERP == 0
  10480. BrTableCache *br_table_cache = NULL;
  10481. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10482. uint32 j;
  10483. #endif
  10484. read_leb_uint32(p, p_end, count);
  10485. #if WASM_ENABLE_FAST_INTERP != 0
  10486. emit_uint32(loader_ctx, count);
  10487. #endif
  10488. POP_I32();
  10489. /* Get each depth and check it */
  10490. p_org = p;
  10491. for (i = 0; i <= count; i++) {
  10492. read_leb_uint32(p, p_end, depth);
  10493. bh_assert(loader_ctx->csp_num > 0);
  10494. if (loader_ctx->csp_num - 1 < depth) {
  10495. set_error_buf(error_buf, error_buf_size,
  10496. "unknown label, "
  10497. "unexpected end of section or function");
  10498. goto fail;
  10499. }
  10500. }
  10501. p = p_org;
  10502. /* Get the default block's arity */
  10503. target_block = loader_ctx->frame_csp - (depth + 1);
  10504. target_block_type = &target_block->block_type;
  10505. default_arity = block_type_get_arity(target_block_type,
  10506. target_block->label_type);
  10507. #if WASM_ENABLE_FAST_INTERP == 0
  10508. p_depth_begin = p_depth = p;
  10509. #endif
  10510. for (i = 0; i <= count; i++) {
  10511. p_org = p;
  10512. read_leb_uint32(p, p_end, depth);
  10513. p = p_org;
  10514. /* Get the target block's arity and check it */
  10515. target_block = loader_ctx->frame_csp - (depth + 1);
  10516. target_block_type = &target_block->block_type;
  10517. arity = block_type_get_arity(target_block_type,
  10518. target_block->label_type);
  10519. if (arity != default_arity) {
  10520. set_error_buf(error_buf, error_buf_size,
  10521. "type mismatch: br_table targets must "
  10522. "all use same result type");
  10523. goto fail;
  10524. }
  10525. if (!(frame_csp_tmp =
  10526. check_branch_block(loader_ctx, &p, p_end, opcode,
  10527. error_buf, error_buf_size))) {
  10528. goto fail;
  10529. }
  10530. #if WASM_ENABLE_FAST_INTERP == 0
  10531. if (br_table_cache) {
  10532. br_table_cache->br_depths[i] = depth;
  10533. }
  10534. else {
  10535. if (depth > 255) {
  10536. /* The depth cannot be stored in one byte,
  10537. create br_table cache to store each depth */
  10538. #if WASM_ENABLE_DEBUG_INTERP != 0
  10539. if (!record_fast_op(module, p_opcode, *p_opcode,
  10540. error_buf, error_buf_size)) {
  10541. goto fail;
  10542. }
  10543. #endif
  10544. if (!(br_table_cache = loader_malloc(
  10545. offsetof(BrTableCache, br_depths)
  10546. + sizeof(uint32)
  10547. * (uint64)(count + 1),
  10548. error_buf, error_buf_size))) {
  10549. goto fail;
  10550. }
  10551. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10552. br_table_cache->br_table_op_addr = p_opcode;
  10553. br_table_cache->br_count = count;
  10554. /* Copy previous depths which are one byte */
  10555. for (j = 0; j < i; j++) {
  10556. br_table_cache->br_depths[j] = p_depth_begin[j];
  10557. }
  10558. br_table_cache->br_depths[i] = depth;
  10559. bh_list_insert(module->br_table_cache_list,
  10560. br_table_cache);
  10561. }
  10562. else {
  10563. /* The depth can be stored in one byte, use the
  10564. byte of the leb to store it */
  10565. *p_depth++ = (uint8)depth;
  10566. }
  10567. }
  10568. #endif
  10569. }
  10570. #if WASM_ENABLE_FAST_INTERP == 0
  10571. /* Set the tailing bytes to nop */
  10572. if (br_table_cache)
  10573. p_depth = p_depth_begin;
  10574. while (p_depth < p)
  10575. *p_depth++ = WASM_OP_NOP;
  10576. #endif
  10577. RESET_STACK();
  10578. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10579. break;
  10580. }
  10581. case WASM_OP_RETURN:
  10582. {
  10583. WASMFuncType *func_type = func->func_type;
  10584. int32 idx;
  10585. uint8 ret_type;
  10586. #if WASM_ENABLE_GC != 0
  10587. uint32 j = func_type->ref_type_map_count - 1;
  10588. #endif
  10589. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10590. idx--) {
  10591. ret_type =
  10592. *(func_type->types + func_type->param_count + idx);
  10593. #if WASM_ENABLE_GC != 0
  10594. if (wasm_is_type_multi_byte_type(ret_type)) {
  10595. WASMRefType *ref_type =
  10596. func_type->ref_type_maps[j].ref_type;
  10597. bh_assert(func_type->ref_type_maps[j].index
  10598. == func_type->param_count + idx);
  10599. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10600. ref_type,
  10601. wasm_reftype_struct_size(ref_type));
  10602. j--;
  10603. }
  10604. #endif
  10605. #if WASM_ENABLE_FAST_INTERP != 0
  10606. /* emit the offset after return opcode */
  10607. POP_OFFSET_TYPE(ret_type);
  10608. #endif
  10609. POP_TYPE(ret_type);
  10610. }
  10611. RESET_STACK();
  10612. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10613. break;
  10614. }
  10615. case WASM_OP_CALL:
  10616. #if WASM_ENABLE_TAIL_CALL != 0
  10617. case WASM_OP_RETURN_CALL:
  10618. #endif
  10619. #if WASM_ENABLE_GC != 0
  10620. case WASM_OP_CALL_REF:
  10621. case WASM_OP_RETURN_CALL_REF:
  10622. #endif
  10623. {
  10624. WASMFuncType *func_type;
  10625. uint8 type;
  10626. int32 idx;
  10627. #if WASM_ENABLE_GC != 0
  10628. WASMRefType *ref_type;
  10629. uint32 type_idx1;
  10630. int32 j;
  10631. #endif
  10632. #if WASM_ENABLE_GC != 0
  10633. if (opcode == WASM_OP_CALL_REF
  10634. || opcode == WASM_OP_RETURN_CALL_REF) {
  10635. read_leb_uint32(p, p_end, type_idx1);
  10636. if (!check_type_index(module, module->type_count, type_idx1,
  10637. error_buf, error_buf_size)) {
  10638. goto fail;
  10639. }
  10640. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10641. set_error_buf(error_buf, error_buf_size,
  10642. "unkown function type");
  10643. goto fail;
  10644. }
  10645. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10646. &type_idx, error_buf,
  10647. error_buf_size)) {
  10648. goto fail;
  10649. }
  10650. if (type == VALUE_TYPE_ANY) {
  10651. type_idx = type_idx1;
  10652. }
  10653. if (!check_type_index(module, module->type_count, type_idx,
  10654. error_buf, error_buf_size)) {
  10655. goto fail;
  10656. }
  10657. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10658. set_error_buf(error_buf, error_buf_size,
  10659. "unkown function type");
  10660. goto fail;
  10661. }
  10662. if (!wasm_func_type_is_super_of(
  10663. (WASMFuncType *)module->types[type_idx1],
  10664. (WASMFuncType *)module->types[type_idx])) {
  10665. set_error_buf(error_buf, error_buf_size,
  10666. "function type mismatch");
  10667. goto fail;
  10668. }
  10669. func_type = (WASMFuncType *)module->types[type_idx];
  10670. }
  10671. else
  10672. #endif
  10673. {
  10674. read_leb_uint32(p, p_end, func_idx);
  10675. #if WASM_ENABLE_FAST_INTERP != 0
  10676. /* we need to emit func_idx before arguments */
  10677. emit_uint32(loader_ctx, func_idx);
  10678. #endif
  10679. if (!check_function_index(module, func_idx, error_buf,
  10680. error_buf_size)) {
  10681. goto fail;
  10682. }
  10683. if (func_idx < module->import_function_count)
  10684. func_type = module->import_functions[func_idx]
  10685. .u.function.func_type;
  10686. else
  10687. func_type =
  10688. module
  10689. ->functions[func_idx
  10690. - module->import_function_count]
  10691. ->func_type;
  10692. }
  10693. if (func_type->param_count > 0) {
  10694. #if WASM_ENABLE_GC != 0
  10695. j = (int32)(func_type->result_ref_type_maps
  10696. - func_type->ref_type_maps - 1);
  10697. #endif
  10698. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10699. idx--) {
  10700. #if WASM_ENABLE_GC != 0
  10701. if (wasm_is_type_multi_byte_type(
  10702. func_type->types[idx])) {
  10703. ref_type = func_type->ref_type_maps[j].ref_type;
  10704. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10705. ref_type,
  10706. wasm_reftype_struct_size(ref_type));
  10707. j--;
  10708. }
  10709. #endif
  10710. #if WASM_ENABLE_FAST_INTERP != 0
  10711. POP_OFFSET_TYPE(func_type->types[idx]);
  10712. #endif
  10713. POP_TYPE(func_type->types[idx]);
  10714. }
  10715. }
  10716. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10717. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10718. #endif
  10719. #if WASM_ENABLE_GC != 0
  10720. j = (int32)(func_type->result_ref_type_maps
  10721. - func_type->ref_type_maps);
  10722. #endif
  10723. for (i = 0; i < func_type->result_count; i++) {
  10724. #if WASM_ENABLE_GC != 0
  10725. if (wasm_is_type_multi_byte_type(
  10726. func_type->types[func_type->param_count + i])) {
  10727. ref_type = func_type->ref_type_maps[j].ref_type;
  10728. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10729. ref_type,
  10730. wasm_reftype_struct_size(ref_type));
  10731. j++;
  10732. }
  10733. #endif
  10734. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10735. #if WASM_ENABLE_FAST_INTERP != 0
  10736. /* Here we emit each return value's dynamic_offset. But
  10737. * in fact these offsets are continuous, so interpreter
  10738. * only need to get the first return value's offset.
  10739. */
  10740. PUSH_OFFSET_TYPE(
  10741. func_type->types[func_type->param_count + i]);
  10742. #endif
  10743. }
  10744. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10745. }
  10746. else {
  10747. #if WASM_ENABLE_GC == 0
  10748. if (func_type->result_count
  10749. != func->func_type->result_count) {
  10750. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10751. "type mismatch: expect ",
  10752. func->func_type->result_count,
  10753. " return values but got other");
  10754. goto fail;
  10755. }
  10756. for (i = 0; i < func_type->result_count; i++) {
  10757. type = func->func_type
  10758. ->types[func->func_type->param_count + i];
  10759. if (func_type->types[func_type->param_count + i]
  10760. != type) {
  10761. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10762. "type mismatch: expect ",
  10763. type2str(type), " but got other");
  10764. goto fail;
  10765. }
  10766. }
  10767. #else
  10768. if (!wasm_func_type_result_is_subtype_of(
  10769. func_type, func->func_type, module->types,
  10770. module->type_count)) {
  10771. set_error_buf(
  10772. error_buf, error_buf_size,
  10773. "type mismatch: invalid func result types");
  10774. goto fail;
  10775. }
  10776. #endif
  10777. RESET_STACK();
  10778. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10779. }
  10780. #endif
  10781. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10782. || WASM_ENABLE_WAMR_COMPILER != 0
  10783. func->has_op_func_call = true;
  10784. #endif
  10785. (void)type;
  10786. break;
  10787. }
  10788. /*
  10789. * if disable reference type: call_indirect typeidx, 0x00
  10790. * if enable reference type: call_indirect typeidx, tableidx
  10791. */
  10792. case WASM_OP_CALL_INDIRECT:
  10793. #if WASM_ENABLE_TAIL_CALL != 0
  10794. case WASM_OP_RETURN_CALL_INDIRECT:
  10795. #endif
  10796. {
  10797. int32 idx;
  10798. WASMFuncType *func_type;
  10799. read_leb_uint32(p, p_end, type_idx);
  10800. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10801. read_leb_uint32(p, p_end, table_idx);
  10802. #else
  10803. CHECK_BUF(p, p_end, 1);
  10804. table_idx = read_uint8(p);
  10805. #endif
  10806. if (!check_table_index(module, table_idx, error_buf,
  10807. error_buf_size)) {
  10808. goto fail;
  10809. }
  10810. #if WASM_ENABLE_FAST_INTERP != 0
  10811. /* we need to emit before arguments */
  10812. #if WASM_ENABLE_TAIL_CALL != 0
  10813. emit_byte(loader_ctx, opcode);
  10814. #endif
  10815. emit_uint32(loader_ctx, type_idx);
  10816. emit_uint32(loader_ctx, table_idx);
  10817. #endif
  10818. /* skip elem idx */
  10819. POP_I32();
  10820. if (type_idx >= module->type_count) {
  10821. set_error_buf(error_buf, error_buf_size, "unknown type");
  10822. goto fail;
  10823. }
  10824. func_type = (WASMFuncType *)module->types[type_idx];
  10825. if (func_type->param_count > 0) {
  10826. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10827. idx--) {
  10828. #if WASM_ENABLE_FAST_INTERP != 0
  10829. POP_OFFSET_TYPE(func_type->types[idx]);
  10830. #endif
  10831. POP_TYPE(func_type->types[idx]);
  10832. }
  10833. }
  10834. #if WASM_ENABLE_TAIL_CALL != 0
  10835. if (opcode == WASM_OP_CALL_INDIRECT) {
  10836. #endif
  10837. for (i = 0; i < func_type->result_count; i++) {
  10838. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10839. #if WASM_ENABLE_FAST_INTERP != 0
  10840. PUSH_OFFSET_TYPE(
  10841. func_type->types[func_type->param_count + i]);
  10842. #endif
  10843. }
  10844. #if WASM_ENABLE_TAIL_CALL != 0
  10845. }
  10846. else {
  10847. uint8 type;
  10848. if (func_type->result_count
  10849. != func->func_type->result_count) {
  10850. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10851. "type mismatch: expect ",
  10852. func->func_type->result_count,
  10853. " return values but got other");
  10854. goto fail;
  10855. }
  10856. for (i = 0; i < func_type->result_count; i++) {
  10857. type = func->func_type
  10858. ->types[func->func_type->param_count + i];
  10859. if (func_type->types[func_type->param_count + i]
  10860. != type) {
  10861. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10862. "type mismatch: expect ",
  10863. type2str(type), " but got other");
  10864. goto fail;
  10865. }
  10866. }
  10867. RESET_STACK();
  10868. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10869. }
  10870. #endif
  10871. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10872. || WASM_ENABLE_WAMR_COMPILER != 0
  10873. func->has_op_func_call = true;
  10874. #endif
  10875. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10876. func->has_op_call_indirect = true;
  10877. #endif
  10878. break;
  10879. }
  10880. case WASM_OP_DROP:
  10881. {
  10882. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10883. int32 available_stack_cell =
  10884. (int32)(loader_ctx->stack_cell_num
  10885. - cur_block->stack_cell_num);
  10886. if (available_stack_cell <= 0
  10887. && !cur_block->is_stack_polymorphic) {
  10888. set_error_buf(error_buf, error_buf_size,
  10889. "type mismatch, opcode drop was found "
  10890. "but stack was empty");
  10891. goto fail;
  10892. }
  10893. if (available_stack_cell > 0) {
  10894. #if WASM_ENABLE_GC != 0
  10895. if (wasm_is_type_multi_byte_type(
  10896. *(loader_ctx->frame_ref - 1))) {
  10897. bh_assert((int32)(loader_ctx->reftype_map_num
  10898. - cur_block->reftype_map_num)
  10899. > 0);
  10900. loader_ctx->frame_reftype_map--;
  10901. loader_ctx->reftype_map_num--;
  10902. }
  10903. #endif
  10904. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10905. loader_ctx->frame_ref--;
  10906. loader_ctx->stack_cell_num--;
  10907. #if WASM_ENABLE_FAST_INTERP != 0
  10908. skip_label();
  10909. loader_ctx->frame_offset--;
  10910. if ((*(loader_ctx->frame_offset)
  10911. > loader_ctx->start_dynamic_offset)
  10912. && (*(loader_ctx->frame_offset)
  10913. < loader_ctx->max_dynamic_offset))
  10914. loader_ctx->dynamic_offset--;
  10915. #endif
  10916. }
  10917. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10918. loader_ctx->frame_ref -= 2;
  10919. loader_ctx->stack_cell_num -= 2;
  10920. #if WASM_ENABLE_FAST_INTERP == 0
  10921. *(p - 1) = WASM_OP_DROP_64;
  10922. #endif
  10923. #if WASM_ENABLE_FAST_INTERP != 0
  10924. skip_label();
  10925. loader_ctx->frame_offset -= 2;
  10926. if ((*(loader_ctx->frame_offset)
  10927. > loader_ctx->start_dynamic_offset)
  10928. && (*(loader_ctx->frame_offset)
  10929. < loader_ctx->max_dynamic_offset))
  10930. loader_ctx->dynamic_offset -= 2;
  10931. #endif
  10932. }
  10933. #if WASM_ENABLE_SIMD != 0
  10934. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10935. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10936. loader_ctx->frame_ref -= 4;
  10937. loader_ctx->stack_cell_num -= 4;
  10938. }
  10939. #endif
  10940. #endif
  10941. else {
  10942. set_error_buf(error_buf, error_buf_size,
  10943. "type mismatch");
  10944. goto fail;
  10945. }
  10946. }
  10947. else {
  10948. #if WASM_ENABLE_FAST_INTERP != 0
  10949. skip_label();
  10950. #endif
  10951. }
  10952. break;
  10953. }
  10954. case WASM_OP_SELECT:
  10955. {
  10956. uint8 ref_type;
  10957. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10958. int32 available_stack_cell;
  10959. #if WASM_ENABLE_FAST_INTERP != 0
  10960. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10961. #endif
  10962. POP_I32();
  10963. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10964. - cur_block->stack_cell_num);
  10965. if (available_stack_cell <= 0
  10966. && !cur_block->is_stack_polymorphic) {
  10967. set_error_buf(error_buf, error_buf_size,
  10968. "type mismatch or invalid result arity, "
  10969. "opcode select was found "
  10970. "but stack was empty");
  10971. goto fail;
  10972. }
  10973. if (available_stack_cell > 0) {
  10974. switch (*(loader_ctx->frame_ref - 1)) {
  10975. case VALUE_TYPE_I32:
  10976. case VALUE_TYPE_F32:
  10977. case VALUE_TYPE_ANY:
  10978. break;
  10979. case VALUE_TYPE_I64:
  10980. case VALUE_TYPE_F64:
  10981. #if WASM_ENABLE_FAST_INTERP == 0
  10982. *(p - 1) = WASM_OP_SELECT_64;
  10983. #endif
  10984. #if WASM_ENABLE_FAST_INTERP != 0
  10985. if (loader_ctx->p_code_compiled) {
  10986. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10987. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10988. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10989. *(void **)(p_code_compiled_tmp
  10990. - sizeof(void *)) =
  10991. handle_table[opcode_tmp];
  10992. #else
  10993. #if UINTPTR_MAX == UINT64_MAX
  10994. /* emit int32 relative offset in 64-bit target
  10995. */
  10996. int32 offset =
  10997. (int32)((uint8 *)handle_table[opcode_tmp]
  10998. - (uint8 *)handle_table[0]);
  10999. *(int32 *)(p_code_compiled_tmp
  11000. - sizeof(int32)) = offset;
  11001. #else
  11002. /* emit uint32 label address in 32-bit target */
  11003. *(uint32 *)(p_code_compiled_tmp
  11004. - sizeof(uint32)) =
  11005. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11006. #endif
  11007. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11008. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11009. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11010. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11011. #else
  11012. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11013. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11014. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11015. }
  11016. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11017. break;
  11018. #if WASM_ENABLE_SIMD != 0
  11019. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11020. case VALUE_TYPE_V128:
  11021. break;
  11022. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11023. #endif /* WASM_ENABLE_SIMD != 0 */
  11024. default:
  11025. {
  11026. set_error_buf(error_buf, error_buf_size,
  11027. "type mismatch");
  11028. goto fail;
  11029. }
  11030. }
  11031. ref_type = *(loader_ctx->frame_ref - 1);
  11032. #if WASM_ENABLE_FAST_INTERP != 0
  11033. POP_OFFSET_TYPE(ref_type);
  11034. POP_TYPE(ref_type);
  11035. POP_OFFSET_TYPE(ref_type);
  11036. POP_TYPE(ref_type);
  11037. PUSH_OFFSET_TYPE(ref_type);
  11038. PUSH_TYPE(ref_type);
  11039. #else
  11040. POP2_AND_PUSH(ref_type, ref_type);
  11041. #endif
  11042. }
  11043. else {
  11044. #if WASM_ENABLE_FAST_INTERP != 0
  11045. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11046. #endif
  11047. PUSH_TYPE(VALUE_TYPE_ANY);
  11048. }
  11049. break;
  11050. }
  11051. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11052. case WASM_OP_SELECT_T:
  11053. {
  11054. uint8 vec_len, type;
  11055. #if WASM_ENABLE_GC != 0
  11056. WASMRefType *ref_type = NULL;
  11057. #endif
  11058. #if WASM_ENABLE_FAST_INTERP != 0
  11059. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11060. #endif
  11061. read_leb_uint32(p, p_end, vec_len);
  11062. if (vec_len != 1) {
  11063. /* typed select must have exactly one result */
  11064. set_error_buf(error_buf, error_buf_size,
  11065. "invalid result arity");
  11066. goto fail;
  11067. }
  11068. #if WASM_ENABLE_GC == 0
  11069. CHECK_BUF(p, p_end, 1);
  11070. type = read_uint8(p);
  11071. if (!is_value_type(type)) {
  11072. set_error_buf(error_buf, error_buf_size,
  11073. "unknown value type");
  11074. goto fail;
  11075. }
  11076. #else
  11077. p_org = p + 1;
  11078. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11079. module->type_count, &need_ref_type_map,
  11080. &wasm_ref_type, false, error_buf,
  11081. error_buf_size)) {
  11082. goto fail;
  11083. }
  11084. type = wasm_ref_type.ref_type;
  11085. if (need_ref_type_map) {
  11086. if (!(ref_type = reftype_set_insert(
  11087. module->ref_type_set, &wasm_ref_type, error_buf,
  11088. error_buf_size))) {
  11089. goto fail;
  11090. }
  11091. }
  11092. #if WASM_ENABLE_FAST_INTERP == 0
  11093. while (p_org < p) {
  11094. #if WASM_ENABLE_DEBUG_INTERP != 0
  11095. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11096. error_buf_size)) {
  11097. goto fail;
  11098. }
  11099. #endif
  11100. /* Ignore extra bytes for interpreter */
  11101. *p_org++ = WASM_OP_NOP;
  11102. }
  11103. #endif
  11104. #endif /* end of WASM_ENABLE_GC == 0 */
  11105. POP_I32();
  11106. #if WASM_ENABLE_FAST_INTERP != 0
  11107. if (loader_ctx->p_code_compiled) {
  11108. uint8 opcode_tmp = WASM_OP_SELECT;
  11109. if (type == VALUE_TYPE_V128) {
  11110. #if (WASM_ENABLE_SIMD == 0) \
  11111. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11112. set_error_buf(error_buf, error_buf_size,
  11113. "SIMD v128 type isn't supported");
  11114. goto fail;
  11115. #endif
  11116. }
  11117. else {
  11118. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11119. opcode_tmp = WASM_OP_SELECT_64;
  11120. #if WASM_ENABLE_GC != 0
  11121. if (wasm_is_type_reftype(type))
  11122. opcode_tmp = WASM_OP_SELECT_T;
  11123. #endif
  11124. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11125. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11126. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11127. handle_table[opcode_tmp];
  11128. #else
  11129. #if UINTPTR_MAX == UINT64_MAX
  11130. /* emit int32 relative offset in 64-bit target */
  11131. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11132. - (uint8 *)handle_table[0]);
  11133. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11134. offset;
  11135. #else
  11136. /* emit uint32 label address in 32-bit target */
  11137. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11138. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11139. #endif
  11140. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11141. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11142. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11143. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11144. #else
  11145. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11146. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11147. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11148. }
  11149. }
  11150. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11151. POP_REF(type);
  11152. #if WASM_ENABLE_GC != 0
  11153. if (need_ref_type_map) {
  11154. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11155. wasm_reftype_struct_size(ref_type));
  11156. }
  11157. #endif
  11158. POP_REF(type);
  11159. #if WASM_ENABLE_GC != 0
  11160. if (need_ref_type_map) {
  11161. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11162. wasm_reftype_struct_size(ref_type));
  11163. }
  11164. #endif
  11165. PUSH_REF(type);
  11166. #if WASM_ENABLE_WAMR_COMPILER != 0
  11167. module->is_ref_types_used = true;
  11168. #endif
  11169. (void)vec_len;
  11170. break;
  11171. }
  11172. /* table.get x. tables[x]. [i32] -> [t] */
  11173. /* table.set x. tables[x]. [i32 t] -> [] */
  11174. case WASM_OP_TABLE_GET:
  11175. case WASM_OP_TABLE_SET:
  11176. {
  11177. uint8 decl_ref_type;
  11178. #if WASM_ENABLE_GC != 0
  11179. WASMRefType *ref_type;
  11180. #endif
  11181. read_leb_uint32(p, p_end, table_idx);
  11182. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11183. #if WASM_ENABLE_GC != 0
  11184. (void **)&ref_type,
  11185. #else
  11186. NULL,
  11187. #endif
  11188. error_buf, error_buf_size))
  11189. goto fail;
  11190. #if WASM_ENABLE_GC != 0
  11191. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11192. bh_assert(ref_type);
  11193. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11194. ref_type, wasm_reftype_struct_size(ref_type));
  11195. }
  11196. #endif
  11197. #if WASM_ENABLE_FAST_INTERP != 0
  11198. emit_uint32(loader_ctx, table_idx);
  11199. #endif
  11200. if (opcode == WASM_OP_TABLE_GET) {
  11201. POP_I32();
  11202. #if WASM_ENABLE_FAST_INTERP != 0
  11203. PUSH_OFFSET_TYPE(decl_ref_type);
  11204. #endif
  11205. PUSH_TYPE(decl_ref_type);
  11206. }
  11207. else {
  11208. #if WASM_ENABLE_FAST_INTERP != 0
  11209. POP_OFFSET_TYPE(decl_ref_type);
  11210. #endif
  11211. POP_TYPE(decl_ref_type);
  11212. POP_I32();
  11213. }
  11214. #if WASM_ENABLE_WAMR_COMPILER != 0
  11215. module->is_ref_types_used = true;
  11216. #endif
  11217. break;
  11218. }
  11219. case WASM_OP_REF_NULL:
  11220. {
  11221. uint8 ref_type;
  11222. #if WASM_ENABLE_GC == 0
  11223. CHECK_BUF(p, p_end, 1);
  11224. ref_type = read_uint8(p);
  11225. if (ref_type != VALUE_TYPE_FUNCREF
  11226. && ref_type != VALUE_TYPE_EXTERNREF) {
  11227. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11228. goto fail;
  11229. }
  11230. #else
  11231. read_leb_int32(p, p_end, heap_type);
  11232. if (heap_type >= 0) {
  11233. if (!check_type_index(module, module->type_count, heap_type,
  11234. error_buf, error_buf_size)) {
  11235. goto fail;
  11236. }
  11237. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11238. true, heap_type);
  11239. ref_type = wasm_ref_type.ref_type;
  11240. }
  11241. else {
  11242. if (!wasm_is_valid_heap_type(heap_type)) {
  11243. set_error_buf(error_buf, error_buf_size,
  11244. "unknown type");
  11245. goto fail;
  11246. }
  11247. ref_type = (uint8)((int32)0x80 + heap_type);
  11248. }
  11249. #endif /* end of WASM_ENABLE_GC == 0 */
  11250. #if WASM_ENABLE_FAST_INTERP != 0
  11251. PUSH_OFFSET_TYPE(ref_type);
  11252. #endif
  11253. PUSH_TYPE(ref_type);
  11254. #if WASM_ENABLE_WAMR_COMPILER != 0
  11255. module->is_ref_types_used = true;
  11256. #endif
  11257. break;
  11258. }
  11259. case WASM_OP_REF_IS_NULL:
  11260. {
  11261. #if WASM_ENABLE_GC == 0
  11262. #if WASM_ENABLE_FAST_INTERP != 0
  11263. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11264. int32 block_stack_cell_num =
  11265. (int32)(loader_ctx->stack_cell_num
  11266. - cur_block->stack_cell_num);
  11267. if (block_stack_cell_num <= 0) {
  11268. if (!cur_block->is_stack_polymorphic) {
  11269. set_error_buf(
  11270. error_buf, error_buf_size,
  11271. "type mismatch: expect data but stack was empty");
  11272. goto fail;
  11273. }
  11274. }
  11275. else {
  11276. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11277. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11278. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11279. if (!wasm_loader_pop_frame_ref_offset(
  11280. loader_ctx, *(loader_ctx->frame_ref - 1),
  11281. error_buf, error_buf_size)) {
  11282. goto fail;
  11283. }
  11284. }
  11285. else {
  11286. set_error_buf(error_buf, error_buf_size,
  11287. "type mismatch");
  11288. goto fail;
  11289. }
  11290. }
  11291. #else
  11292. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11293. error_buf, error_buf_size)
  11294. && !wasm_loader_pop_frame_ref(loader_ctx,
  11295. VALUE_TYPE_EXTERNREF,
  11296. error_buf, error_buf_size)) {
  11297. goto fail;
  11298. }
  11299. #endif
  11300. #else /* else of WASM_ENABLE_GC == 0 */
  11301. uint8 type;
  11302. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11303. error_buf, error_buf_size)) {
  11304. goto fail;
  11305. }
  11306. #endif
  11307. PUSH_I32();
  11308. #if WASM_ENABLE_WAMR_COMPILER != 0
  11309. module->is_ref_types_used = true;
  11310. #endif
  11311. break;
  11312. }
  11313. case WASM_OP_REF_FUNC:
  11314. {
  11315. read_leb_uint32(p, p_end, func_idx);
  11316. if (!check_function_index(module, func_idx, error_buf,
  11317. error_buf_size)) {
  11318. goto fail;
  11319. }
  11320. /* Refer to a forward-declared function:
  11321. the function must be an import, exported, or present in
  11322. a table elem segment or global initializer to be used as
  11323. the operand to ref.func */
  11324. if (func_idx >= module->import_function_count) {
  11325. WASMTableSeg *table_seg = module->table_segments;
  11326. bool func_declared = false;
  11327. uint32 j;
  11328. for (i = 0; i < module->global_count; i++) {
  11329. if (module->globals[i].type.val_type
  11330. == VALUE_TYPE_FUNCREF
  11331. && module->globals[i].init_expr.init_expr_type
  11332. == INIT_EXPR_TYPE_FUNCREF_CONST
  11333. && module->globals[i].init_expr.u.u32 == func_idx) {
  11334. func_declared = true;
  11335. break;
  11336. }
  11337. }
  11338. if (!func_declared) {
  11339. /* Check whether the function is declared in table segs,
  11340. note that it doesn't matter whether the table seg's
  11341. mode is passive, active or declarative. */
  11342. for (i = 0; i < module->table_seg_count;
  11343. i++, table_seg++) {
  11344. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11345. #if WASM_ENABLE_GC != 0
  11346. /* elem type is (ref null? func) or
  11347. (ref null? $t) */
  11348. || ((table_seg->elem_type
  11349. == REF_TYPE_HT_NON_NULLABLE
  11350. || table_seg->elem_type
  11351. == REF_TYPE_HT_NULLABLE)
  11352. && (table_seg->elem_ref_type->ref_ht_common
  11353. .heap_type
  11354. == HEAP_TYPE_FUNC
  11355. || table_seg->elem_ref_type
  11356. ->ref_ht_common.heap_type
  11357. > 0))
  11358. #endif
  11359. ) {
  11360. for (j = 0; j < table_seg->value_count; j++) {
  11361. if (table_seg->init_values[j].u.ref_index
  11362. == func_idx) {
  11363. func_declared = true;
  11364. break;
  11365. }
  11366. }
  11367. }
  11368. }
  11369. }
  11370. if (!func_declared) {
  11371. /* Check whether the function is exported */
  11372. for (i = 0; i < module->export_count; i++) {
  11373. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11374. && module->exports[i].index == func_idx) {
  11375. func_declared = true;
  11376. break;
  11377. }
  11378. }
  11379. }
  11380. if (!func_declared) {
  11381. set_error_buf(error_buf, error_buf_size,
  11382. "undeclared function reference");
  11383. goto fail;
  11384. }
  11385. }
  11386. #if WASM_ENABLE_FAST_INTERP != 0
  11387. emit_uint32(loader_ctx, func_idx);
  11388. #endif
  11389. #if WASM_ENABLE_GC == 0
  11390. PUSH_FUNCREF();
  11391. #else
  11392. if (func_idx < module->import_function_count)
  11393. type_idx =
  11394. module->import_functions[func_idx].u.function.type_idx;
  11395. else
  11396. type_idx = module
  11397. ->functions[func_idx
  11398. - module->import_function_count]
  11399. ->type_idx;
  11400. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11401. false, type_idx);
  11402. PUSH_REF(wasm_ref_type.ref_type);
  11403. #endif
  11404. #if WASM_ENABLE_WAMR_COMPILER != 0
  11405. module->is_ref_types_used = true;
  11406. #endif
  11407. break;
  11408. }
  11409. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11410. #if WASM_ENABLE_GC != 0
  11411. case WASM_OP_REF_AS_NON_NULL:
  11412. case WASM_OP_BR_ON_NULL:
  11413. {
  11414. uint8 type;
  11415. WASMRefType ref_type;
  11416. /* POP (ref null ht) and get the converted (ref ht) */
  11417. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11418. error_buf, error_buf_size)) {
  11419. goto fail;
  11420. }
  11421. if (opcode == WASM_OP_BR_ON_NULL) {
  11422. if (!(frame_csp_tmp =
  11423. check_branch_block(loader_ctx, &p, p_end, opcode,
  11424. error_buf, error_buf_size))) {
  11425. goto fail;
  11426. }
  11427. #if WASM_ENABLE_FAST_INTERP != 0
  11428. disable_emit = true;
  11429. #endif
  11430. }
  11431. /* PUSH the converted (ref ht) */
  11432. if (type != VALUE_TYPE_ANY) {
  11433. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11434. sizeof(WASMRefType));
  11435. }
  11436. PUSH_REF(type);
  11437. break;
  11438. }
  11439. case WASM_OP_BR_ON_NON_NULL:
  11440. {
  11441. uint8 type;
  11442. WASMRefType ref_type;
  11443. uint32 available_stack_cell =
  11444. loader_ctx->stack_cell_num
  11445. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11446. /* POP (ref null ht) and get the converted (ref ht) */
  11447. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11448. error_buf, error_buf_size)) {
  11449. goto fail;
  11450. }
  11451. #if WASM_ENABLE_FAST_INTERP != 0
  11452. disable_emit = true;
  11453. #endif
  11454. /* Temporarily PUSH back (ref ht), check brach block and
  11455. then POP it */
  11456. if (available_stack_cell
  11457. > 0) { /* stack isn't in polymorphic state */
  11458. if (type != VALUE_TYPE_ANY) {
  11459. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11460. &ref_type, sizeof(WASMRefType));
  11461. }
  11462. PUSH_REF(type);
  11463. }
  11464. if (!(frame_csp_tmp =
  11465. check_branch_block(loader_ctx, &p, p_end, opcode,
  11466. error_buf, error_buf_size))) {
  11467. goto fail;
  11468. }
  11469. if (available_stack_cell
  11470. > 0) { /* stack isn't in polymorphic state */
  11471. POP_REF(type);
  11472. #if WASM_ENABLE_FAST_INTERP != 0
  11473. /* Erase the opnd offset emitted by POP_REF() */
  11474. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11475. #endif
  11476. }
  11477. break;
  11478. }
  11479. case WASM_OP_REF_EQ:
  11480. POP_REF(REF_TYPE_EQREF);
  11481. POP_REF(REF_TYPE_EQREF);
  11482. PUSH_I32();
  11483. break;
  11484. #endif /* end of WASM_ENABLE_GC != 0 */
  11485. case WASM_OP_GET_LOCAL:
  11486. {
  11487. p_org = p - 1;
  11488. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11489. PUSH_TYPE(local_type);
  11490. #if WASM_ENABLE_GC != 0
  11491. /* Cannot get a non-nullable and unset local */
  11492. if (local_idx >= param_count
  11493. && wasm_is_reftype_htref_non_nullable(local_type)
  11494. && !wasm_loader_get_local_status(loader_ctx,
  11495. local_idx - param_count)) {
  11496. set_error_buf(error_buf, error_buf_size,
  11497. "uninitialized local");
  11498. goto fail;
  11499. }
  11500. #endif
  11501. #if WASM_ENABLE_FAST_INTERP != 0
  11502. /* Get Local is optimized out */
  11503. skip_label();
  11504. disable_emit = true;
  11505. operand_offset = local_offset;
  11506. PUSH_OFFSET_TYPE(local_type);
  11507. #else
  11508. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11509. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11510. if (local_offset < 0x80
  11511. #if WASM_ENABLE_GC != 0
  11512. && !wasm_is_type_reftype(local_type)
  11513. #endif
  11514. ) {
  11515. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11516. if (is_32bit_type(local_type)) {
  11517. *p_org++ = (uint8)local_offset;
  11518. }
  11519. else {
  11520. *p_org++ = (uint8)(local_offset | 0x80);
  11521. }
  11522. while (p_org < p) {
  11523. *p_org++ = WASM_OP_NOP;
  11524. }
  11525. }
  11526. #endif
  11527. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11528. break;
  11529. }
  11530. case WASM_OP_SET_LOCAL:
  11531. {
  11532. p_org = p - 1;
  11533. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11534. #if WASM_ENABLE_FAST_INTERP != 0
  11535. if (!(preserve_referenced_local(
  11536. loader_ctx, opcode, local_offset, local_type,
  11537. &preserve_local, error_buf, error_buf_size)))
  11538. goto fail;
  11539. if (local_offset < 256
  11540. #if WASM_ENABLE_GC != 0
  11541. && !wasm_is_type_reftype(local_type)
  11542. #endif
  11543. ) {
  11544. skip_label();
  11545. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11546. if (loader_ctx->p_code_compiled)
  11547. STORE_U16(loader_ctx->p_code_compiled - 2,
  11548. local_offset);
  11549. loader_ctx->frame_offset--;
  11550. loader_ctx->dynamic_offset--;
  11551. }
  11552. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11553. if (loader_ctx->p_code_compiled)
  11554. STORE_U16(loader_ctx->p_code_compiled - 2,
  11555. local_offset);
  11556. loader_ctx->frame_offset -= 2;
  11557. loader_ctx->dynamic_offset -= 2;
  11558. }
  11559. else {
  11560. if (is_32bit_type(local_type)) {
  11561. emit_label(EXT_OP_SET_LOCAL_FAST);
  11562. emit_byte(loader_ctx, (uint8)local_offset);
  11563. }
  11564. else {
  11565. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11566. emit_byte(loader_ctx, (uint8)local_offset);
  11567. }
  11568. POP_OFFSET_TYPE(local_type);
  11569. }
  11570. }
  11571. else { /* local index larger than 255, reserve leb */
  11572. emit_uint32(loader_ctx, local_idx);
  11573. POP_OFFSET_TYPE(local_type);
  11574. }
  11575. #else
  11576. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11577. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11578. if (local_offset < 0x80
  11579. #if WASM_ENABLE_GC != 0
  11580. && !wasm_is_type_reftype(local_type)
  11581. #endif
  11582. ) {
  11583. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11584. if (is_32bit_type(local_type)) {
  11585. *p_org++ = (uint8)local_offset;
  11586. }
  11587. else {
  11588. *p_org++ = (uint8)(local_offset | 0x80);
  11589. }
  11590. while (p_org < p) {
  11591. *p_org++ = WASM_OP_NOP;
  11592. }
  11593. }
  11594. #endif
  11595. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11596. #if WASM_ENABLE_GC != 0
  11597. if (local_idx >= param_count) {
  11598. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11599. }
  11600. #endif
  11601. POP_TYPE(local_type);
  11602. break;
  11603. }
  11604. case WASM_OP_TEE_LOCAL:
  11605. {
  11606. p_org = p - 1;
  11607. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11608. #if WASM_ENABLE_FAST_INTERP != 0
  11609. /* If the stack is in polymorphic state, do fake pop and push on
  11610. offset stack to keep the depth of offset stack to be the
  11611. same with ref stack */
  11612. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11613. if (cur_block->is_stack_polymorphic) {
  11614. POP_OFFSET_TYPE(local_type);
  11615. PUSH_OFFSET_TYPE(local_type);
  11616. }
  11617. #endif
  11618. POP_TYPE(local_type);
  11619. PUSH_TYPE(local_type);
  11620. #if WASM_ENABLE_FAST_INTERP != 0
  11621. if (!(preserve_referenced_local(
  11622. loader_ctx, opcode, local_offset, local_type,
  11623. &preserve_local, error_buf, error_buf_size)))
  11624. goto fail;
  11625. if (local_offset < 256
  11626. #if WASM_ENABLE_GC != 0
  11627. && !wasm_is_type_reftype(local_type)
  11628. #endif
  11629. ) {
  11630. skip_label();
  11631. if (is_32bit_type(local_type)) {
  11632. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11633. emit_byte(loader_ctx, (uint8)local_offset);
  11634. }
  11635. else {
  11636. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11637. emit_byte(loader_ctx, (uint8)local_offset);
  11638. }
  11639. }
  11640. else { /* local index larger than 255, reserve leb */
  11641. emit_uint32(loader_ctx, local_idx);
  11642. }
  11643. emit_operand(loader_ctx,
  11644. *(loader_ctx->frame_offset
  11645. - wasm_value_type_cell_num(local_type)));
  11646. #else
  11647. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11648. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11649. if (local_offset < 0x80
  11650. #if WASM_ENABLE_GC != 0
  11651. && !wasm_is_type_reftype(local_type)
  11652. #endif
  11653. ) {
  11654. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11655. if (is_32bit_type(local_type)) {
  11656. *p_org++ = (uint8)local_offset;
  11657. }
  11658. else {
  11659. *p_org++ = (uint8)(local_offset | 0x80);
  11660. }
  11661. while (p_org < p) {
  11662. *p_org++ = WASM_OP_NOP;
  11663. }
  11664. }
  11665. #endif
  11666. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11667. #if WASM_ENABLE_GC != 0
  11668. if (local_idx >= param_count) {
  11669. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11670. }
  11671. #endif
  11672. break;
  11673. }
  11674. case WASM_OP_GET_GLOBAL:
  11675. {
  11676. #if WASM_ENABLE_GC != 0
  11677. WASMRefType *ref_type;
  11678. #endif
  11679. p_org = p - 1;
  11680. read_leb_uint32(p, p_end, global_idx);
  11681. if (global_idx >= global_count) {
  11682. set_error_buf(error_buf, error_buf_size, "unknown global");
  11683. goto fail;
  11684. }
  11685. global_type = global_idx < module->import_global_count
  11686. ? module->import_globals[global_idx]
  11687. .u.global.type.val_type
  11688. : module
  11689. ->globals[global_idx
  11690. - module->import_global_count]
  11691. .type.val_type;
  11692. #if WASM_ENABLE_GC != 0
  11693. ref_type =
  11694. global_idx < module->import_global_count
  11695. ? module->import_globals[global_idx].u.global.ref_type
  11696. : module
  11697. ->globals[global_idx
  11698. - module->import_global_count]
  11699. .ref_type;
  11700. if (wasm_is_type_multi_byte_type(global_type)) {
  11701. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11702. wasm_reftype_struct_size(ref_type));
  11703. }
  11704. #endif
  11705. PUSH_TYPE(global_type);
  11706. #if WASM_ENABLE_FAST_INTERP == 0
  11707. if (global_type == VALUE_TYPE_I64
  11708. || global_type == VALUE_TYPE_F64) {
  11709. #if WASM_ENABLE_DEBUG_INTERP != 0
  11710. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11711. error_buf_size)) {
  11712. goto fail;
  11713. }
  11714. #endif
  11715. *p_org = WASM_OP_GET_GLOBAL_64;
  11716. }
  11717. #else /* else of WASM_ENABLE_FAST_INTERP */
  11718. if (global_type == VALUE_TYPE_I64
  11719. || global_type == VALUE_TYPE_F64) {
  11720. skip_label();
  11721. emit_label(WASM_OP_GET_GLOBAL_64);
  11722. }
  11723. emit_uint32(loader_ctx, global_idx);
  11724. PUSH_OFFSET_TYPE(global_type);
  11725. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11726. break;
  11727. }
  11728. case WASM_OP_SET_GLOBAL:
  11729. {
  11730. bool is_mutable = false;
  11731. #if WASM_ENABLE_GC != 0
  11732. WASMRefType *ref_type;
  11733. #endif
  11734. p_org = p - 1;
  11735. read_leb_uint32(p, p_end, global_idx);
  11736. if (global_idx >= global_count) {
  11737. set_error_buf(error_buf, error_buf_size, "unknown global");
  11738. goto fail;
  11739. }
  11740. is_mutable = global_idx < module->import_global_count
  11741. ? module->import_globals[global_idx]
  11742. .u.global.type.is_mutable
  11743. : module
  11744. ->globals[global_idx
  11745. - module->import_global_count]
  11746. .type.is_mutable;
  11747. if (!is_mutable) {
  11748. #if WASM_ENABLE_GC == 0
  11749. set_error_buf(error_buf, error_buf_size,
  11750. "global is immutable");
  11751. #else
  11752. set_error_buf(error_buf, error_buf_size,
  11753. "immutable global");
  11754. #endif
  11755. goto fail;
  11756. }
  11757. global_type = global_idx < module->import_global_count
  11758. ? module->import_globals[global_idx]
  11759. .u.global.type.val_type
  11760. : module
  11761. ->globals[global_idx
  11762. - module->import_global_count]
  11763. .type.val_type;
  11764. #if WASM_ENABLE_GC != 0
  11765. ref_type =
  11766. global_idx < module->import_global_count
  11767. ? module->import_globals[global_idx].u.global.ref_type
  11768. : module
  11769. ->globals[global_idx
  11770. - module->import_global_count]
  11771. .ref_type;
  11772. if (wasm_is_type_multi_byte_type(global_type)) {
  11773. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11774. wasm_reftype_struct_size(ref_type));
  11775. }
  11776. #endif
  11777. #if WASM_ENABLE_FAST_INTERP == 0
  11778. if (global_type == VALUE_TYPE_I64
  11779. || global_type == VALUE_TYPE_F64) {
  11780. #if WASM_ENABLE_DEBUG_INTERP != 0
  11781. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11782. error_buf_size)) {
  11783. goto fail;
  11784. }
  11785. #endif
  11786. *p_org = WASM_OP_SET_GLOBAL_64;
  11787. }
  11788. else if (module->aux_stack_size > 0
  11789. && global_idx == module->aux_stack_top_global_index) {
  11790. #if WASM_ENABLE_DEBUG_INTERP != 0
  11791. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11792. error_buf_size)) {
  11793. goto fail;
  11794. }
  11795. #endif
  11796. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11797. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11798. func->has_op_set_global_aux_stack = true;
  11799. #endif
  11800. }
  11801. #else /* else of WASM_ENABLE_FAST_INTERP */
  11802. if (global_type == VALUE_TYPE_I64
  11803. || global_type == VALUE_TYPE_F64) {
  11804. skip_label();
  11805. emit_label(WASM_OP_SET_GLOBAL_64);
  11806. }
  11807. else if (module->aux_stack_size > 0
  11808. && global_idx == module->aux_stack_top_global_index) {
  11809. skip_label();
  11810. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11811. }
  11812. emit_uint32(loader_ctx, global_idx);
  11813. POP_OFFSET_TYPE(global_type);
  11814. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11815. POP_TYPE(global_type);
  11816. break;
  11817. }
  11818. /* load */
  11819. case WASM_OP_I32_LOAD:
  11820. case WASM_OP_I32_LOAD8_S:
  11821. case WASM_OP_I32_LOAD8_U:
  11822. case WASM_OP_I32_LOAD16_S:
  11823. case WASM_OP_I32_LOAD16_U:
  11824. case WASM_OP_I64_LOAD:
  11825. case WASM_OP_I64_LOAD8_S:
  11826. case WASM_OP_I64_LOAD8_U:
  11827. case WASM_OP_I64_LOAD16_S:
  11828. case WASM_OP_I64_LOAD16_U:
  11829. case WASM_OP_I64_LOAD32_S:
  11830. case WASM_OP_I64_LOAD32_U:
  11831. case WASM_OP_F32_LOAD:
  11832. case WASM_OP_F64_LOAD:
  11833. /* store */
  11834. case WASM_OP_I32_STORE:
  11835. case WASM_OP_I32_STORE8:
  11836. case WASM_OP_I32_STORE16:
  11837. case WASM_OP_I64_STORE:
  11838. case WASM_OP_I64_STORE8:
  11839. case WASM_OP_I64_STORE16:
  11840. case WASM_OP_I64_STORE32:
  11841. case WASM_OP_F32_STORE:
  11842. case WASM_OP_F64_STORE:
  11843. {
  11844. #if WASM_ENABLE_FAST_INTERP != 0
  11845. /* change F32/F64 into I32/I64 */
  11846. if (opcode == WASM_OP_F32_LOAD) {
  11847. skip_label();
  11848. emit_label(WASM_OP_I32_LOAD);
  11849. }
  11850. else if (opcode == WASM_OP_F64_LOAD) {
  11851. skip_label();
  11852. emit_label(WASM_OP_I64_LOAD);
  11853. }
  11854. else if (opcode == WASM_OP_F32_STORE) {
  11855. skip_label();
  11856. emit_label(WASM_OP_I32_STORE);
  11857. }
  11858. else if (opcode == WASM_OP_F64_STORE) {
  11859. skip_label();
  11860. emit_label(WASM_OP_I64_STORE);
  11861. }
  11862. #endif
  11863. CHECK_MEMORY();
  11864. read_leb_uint32(p, p_end, align); /* align */
  11865. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11866. if (!check_memory_access_align(opcode, align, error_buf,
  11867. error_buf_size)) {
  11868. goto fail;
  11869. }
  11870. #if WASM_ENABLE_FAST_INTERP != 0
  11871. emit_uint32(loader_ctx, mem_offset);
  11872. #endif
  11873. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11874. func->has_memory_operations = true;
  11875. #endif
  11876. switch (opcode) {
  11877. /* load */
  11878. case WASM_OP_I32_LOAD:
  11879. case WASM_OP_I32_LOAD8_S:
  11880. case WASM_OP_I32_LOAD8_U:
  11881. case WASM_OP_I32_LOAD16_S:
  11882. case WASM_OP_I32_LOAD16_U:
  11883. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11884. break;
  11885. case WASM_OP_I64_LOAD:
  11886. case WASM_OP_I64_LOAD8_S:
  11887. case WASM_OP_I64_LOAD8_U:
  11888. case WASM_OP_I64_LOAD16_S:
  11889. case WASM_OP_I64_LOAD16_U:
  11890. case WASM_OP_I64_LOAD32_S:
  11891. case WASM_OP_I64_LOAD32_U:
  11892. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11893. break;
  11894. case WASM_OP_F32_LOAD:
  11895. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11896. break;
  11897. case WASM_OP_F64_LOAD:
  11898. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11899. break;
  11900. /* store */
  11901. case WASM_OP_I32_STORE:
  11902. case WASM_OP_I32_STORE8:
  11903. case WASM_OP_I32_STORE16:
  11904. POP_I32();
  11905. POP_MEM_OFFSET();
  11906. break;
  11907. case WASM_OP_I64_STORE:
  11908. case WASM_OP_I64_STORE8:
  11909. case WASM_OP_I64_STORE16:
  11910. case WASM_OP_I64_STORE32:
  11911. POP_I64();
  11912. POP_MEM_OFFSET();
  11913. break;
  11914. case WASM_OP_F32_STORE:
  11915. POP_F32();
  11916. POP_MEM_OFFSET();
  11917. break;
  11918. case WASM_OP_F64_STORE:
  11919. POP_F64();
  11920. POP_MEM_OFFSET();
  11921. break;
  11922. default:
  11923. break;
  11924. }
  11925. break;
  11926. }
  11927. case WASM_OP_MEMORY_SIZE:
  11928. CHECK_MEMORY();
  11929. /* reserved byte 0x00 */
  11930. if (*p++ != 0x00) {
  11931. set_error_buf(error_buf, error_buf_size,
  11932. "zero byte expected");
  11933. goto fail;
  11934. }
  11935. PUSH_PAGE_COUNT();
  11936. module->possible_memory_grow = true;
  11937. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11938. func->has_memory_operations = true;
  11939. #endif
  11940. break;
  11941. case WASM_OP_MEMORY_GROW:
  11942. CHECK_MEMORY();
  11943. /* reserved byte 0x00 */
  11944. if (*p++ != 0x00) {
  11945. set_error_buf(error_buf, error_buf_size,
  11946. "zero byte expected");
  11947. goto fail;
  11948. }
  11949. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11950. module->possible_memory_grow = true;
  11951. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11952. || WASM_ENABLE_WAMR_COMPILER != 0
  11953. func->has_op_memory_grow = true;
  11954. #endif
  11955. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11956. func->has_memory_operations = true;
  11957. #endif
  11958. break;
  11959. case WASM_OP_I32_CONST:
  11960. read_leb_int32(p, p_end, i32_const);
  11961. #if WASM_ENABLE_FAST_INTERP != 0
  11962. skip_label();
  11963. disable_emit = true;
  11964. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11965. if (operand_offset == 0) {
  11966. disable_emit = false;
  11967. emit_label(WASM_OP_I32_CONST);
  11968. emit_uint32(loader_ctx, i32_const);
  11969. }
  11970. #else
  11971. (void)i32_const;
  11972. #endif
  11973. PUSH_I32();
  11974. break;
  11975. case WASM_OP_I64_CONST:
  11976. read_leb_int64(p, p_end, i64_const);
  11977. #if WASM_ENABLE_FAST_INTERP != 0
  11978. skip_label();
  11979. disable_emit = true;
  11980. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11981. if (operand_offset == 0) {
  11982. disable_emit = false;
  11983. emit_label(WASM_OP_I64_CONST);
  11984. emit_uint64(loader_ctx, i64_const);
  11985. }
  11986. #endif
  11987. PUSH_I64();
  11988. break;
  11989. case WASM_OP_F32_CONST:
  11990. CHECK_BUF(p, p_end, sizeof(float32));
  11991. p += sizeof(float32);
  11992. #if WASM_ENABLE_FAST_INTERP != 0
  11993. skip_label();
  11994. disable_emit = true;
  11995. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11996. sizeof(float32));
  11997. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11998. if (operand_offset == 0) {
  11999. disable_emit = false;
  12000. emit_label(WASM_OP_F32_CONST);
  12001. emit_float32(loader_ctx, f32_const);
  12002. }
  12003. #endif
  12004. PUSH_F32();
  12005. break;
  12006. case WASM_OP_F64_CONST:
  12007. CHECK_BUF(p, p_end, sizeof(float64));
  12008. p += sizeof(float64);
  12009. #if WASM_ENABLE_FAST_INTERP != 0
  12010. skip_label();
  12011. disable_emit = true;
  12012. /* Some MCU may require 8-byte align */
  12013. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12014. sizeof(float64));
  12015. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12016. if (operand_offset == 0) {
  12017. disable_emit = false;
  12018. emit_label(WASM_OP_F64_CONST);
  12019. emit_float64(loader_ctx, f64_const);
  12020. }
  12021. #endif
  12022. PUSH_F64();
  12023. break;
  12024. case WASM_OP_I32_EQZ:
  12025. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12026. break;
  12027. case WASM_OP_I32_EQ:
  12028. case WASM_OP_I32_NE:
  12029. case WASM_OP_I32_LT_S:
  12030. case WASM_OP_I32_LT_U:
  12031. case WASM_OP_I32_GT_S:
  12032. case WASM_OP_I32_GT_U:
  12033. case WASM_OP_I32_LE_S:
  12034. case WASM_OP_I32_LE_U:
  12035. case WASM_OP_I32_GE_S:
  12036. case WASM_OP_I32_GE_U:
  12037. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12038. break;
  12039. case WASM_OP_I64_EQZ:
  12040. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12041. break;
  12042. case WASM_OP_I64_EQ:
  12043. case WASM_OP_I64_NE:
  12044. case WASM_OP_I64_LT_S:
  12045. case WASM_OP_I64_LT_U:
  12046. case WASM_OP_I64_GT_S:
  12047. case WASM_OP_I64_GT_U:
  12048. case WASM_OP_I64_LE_S:
  12049. case WASM_OP_I64_LE_U:
  12050. case WASM_OP_I64_GE_S:
  12051. case WASM_OP_I64_GE_U:
  12052. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12053. break;
  12054. case WASM_OP_F32_EQ:
  12055. case WASM_OP_F32_NE:
  12056. case WASM_OP_F32_LT:
  12057. case WASM_OP_F32_GT:
  12058. case WASM_OP_F32_LE:
  12059. case WASM_OP_F32_GE:
  12060. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12061. break;
  12062. case WASM_OP_F64_EQ:
  12063. case WASM_OP_F64_NE:
  12064. case WASM_OP_F64_LT:
  12065. case WASM_OP_F64_GT:
  12066. case WASM_OP_F64_LE:
  12067. case WASM_OP_F64_GE:
  12068. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12069. break;
  12070. case WASM_OP_I32_CLZ:
  12071. case WASM_OP_I32_CTZ:
  12072. case WASM_OP_I32_POPCNT:
  12073. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12074. break;
  12075. case WASM_OP_I32_ADD:
  12076. case WASM_OP_I32_SUB:
  12077. case WASM_OP_I32_MUL:
  12078. case WASM_OP_I32_DIV_S:
  12079. case WASM_OP_I32_DIV_U:
  12080. case WASM_OP_I32_REM_S:
  12081. case WASM_OP_I32_REM_U:
  12082. case WASM_OP_I32_AND:
  12083. case WASM_OP_I32_OR:
  12084. case WASM_OP_I32_XOR:
  12085. case WASM_OP_I32_SHL:
  12086. case WASM_OP_I32_SHR_S:
  12087. case WASM_OP_I32_SHR_U:
  12088. case WASM_OP_I32_ROTL:
  12089. case WASM_OP_I32_ROTR:
  12090. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12091. break;
  12092. case WASM_OP_I64_CLZ:
  12093. case WASM_OP_I64_CTZ:
  12094. case WASM_OP_I64_POPCNT:
  12095. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12096. break;
  12097. case WASM_OP_I64_ADD:
  12098. case WASM_OP_I64_SUB:
  12099. case WASM_OP_I64_MUL:
  12100. case WASM_OP_I64_DIV_S:
  12101. case WASM_OP_I64_DIV_U:
  12102. case WASM_OP_I64_REM_S:
  12103. case WASM_OP_I64_REM_U:
  12104. case WASM_OP_I64_AND:
  12105. case WASM_OP_I64_OR:
  12106. case WASM_OP_I64_XOR:
  12107. case WASM_OP_I64_SHL:
  12108. case WASM_OP_I64_SHR_S:
  12109. case WASM_OP_I64_SHR_U:
  12110. case WASM_OP_I64_ROTL:
  12111. case WASM_OP_I64_ROTR:
  12112. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12113. break;
  12114. case WASM_OP_F32_ABS:
  12115. case WASM_OP_F32_NEG:
  12116. case WASM_OP_F32_CEIL:
  12117. case WASM_OP_F32_FLOOR:
  12118. case WASM_OP_F32_TRUNC:
  12119. case WASM_OP_F32_NEAREST:
  12120. case WASM_OP_F32_SQRT:
  12121. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12122. break;
  12123. case WASM_OP_F32_ADD:
  12124. case WASM_OP_F32_SUB:
  12125. case WASM_OP_F32_MUL:
  12126. case WASM_OP_F32_DIV:
  12127. case WASM_OP_F32_MIN:
  12128. case WASM_OP_F32_MAX:
  12129. case WASM_OP_F32_COPYSIGN:
  12130. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12131. break;
  12132. case WASM_OP_F64_ABS:
  12133. case WASM_OP_F64_NEG:
  12134. case WASM_OP_F64_CEIL:
  12135. case WASM_OP_F64_FLOOR:
  12136. case WASM_OP_F64_TRUNC:
  12137. case WASM_OP_F64_NEAREST:
  12138. case WASM_OP_F64_SQRT:
  12139. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12140. break;
  12141. case WASM_OP_F64_ADD:
  12142. case WASM_OP_F64_SUB:
  12143. case WASM_OP_F64_MUL:
  12144. case WASM_OP_F64_DIV:
  12145. case WASM_OP_F64_MIN:
  12146. case WASM_OP_F64_MAX:
  12147. case WASM_OP_F64_COPYSIGN:
  12148. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12149. break;
  12150. case WASM_OP_I32_WRAP_I64:
  12151. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12152. break;
  12153. case WASM_OP_I32_TRUNC_S_F32:
  12154. case WASM_OP_I32_TRUNC_U_F32:
  12155. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12156. break;
  12157. case WASM_OP_I32_TRUNC_S_F64:
  12158. case WASM_OP_I32_TRUNC_U_F64:
  12159. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12160. break;
  12161. case WASM_OP_I64_EXTEND_S_I32:
  12162. case WASM_OP_I64_EXTEND_U_I32:
  12163. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12164. break;
  12165. case WASM_OP_I64_TRUNC_S_F32:
  12166. case WASM_OP_I64_TRUNC_U_F32:
  12167. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12168. break;
  12169. case WASM_OP_I64_TRUNC_S_F64:
  12170. case WASM_OP_I64_TRUNC_U_F64:
  12171. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12172. break;
  12173. case WASM_OP_F32_CONVERT_S_I32:
  12174. case WASM_OP_F32_CONVERT_U_I32:
  12175. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12176. break;
  12177. case WASM_OP_F32_CONVERT_S_I64:
  12178. case WASM_OP_F32_CONVERT_U_I64:
  12179. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12180. break;
  12181. case WASM_OP_F32_DEMOTE_F64:
  12182. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12183. break;
  12184. case WASM_OP_F64_CONVERT_S_I32:
  12185. case WASM_OP_F64_CONVERT_U_I32:
  12186. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12187. break;
  12188. case WASM_OP_F64_CONVERT_S_I64:
  12189. case WASM_OP_F64_CONVERT_U_I64:
  12190. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12191. break;
  12192. case WASM_OP_F64_PROMOTE_F32:
  12193. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12194. break;
  12195. case WASM_OP_I32_REINTERPRET_F32:
  12196. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12197. break;
  12198. case WASM_OP_I64_REINTERPRET_F64:
  12199. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12200. break;
  12201. case WASM_OP_F32_REINTERPRET_I32:
  12202. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12203. break;
  12204. case WASM_OP_F64_REINTERPRET_I64:
  12205. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12206. break;
  12207. case WASM_OP_I32_EXTEND8_S:
  12208. case WASM_OP_I32_EXTEND16_S:
  12209. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12210. break;
  12211. case WASM_OP_I64_EXTEND8_S:
  12212. case WASM_OP_I64_EXTEND16_S:
  12213. case WASM_OP_I64_EXTEND32_S:
  12214. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12215. break;
  12216. #if WASM_ENABLE_GC != 0
  12217. case WASM_OP_GC_PREFIX:
  12218. {
  12219. uint32 opcode1;
  12220. read_leb_uint32(p, p_end, opcode1);
  12221. #if WASM_ENABLE_FAST_INTERP != 0
  12222. emit_byte(loader_ctx, ((uint8)opcode1));
  12223. #endif
  12224. switch (opcode1) {
  12225. case WASM_OP_STRUCT_NEW:
  12226. case WASM_OP_STRUCT_NEW_DEFAULT:
  12227. {
  12228. read_leb_uint32(p, p_end, type_idx);
  12229. #if WASM_ENABLE_FAST_INTERP != 0
  12230. emit_uint32(loader_ctx, type_idx);
  12231. #endif
  12232. if (!check_type_index(module, module->type_count,
  12233. type_idx, error_buf,
  12234. error_buf_size)) {
  12235. goto fail;
  12236. }
  12237. if (module->types[type_idx]->type_flag
  12238. != WASM_TYPE_STRUCT) {
  12239. set_error_buf(error_buf, error_buf_size,
  12240. "unkown struct type");
  12241. goto fail;
  12242. }
  12243. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12244. int32 j, k;
  12245. uint8 value_type;
  12246. uint32 ref_type_struct_size;
  12247. WASMStructType *struct_type =
  12248. (WASMStructType *)module->types[type_idx];
  12249. k = struct_type->ref_type_map_count - 1;
  12250. for (j = struct_type->field_count - 1; j >= 0;
  12251. j--) {
  12252. value_type = struct_type->fields[j].field_type;
  12253. if (wasm_is_type_reftype(value_type)) {
  12254. if (wasm_is_type_multi_byte_type(
  12255. value_type)) {
  12256. ref_type_struct_size =
  12257. wasm_reftype_struct_size(
  12258. struct_type->ref_type_maps[k]
  12259. .ref_type);
  12260. bh_memcpy_s(
  12261. &wasm_ref_type,
  12262. (uint32)sizeof(WASMRefType),
  12263. struct_type->ref_type_maps[k]
  12264. .ref_type,
  12265. ref_type_struct_size);
  12266. k--;
  12267. }
  12268. POP_REF(value_type);
  12269. }
  12270. else {
  12271. switch (value_type) {
  12272. case VALUE_TYPE_I32:
  12273. case PACKED_TYPE_I8:
  12274. case PACKED_TYPE_I16:
  12275. POP_I32();
  12276. break;
  12277. case VALUE_TYPE_I64:
  12278. POP_I64();
  12279. break;
  12280. case VALUE_TYPE_F32:
  12281. POP_F32();
  12282. break;
  12283. case VALUE_TYPE_F64:
  12284. POP_F64();
  12285. break;
  12286. default:
  12287. set_error_buf(error_buf,
  12288. error_buf_size,
  12289. "unknown type");
  12290. goto fail;
  12291. }
  12292. }
  12293. }
  12294. }
  12295. /* PUSH struct obj, (ref $t) */
  12296. wasm_set_refheaptype_typeidx(
  12297. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12298. PUSH_REF(wasm_ref_type.ref_type);
  12299. break;
  12300. }
  12301. case WASM_OP_STRUCT_GET:
  12302. case WASM_OP_STRUCT_GET_S:
  12303. case WASM_OP_STRUCT_GET_U:
  12304. case WASM_OP_STRUCT_SET:
  12305. {
  12306. WASMStructType *struct_type;
  12307. WASMRefType *ref_type = NULL;
  12308. uint32 field_idx;
  12309. uint8 field_type;
  12310. read_leb_uint32(p, p_end, type_idx);
  12311. #if WASM_ENABLE_FAST_INTERP != 0
  12312. emit_uint32(loader_ctx, type_idx);
  12313. #endif
  12314. if (!check_type_index(module, module->type_count,
  12315. type_idx, error_buf,
  12316. error_buf_size)) {
  12317. goto fail;
  12318. }
  12319. if (module->types[type_idx]->type_flag
  12320. != WASM_TYPE_STRUCT) {
  12321. set_error_buf(error_buf, error_buf_size,
  12322. "unknown struct type");
  12323. goto fail;
  12324. }
  12325. struct_type = (WASMStructType *)module->types[type_idx];
  12326. read_leb_uint32(p, p_end, field_idx);
  12327. #if WASM_ENABLE_FAST_INTERP != 0
  12328. emit_uint32(loader_ctx, field_idx);
  12329. #endif
  12330. if (field_idx >= struct_type->field_count) {
  12331. set_error_buf(error_buf, error_buf_size,
  12332. "unknown struct field");
  12333. goto fail;
  12334. }
  12335. if (opcode1 == WASM_OP_STRUCT_SET
  12336. && !(struct_type->fields[field_idx].field_flags
  12337. & 1)) {
  12338. set_error_buf(error_buf, error_buf_size,
  12339. "field is immutable");
  12340. goto fail;
  12341. }
  12342. field_type = struct_type->fields[field_idx].field_type;
  12343. if (is_packed_type(field_type)) {
  12344. if (opcode1 == WASM_OP_STRUCT_GET) {
  12345. set_error_buf(error_buf, error_buf_size,
  12346. "type mismatch");
  12347. goto fail;
  12348. }
  12349. else {
  12350. field_type = VALUE_TYPE_I32;
  12351. }
  12352. }
  12353. if (wasm_is_type_multi_byte_type(field_type)) {
  12354. ref_type = wasm_reftype_map_find(
  12355. struct_type->ref_type_maps,
  12356. struct_type->ref_type_map_count, field_idx);
  12357. bh_assert(ref_type);
  12358. }
  12359. if (opcode1 == WASM_OP_STRUCT_SET) {
  12360. /* POP field */
  12361. if (wasm_is_type_multi_byte_type(field_type)) {
  12362. bh_memcpy_s(&wasm_ref_type,
  12363. (uint32)sizeof(WASMRefType),
  12364. ref_type,
  12365. wasm_reftype_struct_size(ref_type));
  12366. }
  12367. POP_REF(field_type);
  12368. /* POP struct obj, (ref null $t) */
  12369. wasm_set_refheaptype_typeidx(
  12370. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12371. POP_REF(wasm_ref_type.ref_type);
  12372. }
  12373. else {
  12374. /* POP struct obj, (ref null $t) */
  12375. wasm_set_refheaptype_typeidx(
  12376. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12377. POP_REF(wasm_ref_type.ref_type);
  12378. /* PUSH field */
  12379. if (wasm_is_type_multi_byte_type(field_type)) {
  12380. bh_memcpy_s(&wasm_ref_type,
  12381. (uint32)sizeof(WASMRefType),
  12382. ref_type,
  12383. wasm_reftype_struct_size(ref_type));
  12384. }
  12385. PUSH_REF(field_type);
  12386. }
  12387. break;
  12388. }
  12389. case WASM_OP_ARRAY_NEW:
  12390. case WASM_OP_ARRAY_NEW_DEFAULT:
  12391. case WASM_OP_ARRAY_NEW_FIXED:
  12392. case WASM_OP_ARRAY_NEW_DATA:
  12393. case WASM_OP_ARRAY_NEW_ELEM:
  12394. {
  12395. WASMArrayType *array_type;
  12396. uint8 elem_type;
  12397. uint32 u32 = 0;
  12398. read_leb_uint32(p, p_end, type_idx);
  12399. #if WASM_ENABLE_FAST_INTERP != 0
  12400. emit_uint32(loader_ctx, type_idx);
  12401. #endif
  12402. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12403. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12404. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12405. read_leb_uint32(p, p_end, u32);
  12406. #if WASM_ENABLE_FAST_INTERP != 0
  12407. emit_uint32(loader_ctx, u32);
  12408. #endif
  12409. }
  12410. if (!check_array_type(module, type_idx, error_buf,
  12411. error_buf_size)) {
  12412. goto fail;
  12413. }
  12414. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12415. /* length */
  12416. POP_I32();
  12417. }
  12418. array_type = (WASMArrayType *)module->types[type_idx];
  12419. elem_type = array_type->elem_type;
  12420. if (opcode1 == WASM_OP_ARRAY_NEW
  12421. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12422. if (wasm_is_type_multi_byte_type(elem_type)) {
  12423. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12424. array_type->elem_ref_type,
  12425. wasm_reftype_struct_size(
  12426. array_type->elem_ref_type));
  12427. }
  12428. if (is_packed_type(elem_type)) {
  12429. elem_type = VALUE_TYPE_I32;
  12430. }
  12431. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12432. uint32 N = u32;
  12433. for (i = 0; i < N; i++) {
  12434. if (wasm_is_type_multi_byte_type(
  12435. elem_type)) {
  12436. bh_memcpy_s(
  12437. &wasm_ref_type, sizeof(WASMRefType),
  12438. array_type->elem_ref_type,
  12439. wasm_reftype_struct_size(
  12440. array_type->elem_ref_type));
  12441. }
  12442. POP_REF(elem_type);
  12443. }
  12444. }
  12445. else
  12446. POP_REF(elem_type);
  12447. }
  12448. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12449. /* offset of data segment */
  12450. POP_I32();
  12451. if (u32 >= module->data_seg_count) {
  12452. set_error_buf(error_buf, error_buf_size,
  12453. "unknown data segment");
  12454. goto fail;
  12455. }
  12456. if (wasm_is_type_reftype(elem_type)) {
  12457. set_error_buf(error_buf, error_buf_size,
  12458. "array elem type mismatch");
  12459. goto fail;
  12460. }
  12461. }
  12462. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12463. WASMTableSeg *table_seg =
  12464. module->table_segments + u32;
  12465. /* offset of element segment */
  12466. POP_I32();
  12467. if (u32 >= module->table_seg_count) {
  12468. set_error_buf(error_buf, error_buf_size,
  12469. "unknown element segment");
  12470. goto fail;
  12471. }
  12472. if (!wasm_reftype_is_subtype_of(
  12473. table_seg->elem_type,
  12474. table_seg->elem_ref_type, elem_type,
  12475. array_type->elem_ref_type, module->types,
  12476. module->type_count)) {
  12477. set_error_buf(error_buf, error_buf_size,
  12478. "array elem type mismatch");
  12479. goto fail;
  12480. }
  12481. }
  12482. /* PUSH array obj, (ref $t) */
  12483. wasm_set_refheaptype_typeidx(
  12484. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12485. PUSH_REF(wasm_ref_type.ref_type);
  12486. break;
  12487. }
  12488. case WASM_OP_ARRAY_GET:
  12489. case WASM_OP_ARRAY_GET_S:
  12490. case WASM_OP_ARRAY_GET_U:
  12491. case WASM_OP_ARRAY_SET:
  12492. {
  12493. uint8 elem_type;
  12494. WASMArrayType *array_type;
  12495. WASMRefType *ref_type = NULL;
  12496. read_leb_uint32(p, p_end, type_idx);
  12497. #if WASM_ENABLE_FAST_INTERP != 0
  12498. emit_uint32(loader_ctx, type_idx);
  12499. #endif
  12500. if (!check_array_type(module, type_idx, error_buf,
  12501. error_buf_size)) {
  12502. goto fail;
  12503. }
  12504. array_type = (WASMArrayType *)module->types[type_idx];
  12505. if (opcode1 == WASM_OP_ARRAY_SET
  12506. && !(array_type->elem_flags & 1)) {
  12507. set_error_buf(error_buf, error_buf_size,
  12508. "array is immutable");
  12509. goto fail;
  12510. }
  12511. elem_type = array_type->elem_type;
  12512. if (is_packed_type(elem_type)) {
  12513. if (opcode1 != WASM_OP_ARRAY_GET_S
  12514. && opcode1 != WASM_OP_ARRAY_GET_U
  12515. && opcode1 != WASM_OP_ARRAY_SET) {
  12516. set_error_buf(error_buf, error_buf_size,
  12517. "type mismatch");
  12518. goto fail;
  12519. }
  12520. else {
  12521. elem_type = VALUE_TYPE_I32;
  12522. }
  12523. }
  12524. ref_type = array_type->elem_ref_type;
  12525. if (opcode1 == WASM_OP_ARRAY_SET) {
  12526. /* POP elem to set */
  12527. if (wasm_is_type_multi_byte_type(elem_type)) {
  12528. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12529. ref_type,
  12530. wasm_reftype_struct_size(ref_type));
  12531. }
  12532. POP_REF(elem_type);
  12533. }
  12534. /* elem idx */
  12535. POP_I32();
  12536. /* POP array obj, (ref null $t) */
  12537. wasm_set_refheaptype_typeidx(
  12538. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12539. POP_REF(wasm_ref_type.ref_type);
  12540. if (opcode1 != WASM_OP_ARRAY_SET) {
  12541. /* PUSH elem */
  12542. if (wasm_is_type_multi_byte_type(elem_type)) {
  12543. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12544. ref_type,
  12545. wasm_reftype_struct_size(ref_type));
  12546. }
  12547. PUSH_REF(elem_type);
  12548. }
  12549. break;
  12550. }
  12551. case WASM_OP_ARRAY_LEN:
  12552. {
  12553. POP_REF(REF_TYPE_ARRAYREF);
  12554. /* length */
  12555. PUSH_I32();
  12556. break;
  12557. }
  12558. case WASM_OP_ARRAY_FILL:
  12559. {
  12560. WASMArrayType *array_type;
  12561. uint8 elem_type;
  12562. /* typeidx */
  12563. read_leb_uint32(p, p_end, type_idx);
  12564. #if WASM_ENABLE_FAST_INTERP != 0
  12565. emit_uint32(loader_ctx, type_idx);
  12566. #endif
  12567. if (!check_array_type(module, type_idx, error_buf,
  12568. error_buf_size)) {
  12569. goto fail;
  12570. }
  12571. array_type = (WASMArrayType *)module->types[type_idx];
  12572. if (!(array_type->elem_flags & 1)) {
  12573. set_error_buf(error_buf, error_buf_size,
  12574. "array is immutable");
  12575. goto fail;
  12576. }
  12577. elem_type = array_type->elem_type;
  12578. if (is_packed_type(elem_type)) {
  12579. elem_type = VALUE_TYPE_I32;
  12580. }
  12581. POP_I32(); /* length */
  12582. #if WASM_ENABLE_FAST_INTERP != 0
  12583. POP_OFFSET_TYPE(elem_type);
  12584. #endif
  12585. POP_TYPE(elem_type);
  12586. POP_I32(); /* start */
  12587. /* POP array obj, (ref null $t) */
  12588. wasm_set_refheaptype_typeidx(
  12589. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12590. POP_REF(wasm_ref_type.ref_type);
  12591. break;
  12592. }
  12593. case WASM_OP_ARRAY_COPY:
  12594. {
  12595. uint32 src_type_idx;
  12596. uint8 src_elem_type, dst_elem_type;
  12597. WASMRefType src_ref_type = { 0 },
  12598. *src_elem_ref_type = NULL;
  12599. WASMRefType dst_ref_type = { 0 },
  12600. *dst_elem_ref_type = NULL;
  12601. WASMArrayType *array_type;
  12602. /* typeidx1 */
  12603. read_leb_uint32(p, p_end, type_idx);
  12604. #if WASM_ENABLE_FAST_INTERP != 0
  12605. emit_uint32(loader_ctx, type_idx);
  12606. #endif
  12607. /* typeidx2 */
  12608. read_leb_uint32(p, p_end, src_type_idx);
  12609. #if WASM_ENABLE_FAST_INTERP != 0
  12610. emit_uint32(loader_ctx, src_type_idx);
  12611. #endif
  12612. if (!check_array_type(module, type_idx, error_buf,
  12613. error_buf_size)) {
  12614. goto fail;
  12615. }
  12616. if (!check_array_type(module, src_type_idx, error_buf,
  12617. error_buf_size)) {
  12618. goto fail;
  12619. }
  12620. POP_I32();
  12621. POP_I32();
  12622. /* POP array obj, (ref null $t) */
  12623. wasm_set_refheaptype_typeidx(
  12624. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12625. POP_REF(wasm_ref_type.ref_type);
  12626. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12627. &wasm_ref_type,
  12628. wasm_reftype_struct_size(&wasm_ref_type));
  12629. POP_I32();
  12630. /* POP array obj, (ref null $t) */
  12631. wasm_set_refheaptype_typeidx(
  12632. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12633. POP_REF(wasm_ref_type.ref_type);
  12634. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12635. &wasm_ref_type,
  12636. wasm_reftype_struct_size(&wasm_ref_type));
  12637. array_type = (WASMArrayType *)module->types[type_idx];
  12638. if (!(array_type->elem_flags & 1)) {
  12639. set_error_buf(error_buf, error_buf_size,
  12640. "destination array is immutable");
  12641. goto fail;
  12642. }
  12643. dst_elem_type = array_type->elem_type;
  12644. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12645. dst_elem_ref_type = array_type->elem_ref_type;
  12646. }
  12647. array_type =
  12648. (WASMArrayType *)module->types[src_type_idx];
  12649. src_elem_type = array_type->elem_type;
  12650. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12651. src_elem_ref_type = array_type->elem_ref_type;
  12652. }
  12653. if (!wasm_reftype_is_subtype_of(
  12654. src_elem_type, src_elem_ref_type, dst_elem_type,
  12655. dst_elem_ref_type, module->types,
  12656. module->type_count)) {
  12657. set_error_buf(error_buf, error_buf_size,
  12658. "array types do not match");
  12659. goto fail;
  12660. }
  12661. break;
  12662. }
  12663. case WASM_OP_REF_I31:
  12664. {
  12665. POP_I32();
  12666. wasm_set_refheaptype_common(
  12667. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12668. PUSH_REF(wasm_ref_type.ref_type);
  12669. break;
  12670. }
  12671. case WASM_OP_I31_GET_S:
  12672. case WASM_OP_I31_GET_U:
  12673. {
  12674. POP_REF(REF_TYPE_I31REF);
  12675. PUSH_I32();
  12676. break;
  12677. }
  12678. case WASM_OP_REF_TEST:
  12679. case WASM_OP_REF_CAST:
  12680. case WASM_OP_REF_TEST_NULLABLE:
  12681. case WASM_OP_REF_CAST_NULLABLE:
  12682. {
  12683. uint8 type;
  12684. read_leb_int32(p, p_end, heap_type);
  12685. #if WASM_ENABLE_FAST_INTERP != 0
  12686. emit_uint32(loader_ctx, (uint32)heap_type);
  12687. #endif
  12688. if (heap_type >= 0) {
  12689. if (!check_type_index(module, module->type_count,
  12690. heap_type, error_buf,
  12691. error_buf_size)) {
  12692. goto fail;
  12693. }
  12694. }
  12695. else {
  12696. if (!wasm_is_valid_heap_type(heap_type)) {
  12697. set_error_buf(error_buf, error_buf_size,
  12698. "unknown type");
  12699. goto fail;
  12700. }
  12701. }
  12702. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12703. &wasm_ref_type, error_buf,
  12704. error_buf_size)) {
  12705. goto fail;
  12706. }
  12707. if (opcode1 == WASM_OP_REF_TEST
  12708. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12709. PUSH_I32();
  12710. else {
  12711. bool nullable =
  12712. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12713. : false;
  12714. if (heap_type >= 0 || !nullable) {
  12715. wasm_set_refheaptype_typeidx(
  12716. &wasm_ref_type.ref_ht_typeidx, nullable,
  12717. heap_type);
  12718. PUSH_REF(wasm_ref_type.ref_type);
  12719. }
  12720. else {
  12721. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12722. }
  12723. }
  12724. break;
  12725. }
  12726. case WASM_OP_BR_ON_CAST:
  12727. case WASM_OP_BR_ON_CAST_FAIL:
  12728. {
  12729. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12730. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12731. uint8 type_tmp, castflags;
  12732. uint32 depth;
  12733. int32 heap_type_dst;
  12734. bool src_nullable, dst_nullable;
  12735. CHECK_BUF(p, p_end, 1);
  12736. castflags = read_uint8(p);
  12737. #if WASM_ENABLE_FAST_INTERP != 0
  12738. /* Emit heap_type firstly */
  12739. emit_byte(loader_ctx, castflags);
  12740. #endif
  12741. p_org = p;
  12742. read_leb_uint32(p, p_end, depth);
  12743. read_leb_int32(p, p_end, heap_type);
  12744. #if WASM_ENABLE_FAST_INTERP != 0
  12745. /* Emit heap_type firstly */
  12746. emit_uint32(loader_ctx, (uint32)heap_type);
  12747. #endif
  12748. read_leb_int32(p, p_end, heap_type_dst);
  12749. #if WASM_ENABLE_FAST_INTERP != 0
  12750. /* Emit heap_type firstly */
  12751. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12752. #endif
  12753. (void)depth;
  12754. /*
  12755. * castflags should be 0~3:
  12756. * 0: (non-null, non-null)
  12757. * 1: (null, non-null)
  12758. * 2: (non-null, null)
  12759. * 3: (null, null)
  12760. */
  12761. if (castflags > 3) {
  12762. set_error_buf(error_buf, error_buf_size,
  12763. "invalid castflags");
  12764. break;
  12765. }
  12766. src_nullable =
  12767. (castflags == 1) || (castflags == 3) ? true : false;
  12768. dst_nullable =
  12769. (castflags == 2) || (castflags == 3) ? true : false;
  12770. /* Pop and backup the stack top's ref type */
  12771. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12772. &ref_type_tmp, error_buf,
  12773. error_buf_size)) {
  12774. goto fail;
  12775. }
  12776. /* The reference type rt1 must be valid */
  12777. if (!init_ref_type(module, &ref_type1, src_nullable,
  12778. heap_type, error_buf,
  12779. error_buf_size)) {
  12780. goto fail;
  12781. }
  12782. /* The reference type rt2 must be valid. */
  12783. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12784. heap_type_dst, error_buf,
  12785. error_buf_size)) {
  12786. goto fail;
  12787. }
  12788. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12789. &ref_type2);
  12790. /* The reference type rt2 must match rt1. */
  12791. if (!wasm_reftype_is_subtype_of(
  12792. ref_type2.ref_type, &ref_type2,
  12793. ref_type1.ref_type, &ref_type1, module->types,
  12794. module->type_count)) {
  12795. set_error_buf(error_buf, error_buf_size,
  12796. "type mismatch");
  12797. goto fail;
  12798. }
  12799. p = p_org;
  12800. /* Push ref type casted for branch block check */
  12801. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12802. /* The reference type rt2 must match rt′. */
  12803. type_tmp = ref_type2.ref_type;
  12804. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12805. bh_memcpy_s(
  12806. &wasm_ref_type,
  12807. wasm_reftype_struct_size(&ref_type2),
  12808. &ref_type2,
  12809. wasm_reftype_struct_size(&ref_type2));
  12810. }
  12811. }
  12812. else {
  12813. /* The reference type rt′1 must match rt′. */
  12814. type_tmp = ref_type_diff.ref_type;
  12815. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12816. bh_memcpy_s(
  12817. &wasm_ref_type,
  12818. wasm_reftype_struct_size(&ref_type_diff),
  12819. &ref_type_diff,
  12820. wasm_reftype_struct_size(&ref_type_diff));
  12821. }
  12822. }
  12823. PUSH_REF(type_tmp);
  12824. if (!(frame_csp_tmp = check_branch_block(
  12825. loader_ctx, &p, p_end, opcode, error_buf,
  12826. error_buf_size))) {
  12827. goto fail;
  12828. }
  12829. /* Ignore heap_types */
  12830. skip_leb_uint32(p, p_end);
  12831. skip_leb_uint32(p, p_end);
  12832. /* Restore the original stack top's ref type */
  12833. POP_REF(type_tmp);
  12834. #if WASM_ENABLE_FAST_INTERP != 0
  12835. /* Erase the opnd offset emitted by POP_REF() */
  12836. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12837. #endif
  12838. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12839. type_tmp = ref_type_diff.ref_type;
  12840. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12841. bh_memcpy_s(
  12842. &wasm_ref_type,
  12843. wasm_reftype_struct_size(&ref_type_diff),
  12844. &ref_type_diff,
  12845. wasm_reftype_struct_size(&ref_type_diff));
  12846. }
  12847. }
  12848. else {
  12849. type_tmp = ref_type2.ref_type;
  12850. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12851. bh_memcpy_s(
  12852. &wasm_ref_type,
  12853. wasm_reftype_struct_size(&ref_type2),
  12854. &ref_type2,
  12855. wasm_reftype_struct_size(&ref_type2));
  12856. }
  12857. }
  12858. PUSH_REF(type_tmp);
  12859. #if WASM_ENABLE_FAST_INTERP != 0
  12860. /* Erase the opnd offset emitted by PUSH_REF() */
  12861. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12862. #endif
  12863. break;
  12864. }
  12865. case WASM_OP_ANY_CONVERT_EXTERN:
  12866. {
  12867. uint8 type;
  12868. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12869. &wasm_ref_type, error_buf,
  12870. error_buf_size)) {
  12871. goto fail;
  12872. }
  12873. if (!(type == REF_TYPE_EXTERNREF
  12874. || (type == REF_TYPE_HT_NON_NULLABLE
  12875. && wasm_ref_type.ref_ht_common.heap_type
  12876. == HEAP_TYPE_EXTERN)
  12877. || type == VALUE_TYPE_ANY)) {
  12878. set_error_buf(error_buf, error_buf_size,
  12879. "type mismatch");
  12880. goto fail;
  12881. }
  12882. if (type == REF_TYPE_EXTERNREF)
  12883. type = REF_TYPE_ANYREF;
  12884. else {
  12885. wasm_ref_type.ref_ht_common.heap_type =
  12886. HEAP_TYPE_ANY;
  12887. }
  12888. PUSH_REF(type);
  12889. break;
  12890. }
  12891. case WASM_OP_EXTERN_CONVERT_ANY:
  12892. {
  12893. uint8 type;
  12894. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12895. &wasm_ref_type, error_buf,
  12896. error_buf_size)) {
  12897. goto fail;
  12898. }
  12899. if (type == REF_TYPE_EXTERNREF
  12900. || ((type == REF_TYPE_HT_NULLABLE
  12901. || type == REF_TYPE_HT_NON_NULLABLE)
  12902. && wasm_ref_type.ref_ht_common.heap_type
  12903. == HEAP_TYPE_EXTERN)) {
  12904. set_error_buf(error_buf, error_buf_size,
  12905. "type mismatch");
  12906. goto fail;
  12907. }
  12908. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12909. /* push (ref null extern) */
  12910. type = REF_TYPE_EXTERNREF;
  12911. }
  12912. else {
  12913. /* push (ref extern) */
  12914. type = REF_TYPE_HT_NON_NULLABLE;
  12915. wasm_set_refheaptype_common(
  12916. &wasm_ref_type.ref_ht_common, false,
  12917. HEAP_TYPE_EXTERN);
  12918. }
  12919. PUSH_REF(type);
  12920. break;
  12921. }
  12922. #if WASM_ENABLE_STRINGREF != 0
  12923. case WASM_OP_STRING_NEW_UTF8:
  12924. case WASM_OP_STRING_NEW_WTF16:
  12925. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12926. case WASM_OP_STRING_NEW_WTF8:
  12927. {
  12928. uint32 memidx;
  12929. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12930. func->has_memory_operations = true;
  12931. #endif
  12932. read_leb_uint32(p, p_end, memidx);
  12933. #if WASM_ENABLE_FAST_INTERP != 0
  12934. emit_uint32(loader_ctx, (uint32)memidx);
  12935. #endif
  12936. POP_I32();
  12937. POP_I32();
  12938. PUSH_REF(REF_TYPE_STRINGREF);
  12939. (void)memidx;
  12940. break;
  12941. }
  12942. case WASM_OP_STRING_CONST:
  12943. {
  12944. uint32 contents;
  12945. read_leb_uint32(p, p_end, contents);
  12946. #if WASM_ENABLE_FAST_INTERP != 0
  12947. emit_uint32(loader_ctx, (uint32)contents);
  12948. #endif
  12949. PUSH_REF(REF_TYPE_STRINGREF);
  12950. (void)contents;
  12951. break;
  12952. }
  12953. case WASM_OP_STRING_MEASURE_UTF8:
  12954. case WASM_OP_STRING_MEASURE_WTF8:
  12955. case WASM_OP_STRING_MEASURE_WTF16:
  12956. {
  12957. POP_STRINGREF();
  12958. PUSH_I32();
  12959. break;
  12960. }
  12961. case WASM_OP_STRING_ENCODE_UTF8:
  12962. case WASM_OP_STRING_ENCODE_WTF16:
  12963. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12964. case WASM_OP_STRING_ENCODE_WTF8:
  12965. {
  12966. uint32 memidx;
  12967. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12968. func->has_memory_operations = true;
  12969. #endif
  12970. read_leb_uint32(p, p_end, memidx);
  12971. #if WASM_ENABLE_FAST_INTERP != 0
  12972. emit_uint32(loader_ctx, (uint32)memidx);
  12973. #endif
  12974. POP_I32();
  12975. POP_STRINGREF();
  12976. PUSH_I32();
  12977. (void)memidx;
  12978. break;
  12979. }
  12980. case WASM_OP_STRING_CONCAT:
  12981. {
  12982. POP_STRINGREF();
  12983. POP_STRINGREF();
  12984. PUSH_REF(REF_TYPE_STRINGREF);
  12985. break;
  12986. }
  12987. case WASM_OP_STRING_EQ:
  12988. {
  12989. POP_STRINGREF();
  12990. POP_STRINGREF();
  12991. PUSH_I32();
  12992. break;
  12993. }
  12994. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12995. {
  12996. POP_STRINGREF();
  12997. PUSH_I32();
  12998. break;
  12999. }
  13000. case WASM_OP_STRING_AS_WTF8:
  13001. {
  13002. POP_STRINGREF();
  13003. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13004. break;
  13005. }
  13006. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13007. {
  13008. POP_I32();
  13009. POP_I32();
  13010. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13011. PUSH_I32();
  13012. break;
  13013. }
  13014. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13015. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13016. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13017. {
  13018. uint32 memidx;
  13019. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13020. func->has_memory_operations = true;
  13021. #endif
  13022. read_leb_uint32(p, p_end, memidx);
  13023. #if WASM_ENABLE_FAST_INTERP != 0
  13024. emit_uint32(loader_ctx, (uint32)memidx);
  13025. #endif
  13026. POP_I32();
  13027. POP_I32();
  13028. POP_I32();
  13029. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13030. PUSH_I32();
  13031. PUSH_I32();
  13032. (void)memidx;
  13033. break;
  13034. }
  13035. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13036. {
  13037. POP_I32();
  13038. POP_I32();
  13039. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13040. PUSH_REF(REF_TYPE_STRINGREF);
  13041. break;
  13042. }
  13043. case WASM_OP_STRING_AS_WTF16:
  13044. {
  13045. POP_STRINGREF();
  13046. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13047. break;
  13048. }
  13049. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13050. {
  13051. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13052. PUSH_I32();
  13053. break;
  13054. }
  13055. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13056. {
  13057. POP_I32();
  13058. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13059. PUSH_I32();
  13060. break;
  13061. }
  13062. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13063. {
  13064. uint32 memidx;
  13065. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13066. func->has_memory_operations = true;
  13067. #endif
  13068. read_leb_uint32(p, p_end, memidx);
  13069. #if WASM_ENABLE_FAST_INTERP != 0
  13070. emit_uint32(loader_ctx, (uint32)memidx);
  13071. #endif
  13072. POP_I32();
  13073. POP_I32();
  13074. POP_I32();
  13075. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13076. PUSH_I32();
  13077. (void)memidx;
  13078. break;
  13079. }
  13080. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13081. {
  13082. POP_I32();
  13083. POP_I32();
  13084. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13085. PUSH_REF(REF_TYPE_STRINGREF);
  13086. break;
  13087. }
  13088. case WASM_OP_STRING_AS_ITER:
  13089. {
  13090. POP_STRINGREF();
  13091. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13092. break;
  13093. }
  13094. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13095. {
  13096. POP_REF(REF_TYPE_STRINGVIEWITER);
  13097. PUSH_I32();
  13098. break;
  13099. }
  13100. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13101. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13102. {
  13103. POP_I32();
  13104. POP_REF(REF_TYPE_STRINGVIEWITER);
  13105. PUSH_I32();
  13106. break;
  13107. }
  13108. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13109. {
  13110. POP_I32();
  13111. POP_REF(REF_TYPE_STRINGVIEWITER);
  13112. PUSH_REF(REF_TYPE_STRINGREF);
  13113. break;
  13114. }
  13115. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13116. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13117. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13118. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13119. {
  13120. POP_I32();
  13121. POP_I32();
  13122. POP_REF(REF_TYPE_ARRAYREF);
  13123. PUSH_REF(REF_TYPE_STRINGREF);
  13124. break;
  13125. }
  13126. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13127. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13128. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13129. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13130. {
  13131. POP_I32();
  13132. POP_REF(REF_TYPE_ARRAYREF);
  13133. POP_STRINGREF();
  13134. PUSH_I32();
  13135. break;
  13136. }
  13137. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13138. default:
  13139. set_error_buf_v(error_buf, error_buf_size,
  13140. "%s %02x %02x", "unsupported opcode",
  13141. 0xfb, opcode1);
  13142. goto fail;
  13143. }
  13144. break;
  13145. }
  13146. #endif /* end of WASM_ENABLE_GC != 0 */
  13147. case WASM_OP_MISC_PREFIX:
  13148. {
  13149. uint32 opcode1;
  13150. read_leb_uint32(p, p_end, opcode1);
  13151. #if WASM_ENABLE_FAST_INTERP != 0
  13152. emit_byte(loader_ctx, ((uint8)opcode1));
  13153. #endif
  13154. switch (opcode1) {
  13155. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13156. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13157. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13158. break;
  13159. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13160. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13161. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13162. break;
  13163. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13164. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13165. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13166. break;
  13167. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13168. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13169. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13170. break;
  13171. #if WASM_ENABLE_BULK_MEMORY != 0
  13172. case WASM_OP_MEMORY_INIT:
  13173. {
  13174. read_leb_uint32(p, p_end, data_seg_idx);
  13175. #if WASM_ENABLE_FAST_INTERP != 0
  13176. emit_uint32(loader_ctx, data_seg_idx);
  13177. #endif
  13178. if (module->import_memory_count == 0
  13179. && module->memory_count == 0)
  13180. goto fail_unknown_memory;
  13181. if (*p++ != 0x00)
  13182. goto fail_zero_byte_expected;
  13183. if (data_seg_idx >= module->data_seg_count) {
  13184. set_error_buf_v(error_buf, error_buf_size,
  13185. "unknown data segment %d",
  13186. data_seg_idx);
  13187. goto fail;
  13188. }
  13189. if (module->data_seg_count1 == 0)
  13190. goto fail_data_cnt_sec_require;
  13191. POP_I32();
  13192. POP_I32();
  13193. POP_MEM_OFFSET();
  13194. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13195. func->has_memory_operations = true;
  13196. #endif
  13197. #if WASM_ENABLE_WAMR_COMPILER != 0
  13198. module->is_bulk_memory_used = true;
  13199. #endif
  13200. break;
  13201. }
  13202. case WASM_OP_DATA_DROP:
  13203. {
  13204. read_leb_uint32(p, p_end, data_seg_idx);
  13205. #if WASM_ENABLE_FAST_INTERP != 0
  13206. emit_uint32(loader_ctx, data_seg_idx);
  13207. #endif
  13208. if (data_seg_idx >= module->data_seg_count) {
  13209. set_error_buf(error_buf, error_buf_size,
  13210. "unknown data segment");
  13211. goto fail;
  13212. }
  13213. if (module->data_seg_count1 == 0)
  13214. goto fail_data_cnt_sec_require;
  13215. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13216. func->has_memory_operations = true;
  13217. #endif
  13218. #if WASM_ENABLE_WAMR_COMPILER != 0
  13219. module->is_bulk_memory_used = true;
  13220. #endif
  13221. break;
  13222. }
  13223. case WASM_OP_MEMORY_COPY:
  13224. {
  13225. CHECK_BUF(p, p_end, sizeof(int16));
  13226. /* both src and dst memory index should be 0 */
  13227. if (*(int16 *)p != 0x0000)
  13228. goto fail_zero_byte_expected;
  13229. p += 2;
  13230. if (module->import_memory_count == 0
  13231. && module->memory_count == 0)
  13232. goto fail_unknown_memory;
  13233. POP_MEM_OFFSET();
  13234. POP_MEM_OFFSET();
  13235. POP_MEM_OFFSET();
  13236. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13237. func->has_memory_operations = true;
  13238. #endif
  13239. #if WASM_ENABLE_WAMR_COMPILER != 0
  13240. module->is_bulk_memory_used = true;
  13241. #endif
  13242. break;
  13243. }
  13244. case WASM_OP_MEMORY_FILL:
  13245. {
  13246. if (*p++ != 0x00) {
  13247. goto fail_zero_byte_expected;
  13248. }
  13249. if (module->import_memory_count == 0
  13250. && module->memory_count == 0) {
  13251. goto fail_unknown_memory;
  13252. }
  13253. POP_MEM_OFFSET();
  13254. POP_I32();
  13255. POP_MEM_OFFSET();
  13256. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13257. func->has_memory_operations = true;
  13258. #endif
  13259. #if WASM_ENABLE_WAMR_COMPILER != 0
  13260. module->is_bulk_memory_used = true;
  13261. #endif
  13262. break;
  13263. }
  13264. fail_zero_byte_expected:
  13265. set_error_buf(error_buf, error_buf_size,
  13266. "zero byte expected");
  13267. goto fail;
  13268. fail_unknown_memory:
  13269. set_error_buf(error_buf, error_buf_size,
  13270. "unknown memory 0");
  13271. goto fail;
  13272. fail_data_cnt_sec_require:
  13273. set_error_buf(error_buf, error_buf_size,
  13274. "data count section required");
  13275. goto fail;
  13276. #endif /* WASM_ENABLE_BULK_MEMORY */
  13277. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13278. case WASM_OP_TABLE_INIT:
  13279. {
  13280. uint8 seg_type = 0, tbl_type = 0;
  13281. #if WASM_ENABLE_GC != 0
  13282. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13283. #endif
  13284. read_leb_uint32(p, p_end, table_seg_idx);
  13285. read_leb_uint32(p, p_end, table_idx);
  13286. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13287. #if WASM_ENABLE_GC != 0
  13288. (void **)&tbl_ref_type,
  13289. #else
  13290. NULL,
  13291. #endif
  13292. error_buf, error_buf_size))
  13293. goto fail;
  13294. if (!get_table_seg_elem_type(module, table_seg_idx,
  13295. &seg_type,
  13296. #if WASM_ENABLE_GC != 0
  13297. (void **)&seg_ref_type,
  13298. #else
  13299. NULL,
  13300. #endif
  13301. error_buf, error_buf_size))
  13302. goto fail;
  13303. #if WASM_ENABLE_GC == 0
  13304. if (seg_type != tbl_type) {
  13305. set_error_buf(error_buf, error_buf_size,
  13306. "type mismatch");
  13307. goto fail;
  13308. }
  13309. #else
  13310. if (!wasm_reftype_is_subtype_of(
  13311. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13312. module->types, module->type_count)) {
  13313. set_error_buf(error_buf, error_buf_size,
  13314. "type mismatch");
  13315. goto fail;
  13316. }
  13317. #endif
  13318. #if WASM_ENABLE_FAST_INTERP != 0
  13319. emit_uint32(loader_ctx, table_seg_idx);
  13320. emit_uint32(loader_ctx, table_idx);
  13321. #endif
  13322. POP_I32();
  13323. POP_I32();
  13324. POP_I32();
  13325. #if WASM_ENABLE_WAMR_COMPILER != 0
  13326. module->is_ref_types_used = true;
  13327. #endif
  13328. break;
  13329. }
  13330. case WASM_OP_ELEM_DROP:
  13331. {
  13332. read_leb_uint32(p, p_end, table_seg_idx);
  13333. if (!get_table_seg_elem_type(module, table_seg_idx,
  13334. NULL, NULL, error_buf,
  13335. error_buf_size))
  13336. goto fail;
  13337. #if WASM_ENABLE_FAST_INTERP != 0
  13338. emit_uint32(loader_ctx, table_seg_idx);
  13339. #endif
  13340. #if WASM_ENABLE_WAMR_COMPILER != 0
  13341. module->is_ref_types_used = true;
  13342. #endif
  13343. break;
  13344. }
  13345. case WASM_OP_TABLE_COPY:
  13346. {
  13347. uint8 src_type, dst_type;
  13348. #if WASM_ENABLE_GC != 0
  13349. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13350. #endif
  13351. uint32 src_tbl_idx, dst_tbl_idx;
  13352. read_leb_uint32(p, p_end, dst_tbl_idx);
  13353. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13354. #if WASM_ENABLE_GC != 0
  13355. (void **)&dst_ref_type,
  13356. #else
  13357. NULL,
  13358. #endif
  13359. error_buf, error_buf_size))
  13360. goto fail;
  13361. read_leb_uint32(p, p_end, src_tbl_idx);
  13362. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13363. #if WASM_ENABLE_GC != 0
  13364. (void **)&src_ref_type,
  13365. #else
  13366. NULL,
  13367. #endif
  13368. error_buf, error_buf_size))
  13369. goto fail;
  13370. #if WASM_ENABLE_GC == 0
  13371. if (src_type != dst_type) {
  13372. set_error_buf(error_buf, error_buf_size,
  13373. "type mismatch");
  13374. goto fail;
  13375. }
  13376. #else
  13377. if (!wasm_reftype_is_subtype_of(
  13378. src_type, src_ref_type, dst_type, dst_ref_type,
  13379. module->types, module->type_count)) {
  13380. set_error_buf(error_buf, error_buf_size,
  13381. "type mismatch");
  13382. goto fail;
  13383. }
  13384. #endif
  13385. #if WASM_ENABLE_FAST_INTERP != 0
  13386. emit_uint32(loader_ctx, dst_tbl_idx);
  13387. emit_uint32(loader_ctx, src_tbl_idx);
  13388. #endif
  13389. POP_I32();
  13390. POP_I32();
  13391. POP_I32();
  13392. #if WASM_ENABLE_WAMR_COMPILER != 0
  13393. module->is_ref_types_used = true;
  13394. #endif
  13395. break;
  13396. }
  13397. case WASM_OP_TABLE_SIZE:
  13398. {
  13399. read_leb_uint32(p, p_end, table_idx);
  13400. /* TODO: shall we create a new function to check
  13401. table idx instead of using below function? */
  13402. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13403. error_buf, error_buf_size))
  13404. goto fail;
  13405. #if WASM_ENABLE_FAST_INTERP != 0
  13406. emit_uint32(loader_ctx, table_idx);
  13407. #endif
  13408. PUSH_I32();
  13409. #if WASM_ENABLE_WAMR_COMPILER != 0
  13410. module->is_ref_types_used = true;
  13411. #endif
  13412. break;
  13413. }
  13414. case WASM_OP_TABLE_GROW:
  13415. case WASM_OP_TABLE_FILL:
  13416. {
  13417. uint8 decl_type;
  13418. #if WASM_ENABLE_GC != 0
  13419. WASMRefType *ref_type = NULL;
  13420. #endif
  13421. read_leb_uint32(p, p_end, table_idx);
  13422. if (!get_table_elem_type(module, table_idx, &decl_type,
  13423. #if WASM_ENABLE_GC != 0
  13424. (void **)&ref_type,
  13425. #else
  13426. NULL,
  13427. #endif
  13428. error_buf, error_buf_size))
  13429. goto fail;
  13430. #if WASM_ENABLE_GC != 0
  13431. if (wasm_is_type_multi_byte_type(decl_type)) {
  13432. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13433. ref_type,
  13434. wasm_reftype_struct_size(ref_type));
  13435. }
  13436. #endif
  13437. if (opcode1 == WASM_OP_TABLE_GROW) {
  13438. if (table_idx < module->import_table_count) {
  13439. module->import_tables[table_idx]
  13440. .u.table.possible_grow = true;
  13441. }
  13442. else {
  13443. module
  13444. ->tables[table_idx
  13445. - module->import_table_count]
  13446. .possible_grow = true;
  13447. }
  13448. }
  13449. #if WASM_ENABLE_FAST_INTERP != 0
  13450. emit_uint32(loader_ctx, table_idx);
  13451. #endif
  13452. POP_I32();
  13453. #if WASM_ENABLE_FAST_INTERP != 0
  13454. POP_OFFSET_TYPE(decl_type);
  13455. #endif
  13456. POP_TYPE(decl_type);
  13457. if (opcode1 == WASM_OP_TABLE_GROW)
  13458. PUSH_I32();
  13459. else
  13460. POP_I32();
  13461. #if WASM_ENABLE_WAMR_COMPILER != 0
  13462. module->is_ref_types_used = true;
  13463. #endif
  13464. break;
  13465. }
  13466. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13467. default:
  13468. set_error_buf_v(error_buf, error_buf_size,
  13469. "%s %02x %02x", "unsupported opcode",
  13470. 0xfc, opcode1);
  13471. goto fail;
  13472. }
  13473. break;
  13474. }
  13475. #if WASM_ENABLE_SIMD != 0
  13476. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13477. case WASM_OP_SIMD_PREFIX:
  13478. {
  13479. /* TODO: memory64 offset type changes */
  13480. uint32 opcode1;
  13481. #if WASM_ENABLE_WAMR_COMPILER != 0
  13482. /* Mark the SIMD instruction is used in this module */
  13483. module->is_simd_used = true;
  13484. #endif
  13485. read_leb_uint32(p, p_end, opcode1);
  13486. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13487. */
  13488. switch (opcode1) {
  13489. /* memory instruction */
  13490. case SIMD_v128_load:
  13491. case SIMD_v128_load8x8_s:
  13492. case SIMD_v128_load8x8_u:
  13493. case SIMD_v128_load16x4_s:
  13494. case SIMD_v128_load16x4_u:
  13495. case SIMD_v128_load32x2_s:
  13496. case SIMD_v128_load32x2_u:
  13497. case SIMD_v128_load8_splat:
  13498. case SIMD_v128_load16_splat:
  13499. case SIMD_v128_load32_splat:
  13500. case SIMD_v128_load64_splat:
  13501. {
  13502. CHECK_MEMORY();
  13503. read_leb_uint32(p, p_end, align); /* align */
  13504. if (!check_simd_memory_access_align(
  13505. opcode1, align, error_buf, error_buf_size)) {
  13506. goto fail;
  13507. }
  13508. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13509. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13510. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13511. func->has_memory_operations = true;
  13512. #endif
  13513. break;
  13514. }
  13515. case SIMD_v128_store:
  13516. {
  13517. CHECK_MEMORY();
  13518. read_leb_uint32(p, p_end, align); /* align */
  13519. if (!check_simd_memory_access_align(
  13520. opcode1, align, error_buf, error_buf_size)) {
  13521. goto fail;
  13522. }
  13523. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13524. POP_V128();
  13525. POP_MEM_OFFSET();
  13526. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13527. func->has_memory_operations = true;
  13528. #endif
  13529. break;
  13530. }
  13531. /* basic operation */
  13532. case SIMD_v128_const:
  13533. {
  13534. CHECK_BUF1(p, p_end, 16);
  13535. p += 16;
  13536. PUSH_V128();
  13537. break;
  13538. }
  13539. case SIMD_v8x16_shuffle:
  13540. {
  13541. V128 mask;
  13542. CHECK_BUF1(p, p_end, 16);
  13543. mask = read_i8x16(p, error_buf, error_buf_size);
  13544. p += 16;
  13545. if (!check_simd_shuffle_mask(mask, error_buf,
  13546. error_buf_size)) {
  13547. goto fail;
  13548. }
  13549. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13550. break;
  13551. }
  13552. case SIMD_v8x16_swizzle:
  13553. {
  13554. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13555. break;
  13556. }
  13557. /* splat operation */
  13558. case SIMD_i8x16_splat:
  13559. case SIMD_i16x8_splat:
  13560. case SIMD_i32x4_splat:
  13561. case SIMD_i64x2_splat:
  13562. case SIMD_f32x4_splat:
  13563. case SIMD_f64x2_splat:
  13564. {
  13565. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13566. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13567. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13568. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13569. VALUE_TYPE_V128);
  13570. break;
  13571. }
  13572. /* lane operation */
  13573. case SIMD_i8x16_extract_lane_s:
  13574. case SIMD_i8x16_extract_lane_u:
  13575. case SIMD_i8x16_replace_lane:
  13576. case SIMD_i16x8_extract_lane_s:
  13577. case SIMD_i16x8_extract_lane_u:
  13578. case SIMD_i16x8_replace_lane:
  13579. case SIMD_i32x4_extract_lane:
  13580. case SIMD_i32x4_replace_lane:
  13581. case SIMD_i64x2_extract_lane:
  13582. case SIMD_i64x2_replace_lane:
  13583. case SIMD_f32x4_extract_lane:
  13584. case SIMD_f32x4_replace_lane:
  13585. case SIMD_f64x2_extract_lane:
  13586. case SIMD_f64x2_replace_lane:
  13587. {
  13588. uint8 lane;
  13589. /* clang-format off */
  13590. uint8 replace[] = {
  13591. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13592. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13593. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13594. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13595. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13596. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13597. };
  13598. uint8 push_type[] = {
  13599. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13600. VALUE_TYPE_V128,
  13601. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13602. VALUE_TYPE_V128,
  13603. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13604. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13605. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13606. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13607. };
  13608. /* clang-format on */
  13609. CHECK_BUF(p, p_end, 1);
  13610. lane = read_uint8(p);
  13611. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13612. error_buf_size)) {
  13613. goto fail;
  13614. }
  13615. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13616. if (!(wasm_loader_pop_frame_ref(
  13617. loader_ctx,
  13618. replace[opcode1
  13619. - SIMD_i8x16_extract_lane_s],
  13620. error_buf, error_buf_size)))
  13621. goto fail;
  13622. }
  13623. POP_AND_PUSH(
  13624. VALUE_TYPE_V128,
  13625. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13626. break;
  13627. }
  13628. /* i8x16 compare operation */
  13629. case SIMD_i8x16_eq:
  13630. case SIMD_i8x16_ne:
  13631. case SIMD_i8x16_lt_s:
  13632. case SIMD_i8x16_lt_u:
  13633. case SIMD_i8x16_gt_s:
  13634. case SIMD_i8x16_gt_u:
  13635. case SIMD_i8x16_le_s:
  13636. case SIMD_i8x16_le_u:
  13637. case SIMD_i8x16_ge_s:
  13638. case SIMD_i8x16_ge_u:
  13639. /* i16x8 compare operation */
  13640. case SIMD_i16x8_eq:
  13641. case SIMD_i16x8_ne:
  13642. case SIMD_i16x8_lt_s:
  13643. case SIMD_i16x8_lt_u:
  13644. case SIMD_i16x8_gt_s:
  13645. case SIMD_i16x8_gt_u:
  13646. case SIMD_i16x8_le_s:
  13647. case SIMD_i16x8_le_u:
  13648. case SIMD_i16x8_ge_s:
  13649. case SIMD_i16x8_ge_u:
  13650. /* i32x4 compare operation */
  13651. case SIMD_i32x4_eq:
  13652. case SIMD_i32x4_ne:
  13653. case SIMD_i32x4_lt_s:
  13654. case SIMD_i32x4_lt_u:
  13655. case SIMD_i32x4_gt_s:
  13656. case SIMD_i32x4_gt_u:
  13657. case SIMD_i32x4_le_s:
  13658. case SIMD_i32x4_le_u:
  13659. case SIMD_i32x4_ge_s:
  13660. case SIMD_i32x4_ge_u:
  13661. /* f32x4 compare operation */
  13662. case SIMD_f32x4_eq:
  13663. case SIMD_f32x4_ne:
  13664. case SIMD_f32x4_lt:
  13665. case SIMD_f32x4_gt:
  13666. case SIMD_f32x4_le:
  13667. case SIMD_f32x4_ge:
  13668. /* f64x2 compare operation */
  13669. case SIMD_f64x2_eq:
  13670. case SIMD_f64x2_ne:
  13671. case SIMD_f64x2_lt:
  13672. case SIMD_f64x2_gt:
  13673. case SIMD_f64x2_le:
  13674. case SIMD_f64x2_ge:
  13675. {
  13676. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13677. break;
  13678. }
  13679. /* v128 operation */
  13680. case SIMD_v128_not:
  13681. {
  13682. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13683. break;
  13684. }
  13685. case SIMD_v128_and:
  13686. case SIMD_v128_andnot:
  13687. case SIMD_v128_or:
  13688. case SIMD_v128_xor:
  13689. {
  13690. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13691. break;
  13692. }
  13693. case SIMD_v128_bitselect:
  13694. {
  13695. POP_V128();
  13696. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13697. break;
  13698. }
  13699. case SIMD_v128_any_true:
  13700. {
  13701. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13702. break;
  13703. }
  13704. /* Load Lane Operation */
  13705. case SIMD_v128_load8_lane:
  13706. case SIMD_v128_load16_lane:
  13707. case SIMD_v128_load32_lane:
  13708. case SIMD_v128_load64_lane:
  13709. case SIMD_v128_store8_lane:
  13710. case SIMD_v128_store16_lane:
  13711. case SIMD_v128_store32_lane:
  13712. case SIMD_v128_store64_lane:
  13713. {
  13714. uint8 lane;
  13715. CHECK_MEMORY();
  13716. read_leb_uint32(p, p_end, align); /* align */
  13717. if (!check_simd_memory_access_align(
  13718. opcode1, align, error_buf, error_buf_size)) {
  13719. goto fail;
  13720. }
  13721. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13722. CHECK_BUF(p, p_end, 1);
  13723. lane = read_uint8(p);
  13724. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13725. error_buf_size)) {
  13726. goto fail;
  13727. }
  13728. POP_V128();
  13729. POP_MEM_OFFSET();
  13730. if (opcode1 < SIMD_v128_store8_lane) {
  13731. PUSH_V128();
  13732. }
  13733. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13734. func->has_memory_operations = true;
  13735. #endif
  13736. break;
  13737. }
  13738. case SIMD_v128_load32_zero:
  13739. case SIMD_v128_load64_zero:
  13740. {
  13741. CHECK_MEMORY();
  13742. read_leb_uint32(p, p_end, align); /* align */
  13743. if (!check_simd_memory_access_align(
  13744. opcode1, align, error_buf, error_buf_size)) {
  13745. goto fail;
  13746. }
  13747. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13748. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13749. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13750. func->has_memory_operations = true;
  13751. #endif
  13752. break;
  13753. }
  13754. /* Float conversion */
  13755. case SIMD_f32x4_demote_f64x2_zero:
  13756. case SIMD_f64x2_promote_low_f32x4_zero:
  13757. {
  13758. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13759. break;
  13760. }
  13761. /* i8x16 Operation */
  13762. case SIMD_i8x16_abs:
  13763. case SIMD_i8x16_neg:
  13764. case SIMD_i8x16_popcnt:
  13765. {
  13766. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13767. break;
  13768. }
  13769. case SIMD_i8x16_all_true:
  13770. case SIMD_i8x16_bitmask:
  13771. {
  13772. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13773. break;
  13774. }
  13775. case SIMD_i8x16_narrow_i16x8_s:
  13776. case SIMD_i8x16_narrow_i16x8_u:
  13777. {
  13778. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13779. break;
  13780. }
  13781. case SIMD_f32x4_ceil:
  13782. case SIMD_f32x4_floor:
  13783. case SIMD_f32x4_trunc:
  13784. case SIMD_f32x4_nearest:
  13785. {
  13786. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13787. break;
  13788. }
  13789. case SIMD_i8x16_shl:
  13790. case SIMD_i8x16_shr_s:
  13791. case SIMD_i8x16_shr_u:
  13792. {
  13793. POP_I32();
  13794. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13795. break;
  13796. }
  13797. case SIMD_i8x16_add:
  13798. case SIMD_i8x16_add_sat_s:
  13799. case SIMD_i8x16_add_sat_u:
  13800. case SIMD_i8x16_sub:
  13801. case SIMD_i8x16_sub_sat_s:
  13802. case SIMD_i8x16_sub_sat_u:
  13803. {
  13804. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13805. break;
  13806. }
  13807. case SIMD_f64x2_ceil:
  13808. case SIMD_f64x2_floor:
  13809. {
  13810. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13811. break;
  13812. }
  13813. case SIMD_i8x16_min_s:
  13814. case SIMD_i8x16_min_u:
  13815. case SIMD_i8x16_max_s:
  13816. case SIMD_i8x16_max_u:
  13817. {
  13818. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13819. break;
  13820. }
  13821. case SIMD_f64x2_trunc:
  13822. {
  13823. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13824. break;
  13825. }
  13826. case SIMD_i8x16_avgr_u:
  13827. {
  13828. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13829. break;
  13830. }
  13831. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13832. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13833. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13834. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13835. /* i16x8 operation */
  13836. case SIMD_i16x8_abs:
  13837. case SIMD_i16x8_neg:
  13838. {
  13839. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13840. break;
  13841. }
  13842. case SIMD_i16x8_q15mulr_sat_s:
  13843. {
  13844. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13845. break;
  13846. }
  13847. case SIMD_i16x8_all_true:
  13848. case SIMD_i16x8_bitmask:
  13849. {
  13850. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13851. break;
  13852. }
  13853. case SIMD_i16x8_narrow_i32x4_s:
  13854. case SIMD_i16x8_narrow_i32x4_u:
  13855. {
  13856. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13857. break;
  13858. }
  13859. case SIMD_i16x8_extend_low_i8x16_s:
  13860. case SIMD_i16x8_extend_high_i8x16_s:
  13861. case SIMD_i16x8_extend_low_i8x16_u:
  13862. case SIMD_i16x8_extend_high_i8x16_u:
  13863. {
  13864. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13865. break;
  13866. }
  13867. case SIMD_i16x8_shl:
  13868. case SIMD_i16x8_shr_s:
  13869. case SIMD_i16x8_shr_u:
  13870. {
  13871. POP_I32();
  13872. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13873. break;
  13874. }
  13875. case SIMD_i16x8_add:
  13876. case SIMD_i16x8_add_sat_s:
  13877. case SIMD_i16x8_add_sat_u:
  13878. case SIMD_i16x8_sub:
  13879. case SIMD_i16x8_sub_sat_s:
  13880. case SIMD_i16x8_sub_sat_u:
  13881. {
  13882. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13883. break;
  13884. }
  13885. case SIMD_f64x2_nearest:
  13886. {
  13887. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13888. break;
  13889. }
  13890. case SIMD_i16x8_mul:
  13891. case SIMD_i16x8_min_s:
  13892. case SIMD_i16x8_min_u:
  13893. case SIMD_i16x8_max_s:
  13894. case SIMD_i16x8_max_u:
  13895. case SIMD_i16x8_avgr_u:
  13896. case SIMD_i16x8_extmul_low_i8x16_s:
  13897. case SIMD_i16x8_extmul_high_i8x16_s:
  13898. case SIMD_i16x8_extmul_low_i8x16_u:
  13899. case SIMD_i16x8_extmul_high_i8x16_u:
  13900. {
  13901. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13902. break;
  13903. }
  13904. /* i32x4 operation */
  13905. case SIMD_i32x4_abs:
  13906. case SIMD_i32x4_neg:
  13907. {
  13908. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13909. break;
  13910. }
  13911. case SIMD_i32x4_all_true:
  13912. case SIMD_i32x4_bitmask:
  13913. {
  13914. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13915. break;
  13916. }
  13917. case SIMD_i32x4_extend_low_i16x8_s:
  13918. case SIMD_i32x4_extend_high_i16x8_s:
  13919. case SIMD_i32x4_extend_low_i16x8_u:
  13920. case SIMD_i32x4_extend_high_i16x8_u:
  13921. {
  13922. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13923. break;
  13924. }
  13925. case SIMD_i32x4_shl:
  13926. case SIMD_i32x4_shr_s:
  13927. case SIMD_i32x4_shr_u:
  13928. {
  13929. POP_I32();
  13930. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13931. break;
  13932. }
  13933. case SIMD_i32x4_add:
  13934. case SIMD_i32x4_sub:
  13935. case SIMD_i32x4_mul:
  13936. case SIMD_i32x4_min_s:
  13937. case SIMD_i32x4_min_u:
  13938. case SIMD_i32x4_max_s:
  13939. case SIMD_i32x4_max_u:
  13940. case SIMD_i32x4_dot_i16x8_s:
  13941. case SIMD_i32x4_extmul_low_i16x8_s:
  13942. case SIMD_i32x4_extmul_high_i16x8_s:
  13943. case SIMD_i32x4_extmul_low_i16x8_u:
  13944. case SIMD_i32x4_extmul_high_i16x8_u:
  13945. {
  13946. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13947. break;
  13948. }
  13949. /* i64x2 operation */
  13950. case SIMD_i64x2_abs:
  13951. case SIMD_i64x2_neg:
  13952. {
  13953. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13954. break;
  13955. }
  13956. case SIMD_i64x2_all_true:
  13957. case SIMD_i64x2_bitmask:
  13958. {
  13959. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13960. break;
  13961. }
  13962. case SIMD_i64x2_extend_low_i32x4_s:
  13963. case SIMD_i64x2_extend_high_i32x4_s:
  13964. case SIMD_i64x2_extend_low_i32x4_u:
  13965. case SIMD_i64x2_extend_high_i32x4_u:
  13966. {
  13967. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13968. break;
  13969. }
  13970. case SIMD_i64x2_shl:
  13971. case SIMD_i64x2_shr_s:
  13972. case SIMD_i64x2_shr_u:
  13973. {
  13974. POP_I32();
  13975. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13976. break;
  13977. }
  13978. case SIMD_i64x2_add:
  13979. case SIMD_i64x2_sub:
  13980. case SIMD_i64x2_mul:
  13981. case SIMD_i64x2_eq:
  13982. case SIMD_i64x2_ne:
  13983. case SIMD_i64x2_lt_s:
  13984. case SIMD_i64x2_gt_s:
  13985. case SIMD_i64x2_le_s:
  13986. case SIMD_i64x2_ge_s:
  13987. case SIMD_i64x2_extmul_low_i32x4_s:
  13988. case SIMD_i64x2_extmul_high_i32x4_s:
  13989. case SIMD_i64x2_extmul_low_i32x4_u:
  13990. case SIMD_i64x2_extmul_high_i32x4_u:
  13991. {
  13992. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13993. break;
  13994. }
  13995. /* f32x4 operation */
  13996. case SIMD_f32x4_abs:
  13997. case SIMD_f32x4_neg:
  13998. case SIMD_f32x4_sqrt:
  13999. {
  14000. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14001. break;
  14002. }
  14003. case SIMD_f32x4_add:
  14004. case SIMD_f32x4_sub:
  14005. case SIMD_f32x4_mul:
  14006. case SIMD_f32x4_div:
  14007. case SIMD_f32x4_min:
  14008. case SIMD_f32x4_max:
  14009. case SIMD_f32x4_pmin:
  14010. case SIMD_f32x4_pmax:
  14011. {
  14012. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14013. break;
  14014. }
  14015. /* f64x2 operation */
  14016. case SIMD_f64x2_abs:
  14017. case SIMD_f64x2_neg:
  14018. case SIMD_f64x2_sqrt:
  14019. {
  14020. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14021. break;
  14022. }
  14023. case SIMD_f64x2_add:
  14024. case SIMD_f64x2_sub:
  14025. case SIMD_f64x2_mul:
  14026. case SIMD_f64x2_div:
  14027. case SIMD_f64x2_min:
  14028. case SIMD_f64x2_max:
  14029. case SIMD_f64x2_pmin:
  14030. case SIMD_f64x2_pmax:
  14031. {
  14032. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14033. break;
  14034. }
  14035. case SIMD_i32x4_trunc_sat_f32x4_s:
  14036. case SIMD_i32x4_trunc_sat_f32x4_u:
  14037. case SIMD_f32x4_convert_i32x4_s:
  14038. case SIMD_f32x4_convert_i32x4_u:
  14039. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14040. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14041. case SIMD_f64x2_convert_low_i32x4_s:
  14042. case SIMD_f64x2_convert_low_i32x4_u:
  14043. {
  14044. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14045. break;
  14046. }
  14047. default:
  14048. {
  14049. if (error_buf != NULL) {
  14050. snprintf(error_buf, error_buf_size,
  14051. "WASM module load failed: "
  14052. "invalid opcode 0xfd %02x.",
  14053. opcode1);
  14054. }
  14055. goto fail;
  14056. }
  14057. }
  14058. break;
  14059. }
  14060. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14061. #endif /* end of WASM_ENABLE_SIMD */
  14062. #if WASM_ENABLE_SHARED_MEMORY != 0
  14063. case WASM_OP_ATOMIC_PREFIX:
  14064. {
  14065. uint32 opcode1;
  14066. read_leb_uint32(p, p_end, opcode1);
  14067. #if WASM_ENABLE_FAST_INTERP != 0
  14068. emit_byte(loader_ctx, opcode1);
  14069. #endif
  14070. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14071. CHECK_MEMORY();
  14072. read_leb_uint32(p, p_end, align); /* align */
  14073. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14074. if (!check_memory_align_equal(opcode1, align, error_buf,
  14075. error_buf_size)) {
  14076. goto fail;
  14077. }
  14078. #if WASM_ENABLE_FAST_INTERP != 0
  14079. emit_uint32(loader_ctx, mem_offset);
  14080. #endif
  14081. }
  14082. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14083. func->has_memory_operations = true;
  14084. #endif
  14085. switch (opcode1) {
  14086. case WASM_OP_ATOMIC_NOTIFY:
  14087. POP_I32();
  14088. POP_MEM_OFFSET();
  14089. PUSH_I32();
  14090. break;
  14091. case WASM_OP_ATOMIC_WAIT32:
  14092. POP_I64();
  14093. POP_I32();
  14094. POP_MEM_OFFSET();
  14095. PUSH_I32();
  14096. break;
  14097. case WASM_OP_ATOMIC_WAIT64:
  14098. POP_I64();
  14099. POP_I64();
  14100. POP_MEM_OFFSET();
  14101. PUSH_I32();
  14102. break;
  14103. case WASM_OP_ATOMIC_FENCE:
  14104. /* reserved byte 0x00 */
  14105. if (*p++ != 0x00) {
  14106. set_error_buf(error_buf, error_buf_size,
  14107. "zero byte expected");
  14108. goto fail;
  14109. }
  14110. break;
  14111. case WASM_OP_ATOMIC_I32_LOAD:
  14112. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14113. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14114. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14115. break;
  14116. case WASM_OP_ATOMIC_I32_STORE:
  14117. case WASM_OP_ATOMIC_I32_STORE8:
  14118. case WASM_OP_ATOMIC_I32_STORE16:
  14119. POP_I32();
  14120. POP_MEM_OFFSET();
  14121. break;
  14122. case WASM_OP_ATOMIC_I64_LOAD:
  14123. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14124. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14125. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14126. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14127. break;
  14128. case WASM_OP_ATOMIC_I64_STORE:
  14129. case WASM_OP_ATOMIC_I64_STORE8:
  14130. case WASM_OP_ATOMIC_I64_STORE16:
  14131. case WASM_OP_ATOMIC_I64_STORE32:
  14132. POP_I64();
  14133. POP_MEM_OFFSET();
  14134. break;
  14135. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14136. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14137. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14138. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14139. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14140. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14141. case WASM_OP_ATOMIC_RMW_I32_AND:
  14142. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14143. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14144. case WASM_OP_ATOMIC_RMW_I32_OR:
  14145. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14146. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14147. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14148. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14149. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14150. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14151. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14152. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14153. POP_I32();
  14154. POP_MEM_OFFSET();
  14155. PUSH_I32();
  14156. break;
  14157. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14158. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14159. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14160. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14161. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14162. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14163. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14164. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14165. case WASM_OP_ATOMIC_RMW_I64_AND:
  14166. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14167. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14168. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14169. case WASM_OP_ATOMIC_RMW_I64_OR:
  14170. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14171. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14172. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14173. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14174. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14175. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14176. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14177. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14178. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14179. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14180. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14181. POP_I64();
  14182. POP_MEM_OFFSET();
  14183. PUSH_I64();
  14184. break;
  14185. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14186. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14187. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14188. POP_I32();
  14189. POP_I32();
  14190. POP_MEM_OFFSET();
  14191. PUSH_I32();
  14192. break;
  14193. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14194. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14195. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14196. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14197. POP_I64();
  14198. POP_I64();
  14199. POP_MEM_OFFSET();
  14200. PUSH_I64();
  14201. break;
  14202. default:
  14203. set_error_buf_v(error_buf, error_buf_size,
  14204. "%s %02x %02x", "unsupported opcode",
  14205. 0xfe, opcode1);
  14206. goto fail;
  14207. }
  14208. break;
  14209. }
  14210. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14211. default:
  14212. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14213. "unsupported opcode", opcode);
  14214. goto fail;
  14215. }
  14216. #if WASM_ENABLE_FAST_INTERP != 0
  14217. last_op = opcode;
  14218. #endif
  14219. }
  14220. if (loader_ctx->csp_num > 0) {
  14221. if (cur_func_idx < module->function_count - 1)
  14222. /* Function with missing end marker (between two functions) */
  14223. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14224. else
  14225. /* Function with missing end marker
  14226. (at EOF or end of code sections) */
  14227. set_error_buf(error_buf, error_buf_size,
  14228. "unexpected end of section or function, "
  14229. "or section size mismatch");
  14230. goto fail;
  14231. }
  14232. #if WASM_ENABLE_FAST_INTERP != 0
  14233. if (loader_ctx->p_code_compiled == NULL)
  14234. goto re_scan;
  14235. func->const_cell_num = loader_ctx->const_cell_num;
  14236. if (func->const_cell_num > 0) {
  14237. int32 j;
  14238. if (!(func->consts = func_const = loader_malloc(
  14239. func->const_cell_num * 4, error_buf, error_buf_size)))
  14240. goto fail;
  14241. func_const_end = func->consts + func->const_cell_num * 4;
  14242. /* reverse the const buf */
  14243. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14244. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14245. if (c->value_type == VALUE_TYPE_F64
  14246. || c->value_type == VALUE_TYPE_I64) {
  14247. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14248. &(c->value.f64), (uint32)sizeof(int64));
  14249. func_const += sizeof(int64);
  14250. }
  14251. else {
  14252. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14253. &(c->value.f32), (uint32)sizeof(int32));
  14254. func_const += sizeof(int32);
  14255. }
  14256. }
  14257. }
  14258. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14259. - loader_ctx->start_dynamic_offset + 1;
  14260. #else
  14261. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14262. #endif
  14263. func->max_block_num = loader_ctx->max_csp_num;
  14264. return_value = true;
  14265. fail:
  14266. wasm_loader_ctx_destroy(loader_ctx);
  14267. (void)table_idx;
  14268. (void)table_seg_idx;
  14269. (void)data_seg_idx;
  14270. (void)i64_const;
  14271. (void)local_offset;
  14272. (void)p_org;
  14273. (void)mem_offset;
  14274. (void)align;
  14275. return return_value;
  14276. }