wasm_loader.c 628 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx - module->import_table_count]
  56. .table_type.flags
  57. & TABLE64_FLAG);
  58. return false;
  59. }
  60. #endif
  61. static void
  62. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  63. {
  64. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  65. }
  66. #if WASM_ENABLE_MEMORY64 != 0
  67. static void
  68. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  69. {
  70. if (error_buf != NULL) {
  71. snprintf(error_buf, error_buf_size, "offset out of range");
  72. }
  73. }
  74. #endif
  75. static void
  76. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  77. {
  78. va_list args;
  79. char buf[128];
  80. if (error_buf != NULL) {
  81. va_start(args, format);
  82. vsnprintf(buf, sizeof(buf), format, args);
  83. va_end(args);
  84. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  85. }
  86. }
  87. static bool
  88. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  89. char *error_buf, uint32 error_buf_size)
  90. {
  91. if ((uintptr_t)buf + length < (uintptr_t)buf
  92. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  93. set_error_buf(error_buf, error_buf_size,
  94. "unexpected end of section or function");
  95. return false;
  96. }
  97. return true;
  98. }
  99. static bool
  100. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  101. char *error_buf, uint32 error_buf_size)
  102. {
  103. if ((uintptr_t)buf + length < (uintptr_t)buf
  104. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  105. set_error_buf(error_buf, error_buf_size, "unexpected end");
  106. return false;
  107. }
  108. return true;
  109. }
  110. #define CHECK_BUF(buf, buf_end, length) \
  111. do { \
  112. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  113. goto fail; \
  114. } \
  115. } while (0)
  116. #define CHECK_BUF1(buf, buf_end, length) \
  117. do { \
  118. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  119. goto fail; \
  120. } \
  121. } while (0)
  122. #define skip_leb(p) while (*p++ & 0x80)
  123. #define skip_leb_int64(p, p_end) skip_leb(p)
  124. #define skip_leb_uint32(p, p_end) skip_leb(p)
  125. #define skip_leb_int32(p, p_end) skip_leb(p)
  126. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  127. #define skip_leb_memidx(p, p_end) skip_leb(p)
  128. #if WASM_ENABLE_MULTI_MEMORY == 0
  129. #define skip_leb_align(p, p_end) skip_leb(p)
  130. #else
  131. /* Skip the following memidx if applicable */
  132. #define skip_leb_align(p, p_end) \
  133. do { \
  134. if (*p++ & OPT_MEMIDX_FLAG) \
  135. skip_leb_uint32(p, p_end); \
  136. } while (0)
  137. #endif
  138. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  139. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  140. #define read_leb_int64(p, p_end, res) \
  141. do { \
  142. uint64 res64; \
  143. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  144. error_buf_size)) \
  145. goto fail; \
  146. res = (int64)res64; \
  147. } while (0)
  148. #if WASM_ENABLE_MEMORY64 != 0
  149. #define read_leb_mem_offset(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  153. &res64, error_buf, error_buf_size)) { \
  154. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  155. goto fail; \
  156. } \
  157. res = (mem_offset_t)res64; \
  158. } while (0)
  159. #else
  160. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  161. #endif
  162. #define read_leb_uint32(p, p_end, res) \
  163. do { \
  164. uint64 res64; \
  165. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  166. error_buf_size)) \
  167. goto fail; \
  168. res = (uint32)res64; \
  169. } while (0)
  170. #define read_leb_int32(p, p_end, res) \
  171. do { \
  172. uint64 res64; \
  173. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  174. error_buf_size)) \
  175. goto fail; \
  176. res = (int32)res64; \
  177. } while (0)
  178. #if WASM_ENABLE_MULTI_MEMORY != 0
  179. #define check_memidx(module, memidx) \
  180. do { \
  181. if (memidx >= module->import_memory_count + module->memory_count) { \
  182. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  183. memidx); \
  184. goto fail; \
  185. } \
  186. } while (0)
  187. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  188. * alignment check */
  189. #define read_leb_memarg(p, p_end, res) \
  190. do { \
  191. read_leb_uint32(p, p_end, res); \
  192. if (res & OPT_MEMIDX_FLAG) { \
  193. res &= ~OPT_MEMIDX_FLAG; \
  194. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  195. check_memidx(module, memidx); \
  196. } \
  197. } while (0)
  198. #else
  199. /* reserved byte 0x00 */
  200. #define check_memidx(module, memidx) \
  201. do { \
  202. (void)module; \
  203. if (memidx != 0) { \
  204. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  205. goto fail; \
  206. } \
  207. } while (0)
  208. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  209. #endif
  210. static char *
  211. type2str(uint8 type)
  212. {
  213. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  214. #if WASM_ENABLE_GC != 0
  215. char *type_str_ref[] = { "stringview_iter",
  216. "stringview_wtf16",
  217. "(ref null ht)",
  218. "(ref ht)",
  219. "", /* reserved */
  220. "stringview_wtf8",
  221. "stringref",
  222. "", /* reserved */
  223. "", /* reserved */
  224. "arrayref",
  225. "structref",
  226. "i31ref",
  227. "eqref",
  228. "anyref",
  229. "externref",
  230. "funcref",
  231. "nullref",
  232. "nullexternref",
  233. "nullfuncref" };
  234. #endif
  235. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  236. return type_str[type - VALUE_TYPE_V128];
  237. #if WASM_ENABLE_GC != 0
  238. else if (wasm_is_type_reftype(type))
  239. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  240. #endif
  241. else if (type == VALUE_TYPE_FUNCREF)
  242. return "funcref";
  243. else if (type == VALUE_TYPE_EXTERNREF)
  244. return "externref";
  245. else
  246. return "unknown type";
  247. }
  248. static bool
  249. is_32bit_type(uint8 type)
  250. {
  251. if (type == VALUE_TYPE_I32
  252. || type == VALUE_TYPE_F32
  253. /* the operand stack is in polymorphic state */
  254. || type == VALUE_TYPE_ANY
  255. #if WASM_ENABLE_GC != 0
  256. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  257. #elif WASM_ENABLE_REF_TYPES != 0
  258. /* For reference types, we use uint32 index to represent
  259. the funcref and externref */
  260. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  261. #endif
  262. )
  263. return true;
  264. return false;
  265. }
  266. static bool
  267. is_64bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  270. #if WASM_ENABLE_GC != 0
  271. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  272. #endif
  273. )
  274. return true;
  275. return false;
  276. }
  277. #if WASM_ENABLE_GC != 0
  278. static bool
  279. is_packed_type(uint8 type)
  280. {
  281. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  282. }
  283. #endif
  284. static bool
  285. is_byte_a_type(uint8 type)
  286. {
  287. return (is_valid_value_type_for_interpreter(type)
  288. || (type == VALUE_TYPE_VOID))
  289. ? true
  290. : false;
  291. }
  292. #if WASM_ENABLE_SIMD != 0
  293. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  294. || (WASM_ENABLE_FAST_INTERP != 0)
  295. static V128
  296. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  297. {
  298. V128 result;
  299. uint8 i;
  300. for (i = 0; i != 16; ++i) {
  301. result.i8x16[i] = read_uint8(p_buf);
  302. }
  303. return result;
  304. }
  305. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  306. (WASM_ENABLE_FAST_INTERP != 0) */
  307. #endif /* end of WASM_ENABLE_SIMD */
  308. static void *
  309. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  310. {
  311. void *mem;
  312. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  313. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  314. return NULL;
  315. }
  316. memset(mem, 0, (uint32)size);
  317. return mem;
  318. }
  319. static void *
  320. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  321. uint32 error_buf_size)
  322. {
  323. uint8 *mem_new;
  324. bh_assert(size_new > size_old);
  325. if ((mem_new = wasm_runtime_realloc(mem_old, size_new))) {
  326. memset(mem_new + size_old, 0, size_new - size_old);
  327. return mem_new;
  328. }
  329. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  330. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  331. wasm_runtime_free(mem_old);
  332. }
  333. return mem_new;
  334. }
  335. #define MEM_REALLOC(mem, size_old, size_new) \
  336. do { \
  337. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  338. error_buf_size); \
  339. if (!mem_new) \
  340. goto fail; \
  341. mem = mem_new; \
  342. } while (0)
  343. static bool
  344. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  345. char *error_buf, uint32 error_buf_size)
  346. {
  347. if (type_index >= type_count) {
  348. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  349. type_index);
  350. return false;
  351. }
  352. return true;
  353. }
  354. #if WASM_ENABLE_GC != 0
  355. static bool
  356. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  357. uint32 error_buf_size)
  358. {
  359. if (!check_type_index(module, module->type_count, type_index, error_buf,
  360. error_buf_size)) {
  361. return false;
  362. }
  363. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  364. set_error_buf(error_buf, error_buf_size, "unknown array type");
  365. return false;
  366. }
  367. return true;
  368. }
  369. #endif
  370. /*
  371. * if no GC is enabled, an valid type is always a function type.
  372. * but if GC is enabled, we need to check the type flag
  373. */
  374. static bool
  375. check_function_type(const WASMModule *module, uint32 type_index,
  376. char *error_buf, uint32 error_buf_size)
  377. {
  378. if (!check_type_index(module, module->type_count, type_index, error_buf,
  379. error_buf_size)) {
  380. return false;
  381. }
  382. #if WASM_ENABLE_GC != 0
  383. if (module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
  384. set_error_buf(error_buf, error_buf_size, "unknown function type");
  385. return false;
  386. }
  387. #endif
  388. return true;
  389. }
  390. static bool
  391. check_function_index(const WASMModule *module, uint32 function_index,
  392. char *error_buf, uint32 error_buf_size)
  393. {
  394. if (function_index
  395. >= module->import_function_count + module->function_count) {
  396. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  397. function_index);
  398. return false;
  399. }
  400. return true;
  401. }
  402. typedef struct InitValue {
  403. uint8 type;
  404. uint8 flag;
  405. #if WASM_ENABLE_GC != 0
  406. uint8 gc_opcode;
  407. WASMRefType ref_type;
  408. #endif
  409. WASMValue value;
  410. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  411. InitializerExpression *expr;
  412. #endif
  413. } InitValue;
  414. typedef struct ConstExprContext {
  415. uint32 sp;
  416. uint32 size;
  417. WASMModule *module;
  418. InitValue *stack;
  419. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  420. } ConstExprContext;
  421. static void
  422. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  423. {
  424. ctx->sp = 0;
  425. ctx->module = module;
  426. ctx->stack = ctx->data;
  427. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  428. }
  429. static bool
  430. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  431. #if WASM_ENABLE_GC != 0
  432. WASMRefType *ref_type, uint8 gc_opcode,
  433. #endif
  434. WASMValue *value,
  435. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  436. InitializerExpression *expr,
  437. #endif
  438. char *error_buf, uint32 error_buf_size)
  439. {
  440. InitValue *cur_value;
  441. if (ctx->sp >= ctx->size) {
  442. if (ctx->stack != ctx->data) {
  443. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  444. (ctx->size + 4) * sizeof(InitValue));
  445. }
  446. else {
  447. if (!(ctx->stack =
  448. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  449. error_buf, error_buf_size))) {
  450. goto fail;
  451. }
  452. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  453. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  454. }
  455. ctx->size += 4;
  456. }
  457. cur_value = &ctx->stack[ctx->sp++];
  458. cur_value->type = type;
  459. cur_value->flag = flag;
  460. cur_value->value = *value;
  461. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  462. cur_value->expr = expr;
  463. #endif
  464. #if WASM_ENABLE_GC != 0
  465. cur_value->gc_opcode = gc_opcode;
  466. if (wasm_is_type_multi_byte_type(type)) {
  467. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  468. ref_type, wasm_reftype_struct_size(ref_type));
  469. }
  470. #endif
  471. return true;
  472. fail:
  473. return false;
  474. }
  475. #if WASM_ENABLE_GC != 0
  476. static void
  477. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  478. {
  479. WASMStructNewInitValues *struct_init_values =
  480. (WASMStructNewInitValues *)data;
  481. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  482. WASMType *wasm_type;
  483. uint32 i;
  484. if (!data)
  485. return;
  486. wasm_type = module->types[struct_init_values->type_idx];
  487. /* The data can only be type of `WASMStructNewInitValues *`
  488. or `WASMArrayNewInitValues *` */
  489. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  490. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  491. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  492. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  493. WASMRefType *ref_type;
  494. uint8 field_type;
  495. uint16 ref_type_map_index = 0;
  496. for (i = 0; i < struct_init_values->count; i++) {
  497. field_type = struct_type->fields[i].field_type;
  498. if (wasm_is_type_multi_byte_type(field_type))
  499. ref_type =
  500. struct_type->ref_type_maps[ref_type_map_index++].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,
  539. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  540. InitializerExpression **p_expr,
  541. #endif
  542. char *error_buf, uint32 error_buf_size)
  543. {
  544. InitValue *cur_value;
  545. if (ctx->sp == 0) {
  546. set_error_buf(error_buf, error_buf_size,
  547. "type mismatch: const expr stack underflow");
  548. return false;
  549. }
  550. cur_value = &ctx->stack[--ctx->sp];
  551. #if WASM_ENABLE_GC == 0
  552. if (cur_value->type != type) {
  553. set_error_buf(error_buf, error_buf_size, "type mismatch");
  554. return false;
  555. }
  556. #else
  557. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  558. ref_type, ctx->module->types,
  559. ctx->module->type_count)) {
  560. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  561. "type mismatch: expect ", type2str(type),
  562. " but got other");
  563. goto fail;
  564. }
  565. #endif
  566. if (p_flag)
  567. *p_flag = cur_value->flag;
  568. if (p_value)
  569. *p_value = cur_value->value;
  570. #if WASM_ENABLE_GC != 0
  571. if (p_gc_opcode)
  572. *p_gc_opcode = cur_value->gc_opcode;
  573. #endif
  574. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  575. if (p_expr)
  576. *p_expr = cur_value->expr;
  577. #endif
  578. return true;
  579. #if WASM_ENABLE_GC != 0
  580. fail:
  581. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  582. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  583. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  584. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  585. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  586. }
  587. return false;
  588. #endif
  589. }
  590. static void
  591. destroy_const_expr_stack(ConstExprContext *ctx, bool free_exprs)
  592. {
  593. #if WASM_ENABLE_GC != 0
  594. uint32 i;
  595. for (i = 0; i < ctx->sp; i++) {
  596. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  597. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  598. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  599. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  600. destroy_init_expr_data_recursive(ctx->module,
  601. ctx->stack[i].value.data);
  602. }
  603. }
  604. #endif
  605. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  606. if (free_exprs) {
  607. for (uint32 j = 0; j < ctx->sp; j++) {
  608. if (is_expr_binary_op(ctx->stack[j].expr->init_expr_type)) {
  609. destroy_init_expr_recursive(ctx->stack[j].expr);
  610. ctx->stack[j].expr = NULL;
  611. }
  612. }
  613. }
  614. #endif
  615. if (ctx->stack != ctx->data) {
  616. wasm_runtime_free(ctx->stack);
  617. }
  618. }
  619. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  620. static void
  621. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  622. {
  623. #if WASM_ENABLE_GC != 0
  624. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  625. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  626. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  627. destroy_init_expr_data_recursive(module, expr->u.unary.v.data);
  628. }
  629. #endif
  630. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  631. // free left expr and right exprs for binary oprand
  632. if (!is_expr_binary_op(expr->init_expr_type)) {
  633. return;
  634. }
  635. if (expr->u.binary.l_expr) {
  636. destroy_init_expr_recursive(expr->u.binary.l_expr);
  637. }
  638. if (expr->u.binary.r_expr) {
  639. destroy_init_expr_recursive(expr->u.binary.r_expr);
  640. }
  641. expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
  642. #endif
  643. }
  644. #endif
  645. /* for init expr
  646. * (data (i32.add (i32.const 0) (i32.sub (i32.const 1) (i32.const 2)))),
  647. * the binary format is
  648. * 0x11: 41 00 ; i32.const 0
  649. * 0x13: 41 01 ; i32.const 1
  650. * 0x15: 41 02 ; i32.const 2
  651. * 0x17: 6b ; i32.sub
  652. * 0x18: 6a ; i32.add
  653. * for traversal: read opcodes and push them onto the stack. When encountering
  654. * a binary opcode, pop two values from the stack which become the left and
  655. * right child nodes of this binary operation node.
  656. */
  657. static bool
  658. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  659. InitializerExpression *init_expr, uint8 type, void *ref_type,
  660. char *error_buf, uint32 error_buf_size)
  661. {
  662. const uint8 *p = *p_buf, *p_end = buf_end;
  663. uint8 flag, *p_float;
  664. uint32 i;
  665. ConstExprContext const_expr_ctx = { 0 };
  666. WASMValue cur_value;
  667. #if WASM_ENABLE_GC != 0
  668. uint32 opcode1, type_idx;
  669. uint8 opcode;
  670. WASMRefType cur_ref_type = { 0 };
  671. #endif
  672. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  673. InitializerExpression *cur_expr = NULL;
  674. #endif
  675. init_const_expr_stack(&const_expr_ctx, module);
  676. CHECK_BUF(p, p_end, 1);
  677. flag = read_uint8(p);
  678. while (flag != WASM_OP_END) {
  679. switch (flag) {
  680. /* i32.const */
  681. case INIT_EXPR_TYPE_I32_CONST:
  682. read_leb_int32(p, p_end, cur_value.i32);
  683. if (!push_const_expr_stack(&const_expr_ctx, flag,
  684. VALUE_TYPE_I32,
  685. #if WASM_ENABLE_GC != 0
  686. NULL, 0,
  687. #endif
  688. &cur_value,
  689. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  690. NULL,
  691. #endif
  692. error_buf, error_buf_size))
  693. goto fail;
  694. break;
  695. /* i64.const */
  696. case INIT_EXPR_TYPE_I64_CONST:
  697. read_leb_int64(p, p_end, cur_value.i64);
  698. if (!push_const_expr_stack(&const_expr_ctx, flag,
  699. VALUE_TYPE_I64,
  700. #if WASM_ENABLE_GC != 0
  701. NULL, 0,
  702. #endif
  703. &cur_value,
  704. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  705. NULL,
  706. #endif
  707. error_buf, error_buf_size))
  708. goto fail;
  709. break;
  710. /* f32.const */
  711. case INIT_EXPR_TYPE_F32_CONST:
  712. CHECK_BUF(p, p_end, 4);
  713. p_float = (uint8 *)&cur_value.f32;
  714. for (i = 0; i < sizeof(float32); i++)
  715. *p_float++ = *p++;
  716. if (!push_const_expr_stack(&const_expr_ctx, flag,
  717. VALUE_TYPE_F32,
  718. #if WASM_ENABLE_GC != 0
  719. NULL, 0,
  720. #endif
  721. &cur_value,
  722. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  723. NULL,
  724. #endif
  725. error_buf, error_buf_size))
  726. goto fail;
  727. break;
  728. /* f64.const */
  729. case INIT_EXPR_TYPE_F64_CONST:
  730. CHECK_BUF(p, p_end, 8);
  731. p_float = (uint8 *)&cur_value.f64;
  732. for (i = 0; i < sizeof(float64); i++)
  733. *p_float++ = *p++;
  734. if (!push_const_expr_stack(&const_expr_ctx, flag,
  735. VALUE_TYPE_F64,
  736. #if WASM_ENABLE_GC != 0
  737. NULL, 0,
  738. #endif
  739. &cur_value,
  740. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  741. NULL,
  742. #endif
  743. error_buf, error_buf_size))
  744. goto fail;
  745. break;
  746. #if WASM_ENABLE_SIMD != 0
  747. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  748. || (WASM_ENABLE_FAST_INTERP != 0)
  749. /* v128.const */
  750. case INIT_EXPR_TYPE_V128_CONST:
  751. {
  752. uint64 high, low;
  753. CHECK_BUF(p, p_end, 1);
  754. (void)read_uint8(p);
  755. CHECK_BUF(p, p_end, 16);
  756. wasm_runtime_read_v128(p, &high, &low);
  757. p += 16;
  758. cur_value.v128.i64x2[0] = high;
  759. cur_value.v128.i64x2[1] = low;
  760. if (!push_const_expr_stack(&const_expr_ctx, flag,
  761. VALUE_TYPE_V128,
  762. #if WASM_ENABLE_GC != 0
  763. NULL, 0,
  764. #endif
  765. &cur_value,
  766. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  767. NULL,
  768. #endif
  769. error_buf, error_buf_size))
  770. goto fail;
  771. #if WASM_ENABLE_WAMR_COMPILER != 0
  772. /* If any init_expr is v128.const, mark SIMD used */
  773. module->is_simd_used = true;
  774. #endif
  775. break;
  776. }
  777. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  778. (WASM_ENABLE_FAST_INTERP != 0) */
  779. #endif /* end of WASM_ENABLE_SIMD */
  780. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  781. case INIT_EXPR_TYPE_I32_ADD:
  782. case INIT_EXPR_TYPE_I32_SUB:
  783. case INIT_EXPR_TYPE_I32_MUL:
  784. case INIT_EXPR_TYPE_I64_ADD:
  785. case INIT_EXPR_TYPE_I64_SUB:
  786. case INIT_EXPR_TYPE_I64_MUL:
  787. {
  788. InitializerExpression *l_expr, *r_expr;
  789. WASMValue l_value, r_value;
  790. uint8 l_flag, r_flag;
  791. uint8 value_type;
  792. if (flag == INIT_EXPR_TYPE_I32_ADD
  793. || flag == INIT_EXPR_TYPE_I32_SUB
  794. || flag == INIT_EXPR_TYPE_I32_MUL) {
  795. value_type = VALUE_TYPE_I32;
  796. }
  797. else {
  798. value_type = VALUE_TYPE_I64;
  799. }
  800. /* If right flag indicates a binary operation, right expr will
  801. * be popped from stack. Otherwise, allocate a new expr for
  802. * right expr. Same for left expr.
  803. */
  804. if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
  805. #if WASM_ENABLE_GC != 0
  806. NULL, NULL,
  807. #endif
  808. &r_value, &r_expr, error_buf,
  809. error_buf_size))) {
  810. goto fail;
  811. }
  812. if (!is_expr_binary_op(r_flag)) {
  813. if (!(r_expr = loader_malloc(sizeof(InitializerExpression),
  814. error_buf, error_buf_size))) {
  815. goto fail;
  816. }
  817. r_expr->init_expr_type = r_flag;
  818. r_expr->u.unary.v = r_value;
  819. }
  820. if (!(pop_const_expr_stack(&const_expr_ctx, &l_flag, value_type,
  821. #if WASM_ENABLE_GC != 0
  822. NULL, NULL,
  823. #endif
  824. &l_value, &l_expr, error_buf,
  825. error_buf_size))) {
  826. destroy_init_expr_recursive(r_expr);
  827. goto fail;
  828. }
  829. if (!is_expr_binary_op(l_flag)) {
  830. if (!(l_expr = loader_malloc(sizeof(InitializerExpression),
  831. error_buf, error_buf_size))) {
  832. destroy_init_expr_recursive(r_expr);
  833. goto fail;
  834. }
  835. l_expr->init_expr_type = l_flag;
  836. l_expr->u.unary.v = l_value;
  837. }
  838. if (!(cur_expr = loader_malloc(sizeof(InitializerExpression),
  839. error_buf, error_buf_size))) {
  840. destroy_init_expr_recursive(l_expr);
  841. destroy_init_expr_recursive(r_expr);
  842. goto fail;
  843. }
  844. cur_expr->init_expr_type = flag;
  845. cur_expr->u.binary.l_expr = l_expr;
  846. cur_expr->u.binary.r_expr = r_expr;
  847. if (!push_const_expr_stack(&const_expr_ctx, flag, value_type,
  848. #if WASM_ENABLE_GC != 0
  849. NULL, 0,
  850. #endif
  851. &cur_value, cur_expr, error_buf,
  852. error_buf_size)) {
  853. destroy_init_expr_recursive(cur_expr);
  854. goto fail;
  855. }
  856. break;
  857. }
  858. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR */
  859. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  860. /* ref.func */
  861. case INIT_EXPR_TYPE_FUNCREF_CONST:
  862. {
  863. uint32 func_idx;
  864. read_leb_uint32(p, p_end, func_idx);
  865. cur_value.ref_index = func_idx;
  866. if (!check_function_index(module, func_idx, error_buf,
  867. error_buf_size)) {
  868. goto fail;
  869. }
  870. #if WASM_ENABLE_GC == 0
  871. if (!push_const_expr_stack(&const_expr_ctx, flag,
  872. VALUE_TYPE_FUNCREF, &cur_value,
  873. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  874. NULL,
  875. #endif
  876. error_buf, error_buf_size))
  877. goto fail;
  878. #else
  879. if (func_idx < module->import_function_count) {
  880. type_idx =
  881. module->import_functions[func_idx].u.function.type_idx;
  882. }
  883. else {
  884. type_idx = module
  885. ->functions[func_idx
  886. - module->import_function_count]
  887. ->type_idx;
  888. }
  889. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  890. false, type_idx);
  891. if (!push_const_expr_stack(&const_expr_ctx, flag,
  892. cur_ref_type.ref_type, &cur_ref_type,
  893. 0, &cur_value,
  894. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  895. NULL,
  896. #endif
  897. error_buf, error_buf_size))
  898. goto fail;
  899. #endif
  900. #if WASM_ENABLE_WAMR_COMPILER != 0
  901. module->is_ref_types_used = true;
  902. #endif
  903. break;
  904. }
  905. /* ref.null */
  906. case INIT_EXPR_TYPE_REFNULL_CONST:
  907. {
  908. #if WASM_ENABLE_GC == 0
  909. uint8 type1;
  910. CHECK_BUF(p, p_end, 1);
  911. type1 = read_uint8(p);
  912. cur_value.ref_index = NULL_REF;
  913. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  914. &cur_value,
  915. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  916. NULL,
  917. #endif
  918. error_buf, error_buf_size))
  919. goto fail;
  920. #else
  921. /*
  922. * According to the current GC SPEC rules, the heap_type must be
  923. * validated when ref.null is used. It can be an absheaptype,
  924. * or the type C.types[type_idx] must be defined in the context.
  925. */
  926. int32 heap_type;
  927. read_leb_int32(p, p_end, heap_type);
  928. cur_value.gc_obj = NULL_REF;
  929. /*
  930. * The current check of heap_type can deterministically infer
  931. * the result of the previous condition
  932. * `(!is_byte_a_type(type1) ||
  933. * wasm_is_type_multi_byte_type(type1))`. Therefore, the
  934. * original condition is redundant and has been removed.
  935. *
  936. * This logic is consistent with the implementation of the
  937. * `WASM_OP_REF_NULL` case in the `wasm_loader_prepare_bytecode`
  938. * function.
  939. */
  940. if (heap_type >= 0) {
  941. if (!check_type_index(module, module->type_count, heap_type,
  942. error_buf, error_buf_size)) {
  943. goto fail;
  944. }
  945. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  946. true, heap_type);
  947. if (!push_const_expr_stack(&const_expr_ctx, flag,
  948. cur_ref_type.ref_type,
  949. &cur_ref_type, 0, &cur_value,
  950. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  951. NULL,
  952. #endif
  953. error_buf, error_buf_size))
  954. goto fail;
  955. }
  956. else {
  957. if (!wasm_is_valid_heap_type(heap_type)) {
  958. set_error_buf_v(error_buf, error_buf_size,
  959. "unknown type %d", heap_type);
  960. goto fail;
  961. }
  962. cur_ref_type.ref_ht_common.ref_type =
  963. (uint8)((int32)0x80 + heap_type);
  964. if (!push_const_expr_stack(&const_expr_ctx, flag,
  965. cur_ref_type.ref_type, NULL, 0,
  966. &cur_value,
  967. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  968. NULL,
  969. #endif
  970. error_buf, error_buf_size))
  971. goto fail;
  972. }
  973. #endif
  974. #if WASM_ENABLE_WAMR_COMPILER != 0
  975. module->is_ref_types_used = true;
  976. #endif
  977. break;
  978. }
  979. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  980. /* get_global */
  981. case INIT_EXPR_TYPE_GET_GLOBAL:
  982. {
  983. uint32 global_idx;
  984. uint8 global_type;
  985. read_leb_uint32(p, p_end, cur_value.global_index);
  986. global_idx = cur_value.global_index;
  987. /*
  988. * Currently, constant expressions occurring as initializers
  989. * of globals are further constrained in that contained
  990. * global.get instructions are
  991. * only allowed to refer to imported globals.
  992. *
  993. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  994. */
  995. if (global_idx >= module->import_global_count
  996. /* make spec test happy */
  997. #if WASM_ENABLE_GC != 0
  998. + module->global_count
  999. #endif
  1000. ) {
  1001. set_error_buf_v(error_buf, error_buf_size,
  1002. "unknown global %u", global_idx);
  1003. goto fail;
  1004. }
  1005. if (
  1006. /* make spec test happy */
  1007. #if WASM_ENABLE_GC != 0
  1008. global_idx < module->import_global_count &&
  1009. #endif
  1010. module->import_globals[global_idx]
  1011. .u.global.type.is_mutable) {
  1012. set_error_buf_v(error_buf, error_buf_size,
  1013. "constant expression required");
  1014. goto fail;
  1015. }
  1016. if (global_idx < module->import_global_count) {
  1017. global_type = module->import_globals[global_idx]
  1018. .u.global.type.val_type;
  1019. #if WASM_ENABLE_GC != 0
  1020. if (wasm_is_type_multi_byte_type(global_type)) {
  1021. WASMRefType *global_ref_type =
  1022. module->import_globals[global_idx]
  1023. .u.global.ref_type;
  1024. bh_memcpy_s(&cur_ref_type,
  1025. wasm_reftype_struct_size(global_ref_type),
  1026. global_ref_type,
  1027. wasm_reftype_struct_size(global_ref_type));
  1028. }
  1029. #endif
  1030. }
  1031. else {
  1032. global_type =
  1033. module
  1034. ->globals[global_idx - module->import_global_count]
  1035. .type.val_type;
  1036. #if WASM_ENABLE_GC != 0
  1037. if (wasm_is_type_multi_byte_type(global_type)) {
  1038. WASMRefType *global_ref_type =
  1039. module
  1040. ->globals[global_idx
  1041. - module->import_global_count]
  1042. .ref_type;
  1043. bh_memcpy_s(&cur_ref_type,
  1044. wasm_reftype_struct_size(global_ref_type),
  1045. global_ref_type,
  1046. wasm_reftype_struct_size(global_ref_type));
  1047. }
  1048. #endif
  1049. }
  1050. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  1051. #if WASM_ENABLE_GC != 0
  1052. &cur_ref_type, 0,
  1053. #endif
  1054. &cur_value,
  1055. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1056. NULL,
  1057. #endif
  1058. error_buf, error_buf_size))
  1059. goto fail;
  1060. break;
  1061. }
  1062. #if WASM_ENABLE_GC != 0
  1063. /* struct.new and array.new */
  1064. case WASM_OP_GC_PREFIX:
  1065. {
  1066. read_leb_uint32(p, p_end, opcode1);
  1067. switch (opcode1) {
  1068. case WASM_OP_STRUCT_NEW:
  1069. {
  1070. WASMStructType *struct_type;
  1071. WASMStructNewInitValues *struct_init_values = NULL;
  1072. uint32 field_count;
  1073. read_leb_uint32(p, p_end, type_idx);
  1074. if (!check_type_index(module, module->type_count,
  1075. type_idx, error_buf,
  1076. error_buf_size)) {
  1077. goto fail;
  1078. }
  1079. struct_type = (WASMStructType *)module->types[type_idx];
  1080. if (struct_type->base_type.type_flag
  1081. != WASM_TYPE_STRUCT) {
  1082. set_error_buf(error_buf, error_buf_size,
  1083. "unknown struct type");
  1084. goto fail;
  1085. }
  1086. field_count = struct_type->field_count;
  1087. if (!(struct_init_values = loader_malloc(
  1088. offsetof(WASMStructNewInitValues, fields)
  1089. + (uint64)field_count * sizeof(WASMValue),
  1090. error_buf, error_buf_size))) {
  1091. goto fail;
  1092. }
  1093. struct_init_values->type_idx = type_idx;
  1094. struct_init_values->count = field_count;
  1095. for (i = field_count; i > 0; i--) {
  1096. WASMRefType *field_ref_type = NULL;
  1097. uint32 field_idx = i - 1;
  1098. uint8 field_type =
  1099. struct_type->fields[field_idx].field_type;
  1100. if (wasm_is_type_multi_byte_type(field_type)) {
  1101. field_ref_type = wasm_reftype_map_find(
  1102. struct_type->ref_type_maps,
  1103. struct_type->ref_type_map_count, field_idx);
  1104. }
  1105. if (is_packed_type(field_type)) {
  1106. field_type = VALUE_TYPE_I32;
  1107. }
  1108. if (!pop_const_expr_stack(
  1109. &const_expr_ctx, NULL, field_type,
  1110. field_ref_type, NULL,
  1111. &struct_init_values->fields[field_idx],
  1112. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1113. NULL,
  1114. #endif
  1115. error_buf, error_buf_size)) {
  1116. destroy_init_expr_data_recursive(
  1117. module, struct_init_values);
  1118. goto fail;
  1119. }
  1120. }
  1121. cur_value.data = struct_init_values;
  1122. wasm_set_refheaptype_typeidx(
  1123. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1124. if (!push_const_expr_stack(
  1125. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1126. &cur_ref_type, (uint8)opcode1, &cur_value,
  1127. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1128. NULL,
  1129. #endif
  1130. error_buf, error_buf_size)) {
  1131. destroy_init_expr_data_recursive(
  1132. module, struct_init_values);
  1133. goto fail;
  1134. }
  1135. break;
  1136. }
  1137. case WASM_OP_STRUCT_NEW_DEFAULT:
  1138. {
  1139. read_leb_uint32(p, p_end, cur_value.type_index);
  1140. type_idx = cur_value.type_index;
  1141. if (!check_type_index(module, module->type_count,
  1142. type_idx, error_buf,
  1143. error_buf_size)) {
  1144. goto fail;
  1145. }
  1146. if (module->types[type_idx]->type_flag
  1147. != WASM_TYPE_STRUCT) {
  1148. set_error_buf(error_buf, error_buf_size,
  1149. "unknown struct type");
  1150. goto fail;
  1151. }
  1152. cur_value.type_index = type_idx;
  1153. cur_value.data = NULL;
  1154. wasm_set_refheaptype_typeidx(
  1155. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1156. if (!push_const_expr_stack(
  1157. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1158. &cur_ref_type, (uint8)opcode1, &cur_value,
  1159. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1160. NULL,
  1161. #endif
  1162. error_buf, error_buf_size)) {
  1163. goto fail;
  1164. }
  1165. break;
  1166. }
  1167. case WASM_OP_ARRAY_NEW:
  1168. case WASM_OP_ARRAY_NEW_DEFAULT:
  1169. case WASM_OP_ARRAY_NEW_FIXED:
  1170. {
  1171. WASMArrayNewInitValues *array_init_values = NULL;
  1172. WASMArrayType *array_type = NULL;
  1173. WASMRefType *elem_ref_type = NULL;
  1174. uint64 total_size;
  1175. uint8 elem_type;
  1176. read_leb_uint32(p, p_end, cur_value.type_index);
  1177. type_idx = cur_value.type_index;
  1178. if (!check_type_index(module, module->type_count,
  1179. type_idx, error_buf,
  1180. error_buf_size)) {
  1181. goto fail;
  1182. }
  1183. array_type = (WASMArrayType *)module->types[type_idx];
  1184. if (array_type->base_type.type_flag
  1185. != WASM_TYPE_ARRAY) {
  1186. set_error_buf(error_buf, error_buf_size,
  1187. "unknown array type");
  1188. goto fail;
  1189. }
  1190. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  1191. elem_type = array_type->elem_type;
  1192. if (wasm_is_type_multi_byte_type(elem_type)) {
  1193. elem_ref_type = array_type->elem_ref_type;
  1194. }
  1195. if (is_packed_type(elem_type)) {
  1196. elem_type = VALUE_TYPE_I32;
  1197. }
  1198. if (opcode1 == WASM_OP_ARRAY_NEW) {
  1199. WASMValue len_val = { 0 };
  1200. uint64 size = 0;
  1201. if (!pop_const_expr_stack(
  1202. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  1203. NULL, NULL, &len_val,
  1204. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1205. NULL,
  1206. #endif
  1207. error_buf, error_buf_size)) {
  1208. goto fail;
  1209. }
  1210. size =
  1211. sizeof(WASMArrayNewInitValues)
  1212. + sizeof(WASMValue) * (uint64)len_val.i32;
  1213. if (!(array_init_values = loader_malloc(
  1214. size, error_buf, error_buf_size))) {
  1215. goto fail;
  1216. }
  1217. array_init_values->type_idx = type_idx;
  1218. array_init_values->length = len_val.i32;
  1219. if (!pop_const_expr_stack(
  1220. &const_expr_ctx, NULL, elem_type,
  1221. elem_ref_type, NULL,
  1222. &array_init_values->elem_data[0],
  1223. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1224. NULL,
  1225. #endif
  1226. error_buf, error_buf_size)) {
  1227. destroy_init_expr_data_recursive(
  1228. module, array_init_values);
  1229. goto fail;
  1230. }
  1231. cur_value.data = array_init_values;
  1232. }
  1233. else {
  1234. /* WASM_OP_ARRAY_NEW_FIXED */
  1235. uint32 len;
  1236. read_leb_uint32(p, p_end, len);
  1237. total_size =
  1238. (uint64)offsetof(WASMArrayNewInitValues,
  1239. elem_data)
  1240. + (uint64)sizeof(WASMValue) * len;
  1241. if (!(array_init_values =
  1242. loader_malloc(total_size, error_buf,
  1243. error_buf_size))) {
  1244. goto fail;
  1245. }
  1246. array_init_values->type_idx = type_idx;
  1247. array_init_values->length = len;
  1248. for (i = len; i > 0; i--) {
  1249. if (!pop_const_expr_stack(
  1250. &const_expr_ctx, NULL, elem_type,
  1251. elem_ref_type, NULL,
  1252. &array_init_values
  1253. ->elem_data[i - 1],
  1254. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1255. NULL,
  1256. #endif
  1257. error_buf, error_buf_size)) {
  1258. destroy_init_expr_data_recursive(
  1259. module, array_init_values);
  1260. goto fail;
  1261. }
  1262. }
  1263. cur_value.data = array_init_values;
  1264. }
  1265. }
  1266. else {
  1267. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1268. WASMValue len_val;
  1269. uint32 len;
  1270. /* POP(i32) */
  1271. if (!pop_const_expr_stack(
  1272. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1273. NULL, &len_val,
  1274. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1275. NULL,
  1276. #endif
  1277. error_buf, error_buf_size)) {
  1278. goto fail;
  1279. }
  1280. len = len_val.i32;
  1281. cur_value.array_new_default.type_index = type_idx;
  1282. cur_value.array_new_default.length = len;
  1283. }
  1284. wasm_set_refheaptype_typeidx(
  1285. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1286. if (!push_const_expr_stack(
  1287. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1288. &cur_ref_type, (uint8)opcode1, &cur_value,
  1289. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1290. NULL,
  1291. #endif
  1292. error_buf, error_buf_size)) {
  1293. if (array_init_values) {
  1294. destroy_init_expr_data_recursive(
  1295. module, array_init_values);
  1296. }
  1297. goto fail;
  1298. }
  1299. break;
  1300. }
  1301. case WASM_OP_ANY_CONVERT_EXTERN:
  1302. {
  1303. set_error_buf(error_buf, error_buf_size,
  1304. "unsupported constant expression of "
  1305. "extern.internalize");
  1306. goto fail;
  1307. }
  1308. case WASM_OP_EXTERN_CONVERT_ANY:
  1309. {
  1310. set_error_buf(error_buf, error_buf_size,
  1311. "unsupported constant expression of "
  1312. "extern.externalize");
  1313. goto fail;
  1314. }
  1315. case WASM_OP_REF_I31:
  1316. {
  1317. /* POP(i32) */
  1318. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1319. VALUE_TYPE_I32, NULL, NULL,
  1320. &cur_value,
  1321. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1322. NULL,
  1323. #endif
  1324. error_buf, error_buf_size)) {
  1325. goto fail;
  1326. }
  1327. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1328. false, HEAP_TYPE_I31);
  1329. if (!push_const_expr_stack(
  1330. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1331. &cur_ref_type, (uint8)opcode1, &cur_value,
  1332. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1333. NULL,
  1334. #endif
  1335. error_buf, error_buf_size)) {
  1336. goto fail;
  1337. }
  1338. break;
  1339. }
  1340. default:
  1341. set_error_buf(
  1342. error_buf, error_buf_size,
  1343. "type mismatch or constant expression required");
  1344. goto fail;
  1345. }
  1346. break;
  1347. }
  1348. #endif /* end of WASM_ENABLE_GC != 0 */
  1349. default:
  1350. {
  1351. set_error_buf(error_buf, error_buf_size,
  1352. "illegal opcode "
  1353. "or constant expression required "
  1354. "or type mismatch");
  1355. goto fail;
  1356. }
  1357. }
  1358. CHECK_BUF(p, p_end, 1);
  1359. flag = read_uint8(p);
  1360. }
  1361. /* There should be only one value left on the init value stack */
  1362. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1363. #if WASM_ENABLE_GC != 0
  1364. ref_type, &opcode,
  1365. #endif
  1366. &cur_value,
  1367. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1368. &cur_expr,
  1369. #endif
  1370. error_buf, error_buf_size)) {
  1371. goto fail;
  1372. }
  1373. if (const_expr_ctx.sp != 0) {
  1374. set_error_buf(error_buf, error_buf_size,
  1375. "type mismatch: illegal constant opcode sequence");
  1376. goto fail;
  1377. }
  1378. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1379. if (cur_expr != NULL) {
  1380. bh_memcpy_s(init_expr, sizeof(InitializerExpression), cur_expr,
  1381. sizeof(InitializerExpression));
  1382. wasm_runtime_free(cur_expr);
  1383. }
  1384. else {
  1385. init_expr->init_expr_type = flag;
  1386. init_expr->u.unary.v = cur_value;
  1387. }
  1388. #else
  1389. init_expr->init_expr_type = flag;
  1390. init_expr->u.unary.v = cur_value;
  1391. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
  1392. #if WASM_ENABLE_GC != 0
  1393. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1394. switch (opcode) {
  1395. case WASM_OP_STRUCT_NEW:
  1396. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1397. break;
  1398. case WASM_OP_STRUCT_NEW_DEFAULT:
  1399. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1400. break;
  1401. case WASM_OP_ARRAY_NEW:
  1402. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1403. break;
  1404. case WASM_OP_ARRAY_NEW_DEFAULT:
  1405. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1406. break;
  1407. case WASM_OP_ARRAY_NEW_FIXED:
  1408. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1409. break;
  1410. case WASM_OP_REF_I31:
  1411. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1412. break;
  1413. default:
  1414. bh_assert(0);
  1415. break;
  1416. }
  1417. }
  1418. #endif /* end of WASM_ENABLE_GC != 0 */
  1419. *p_buf = p;
  1420. destroy_const_expr_stack(&const_expr_ctx, false);
  1421. return true;
  1422. fail:
  1423. destroy_const_expr_stack(&const_expr_ctx, true);
  1424. return false;
  1425. }
  1426. static bool
  1427. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1428. {
  1429. if (mutable >= 2) {
  1430. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1431. return false;
  1432. }
  1433. return true;
  1434. }
  1435. #if WASM_ENABLE_GC != 0
  1436. static void
  1437. destroy_func_type(WASMFuncType *type)
  1438. {
  1439. /* Destroy the reference type hash set */
  1440. if (type->ref_type_maps)
  1441. wasm_runtime_free(type->ref_type_maps);
  1442. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1443. && WASM_ENABLE_LAZY_JIT != 0
  1444. if (type->call_to_llvm_jit_from_fast_jit)
  1445. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1446. #endif
  1447. /* Free the type */
  1448. wasm_runtime_free(type);
  1449. }
  1450. static void
  1451. destroy_struct_type(WASMStructType *type)
  1452. {
  1453. if (type->ref_type_maps)
  1454. wasm_runtime_free(type->ref_type_maps);
  1455. wasm_runtime_free(type);
  1456. }
  1457. static void
  1458. destroy_array_type(WASMArrayType *type)
  1459. {
  1460. wasm_runtime_free(type);
  1461. }
  1462. static void
  1463. destroy_wasm_type(WASMType *type)
  1464. {
  1465. if (type->ref_count > 1) {
  1466. /* The type is referenced by other types
  1467. of current wasm module */
  1468. type->ref_count--;
  1469. return;
  1470. }
  1471. if (type->type_flag == WASM_TYPE_FUNC)
  1472. destroy_func_type((WASMFuncType *)type);
  1473. else if (type->type_flag == WASM_TYPE_STRUCT)
  1474. destroy_struct_type((WASMStructType *)type);
  1475. else if (type->type_flag == WASM_TYPE_ARRAY)
  1476. destroy_array_type((WASMArrayType *)type);
  1477. else {
  1478. bh_assert(0);
  1479. }
  1480. }
  1481. /* Resolve (ref null ht) or (ref ht) */
  1482. static bool
  1483. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1484. WASMModule *module, uint32 type_count, bool nullable,
  1485. WASMRefType *ref_type, char *error_buf,
  1486. uint32 error_buf_size)
  1487. {
  1488. const uint8 *p = *p_buf, *p_end = buf_end;
  1489. ref_type->ref_type =
  1490. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1491. ref_type->ref_ht_common.nullable = nullable;
  1492. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1493. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1494. /* heap type is (type i), i : typeidx, >= 0 */
  1495. if (!check_type_index(module, type_count,
  1496. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1497. error_buf_size)) {
  1498. return false;
  1499. }
  1500. }
  1501. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1502. /* heap type is func, extern, any, eq, i31 or data */
  1503. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1504. return false;
  1505. }
  1506. *p_buf = p;
  1507. return true;
  1508. fail:
  1509. return false;
  1510. }
  1511. static bool
  1512. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1513. WASMModule *module, uint32 type_count,
  1514. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1515. bool allow_packed_type, char *error_buf,
  1516. uint32 error_buf_size)
  1517. {
  1518. const uint8 *p = *p_buf, *p_end = buf_end;
  1519. uint8 type;
  1520. memset(ref_type, 0, sizeof(WASMRefType));
  1521. CHECK_BUF(p, p_end, 1);
  1522. type = read_uint8(p);
  1523. if (wasm_is_reftype_htref_nullable(type)) {
  1524. /* (ref null ht) */
  1525. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1526. ref_type, error_buf, error_buf_size))
  1527. return false;
  1528. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1529. *p_need_ref_type_map = true;
  1530. else {
  1531. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1532. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1533. multi-byte type to one-byte type to reduce the footprint and
  1534. the complexity of type equal/subtype checking */
  1535. ref_type->ref_type =
  1536. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1537. *p_need_ref_type_map = false;
  1538. }
  1539. }
  1540. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1541. /* (ref ht) */
  1542. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1543. ref_type, error_buf, error_buf_size))
  1544. return false;
  1545. *p_need_ref_type_map = true;
  1546. #if WASM_ENABLE_STRINGREF != 0
  1547. /* covert (ref string) to stringref */
  1548. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1549. ref_type->ref_type =
  1550. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1551. *p_need_ref_type_map = false;
  1552. }
  1553. #endif
  1554. }
  1555. else {
  1556. /* type which can be represented by one byte */
  1557. if (!is_valid_value_type_for_interpreter(type)
  1558. && !(allow_packed_type && is_packed_type(type))) {
  1559. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1560. return false;
  1561. }
  1562. ref_type->ref_type = type;
  1563. *p_need_ref_type_map = false;
  1564. #if WASM_ENABLE_WAMR_COMPILER != 0
  1565. /* If any value's type is v128, mark the module as SIMD used */
  1566. if (type == VALUE_TYPE_V128)
  1567. module->is_simd_used = true;
  1568. #endif
  1569. }
  1570. *p_buf = p;
  1571. return true;
  1572. fail:
  1573. return false;
  1574. }
  1575. static WASMRefType *
  1576. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1577. char *error_buf, uint32 error_buf_size)
  1578. {
  1579. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1580. if (!ret) {
  1581. set_error_buf(error_buf, error_buf_size,
  1582. "insert ref type to hash set failed");
  1583. }
  1584. return ret;
  1585. }
  1586. static bool
  1587. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1588. uint32 type_count, uint32 type_idx, char *error_buf,
  1589. uint32 error_buf_size)
  1590. {
  1591. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1592. uint32 param_count, result_count, i, j = 0;
  1593. uint32 param_cell_num, ret_cell_num;
  1594. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1595. uint64 total_size;
  1596. bool need_ref_type_map;
  1597. WASMRefType ref_type;
  1598. WASMFuncType *type = NULL;
  1599. /* Parse first time to resolve param count, result count and
  1600. ref type map count */
  1601. read_leb_uint32(p, p_end, param_count);
  1602. p_org = p;
  1603. for (i = 0; i < param_count; i++) {
  1604. if (!resolve_value_type(&p, p_end, module, type_count,
  1605. &need_ref_type_map, &ref_type, false, error_buf,
  1606. error_buf_size)) {
  1607. return false;
  1608. }
  1609. if (need_ref_type_map)
  1610. ref_type_map_count++;
  1611. }
  1612. read_leb_uint32(p, p_end, result_count);
  1613. for (i = 0; i < result_count; i++) {
  1614. if (!resolve_value_type(&p, p_end, module, type_count,
  1615. &need_ref_type_map, &ref_type, false, error_buf,
  1616. error_buf_size)) {
  1617. return false;
  1618. }
  1619. if (need_ref_type_map) {
  1620. ref_type_map_count++;
  1621. result_ref_type_map_count++;
  1622. }
  1623. }
  1624. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1625. "ref type map count: %d",
  1626. type_idx, param_count, result_count, ref_type_map_count);
  1627. /* Parse second time to resolve param types, result types and
  1628. ref type map info */
  1629. p = p_org;
  1630. total_size = offsetof(WASMFuncType, types)
  1631. + sizeof(uint8) * (uint64)(param_count + result_count);
  1632. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1633. return false;
  1634. }
  1635. if (ref_type_map_count > 0) {
  1636. if (ref_type_map_count > UINT16_MAX) {
  1637. set_error_buf(error_buf, error_buf_size,
  1638. "ref type count too large");
  1639. return false;
  1640. }
  1641. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1642. if (!(type->ref_type_maps =
  1643. loader_malloc(total_size, error_buf, error_buf_size))) {
  1644. goto fail;
  1645. }
  1646. }
  1647. type->base_type.type_flag = WASM_TYPE_FUNC;
  1648. type->param_count = param_count;
  1649. type->result_count = result_count;
  1650. type->ref_type_map_count = ref_type_map_count;
  1651. if (ref_type_map_count > 0) {
  1652. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1653. - result_ref_type_map_count;
  1654. }
  1655. for (i = 0; i < param_count; i++) {
  1656. if (!resolve_value_type(&p, p_end, module, type_count,
  1657. &need_ref_type_map, &ref_type, false, error_buf,
  1658. error_buf_size)) {
  1659. goto fail;
  1660. }
  1661. type->types[i] = ref_type.ref_type;
  1662. if (need_ref_type_map) {
  1663. type->ref_type_maps[j].index = i;
  1664. if (!(type->ref_type_maps[j++].ref_type =
  1665. reftype_set_insert(module->ref_type_set, &ref_type,
  1666. error_buf, error_buf_size))) {
  1667. goto fail;
  1668. }
  1669. }
  1670. }
  1671. read_leb_uint32(p, p_end, result_count);
  1672. for (i = 0; i < result_count; i++) {
  1673. if (!resolve_value_type(&p, p_end, module, type_count,
  1674. &need_ref_type_map, &ref_type, false, error_buf,
  1675. error_buf_size)) {
  1676. goto fail;
  1677. }
  1678. type->types[param_count + i] = ref_type.ref_type;
  1679. if (need_ref_type_map) {
  1680. type->ref_type_maps[j].index = param_count + i;
  1681. if (!(type->ref_type_maps[j++].ref_type =
  1682. reftype_set_insert(module->ref_type_set, &ref_type,
  1683. error_buf, error_buf_size))) {
  1684. goto fail;
  1685. }
  1686. }
  1687. }
  1688. bh_assert(j == type->ref_type_map_count);
  1689. #if TRACE_WASM_LOADER != 0
  1690. os_printf("type %d = ", type_idx);
  1691. wasm_dump_func_type(type);
  1692. #endif
  1693. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1694. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1695. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1696. set_error_buf(error_buf, error_buf_size,
  1697. "param count or result count too large");
  1698. goto fail;
  1699. }
  1700. type->param_cell_num = (uint16)param_cell_num;
  1701. type->ret_cell_num = (uint16)ret_cell_num;
  1702. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1703. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1704. #endif
  1705. #if WASM_ENABLE_WAMR_COMPILER != 0
  1706. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1707. if (type->types[i] == VALUE_TYPE_V128)
  1708. module->is_simd_used = true;
  1709. }
  1710. #endif
  1711. *p_buf = p;
  1712. module->types[type_idx] = (WASMType *)type;
  1713. return true;
  1714. fail:
  1715. if (type)
  1716. destroy_func_type(type);
  1717. return false;
  1718. }
  1719. static bool
  1720. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1721. WASMModule *module, uint32 type_count, uint32 type_idx,
  1722. char *error_buf, uint32 error_buf_size)
  1723. {
  1724. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1725. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1726. uint32 i, j = 0, offset;
  1727. uint16 *reference_table;
  1728. uint64 total_size;
  1729. uint8 mutable;
  1730. bool need_ref_type_map;
  1731. WASMRefType ref_type;
  1732. WASMStructType *type = NULL;
  1733. /* Parse first time to resolve field count and ref type map count */
  1734. read_leb_uint32(p, p_end, field_count);
  1735. p_org = p;
  1736. for (i = 0; i < field_count; i++) {
  1737. if (!resolve_value_type(&p, p_end, module, type_count,
  1738. &need_ref_type_map, &ref_type, true, error_buf,
  1739. error_buf_size)) {
  1740. return false;
  1741. }
  1742. if (need_ref_type_map)
  1743. ref_type_map_count++;
  1744. if (wasm_is_type_reftype(ref_type.ref_type))
  1745. ref_field_count++;
  1746. CHECK_BUF(p, p_end, 1);
  1747. mutable = read_uint8(p);
  1748. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1749. return false;
  1750. }
  1751. }
  1752. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1753. type_idx, field_count, ref_type_map_count);
  1754. /* Parse second time to resolve field types and ref type map info */
  1755. p = p_org;
  1756. total_size = offsetof(WASMStructType, fields)
  1757. + sizeof(WASMStructFieldType) * (uint64)field_count
  1758. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1759. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1760. return false;
  1761. }
  1762. if (ref_type_map_count > 0) {
  1763. if (ref_type_map_count > UINT16_MAX) {
  1764. set_error_buf(error_buf, error_buf_size,
  1765. "ref type count too large");
  1766. return false;
  1767. }
  1768. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1769. if (!(type->ref_type_maps =
  1770. loader_malloc(total_size, error_buf, error_buf_size))) {
  1771. goto fail;
  1772. }
  1773. }
  1774. type->reference_table = reference_table =
  1775. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1776. + sizeof(WASMStructFieldType) * field_count);
  1777. *reference_table++ = ref_field_count;
  1778. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1779. type->field_count = field_count;
  1780. type->ref_type_map_count = ref_type_map_count;
  1781. offset = (uint32)offsetof(WASMStructObject, field_data);
  1782. for (i = 0; i < field_count; i++) {
  1783. if (!resolve_value_type(&p, p_end, module, type_count,
  1784. &need_ref_type_map, &ref_type, true, error_buf,
  1785. error_buf_size)) {
  1786. goto fail;
  1787. }
  1788. if (!is_valid_field_type(ref_type.ref_type)) {
  1789. set_error_buf(error_buf, error_buf_size, "invalid field type");
  1790. goto fail;
  1791. }
  1792. type->fields[i].field_type = ref_type.ref_type;
  1793. if (need_ref_type_map) {
  1794. type->ref_type_maps[j].index = i;
  1795. if (!(type->ref_type_maps[j++].ref_type =
  1796. reftype_set_insert(module->ref_type_set, &ref_type,
  1797. error_buf, error_buf_size))) {
  1798. goto fail;
  1799. }
  1800. }
  1801. CHECK_BUF(p, p_end, 1);
  1802. type->fields[i].field_flags = read_uint8(p);
  1803. type->fields[i].field_size =
  1804. (uint8)wasm_reftype_size(ref_type.ref_type);
  1805. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1806. || defined(BUILD_TARGET_X86_32))
  1807. if (type->fields[i].field_size == 2)
  1808. offset = align_uint(offset, 2);
  1809. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1810. offset = align_uint(offset, 4);
  1811. #endif
  1812. type->fields[i].field_offset = offset;
  1813. if (wasm_is_type_reftype(ref_type.ref_type))
  1814. *reference_table++ = offset;
  1815. offset += type->fields[i].field_size;
  1816. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1817. type->fields[i].field_flags, type->fields[i].field_type);
  1818. }
  1819. type->total_size = offset;
  1820. bh_assert(j == type->ref_type_map_count);
  1821. #if TRACE_WASM_LOADER != 0
  1822. os_printf("type %d = ", type_idx);
  1823. wasm_dump_struct_type(type);
  1824. #endif
  1825. *p_buf = p;
  1826. module->types[type_idx] = (WASMType *)type;
  1827. return true;
  1828. fail:
  1829. if (type)
  1830. destroy_struct_type(type);
  1831. return false;
  1832. }
  1833. static bool
  1834. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1835. WASMModule *module, uint32 type_count, uint32 type_idx,
  1836. char *error_buf, uint32 error_buf_size)
  1837. {
  1838. const uint8 *p = *p_buf, *p_end = buf_end;
  1839. uint8 mutable;
  1840. bool need_ref_type_map;
  1841. WASMRefType ref_type;
  1842. WASMArrayType *type = NULL;
  1843. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1844. &ref_type, true, error_buf, error_buf_size)) {
  1845. return false;
  1846. }
  1847. CHECK_BUF(p, p_end, 1);
  1848. mutable = read_uint8(p);
  1849. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1850. return false;
  1851. }
  1852. LOG_VERBOSE("type %u: array", type_idx);
  1853. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1854. error_buf_size))) {
  1855. return false;
  1856. }
  1857. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1858. type->elem_flags = mutable;
  1859. type->elem_type = ref_type.ref_type;
  1860. if (need_ref_type_map) {
  1861. if (!(type->elem_ref_type =
  1862. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1863. error_buf_size))) {
  1864. goto fail;
  1865. }
  1866. }
  1867. #if TRACE_WASM_LOADER != 0
  1868. os_printf("type %d = ", type_idx);
  1869. wasm_dump_array_type(type);
  1870. #endif
  1871. *p_buf = p;
  1872. module->types[type_idx] = (WASMType *)type;
  1873. return true;
  1874. fail:
  1875. if (type)
  1876. destroy_array_type(type);
  1877. return false;
  1878. }
  1879. static bool
  1880. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1881. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1882. {
  1883. if (heap_type >= 0) {
  1884. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1885. error_buf_size)) {
  1886. return false;
  1887. }
  1888. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1889. heap_type);
  1890. }
  1891. else {
  1892. if (!wasm_is_valid_heap_type(heap_type)) {
  1893. set_error_buf(error_buf, error_buf_size, "unknown type");
  1894. return false;
  1895. }
  1896. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1897. heap_type);
  1898. if (nullable) {
  1899. /* For (ref null func/extern/any/eq/i31/data),
  1900. they are same as
  1901. funcref/externref/anyref/eqref/i31ref/dataref,
  1902. we convert the multi-byte type to one-byte
  1903. type to reduce the footprint and the
  1904. complexity of type equal/subtype checking */
  1905. ref_type->ref_type =
  1906. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1907. }
  1908. }
  1909. return true;
  1910. }
  1911. static void
  1912. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1913. WASMRefType *ref_type2)
  1914. {
  1915. /**
  1916. * The difference rt1 ∖ rt2 between two reference types is defined as
  1917. * follows:
  1918. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1919. * (ref ht2) = (ref null?1 ht1)
  1920. */
  1921. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1922. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1923. ref_type1, wasm_reftype_struct_size(ref_type1));
  1924. }
  1925. else {
  1926. ref_type_diff->ref_type = ref_type1->ref_type;
  1927. }
  1928. if (ref_type2->ref_ht_common.nullable) {
  1929. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1930. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1931. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1932. (int32)ref_type_diff->ref_type - 0x80);
  1933. }
  1934. else {
  1935. ref_type_diff->ref_ht_typeidx.nullable = false;
  1936. }
  1937. }
  1938. }
  1939. #else /* else of WASM_ENABLE_GC != 0 */
  1940. static void
  1941. destroy_wasm_type(WASMType *type)
  1942. {
  1943. if (type->ref_count > 1) {
  1944. /* The type is referenced by other types
  1945. of current wasm module */
  1946. type->ref_count--;
  1947. return;
  1948. }
  1949. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1950. && WASM_ENABLE_LAZY_JIT != 0
  1951. if (type->call_to_llvm_jit_from_fast_jit)
  1952. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1953. #endif
  1954. wasm_runtime_free(type);
  1955. }
  1956. #endif /* end of WASM_ENABLE_GC != 0 */
  1957. static bool
  1958. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1959. char *error_buf, uint32 error_buf_size)
  1960. {
  1961. const uint8 *p = buf, *p_end = buf_end;
  1962. uint32 type_count, i;
  1963. uint64 total_size;
  1964. uint8 flag;
  1965. #if WASM_ENABLE_GC != 0
  1966. uint32 processed_type_count = 0;
  1967. #endif
  1968. read_leb_uint32(p, p_end, type_count);
  1969. if (type_count) {
  1970. module->type_count = type_count;
  1971. total_size = sizeof(WASMType *) * (uint64)type_count;
  1972. if (!(module->types =
  1973. loader_malloc(total_size, error_buf, error_buf_size))) {
  1974. return false;
  1975. }
  1976. #if WASM_ENABLE_GC == 0
  1977. for (i = 0; i < type_count; i++) {
  1978. WASMFuncType *type;
  1979. const uint8 *p_org;
  1980. uint32 param_count, result_count, j;
  1981. uint32 param_cell_num, ret_cell_num;
  1982. CHECK_BUF(p, p_end, 1);
  1983. flag = read_uint8(p);
  1984. if (flag != 0x60) {
  1985. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1986. return false;
  1987. }
  1988. read_leb_uint32(p, p_end, param_count);
  1989. /* Resolve param count and result count firstly */
  1990. p_org = p;
  1991. CHECK_BUF(p, p_end, param_count);
  1992. p += param_count;
  1993. read_leb_uint32(p, p_end, result_count);
  1994. CHECK_BUF(p, p_end, result_count);
  1995. p = p_org;
  1996. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1997. set_error_buf(error_buf, error_buf_size,
  1998. "param count or result count too large");
  1999. return false;
  2000. }
  2001. total_size = offsetof(WASMFuncType, types)
  2002. + sizeof(uint8) * (uint64)(param_count + result_count);
  2003. if (!(type = module->types[i] =
  2004. loader_malloc(total_size, error_buf, error_buf_size))) {
  2005. return false;
  2006. }
  2007. /* Resolve param types and result types */
  2008. type->ref_count = 1;
  2009. type->param_count = (uint16)param_count;
  2010. type->result_count = (uint16)result_count;
  2011. for (j = 0; j < param_count; j++) {
  2012. CHECK_BUF(p, p_end, 1);
  2013. type->types[j] = read_uint8(p);
  2014. }
  2015. read_leb_uint32(p, p_end, result_count);
  2016. for (j = 0; j < result_count; j++) {
  2017. CHECK_BUF(p, p_end, 1);
  2018. type->types[param_count + j] = read_uint8(p);
  2019. }
  2020. for (j = 0; j < param_count + result_count; j++) {
  2021. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  2022. set_error_buf(error_buf, error_buf_size,
  2023. "unknown value type");
  2024. return false;
  2025. }
  2026. }
  2027. param_cell_num = wasm_get_cell_num(type->types, param_count);
  2028. ret_cell_num =
  2029. wasm_get_cell_num(type->types + param_count, result_count);
  2030. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  2031. set_error_buf(error_buf, error_buf_size,
  2032. "param count or result count too large");
  2033. return false;
  2034. }
  2035. type->param_cell_num = (uint16)param_cell_num;
  2036. type->ret_cell_num = (uint16)ret_cell_num;
  2037. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  2038. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  2039. #endif
  2040. #if WASM_ENABLE_WAMR_COMPILER != 0
  2041. for (j = 0; j < type->param_count + type->result_count; j++) {
  2042. if (type->types[j] == VALUE_TYPE_V128)
  2043. module->is_simd_used = true;
  2044. else if (type->types[j] == VALUE_TYPE_FUNCREF
  2045. || type->types[j] == VALUE_TYPE_EXTERNREF)
  2046. module->is_ref_types_used = true;
  2047. }
  2048. #endif
  2049. /* If there is already a same type created, use it instead */
  2050. for (j = 0; j < i; j++) {
  2051. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  2052. if (module->types[j]->ref_count == UINT16_MAX) {
  2053. set_error_buf(error_buf, error_buf_size,
  2054. "wasm type's ref count too large");
  2055. return false;
  2056. }
  2057. destroy_wasm_type(type);
  2058. module->types[i] = module->types[j];
  2059. module->types[j]->ref_count++;
  2060. break;
  2061. }
  2062. }
  2063. }
  2064. #else /* else of WASM_ENABLE_GC == 0 */
  2065. for (i = 0; i < type_count; i++) {
  2066. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  2067. uint32 rec_count = 1, j;
  2068. bool is_sub_final = true;
  2069. CHECK_BUF(p, p_end, 1);
  2070. flag = read_uint8(p);
  2071. if (flag == DEFINED_TYPE_REC) {
  2072. read_leb_uint32(p, p_end, rec_count);
  2073. if (rec_count > 1) {
  2074. uint64 new_total_size;
  2075. /* integer overflow */
  2076. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  2077. set_error_buf(error_buf, error_buf_size,
  2078. "recursive type count too large");
  2079. return false;
  2080. }
  2081. new_total_size =
  2082. sizeof(WASMFuncType *)
  2083. * (uint64)(module->type_count + rec_count - 1);
  2084. if (new_total_size > UINT32_MAX) {
  2085. set_error_buf(error_buf, error_buf_size,
  2086. "allocate memory failed");
  2087. return false;
  2088. }
  2089. MEM_REALLOC(module->types, (uint32)total_size,
  2090. (uint32)new_total_size);
  2091. module->type_count += rec_count - 1;
  2092. total_size = new_total_size;
  2093. }
  2094. if (rec_count < 1) {
  2095. LOG_VERBOSE("Processing 0-entry rec group");
  2096. }
  2097. else {
  2098. LOG_VERBOSE("Processing rec group [%d-%d]",
  2099. processed_type_count,
  2100. processed_type_count + rec_count - 1);
  2101. }
  2102. }
  2103. else {
  2104. p--;
  2105. }
  2106. for (j = 0; j < rec_count; j++) {
  2107. WASMType *cur_type = NULL;
  2108. CHECK_BUF(p, p_end, 1);
  2109. flag = read_uint8(p);
  2110. parent_type_idx = -1;
  2111. if (flag == DEFINED_TYPE_SUB
  2112. || flag == DEFINED_TYPE_SUB_FINAL) {
  2113. read_leb_uint32(p, p_end, super_type_count);
  2114. if (super_type_count > 1) {
  2115. set_error_buf(error_buf, error_buf_size,
  2116. "super type count too large");
  2117. return false;
  2118. }
  2119. if (super_type_count > 0) {
  2120. read_leb_uint32(p, p_end, parent_type_idx);
  2121. if (parent_type_idx >= processed_type_count + j) {
  2122. set_error_buf_v(error_buf, error_buf_size,
  2123. "unknown type %d", parent_type_idx);
  2124. return false;
  2125. }
  2126. if (module->types[parent_type_idx]->is_sub_final) {
  2127. set_error_buf(error_buf, error_buf_size,
  2128. "sub type can not inherit from "
  2129. "a final super type");
  2130. return false;
  2131. }
  2132. }
  2133. if (flag == DEFINED_TYPE_SUB)
  2134. is_sub_final = false;
  2135. CHECK_BUF(p, p_end, 1);
  2136. flag = read_uint8(p);
  2137. }
  2138. if (flag == DEFINED_TYPE_FUNC) {
  2139. if (!resolve_func_type(&p, buf_end, module,
  2140. processed_type_count + rec_count,
  2141. processed_type_count + j, error_buf,
  2142. error_buf_size)) {
  2143. return false;
  2144. }
  2145. }
  2146. else if (flag == DEFINED_TYPE_STRUCT) {
  2147. if (!resolve_struct_type(&p, buf_end, module,
  2148. processed_type_count + rec_count,
  2149. processed_type_count + j,
  2150. error_buf, error_buf_size)) {
  2151. return false;
  2152. }
  2153. }
  2154. else if (flag == DEFINED_TYPE_ARRAY) {
  2155. if (!resolve_array_type(&p, buf_end, module,
  2156. processed_type_count + rec_count,
  2157. processed_type_count + j, error_buf,
  2158. error_buf_size)) {
  2159. return false;
  2160. }
  2161. }
  2162. else {
  2163. set_error_buf(error_buf, error_buf_size,
  2164. "invalid type flag");
  2165. return false;
  2166. }
  2167. cur_type = module->types[processed_type_count + j];
  2168. cur_type->ref_count = 1;
  2169. cur_type->parent_type_idx = parent_type_idx;
  2170. cur_type->is_sub_final = is_sub_final;
  2171. cur_type->rec_count = rec_count;
  2172. cur_type->rec_idx = j;
  2173. cur_type->rec_begin_type_idx = processed_type_count;
  2174. }
  2175. /* resolve subtyping relationship in current rec group */
  2176. for (j = 0; j < rec_count; j++) {
  2177. WASMType *cur_type = module->types[processed_type_count + j];
  2178. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2179. WASMType *parent_type =
  2180. module->types[cur_type->parent_type_idx];
  2181. cur_type->parent_type = parent_type;
  2182. cur_type->root_type = parent_type->root_type;
  2183. if (parent_type->inherit_depth == UINT16_MAX) {
  2184. set_error_buf(error_buf, error_buf_size,
  2185. "parent type's inherit depth too large");
  2186. return false;
  2187. }
  2188. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  2189. }
  2190. else {
  2191. cur_type->parent_type = NULL;
  2192. cur_type->root_type = cur_type;
  2193. cur_type->inherit_depth = 0;
  2194. }
  2195. }
  2196. for (j = 0; j < rec_count; j++) {
  2197. WASMType *cur_type = module->types[processed_type_count + j];
  2198. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2199. WASMType *parent_type =
  2200. module->types[cur_type->parent_type_idx];
  2201. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  2202. module->types,
  2203. module->type_count)) {
  2204. set_error_buf_v(error_buf, error_buf_size,
  2205. "sub type %u does not match super type",
  2206. processed_type_count + j);
  2207. return false;
  2208. }
  2209. }
  2210. }
  2211. /* If there is already an equivalence type or a group of equivalence
  2212. recursive types created, use it or them instead */
  2213. for (j = 0; j < processed_type_count;) {
  2214. WASMType *src_type = module->types[j];
  2215. WASMType *cur_type = module->types[processed_type_count];
  2216. uint32 k, src_rec_count;
  2217. src_rec_count = src_type->rec_count;
  2218. if (src_rec_count != rec_count) {
  2219. /* no type equivalence */
  2220. j += src_rec_count;
  2221. continue;
  2222. }
  2223. for (k = 0; k < rec_count; k++) {
  2224. src_type = module->types[j + k];
  2225. cur_type = module->types[processed_type_count + k];
  2226. if (!wasm_type_equal(src_type, cur_type, module->types,
  2227. module->type_count)) {
  2228. break;
  2229. }
  2230. }
  2231. if (k < rec_count) {
  2232. /* no type equivalence */
  2233. j += src_rec_count;
  2234. continue;
  2235. }
  2236. /* type equivalence */
  2237. for (k = 0; k < rec_count; k++) {
  2238. if (module->types[j + k]->ref_count == UINT16_MAX) {
  2239. set_error_buf(error_buf, error_buf_size,
  2240. "wasm type's ref count too large");
  2241. return false;
  2242. }
  2243. destroy_wasm_type(module->types[processed_type_count + k]);
  2244. module->types[processed_type_count + k] =
  2245. module->types[j + k];
  2246. module->types[j + k]->ref_count++;
  2247. }
  2248. break;
  2249. }
  2250. if (rec_count > 1) {
  2251. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  2252. processed_type_count,
  2253. processed_type_count + rec_count - 1);
  2254. }
  2255. processed_type_count += rec_count;
  2256. }
  2257. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  2258. * module->type_count,
  2259. error_buf, error_buf_size))) {
  2260. return false;
  2261. }
  2262. #endif /* end of WASM_ENABLE_GC == 0 */
  2263. }
  2264. for (i = 0; i < module->type_count; i++) {
  2265. if (module->types[i] == NULL) {
  2266. set_error_buf_v(error_buf, error_buf_size, "unknown type %d", i);
  2267. return false;
  2268. }
  2269. }
  2270. if (p != p_end) {
  2271. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2272. return false;
  2273. }
  2274. LOG_VERBOSE("Load type section success.\n");
  2275. return true;
  2276. fail:
  2277. return false;
  2278. }
  2279. static void
  2280. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  2281. uint32 *max_size)
  2282. {
  2283. uint32 default_max_size;
  2284. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  2285. * ABI unchanged */
  2286. (void)is_table64;
  2287. if (UINT32_MAX / 2 > init_size)
  2288. default_max_size = init_size * 2;
  2289. else
  2290. default_max_size = UINT32_MAX;
  2291. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2292. default_max_size = WASM_TABLE_MAX_SIZE;
  2293. if (max_size_flag) {
  2294. /* module defines the table limitation */
  2295. bh_assert(init_size <= *max_size);
  2296. if (init_size < *max_size) {
  2297. *max_size =
  2298. *max_size < default_max_size ? *max_size : default_max_size;
  2299. }
  2300. }
  2301. else {
  2302. /* partial defined table limitation, gives a default value */
  2303. *max_size = default_max_size;
  2304. }
  2305. }
  2306. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2307. /**
  2308. * Find export item of a module with export info:
  2309. * module name, field name and export kind
  2310. */
  2311. static WASMExport *
  2312. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2313. const char *field_name, uint8 export_kind,
  2314. char *error_buf, uint32 error_buf_size)
  2315. {
  2316. WASMExport *export =
  2317. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2318. export_kind, error_buf, error_buf_size);
  2319. return export;
  2320. }
  2321. #endif
  2322. #if WASM_ENABLE_MULTI_MODULE != 0
  2323. static WASMTable *
  2324. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2325. uint32 init_size, uint32 max_size, char *error_buf,
  2326. uint32 error_buf_size)
  2327. {
  2328. WASMModuleCommon *module_reg;
  2329. WASMTable *table = NULL;
  2330. WASMExport *export = NULL;
  2331. WASMModule *module = NULL;
  2332. module_reg = wasm_runtime_find_module_registered(module_name);
  2333. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2334. LOG_DEBUG("can not find a module named %s for table", module_name);
  2335. set_error_buf(error_buf, error_buf_size, "unknown import");
  2336. return NULL;
  2337. }
  2338. module = (WASMModule *)module_reg;
  2339. export =
  2340. wasm_loader_find_export(module, module_name, table_name,
  2341. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2342. if (!export) {
  2343. return NULL;
  2344. }
  2345. /* resolve table and check the init/max size */
  2346. if (export->index < module->import_table_count) {
  2347. table =
  2348. module->import_tables[export->index].u.table.import_table_linked;
  2349. }
  2350. else {
  2351. table = &(module->tables[export->index - module->import_table_count]);
  2352. }
  2353. if (table->table_type.init_size < init_size
  2354. || table->table_type.max_size > max_size) {
  2355. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2356. module_name, table_name, table->table_type.init_size,
  2357. table->table_type.max_size, init_size, max_size);
  2358. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2359. return NULL;
  2360. }
  2361. return table;
  2362. }
  2363. static WASMMemory *
  2364. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2365. uint32 init_page_count, uint32 max_page_count,
  2366. char *error_buf, uint32 error_buf_size)
  2367. {
  2368. WASMModuleCommon *module_reg;
  2369. WASMMemory *memory = NULL;
  2370. WASMExport *export = NULL;
  2371. WASMModule *module = NULL;
  2372. module_reg = wasm_runtime_find_module_registered(module_name);
  2373. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2374. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2375. set_error_buf(error_buf, error_buf_size, "unknown import");
  2376. return NULL;
  2377. }
  2378. module = (WASMModule *)module_reg;
  2379. export =
  2380. wasm_loader_find_export(module, module_name, memory_name,
  2381. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2382. if (!export) {
  2383. return NULL;
  2384. }
  2385. /* resolve memory and check the init/max page count */
  2386. if (export->index < module->import_memory_count) {
  2387. memory = module->import_memories[export->index]
  2388. .u.memory.import_memory_linked;
  2389. }
  2390. else {
  2391. memory =
  2392. &(module->memories[export->index - module->import_memory_count]);
  2393. }
  2394. if (memory->init_page_count < init_page_count
  2395. || memory->max_page_count > max_page_count) {
  2396. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2397. module_name, memory_name, memory->init_page_count,
  2398. memory->max_page_count, init_page_count, max_page_count);
  2399. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2400. return NULL;
  2401. }
  2402. return memory;
  2403. }
  2404. static WASMGlobal *
  2405. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2406. uint8 type, bool is_mutable, char *error_buf,
  2407. uint32 error_buf_size)
  2408. {
  2409. WASMModuleCommon *module_reg;
  2410. WASMGlobal *global = NULL;
  2411. WASMExport *export = NULL;
  2412. WASMModule *module = NULL;
  2413. module_reg = wasm_runtime_find_module_registered(module_name);
  2414. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2415. LOG_DEBUG("can not find a module named %s for global", module_name);
  2416. set_error_buf(error_buf, error_buf_size, "unknown import");
  2417. return NULL;
  2418. }
  2419. module = (WASMModule *)module_reg;
  2420. export =
  2421. wasm_loader_find_export(module, module_name, global_name,
  2422. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2423. if (!export) {
  2424. return NULL;
  2425. }
  2426. /* resolve and check the global */
  2427. if (export->index < module->import_global_count) {
  2428. global =
  2429. module->import_globals[export->index].u.global.import_global_linked;
  2430. }
  2431. else {
  2432. global =
  2433. &(module->globals[export->index - module->import_global_count]);
  2434. }
  2435. if (global->type.val_type != type
  2436. || global->type.is_mutable != is_mutable) {
  2437. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2438. module_name, global_name, global->type.val_type,
  2439. global->type.is_mutable, type, is_mutable);
  2440. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2441. return NULL;
  2442. }
  2443. return global;
  2444. }
  2445. #if WASM_ENABLE_TAGS != 0
  2446. static WASMTag *
  2447. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2448. const WASMType *expected_tag_type,
  2449. uint32 *linked_tag_index, char *error_buf,
  2450. uint32 error_buf_size)
  2451. {
  2452. WASMModuleCommon *module_reg;
  2453. WASMTag *tag = NULL;
  2454. WASMExport *export = NULL;
  2455. WASMModule *module = NULL;
  2456. module_reg = wasm_runtime_find_module_registered(module_name);
  2457. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2458. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2459. tag_name);
  2460. set_error_buf(error_buf, error_buf_size, "unknown import");
  2461. return NULL;
  2462. }
  2463. module = (WASMModule *)module_reg;
  2464. export =
  2465. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2466. error_buf, error_buf_size);
  2467. if (!export) {
  2468. return NULL;
  2469. }
  2470. /* resolve tag type and tag */
  2471. if (export->index < module->import_tag_count) {
  2472. /* importing an imported tag from the submodule */
  2473. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2474. }
  2475. else {
  2476. /* importing an section tag from the submodule */
  2477. tag = module->tags[export->index - module->import_tag_count];
  2478. }
  2479. /* check function type */
  2480. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2481. module->type_count)) {
  2482. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2483. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2484. return NULL;
  2485. }
  2486. if (linked_tag_index != NULL) {
  2487. *linked_tag_index = export->index;
  2488. }
  2489. return tag;
  2490. }
  2491. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2492. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2493. static bool
  2494. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2495. const WASMModule *parent_module,
  2496. const char *sub_module_name, const char *function_name,
  2497. WASMFunctionImport *function, bool no_resolve,
  2498. char *error_buf, uint32 error_buf_size)
  2499. {
  2500. const uint8 *p = *p_buf, *p_end = buf_end;
  2501. uint32 declare_type_index = 0;
  2502. read_leb_uint32(p, p_end, declare_type_index);
  2503. *p_buf = p;
  2504. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2505. error_buf_size)) {
  2506. return false;
  2507. }
  2508. #if WASM_ENABLE_GC != 0
  2509. function->type_idx = declare_type_index;
  2510. #endif
  2511. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2512. declare_type_index = wasm_get_smallest_type_idx(
  2513. parent_module->types, parent_module->type_count, declare_type_index);
  2514. #endif
  2515. function->func_type =
  2516. (WASMFuncType *)parent_module->types[declare_type_index];
  2517. function->module_name = (char *)sub_module_name;
  2518. function->field_name = (char *)function_name;
  2519. function->attachment = NULL;
  2520. function->signature = NULL;
  2521. function->call_conv_raw = false;
  2522. /* lookup registered native symbols first */
  2523. if (!no_resolve) {
  2524. wasm_resolve_import_func(parent_module, function);
  2525. }
  2526. return true;
  2527. fail:
  2528. return false;
  2529. }
  2530. static bool
  2531. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2532. uint32 error_buf_size)
  2533. {
  2534. if (max_size < init_size) {
  2535. set_error_buf(error_buf, error_buf_size,
  2536. "size minimum must not be greater than maximum");
  2537. return false;
  2538. }
  2539. return true;
  2540. }
  2541. static bool
  2542. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2543. WASMModule *parent_module, const char *sub_module_name,
  2544. const char *table_name, WASMTableImport *table,
  2545. char *error_buf, uint32 error_buf_size)
  2546. {
  2547. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2548. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2549. declare_max_size = 0;
  2550. #if WASM_ENABLE_MULTI_MODULE != 0
  2551. WASMModule *sub_module = NULL;
  2552. WASMTable *linked_table = NULL;
  2553. #endif
  2554. #if WASM_ENABLE_GC != 0
  2555. WASMRefType ref_type;
  2556. bool need_ref_type_map;
  2557. #endif
  2558. bool is_table64 = false;
  2559. #if WASM_ENABLE_GC == 0
  2560. CHECK_BUF(p, p_end, 1);
  2561. /* 0x70 or 0x6F */
  2562. declare_elem_type = read_uint8(p);
  2563. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2564. #if WASM_ENABLE_REF_TYPES != 0
  2565. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2566. #endif
  2567. ) {
  2568. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2569. return false;
  2570. }
  2571. #else /* else of WASM_ENABLE_GC == 0 */
  2572. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2573. &need_ref_type_map, &ref_type, false, error_buf,
  2574. error_buf_size)) {
  2575. return false;
  2576. }
  2577. if (!wasm_is_type_reftype(ref_type.ref_type)
  2578. || wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2579. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2580. return false;
  2581. }
  2582. declare_elem_type = ref_type.ref_type;
  2583. if (need_ref_type_map) {
  2584. if (!(table->table_type.elem_ref_type =
  2585. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2586. error_buf, error_buf_size))) {
  2587. return false;
  2588. }
  2589. }
  2590. #if TRACE_WASM_LOADER != 0
  2591. os_printf("import table type: ");
  2592. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2593. os_printf("\n");
  2594. #endif
  2595. #endif /* end of WASM_ENABLE_GC == 0 */
  2596. p_org = p;
  2597. read_leb_uint32(p, p_end, table_flag);
  2598. is_table64 = table_flag & TABLE64_FLAG;
  2599. if (p - p_org > 1) {
  2600. LOG_VERBOSE("integer representation too long(import table)");
  2601. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2602. return false;
  2603. }
  2604. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2605. return false;
  2606. }
  2607. read_leb_uint32(p, p_end, declare_init_size);
  2608. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2609. read_leb_uint32(p, p_end, declare_max_size);
  2610. if (!check_table_max_size(declare_init_size, declare_max_size,
  2611. error_buf, error_buf_size))
  2612. return false;
  2613. }
  2614. adjust_table_max_size(is_table64, declare_init_size,
  2615. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2616. *p_buf = p;
  2617. #if WASM_ENABLE_MULTI_MODULE != 0
  2618. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2619. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2620. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2621. error_buf_size);
  2622. if (sub_module) {
  2623. linked_table = wasm_loader_resolve_table(
  2624. sub_module_name, table_name, declare_init_size,
  2625. declare_max_size, error_buf, error_buf_size);
  2626. if (linked_table) {
  2627. /* reset with linked table limit */
  2628. declare_elem_type = linked_table->table_type.elem_type;
  2629. declare_init_size = linked_table->table_type.init_size;
  2630. declare_max_size = linked_table->table_type.max_size;
  2631. table_flag = linked_table->table_type.flags;
  2632. table->import_table_linked = linked_table;
  2633. table->import_module = sub_module;
  2634. }
  2635. }
  2636. }
  2637. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2638. /* (table (export "table") 10 20 funcref) */
  2639. /* (table (export "table64") 10 20 funcref) */
  2640. /* we need this section working in wamrc */
  2641. if (!strcmp("spectest", sub_module_name)) {
  2642. const uint32 spectest_table_init_size = 10;
  2643. const uint32 spectest_table_max_size = 20;
  2644. if (strcmp("table", table_name)
  2645. #if WASM_ENABLE_MEMORY64 != 0
  2646. && strcmp("table64", table_name)
  2647. #endif
  2648. ) {
  2649. set_error_buf(error_buf, error_buf_size,
  2650. "incompatible import type or unknown import");
  2651. return false;
  2652. }
  2653. if (declare_init_size > spectest_table_init_size
  2654. || declare_max_size < spectest_table_max_size) {
  2655. set_error_buf(error_buf, error_buf_size,
  2656. "incompatible import type");
  2657. return false;
  2658. }
  2659. declare_init_size = spectest_table_init_size;
  2660. declare_max_size = spectest_table_max_size;
  2661. }
  2662. /* now we believe all declaration are ok */
  2663. table->table_type.elem_type = declare_elem_type;
  2664. table->table_type.init_size = declare_init_size;
  2665. table->table_type.flags = table_flag;
  2666. table->table_type.max_size = declare_max_size;
  2667. #if WASM_ENABLE_WAMR_COMPILER != 0
  2668. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2669. parent_module->is_ref_types_used = true;
  2670. #endif
  2671. (void)parent_module;
  2672. return true;
  2673. fail:
  2674. return false;
  2675. }
  2676. static bool
  2677. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2678. uint32 error_buf_size)
  2679. {
  2680. uint32 default_max_size =
  2681. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2682. if (!is_memory64 && init_size > default_max_size) {
  2683. set_error_buf(error_buf, error_buf_size,
  2684. "memory size must be at most 65536 pages (4GiB)");
  2685. return false;
  2686. }
  2687. #if WASM_ENABLE_MEMORY64 != 0
  2688. else if (is_memory64 && init_size > default_max_size) {
  2689. set_error_buf(
  2690. error_buf, error_buf_size,
  2691. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2692. return false;
  2693. }
  2694. #endif
  2695. return true;
  2696. }
  2697. static bool
  2698. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2699. char *error_buf, uint32 error_buf_size)
  2700. {
  2701. uint32 default_max_size =
  2702. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2703. if (max_size < init_size) {
  2704. set_error_buf(error_buf, error_buf_size,
  2705. "size minimum must not be greater than maximum");
  2706. return false;
  2707. }
  2708. if (!is_memory64 && max_size > default_max_size) {
  2709. set_error_buf(error_buf, error_buf_size,
  2710. "memory size must be at most 65536 pages (4GiB)");
  2711. return false;
  2712. }
  2713. #if WASM_ENABLE_MEMORY64 != 0
  2714. else if (is_memory64 && max_size > default_max_size) {
  2715. set_error_buf(
  2716. error_buf, error_buf_size,
  2717. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2718. return false;
  2719. }
  2720. #endif
  2721. return true;
  2722. }
  2723. static bool
  2724. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2725. WASMModule *parent_module, const char *sub_module_name,
  2726. const char *memory_name, WASMMemoryImport *memory,
  2727. char *error_buf, uint32 error_buf_size)
  2728. {
  2729. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2730. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2731. uint32 pool_size = wasm_runtime_memory_pool_size();
  2732. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2733. / DEFAULT_NUM_BYTES_PER_PAGE;
  2734. #else
  2735. uint32 max_page_count;
  2736. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2737. uint32 mem_flag = 0;
  2738. bool is_memory64 = false;
  2739. uint32 declare_init_page_count = 0;
  2740. uint32 declare_max_page_count = 0;
  2741. #if WASM_ENABLE_MULTI_MODULE != 0
  2742. WASMModule *sub_module = NULL;
  2743. WASMMemory *linked_memory = NULL;
  2744. #endif
  2745. p_org = p;
  2746. read_leb_uint32(p, p_end, mem_flag);
  2747. is_memory64 = mem_flag & MEMORY64_FLAG;
  2748. if (p - p_org > 1) {
  2749. LOG_VERBOSE("integer representation too long(import memory)");
  2750. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2751. return false;
  2752. }
  2753. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2754. return false;
  2755. }
  2756. read_leb_uint32(p, p_end, declare_init_page_count);
  2757. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2758. error_buf_size)) {
  2759. return false;
  2760. }
  2761. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2762. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2763. #endif
  2764. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2765. read_leb_uint32(p, p_end, declare_max_page_count);
  2766. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2767. declare_max_page_count, error_buf,
  2768. error_buf_size)) {
  2769. return false;
  2770. }
  2771. if (declare_max_page_count > max_page_count) {
  2772. declare_max_page_count = max_page_count;
  2773. }
  2774. }
  2775. else {
  2776. /* Limit the maximum memory size to max_page_count */
  2777. declare_max_page_count = max_page_count;
  2778. }
  2779. #if WASM_ENABLE_MULTI_MODULE != 0
  2780. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2781. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2782. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2783. error_buf_size);
  2784. if (sub_module) {
  2785. linked_memory = wasm_loader_resolve_memory(
  2786. sub_module_name, memory_name, declare_init_page_count,
  2787. declare_max_page_count, error_buf, error_buf_size);
  2788. if (linked_memory) {
  2789. /**
  2790. * reset with linked memory limit
  2791. */
  2792. memory->import_module = sub_module;
  2793. memory->import_memory_linked = linked_memory;
  2794. declare_init_page_count = linked_memory->init_page_count;
  2795. declare_max_page_count = linked_memory->max_page_count;
  2796. }
  2797. }
  2798. }
  2799. #endif
  2800. /* (memory (export "memory") 1 2) */
  2801. if (!strcmp("spectest", sub_module_name)) {
  2802. uint32 spectest_memory_init_page = 1;
  2803. uint32 spectest_memory_max_page = 2;
  2804. if (strcmp("memory", memory_name)) {
  2805. set_error_buf(error_buf, error_buf_size,
  2806. "incompatible import type or unknown import");
  2807. return false;
  2808. }
  2809. if (declare_init_page_count > spectest_memory_init_page
  2810. || declare_max_page_count < spectest_memory_max_page) {
  2811. set_error_buf(error_buf, error_buf_size,
  2812. "incompatible import type");
  2813. return false;
  2814. }
  2815. declare_init_page_count = spectest_memory_init_page;
  2816. declare_max_page_count = spectest_memory_max_page;
  2817. }
  2818. #if WASM_ENABLE_WASI_TEST != 0
  2819. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2820. else if (!strcmp("foo", sub_module_name)) {
  2821. uint32 spectest_memory_init_page = 1;
  2822. uint32 spectest_memory_max_page = 1;
  2823. if (strcmp("bar", memory_name)) {
  2824. set_error_buf(error_buf, error_buf_size,
  2825. "incompatible import type or unknown import");
  2826. return false;
  2827. }
  2828. if (declare_init_page_count > spectest_memory_init_page
  2829. || declare_max_page_count < spectest_memory_max_page) {
  2830. set_error_buf(error_buf, error_buf_size,
  2831. "incompatible import type");
  2832. return false;
  2833. }
  2834. declare_init_page_count = spectest_memory_init_page;
  2835. declare_max_page_count = spectest_memory_max_page;
  2836. }
  2837. #endif
  2838. /* now we believe all declaration are ok */
  2839. memory->mem_type.flags = mem_flag;
  2840. memory->mem_type.init_page_count = declare_init_page_count;
  2841. memory->mem_type.max_page_count = declare_max_page_count;
  2842. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2843. *p_buf = p;
  2844. (void)parent_module;
  2845. return true;
  2846. fail:
  2847. return false;
  2848. }
  2849. #if WASM_ENABLE_TAGS != 0
  2850. static bool
  2851. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2852. const WASMModule *parent_module, /* this module ! */
  2853. const char *sub_module_name, const char *tag_name,
  2854. WASMTagImport *tag, /* structure to fill */
  2855. char *error_buf, uint32 error_buf_size)
  2856. {
  2857. /* attribute and type of the import statement */
  2858. uint8 declare_tag_attribute;
  2859. uint32 declare_type_index;
  2860. const uint8 *p = *p_buf, *p_end = buf_end;
  2861. #if WASM_ENABLE_MULTI_MODULE != 0
  2862. WASMModule *sub_module = NULL;
  2863. #endif
  2864. /* get the one byte attribute */
  2865. CHECK_BUF(p, p_end, 1);
  2866. declare_tag_attribute = read_uint8(p);
  2867. if (declare_tag_attribute != 0) {
  2868. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2869. goto fail;
  2870. }
  2871. /* get type */
  2872. read_leb_uint32(p, p_end, declare_type_index);
  2873. /* compare against module->types */
  2874. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2875. error_buf_size)) {
  2876. goto fail;
  2877. }
  2878. WASMFuncType *declare_tag_type =
  2879. (WASMFuncType *)parent_module->types[declare_type_index];
  2880. /* check, that the type of the declared tag returns void */
  2881. if (declare_tag_type->result_count != 0) {
  2882. set_error_buf(error_buf, error_buf_size,
  2883. "tag type signature does not return void");
  2884. goto fail;
  2885. }
  2886. #if WASM_ENABLE_MULTI_MODULE != 0
  2887. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2888. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2889. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2890. error_buf_size);
  2891. if (sub_module) {
  2892. /* wasm_loader_resolve_tag checks, that the imported tag
  2893. * and the declared tag have the same type
  2894. */
  2895. uint32 linked_tag_index = 0;
  2896. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2897. sub_module_name, tag_name, declare_tag_type,
  2898. &linked_tag_index /* out */, error_buf, error_buf_size);
  2899. if (linked_tag) {
  2900. tag->import_module = sub_module;
  2901. tag->import_tag_linked = linked_tag;
  2902. tag->import_tag_index_linked = linked_tag_index;
  2903. }
  2904. }
  2905. }
  2906. #endif
  2907. /* store to module tag declarations */
  2908. tag->attribute = declare_tag_attribute;
  2909. tag->type = declare_type_index;
  2910. tag->module_name = (char *)sub_module_name;
  2911. tag->field_name = (char *)tag_name;
  2912. tag->tag_type = declare_tag_type;
  2913. *p_buf = p;
  2914. (void)parent_module;
  2915. LOG_VERBOSE("Load tag import success\n");
  2916. return true;
  2917. fail:
  2918. return false;
  2919. }
  2920. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2921. static bool
  2922. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2923. WASMModule *parent_module, char *sub_module_name,
  2924. char *global_name, WASMGlobalImport *global, char *error_buf,
  2925. uint32 error_buf_size)
  2926. {
  2927. const uint8 *p = *p_buf, *p_end = buf_end;
  2928. uint8 declare_type = 0;
  2929. uint8 declare_mutable = 0;
  2930. #if WASM_ENABLE_MULTI_MODULE != 0
  2931. WASMModule *sub_module = NULL;
  2932. WASMGlobal *linked_global = NULL;
  2933. #endif
  2934. #if WASM_ENABLE_GC != 0
  2935. WASMRefType ref_type;
  2936. bool need_ref_type_map;
  2937. #endif
  2938. bool ret = false;
  2939. #if WASM_ENABLE_GC == 0
  2940. CHECK_BUF(p, p_end, 2);
  2941. /* global type */
  2942. declare_type = read_uint8(p);
  2943. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2944. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2945. return false;
  2946. }
  2947. declare_mutable = read_uint8(p);
  2948. #else
  2949. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2950. &need_ref_type_map, &ref_type, false, error_buf,
  2951. error_buf_size)) {
  2952. return false;
  2953. }
  2954. declare_type = ref_type.ref_type;
  2955. if (need_ref_type_map) {
  2956. if (!(global->ref_type =
  2957. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2958. error_buf, error_buf_size))) {
  2959. return false;
  2960. }
  2961. }
  2962. #if TRACE_WASM_LOADER != 0
  2963. os_printf("import global type: ");
  2964. wasm_dump_value_type(declare_type, global->ref_type);
  2965. os_printf("\n");
  2966. #endif
  2967. CHECK_BUF(p, p_end, 1);
  2968. declare_mutable = read_uint8(p);
  2969. #endif /* end of WASM_ENABLE_GC == 0 */
  2970. *p_buf = p;
  2971. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2972. return false;
  2973. }
  2974. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2975. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2976. global);
  2977. if (ret) {
  2978. if (global->type.val_type != declare_type
  2979. || global->type.is_mutable != declare_mutable) {
  2980. set_error_buf(error_buf, error_buf_size,
  2981. "incompatible import type");
  2982. return false;
  2983. }
  2984. global->is_linked = true;
  2985. }
  2986. #endif
  2987. #if WASM_ENABLE_MULTI_MODULE != 0
  2988. if (!global->is_linked
  2989. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2990. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2991. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2992. error_buf_size);
  2993. if (sub_module) {
  2994. /* check sub modules */
  2995. linked_global = wasm_loader_resolve_global(
  2996. sub_module_name, global_name, declare_type, declare_mutable,
  2997. error_buf, error_buf_size);
  2998. if (linked_global) {
  2999. global->import_module = sub_module;
  3000. global->import_global_linked = linked_global;
  3001. global->is_linked = true;
  3002. }
  3003. }
  3004. }
  3005. #endif
  3006. global->module_name = sub_module_name;
  3007. global->field_name = global_name;
  3008. global->type.val_type = declare_type;
  3009. global->type.is_mutable = (declare_mutable == 1);
  3010. #if WASM_ENABLE_WAMR_COMPILER != 0
  3011. if (global->type.val_type == VALUE_TYPE_V128)
  3012. parent_module->is_simd_used = true;
  3013. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  3014. parent_module->is_ref_types_used = true;
  3015. #endif
  3016. (void)parent_module;
  3017. (void)ret;
  3018. return true;
  3019. fail:
  3020. return false;
  3021. }
  3022. static bool
  3023. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3024. WASMTable *table, char *error_buf, uint32 error_buf_size)
  3025. {
  3026. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3027. #if WASM_ENABLE_GC != 0
  3028. WASMRefType ref_type;
  3029. bool need_ref_type_map;
  3030. #endif
  3031. bool is_table64 = false;
  3032. #if WASM_ENABLE_GC == 0
  3033. CHECK_BUF(p, p_end, 1);
  3034. /* 0x70 or 0x6F */
  3035. table->table_type.elem_type = read_uint8(p);
  3036. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  3037. #if WASM_ENABLE_REF_TYPES != 0
  3038. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  3039. #endif
  3040. ) {
  3041. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  3042. return false;
  3043. }
  3044. #else /* else of WASM_ENABLE_GC == 0 */
  3045. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3046. &need_ref_type_map, &ref_type, false, error_buf,
  3047. error_buf_size)) {
  3048. return false;
  3049. }
  3050. /*
  3051. * TODO: add this validator
  3052. * `wasm_is_reftype_htref_non_nullable(ref_type.ref_type)`
  3053. * after sync up with the latest GC spec
  3054. */
  3055. if (!wasm_is_type_reftype(ref_type.ref_type)) {
  3056. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3057. return false;
  3058. }
  3059. table->table_type.elem_type = ref_type.ref_type;
  3060. if (need_ref_type_map) {
  3061. if (!(table->table_type.elem_ref_type =
  3062. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  3063. error_buf_size))) {
  3064. return false;
  3065. }
  3066. }
  3067. #if TRACE_WASM_LOADER != 0
  3068. os_printf("table type: ");
  3069. wasm_dump_value_type(table->table_type.elem_type,
  3070. table->table_type.elem_ref_type);
  3071. os_printf("\n");
  3072. #endif
  3073. #endif /* end of WASM_ENABLE_GC == 0 */
  3074. p_org = p;
  3075. read_leb_uint32(p, p_end, table->table_type.flags);
  3076. is_table64 = table->table_type.flags & TABLE64_FLAG;
  3077. if (p - p_org > 1) {
  3078. LOG_VERBOSE("integer representation too long(table)");
  3079. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3080. return false;
  3081. }
  3082. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  3083. error_buf_size, false)) {
  3084. return false;
  3085. }
  3086. read_leb_uint32(p, p_end, table->table_type.init_size);
  3087. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  3088. read_leb_uint32(p, p_end, table->table_type.max_size);
  3089. if (!check_table_max_size(table->table_type.init_size,
  3090. table->table_type.max_size, error_buf,
  3091. error_buf_size))
  3092. return false;
  3093. }
  3094. adjust_table_max_size(is_table64, table->table_type.init_size,
  3095. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  3096. &table->table_type.max_size);
  3097. #if WASM_ENABLE_WAMR_COMPILER != 0
  3098. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3099. module->is_ref_types_used = true;
  3100. #endif
  3101. *p_buf = p;
  3102. return true;
  3103. fail:
  3104. return false;
  3105. }
  3106. static bool
  3107. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  3108. char *error_buf, uint32 error_buf_size)
  3109. {
  3110. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3111. #if WASM_ENABLE_APP_FRAMEWORK != 0
  3112. uint32 pool_size = wasm_runtime_memory_pool_size();
  3113. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  3114. / DEFAULT_NUM_BYTES_PER_PAGE;
  3115. #else
  3116. uint32 max_page_count;
  3117. #endif
  3118. bool is_memory64 = false;
  3119. p_org = p;
  3120. read_leb_uint32(p, p_end, memory->flags);
  3121. is_memory64 = memory->flags & MEMORY64_FLAG;
  3122. if (p - p_org > 1) {
  3123. LOG_VERBOSE("integer representation too long(memory)");
  3124. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3125. return false;
  3126. }
  3127. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  3128. false)) {
  3129. return false;
  3130. }
  3131. read_leb_uint32(p, p_end, memory->init_page_count);
  3132. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  3133. error_buf_size))
  3134. return false;
  3135. #if WASM_ENABLE_APP_FRAMEWORK == 0
  3136. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  3137. #endif
  3138. if (memory->flags & 1) {
  3139. read_leb_uint32(p, p_end, memory->max_page_count);
  3140. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  3141. memory->max_page_count, error_buf,
  3142. error_buf_size))
  3143. return false;
  3144. if (memory->max_page_count > max_page_count)
  3145. memory->max_page_count = max_page_count;
  3146. }
  3147. else {
  3148. /* Limit the maximum memory size to max_page_count */
  3149. memory->max_page_count = max_page_count;
  3150. }
  3151. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  3152. *p_buf = p;
  3153. return true;
  3154. fail:
  3155. return false;
  3156. }
  3157. static int
  3158. cmp_export_name(const void *a, const void *b)
  3159. {
  3160. return strcmp(*(char **)a, *(char **)b);
  3161. }
  3162. static bool
  3163. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3164. bool is_load_from_file_buf, bool no_resolve,
  3165. char *error_buf, uint32 error_buf_size)
  3166. {
  3167. const uint8 *p = buf, *p_end = buf_end, *p_old;
  3168. uint32 import_count, name_len, type_index, i, u32, flags;
  3169. uint64 total_size;
  3170. WASMImport *import;
  3171. WASMImport *import_functions = NULL, *import_tables = NULL;
  3172. WASMImport *import_memories = NULL, *import_globals = NULL;
  3173. #if WASM_ENABLE_TAGS != 0
  3174. WASMImport *import_tags = NULL;
  3175. #endif
  3176. char *sub_module_name, *field_name;
  3177. uint8 u8, kind, global_type;
  3178. read_leb_uint32(p, p_end, import_count);
  3179. if (import_count) {
  3180. module->import_count = import_count;
  3181. total_size = sizeof(WASMImport) * (uint64)import_count;
  3182. if (!(module->imports =
  3183. loader_malloc(total_size, error_buf, error_buf_size))) {
  3184. return false;
  3185. }
  3186. p_old = p;
  3187. /* Scan firstly to get import count of each type */
  3188. for (i = 0; i < import_count; i++) {
  3189. /* module name */
  3190. read_leb_uint32(p, p_end, name_len);
  3191. CHECK_BUF(p, p_end, name_len);
  3192. p += name_len;
  3193. /* field name */
  3194. read_leb_uint32(p, p_end, name_len);
  3195. CHECK_BUF(p, p_end, name_len);
  3196. p += name_len;
  3197. CHECK_BUF(p, p_end, 1);
  3198. /* 0x00/0x01/0x02/0x03/0x04 */
  3199. kind = read_uint8(p);
  3200. switch (kind) {
  3201. case IMPORT_KIND_FUNC: /* import function */
  3202. read_leb_uint32(p, p_end, type_index);
  3203. module->import_function_count++;
  3204. break;
  3205. case IMPORT_KIND_TABLE: /* import table */
  3206. CHECK_BUF(p, p_end, 1);
  3207. /* 0x70 */
  3208. u8 = read_uint8(p);
  3209. #if WASM_ENABLE_GC != 0
  3210. if (wasm_is_reftype_htref_nullable(u8)) {
  3211. int32 heap_type;
  3212. read_leb_int32(p, p_end, heap_type);
  3213. (void)heap_type;
  3214. }
  3215. #endif
  3216. read_leb_uint32(p, p_end, flags);
  3217. read_leb_uint32(p, p_end, u32);
  3218. if (flags & 1)
  3219. read_leb_uint32(p, p_end, u32);
  3220. module->import_table_count++;
  3221. if (module->import_table_count > 1) {
  3222. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3223. set_error_buf(error_buf, error_buf_size,
  3224. "multiple tables");
  3225. return false;
  3226. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3227. module->is_ref_types_used = true;
  3228. #endif
  3229. }
  3230. break;
  3231. case IMPORT_KIND_MEMORY: /* import memory */
  3232. read_leb_uint32(p, p_end, flags);
  3233. read_leb_uint32(p, p_end, u32);
  3234. if (flags & 1)
  3235. read_leb_uint32(p, p_end, u32);
  3236. module->import_memory_count++;
  3237. #if WASM_ENABLE_MULTI_MEMORY == 0
  3238. if (module->import_memory_count > 1) {
  3239. set_error_buf(error_buf, error_buf_size,
  3240. "multiple memories");
  3241. return false;
  3242. }
  3243. #endif
  3244. break;
  3245. #if WASM_ENABLE_TAGS != 0
  3246. case IMPORT_KIND_TAG: /* import tags */
  3247. /* it only counts the number of tags to import */
  3248. module->import_tag_count++;
  3249. CHECK_BUF(p, p_end, 1);
  3250. u8 = read_uint8(p);
  3251. read_leb_uint32(p, p_end, type_index);
  3252. break;
  3253. #endif
  3254. case IMPORT_KIND_GLOBAL: /* import global */
  3255. #if WASM_ENABLE_GC != 0
  3256. /* valtype */
  3257. CHECK_BUF(p, p_end, 1);
  3258. global_type = read_uint8(p);
  3259. if (wasm_is_reftype_htref_nullable(global_type)
  3260. || wasm_is_reftype_htref_non_nullable(global_type)) {
  3261. int32 heap_type;
  3262. read_leb_int32(p, p_end, heap_type);
  3263. (void)heap_type;
  3264. }
  3265. /* mutability */
  3266. CHECK_BUF(p, p_end, 1);
  3267. p += 1;
  3268. #else
  3269. CHECK_BUF(p, p_end, 2);
  3270. p += 2;
  3271. #endif
  3272. (void)global_type;
  3273. module->import_global_count++;
  3274. break;
  3275. default:
  3276. set_error_buf(error_buf, error_buf_size,
  3277. "invalid import kind");
  3278. return false;
  3279. }
  3280. }
  3281. if (module->import_function_count)
  3282. import_functions = module->import_functions = module->imports;
  3283. if (module->import_table_count)
  3284. import_tables = module->import_tables =
  3285. module->imports + module->import_function_count;
  3286. if (module->import_memory_count)
  3287. import_memories = module->import_memories =
  3288. module->imports + module->import_function_count
  3289. + module->import_table_count;
  3290. #if WASM_ENABLE_TAGS != 0
  3291. if (module->import_tag_count)
  3292. import_tags = module->import_tags =
  3293. module->imports + module->import_function_count
  3294. + module->import_table_count + module->import_memory_count;
  3295. if (module->import_global_count)
  3296. import_globals = module->import_globals =
  3297. module->imports + module->import_function_count
  3298. + module->import_table_count + module->import_memory_count
  3299. + module->import_tag_count;
  3300. #else
  3301. if (module->import_global_count)
  3302. import_globals = module->import_globals =
  3303. module->imports + module->import_function_count
  3304. + module->import_table_count + module->import_memory_count;
  3305. #endif
  3306. p = p_old;
  3307. /* Scan again to resolve the data */
  3308. for (i = 0; i < import_count; i++) {
  3309. /* load module name */
  3310. read_leb_uint32(p, p_end, name_len);
  3311. CHECK_BUF(p, p_end, name_len);
  3312. if (!(sub_module_name = wasm_const_str_list_insert(
  3313. p, name_len, module, is_load_from_file_buf, error_buf,
  3314. error_buf_size))) {
  3315. return false;
  3316. }
  3317. p += name_len;
  3318. /* load field name */
  3319. read_leb_uint32(p, p_end, name_len);
  3320. CHECK_BUF(p, p_end, name_len);
  3321. if (!(field_name = wasm_const_str_list_insert(
  3322. p, name_len, module, is_load_from_file_buf, error_buf,
  3323. error_buf_size))) {
  3324. return false;
  3325. }
  3326. p += name_len;
  3327. CHECK_BUF(p, p_end, 1);
  3328. /* 0x00/0x01/0x02/0x03/0x4 */
  3329. kind = read_uint8(p);
  3330. switch (kind) {
  3331. case IMPORT_KIND_FUNC: /* import function */
  3332. bh_assert(import_functions);
  3333. import = import_functions++;
  3334. if (!load_function_import(&p, p_end, module,
  3335. sub_module_name, field_name,
  3336. &import->u.function, no_resolve,
  3337. error_buf, error_buf_size)) {
  3338. return false;
  3339. }
  3340. break;
  3341. case IMPORT_KIND_TABLE: /* import table */
  3342. bh_assert(import_tables);
  3343. import = import_tables++;
  3344. if (!load_table_import(&p, p_end, module, sub_module_name,
  3345. field_name, &import->u.table,
  3346. error_buf, error_buf_size)) {
  3347. LOG_DEBUG("can not import such a table (%s,%s)",
  3348. sub_module_name, field_name);
  3349. return false;
  3350. }
  3351. break;
  3352. case IMPORT_KIND_MEMORY: /* import memory */
  3353. bh_assert(import_memories);
  3354. import = import_memories++;
  3355. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3356. field_name, &import->u.memory,
  3357. error_buf, error_buf_size)) {
  3358. return false;
  3359. }
  3360. break;
  3361. #if WASM_ENABLE_TAGS != 0
  3362. case IMPORT_KIND_TAG:
  3363. bh_assert(import_tags);
  3364. import = import_tags++;
  3365. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3366. field_name, &import->u.tag, error_buf,
  3367. error_buf_size)) {
  3368. return false;
  3369. }
  3370. break;
  3371. #endif
  3372. case IMPORT_KIND_GLOBAL: /* import global */
  3373. bh_assert(import_globals);
  3374. import = import_globals++;
  3375. if (!load_global_import(&p, p_end, module, sub_module_name,
  3376. field_name, &import->u.global,
  3377. error_buf, error_buf_size)) {
  3378. return false;
  3379. }
  3380. break;
  3381. default:
  3382. set_error_buf(error_buf, error_buf_size,
  3383. "invalid import kind");
  3384. return false;
  3385. }
  3386. import->kind = kind;
  3387. import->u.names.module_name = sub_module_name;
  3388. import->u.names.field_name = field_name;
  3389. }
  3390. #if WASM_ENABLE_LIBC_WASI != 0
  3391. import = module->import_functions;
  3392. for (i = 0; i < module->import_function_count; i++, import++) {
  3393. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3394. || !strcmp(import->u.names.module_name,
  3395. "wasi_snapshot_preview1")) {
  3396. module->import_wasi_api = true;
  3397. break;
  3398. }
  3399. }
  3400. #endif
  3401. }
  3402. if (p != p_end) {
  3403. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3404. return false;
  3405. }
  3406. LOG_VERBOSE("Load import section success.\n");
  3407. (void)u8;
  3408. (void)u32;
  3409. (void)type_index;
  3410. return true;
  3411. fail:
  3412. return false;
  3413. }
  3414. static bool
  3415. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3416. uint32 error_buf_size)
  3417. {
  3418. WASMFuncType *param_type = func->func_type;
  3419. uint32 param_count = param_type->param_count;
  3420. uint8 *param_types = param_type->types;
  3421. uint32 local_count = func->local_count;
  3422. uint8 *local_types = func->local_types;
  3423. uint32 i, local_offset = 0;
  3424. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3425. /*
  3426. * Only allocate memory when total_size is not 0,
  3427. * or the return value of malloc(0) might be NULL on some platforms,
  3428. * which causes wasm loader return false.
  3429. */
  3430. if (total_size > 0
  3431. && !(func->local_offsets =
  3432. loader_malloc(total_size, error_buf, error_buf_size))) {
  3433. return false;
  3434. }
  3435. for (i = 0; i < param_count; i++) {
  3436. func->local_offsets[i] = (uint16)local_offset;
  3437. local_offset += wasm_value_type_cell_num(param_types[i]);
  3438. }
  3439. for (i = 0; i < local_count; i++) {
  3440. func->local_offsets[param_count + i] = (uint16)local_offset;
  3441. local_offset += wasm_value_type_cell_num(local_types[i]);
  3442. }
  3443. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3444. return true;
  3445. }
  3446. static bool
  3447. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3448. const uint8 *buf_code, const uint8 *buf_code_end,
  3449. WASMModule *module, char *error_buf,
  3450. uint32 error_buf_size)
  3451. {
  3452. const uint8 *p = buf, *p_end = buf_end;
  3453. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3454. uint32 func_count;
  3455. uint64 total_size;
  3456. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3457. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3458. uint8 type;
  3459. WASMFunction *func;
  3460. #if WASM_ENABLE_GC != 0
  3461. bool need_ref_type_map;
  3462. WASMRefType ref_type;
  3463. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3464. #endif
  3465. read_leb_uint32(p, p_end, func_count);
  3466. if (buf_code)
  3467. read_leb_uint32(p_code, buf_code_end, code_count);
  3468. if (func_count != code_count) {
  3469. set_error_buf(error_buf, error_buf_size,
  3470. "function and code section have inconsistent lengths or "
  3471. "unexpected end");
  3472. return false;
  3473. }
  3474. if (is_indices_overflow(module->import_function_count, func_count,
  3475. error_buf, error_buf_size))
  3476. return false;
  3477. if (func_count) {
  3478. module->function_count = func_count;
  3479. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3480. if (!(module->functions =
  3481. loader_malloc(total_size, error_buf, error_buf_size))) {
  3482. return false;
  3483. }
  3484. for (i = 0; i < func_count; i++) {
  3485. /* Resolve function type */
  3486. read_leb_uint32(p, p_end, type_index);
  3487. if (!check_function_type(module, type_index, error_buf,
  3488. error_buf_size)) {
  3489. return false;
  3490. }
  3491. #if WASM_ENABLE_GC != 0
  3492. type_index_org = type_index;
  3493. #endif
  3494. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3495. && WASM_ENABLE_GC == 0
  3496. type_index = wasm_get_smallest_type_idx(
  3497. module->types, module->type_count, type_index);
  3498. #endif
  3499. read_leb_uint32(p_code, buf_code_end, code_size);
  3500. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3501. set_error_buf(error_buf, error_buf_size,
  3502. "invalid function code size");
  3503. return false;
  3504. }
  3505. /* Resolve local set count */
  3506. p_code_end = p_code + code_size;
  3507. #if WASM_ENABLE_BRANCH_HINTS != 0
  3508. uint8 *p_body_start = (uint8 *)p_code;
  3509. #endif
  3510. local_count = 0;
  3511. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3512. p_code_save = p_code;
  3513. #if WASM_ENABLE_GC != 0
  3514. ref_type_map_count = 0;
  3515. #endif
  3516. /* Calculate total local count */
  3517. for (j = 0; j < local_set_count; j++) {
  3518. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3519. if (sub_local_count > UINT32_MAX - local_count) {
  3520. set_error_buf(error_buf, error_buf_size, "too many locals");
  3521. return false;
  3522. }
  3523. #if WASM_ENABLE_GC == 0
  3524. CHECK_BUF(p_code, buf_code_end, 1);
  3525. /* 0x7F/0x7E/0x7D/0x7C */
  3526. type = read_uint8(p_code);
  3527. local_count += sub_local_count;
  3528. #if WASM_ENABLE_WAMR_COMPILER != 0
  3529. /* If any value's type is v128, mark the module as SIMD used */
  3530. if (type == VALUE_TYPE_V128)
  3531. module->is_simd_used = true;
  3532. #endif
  3533. #else
  3534. if (!resolve_value_type(&p_code, buf_code_end, module,
  3535. module->type_count, &need_ref_type_map,
  3536. &ref_type, false, error_buf,
  3537. error_buf_size)) {
  3538. return false;
  3539. }
  3540. local_count += sub_local_count;
  3541. if (need_ref_type_map)
  3542. ref_type_map_count += sub_local_count;
  3543. #endif
  3544. }
  3545. /* Code size in code entry can't be smaller than size of vec(locals)
  3546. * + expr(at least 1 for opcode end). And expressions are encoded by
  3547. * their instruction sequence terminated with an explicit 0x0B
  3548. * opcode for end. */
  3549. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3550. set_error_buf(
  3551. error_buf, error_buf_size,
  3552. "section size mismatch: function body END opcode expected");
  3553. return false;
  3554. }
  3555. /* Alloc memory, layout: function structure + local types */
  3556. code_size = (uint32)(p_code_end - p_code);
  3557. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3558. if (!(func = module->functions[i] =
  3559. loader_malloc(total_size, error_buf, error_buf_size))) {
  3560. return false;
  3561. }
  3562. #if WASM_ENABLE_GC != 0
  3563. if (ref_type_map_count > 0) {
  3564. if (ref_type_map_count > UINT16_MAX) {
  3565. set_error_buf(error_buf, error_buf_size,
  3566. "ref type count too large");
  3567. return false;
  3568. }
  3569. total_size =
  3570. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3571. if (!(func->local_ref_type_maps = loader_malloc(
  3572. total_size, error_buf, error_buf_size))) {
  3573. return false;
  3574. }
  3575. func->local_ref_type_map_count = ref_type_map_count;
  3576. }
  3577. #endif
  3578. /* Set function type, local count, code size and code body */
  3579. func->func_type = (WASMFuncType *)module->types[type_index];
  3580. func->local_count = local_count;
  3581. if (local_count > 0)
  3582. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3583. func->code_size = code_size;
  3584. #if WASM_ENABLE_BRANCH_HINTS != 0
  3585. func->code_body_begin = p_body_start;
  3586. #endif
  3587. /*
  3588. * we shall make a copy of code body [p_code, p_code + code_size]
  3589. * when we are worrying about inappropriate releasing behaviour.
  3590. * all code bodies are actually in a buffer which user allocates in
  3591. * their embedding environment and we don't have power over them.
  3592. * it will be like:
  3593. * code_body_cp = malloc(code_size);
  3594. * memcpy(code_body_cp, p_code, code_size);
  3595. * func->code = code_body_cp;
  3596. */
  3597. func->code = (uint8 *)p_code;
  3598. #if WASM_ENABLE_GC != 0
  3599. func->type_idx = type_index_org;
  3600. #endif
  3601. #if WASM_ENABLE_GC != 0
  3602. t = 0;
  3603. #endif
  3604. /* Load each local type */
  3605. p_code = p_code_save;
  3606. local_type_index = 0;
  3607. for (j = 0; j < local_set_count; j++) {
  3608. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3609. /* Note: sub_local_count is allowed to be 0 */
  3610. if (local_type_index > UINT32_MAX - sub_local_count
  3611. || local_type_index + sub_local_count > local_count) {
  3612. set_error_buf(error_buf, error_buf_size,
  3613. "invalid local count");
  3614. return false;
  3615. }
  3616. #if WASM_ENABLE_GC == 0
  3617. CHECK_BUF(p_code, buf_code_end, 1);
  3618. /* 0x7F/0x7E/0x7D/0x7C */
  3619. type = read_uint8(p_code);
  3620. if (!is_valid_value_type_for_interpreter(type)) {
  3621. if (type == VALUE_TYPE_V128)
  3622. set_error_buf(error_buf, error_buf_size,
  3623. "v128 value type requires simd feature");
  3624. else if (type == VALUE_TYPE_FUNCREF
  3625. || type == VALUE_TYPE_EXTERNREF)
  3626. set_error_buf(error_buf, error_buf_size,
  3627. "ref value type requires "
  3628. "reference types feature");
  3629. else
  3630. set_error_buf_v(error_buf, error_buf_size,
  3631. "invalid local type 0x%02X", type);
  3632. return false;
  3633. }
  3634. #else
  3635. if (!resolve_value_type(&p_code, buf_code_end, module,
  3636. module->type_count, &need_ref_type_map,
  3637. &ref_type, false, error_buf,
  3638. error_buf_size)) {
  3639. return false;
  3640. }
  3641. if (need_ref_type_map) {
  3642. WASMRefType *ref_type_tmp;
  3643. if (!(ref_type_tmp = reftype_set_insert(
  3644. module->ref_type_set, &ref_type, error_buf,
  3645. error_buf_size))) {
  3646. return false;
  3647. }
  3648. for (k = 0; k < sub_local_count; k++) {
  3649. func->local_ref_type_maps[t + k].ref_type =
  3650. ref_type_tmp;
  3651. func->local_ref_type_maps[t + k].index =
  3652. local_type_index + k;
  3653. }
  3654. t += sub_local_count;
  3655. }
  3656. type = ref_type.ref_type;
  3657. #endif
  3658. for (k = 0; k < sub_local_count; k++) {
  3659. func->local_types[local_type_index++] = type;
  3660. }
  3661. #if WASM_ENABLE_WAMR_COMPILER != 0
  3662. if (type == VALUE_TYPE_V128)
  3663. module->is_simd_used = true;
  3664. else if (type == VALUE_TYPE_FUNCREF
  3665. || type == VALUE_TYPE_EXTERNREF)
  3666. module->is_ref_types_used = true;
  3667. #endif
  3668. }
  3669. bh_assert(local_type_index == func->local_count);
  3670. #if WASM_ENABLE_GC != 0
  3671. bh_assert(t == func->local_ref_type_map_count);
  3672. #if TRACE_WASM_LOADER != 0
  3673. os_printf("func %u, local types: [", i);
  3674. k = 0;
  3675. for (j = 0; j < func->local_count; j++) {
  3676. WASMRefType *ref_type_tmp = NULL;
  3677. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3678. bh_assert(j == func->local_ref_type_maps[k].index);
  3679. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3680. }
  3681. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3682. if (j < func->local_count - 1)
  3683. os_printf(" ");
  3684. }
  3685. os_printf("]\n");
  3686. #endif
  3687. #endif
  3688. func->param_cell_num = func->func_type->param_cell_num;
  3689. func->ret_cell_num = func->func_type->ret_cell_num;
  3690. local_cell_num =
  3691. wasm_get_cell_num(func->local_types, func->local_count);
  3692. if (local_cell_num > UINT16_MAX) {
  3693. set_error_buf(error_buf, error_buf_size,
  3694. "local count too large");
  3695. return false;
  3696. }
  3697. func->local_cell_num = (uint16)local_cell_num;
  3698. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3699. return false;
  3700. p_code = p_code_end;
  3701. }
  3702. }
  3703. if (p != p_end) {
  3704. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3705. return false;
  3706. }
  3707. LOG_VERBOSE("Load function section success.\n");
  3708. return true;
  3709. fail:
  3710. return false;
  3711. }
  3712. static bool
  3713. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3714. char *error_buf, uint32 error_buf_size)
  3715. {
  3716. const uint8 *p = buf, *p_end = buf_end;
  3717. uint32 table_count, i;
  3718. uint64 total_size;
  3719. WASMTable *table;
  3720. read_leb_uint32(p, p_end, table_count);
  3721. if (module->import_table_count + table_count > 1) {
  3722. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3723. /* a total of one table is allowed */
  3724. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3725. return false;
  3726. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3727. module->is_ref_types_used = true;
  3728. #endif
  3729. }
  3730. if (table_count) {
  3731. module->table_count = table_count;
  3732. total_size = sizeof(WASMTable) * (uint64)table_count;
  3733. if (!(module->tables =
  3734. loader_malloc(total_size, error_buf, error_buf_size))) {
  3735. return false;
  3736. }
  3737. /* load each table */
  3738. table = module->tables;
  3739. for (i = 0; i < table_count; i++, table++) {
  3740. #if WASM_ENABLE_GC != 0
  3741. uint8 flag;
  3742. bool has_init = false;
  3743. CHECK_BUF(p, p_end, 1);
  3744. flag = read_uint8(p);
  3745. if (flag == TABLE_INIT_EXPR_FLAG) {
  3746. CHECK_BUF(p, p_end, 1);
  3747. flag = read_uint8(p);
  3748. if (flag != 0x00) {
  3749. set_error_buf(error_buf, error_buf_size,
  3750. "invalid leading bytes for table");
  3751. return false;
  3752. }
  3753. has_init = true;
  3754. }
  3755. else {
  3756. p--;
  3757. }
  3758. #endif /* end of WASM_ENABLE_GC != 0 */
  3759. if (!load_table(&p, p_end, module, table, error_buf,
  3760. error_buf_size))
  3761. return false;
  3762. #if WASM_ENABLE_GC != 0
  3763. if (has_init) {
  3764. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3765. table->table_type.elem_type,
  3766. table->table_type.elem_ref_type, error_buf,
  3767. error_buf_size))
  3768. return false;
  3769. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3770. && table->init_expr.init_expr_type
  3771. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3772. set_error_buf(
  3773. error_buf, error_buf_size,
  3774. "unsupported initializer expression for table");
  3775. return false;
  3776. }
  3777. }
  3778. else {
  3779. if (wasm_is_reftype_htref_non_nullable(
  3780. table->table_type.elem_type)) {
  3781. set_error_buf(
  3782. error_buf, error_buf_size,
  3783. "type mismatch: non-nullable table without init expr");
  3784. return false;
  3785. }
  3786. }
  3787. #endif /* end of WASM_ENABLE_GC != 0 */
  3788. #if WASM_ENABLE_WAMR_COMPILER != 0
  3789. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3790. module->is_ref_types_used = true;
  3791. #endif
  3792. }
  3793. }
  3794. if (p != p_end) {
  3795. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3796. return false;
  3797. }
  3798. LOG_VERBOSE("Load table section success.\n");
  3799. return true;
  3800. fail:
  3801. return false;
  3802. }
  3803. static bool
  3804. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3805. char *error_buf, uint32 error_buf_size)
  3806. {
  3807. const uint8 *p = buf, *p_end = buf_end;
  3808. uint32 memory_count, i;
  3809. uint64 total_size;
  3810. WASMMemory *memory;
  3811. read_leb_uint32(p, p_end, memory_count);
  3812. #if WASM_ENABLE_MULTI_MEMORY == 0
  3813. /* a total of one memory is allowed */
  3814. if (module->import_memory_count + memory_count > 1) {
  3815. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3816. return false;
  3817. }
  3818. #endif
  3819. if (memory_count) {
  3820. module->memory_count = memory_count;
  3821. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3822. if (!(module->memories =
  3823. loader_malloc(total_size, error_buf, error_buf_size))) {
  3824. return false;
  3825. }
  3826. /* load each memory */
  3827. memory = module->memories;
  3828. for (i = 0; i < memory_count; i++, memory++)
  3829. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3830. return false;
  3831. }
  3832. if (p != p_end) {
  3833. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3834. return false;
  3835. }
  3836. LOG_VERBOSE("Load memory section success.\n");
  3837. return true;
  3838. fail:
  3839. return false;
  3840. }
  3841. static bool
  3842. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3843. char *error_buf, uint32 error_buf_size)
  3844. {
  3845. const uint8 *p = buf, *p_end = buf_end;
  3846. uint32 global_count, i;
  3847. uint64 total_size;
  3848. WASMGlobal *global;
  3849. uint8 mutable;
  3850. #if WASM_ENABLE_GC != 0
  3851. bool need_ref_type_map;
  3852. WASMRefType ref_type;
  3853. #endif
  3854. read_leb_uint32(p, p_end, global_count);
  3855. if (is_indices_overflow(module->import_global_count, global_count,
  3856. error_buf, error_buf_size))
  3857. return false;
  3858. module->global_count = 0;
  3859. if (global_count) {
  3860. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3861. if (!(module->globals =
  3862. loader_malloc(total_size, error_buf, error_buf_size))) {
  3863. return false;
  3864. }
  3865. global = module->globals;
  3866. for (i = 0; i < global_count; i++, global++) {
  3867. #if WASM_ENABLE_GC == 0
  3868. CHECK_BUF(p, p_end, 2);
  3869. /* global type */
  3870. global->type.val_type = read_uint8(p);
  3871. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3872. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3873. return false;
  3874. }
  3875. mutable = read_uint8(p);
  3876. #else
  3877. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3878. &need_ref_type_map, &ref_type, false,
  3879. error_buf, error_buf_size)) {
  3880. return false;
  3881. }
  3882. global->type.val_type = ref_type.ref_type;
  3883. CHECK_BUF(p, p_end, 1);
  3884. mutable = read_uint8(p);
  3885. #endif /* end of WASM_ENABLE_GC */
  3886. #if WASM_ENABLE_WAMR_COMPILER != 0
  3887. if (global->type.val_type == VALUE_TYPE_V128)
  3888. module->is_simd_used = true;
  3889. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3890. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3891. module->is_ref_types_used = true;
  3892. #endif
  3893. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3894. return false;
  3895. }
  3896. global->type.is_mutable = mutable ? true : false;
  3897. /* initialize expression */
  3898. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3899. global->type.val_type,
  3900. #if WASM_ENABLE_GC == 0
  3901. NULL,
  3902. #else
  3903. &ref_type,
  3904. #endif
  3905. error_buf, error_buf_size))
  3906. return false;
  3907. #if WASM_ENABLE_GC != 0
  3908. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3909. uint8 global_type;
  3910. WASMRefType *global_ref_type;
  3911. uint32 global_idx = global->init_expr.u.unary.v.global_index;
  3912. if (global->init_expr.u.unary.v.global_index
  3913. >= module->import_global_count + i) {
  3914. set_error_buf(error_buf, error_buf_size, "unknown global");
  3915. return false;
  3916. }
  3917. if (global_idx < module->import_global_count) {
  3918. global_type = module->import_globals[global_idx]
  3919. .u.global.type.val_type;
  3920. global_ref_type =
  3921. module->import_globals[global_idx].u.global.ref_type;
  3922. }
  3923. else {
  3924. global_type =
  3925. module
  3926. ->globals[global_idx - module->import_global_count]
  3927. .type.val_type;
  3928. global_ref_type =
  3929. module
  3930. ->globals[global_idx - module->import_global_count]
  3931. .ref_type;
  3932. }
  3933. if (!wasm_reftype_is_subtype_of(
  3934. global_type, global_ref_type, global->type.val_type,
  3935. global->ref_type, module->types, module->type_count)) {
  3936. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3937. return false;
  3938. }
  3939. }
  3940. if (need_ref_type_map) {
  3941. if (!(global->ref_type =
  3942. reftype_set_insert(module->ref_type_set, &ref_type,
  3943. error_buf, error_buf_size))) {
  3944. return false;
  3945. }
  3946. }
  3947. #if TRACE_WASM_LOADER != 0
  3948. os_printf("global type: ");
  3949. wasm_dump_value_type(global->type, global->ref_type);
  3950. os_printf("\n");
  3951. #endif
  3952. #endif
  3953. module->global_count++;
  3954. }
  3955. bh_assert(module->global_count == global_count);
  3956. }
  3957. if (p != p_end) {
  3958. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3959. return false;
  3960. }
  3961. LOG_VERBOSE("Load global section success.\n");
  3962. return true;
  3963. fail:
  3964. return false;
  3965. }
  3966. static bool
  3967. check_duplicate_exports(WASMModule *module, char *error_buf,
  3968. uint32 error_buf_size)
  3969. {
  3970. uint32 i;
  3971. bool result = false;
  3972. char *names_buf[32], **names = names_buf;
  3973. if (module->export_count > 32) {
  3974. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3975. error_buf_size);
  3976. if (!names) {
  3977. return result;
  3978. }
  3979. }
  3980. for (i = 0; i < module->export_count; i++) {
  3981. names[i] = module->exports[i].name;
  3982. }
  3983. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3984. for (i = 1; i < module->export_count; i++) {
  3985. if (!strcmp(names[i], names[i - 1])) {
  3986. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3987. goto cleanup;
  3988. }
  3989. }
  3990. result = true;
  3991. cleanup:
  3992. if (module->export_count > 32) {
  3993. wasm_runtime_free(names);
  3994. }
  3995. return result;
  3996. }
  3997. static bool
  3998. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3999. bool is_load_from_file_buf, char *error_buf,
  4000. uint32 error_buf_size)
  4001. {
  4002. const uint8 *p = buf, *p_end = buf_end;
  4003. uint32 export_count, i, index;
  4004. uint64 total_size;
  4005. uint32 str_len;
  4006. WASMExport *export;
  4007. read_leb_uint32(p, p_end, export_count);
  4008. if (export_count) {
  4009. module->export_count = export_count;
  4010. total_size = sizeof(WASMExport) * (uint64)export_count;
  4011. if (!(module->exports =
  4012. loader_malloc(total_size, error_buf, error_buf_size))) {
  4013. return false;
  4014. }
  4015. export = module->exports;
  4016. for (i = 0; i < export_count; i++, export ++) {
  4017. #if WASM_ENABLE_THREAD_MGR == 0
  4018. if (p == p_end) {
  4019. /* export section with inconsistent count:
  4020. n export declared, but less than n given */
  4021. set_error_buf(error_buf, error_buf_size,
  4022. "length out of bounds");
  4023. return false;
  4024. }
  4025. #endif
  4026. read_leb_uint32(p, p_end, str_len);
  4027. CHECK_BUF(p, p_end, str_len);
  4028. if (!(export->name = wasm_const_str_list_insert(
  4029. p, str_len, module, is_load_from_file_buf, error_buf,
  4030. error_buf_size))) {
  4031. return false;
  4032. }
  4033. p += str_len;
  4034. CHECK_BUF(p, p_end, 1);
  4035. export->kind = read_uint8(p);
  4036. read_leb_uint32(p, p_end, index);
  4037. export->index = index;
  4038. switch (export->kind) {
  4039. /* function index */
  4040. case EXPORT_KIND_FUNC:
  4041. if (index >= module->function_count
  4042. + module->import_function_count) {
  4043. set_error_buf(error_buf, error_buf_size,
  4044. "unknown function");
  4045. return false;
  4046. }
  4047. #if WASM_ENABLE_SIMD != 0
  4048. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  4049. || (WASM_ENABLE_FAST_INTERP != 0)
  4050. /* TODO: check func type, if it has v128 param or result,
  4051. report error */
  4052. #endif
  4053. #endif
  4054. break;
  4055. /* table index */
  4056. case EXPORT_KIND_TABLE:
  4057. if (index
  4058. >= module->table_count + module->import_table_count) {
  4059. set_error_buf(error_buf, error_buf_size,
  4060. "unknown table");
  4061. return false;
  4062. }
  4063. break;
  4064. /* memory index */
  4065. case EXPORT_KIND_MEMORY:
  4066. if (index
  4067. >= module->memory_count + module->import_memory_count) {
  4068. set_error_buf(error_buf, error_buf_size,
  4069. "unknown memory");
  4070. return false;
  4071. }
  4072. break;
  4073. #if WASM_ENABLE_TAGS != 0
  4074. /* export tag */
  4075. case EXPORT_KIND_TAG:
  4076. if (index >= module->tag_count + module->import_tag_count) {
  4077. set_error_buf(error_buf, error_buf_size, "unknown tag");
  4078. return false;
  4079. }
  4080. break;
  4081. #endif
  4082. /* global index */
  4083. case EXPORT_KIND_GLOBAL:
  4084. if (index
  4085. >= module->global_count + module->import_global_count) {
  4086. set_error_buf(error_buf, error_buf_size,
  4087. "unknown global");
  4088. return false;
  4089. }
  4090. break;
  4091. default:
  4092. set_error_buf(error_buf, error_buf_size,
  4093. "invalid export kind");
  4094. return false;
  4095. }
  4096. }
  4097. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  4098. return false;
  4099. }
  4100. }
  4101. if (p != p_end) {
  4102. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4103. return false;
  4104. }
  4105. LOG_VERBOSE("Load export section success.\n");
  4106. return true;
  4107. fail:
  4108. return false;
  4109. }
  4110. static bool
  4111. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  4112. uint32 error_buf_size)
  4113. {
  4114. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  4115. if (table_index != 0) {
  4116. set_error_buf(
  4117. error_buf, error_buf_size,
  4118. "zero byte expected. The module uses reference types feature "
  4119. "which is disabled in the runtime.");
  4120. return false;
  4121. }
  4122. #endif
  4123. if (table_index >= module->import_table_count + module->table_count) {
  4124. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  4125. table_index);
  4126. return false;
  4127. }
  4128. return true;
  4129. }
  4130. static bool
  4131. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  4132. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  4133. {
  4134. const uint8 *p = *p_buf, *p_end = buf_end;
  4135. uint32 table_index;
  4136. read_leb_uint32(p, p_end, table_index);
  4137. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  4138. return false;
  4139. }
  4140. *p_table_index = table_index;
  4141. *p_buf = p;
  4142. return true;
  4143. fail:
  4144. return false;
  4145. }
  4146. /* Element segments must match element type of table */
  4147. static bool
  4148. check_table_elem_type(WASMModule *module, uint32 table_index,
  4149. uint32 type_from_elem_seg, char *error_buf,
  4150. uint32 error_buf_size)
  4151. {
  4152. uint32 table_declared_elem_type;
  4153. if (table_index < module->import_table_count)
  4154. table_declared_elem_type =
  4155. module->import_tables[table_index].u.table.table_type.elem_type;
  4156. else
  4157. table_declared_elem_type =
  4158. module->tables[table_index - module->import_table_count]
  4159. .table_type.elem_type;
  4160. if (table_declared_elem_type == type_from_elem_seg)
  4161. return true;
  4162. #if WASM_ENABLE_GC != 0
  4163. /*
  4164. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  4165. */
  4166. if (table_declared_elem_type == REF_TYPE_ANYREF)
  4167. return true;
  4168. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  4169. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4170. return true;
  4171. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  4172. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4173. return true;
  4174. #endif
  4175. set_error_buf(error_buf, error_buf_size, "type mismatch");
  4176. return false;
  4177. }
  4178. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4179. static bool
  4180. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  4181. uint32 *p_elem_type,
  4182. #if WASM_ENABLE_GC != 0
  4183. WASMRefType **p_elem_ref_type,
  4184. #endif
  4185. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  4186. {
  4187. const uint8 *p = *p_buf, *p_end = buf_end;
  4188. uint8 elem_type;
  4189. #if WASM_ENABLE_GC != 0
  4190. WASMRefType elem_ref_type;
  4191. bool need_ref_type_map;
  4192. #endif
  4193. CHECK_BUF(p, p_end, 1);
  4194. elem_type = read_uint8(p);
  4195. if (elemkind_zero) {
  4196. if (elem_type != 0) {
  4197. set_error_buf(error_buf, error_buf_size,
  4198. "invalid reference type or unknown type");
  4199. return false;
  4200. }
  4201. else {
  4202. *p_elem_type = VALUE_TYPE_FUNCREF;
  4203. *p_buf = p;
  4204. return true;
  4205. }
  4206. }
  4207. #if WASM_ENABLE_GC == 0
  4208. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  4209. set_error_buf(error_buf, error_buf_size,
  4210. "invalid reference type or unknown type");
  4211. return false;
  4212. }
  4213. *p_elem_type = elem_type;
  4214. #else
  4215. p--;
  4216. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  4217. module->type_count, &need_ref_type_map,
  4218. &elem_ref_type, false, error_buf, error_buf_size)) {
  4219. return false;
  4220. }
  4221. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  4222. set_error_buf(error_buf, error_buf_size,
  4223. "invalid reference type or unknown type");
  4224. return false;
  4225. }
  4226. *p_elem_type = elem_ref_type.ref_type;
  4227. if (need_ref_type_map) {
  4228. if (!(*p_elem_ref_type =
  4229. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  4230. error_buf, error_buf_size))) {
  4231. return false;
  4232. }
  4233. }
  4234. #endif
  4235. *p_buf = p;
  4236. return true;
  4237. fail:
  4238. return false;
  4239. }
  4240. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4241. static bool
  4242. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  4243. WASMModule *module, WASMTableSeg *table_segment,
  4244. char *error_buf, uint32 error_buf_size)
  4245. {
  4246. const uint8 *p = *p_buf, *p_end = buf_end;
  4247. uint32 function_count, function_index = 0, i;
  4248. uint64 total_size;
  4249. read_leb_uint32(p, p_end, function_count);
  4250. table_segment->value_count = function_count;
  4251. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  4252. if (total_size > 0
  4253. && !(table_segment->init_values =
  4254. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4255. error_buf_size))) {
  4256. return false;
  4257. }
  4258. for (i = 0; i < function_count; i++) {
  4259. InitializerExpression *init_expr = &table_segment->init_values[i];
  4260. read_leb_uint32(p, p_end, function_index);
  4261. if (!check_function_index(module, function_index, error_buf,
  4262. error_buf_size)) {
  4263. return false;
  4264. }
  4265. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  4266. init_expr->u.unary.v.ref_index = function_index;
  4267. }
  4268. *p_buf = p;
  4269. return true;
  4270. fail:
  4271. return false;
  4272. }
  4273. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  4274. static bool
  4275. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  4276. WASMModule *module, WASMTableSeg *table_segment,
  4277. char *error_buf, uint32 error_buf_size)
  4278. {
  4279. const uint8 *p = *p_buf, *p_end = buf_end;
  4280. uint32 ref_count, i;
  4281. uint64 total_size;
  4282. read_leb_uint32(p, p_end, ref_count);
  4283. table_segment->value_count = ref_count;
  4284. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  4285. if (total_size > 0
  4286. && !(table_segment->init_values =
  4287. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4288. error_buf_size))) {
  4289. return false;
  4290. }
  4291. for (i = 0; i < ref_count; i++) {
  4292. InitializerExpression *init_expr = &table_segment->init_values[i];
  4293. if (!load_init_expr(module, &p, p_end, init_expr,
  4294. table_segment->elem_type,
  4295. #if WASM_ENABLE_GC == 0
  4296. NULL,
  4297. #else
  4298. table_segment->elem_ref_type,
  4299. #endif
  4300. error_buf, error_buf_size))
  4301. return false;
  4302. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4303. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4304. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4305. && init_expr->init_expr_type
  4306. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4307. }
  4308. *p_buf = p;
  4309. return true;
  4310. fail:
  4311. return false;
  4312. }
  4313. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4314. static bool
  4315. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4316. WASMModule *module, char *error_buf,
  4317. uint32 error_buf_size)
  4318. {
  4319. const uint8 *p = buf, *p_end = buf_end;
  4320. uint8 table_elem_idx_type;
  4321. uint32 table_segment_count, i;
  4322. uint64 total_size;
  4323. WASMTableSeg *table_segment;
  4324. read_leb_uint32(p, p_end, table_segment_count);
  4325. if (table_segment_count) {
  4326. module->table_seg_count = table_segment_count;
  4327. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4328. if (!(module->table_segments =
  4329. loader_malloc(total_size, error_buf, error_buf_size))) {
  4330. return false;
  4331. }
  4332. table_segment = module->table_segments;
  4333. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4334. if (p >= p_end) {
  4335. set_error_buf(error_buf, error_buf_size,
  4336. "invalid value type or "
  4337. "invalid elements segment kind");
  4338. return false;
  4339. }
  4340. table_elem_idx_type = VALUE_TYPE_I32;
  4341. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4342. read_leb_uint32(p, p_end, table_segment->mode);
  4343. /* last three bits */
  4344. table_segment->mode = table_segment->mode & 0x07;
  4345. switch (table_segment->mode) {
  4346. /* elemkind/elemtype + active */
  4347. case 0:
  4348. case 4:
  4349. {
  4350. #if WASM_ENABLE_GC != 0
  4351. if (table_segment->mode == 0) {
  4352. /* vec(funcidx), set elem type to (ref func) */
  4353. WASMRefType elem_ref_type = { 0 };
  4354. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4355. wasm_set_refheaptype_common(
  4356. &elem_ref_type.ref_ht_common, false,
  4357. HEAP_TYPE_FUNC);
  4358. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4359. module->ref_type_set, &elem_ref_type,
  4360. error_buf, error_buf_size)))
  4361. return false;
  4362. }
  4363. else {
  4364. /* vec(expr), set elem type to funcref */
  4365. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4366. }
  4367. #else
  4368. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4369. #endif
  4370. table_segment->table_index = 0;
  4371. if (!check_table_index(module, table_segment->table_index,
  4372. error_buf, error_buf_size))
  4373. return false;
  4374. #if WASM_ENABLE_MEMORY64 != 0
  4375. table_elem_idx_type =
  4376. is_table_64bit(module, table_segment->table_index)
  4377. ? VALUE_TYPE_I64
  4378. : VALUE_TYPE_I32;
  4379. #endif
  4380. if (!load_init_expr(module, &p, p_end,
  4381. &table_segment->base_offset,
  4382. table_elem_idx_type, NULL, error_buf,
  4383. error_buf_size))
  4384. return false;
  4385. if (table_segment->mode == 0) {
  4386. /* vec(funcidx) */
  4387. if (!load_func_index_vec(&p, p_end, module,
  4388. table_segment, error_buf,
  4389. error_buf_size))
  4390. return false;
  4391. }
  4392. else {
  4393. /* vec(expr) */
  4394. if (!load_init_expr_vec(&p, p_end, module,
  4395. table_segment, error_buf,
  4396. error_buf_size))
  4397. return false;
  4398. }
  4399. if (!check_table_elem_type(module,
  4400. table_segment->table_index,
  4401. table_segment->elem_type,
  4402. error_buf, error_buf_size))
  4403. return false;
  4404. break;
  4405. }
  4406. /* elemkind + passive/declarative */
  4407. case 1:
  4408. case 3:
  4409. if (!load_elem_type(module, &p, p_end,
  4410. &table_segment->elem_type,
  4411. #if WASM_ENABLE_GC != 0
  4412. &table_segment->elem_ref_type,
  4413. #endif
  4414. true, error_buf, error_buf_size))
  4415. return false;
  4416. /* vec(funcidx) */
  4417. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4418. error_buf, error_buf_size))
  4419. return false;
  4420. break;
  4421. /* elemkind/elemtype + table_idx + active */
  4422. case 2:
  4423. case 6:
  4424. if (!load_table_index(&p, p_end, module,
  4425. &table_segment->table_index,
  4426. error_buf, error_buf_size))
  4427. return false;
  4428. #if WASM_ENABLE_MEMORY64 != 0
  4429. table_elem_idx_type =
  4430. is_table_64bit(module, table_segment->table_index)
  4431. ? VALUE_TYPE_I64
  4432. : VALUE_TYPE_I32;
  4433. #endif
  4434. if (!load_init_expr(module, &p, p_end,
  4435. &table_segment->base_offset,
  4436. table_elem_idx_type, NULL, error_buf,
  4437. error_buf_size))
  4438. return false;
  4439. if (!load_elem_type(module, &p, p_end,
  4440. &table_segment->elem_type,
  4441. #if WASM_ENABLE_GC != 0
  4442. &table_segment->elem_ref_type,
  4443. #endif
  4444. table_segment->mode == 2 ? true : false,
  4445. error_buf, error_buf_size))
  4446. return false;
  4447. if (table_segment->mode == 2) {
  4448. /* vec(funcidx) */
  4449. if (!load_func_index_vec(&p, p_end, module,
  4450. table_segment, error_buf,
  4451. error_buf_size))
  4452. return false;
  4453. }
  4454. else {
  4455. /* vec(expr) */
  4456. if (!load_init_expr_vec(&p, p_end, module,
  4457. table_segment, error_buf,
  4458. error_buf_size))
  4459. return false;
  4460. }
  4461. if (!check_table_elem_type(module,
  4462. table_segment->table_index,
  4463. table_segment->elem_type,
  4464. error_buf, error_buf_size))
  4465. return false;
  4466. break;
  4467. case 5:
  4468. case 7:
  4469. if (!load_elem_type(module, &p, p_end,
  4470. &table_segment->elem_type,
  4471. #if WASM_ENABLE_GC != 0
  4472. &table_segment->elem_ref_type,
  4473. #endif
  4474. false, error_buf, error_buf_size))
  4475. return false;
  4476. /* vec(expr) */
  4477. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4478. error_buf, error_buf_size))
  4479. return false;
  4480. break;
  4481. default:
  4482. set_error_buf(error_buf, error_buf_size,
  4483. "unknown element segment kind");
  4484. return false;
  4485. }
  4486. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4487. /*
  4488. * like: 00 41 05 0b 04 00 01 00 01
  4489. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4490. */
  4491. if (!load_table_index(&p, p_end, module,
  4492. &table_segment->table_index, error_buf,
  4493. error_buf_size))
  4494. return false;
  4495. #if WASM_ENABLE_MEMORY64 != 0
  4496. table_elem_idx_type =
  4497. is_table_64bit(module, table_segment->table_index)
  4498. ? VALUE_TYPE_I64
  4499. : VALUE_TYPE_I32;
  4500. #endif
  4501. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4502. table_elem_idx_type, NULL, error_buf,
  4503. error_buf_size))
  4504. return false;
  4505. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4506. error_buf, error_buf_size))
  4507. return false;
  4508. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4509. if (!check_table_elem_type(module, table_segment->table_index,
  4510. table_segment->elem_type, error_buf,
  4511. error_buf_size))
  4512. return false;
  4513. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4514. #if WASM_ENABLE_MEMORY64 != 0
  4515. if (table_elem_idx_type == VALUE_TYPE_I64
  4516. && table_segment->base_offset.u.unary.v.u64 > UINT32_MAX) {
  4517. set_error_buf(error_buf, error_buf_size,
  4518. "In table64, table base offset can't be "
  4519. "larger than UINT32_MAX");
  4520. return false;
  4521. }
  4522. #endif
  4523. #if WASM_ENABLE_WAMR_COMPILER != 0
  4524. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4525. module->is_ref_types_used = true;
  4526. #endif
  4527. }
  4528. }
  4529. if (p != p_end) {
  4530. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4531. return false;
  4532. }
  4533. LOG_VERBOSE("Load table segment section success.\n");
  4534. return true;
  4535. fail:
  4536. return false;
  4537. }
  4538. #if WASM_ENABLE_BULK_MEMORY != 0
  4539. static bool
  4540. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4541. int data_seg_len, char *error_buf,
  4542. uint32 error_buf_size)
  4543. {
  4544. if (has_datacount_section && datacount_len != data_seg_len) {
  4545. set_error_buf(error_buf, error_buf_size,
  4546. "data count and data section have inconsistent lengths");
  4547. return false;
  4548. }
  4549. return true;
  4550. }
  4551. #endif
  4552. static bool
  4553. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4554. WASMModule *module,
  4555. #if WASM_ENABLE_BULK_MEMORY != 0
  4556. bool has_datacount_section,
  4557. #endif
  4558. bool clone_data_seg, char *error_buf,
  4559. uint32 error_buf_size)
  4560. {
  4561. const uint8 *p = buf, *p_end = buf_end;
  4562. uint32 data_seg_count, i, mem_index, data_seg_len;
  4563. uint64 total_size;
  4564. WASMDataSeg *dataseg;
  4565. InitializerExpression init_expr;
  4566. #if WASM_ENABLE_BULK_MEMORY != 0
  4567. bool is_passive = false;
  4568. uint32 mem_flag;
  4569. #endif
  4570. uint8 mem_offset_type = VALUE_TYPE_I32;
  4571. read_leb_uint32(p, p_end, data_seg_count);
  4572. #if WASM_ENABLE_BULK_MEMORY != 0
  4573. if (!check_data_count_consistency(has_datacount_section,
  4574. module->data_seg_count1, data_seg_count,
  4575. error_buf, error_buf_size)) {
  4576. return false;
  4577. }
  4578. #endif
  4579. if (data_seg_count) {
  4580. module->data_seg_count = data_seg_count;
  4581. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4582. if (!(module->data_segments =
  4583. loader_malloc(total_size, error_buf, error_buf_size))) {
  4584. return false;
  4585. }
  4586. for (i = 0; i < data_seg_count; i++) {
  4587. read_leb_uint32(p, p_end, mem_index);
  4588. #if WASM_ENABLE_BULK_MEMORY != 0
  4589. is_passive = false;
  4590. mem_flag = mem_index & 0x03;
  4591. switch (mem_flag) {
  4592. case 0x01:
  4593. is_passive = true;
  4594. #if WASM_ENABLE_WAMR_COMPILER != 0
  4595. module->is_bulk_memory_used = true;
  4596. #endif
  4597. break;
  4598. case 0x00:
  4599. /* no memory index, treat index as 0 */
  4600. mem_index = 0;
  4601. goto check_mem_index;
  4602. case 0x02:
  4603. /* read following memory index */
  4604. read_leb_uint32(p, p_end, mem_index);
  4605. #if WASM_ENABLE_WAMR_COMPILER != 0
  4606. module->is_bulk_memory_used = true;
  4607. #endif
  4608. check_mem_index:
  4609. if (mem_index
  4610. >= module->import_memory_count + module->memory_count) {
  4611. set_error_buf_v(error_buf, error_buf_size,
  4612. "unknown memory %d", mem_index);
  4613. return false;
  4614. }
  4615. break;
  4616. case 0x03:
  4617. default:
  4618. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4619. return false;
  4620. break;
  4621. }
  4622. #else
  4623. if (mem_index
  4624. >= module->import_memory_count + module->memory_count) {
  4625. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4626. mem_index);
  4627. return false;
  4628. }
  4629. #endif /* WASM_ENABLE_BULK_MEMORY */
  4630. #if WASM_ENABLE_BULK_MEMORY != 0
  4631. if (!is_passive)
  4632. #endif
  4633. {
  4634. #if WASM_ENABLE_MEMORY64 != 0
  4635. /* This memory_flag is from memory instead of data segment */
  4636. uint8 memory_flag;
  4637. if (module->import_memory_count > 0) {
  4638. memory_flag = module->import_memories[mem_index]
  4639. .u.memory.mem_type.flags;
  4640. }
  4641. else {
  4642. memory_flag =
  4643. module
  4644. ->memories[mem_index - module->import_memory_count]
  4645. .flags;
  4646. }
  4647. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4648. : VALUE_TYPE_I32;
  4649. #else
  4650. mem_offset_type = VALUE_TYPE_I32;
  4651. #endif
  4652. }
  4653. #if WASM_ENABLE_BULK_MEMORY != 0
  4654. if (!is_passive)
  4655. #endif
  4656. if (!load_init_expr(module, &p, p_end, &init_expr,
  4657. mem_offset_type, NULL, error_buf,
  4658. error_buf_size))
  4659. return false;
  4660. read_leb_uint32(p, p_end, data_seg_len);
  4661. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4662. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4663. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  4664. destroy_init_expr(module, &init_expr);
  4665. #endif
  4666. return false;
  4667. }
  4668. #if WASM_ENABLE_BULK_MEMORY != 0
  4669. dataseg->is_passive = is_passive;
  4670. if (!is_passive)
  4671. #endif
  4672. {
  4673. bh_memcpy_s(&dataseg->base_offset,
  4674. sizeof(InitializerExpression), &init_expr,
  4675. sizeof(InitializerExpression));
  4676. dataseg->memory_index = mem_index;
  4677. }
  4678. dataseg->data_length = data_seg_len;
  4679. CHECK_BUF(p, p_end, data_seg_len);
  4680. if (clone_data_seg) {
  4681. if (!(dataseg->data = loader_malloc(
  4682. dataseg->data_length, error_buf, error_buf_size))) {
  4683. return false;
  4684. }
  4685. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4686. data_seg_len);
  4687. }
  4688. else {
  4689. dataseg->data = (uint8 *)p;
  4690. }
  4691. dataseg->is_data_cloned = clone_data_seg;
  4692. p += data_seg_len;
  4693. }
  4694. }
  4695. if (p != p_end) {
  4696. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4697. return false;
  4698. }
  4699. LOG_VERBOSE("Load data segment section success.\n");
  4700. return true;
  4701. fail:
  4702. return false;
  4703. }
  4704. #if WASM_ENABLE_BULK_MEMORY != 0
  4705. static bool
  4706. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4707. WASMModule *module, char *error_buf,
  4708. uint32 error_buf_size)
  4709. {
  4710. const uint8 *p = buf, *p_end = buf_end;
  4711. uint32 data_seg_count1 = 0;
  4712. read_leb_uint32(p, p_end, data_seg_count1);
  4713. module->data_seg_count1 = data_seg_count1;
  4714. if (p != p_end) {
  4715. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4716. return false;
  4717. }
  4718. #if WASM_ENABLE_WAMR_COMPILER != 0
  4719. module->is_bulk_memory_used = true;
  4720. #endif
  4721. LOG_VERBOSE("Load datacount section success.\n");
  4722. return true;
  4723. fail:
  4724. return false;
  4725. }
  4726. #endif
  4727. #if WASM_ENABLE_TAGS != 0
  4728. static bool
  4729. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4730. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4731. uint32 error_buf_size)
  4732. {
  4733. (void)buf_code;
  4734. (void)buf_code_end;
  4735. const uint8 *p = buf, *p_end = buf_end;
  4736. size_t total_size = 0;
  4737. /* number of tags defined in the section */
  4738. uint32 section_tag_count = 0;
  4739. uint8 tag_attribute;
  4740. uint32 tag_type;
  4741. WASMTag *tag = NULL;
  4742. /* get tag count */
  4743. read_leb_uint32(p, p_end, section_tag_count);
  4744. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4745. error_buf, error_buf_size))
  4746. return false;
  4747. module->tag_count = section_tag_count;
  4748. if (section_tag_count) {
  4749. total_size = sizeof(WASMTag *) * module->tag_count;
  4750. if (!(module->tags =
  4751. loader_malloc(total_size, error_buf, error_buf_size))) {
  4752. return false;
  4753. }
  4754. /* load each tag, imported tags precede the tags */
  4755. uint32 tag_index;
  4756. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4757. /* get the one byte attribute */
  4758. CHECK_BUF(p, p_end, 1);
  4759. tag_attribute = read_uint8(p);
  4760. /* get type */
  4761. read_leb_uint32(p, p_end, tag_type);
  4762. /* compare against module->types */
  4763. if (!check_function_type(module, tag_type, error_buf,
  4764. error_buf_size)) {
  4765. return false;
  4766. }
  4767. /* get return type (must be 0) */
  4768. /* check, that the type of the referred tag returns void */
  4769. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4770. if (func_type->result_count != 0) {
  4771. set_error_buf(error_buf, error_buf_size,
  4772. "non-empty tag result type");
  4773. goto fail;
  4774. }
  4775. if (!(tag = module->tags[tag_index] = loader_malloc(
  4776. sizeof(WASMTag), error_buf, error_buf_size))) {
  4777. return false;
  4778. }
  4779. /* store to module tag declarations */
  4780. tag->attribute = tag_attribute;
  4781. tag->type = tag_type;
  4782. tag->tag_type = func_type;
  4783. }
  4784. }
  4785. if (p != p_end) {
  4786. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4787. return false;
  4788. }
  4789. LOG_VERBOSE("Load tag section success.\n");
  4790. return true;
  4791. fail:
  4792. return false;
  4793. }
  4794. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4795. static bool
  4796. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4797. const uint8 *buf_func_end, WASMModule *module,
  4798. char *error_buf, uint32 error_buf_size)
  4799. {
  4800. const uint8 *p = buf, *p_end = buf_end;
  4801. const uint8 *p_func = buf_func;
  4802. uint32 func_count = 0, code_count;
  4803. /* code has been loaded in function section, so pass it here, just check
  4804. * whether function and code section have inconsistent lengths */
  4805. read_leb_uint32(p, p_end, code_count);
  4806. if (buf_func)
  4807. read_leb_uint32(p_func, buf_func_end, func_count);
  4808. if (func_count != code_count) {
  4809. set_error_buf(error_buf, error_buf_size,
  4810. "function and code section have inconsistent lengths");
  4811. return false;
  4812. }
  4813. LOG_VERBOSE("Load code segment section success.\n");
  4814. (void)module;
  4815. return true;
  4816. fail:
  4817. return false;
  4818. }
  4819. static bool
  4820. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4821. char *error_buf, uint32 error_buf_size)
  4822. {
  4823. const uint8 *p = buf, *p_end = buf_end;
  4824. WASMFuncType *type;
  4825. uint32 start_function;
  4826. read_leb_uint32(p, p_end, start_function);
  4827. if (start_function
  4828. >= module->function_count + module->import_function_count) {
  4829. set_error_buf(error_buf, error_buf_size, "unknown function");
  4830. return false;
  4831. }
  4832. if (start_function < module->import_function_count)
  4833. type = module->import_functions[start_function].u.function.func_type;
  4834. else
  4835. type = module->functions[start_function - module->import_function_count]
  4836. ->func_type;
  4837. if (type->param_count != 0 || type->result_count != 0) {
  4838. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4839. return false;
  4840. }
  4841. module->start_function = start_function;
  4842. if (p != p_end) {
  4843. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4844. return false;
  4845. }
  4846. LOG_VERBOSE("Load start section success.\n");
  4847. return true;
  4848. fail:
  4849. return false;
  4850. }
  4851. #if WASM_ENABLE_STRINGREF != 0
  4852. static bool
  4853. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4854. WASMModule *module, bool is_load_from_file_buf,
  4855. char *error_buf, uint32 error_buf_size)
  4856. {
  4857. const uint8 *p = buf, *p_end = buf_end;
  4858. int32 deferred_count, immediate_count, string_length, i;
  4859. uint64 total_size;
  4860. read_leb_uint32(p, p_end, deferred_count);
  4861. read_leb_uint32(p, p_end, immediate_count);
  4862. /* proposal set deferred_count for future extension */
  4863. if (deferred_count != 0) {
  4864. goto fail;
  4865. }
  4866. if (immediate_count > 0) {
  4867. total_size = sizeof(char *) * (uint64)immediate_count;
  4868. if (!(module->string_literal_ptrs =
  4869. loader_malloc(total_size, error_buf, error_buf_size))) {
  4870. goto fail;
  4871. }
  4872. module->string_literal_count = immediate_count;
  4873. total_size = sizeof(uint32) * (uint64)immediate_count;
  4874. if (!(module->string_literal_lengths =
  4875. loader_malloc(total_size, error_buf, error_buf_size))) {
  4876. goto fail;
  4877. }
  4878. for (i = 0; i < immediate_count; i++) {
  4879. read_leb_uint32(p, p_end, string_length);
  4880. CHECK_BUF(p, p_end, string_length);
  4881. module->string_literal_ptrs[i] = p;
  4882. module->string_literal_lengths[i] = string_length;
  4883. p += string_length;
  4884. }
  4885. }
  4886. if (p != p_end) {
  4887. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4888. goto fail;
  4889. }
  4890. LOG_VERBOSE("Load stringref section success.\n");
  4891. return true;
  4892. fail:
  4893. return false;
  4894. }
  4895. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4896. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4897. static bool
  4898. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4899. bool is_load_from_file_buf, char *error_buf,
  4900. uint32 error_buf_size)
  4901. {
  4902. const uint8 *p = buf, *p_end = buf_end;
  4903. uint32 name_type, subsection_size;
  4904. uint32 previous_name_type = 0;
  4905. uint32 num_func_name;
  4906. uint32 func_index;
  4907. uint32 previous_func_index = ~0U;
  4908. uint32 func_name_len;
  4909. uint32 name_index;
  4910. int i = 0;
  4911. if (p >= p_end) {
  4912. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4913. return false;
  4914. }
  4915. while (p < p_end) {
  4916. read_leb_uint32(p, p_end, name_type);
  4917. if (i != 0) {
  4918. if (name_type == previous_name_type) {
  4919. set_error_buf(error_buf, error_buf_size,
  4920. "duplicate sub-section");
  4921. return false;
  4922. }
  4923. if (name_type < previous_name_type) {
  4924. set_error_buf(error_buf, error_buf_size,
  4925. "out-of-order sub-section");
  4926. return false;
  4927. }
  4928. }
  4929. previous_name_type = name_type;
  4930. read_leb_uint32(p, p_end, subsection_size);
  4931. CHECK_BUF(p, p_end, subsection_size);
  4932. switch (name_type) {
  4933. case SUB_SECTION_TYPE_FUNC:
  4934. if (subsection_size) {
  4935. read_leb_uint32(p, p_end, num_func_name);
  4936. for (name_index = 0; name_index < num_func_name;
  4937. name_index++) {
  4938. read_leb_uint32(p, p_end, func_index);
  4939. if (func_index == previous_func_index) {
  4940. set_error_buf(error_buf, error_buf_size,
  4941. "duplicate function name");
  4942. return false;
  4943. }
  4944. if (func_index < previous_func_index
  4945. && previous_func_index != ~0U) {
  4946. set_error_buf(error_buf, error_buf_size,
  4947. "out-of-order function index ");
  4948. return false;
  4949. }
  4950. previous_func_index = func_index;
  4951. read_leb_uint32(p, p_end, func_name_len);
  4952. CHECK_BUF(p, p_end, func_name_len);
  4953. /* Skip the import functions */
  4954. if (func_index >= module->import_function_count) {
  4955. func_index -= module->import_function_count;
  4956. if (func_index >= module->function_count) {
  4957. set_error_buf(error_buf, error_buf_size,
  4958. "out-of-range function index");
  4959. return false;
  4960. }
  4961. if (!(module->functions[func_index]->field_name =
  4962. wasm_const_str_list_insert(
  4963. p, func_name_len, module,
  4964. #if WASM_ENABLE_WAMR_COMPILER != 0
  4965. false,
  4966. #else
  4967. is_load_from_file_buf,
  4968. #endif
  4969. error_buf, error_buf_size))) {
  4970. return false;
  4971. }
  4972. }
  4973. p += func_name_len;
  4974. }
  4975. }
  4976. break;
  4977. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4978. */
  4979. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4980. default:
  4981. p = p + subsection_size;
  4982. break;
  4983. }
  4984. i++;
  4985. }
  4986. return true;
  4987. fail:
  4988. return false;
  4989. }
  4990. #endif
  4991. #if WASM_ENABLE_BRANCH_HINTS != 0
  4992. static bool
  4993. handle_branch_hint_section(const uint8 *buf, const uint8 *buf_end,
  4994. WASMModule *module, char *error_buf,
  4995. uint32 error_buf_size)
  4996. {
  4997. if (module->function_hints == NULL) {
  4998. module->function_hints = loader_malloc(
  4999. sizeof(struct WASMCompilationHint) * module->function_count,
  5000. error_buf, error_buf_size);
  5001. }
  5002. uint32 numFunctionHints = 0;
  5003. read_leb_uint32(buf, buf_end, numFunctionHints);
  5004. for (uint32 i = 0; i < numFunctionHints; ++i) {
  5005. uint32 func_idx;
  5006. read_leb_uint32(buf, buf_end, func_idx);
  5007. if (!check_function_index(module, func_idx, error_buf,
  5008. error_buf_size)) {
  5009. goto fail;
  5010. }
  5011. if (func_idx < module->import_function_count) {
  5012. set_error_buf(error_buf, error_buf_size,
  5013. "branch hint for imported function is not allowed");
  5014. goto fail;
  5015. }
  5016. struct WASMCompilationHint *current_hint =
  5017. (struct WASMCompilationHint *)&module
  5018. ->function_hints[func_idx - module->import_function_count];
  5019. while (current_hint->next != NULL) {
  5020. current_hint = current_hint->next;
  5021. }
  5022. uint32 num_hints;
  5023. read_leb_uint32(buf, buf_end, num_hints);
  5024. struct WASMCompilationHintBranchHint *new_hints = loader_malloc(
  5025. sizeof(struct WASMCompilationHintBranchHint) * num_hints, error_buf,
  5026. error_buf_size);
  5027. for (uint32 j = 0; j < num_hints; ++j) {
  5028. struct WASMCompilationHintBranchHint *new_hint = &new_hints[j];
  5029. new_hint->next = NULL;
  5030. new_hint->type = WASM_COMPILATION_BRANCH_HINT;
  5031. read_leb_uint32(buf, buf_end, new_hint->offset);
  5032. uint32 size;
  5033. read_leb_uint32(buf, buf_end, size);
  5034. if (size != 1) {
  5035. set_error_buf_v(error_buf, error_buf_size,
  5036. "invalid branch hint size, expected 1, got %d.",
  5037. size);
  5038. wasm_runtime_free(new_hint);
  5039. goto fail;
  5040. }
  5041. uint8 data = *buf++;
  5042. if (data == 0x00)
  5043. new_hint->is_likely = false;
  5044. else if (data == 0x01)
  5045. new_hint->is_likely = true;
  5046. else {
  5047. set_error_buf_v(error_buf, error_buf_size,
  5048. "invalid branch hint, expected 0 or 1, got %d",
  5049. data);
  5050. wasm_runtime_free(new_hint);
  5051. goto fail;
  5052. }
  5053. current_hint->next = (struct WASMCompilationHint *)new_hint;
  5054. current_hint = (struct WASMCompilationHint *)new_hint;
  5055. }
  5056. }
  5057. if (buf != buf_end) {
  5058. set_error_buf(error_buf, error_buf_size,
  5059. "invalid branch hint section, not filled until end");
  5060. goto fail;
  5061. }
  5062. return true;
  5063. fail:
  5064. return false;
  5065. }
  5066. #endif
  5067. static bool
  5068. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  5069. bool is_load_from_file_buf, char *error_buf,
  5070. uint32 error_buf_size)
  5071. {
  5072. const uint8 *p = buf, *p_end = buf_end;
  5073. char section_name[32];
  5074. uint32 name_len, buffer_len;
  5075. if (p >= p_end) {
  5076. set_error_buf(error_buf, error_buf_size, "unexpected end");
  5077. return false;
  5078. }
  5079. read_leb_uint32(p, p_end, name_len);
  5080. if (p + name_len > p_end) {
  5081. set_error_buf(error_buf, error_buf_size, "unexpected end");
  5082. return false;
  5083. }
  5084. if (!wasm_check_utf8_str(p, name_len)) {
  5085. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  5086. return false;
  5087. }
  5088. buffer_len = sizeof(section_name);
  5089. memset(section_name, 0, buffer_len);
  5090. if (name_len < buffer_len) {
  5091. bh_memcpy_s(section_name, buffer_len, p, name_len);
  5092. }
  5093. else {
  5094. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  5095. memset(section_name + buffer_len - 4, '.', 3);
  5096. }
  5097. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  5098. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  5099. module->name_section_buf = buf;
  5100. module->name_section_buf_end = buf_end;
  5101. p += name_len;
  5102. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  5103. error_buf, error_buf_size)) {
  5104. return false;
  5105. }
  5106. LOG_VERBOSE("Load custom name section success.");
  5107. }
  5108. #endif
  5109. #if WASM_ENABLE_BRANCH_HINTS != 0
  5110. if (name_len == 25
  5111. && memcmp((const char *)p, "metadata.code.branch_hint", 25) == 0) {
  5112. p += name_len;
  5113. if (!handle_branch_hint_section(p, p_end, module, error_buf,
  5114. error_buf_size)) {
  5115. return false;
  5116. }
  5117. LOG_VERBOSE("Load branch hint section success.");
  5118. }
  5119. #else
  5120. if (name_len == 25
  5121. && memcmp((const char *)p, "metadata.code.branch_hint", 25) == 0) {
  5122. LOG_VERBOSE("Found branch hint section, but branch hints are disabled "
  5123. "in this build, skipping.");
  5124. }
  5125. #endif
  5126. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5127. {
  5128. WASMCustomSection *section =
  5129. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  5130. if (!section) {
  5131. return false;
  5132. }
  5133. section->name_addr = (char *)p;
  5134. section->name_len = name_len;
  5135. section->content_addr = (uint8 *)(p + name_len);
  5136. section->content_len = (uint32)(p_end - p - name_len);
  5137. section->next = module->custom_section_list;
  5138. module->custom_section_list = section;
  5139. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  5140. return true;
  5141. }
  5142. #endif
  5143. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  5144. (void)is_load_from_file_buf;
  5145. (void)module;
  5146. return true;
  5147. fail:
  5148. return false;
  5149. }
  5150. static void
  5151. calculate_global_data_offset(WASMModule *module)
  5152. {
  5153. uint32 i, data_offset;
  5154. data_offset = 0;
  5155. for (i = 0; i < module->import_global_count; i++) {
  5156. WASMGlobalImport *import_global =
  5157. &((module->import_globals + i)->u.global);
  5158. #if WASM_ENABLE_FAST_JIT != 0
  5159. import_global->data_offset = data_offset;
  5160. #endif
  5161. data_offset += wasm_value_type_size(import_global->type.val_type);
  5162. }
  5163. for (i = 0; i < module->global_count; i++) {
  5164. WASMGlobal *global = module->globals + i;
  5165. #if WASM_ENABLE_FAST_JIT != 0
  5166. global->data_offset = data_offset;
  5167. #endif
  5168. data_offset += wasm_value_type_size(global->type.val_type);
  5169. }
  5170. module->global_data_size = data_offset;
  5171. }
  5172. #if WASM_ENABLE_FAST_JIT != 0
  5173. static bool
  5174. init_fast_jit_functions(WASMModule *module, char *error_buf,
  5175. uint32 error_buf_size)
  5176. {
  5177. #if WASM_ENABLE_LAZY_JIT != 0
  5178. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  5179. #endif
  5180. uint32 i;
  5181. if (!module->function_count)
  5182. return true;
  5183. if (!(module->fast_jit_func_ptrs =
  5184. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  5185. error_buf_size))) {
  5186. return false;
  5187. }
  5188. #if WASM_ENABLE_LAZY_JIT != 0
  5189. for (i = 0; i < module->function_count; i++) {
  5190. module->fast_jit_func_ptrs[i] =
  5191. jit_globals->compile_fast_jit_and_then_call;
  5192. }
  5193. #endif
  5194. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  5195. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  5196. set_error_buf(error_buf, error_buf_size,
  5197. "init fast jit thread lock failed");
  5198. return false;
  5199. }
  5200. module->fast_jit_thread_locks_inited[i] = true;
  5201. }
  5202. return true;
  5203. }
  5204. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  5205. #if WASM_ENABLE_JIT != 0
  5206. static bool
  5207. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  5208. uint32 error_buf_size)
  5209. {
  5210. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  5211. AOTCompOption option = { 0 };
  5212. char *aot_last_error;
  5213. uint64 size;
  5214. #if WASM_ENABLE_GC != 0
  5215. bool gc_enabled = true;
  5216. #else
  5217. bool gc_enabled = false;
  5218. #endif
  5219. if (module->function_count == 0)
  5220. return true;
  5221. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5222. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  5223. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  5224. return false;
  5225. }
  5226. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  5227. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  5228. os_mutex_destroy(&module->tierup_wait_lock);
  5229. return false;
  5230. }
  5231. module->tierup_wait_lock_inited = true;
  5232. #endif
  5233. size = sizeof(void *) * (uint64)module->function_count
  5234. + sizeof(bool) * (uint64)module->function_count;
  5235. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  5236. return false;
  5237. }
  5238. module->func_ptrs_compiled =
  5239. (bool *)((uint8 *)module->func_ptrs
  5240. + sizeof(void *) * module->function_count);
  5241. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  5242. if (!module->comp_data) {
  5243. aot_last_error = aot_get_last_error();
  5244. bh_assert(aot_last_error != NULL);
  5245. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5246. return false;
  5247. }
  5248. option.is_jit_mode = true;
  5249. option.opt_level = llvm_jit_options->opt_level;
  5250. option.size_level = llvm_jit_options->size_level;
  5251. option.segue_flags = llvm_jit_options->segue_flags;
  5252. option.quick_invoke_c_api_import =
  5253. llvm_jit_options->quick_invoke_c_api_import;
  5254. #if WASM_ENABLE_BULK_MEMORY != 0
  5255. option.enable_bulk_memory = true;
  5256. #endif
  5257. #if WASM_ENABLE_THREAD_MGR != 0
  5258. option.enable_thread_mgr = true;
  5259. #endif
  5260. #if WASM_ENABLE_TAIL_CALL != 0
  5261. option.enable_tail_call = true;
  5262. #endif
  5263. #if WASM_ENABLE_SIMD != 0
  5264. option.enable_simd = true;
  5265. #endif
  5266. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5267. option.enable_ref_types = true;
  5268. #elif WASM_ENABLE_GC != 0
  5269. option.enable_gc = true;
  5270. #endif
  5271. option.enable_aux_stack_check = true;
  5272. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  5273. || WASM_ENABLE_AOT_STACK_FRAME != 0
  5274. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  5275. aot_call_stack_features_init_default(&option.call_stack_features);
  5276. #endif
  5277. #if WASM_ENABLE_PERF_PROFILING != 0
  5278. option.enable_perf_profiling = true;
  5279. #endif
  5280. #if WASM_ENABLE_MEMORY_PROFILING != 0
  5281. option.enable_memory_profiling = true;
  5282. option.enable_stack_estimation = true;
  5283. #endif
  5284. #if WASM_ENABLE_SHARED_HEAP != 0
  5285. option.enable_shared_heap = true;
  5286. #endif
  5287. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  5288. if (!module->comp_ctx) {
  5289. aot_last_error = aot_get_last_error();
  5290. bh_assert(aot_last_error != NULL);
  5291. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5292. return false;
  5293. }
  5294. return true;
  5295. }
  5296. static bool
  5297. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  5298. uint32 error_buf_size)
  5299. {
  5300. char *aot_last_error;
  5301. uint32 i;
  5302. if (module->function_count == 0)
  5303. return true;
  5304. if (!aot_compile_wasm(module->comp_ctx)) {
  5305. aot_last_error = aot_get_last_error();
  5306. bh_assert(aot_last_error != NULL);
  5307. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5308. return false;
  5309. }
  5310. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5311. if (module->orcjit_stop_compiling)
  5312. return false;
  5313. #endif
  5314. bh_print_time("Begin to lookup llvm jit functions");
  5315. for (i = 0; i < module->function_count; i++) {
  5316. LLVMOrcJITTargetAddress func_addr = 0;
  5317. LLVMErrorRef error;
  5318. char func_name[48];
  5319. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  5320. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  5321. func_name);
  5322. if (error != LLVMErrorSuccess) {
  5323. char *err_msg = LLVMGetErrorMessage(error);
  5324. set_error_buf_v(error_buf, error_buf_size,
  5325. "failed to compile llvm jit function: %s", err_msg);
  5326. LLVMDisposeErrorMessage(err_msg);
  5327. return false;
  5328. }
  5329. /**
  5330. * No need to lock the func_ptr[func_idx] here as it is basic
  5331. * data type, the load/store for it can be finished by one cpu
  5332. * instruction, and there can be only one cpu instruction
  5333. * loading/storing at the same time.
  5334. */
  5335. module->func_ptrs[i] = (void *)func_addr;
  5336. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5337. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  5338. if (module->orcjit_stop_compiling)
  5339. return false;
  5340. #endif
  5341. }
  5342. bh_print_time("End lookup llvm jit functions");
  5343. return true;
  5344. }
  5345. #endif /* end of WASM_ENABLE_JIT != 0 */
  5346. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5347. && WASM_ENABLE_LAZY_JIT != 0
  5348. static void *
  5349. init_llvm_jit_functions_stage2_callback(void *arg)
  5350. {
  5351. WASMModule *module = (WASMModule *)arg;
  5352. char error_buf[128];
  5353. uint32 error_buf_size = (uint32)sizeof(error_buf);
  5354. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5355. module->orcjit_stop_compiling = true;
  5356. return NULL;
  5357. }
  5358. os_mutex_lock(&module->tierup_wait_lock);
  5359. module->llvm_jit_inited = true;
  5360. os_cond_broadcast(&module->tierup_wait_cond);
  5361. os_mutex_unlock(&module->tierup_wait_lock);
  5362. return NULL;
  5363. }
  5364. #endif
  5365. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5366. /* The callback function to compile jit functions */
  5367. static void *
  5368. orcjit_thread_callback(void *arg)
  5369. {
  5370. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  5371. #if WASM_ENABLE_JIT != 0
  5372. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  5373. #endif
  5374. WASMModule *module = thread_arg->module;
  5375. uint32 group_idx = thread_arg->group_idx;
  5376. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  5377. uint32 func_count = module->function_count;
  5378. uint32 i;
  5379. #if WASM_ENABLE_FAST_JIT != 0
  5380. /* Compile fast jit functions of this group */
  5381. for (i = group_idx; i < func_count; i += group_stride) {
  5382. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  5383. LOG_ERROR("failed to compile fast jit function %u\n", i);
  5384. break;
  5385. }
  5386. if (module->orcjit_stop_compiling) {
  5387. return NULL;
  5388. }
  5389. }
  5390. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5391. os_mutex_lock(&module->tierup_wait_lock);
  5392. module->fast_jit_ready_groups++;
  5393. os_mutex_unlock(&module->tierup_wait_lock);
  5394. #endif
  5395. #endif
  5396. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5397. && WASM_ENABLE_LAZY_JIT != 0
  5398. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  5399. for (i = group_idx; i < func_count;
  5400. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5401. uint32 j;
  5402. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5403. if (i + j * group_stride < func_count) {
  5404. if (!jit_compiler_set_call_to_fast_jit(
  5405. module,
  5406. i + j * group_stride + module->import_function_count)) {
  5407. LOG_ERROR(
  5408. "failed to compile call_to_fast_jit for func %u\n",
  5409. i + j * group_stride + module->import_function_count);
  5410. module->orcjit_stop_compiling = true;
  5411. return NULL;
  5412. }
  5413. }
  5414. if (module->orcjit_stop_compiling) {
  5415. return NULL;
  5416. }
  5417. }
  5418. }
  5419. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5420. fast jit functions are compiled */
  5421. os_mutex_lock(&module->tierup_wait_lock);
  5422. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5423. && module->fast_jit_ready_groups >= group_stride)) {
  5424. os_cond_reltimedwait(&module->tierup_wait_cond,
  5425. &module->tierup_wait_lock, 10000);
  5426. if (module->orcjit_stop_compiling) {
  5427. /* init_llvm_jit_functions_stage2 failed */
  5428. os_mutex_unlock(&module->tierup_wait_lock);
  5429. return NULL;
  5430. }
  5431. }
  5432. os_mutex_unlock(&module->tierup_wait_lock);
  5433. #endif
  5434. #if WASM_ENABLE_JIT != 0
  5435. /* Compile llvm jit functions of this group */
  5436. for (i = group_idx; i < func_count;
  5437. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5438. LLVMOrcJITTargetAddress func_addr = 0;
  5439. LLVMErrorRef error;
  5440. char func_name[48];
  5441. typedef void (*F)(void);
  5442. union {
  5443. F f;
  5444. void *v;
  5445. } u;
  5446. uint32 j;
  5447. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5448. "_wrapper");
  5449. LOG_DEBUG("compile llvm jit func %s", func_name);
  5450. error =
  5451. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5452. if (error != LLVMErrorSuccess) {
  5453. char *err_msg = LLVMGetErrorMessage(error);
  5454. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5455. LLVMDisposeErrorMessage(err_msg);
  5456. break;
  5457. }
  5458. /* Call the jit wrapper function to trigger its compilation, so as
  5459. to compile the actual jit functions, since we add the latter to
  5460. function list in the PartitionFunction callback */
  5461. u.v = (void *)func_addr;
  5462. u.f();
  5463. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5464. if (i + j * group_stride < func_count) {
  5465. module->func_ptrs_compiled[i + j * group_stride] = true;
  5466. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5467. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5468. i + j * group_stride);
  5469. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5470. func_name);
  5471. if (error != LLVMErrorSuccess) {
  5472. char *err_msg = LLVMGetErrorMessage(error);
  5473. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5474. err_msg);
  5475. LLVMDisposeErrorMessage(err_msg);
  5476. /* Ignore current llvm jit func, as its func ptr is
  5477. previous set to call_to_fast_jit, which also works */
  5478. continue;
  5479. }
  5480. jit_compiler_set_llvm_jit_func_ptr(
  5481. module,
  5482. i + j * group_stride + module->import_function_count,
  5483. (void *)func_addr);
  5484. /* Try to switch to call this llvm jit function instead of
  5485. fast jit function from fast jit jitted code */
  5486. jit_compiler_set_call_to_llvm_jit(
  5487. module,
  5488. i + j * group_stride + module->import_function_count);
  5489. #endif
  5490. }
  5491. }
  5492. if (module->orcjit_stop_compiling) {
  5493. break;
  5494. }
  5495. }
  5496. #endif
  5497. return NULL;
  5498. }
  5499. static void
  5500. orcjit_stop_compile_threads(WASMModule *module)
  5501. {
  5502. #if WASM_ENABLE_LAZY_JIT != 0
  5503. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5504. / sizeof(OrcJitThreadArg));
  5505. module->orcjit_stop_compiling = true;
  5506. for (i = 0; i < thread_num; i++) {
  5507. if (module->orcjit_threads[i])
  5508. os_thread_join(module->orcjit_threads[i], NULL);
  5509. }
  5510. #endif
  5511. }
  5512. static bool
  5513. compile_jit_functions(WASMModule *module, char *error_buf,
  5514. uint32 error_buf_size)
  5515. {
  5516. uint32 thread_num =
  5517. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5518. uint32 i, j;
  5519. bh_print_time("Begin to compile jit functions");
  5520. /* Create threads to compile the jit functions */
  5521. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5522. #if WASM_ENABLE_JIT != 0
  5523. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5524. #endif
  5525. module->orcjit_thread_args[i].module = module;
  5526. module->orcjit_thread_args[i].group_idx = i;
  5527. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5528. (void *)&module->orcjit_thread_args[i],
  5529. APP_THREAD_STACK_SIZE_DEFAULT)
  5530. != 0) {
  5531. set_error_buf(error_buf, error_buf_size,
  5532. "create orcjit compile thread failed");
  5533. /* Terminate the threads created */
  5534. module->orcjit_stop_compiling = true;
  5535. for (j = 0; j < i; j++) {
  5536. os_thread_join(module->orcjit_threads[j], NULL);
  5537. }
  5538. return false;
  5539. }
  5540. }
  5541. #if WASM_ENABLE_LAZY_JIT == 0
  5542. /* Wait until all jit functions are compiled for eager mode */
  5543. for (i = 0; i < thread_num; i++) {
  5544. if (module->orcjit_threads[i])
  5545. os_thread_join(module->orcjit_threads[i], NULL);
  5546. }
  5547. #if WASM_ENABLE_FAST_JIT != 0
  5548. /* Ensure all the fast-jit functions are compiled */
  5549. for (i = 0; i < module->function_count; i++) {
  5550. if (!jit_compiler_is_compiled(module,
  5551. i + module->import_function_count)) {
  5552. set_error_buf(error_buf, error_buf_size,
  5553. "failed to compile fast jit function");
  5554. return false;
  5555. }
  5556. }
  5557. #endif
  5558. #if WASM_ENABLE_JIT != 0
  5559. /* Ensure all the llvm-jit functions are compiled */
  5560. for (i = 0; i < module->function_count; i++) {
  5561. if (!module->func_ptrs_compiled[i]) {
  5562. set_error_buf(error_buf, error_buf_size,
  5563. "failed to compile llvm jit function");
  5564. return false;
  5565. }
  5566. }
  5567. #endif
  5568. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5569. bh_print_time("End compile jit functions");
  5570. return true;
  5571. }
  5572. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5573. static bool
  5574. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5575. uint32 cur_func_idx, char *error_buf,
  5576. uint32 error_buf_size);
  5577. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5578. void **
  5579. wasm_interp_get_handle_table(void);
  5580. static void **handle_table;
  5581. #endif
  5582. static bool
  5583. load_from_sections(WASMModule *module, WASMSection *sections,
  5584. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5585. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5586. {
  5587. WASMExport *export;
  5588. WASMSection *section = sections;
  5589. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5590. *buf_func = NULL, *buf_func_end = NULL;
  5591. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5592. WASMGlobal *aux_stack_top_global = NULL, *global;
  5593. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5594. aux_stack_top = (uint64)-1LL;
  5595. uint32 global_index, func_index, i;
  5596. uint32 aux_data_end_global_index = (uint32)-1;
  5597. uint32 aux_heap_base_global_index = (uint32)-1;
  5598. WASMFuncType *func_type;
  5599. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5600. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5601. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5602. #if WASM_ENABLE_BULK_MEMORY != 0
  5603. bool has_datacount_section = false;
  5604. #endif
  5605. /* Find code and function sections if have */
  5606. while (section) {
  5607. if (section->section_type == SECTION_TYPE_CODE) {
  5608. buf_code = section->section_body;
  5609. buf_code_end = buf_code + section->section_body_size;
  5610. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5611. module->buf_code = (uint8 *)buf_code;
  5612. module->buf_code_size = section->section_body_size;
  5613. #endif
  5614. }
  5615. else if (section->section_type == SECTION_TYPE_FUNC) {
  5616. buf_func = section->section_body;
  5617. buf_func_end = buf_func + section->section_body_size;
  5618. }
  5619. section = section->next;
  5620. }
  5621. section = sections;
  5622. while (section) {
  5623. buf = section->section_body;
  5624. buf_end = buf + section->section_body_size;
  5625. switch (section->section_type) {
  5626. case SECTION_TYPE_USER:
  5627. /* unsupported user section, ignore it. */
  5628. if (!load_user_section(buf, buf_end, module,
  5629. reuse_const_strings, error_buf,
  5630. error_buf_size))
  5631. return false;
  5632. break;
  5633. case SECTION_TYPE_TYPE:
  5634. if (!load_type_section(buf, buf_end, module, error_buf,
  5635. error_buf_size))
  5636. return false;
  5637. break;
  5638. case SECTION_TYPE_IMPORT:
  5639. if (!load_import_section(buf, buf_end, module,
  5640. reuse_const_strings, no_resolve,
  5641. error_buf, error_buf_size))
  5642. return false;
  5643. break;
  5644. case SECTION_TYPE_FUNC:
  5645. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5646. module, error_buf, error_buf_size))
  5647. return false;
  5648. break;
  5649. case SECTION_TYPE_TABLE:
  5650. if (!load_table_section(buf, buf_end, module, error_buf,
  5651. error_buf_size))
  5652. return false;
  5653. break;
  5654. case SECTION_TYPE_MEMORY:
  5655. if (!load_memory_section(buf, buf_end, module, error_buf,
  5656. error_buf_size))
  5657. return false;
  5658. break;
  5659. #if WASM_ENABLE_TAGS != 0
  5660. case SECTION_TYPE_TAG:
  5661. /* load tag declaration section */
  5662. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5663. module, error_buf, error_buf_size))
  5664. return false;
  5665. break;
  5666. #endif
  5667. case SECTION_TYPE_GLOBAL:
  5668. if (!load_global_section(buf, buf_end, module, error_buf,
  5669. error_buf_size))
  5670. return false;
  5671. break;
  5672. case SECTION_TYPE_EXPORT:
  5673. if (!load_export_section(buf, buf_end, module,
  5674. reuse_const_strings, error_buf,
  5675. error_buf_size))
  5676. return false;
  5677. break;
  5678. case SECTION_TYPE_START:
  5679. if (!load_start_section(buf, buf_end, module, error_buf,
  5680. error_buf_size))
  5681. return false;
  5682. break;
  5683. case SECTION_TYPE_ELEM:
  5684. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5685. error_buf_size))
  5686. return false;
  5687. break;
  5688. case SECTION_TYPE_CODE:
  5689. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5690. module, error_buf, error_buf_size))
  5691. return false;
  5692. break;
  5693. case SECTION_TYPE_DATA:
  5694. if (!load_data_segment_section(buf, buf_end, module,
  5695. #if WASM_ENABLE_BULK_MEMORY != 0
  5696. has_datacount_section,
  5697. #endif
  5698. clone_data_seg, error_buf,
  5699. error_buf_size))
  5700. return false;
  5701. break;
  5702. #if WASM_ENABLE_BULK_MEMORY != 0
  5703. case SECTION_TYPE_DATACOUNT:
  5704. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5705. error_buf_size))
  5706. return false;
  5707. has_datacount_section = true;
  5708. break;
  5709. #endif
  5710. #if WASM_ENABLE_STRINGREF != 0
  5711. case SECTION_TYPE_STRINGREF:
  5712. if (!load_stringref_section(buf, buf_end, module,
  5713. reuse_const_strings, error_buf,
  5714. error_buf_size))
  5715. return false;
  5716. break;
  5717. #endif
  5718. default:
  5719. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5720. return false;
  5721. }
  5722. section = section->next;
  5723. }
  5724. #if WASM_ENABLE_BULK_MEMORY != 0
  5725. if (!check_data_count_consistency(
  5726. has_datacount_section, module->data_seg_count1,
  5727. module->data_seg_count, error_buf, error_buf_size)) {
  5728. return false;
  5729. }
  5730. #endif
  5731. module->aux_data_end_global_index = (uint32)-1;
  5732. module->aux_heap_base_global_index = (uint32)-1;
  5733. module->aux_stack_top_global_index = (uint32)-1;
  5734. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5735. export = module->exports;
  5736. for (i = 0; i < module->export_count; i++, export ++) {
  5737. if (export->kind == EXPORT_KIND_GLOBAL) {
  5738. if (!strcmp(export->name, "__heap_base")) {
  5739. if (export->index < module->import_global_count) {
  5740. LOG_DEBUG("Skip the process if __heap_base is imported "
  5741. "instead of being a local global");
  5742. continue;
  5743. }
  5744. /* only process linker-generated symbols */
  5745. global_index = export->index - module->import_global_count;
  5746. global = module->globals + global_index;
  5747. if (global->type.val_type == VALUE_TYPE_I32
  5748. && !global->type.is_mutable
  5749. && global->init_expr.init_expr_type
  5750. == INIT_EXPR_TYPE_I32_CONST) {
  5751. aux_heap_base_global = global;
  5752. aux_heap_base =
  5753. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5754. aux_heap_base_global_index = export->index;
  5755. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5756. aux_heap_base);
  5757. }
  5758. }
  5759. else if (!strcmp(export->name, "__data_end")) {
  5760. if (export->index < module->import_global_count) {
  5761. LOG_DEBUG("Skip the process if __data_end is imported "
  5762. "instead of being a local global");
  5763. continue;
  5764. }
  5765. /* only process linker-generated symbols */
  5766. global_index = export->index - module->import_global_count;
  5767. global = module->globals + global_index;
  5768. if (global->type.val_type == VALUE_TYPE_I32
  5769. && !global->type.is_mutable
  5770. && global->init_expr.init_expr_type
  5771. == INIT_EXPR_TYPE_I32_CONST) {
  5772. aux_data_end_global = global;
  5773. aux_data_end =
  5774. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5775. aux_data_end_global_index = export->index;
  5776. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5777. aux_data_end);
  5778. aux_data_end = align_uint64(aux_data_end, 16);
  5779. }
  5780. }
  5781. /* For module compiled with -pthread option, the global is:
  5782. [0] stack_top <-- 0
  5783. [1] tls_pointer
  5784. [2] tls_size
  5785. [3] data_end <-- 3
  5786. [4] global_base
  5787. [5] heap_base <-- 5
  5788. [6] dso_handle
  5789. For module compiled without -pthread option:
  5790. [0] stack_top <-- 0
  5791. [1] data_end <-- 1
  5792. [2] global_base
  5793. [3] heap_base <-- 3
  5794. [4] dso_handle
  5795. */
  5796. if (aux_data_end_global && aux_heap_base_global
  5797. && aux_data_end <= aux_heap_base) {
  5798. module->aux_data_end_global_index = aux_data_end_global_index;
  5799. module->aux_data_end = aux_data_end;
  5800. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5801. module->aux_heap_base = aux_heap_base;
  5802. /* Resolve aux stack top global */
  5803. for (global_index = 0; global_index < module->global_count;
  5804. global_index++) {
  5805. global = module->globals + global_index;
  5806. if (global->type.is_mutable /* heap_base and data_end is
  5807. not mutable */
  5808. && global->type.val_type == VALUE_TYPE_I32
  5809. && global->init_expr.init_expr_type
  5810. == INIT_EXPR_TYPE_I32_CONST
  5811. && (uint64)(uint32)global->init_expr.u.unary.v.i32
  5812. <= aux_heap_base) {
  5813. aux_stack_top_global = global;
  5814. aux_stack_top =
  5815. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5816. module->aux_stack_top_global_index =
  5817. module->import_global_count + global_index;
  5818. module->aux_stack_bottom = aux_stack_top;
  5819. module->aux_stack_size =
  5820. aux_stack_top > aux_data_end
  5821. ? (uint32)(aux_stack_top - aux_data_end)
  5822. : (uint32)aux_stack_top;
  5823. LOG_VERBOSE(
  5824. "Found aux stack top global, value: %" PRIu64 ", "
  5825. "global index: %d, stack size: %d",
  5826. aux_stack_top, global_index,
  5827. module->aux_stack_size);
  5828. break;
  5829. }
  5830. }
  5831. if (!aux_stack_top_global) {
  5832. /* Auxiliary stack global isn't found, it must be unused
  5833. in the wasm app, as if it is used, the global must be
  5834. defined. Here we set it to __heap_base global and set
  5835. its size to 0. */
  5836. aux_stack_top_global = aux_heap_base_global;
  5837. aux_stack_top = aux_heap_base;
  5838. module->aux_stack_top_global_index =
  5839. module->aux_heap_base_global_index;
  5840. module->aux_stack_bottom = aux_stack_top;
  5841. module->aux_stack_size = 0;
  5842. }
  5843. break;
  5844. }
  5845. }
  5846. }
  5847. module->malloc_function = (uint32)-1;
  5848. module->free_function = (uint32)-1;
  5849. module->retain_function = (uint32)-1;
  5850. /* Resolve malloc/free function exported by wasm module */
  5851. #if WASM_ENABLE_MEMORY64 != 0
  5852. if (has_module_memory64(module))
  5853. malloc_free_io_type = VALUE_TYPE_I64;
  5854. #endif
  5855. export = module->exports;
  5856. for (i = 0; i < module->export_count; i++, export ++) {
  5857. if (export->kind == EXPORT_KIND_FUNC) {
  5858. if (!strcmp(export->name, "malloc")
  5859. && export->index >= module->import_function_count) {
  5860. func_index = export->index - module->import_function_count;
  5861. func_type = module->functions[func_index]->func_type;
  5862. if (func_type->param_count == 1 && func_type->result_count == 1
  5863. && func_type->types[0] == malloc_free_io_type
  5864. && func_type->types[1] == malloc_free_io_type) {
  5865. bh_assert(module->malloc_function == (uint32)-1);
  5866. module->malloc_function = export->index;
  5867. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5868. export->name, export->index);
  5869. }
  5870. }
  5871. else if (!strcmp(export->name, "__new")
  5872. && export->index >= module->import_function_count) {
  5873. /* __new && __pin for AssemblyScript */
  5874. func_index = export->index - module->import_function_count;
  5875. func_type = module->functions[func_index]->func_type;
  5876. if (func_type->param_count == 2 && func_type->result_count == 1
  5877. && func_type->types[0] == malloc_free_io_type
  5878. && func_type->types[1] == VALUE_TYPE_I32
  5879. && func_type->types[2] == malloc_free_io_type) {
  5880. uint32 j;
  5881. WASMExport *export_tmp;
  5882. bh_assert(module->malloc_function == (uint32)-1);
  5883. module->malloc_function = export->index;
  5884. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5885. export->name, export->index);
  5886. /* resolve retain function.
  5887. If not found, reset malloc function index */
  5888. export_tmp = module->exports;
  5889. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5890. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5891. && (!strcmp(export_tmp->name, "__retain")
  5892. || (!strcmp(export_tmp->name, "__pin")))
  5893. && (export_tmp->index
  5894. >= module->import_function_count)) {
  5895. func_index = export_tmp->index
  5896. - module->import_function_count;
  5897. func_type =
  5898. module->functions[func_index]->func_type;
  5899. if (func_type->param_count == 1
  5900. && func_type->result_count == 1
  5901. && func_type->types[0] == malloc_free_io_type
  5902. && func_type->types[1] == malloc_free_io_type) {
  5903. bh_assert(module->retain_function
  5904. == (uint32)-1);
  5905. module->retain_function = export_tmp->index;
  5906. LOG_VERBOSE("Found retain function, name: %s, "
  5907. "index: %u",
  5908. export_tmp->name,
  5909. export_tmp->index);
  5910. break;
  5911. }
  5912. }
  5913. }
  5914. if (j == module->export_count) {
  5915. module->malloc_function = (uint32)-1;
  5916. LOG_VERBOSE("Can't find retain function,"
  5917. "reset malloc function index to -1");
  5918. }
  5919. }
  5920. }
  5921. else if (((!strcmp(export->name, "free"))
  5922. || (!strcmp(export->name, "__release"))
  5923. || (!strcmp(export->name, "__unpin")))
  5924. && export->index >= module->import_function_count) {
  5925. func_index = export->index - module->import_function_count;
  5926. func_type = module->functions[func_index]->func_type;
  5927. if (func_type->param_count == 1 && func_type->result_count == 0
  5928. && func_type->types[0] == malloc_free_io_type) {
  5929. bh_assert(module->free_function == (uint32)-1);
  5930. module->free_function = export->index;
  5931. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5932. export->name, export->index);
  5933. }
  5934. }
  5935. }
  5936. }
  5937. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5938. handle_table = wasm_interp_get_handle_table();
  5939. #endif
  5940. for (i = 0; i < module->function_count; i++) {
  5941. WASMFunction *func = module->functions[i];
  5942. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5943. error_buf_size)) {
  5944. return false;
  5945. }
  5946. if (i == module->function_count - 1
  5947. && func->code + func->code_size != buf_code_end) {
  5948. set_error_buf(error_buf, error_buf_size,
  5949. "code section size mismatch");
  5950. return false;
  5951. }
  5952. }
  5953. if (!module->possible_memory_grow) {
  5954. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5955. if (aux_data_end_global && aux_heap_base_global
  5956. && aux_stack_top_global) {
  5957. uint64 init_memory_size;
  5958. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5959. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5960. * valid range of uint32 */
  5961. if (shrunk_memory_size <= UINT32_MAX) {
  5962. if (module->import_memory_count) {
  5963. WASMMemoryImport *memory_import =
  5964. &module->import_memories[0].u.memory;
  5965. init_memory_size =
  5966. (uint64)memory_import->mem_type.num_bytes_per_page
  5967. * memory_import->mem_type.init_page_count;
  5968. if (shrunk_memory_size <= init_memory_size) {
  5969. /* Reset memory info to decrease memory usage */
  5970. memory_import->mem_type.num_bytes_per_page =
  5971. (uint32)shrunk_memory_size;
  5972. memory_import->mem_type.init_page_count = 1;
  5973. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5974. shrunk_memory_size);
  5975. }
  5976. }
  5977. if (module->memory_count) {
  5978. WASMMemory *memory = &module->memories[0];
  5979. init_memory_size = (uint64)memory->num_bytes_per_page
  5980. * memory->init_page_count;
  5981. if (shrunk_memory_size <= init_memory_size) {
  5982. /* Reset memory info to decrease memory usage */
  5983. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5984. memory->init_page_count = 1;
  5985. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5986. shrunk_memory_size);
  5987. }
  5988. }
  5989. }
  5990. }
  5991. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5992. #if WASM_ENABLE_MULTI_MODULE == 0
  5993. if (module->import_memory_count) {
  5994. WASMMemoryImport *memory_import =
  5995. &module->import_memories[0].u.memory;
  5996. /* Only resize the memory to one big page if num_bytes_per_page is
  5997. * in valid range of uint32 */
  5998. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5999. memory_import->mem_type.num_bytes_per_page *=
  6000. memory_import->mem_type.init_page_count;
  6001. if (memory_import->mem_type.init_page_count > 0)
  6002. memory_import->mem_type.init_page_count =
  6003. memory_import->mem_type.max_page_count = 1;
  6004. else
  6005. memory_import->mem_type.init_page_count =
  6006. memory_import->mem_type.max_page_count = 0;
  6007. }
  6008. }
  6009. if (module->memory_count) {
  6010. WASMMemory *memory = &module->memories[0];
  6011. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  6012. * valid range of uint32 */
  6013. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  6014. memory->num_bytes_per_page *= memory->init_page_count;
  6015. if (memory->init_page_count > 0)
  6016. memory->init_page_count = memory->max_page_count = 1;
  6017. else
  6018. memory->init_page_count = memory->max_page_count = 0;
  6019. }
  6020. }
  6021. #endif
  6022. }
  6023. #if WASM_ENABLE_MEMORY64 != 0
  6024. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  6025. false))
  6026. return false;
  6027. #endif
  6028. calculate_global_data_offset(module);
  6029. #if WASM_ENABLE_FAST_JIT != 0
  6030. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  6031. return false;
  6032. }
  6033. #endif
  6034. #if WASM_ENABLE_JIT != 0
  6035. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  6036. return false;
  6037. }
  6038. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  6039. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  6040. return false;
  6041. }
  6042. #else
  6043. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  6044. thread fast jit execution, since applying llvm optimizations in
  6045. aot_compile_wasm may cost a lot of time.
  6046. Create thread with enough native stack to apply llvm optimizations */
  6047. if (os_thread_create(&module->llvm_jit_init_thread,
  6048. init_llvm_jit_functions_stage2_callback,
  6049. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  6050. != 0) {
  6051. set_error_buf(error_buf, error_buf_size,
  6052. "create orcjit compile thread failed");
  6053. return false;
  6054. }
  6055. #endif
  6056. #endif
  6057. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6058. /* Create threads to compile the jit functions */
  6059. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  6060. return false;
  6061. }
  6062. #endif
  6063. #if WASM_ENABLE_MEMORY_TRACING != 0
  6064. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  6065. #endif
  6066. return true;
  6067. }
  6068. static WASMModule *
  6069. create_module(char *name, char *error_buf, uint32 error_buf_size)
  6070. {
  6071. WASMModule *module =
  6072. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  6073. bh_list_status ret;
  6074. if (!module) {
  6075. return NULL;
  6076. }
  6077. module->module_type = Wasm_Module_Bytecode;
  6078. /* Set start_function to -1, means no start function */
  6079. module->start_function = (uint32)-1;
  6080. module->name = name;
  6081. module->is_binary_freeable = false;
  6082. #if WASM_ENABLE_FAST_INTERP == 0
  6083. module->br_table_cache_list = &module->br_table_cache_list_head;
  6084. ret = bh_list_init(module->br_table_cache_list);
  6085. bh_assert(ret == BH_LIST_SUCCESS);
  6086. #endif
  6087. #if WASM_ENABLE_MULTI_MODULE != 0
  6088. module->import_module_list = &module->import_module_list_head;
  6089. ret = bh_list_init(module->import_module_list);
  6090. bh_assert(ret == BH_LIST_SUCCESS);
  6091. #endif
  6092. #if WASM_ENABLE_DEBUG_INTERP != 0
  6093. ret = bh_list_init(&module->fast_opcode_list);
  6094. bh_assert(ret == BH_LIST_SUCCESS);
  6095. #endif
  6096. #if WASM_ENABLE_GC != 0
  6097. if (!(module->ref_type_set =
  6098. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  6099. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  6100. goto fail1;
  6101. }
  6102. if (os_mutex_init(&module->rtt_type_lock)) {
  6103. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  6104. goto fail2;
  6105. }
  6106. #endif
  6107. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6108. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6109. && WASM_ENABLE_LAZY_JIT != 0)
  6110. if (os_mutex_init(&module->instance_list_lock) != 0) {
  6111. set_error_buf(error_buf, error_buf_size,
  6112. "init instance list lock failed");
  6113. goto fail3;
  6114. }
  6115. #endif
  6116. #if WASM_ENABLE_LIBC_WASI != 0
  6117. wasi_args_set_defaults(&module->wasi_args);
  6118. #endif /* WASM_ENABLE_LIBC_WASI != 0 */
  6119. (void)ret;
  6120. return module;
  6121. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6122. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  6123. && WASM_ENABLE_LAZY_JIT != 0)
  6124. fail3:
  6125. #endif
  6126. #if WASM_ENABLE_GC != 0
  6127. os_mutex_destroy(&module->rtt_type_lock);
  6128. fail2:
  6129. bh_hash_map_destroy(module->ref_type_set);
  6130. fail1:
  6131. #endif
  6132. wasm_runtime_free(module);
  6133. return NULL;
  6134. }
  6135. #if WASM_ENABLE_DEBUG_INTERP != 0
  6136. static bool
  6137. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  6138. uint32 error_buf_size)
  6139. {
  6140. WASMFastOPCodeNode *fast_op =
  6141. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  6142. if (fast_op) {
  6143. fast_op->offset = pos - module->load_addr;
  6144. fast_op->orig_op = orig_op;
  6145. bh_list_insert(&module->fast_opcode_list, fast_op);
  6146. }
  6147. return fast_op ? true : false;
  6148. }
  6149. #endif
  6150. WASMModule *
  6151. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  6152. uint32 error_buf_size)
  6153. {
  6154. WASMModule *module = create_module("", error_buf, error_buf_size);
  6155. if (!module)
  6156. return NULL;
  6157. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  6158. error_buf_size)) {
  6159. wasm_loader_unload(module);
  6160. return NULL;
  6161. }
  6162. LOG_VERBOSE("Load module from sections success.\n");
  6163. return module;
  6164. }
  6165. static void
  6166. destroy_sections(WASMSection *section_list)
  6167. {
  6168. WASMSection *section = section_list, *next;
  6169. while (section) {
  6170. next = section->next;
  6171. wasm_runtime_free(section);
  6172. section = next;
  6173. }
  6174. }
  6175. /* clang-format off */
  6176. static uint8 section_ids[] = {
  6177. SECTION_TYPE_USER,
  6178. SECTION_TYPE_TYPE,
  6179. SECTION_TYPE_IMPORT,
  6180. SECTION_TYPE_FUNC,
  6181. SECTION_TYPE_TABLE,
  6182. SECTION_TYPE_MEMORY,
  6183. #if WASM_ENABLE_TAGS != 0
  6184. SECTION_TYPE_TAG,
  6185. #endif
  6186. #if WASM_ENABLE_STRINGREF != 0
  6187. /* must immediately precede the global section,
  6188. or where the global section would be */
  6189. SECTION_TYPE_STRINGREF,
  6190. #endif
  6191. SECTION_TYPE_GLOBAL,
  6192. SECTION_TYPE_EXPORT,
  6193. SECTION_TYPE_START,
  6194. SECTION_TYPE_ELEM,
  6195. #if WASM_ENABLE_BULK_MEMORY != 0
  6196. SECTION_TYPE_DATACOUNT,
  6197. #endif
  6198. SECTION_TYPE_CODE,
  6199. SECTION_TYPE_DATA
  6200. };
  6201. /* clang-format on */
  6202. static uint8
  6203. get_section_index(uint8 section_type)
  6204. {
  6205. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  6206. for (uint8 i = 0; i < max_id; i++) {
  6207. if (section_type == section_ids[i])
  6208. return i;
  6209. }
  6210. return (uint8)-1;
  6211. }
  6212. static bool
  6213. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  6214. char *error_buf, uint32 error_buf_size)
  6215. {
  6216. WASMSection *section_list_end = NULL, *section;
  6217. const uint8 *p = buf, *p_end = buf + size;
  6218. uint8 section_type, section_index, last_section_index = (uint8)-1;
  6219. uint32 section_size;
  6220. bh_assert(!*p_section_list);
  6221. p += 8;
  6222. while (p < p_end) {
  6223. CHECK_BUF(p, p_end, 1);
  6224. section_type = read_uint8(p);
  6225. section_index = get_section_index(section_type);
  6226. if (section_index != (uint8)-1) {
  6227. if (section_type != SECTION_TYPE_USER) {
  6228. /* Custom sections may be inserted at any place,
  6229. while other sections must occur at most once
  6230. and in prescribed order. */
  6231. if (last_section_index != (uint8)-1
  6232. && (section_index <= last_section_index)) {
  6233. set_error_buf(error_buf, error_buf_size,
  6234. "unexpected content after last section or "
  6235. "junk after last section");
  6236. return false;
  6237. }
  6238. last_section_index = section_index;
  6239. }
  6240. read_leb_uint32(p, p_end, section_size);
  6241. CHECK_BUF1(p, p_end, section_size);
  6242. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  6243. error_buf_size))) {
  6244. return false;
  6245. }
  6246. section->section_type = section_type;
  6247. section->section_body = (uint8 *)p;
  6248. section->section_body_size = section_size;
  6249. if (!section_list_end)
  6250. *p_section_list = section_list_end = section;
  6251. else {
  6252. section_list_end->next = section;
  6253. section_list_end = section;
  6254. }
  6255. p += section_size;
  6256. }
  6257. else {
  6258. set_error_buf(error_buf, error_buf_size, "invalid section id");
  6259. return false;
  6260. }
  6261. }
  6262. return true;
  6263. fail:
  6264. return false;
  6265. }
  6266. static void
  6267. exchange32(uint8 *p_data)
  6268. {
  6269. uint8 value = *p_data;
  6270. *p_data = *(p_data + 3);
  6271. *(p_data + 3) = value;
  6272. value = *(p_data + 1);
  6273. *(p_data + 1) = *(p_data + 2);
  6274. *(p_data + 2) = value;
  6275. }
  6276. static union {
  6277. int a;
  6278. char b;
  6279. } __ue = { .a = 1 };
  6280. #define is_little_endian() (__ue.b == 1)
  6281. static bool
  6282. load(const uint8 *buf, uint32 size, WASMModule *module,
  6283. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  6284. uint32 error_buf_size)
  6285. {
  6286. const uint8 *buf_end = buf + size;
  6287. const uint8 *p = buf, *p_end = buf_end;
  6288. uint32 magic_number, version;
  6289. WASMSection *section_list = NULL;
  6290. CHECK_BUF1(p, p_end, sizeof(uint32));
  6291. magic_number = read_uint32(p);
  6292. if (!is_little_endian())
  6293. exchange32((uint8 *)&magic_number);
  6294. if (magic_number != WASM_MAGIC_NUMBER) {
  6295. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  6296. return false;
  6297. }
  6298. CHECK_BUF1(p, p_end, sizeof(uint32));
  6299. version = read_uint32(p);
  6300. if (!is_little_endian())
  6301. exchange32((uint8 *)&version);
  6302. if (version != WASM_CURRENT_VERSION) {
  6303. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  6304. return false;
  6305. }
  6306. module->package_version = version;
  6307. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  6308. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  6309. no_resolve, error_buf, error_buf_size)) {
  6310. destroy_sections(section_list);
  6311. return false;
  6312. }
  6313. destroy_sections(section_list);
  6314. return true;
  6315. fail:
  6316. return false;
  6317. }
  6318. #if WASM_ENABLE_LIBC_WASI != 0
  6319. /**
  6320. * refer to
  6321. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  6322. */
  6323. static bool
  6324. check_wasi_abi_compatibility(const WASMModule *module,
  6325. #if WASM_ENABLE_MULTI_MODULE != 0
  6326. bool main_module,
  6327. #endif
  6328. char *error_buf, uint32 error_buf_size)
  6329. {
  6330. /**
  6331. * be careful with:
  6332. * wasi compatible modules(command/reactor) which don't import any wasi
  6333. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  6334. * reactor can depend on nothing. At the same time, each has its own entry
  6335. * point.
  6336. *
  6337. * observations:
  6338. * - clang always injects `_start` into a command
  6339. * - clang always injects `_initialize` into a reactor
  6340. * - `iwasm -f` allows to run a function in the reactor
  6341. *
  6342. * strong assumptions:
  6343. * - no one will define either `_start` or `_initialize` on purpose
  6344. * - `_start` should always be `void _start(void)`
  6345. * - `_initialize` should always be `void _initialize(void)`
  6346. *
  6347. */
  6348. /* clang-format off */
  6349. /**
  6350. *
  6351. * | | import_wasi_api True | | import_wasi_api False | |
  6352. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  6353. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  6354. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  6355. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  6356. */
  6357. /* clang-format on */
  6358. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  6359. uint32 import_function_count = module->import_function_count;
  6360. WASMFuncType *func_type;
  6361. /* (func (export "_start") (...) */
  6362. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  6363. error_buf, error_buf_size);
  6364. if (start) {
  6365. if (start->index < import_function_count) {
  6366. set_error_buf(
  6367. error_buf, error_buf_size,
  6368. "the builtin _start function can not be an import function");
  6369. return false;
  6370. }
  6371. func_type =
  6372. module->functions[start->index - import_function_count]->func_type;
  6373. if (func_type->param_count || func_type->result_count) {
  6374. set_error_buf(error_buf, error_buf_size,
  6375. "the signature of builtin _start function is wrong");
  6376. return false;
  6377. }
  6378. }
  6379. else {
  6380. /* (func (export "_initialize") (...) */
  6381. initialize =
  6382. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  6383. error_buf, error_buf_size);
  6384. if (initialize) {
  6385. if (initialize->index < import_function_count) {
  6386. set_error_buf(error_buf, error_buf_size,
  6387. "the builtin _initialize function can not be an "
  6388. "import function");
  6389. return false;
  6390. }
  6391. func_type =
  6392. module->functions[initialize->index - import_function_count]
  6393. ->func_type;
  6394. if (func_type->param_count || func_type->result_count) {
  6395. set_error_buf(
  6396. error_buf, error_buf_size,
  6397. "the signature of builtin _initialize function is wrong");
  6398. return false;
  6399. }
  6400. }
  6401. }
  6402. /* filter out non-wasi compatible modules */
  6403. if (!module->import_wasi_api && !start && !initialize) {
  6404. return true;
  6405. }
  6406. /* should have one at least */
  6407. if (module->import_wasi_api && !start && !initialize) {
  6408. LOG_WARNING("warning: a module with WASI apis should be either "
  6409. "a command or a reactor");
  6410. }
  6411. /*
  6412. * there is at least one of `_start` and `_initialize` in below cases.
  6413. * according to the assumption, they should be all wasi compatible
  6414. */
  6415. #if WASM_ENABLE_MULTI_MODULE != 0
  6416. /* filter out commands (with `_start`) cases */
  6417. if (start && !main_module) {
  6418. set_error_buf(
  6419. error_buf, error_buf_size,
  6420. "a command (with _start function) can not be a sub-module");
  6421. return false;
  6422. }
  6423. #endif
  6424. /*
  6425. * it is ok a reactor acts as a main module,
  6426. * so skip the check about (with `_initialize`)
  6427. */
  6428. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6429. error_buf, error_buf_size);
  6430. if (!memory
  6431. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6432. /*
  6433. * with wasi-threads, it's still an open question if a memory
  6434. * should be exported.
  6435. *
  6436. * https://github.com/WebAssembly/wasi-threads/issues/22
  6437. * https://github.com/WebAssembly/WASI/issues/502
  6438. *
  6439. * Note: this code assumes the number of memories is at most 1.
  6440. */
  6441. && module->import_memory_count == 0
  6442. #endif
  6443. ) {
  6444. set_error_buf(error_buf, error_buf_size,
  6445. "a module with WASI apis must export memory by default");
  6446. return false;
  6447. }
  6448. return true;
  6449. }
  6450. #endif
  6451. WASMModule *
  6452. wasm_loader_load(uint8 *buf, uint32 size,
  6453. #if WASM_ENABLE_MULTI_MODULE != 0
  6454. bool main_module,
  6455. #endif
  6456. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6457. {
  6458. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6459. if (!module) {
  6460. return NULL;
  6461. }
  6462. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6463. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6464. module->load_addr = (uint8 *)buf;
  6465. module->load_size = size;
  6466. #endif
  6467. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6468. error_buf, error_buf_size)) {
  6469. goto fail;
  6470. }
  6471. #if WASM_ENABLE_LIBC_WASI != 0
  6472. /* Check the WASI application ABI */
  6473. if (!check_wasi_abi_compatibility(module,
  6474. #if WASM_ENABLE_MULTI_MODULE != 0
  6475. main_module,
  6476. #endif
  6477. error_buf, error_buf_size)) {
  6478. goto fail;
  6479. }
  6480. #endif
  6481. LOG_VERBOSE("Load module success.\n");
  6482. return module;
  6483. fail:
  6484. wasm_loader_unload(module);
  6485. return NULL;
  6486. }
  6487. void
  6488. wasm_loader_unload(WASMModule *module)
  6489. {
  6490. uint32 i;
  6491. if (!module)
  6492. return;
  6493. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6494. && WASM_ENABLE_LAZY_JIT != 0
  6495. module->orcjit_stop_compiling = true;
  6496. if (module->llvm_jit_init_thread)
  6497. os_thread_join(module->llvm_jit_init_thread, NULL);
  6498. #endif
  6499. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6500. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6501. module internal data after they were freed */
  6502. orcjit_stop_compile_threads(module);
  6503. #endif
  6504. #if WASM_ENABLE_JIT != 0
  6505. if (module->func_ptrs)
  6506. wasm_runtime_free(module->func_ptrs);
  6507. if (module->comp_ctx)
  6508. aot_destroy_comp_context(module->comp_ctx);
  6509. if (module->comp_data)
  6510. aot_destroy_comp_data(module->comp_data);
  6511. #endif
  6512. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6513. && WASM_ENABLE_LAZY_JIT != 0
  6514. if (module->tierup_wait_lock_inited) {
  6515. os_mutex_destroy(&module->tierup_wait_lock);
  6516. os_cond_destroy(&module->tierup_wait_cond);
  6517. }
  6518. #endif
  6519. if (module->imports)
  6520. wasm_runtime_free(module->imports);
  6521. if (module->functions) {
  6522. for (i = 0; i < module->function_count; i++) {
  6523. if (module->functions[i]) {
  6524. if (module->functions[i]->local_offsets)
  6525. wasm_runtime_free(module->functions[i]->local_offsets);
  6526. #if WASM_ENABLE_FAST_INTERP != 0
  6527. if (module->functions[i]->code_compiled)
  6528. wasm_runtime_free(module->functions[i]->code_compiled);
  6529. if (module->functions[i]->consts)
  6530. wasm_runtime_free(module->functions[i]->consts);
  6531. #endif
  6532. #if WASM_ENABLE_FAST_JIT != 0
  6533. if (module->functions[i]->fast_jit_jitted_code) {
  6534. jit_code_cache_free(
  6535. module->functions[i]->fast_jit_jitted_code);
  6536. }
  6537. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6538. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6539. jit_code_cache_free(
  6540. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6541. }
  6542. #endif
  6543. #endif
  6544. #if WASM_ENABLE_GC != 0
  6545. if (module->functions[i]->local_ref_type_maps) {
  6546. wasm_runtime_free(
  6547. module->functions[i]->local_ref_type_maps);
  6548. }
  6549. #endif
  6550. wasm_runtime_free(module->functions[i]);
  6551. }
  6552. }
  6553. wasm_runtime_free(module->functions);
  6554. }
  6555. if (module->tables) {
  6556. #if WASM_ENABLE_GC != 0
  6557. for (i = 0; i < module->table_count; i++) {
  6558. destroy_init_expr(module, &module->tables[i].init_expr);
  6559. }
  6560. #endif
  6561. wasm_runtime_free(module->tables);
  6562. }
  6563. if (module->memories)
  6564. wasm_runtime_free(module->memories);
  6565. if (module->globals) {
  6566. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6567. for (i = 0; i < module->global_count; i++) {
  6568. destroy_init_expr(module, &module->globals[i].init_expr);
  6569. }
  6570. #endif
  6571. wasm_runtime_free(module->globals);
  6572. }
  6573. #if WASM_ENABLE_TAGS != 0
  6574. if (module->tags) {
  6575. for (i = 0; i < module->tag_count; i++) {
  6576. if (module->tags[i])
  6577. wasm_runtime_free(module->tags[i]);
  6578. }
  6579. wasm_runtime_free(module->tags);
  6580. }
  6581. #endif
  6582. if (module->exports)
  6583. wasm_runtime_free(module->exports);
  6584. if (module->table_segments) {
  6585. for (i = 0; i < module->table_seg_count; i++) {
  6586. if (module->table_segments[i].init_values) {
  6587. #if WASM_ENABLE_GC != 0
  6588. uint32 j;
  6589. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6590. destroy_init_expr(
  6591. module, &module->table_segments[i].init_values[j]);
  6592. }
  6593. #endif
  6594. wasm_runtime_free(module->table_segments[i].init_values);
  6595. }
  6596. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6597. destroy_init_expr(module, &module->table_segments[i].base_offset);
  6598. #endif
  6599. }
  6600. wasm_runtime_free(module->table_segments);
  6601. }
  6602. if (module->data_segments) {
  6603. for (i = 0; i < module->data_seg_count; i++) {
  6604. if (module->data_segments[i]) {
  6605. if (module->data_segments[i]->is_data_cloned)
  6606. wasm_runtime_free(module->data_segments[i]->data);
  6607. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6608. destroy_init_expr(module,
  6609. &(module->data_segments[i]->base_offset));
  6610. #endif
  6611. wasm_runtime_free(module->data_segments[i]);
  6612. }
  6613. }
  6614. wasm_runtime_free(module->data_segments);
  6615. }
  6616. if (module->types) {
  6617. for (i = 0; i < module->type_count; i++) {
  6618. if (module->types[i])
  6619. destroy_wasm_type(module->types[i]);
  6620. }
  6621. wasm_runtime_free(module->types);
  6622. }
  6623. if (module->const_str_list) {
  6624. StringNode *node = module->const_str_list, *node_next;
  6625. while (node) {
  6626. node_next = node->next;
  6627. wasm_runtime_free(node);
  6628. node = node_next;
  6629. }
  6630. }
  6631. #if WASM_ENABLE_STRINGREF != 0
  6632. if (module->string_literal_ptrs) {
  6633. wasm_runtime_free((void *)module->string_literal_ptrs);
  6634. }
  6635. if (module->string_literal_lengths) {
  6636. wasm_runtime_free(module->string_literal_lengths);
  6637. }
  6638. #endif
  6639. #if WASM_ENABLE_FAST_INTERP == 0
  6640. if (module->br_table_cache_list) {
  6641. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6642. BrTableCache *node_next;
  6643. while (node) {
  6644. node_next = bh_list_elem_next(node);
  6645. wasm_runtime_free(node);
  6646. node = node_next;
  6647. }
  6648. }
  6649. #endif
  6650. #if WASM_ENABLE_MULTI_MODULE != 0
  6651. /* just release the sub module list */
  6652. if (module->import_module_list) {
  6653. WASMRegisteredModule *node =
  6654. bh_list_first_elem(module->import_module_list);
  6655. while (node) {
  6656. WASMRegisteredModule *next = bh_list_elem_next(node);
  6657. bh_list_remove(module->import_module_list, node);
  6658. /*
  6659. * unload(sub_module) will be triggered during runtime_destroy().
  6660. * every module in the global module list will be unloaded one by
  6661. * one. so don't worry.
  6662. */
  6663. wasm_runtime_free(node);
  6664. /*
  6665. * the module file reading buffer will be released
  6666. * in runtime_destroy()
  6667. */
  6668. node = next;
  6669. }
  6670. }
  6671. #endif
  6672. #if WASM_ENABLE_DEBUG_INTERP != 0
  6673. WASMFastOPCodeNode *fast_opcode =
  6674. bh_list_first_elem(&module->fast_opcode_list);
  6675. while (fast_opcode) {
  6676. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6677. wasm_runtime_free(fast_opcode);
  6678. fast_opcode = next;
  6679. }
  6680. #endif
  6681. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6682. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6683. && WASM_ENABLE_LAZY_JIT != 0)
  6684. os_mutex_destroy(&module->instance_list_lock);
  6685. #endif
  6686. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6687. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6688. #endif
  6689. #if WASM_ENABLE_FAST_JIT != 0
  6690. if (module->fast_jit_func_ptrs) {
  6691. wasm_runtime_free(module->fast_jit_func_ptrs);
  6692. }
  6693. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6694. if (module->fast_jit_thread_locks_inited[i]) {
  6695. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6696. }
  6697. }
  6698. #endif
  6699. #if WASM_ENABLE_GC != 0
  6700. os_mutex_destroy(&module->rtt_type_lock);
  6701. bh_hash_map_destroy(module->ref_type_set);
  6702. if (module->rtt_types) {
  6703. for (i = 0; i < module->type_count; i++) {
  6704. if (module->rtt_types[i])
  6705. wasm_runtime_free(module->rtt_types[i]);
  6706. }
  6707. wasm_runtime_free(module->rtt_types);
  6708. }
  6709. #if WASM_ENABLE_STRINGREF != 0
  6710. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6711. if (module->stringref_rtts[i])
  6712. wasm_runtime_free(module->stringref_rtts[i]);
  6713. }
  6714. #endif
  6715. #endif
  6716. #if WASM_ENABLE_BRANCH_HINTS != 0
  6717. for (size_t i = 0; i < module->function_count; i++) {
  6718. // be carefull when adding more hints. This only works as long as
  6719. // the hint structs have been allocated all at once as an array.
  6720. // With only branch-hints at the moment, this is the case.
  6721. if (module->function_hints != NULL && module->function_hints[i] != NULL)
  6722. wasm_runtime_free(module->function_hints[i]);
  6723. }
  6724. if (module->function_hints != NULL)
  6725. wasm_runtime_free(module->function_hints);
  6726. #endif
  6727. wasm_runtime_free(module);
  6728. }
  6729. bool
  6730. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6731. const uint8 *start_addr, const uint8 *code_end_addr,
  6732. uint8 label_type, uint8 **p_else_addr,
  6733. uint8 **p_end_addr)
  6734. {
  6735. const uint8 *p = start_addr, *p_end = code_end_addr;
  6736. uint8 *else_addr = NULL;
  6737. char error_buf[128];
  6738. uint32 block_nested_depth = 1, count, i, j, t;
  6739. uint32 error_buf_size = sizeof(error_buf);
  6740. uint8 opcode, u8;
  6741. BlockAddr block_stack[16] = { { 0 } }, *block;
  6742. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6743. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6744. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6745. if (block[j].start_addr == start_addr) {
  6746. /* Cache hit */
  6747. *p_else_addr = block[j].else_addr;
  6748. *p_end_addr = block[j].end_addr;
  6749. return true;
  6750. }
  6751. }
  6752. /* Cache unhit */
  6753. block_stack[0].start_addr = start_addr;
  6754. while (p < code_end_addr) {
  6755. opcode = *p++;
  6756. #if WASM_ENABLE_DEBUG_INTERP != 0
  6757. op_break_retry:
  6758. #endif
  6759. switch (opcode) {
  6760. case WASM_OP_UNREACHABLE:
  6761. case WASM_OP_NOP:
  6762. break;
  6763. #if WASM_ENABLE_EXCE_HANDLING != 0
  6764. case WASM_OP_TRY:
  6765. u8 = read_uint8(p);
  6766. if (block_nested_depth
  6767. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6768. block_stack[block_nested_depth].start_addr = p;
  6769. block_stack[block_nested_depth].else_addr = NULL;
  6770. }
  6771. block_nested_depth++;
  6772. break;
  6773. case EXT_OP_TRY:
  6774. skip_leb_uint32(p, p_end);
  6775. if (block_nested_depth
  6776. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6777. block_stack[block_nested_depth].start_addr = p;
  6778. block_stack[block_nested_depth].else_addr = NULL;
  6779. }
  6780. block_nested_depth++;
  6781. break;
  6782. case WASM_OP_CATCH:
  6783. if (block_nested_depth == 1) {
  6784. *p_end_addr = (uint8 *)(p - 1);
  6785. /* stop search and return the address of the catch block */
  6786. return true;
  6787. }
  6788. break;
  6789. case WASM_OP_CATCH_ALL:
  6790. if (block_nested_depth == 1) {
  6791. *p_end_addr = (uint8 *)(p - 1);
  6792. /* stop search and return the address of the catch_all block
  6793. */
  6794. return true;
  6795. }
  6796. break;
  6797. case WASM_OP_THROW:
  6798. /* skip tag_index */
  6799. skip_leb(p);
  6800. break;
  6801. case WASM_OP_RETHROW:
  6802. /* skip depth */
  6803. skip_leb(p);
  6804. break;
  6805. case WASM_OP_DELEGATE:
  6806. if (block_nested_depth == 1) {
  6807. *p_end_addr = (uint8 *)(p - 1);
  6808. return true;
  6809. }
  6810. else {
  6811. skip_leb(p);
  6812. /* the DELEGATE opcode ends the tryblock, */
  6813. block_nested_depth--;
  6814. if (block_nested_depth
  6815. < sizeof(block_stack) / sizeof(BlockAddr))
  6816. block_stack[block_nested_depth].end_addr =
  6817. (uint8 *)(p - 1);
  6818. }
  6819. break;
  6820. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6821. case WASM_OP_BLOCK:
  6822. case WASM_OP_LOOP:
  6823. case WASM_OP_IF:
  6824. {
  6825. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6826. u8 = read_uint8(p);
  6827. if (is_byte_a_type(u8)) {
  6828. #if WASM_ENABLE_GC != 0
  6829. if (wasm_is_type_multi_byte_type(u8)) {
  6830. /* the possible extra bytes of GC ref type have been
  6831. modified to OP_NOP, no need to resolve them again */
  6832. }
  6833. #endif
  6834. }
  6835. else {
  6836. p--;
  6837. /* block type */
  6838. skip_leb_int32(p, p_end);
  6839. }
  6840. if (block_nested_depth
  6841. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6842. block_stack[block_nested_depth].start_addr = p;
  6843. block_stack[block_nested_depth].else_addr = NULL;
  6844. }
  6845. block_nested_depth++;
  6846. break;
  6847. }
  6848. case EXT_OP_BLOCK:
  6849. case EXT_OP_LOOP:
  6850. case EXT_OP_IF:
  6851. /* block type */
  6852. skip_leb_int32(p, p_end);
  6853. if (block_nested_depth
  6854. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6855. block_stack[block_nested_depth].start_addr = p;
  6856. block_stack[block_nested_depth].else_addr = NULL;
  6857. }
  6858. block_nested_depth++;
  6859. break;
  6860. case WASM_OP_ELSE:
  6861. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6862. else_addr = (uint8 *)(p - 1);
  6863. if (block_nested_depth - 1
  6864. < sizeof(block_stack) / sizeof(BlockAddr))
  6865. block_stack[block_nested_depth - 1].else_addr =
  6866. (uint8 *)(p - 1);
  6867. break;
  6868. case WASM_OP_END:
  6869. if (block_nested_depth == 1) {
  6870. if (label_type == LABEL_TYPE_IF)
  6871. *p_else_addr = else_addr;
  6872. *p_end_addr = (uint8 *)(p - 1);
  6873. block_stack[0].end_addr = (uint8 *)(p - 1);
  6874. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6875. t++) {
  6876. start_addr = block_stack[t].start_addr;
  6877. if (start_addr) {
  6878. i = ((uintptr_t)start_addr)
  6879. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6880. block =
  6881. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6882. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6883. if (!block[j].start_addr)
  6884. break;
  6885. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6886. memmove(block + 1, block,
  6887. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6888. * sizeof(BlockAddr));
  6889. j = 0;
  6890. }
  6891. block[j].start_addr = block_stack[t].start_addr;
  6892. block[j].else_addr = block_stack[t].else_addr;
  6893. block[j].end_addr = block_stack[t].end_addr;
  6894. }
  6895. else
  6896. break;
  6897. }
  6898. return true;
  6899. }
  6900. else {
  6901. block_nested_depth--;
  6902. if (block_nested_depth
  6903. < sizeof(block_stack) / sizeof(BlockAddr))
  6904. block_stack[block_nested_depth].end_addr =
  6905. (uint8 *)(p - 1);
  6906. }
  6907. break;
  6908. case WASM_OP_BR:
  6909. case WASM_OP_BR_IF:
  6910. skip_leb_uint32(p, p_end); /* labelidx */
  6911. break;
  6912. case WASM_OP_BR_TABLE:
  6913. read_leb_uint32(p, p_end, count); /* label num */
  6914. #if WASM_ENABLE_FAST_INTERP != 0
  6915. for (i = 0; i <= count; i++) /* labelidxs */
  6916. skip_leb_uint32(p, p_end);
  6917. #else
  6918. p += count + 1;
  6919. while (*p == WASM_OP_NOP)
  6920. p++;
  6921. #endif
  6922. break;
  6923. #if WASM_ENABLE_FAST_INTERP == 0
  6924. case EXT_OP_BR_TABLE_CACHE:
  6925. read_leb_uint32(p, p_end, count); /* label num */
  6926. while (*p == WASM_OP_NOP)
  6927. p++;
  6928. break;
  6929. #endif
  6930. case WASM_OP_RETURN:
  6931. break;
  6932. case WASM_OP_CALL:
  6933. #if WASM_ENABLE_TAIL_CALL != 0
  6934. case WASM_OP_RETURN_CALL:
  6935. #endif
  6936. skip_leb_uint32(p, p_end); /* funcidx */
  6937. break;
  6938. case WASM_OP_CALL_INDIRECT:
  6939. #if WASM_ENABLE_TAIL_CALL != 0
  6940. case WASM_OP_RETURN_CALL_INDIRECT:
  6941. #endif
  6942. skip_leb_uint32(p, p_end); /* typeidx */
  6943. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6944. skip_leb_uint32(p, p_end); /* tableidx */
  6945. #else
  6946. u8 = read_uint8(p); /* 0x00 */
  6947. #endif
  6948. break;
  6949. #if WASM_ENABLE_GC != 0
  6950. case WASM_OP_CALL_REF:
  6951. case WASM_OP_RETURN_CALL_REF:
  6952. skip_leb_uint32(p, p_end); /* typeidx */
  6953. break;
  6954. #endif
  6955. case WASM_OP_DROP:
  6956. case WASM_OP_SELECT:
  6957. case WASM_OP_DROP_64:
  6958. case WASM_OP_SELECT_64:
  6959. #if WASM_ENABLE_SIMDE != 0
  6960. case WASM_OP_SELECT_128:
  6961. #endif
  6962. break;
  6963. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6964. case WASM_OP_SELECT_T:
  6965. {
  6966. skip_leb_uint32(p, p_end); /* vec length */
  6967. u8 = read_uint8(p); /* typeidx */
  6968. /* the possible extra bytes of GC ref type have been
  6969. modified to OP_NOP, no need to resolve them again */
  6970. break;
  6971. }
  6972. case WASM_OP_TABLE_GET:
  6973. case WASM_OP_TABLE_SET:
  6974. skip_leb_uint32(p, p_end); /* table index */
  6975. break;
  6976. case WASM_OP_REF_NULL:
  6977. {
  6978. u8 = read_uint8(p); /* type */
  6979. if (is_byte_a_type(u8)) {
  6980. #if WASM_ENABLE_GC != 0
  6981. if (wasm_is_type_multi_byte_type(u8)) {
  6982. /* the possible extra bytes of GC ref type have been
  6983. modified to OP_NOP, no need to resolve them again */
  6984. }
  6985. #endif
  6986. }
  6987. else {
  6988. p--;
  6989. skip_leb_uint32(p, p_end);
  6990. }
  6991. break;
  6992. }
  6993. case WASM_OP_REF_IS_NULL:
  6994. break;
  6995. case WASM_OP_REF_FUNC:
  6996. skip_leb_uint32(p, p_end); /* func index */
  6997. break;
  6998. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6999. #if WASM_ENABLE_GC != 0
  7000. case WASM_OP_REF_AS_NON_NULL:
  7001. case WASM_OP_REF_EQ:
  7002. break;
  7003. case WASM_OP_BR_ON_NULL:
  7004. case WASM_OP_BR_ON_NON_NULL:
  7005. skip_leb_uint32(p, p_end); /* label index */
  7006. break;
  7007. #endif /* end of WASM_ENABLE_GC != 0 */
  7008. case WASM_OP_GET_LOCAL:
  7009. case WASM_OP_SET_LOCAL:
  7010. case WASM_OP_TEE_LOCAL:
  7011. case WASM_OP_GET_GLOBAL:
  7012. case WASM_OP_SET_GLOBAL:
  7013. case WASM_OP_GET_GLOBAL_64:
  7014. case WASM_OP_SET_GLOBAL_64:
  7015. #if WASM_ENABLE_SIMDE != 0
  7016. case WASM_OP_GET_GLOBAL_V128:
  7017. case WASM_OP_SET_GLOBAL_V128:
  7018. #endif
  7019. case WASM_OP_SET_GLOBAL_AUX_STACK:
  7020. skip_leb_uint32(p, p_end); /* local index */
  7021. break;
  7022. case EXT_OP_GET_LOCAL_FAST:
  7023. case EXT_OP_SET_LOCAL_FAST:
  7024. case EXT_OP_TEE_LOCAL_FAST:
  7025. CHECK_BUF(p, p_end, 1);
  7026. p++;
  7027. break;
  7028. case WASM_OP_I32_LOAD:
  7029. case WASM_OP_I64_LOAD:
  7030. case WASM_OP_F32_LOAD:
  7031. case WASM_OP_F64_LOAD:
  7032. case WASM_OP_I32_LOAD8_S:
  7033. case WASM_OP_I32_LOAD8_U:
  7034. case WASM_OP_I32_LOAD16_S:
  7035. case WASM_OP_I32_LOAD16_U:
  7036. case WASM_OP_I64_LOAD8_S:
  7037. case WASM_OP_I64_LOAD8_U:
  7038. case WASM_OP_I64_LOAD16_S:
  7039. case WASM_OP_I64_LOAD16_U:
  7040. case WASM_OP_I64_LOAD32_S:
  7041. case WASM_OP_I64_LOAD32_U:
  7042. case WASM_OP_I32_STORE:
  7043. case WASM_OP_I64_STORE:
  7044. case WASM_OP_F32_STORE:
  7045. case WASM_OP_F64_STORE:
  7046. case WASM_OP_I32_STORE8:
  7047. case WASM_OP_I32_STORE16:
  7048. case WASM_OP_I64_STORE8:
  7049. case WASM_OP_I64_STORE16:
  7050. case WASM_OP_I64_STORE32:
  7051. skip_leb_align(p, p_end); /* align */
  7052. skip_leb_mem_offset(p, p_end); /* offset */
  7053. break;
  7054. case WASM_OP_MEMORY_SIZE:
  7055. case WASM_OP_MEMORY_GROW:
  7056. skip_leb_memidx(p, p_end); /* memidx */
  7057. break;
  7058. case WASM_OP_I32_CONST:
  7059. skip_leb_int32(p, p_end);
  7060. break;
  7061. case WASM_OP_I64_CONST:
  7062. skip_leb_int64(p, p_end);
  7063. break;
  7064. case WASM_OP_F32_CONST:
  7065. p += sizeof(float32);
  7066. break;
  7067. case WASM_OP_F64_CONST:
  7068. p += sizeof(float64);
  7069. break;
  7070. case WASM_OP_I32_EQZ:
  7071. case WASM_OP_I32_EQ:
  7072. case WASM_OP_I32_NE:
  7073. case WASM_OP_I32_LT_S:
  7074. case WASM_OP_I32_LT_U:
  7075. case WASM_OP_I32_GT_S:
  7076. case WASM_OP_I32_GT_U:
  7077. case WASM_OP_I32_LE_S:
  7078. case WASM_OP_I32_LE_U:
  7079. case WASM_OP_I32_GE_S:
  7080. case WASM_OP_I32_GE_U:
  7081. case WASM_OP_I64_EQZ:
  7082. case WASM_OP_I64_EQ:
  7083. case WASM_OP_I64_NE:
  7084. case WASM_OP_I64_LT_S:
  7085. case WASM_OP_I64_LT_U:
  7086. case WASM_OP_I64_GT_S:
  7087. case WASM_OP_I64_GT_U:
  7088. case WASM_OP_I64_LE_S:
  7089. case WASM_OP_I64_LE_U:
  7090. case WASM_OP_I64_GE_S:
  7091. case WASM_OP_I64_GE_U:
  7092. case WASM_OP_F32_EQ:
  7093. case WASM_OP_F32_NE:
  7094. case WASM_OP_F32_LT:
  7095. case WASM_OP_F32_GT:
  7096. case WASM_OP_F32_LE:
  7097. case WASM_OP_F32_GE:
  7098. case WASM_OP_F64_EQ:
  7099. case WASM_OP_F64_NE:
  7100. case WASM_OP_F64_LT:
  7101. case WASM_OP_F64_GT:
  7102. case WASM_OP_F64_LE:
  7103. case WASM_OP_F64_GE:
  7104. case WASM_OP_I32_CLZ:
  7105. case WASM_OP_I32_CTZ:
  7106. case WASM_OP_I32_POPCNT:
  7107. case WASM_OP_I32_ADD:
  7108. case WASM_OP_I32_SUB:
  7109. case WASM_OP_I32_MUL:
  7110. case WASM_OP_I32_DIV_S:
  7111. case WASM_OP_I32_DIV_U:
  7112. case WASM_OP_I32_REM_S:
  7113. case WASM_OP_I32_REM_U:
  7114. case WASM_OP_I32_AND:
  7115. case WASM_OP_I32_OR:
  7116. case WASM_OP_I32_XOR:
  7117. case WASM_OP_I32_SHL:
  7118. case WASM_OP_I32_SHR_S:
  7119. case WASM_OP_I32_SHR_U:
  7120. case WASM_OP_I32_ROTL:
  7121. case WASM_OP_I32_ROTR:
  7122. case WASM_OP_I64_CLZ:
  7123. case WASM_OP_I64_CTZ:
  7124. case WASM_OP_I64_POPCNT:
  7125. case WASM_OP_I64_ADD:
  7126. case WASM_OP_I64_SUB:
  7127. case WASM_OP_I64_MUL:
  7128. case WASM_OP_I64_DIV_S:
  7129. case WASM_OP_I64_DIV_U:
  7130. case WASM_OP_I64_REM_S:
  7131. case WASM_OP_I64_REM_U:
  7132. case WASM_OP_I64_AND:
  7133. case WASM_OP_I64_OR:
  7134. case WASM_OP_I64_XOR:
  7135. case WASM_OP_I64_SHL:
  7136. case WASM_OP_I64_SHR_S:
  7137. case WASM_OP_I64_SHR_U:
  7138. case WASM_OP_I64_ROTL:
  7139. case WASM_OP_I64_ROTR:
  7140. case WASM_OP_F32_ABS:
  7141. case WASM_OP_F32_NEG:
  7142. case WASM_OP_F32_CEIL:
  7143. case WASM_OP_F32_FLOOR:
  7144. case WASM_OP_F32_TRUNC:
  7145. case WASM_OP_F32_NEAREST:
  7146. case WASM_OP_F32_SQRT:
  7147. case WASM_OP_F32_ADD:
  7148. case WASM_OP_F32_SUB:
  7149. case WASM_OP_F32_MUL:
  7150. case WASM_OP_F32_DIV:
  7151. case WASM_OP_F32_MIN:
  7152. case WASM_OP_F32_MAX:
  7153. case WASM_OP_F32_COPYSIGN:
  7154. case WASM_OP_F64_ABS:
  7155. case WASM_OP_F64_NEG:
  7156. case WASM_OP_F64_CEIL:
  7157. case WASM_OP_F64_FLOOR:
  7158. case WASM_OP_F64_TRUNC:
  7159. case WASM_OP_F64_NEAREST:
  7160. case WASM_OP_F64_SQRT:
  7161. case WASM_OP_F64_ADD:
  7162. case WASM_OP_F64_SUB:
  7163. case WASM_OP_F64_MUL:
  7164. case WASM_OP_F64_DIV:
  7165. case WASM_OP_F64_MIN:
  7166. case WASM_OP_F64_MAX:
  7167. case WASM_OP_F64_COPYSIGN:
  7168. case WASM_OP_I32_WRAP_I64:
  7169. case WASM_OP_I32_TRUNC_S_F32:
  7170. case WASM_OP_I32_TRUNC_U_F32:
  7171. case WASM_OP_I32_TRUNC_S_F64:
  7172. case WASM_OP_I32_TRUNC_U_F64:
  7173. case WASM_OP_I64_EXTEND_S_I32:
  7174. case WASM_OP_I64_EXTEND_U_I32:
  7175. case WASM_OP_I64_TRUNC_S_F32:
  7176. case WASM_OP_I64_TRUNC_U_F32:
  7177. case WASM_OP_I64_TRUNC_S_F64:
  7178. case WASM_OP_I64_TRUNC_U_F64:
  7179. case WASM_OP_F32_CONVERT_S_I32:
  7180. case WASM_OP_F32_CONVERT_U_I32:
  7181. case WASM_OP_F32_CONVERT_S_I64:
  7182. case WASM_OP_F32_CONVERT_U_I64:
  7183. case WASM_OP_F32_DEMOTE_F64:
  7184. case WASM_OP_F64_CONVERT_S_I32:
  7185. case WASM_OP_F64_CONVERT_U_I32:
  7186. case WASM_OP_F64_CONVERT_S_I64:
  7187. case WASM_OP_F64_CONVERT_U_I64:
  7188. case WASM_OP_F64_PROMOTE_F32:
  7189. case WASM_OP_I32_REINTERPRET_F32:
  7190. case WASM_OP_I64_REINTERPRET_F64:
  7191. case WASM_OP_F32_REINTERPRET_I32:
  7192. case WASM_OP_F64_REINTERPRET_I64:
  7193. case WASM_OP_I32_EXTEND8_S:
  7194. case WASM_OP_I32_EXTEND16_S:
  7195. case WASM_OP_I64_EXTEND8_S:
  7196. case WASM_OP_I64_EXTEND16_S:
  7197. case WASM_OP_I64_EXTEND32_S:
  7198. break;
  7199. #if WASM_ENABLE_GC != 0
  7200. case WASM_OP_GC_PREFIX:
  7201. {
  7202. uint32 opcode1;
  7203. read_leb_uint32(p, p_end, opcode1);
  7204. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7205. is no larger than UINT8_MAX */
  7206. opcode = (uint8)opcode1;
  7207. switch (opcode) {
  7208. case WASM_OP_STRUCT_NEW:
  7209. case WASM_OP_STRUCT_NEW_DEFAULT:
  7210. skip_leb_uint32(p, p_end); /* typeidx */
  7211. break;
  7212. case WASM_OP_STRUCT_GET:
  7213. case WASM_OP_STRUCT_GET_S:
  7214. case WASM_OP_STRUCT_GET_U:
  7215. case WASM_OP_STRUCT_SET:
  7216. skip_leb_uint32(p, p_end); /* typeidx */
  7217. skip_leb_uint32(p, p_end); /* fieldidx */
  7218. break;
  7219. case WASM_OP_ARRAY_NEW:
  7220. case WASM_OP_ARRAY_NEW_DEFAULT:
  7221. case WASM_OP_ARRAY_GET:
  7222. case WASM_OP_ARRAY_GET_S:
  7223. case WASM_OP_ARRAY_GET_U:
  7224. case WASM_OP_ARRAY_SET:
  7225. case WASM_OP_ARRAY_FILL:
  7226. skip_leb_uint32(p, p_end); /* typeidx */
  7227. break;
  7228. case WASM_OP_ARRAY_COPY:
  7229. skip_leb_uint32(p, p_end); /* typeidx1 */
  7230. skip_leb_uint32(p, p_end); /* typeidx2 */
  7231. break;
  7232. case WASM_OP_ARRAY_LEN:
  7233. break;
  7234. case WASM_OP_ARRAY_NEW_FIXED:
  7235. case WASM_OP_ARRAY_NEW_DATA:
  7236. case WASM_OP_ARRAY_NEW_ELEM:
  7237. skip_leb_uint32(p, p_end); /* typeidx */
  7238. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  7239. break;
  7240. case WASM_OP_REF_I31:
  7241. case WASM_OP_I31_GET_S:
  7242. case WASM_OP_I31_GET_U:
  7243. break;
  7244. case WASM_OP_REF_TEST:
  7245. case WASM_OP_REF_CAST:
  7246. case WASM_OP_REF_TEST_NULLABLE:
  7247. case WASM_OP_REF_CAST_NULLABLE:
  7248. skip_leb_int32(p, p_end); /* heaptype */
  7249. break;
  7250. case WASM_OP_BR_ON_CAST:
  7251. case WASM_OP_BR_ON_CAST_FAIL:
  7252. p += sizeof(uint8); /* castflag */
  7253. skip_leb_uint32(p, p_end); /* labelidx */
  7254. skip_leb_int32(p, p_end); /* heaptype */
  7255. skip_leb_int32(p, p_end); /* heaptype2 */
  7256. break;
  7257. case WASM_OP_ANY_CONVERT_EXTERN:
  7258. case WASM_OP_EXTERN_CONVERT_ANY:
  7259. break;
  7260. #if WASM_ENABLE_STRINGREF != 0
  7261. case WASM_OP_STRING_NEW_UTF8:
  7262. case WASM_OP_STRING_NEW_WTF16:
  7263. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  7264. case WASM_OP_STRING_NEW_WTF8:
  7265. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7266. break;
  7267. case WASM_OP_STRING_CONST:
  7268. skip_leb_int32(p, p_end); /* contents */
  7269. break;
  7270. case WASM_OP_STRING_MEASURE_UTF8:
  7271. case WASM_OP_STRING_MEASURE_WTF8:
  7272. case WASM_OP_STRING_MEASURE_WTF16:
  7273. break;
  7274. case WASM_OP_STRING_ENCODE_UTF8:
  7275. case WASM_OP_STRING_ENCODE_WTF16:
  7276. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  7277. case WASM_OP_STRING_ENCODE_WTF8:
  7278. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7279. break;
  7280. case WASM_OP_STRING_CONCAT:
  7281. case WASM_OP_STRING_EQ:
  7282. case WASM_OP_STRING_IS_USV_SEQUENCE:
  7283. case WASM_OP_STRING_AS_WTF8:
  7284. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  7285. break;
  7286. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  7287. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  7288. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  7289. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7290. break;
  7291. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  7292. case WASM_OP_STRING_AS_WTF16:
  7293. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  7294. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  7295. break;
  7296. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  7297. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7298. break;
  7299. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  7300. case WASM_OP_STRING_AS_ITER:
  7301. case WASM_OP_STRINGVIEW_ITER_NEXT:
  7302. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  7303. case WASM_OP_STRINGVIEW_ITER_REWIND:
  7304. case WASM_OP_STRINGVIEW_ITER_SLICE:
  7305. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  7306. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  7307. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  7308. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  7309. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  7310. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  7311. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  7312. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  7313. break;
  7314. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  7315. default:
  7316. return false;
  7317. }
  7318. break;
  7319. }
  7320. #endif /* end of WASM_ENABLE_GC != 0 */
  7321. case WASM_OP_MISC_PREFIX:
  7322. {
  7323. uint32 opcode1;
  7324. read_leb_uint32(p, p_end, opcode1);
  7325. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7326. is no larger than UINT8_MAX */
  7327. opcode = (uint8)opcode1;
  7328. switch (opcode) {
  7329. case WASM_OP_I32_TRUNC_SAT_S_F32:
  7330. case WASM_OP_I32_TRUNC_SAT_U_F32:
  7331. case WASM_OP_I32_TRUNC_SAT_S_F64:
  7332. case WASM_OP_I32_TRUNC_SAT_U_F64:
  7333. case WASM_OP_I64_TRUNC_SAT_S_F32:
  7334. case WASM_OP_I64_TRUNC_SAT_U_F32:
  7335. case WASM_OP_I64_TRUNC_SAT_S_F64:
  7336. case WASM_OP_I64_TRUNC_SAT_U_F64:
  7337. break;
  7338. #if WASM_ENABLE_BULK_MEMORY != 0
  7339. case WASM_OP_MEMORY_INIT:
  7340. skip_leb_uint32(p, p_end);
  7341. skip_leb_memidx(p, p_end);
  7342. break;
  7343. case WASM_OP_DATA_DROP:
  7344. skip_leb_uint32(p, p_end);
  7345. break;
  7346. case WASM_OP_MEMORY_COPY:
  7347. skip_leb_memidx(p, p_end);
  7348. skip_leb_memidx(p, p_end);
  7349. break;
  7350. case WASM_OP_MEMORY_FILL:
  7351. skip_leb_memidx(p, p_end);
  7352. break;
  7353. #endif /* WASM_ENABLE_BULK_MEMORY */
  7354. #if WASM_ENABLE_REF_TYPES != 0
  7355. case WASM_OP_TABLE_INIT:
  7356. case WASM_OP_TABLE_COPY:
  7357. /* tableidx */
  7358. skip_leb_uint32(p, p_end);
  7359. /* elemidx */
  7360. skip_leb_uint32(p, p_end);
  7361. break;
  7362. case WASM_OP_ELEM_DROP:
  7363. /* elemidx */
  7364. skip_leb_uint32(p, p_end);
  7365. break;
  7366. case WASM_OP_TABLE_SIZE:
  7367. case WASM_OP_TABLE_GROW:
  7368. case WASM_OP_TABLE_FILL:
  7369. skip_leb_uint32(p, p_end); /* table idx */
  7370. break;
  7371. #endif /* WASM_ENABLE_REF_TYPES */
  7372. default:
  7373. return false;
  7374. }
  7375. break;
  7376. }
  7377. #if WASM_ENABLE_SIMD != 0
  7378. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  7379. || (WASM_ENABLE_FAST_INTERP != 0)
  7380. case WASM_OP_SIMD_PREFIX:
  7381. {
  7382. uint32 opcode1;
  7383. read_leb_uint32(p, p_end, opcode1);
  7384. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7385. is no larger than UINT8_MAX */
  7386. opcode = (uint8)opcode1;
  7387. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  7388. */
  7389. switch (opcode) {
  7390. case SIMD_v128_load:
  7391. case SIMD_v128_load8x8_s:
  7392. case SIMD_v128_load8x8_u:
  7393. case SIMD_v128_load16x4_s:
  7394. case SIMD_v128_load16x4_u:
  7395. case SIMD_v128_load32x2_s:
  7396. case SIMD_v128_load32x2_u:
  7397. case SIMD_v128_load8_splat:
  7398. case SIMD_v128_load16_splat:
  7399. case SIMD_v128_load32_splat:
  7400. case SIMD_v128_load64_splat:
  7401. case SIMD_v128_store:
  7402. /* memarg align */
  7403. skip_leb_uint32(p, p_end);
  7404. /* memarg offset */
  7405. skip_leb_mem_offset(p, p_end);
  7406. break;
  7407. case SIMD_v128_const:
  7408. case SIMD_v8x16_shuffle:
  7409. /* immByte[16] immLaneId[16] */
  7410. CHECK_BUF1(p, p_end, 16);
  7411. p += 16;
  7412. break;
  7413. case SIMD_i8x16_extract_lane_s:
  7414. case SIMD_i8x16_extract_lane_u:
  7415. case SIMD_i8x16_replace_lane:
  7416. case SIMD_i16x8_extract_lane_s:
  7417. case SIMD_i16x8_extract_lane_u:
  7418. case SIMD_i16x8_replace_lane:
  7419. case SIMD_i32x4_extract_lane:
  7420. case SIMD_i32x4_replace_lane:
  7421. case SIMD_i64x2_extract_lane:
  7422. case SIMD_i64x2_replace_lane:
  7423. case SIMD_f32x4_extract_lane:
  7424. case SIMD_f32x4_replace_lane:
  7425. case SIMD_f64x2_extract_lane:
  7426. case SIMD_f64x2_replace_lane:
  7427. /* ImmLaneId */
  7428. CHECK_BUF(p, p_end, 1);
  7429. p++;
  7430. break;
  7431. case SIMD_v128_load8_lane:
  7432. case SIMD_v128_load16_lane:
  7433. case SIMD_v128_load32_lane:
  7434. case SIMD_v128_load64_lane:
  7435. case SIMD_v128_store8_lane:
  7436. case SIMD_v128_store16_lane:
  7437. case SIMD_v128_store32_lane:
  7438. case SIMD_v128_store64_lane:
  7439. /* memarg align */
  7440. skip_leb_uint32(p, p_end);
  7441. /* memarg offset */
  7442. skip_leb_mem_offset(p, p_end);
  7443. /* ImmLaneId */
  7444. CHECK_BUF(p, p_end, 1);
  7445. p++;
  7446. break;
  7447. case SIMD_v128_load32_zero:
  7448. case SIMD_v128_load64_zero:
  7449. /* memarg align */
  7450. skip_leb_uint32(p, p_end);
  7451. /* memarg offset */
  7452. skip_leb_mem_offset(p, p_end);
  7453. break;
  7454. default:
  7455. /*
  7456. * since latest SIMD specific used almost every value
  7457. * from 0x00 to 0xff, the default branch will present
  7458. * all opcodes without imm
  7459. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7460. */
  7461. break;
  7462. }
  7463. break;
  7464. }
  7465. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7466. (WASM_ENABLE_FAST_INTERP != 0) */
  7467. #endif /* end of WASM_ENABLE_SIMD */
  7468. #if WASM_ENABLE_SHARED_MEMORY != 0
  7469. case WASM_OP_ATOMIC_PREFIX:
  7470. {
  7471. uint32 opcode1;
  7472. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7473. read_leb_uint32(p, p_end, opcode1);
  7474. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7475. is no larger than UINT8_MAX */
  7476. opcode = (uint8)opcode1;
  7477. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7478. skip_leb_uint32(p, p_end); /* align */
  7479. skip_leb_mem_offset(p, p_end); /* offset */
  7480. }
  7481. else {
  7482. /* atomic.fence doesn't have memarg */
  7483. p++;
  7484. }
  7485. break;
  7486. }
  7487. #endif
  7488. #if WASM_ENABLE_DEBUG_INTERP != 0
  7489. case DEBUG_OP_BREAK:
  7490. {
  7491. WASMDebugInstance *debug_instance =
  7492. wasm_exec_env_get_instance(exec_env);
  7493. char original_opcode[1];
  7494. uint64 size = 1;
  7495. WASMModuleInstance *module_inst =
  7496. (WASMModuleInstance *)exec_env->module_inst;
  7497. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7498. ? (p - 1) - module_inst->module->load_addr
  7499. : ~0;
  7500. if (debug_instance) {
  7501. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7502. original_opcode, &size)
  7503. && size == 1) {
  7504. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7505. "with %02x: ",
  7506. original_opcode[0]);
  7507. opcode = original_opcode[0];
  7508. goto op_break_retry;
  7509. }
  7510. }
  7511. break;
  7512. }
  7513. #endif
  7514. default:
  7515. return false;
  7516. }
  7517. }
  7518. (void)u8;
  7519. (void)exec_env;
  7520. return false;
  7521. fail:
  7522. return false;
  7523. }
  7524. #if WASM_ENABLE_FAST_INTERP != 0
  7525. #if WASM_DEBUG_PREPROCESSOR != 0
  7526. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7527. #else
  7528. #define LOG_OP(...) (void)0
  7529. #endif
  7530. #define PATCH_ELSE 0
  7531. #define PATCH_END 1
  7532. typedef struct BranchBlockPatch {
  7533. struct BranchBlockPatch *next;
  7534. uint8 patch_type;
  7535. uint8 *code_compiled;
  7536. } BranchBlockPatch;
  7537. #endif
  7538. typedef struct BranchBlock {
  7539. uint8 label_type;
  7540. BlockType block_type;
  7541. uint8 *start_addr;
  7542. uint8 *else_addr;
  7543. uint8 *end_addr;
  7544. uint32 stack_cell_num;
  7545. #if WASM_ENABLE_GC != 0
  7546. uint32 reftype_map_num;
  7547. /* Indicate which local is used inside current block, used to validate
  7548. * local.get with non-nullable ref types */
  7549. uint8 *local_use_mask;
  7550. uint32 local_use_mask_size;
  7551. #endif
  7552. #if WASM_ENABLE_FAST_INTERP != 0
  7553. uint16 dynamic_offset;
  7554. uint8 *code_compiled;
  7555. BranchBlockPatch *patch_list;
  7556. /* This is used to save params frame_offset of of if block */
  7557. int16 *param_frame_offsets;
  7558. /* This is used to recover the dynamic offset for else branch,
  7559. * and also to remember the start offset of dynamic space which
  7560. * stores the block arguments for loop block, so we can use it
  7561. * to copy the stack operands to the loop block's arguments in
  7562. * wasm_loader_emit_br_info for opcode br. */
  7563. uint16 start_dynamic_offset;
  7564. #endif
  7565. /* Indicate the operand stack is in polymorphic state.
  7566. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7567. * to polymorphic state until the block's 'end' opcode is processed.
  7568. * If stack is in polymorphic state and stack is empty, instruction can
  7569. * pop any type of value directly without decreasing stack top pointer
  7570. * and stack cell num. */
  7571. bool is_stack_polymorphic;
  7572. } BranchBlock;
  7573. typedef struct WASMLoaderContext {
  7574. /* frame ref stack */
  7575. uint8 *frame_ref;
  7576. uint8 *frame_ref_bottom;
  7577. uint8 *frame_ref_boundary;
  7578. uint32 frame_ref_size;
  7579. uint32 stack_cell_num;
  7580. uint32 max_stack_cell_num;
  7581. #if WASM_ENABLE_GC != 0
  7582. /* frame reftype map stack */
  7583. WASMRefTypeMap *frame_reftype_map;
  7584. WASMRefTypeMap *frame_reftype_map_bottom;
  7585. WASMRefTypeMap *frame_reftype_map_boundary;
  7586. uint32 frame_reftype_map_size;
  7587. uint32 reftype_map_num;
  7588. uint32 max_reftype_map_num;
  7589. /* Current module */
  7590. WASMModule *module;
  7591. /* Current module's ref_type_set */
  7592. HashMap *ref_type_set;
  7593. /* Always point to local variable ref_type of
  7594. wasm_loader_prepare_bytecode */
  7595. WASMRefType *ref_type_tmp;
  7596. #endif
  7597. /* frame csp stack */
  7598. BranchBlock *frame_csp;
  7599. BranchBlock *frame_csp_bottom;
  7600. BranchBlock *frame_csp_boundary;
  7601. uint32 frame_csp_size;
  7602. uint32 csp_num;
  7603. uint32 max_csp_num;
  7604. #if WASM_ENABLE_FAST_INTERP != 0
  7605. /* frame offset stack */
  7606. int16 *frame_offset;
  7607. int16 *frame_offset_bottom;
  7608. int16 *frame_offset_boundary;
  7609. uint32 frame_offset_size;
  7610. int16 dynamic_offset;
  7611. int16 start_dynamic_offset;
  7612. int16 max_dynamic_offset;
  7613. /* preserved local offset */
  7614. int16 preserved_local_offset;
  7615. /* const buffer for i64 and f64 consts, note that the raw bytes
  7616. * of i64 and f64 are the same, so we read an i64 value from an
  7617. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7618. int64 *i64_consts;
  7619. uint32 i64_const_max_num;
  7620. uint32 i64_const_num;
  7621. /* const buffer for i32 and f32 consts */
  7622. int32 *i32_consts;
  7623. uint32 i32_const_max_num;
  7624. uint32 i32_const_num;
  7625. /* const buffer for V128 */
  7626. V128 *v128_consts;
  7627. uint32 v128_const_max_num;
  7628. uint32 v128_const_num;
  7629. /* processed code */
  7630. uint8 *p_code_compiled;
  7631. uint8 *p_code_compiled_end;
  7632. uint32 code_compiled_size;
  7633. /* If the last opcode will be dropped, the peak memory usage will be larger
  7634. * than the final code_compiled_size, we record the peak size to ensure
  7635. * there will not be invalid memory access during second traverse */
  7636. uint32 code_compiled_peak_size;
  7637. #endif
  7638. } WASMLoaderContext;
  7639. #define CHECK_CSP_PUSH() \
  7640. do { \
  7641. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7642. MEM_REALLOC( \
  7643. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7644. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7645. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7646. ctx->frame_csp_boundary = \
  7647. ctx->frame_csp_bottom \
  7648. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7649. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7650. } \
  7651. } while (0)
  7652. #define CHECK_CSP_POP() \
  7653. do { \
  7654. if (ctx->csp_num < 1) { \
  7655. set_error_buf(error_buf, error_buf_size, \
  7656. "type mismatch: " \
  7657. "expect data but block stack was empty"); \
  7658. goto fail; \
  7659. } \
  7660. } while (0)
  7661. #if WASM_ENABLE_FAST_INTERP != 0
  7662. static bool
  7663. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7664. uint32 error_buf_size)
  7665. {
  7666. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7667. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7668. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7669. ctx->frame_offset_size + 16);
  7670. ctx->frame_offset_size += 16;
  7671. ctx->frame_offset_boundary =
  7672. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7673. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7674. }
  7675. return true;
  7676. fail:
  7677. return false;
  7678. }
  7679. static bool
  7680. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7681. {
  7682. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7683. return false;
  7684. return true;
  7685. }
  7686. static void
  7687. free_label_patch_list(BranchBlock *frame_csp)
  7688. {
  7689. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7690. BranchBlockPatch *next;
  7691. while (label_patch != NULL) {
  7692. next = label_patch->next;
  7693. wasm_runtime_free(label_patch);
  7694. label_patch = next;
  7695. }
  7696. frame_csp->patch_list = NULL;
  7697. }
  7698. static void
  7699. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7700. {
  7701. BranchBlock *tmp_csp = frame_csp;
  7702. uint32 i;
  7703. for (i = 0; i < csp_num; i++) {
  7704. free_label_patch_list(tmp_csp);
  7705. tmp_csp++;
  7706. }
  7707. }
  7708. static void
  7709. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7710. {
  7711. BranchBlock *tmp_csp = frame_csp;
  7712. uint32 i;
  7713. for (i = 0; i < csp_num; i++) {
  7714. if (tmp_csp->param_frame_offsets)
  7715. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7716. tmp_csp++;
  7717. }
  7718. }
  7719. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7720. #if WASM_ENABLE_GC != 0
  7721. static bool
  7722. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7723. char *error_buf, uint32 error_buf_size)
  7724. {
  7725. BranchBlock *current_csp = ctx->frame_csp - 1;
  7726. uint32 local_mask_size;
  7727. if (local_count == 0) {
  7728. current_csp->local_use_mask_size = 0;
  7729. return true;
  7730. }
  7731. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7732. * else branch, we don't need to allocate memory again */
  7733. if (!current_csp->local_use_mask) {
  7734. local_mask_size = (local_count + 7) / sizeof(uint8);
  7735. if (!(current_csp->local_use_mask =
  7736. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7737. return false;
  7738. }
  7739. current_csp->local_use_mask_size = local_mask_size;
  7740. }
  7741. else {
  7742. local_mask_size = current_csp->local_use_mask_size;
  7743. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7744. }
  7745. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7746. /* For non-function blocks, inherit the use status from parent block */
  7747. BranchBlock *parent_csp = current_csp - 1;
  7748. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7749. bh_assert(parent_csp->local_use_mask);
  7750. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7751. parent_csp->local_use_mask, local_mask_size);
  7752. }
  7753. return true;
  7754. }
  7755. static void
  7756. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7757. {
  7758. BranchBlock *current_csp = ctx->frame_csp - 1;
  7759. bh_assert(current_csp->local_use_mask
  7760. || current_csp->local_use_mask_size == 0);
  7761. if (current_csp->local_use_mask) {
  7762. wasm_runtime_free(current_csp->local_use_mask);
  7763. }
  7764. current_csp->local_use_mask = NULL;
  7765. current_csp->local_use_mask_size = 0;
  7766. }
  7767. static void
  7768. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7769. {
  7770. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7771. uint32 i;
  7772. for (i = 0; i < ctx->csp_num; i++) {
  7773. if (tmp_csp->local_use_mask) {
  7774. wasm_runtime_free(tmp_csp->local_use_mask);
  7775. tmp_csp->local_use_mask = NULL;
  7776. tmp_csp->local_use_mask_size = 0;
  7777. }
  7778. tmp_csp++;
  7779. }
  7780. }
  7781. static void
  7782. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7783. {
  7784. BranchBlock *current_csp = ctx->frame_csp - 1;
  7785. uint32 byte_offset = index / sizeof(uint8);
  7786. uint32 bit_offset = index % sizeof(uint8);
  7787. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7788. bh_assert(current_csp->local_use_mask);
  7789. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7790. }
  7791. static bool
  7792. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7793. {
  7794. BranchBlock *current_csp = ctx->frame_csp - 1;
  7795. uint32 byte_offset = index / sizeof(uint8);
  7796. uint32 bit_offset = index % sizeof(uint8);
  7797. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7798. bh_assert(current_csp->local_use_mask);
  7799. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7800. ? true
  7801. : false;
  7802. }
  7803. #endif /* end of WASM_ENABLE_GC != 0 */
  7804. static void
  7805. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7806. {
  7807. if (ctx) {
  7808. if (ctx->frame_ref_bottom)
  7809. wasm_runtime_free(ctx->frame_ref_bottom);
  7810. #if WASM_ENABLE_GC != 0
  7811. if (ctx->frame_reftype_map_bottom)
  7812. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7813. #endif
  7814. if (ctx->frame_csp_bottom) {
  7815. #if WASM_ENABLE_FAST_INTERP != 0
  7816. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7817. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7818. ctx->csp_num);
  7819. #endif
  7820. #if WASM_ENABLE_GC != 0
  7821. wasm_loader_clean_all_local_use_masks(ctx);
  7822. #endif
  7823. wasm_runtime_free(ctx->frame_csp_bottom);
  7824. }
  7825. #if WASM_ENABLE_FAST_INTERP != 0
  7826. if (ctx->frame_offset_bottom)
  7827. wasm_runtime_free(ctx->frame_offset_bottom);
  7828. if (ctx->i64_consts)
  7829. wasm_runtime_free(ctx->i64_consts);
  7830. if (ctx->i32_consts)
  7831. wasm_runtime_free(ctx->i32_consts);
  7832. if (ctx->v128_consts)
  7833. wasm_runtime_free(ctx->v128_consts);
  7834. #endif
  7835. wasm_runtime_free(ctx);
  7836. }
  7837. }
  7838. static WASMLoaderContext *
  7839. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7840. {
  7841. WASMLoaderContext *loader_ctx =
  7842. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7843. if (!loader_ctx)
  7844. return NULL;
  7845. loader_ctx->frame_ref_size = 32;
  7846. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7847. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7848. goto fail;
  7849. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7850. #if WASM_ENABLE_GC != 0
  7851. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7852. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7853. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7854. error_buf_size)))
  7855. goto fail;
  7856. loader_ctx->frame_reftype_map_boundary =
  7857. loader_ctx->frame_reftype_map_bottom + 16;
  7858. #endif
  7859. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7860. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7861. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7862. goto fail;
  7863. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7864. #if WASM_ENABLE_EXCE_HANDLING != 0
  7865. func->exception_handler_count = 0;
  7866. #endif
  7867. #if WASM_ENABLE_FAST_INTERP != 0
  7868. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7869. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7870. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7871. error_buf_size)))
  7872. goto fail;
  7873. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7874. loader_ctx->i64_const_max_num = 8;
  7875. if (!(loader_ctx->i64_consts =
  7876. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7877. error_buf, error_buf_size)))
  7878. goto fail;
  7879. loader_ctx->i32_const_max_num = 8;
  7880. if (!(loader_ctx->i32_consts =
  7881. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7882. error_buf, error_buf_size)))
  7883. goto fail;
  7884. loader_ctx->v128_const_max_num = 8;
  7885. if (!(loader_ctx->v128_consts =
  7886. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7887. error_buf, error_buf_size)))
  7888. goto fail;
  7889. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7890. set_error_buf(error_buf, error_buf_size,
  7891. "fast interpreter offset overflow");
  7892. goto fail;
  7893. }
  7894. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7895. loader_ctx->max_dynamic_offset =
  7896. func->param_cell_num + func->local_cell_num;
  7897. #endif
  7898. return loader_ctx;
  7899. fail:
  7900. wasm_loader_ctx_destroy(loader_ctx);
  7901. return NULL;
  7902. }
  7903. static bool
  7904. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7905. uint32 error_buf_size)
  7906. {
  7907. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7908. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7909. /* Increase the frame ref stack */
  7910. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7911. ctx->frame_ref_size + 16);
  7912. ctx->frame_ref_size += 16;
  7913. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7914. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7915. }
  7916. #if WASM_ENABLE_GC != 0
  7917. if (wasm_is_type_multi_byte_type(type)
  7918. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7919. /* Increase the frame reftype map stack */
  7920. bh_assert(
  7921. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7922. * sizeof(WASMRefTypeMap))
  7923. == ctx->frame_reftype_map_size);
  7924. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7925. ctx->frame_reftype_map_size
  7926. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7927. ctx->frame_reftype_map =
  7928. ctx->frame_reftype_map_bottom
  7929. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7930. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7931. ctx->frame_reftype_map_boundary =
  7932. ctx->frame_reftype_map_bottom
  7933. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7934. }
  7935. #endif
  7936. return true;
  7937. fail:
  7938. return false;
  7939. }
  7940. static bool
  7941. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7942. uint32 error_buf_size)
  7943. {
  7944. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7945. uint32 i;
  7946. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7947. return false;
  7948. #if WASM_ENABLE_GC != 0
  7949. if (wasm_is_type_multi_byte_type(type)) {
  7950. WASMRefType *ref_type;
  7951. if (!(ref_type =
  7952. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7953. error_buf, error_buf_size))) {
  7954. return false;
  7955. }
  7956. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7957. /* Increase the frame reftype map stack */
  7958. bh_assert((uint32)((ctx->frame_reftype_map
  7959. - ctx->frame_reftype_map_bottom)
  7960. * sizeof(WASMRefTypeMap))
  7961. == ctx->frame_reftype_map_size);
  7962. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7963. ctx->frame_reftype_map_size,
  7964. ctx->frame_reftype_map_size
  7965. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7966. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7967. + ctx->frame_reftype_map_size
  7968. / ((uint32)sizeof(WASMRefTypeMap));
  7969. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7970. ctx->frame_reftype_map_boundary =
  7971. ctx->frame_reftype_map_bottom
  7972. + ctx->frame_reftype_map_size
  7973. / ((uint32)sizeof(WASMRefTypeMap));
  7974. }
  7975. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7976. ctx->frame_reftype_map->ref_type = ref_type;
  7977. ctx->frame_reftype_map++;
  7978. ctx->reftype_map_num++;
  7979. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7980. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7981. }
  7982. #endif
  7983. for (i = 0; i < type_cell_num; i++)
  7984. *ctx->frame_ref++ = type;
  7985. ctx->stack_cell_num += type_cell_num;
  7986. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7987. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7988. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7989. set_error_buf(error_buf, error_buf_size,
  7990. "operand stack depth limit exceeded");
  7991. return false;
  7992. }
  7993. }
  7994. return true;
  7995. #if WASM_ENABLE_GC != 0
  7996. fail:
  7997. return false;
  7998. #endif
  7999. }
  8000. static bool
  8001. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  8002. int32 stack_cell_num,
  8003. #if WASM_ENABLE_GC != 0
  8004. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  8005. #endif
  8006. uint8 type,
  8007. #if WASM_ENABLE_GC != 0
  8008. WASMRefType *ref_type,
  8009. #endif
  8010. char *error_buf, uint32 error_buf_size)
  8011. {
  8012. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  8013. #if WASM_ENABLE_GC != 0
  8014. WASMRefType *frame_reftype = NULL;
  8015. #endif
  8016. if (stack_cell_num < type_cell_num) {
  8017. set_error_buf(error_buf, error_buf_size,
  8018. "type mismatch: expect data but stack was empty");
  8019. return false;
  8020. }
  8021. #if WASM_ENABLE_GC == 0
  8022. for (i = 0; i < type_cell_num; i++) {
  8023. if (*(frame_ref - 1 - i) != type) {
  8024. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8025. "type mismatch: expect ", type2str(type),
  8026. " but got other");
  8027. return false;
  8028. }
  8029. }
  8030. #else
  8031. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  8032. bh_assert(reftype_map_num > 0);
  8033. frame_reftype = (frame_reftype_map - 1)->ref_type;
  8034. }
  8035. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  8036. ref_type, ctx->module->types,
  8037. ctx->module->type_count)) {
  8038. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8039. "type mismatch: expect ", type2str(type),
  8040. " but got other");
  8041. return false;
  8042. }
  8043. for (i = 0; i < type_cell_num - 1; i++) {
  8044. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  8045. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8046. "type mismatch: expect ", type2str(type),
  8047. " but got other");
  8048. return false;
  8049. }
  8050. }
  8051. #endif
  8052. return true;
  8053. }
  8054. static bool
  8055. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  8056. uint32 error_buf_size)
  8057. {
  8058. int32 block_stack_cell_num =
  8059. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  8060. #if WASM_ENABLE_GC != 0
  8061. int32 reftype_map_num =
  8062. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  8063. #endif
  8064. if (block_stack_cell_num > 0) {
  8065. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8066. /* the stack top is a value of any type, return success */
  8067. return true;
  8068. }
  8069. #if WASM_ENABLE_GC != 0
  8070. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  8071. uint8 stack_top_type = *(ctx->frame_ref - 1);
  8072. WASMRefType *stack_top_ref_type = NULL;
  8073. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  8074. bh_assert(reftype_map_num > 0);
  8075. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  8076. }
  8077. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  8078. ctx->ref_type_tmp, ctx->module->types,
  8079. ctx->module->type_count)) {
  8080. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  8081. uint32 ref_type_struct_size =
  8082. wasm_reftype_struct_size(stack_top_ref_type);
  8083. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  8084. stack_top_ref_type, ref_type_struct_size);
  8085. }
  8086. return true;
  8087. }
  8088. }
  8089. #endif
  8090. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  8091. #if WASM_ENABLE_GC != 0
  8092. ctx->frame_reftype_map, reftype_map_num,
  8093. #endif
  8094. type,
  8095. #if WASM_ENABLE_GC != 0
  8096. ctx->ref_type_tmp,
  8097. #endif
  8098. error_buf, error_buf_size)) {
  8099. return false;
  8100. }
  8101. return true;
  8102. }
  8103. static bool
  8104. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  8105. uint32 error_buf_size)
  8106. {
  8107. BranchBlock *cur_block = ctx->frame_csp - 1;
  8108. int32 available_stack_cell =
  8109. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8110. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  8111. /* Directly return success if current block is in stack
  8112. polymorphic state while stack is empty. */
  8113. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8114. return true;
  8115. if (type == VALUE_TYPE_VOID)
  8116. return true;
  8117. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  8118. return false;
  8119. bh_assert(available_stack_cell > 0);
  8120. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  8121. type = VALUE_TYPE_ANY;
  8122. cell_num_to_pop = 1;
  8123. }
  8124. ctx->frame_ref -= cell_num_to_pop;
  8125. ctx->stack_cell_num -= cell_num_to_pop;
  8126. #if WASM_ENABLE_GC != 0
  8127. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  8128. ctx->frame_reftype_map--;
  8129. ctx->reftype_map_num--;
  8130. }
  8131. #endif
  8132. return true;
  8133. }
  8134. #if WASM_ENABLE_GC != 0
  8135. /* Get the stack top element of current block */
  8136. static bool
  8137. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  8138. WASMRefType **p_ref_type, char *error_buf,
  8139. uint32 error_buf_size)
  8140. {
  8141. BranchBlock *cur_block = ctx->frame_csp - 1;
  8142. int32 available_stack_cell =
  8143. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8144. if (available_stack_cell <= 0) {
  8145. /* Directly return success if current block is in stack
  8146. polymorphic state while stack is empty. */
  8147. if (cur_block->is_stack_polymorphic) {
  8148. *p_type = VALUE_TYPE_ANY;
  8149. return true;
  8150. }
  8151. else {
  8152. set_error_buf(
  8153. error_buf, error_buf_size,
  8154. "type mismatch: expect data but block stack was empty");
  8155. return false;
  8156. }
  8157. }
  8158. *p_type = *(ctx->frame_ref - 1);
  8159. if (wasm_is_type_multi_byte_type(*p_type)) {
  8160. int32 available_reftype_map =
  8161. (int32)(ctx->reftype_map_num
  8162. - (ctx->frame_csp - 1)->reftype_map_num);
  8163. bh_assert(available_reftype_map > 0);
  8164. (void)available_reftype_map;
  8165. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  8166. }
  8167. return true;
  8168. }
  8169. #if WASM_ENABLE_FAST_INTERP != 0
  8170. static bool
  8171. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8172. char *error_buf, uint32 error_buf_size);
  8173. #endif
  8174. /* Check whether the stack top elem is a heap object, and if yes,
  8175. pop and return it */
  8176. static bool
  8177. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  8178. WASMRefType *ref_ht_ret, char *error_buf,
  8179. uint32 error_buf_size)
  8180. {
  8181. uint8 type = 0;
  8182. WASMRefType *ref_type = NULL;
  8183. /* Get stack top element */
  8184. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8185. error_buf_size)) {
  8186. return false;
  8187. }
  8188. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  8189. /* stack top isn't a ref type */
  8190. && !wasm_is_type_reftype(type)) {
  8191. set_error_buf(error_buf, error_buf_size,
  8192. "type mismatch: expect heap object but got others");
  8193. return false;
  8194. }
  8195. /* POP stack top */
  8196. if (wasm_is_type_multi_byte_type(type)) {
  8197. bh_assert(ref_type);
  8198. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8199. wasm_reftype_struct_size(ref_type));
  8200. }
  8201. #if WASM_ENABLE_FAST_INTERP != 0
  8202. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8203. error_buf_size)) {
  8204. return false;
  8205. }
  8206. #else
  8207. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8208. return false;
  8209. }
  8210. #endif
  8211. if (p_type)
  8212. *p_type = type;
  8213. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  8214. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  8215. wasm_reftype_struct_size(ref_type));
  8216. }
  8217. return true;
  8218. }
  8219. /* Check whether the stack top elem is subtype of (ref null ht),
  8220. and if yes, pop it and return the converted (ref ht) */
  8221. static bool
  8222. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  8223. WASMRefType *ref_ht_ret, char *error_buf,
  8224. uint32 error_buf_size)
  8225. {
  8226. uint8 type = 0;
  8227. WASMRefType ref_type = { 0 };
  8228. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  8229. error_buf_size)) {
  8230. return false;
  8231. }
  8232. /* Convert to related (ref ht) and return */
  8233. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  8234. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  8235. */
  8236. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  8237. HEAP_TYPE_ARRAY
  8238. + (type - REF_TYPE_ARRAYREF));
  8239. type = ref_ht_ret->ref_type;
  8240. }
  8241. else if (wasm_is_reftype_htref_nullable(type)
  8242. || wasm_is_reftype_htref_non_nullable(type)) {
  8243. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  8244. wasm_reftype_struct_size(&ref_type));
  8245. /* Convert to (ref ht) */
  8246. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  8247. ref_ht_ret->ref_ht_common.nullable = false;
  8248. type = ref_ht_ret->ref_type;
  8249. }
  8250. *p_type = type;
  8251. return true;
  8252. }
  8253. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  8254. and if yes, pop it and return the type_idx */
  8255. static bool
  8256. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  8257. uint32 *p_type_idx, char *error_buf,
  8258. uint32 error_buf_size)
  8259. {
  8260. uint8 type = 0;
  8261. int32 type_idx = -1;
  8262. WASMRefType *ref_type = NULL;
  8263. /* Get stack top element */
  8264. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8265. error_buf_size)) {
  8266. return false;
  8267. }
  8268. if (type != VALUE_TYPE_ANY) {
  8269. /* stack top isn't (ref null $t) */
  8270. if (!((wasm_is_reftype_htref_nullable(type)
  8271. || wasm_is_reftype_htref_non_nullable(type))
  8272. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  8273. set_error_buf(error_buf, error_buf_size,
  8274. "type mismatch: expect (ref null $t) but got others");
  8275. return false;
  8276. }
  8277. type_idx = ref_type->ref_ht_typeidx.type_idx;
  8278. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8279. wasm_reftype_struct_size(ref_type));
  8280. }
  8281. /* POP stack top */
  8282. #if WASM_ENABLE_FAST_INTERP != 0
  8283. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8284. error_buf_size)) {
  8285. return false;
  8286. }
  8287. #else
  8288. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8289. return false;
  8290. }
  8291. #endif
  8292. /* Convert to type_idx and return */
  8293. *p_type = type;
  8294. if (type != VALUE_TYPE_ANY)
  8295. *p_type_idx = (uint32)type_idx;
  8296. return true;
  8297. }
  8298. #endif /* WASM_ENABLE_GC != 0 */
  8299. #if WASM_ENABLE_FAST_INTERP == 0
  8300. static bool
  8301. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  8302. uint8 type_push, uint8 type_pop, char *error_buf,
  8303. uint32 error_buf_size)
  8304. {
  8305. for (int i = 0; i < pop_cnt; i++) {
  8306. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8307. error_buf_size))
  8308. return false;
  8309. }
  8310. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8311. return false;
  8312. return true;
  8313. }
  8314. #endif
  8315. static bool
  8316. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  8317. BlockType block_type, uint8 *start_addr,
  8318. char *error_buf, uint32 error_buf_size)
  8319. {
  8320. CHECK_CSP_PUSH();
  8321. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  8322. ctx->frame_csp->label_type = label_type;
  8323. ctx->frame_csp->block_type = block_type;
  8324. ctx->frame_csp->start_addr = start_addr;
  8325. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  8326. #if WASM_ENABLE_GC != 0
  8327. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  8328. #endif
  8329. #if WASM_ENABLE_FAST_INTERP != 0
  8330. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  8331. ctx->frame_csp->patch_list = NULL;
  8332. #endif
  8333. ctx->frame_csp++;
  8334. ctx->csp_num++;
  8335. if (ctx->csp_num > ctx->max_csp_num) {
  8336. ctx->max_csp_num = ctx->csp_num;
  8337. if (ctx->max_csp_num > UINT16_MAX) {
  8338. set_error_buf(error_buf, error_buf_size,
  8339. "label stack depth limit exceeded");
  8340. return false;
  8341. }
  8342. }
  8343. return true;
  8344. fail:
  8345. return false;
  8346. }
  8347. static bool
  8348. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  8349. uint32 error_buf_size)
  8350. {
  8351. CHECK_CSP_POP();
  8352. #if WASM_ENABLE_FAST_INTERP != 0
  8353. if ((ctx->frame_csp - 1)->param_frame_offsets)
  8354. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  8355. #endif
  8356. ctx->frame_csp--;
  8357. ctx->csp_num--;
  8358. return true;
  8359. fail:
  8360. return false;
  8361. }
  8362. #if WASM_ENABLE_FAST_INTERP != 0
  8363. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  8364. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  8365. #define emit_label(opcode) \
  8366. do { \
  8367. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  8368. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8369. } while (0)
  8370. #define skip_label() \
  8371. do { \
  8372. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  8373. LOG_OP("\ndelete last op\n"); \
  8374. } while (0)
  8375. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8376. #if UINTPTR_MAX == UINT64_MAX
  8377. #define emit_label(opcode) \
  8378. do { \
  8379. int32 offset = \
  8380. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  8381. /* emit int32 relative offset in 64-bit target */ \
  8382. wasm_loader_emit_uint32(loader_ctx, offset); \
  8383. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8384. } while (0)
  8385. #else
  8386. #define emit_label(opcode) \
  8387. do { \
  8388. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  8389. /* emit uint32 label address in 32-bit target */ \
  8390. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  8391. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8392. } while (0)
  8393. #endif
  8394. #define skip_label() \
  8395. do { \
  8396. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  8397. LOG_OP("\ndelete last op\n"); \
  8398. } while (0)
  8399. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8400. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  8401. #define emit_label(opcode) \
  8402. do { \
  8403. wasm_loader_emit_uint8(loader_ctx, opcode); \
  8404. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8405. } while (0)
  8406. #define skip_label() \
  8407. do { \
  8408. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  8409. LOG_OP("\ndelete last op\n"); \
  8410. } while (0)
  8411. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  8412. #define emit_empty_label_addr_and_frame_ip(type) \
  8413. do { \
  8414. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  8415. loader_ctx->p_code_compiled, error_buf, \
  8416. error_buf_size)) \
  8417. goto fail; \
  8418. /* label address, to be patched */ \
  8419. wasm_loader_emit_ptr(loader_ctx, NULL); \
  8420. } while (0)
  8421. #define emit_br_info(frame_csp, is_br) \
  8422. do { \
  8423. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  8424. error_buf_size)) \
  8425. goto fail; \
  8426. } while (0)
  8427. #define LAST_OP_OUTPUT_I32() \
  8428. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  8429. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  8430. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  8431. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  8432. || (last_op >= WASM_OP_I32_WRAP_I64 \
  8433. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  8434. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  8435. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  8436. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  8437. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  8438. || (last_op == EXT_OP_COPY_STACK_TOP)
  8439. #define LAST_OP_OUTPUT_I64() \
  8440. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  8441. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  8442. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  8443. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  8444. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  8445. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  8446. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8447. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8448. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8449. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8450. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8451. #define GET_CONST_OFFSET(type, val) \
  8452. do { \
  8453. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8454. &operand_offset, error_buf, \
  8455. error_buf_size))) \
  8456. goto fail; \
  8457. } while (0)
  8458. #define GET_CONST_F32_OFFSET(type, fval) \
  8459. do { \
  8460. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8461. &operand_offset, error_buf, \
  8462. error_buf_size))) \
  8463. goto fail; \
  8464. } while (0)
  8465. #define GET_CONST_F64_OFFSET(type, fval) \
  8466. do { \
  8467. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8468. &operand_offset, error_buf, \
  8469. error_buf_size))) \
  8470. goto fail; \
  8471. } while (0)
  8472. #define emit_operand(ctx, offset) \
  8473. do { \
  8474. wasm_loader_emit_int16(ctx, offset); \
  8475. LOG_OP("%d\t", offset); \
  8476. } while (0)
  8477. #define emit_byte(ctx, byte) \
  8478. do { \
  8479. wasm_loader_emit_uint8(ctx, byte); \
  8480. LOG_OP("%d\t", byte); \
  8481. } while (0)
  8482. #define emit_uint32(ctx, value) \
  8483. do { \
  8484. wasm_loader_emit_uint32(ctx, value); \
  8485. LOG_OP("%d\t", value); \
  8486. } while (0)
  8487. #define emit_uint64(ctx, value) \
  8488. do { \
  8489. wasm_loader_emit_const(ctx, &value, false); \
  8490. LOG_OP("%lld\t", value); \
  8491. } while (0)
  8492. #define emit_float32(ctx, value) \
  8493. do { \
  8494. wasm_loader_emit_const(ctx, &value, true); \
  8495. LOG_OP("%f\t", value); \
  8496. } while (0)
  8497. #define emit_float64(ctx, value) \
  8498. do { \
  8499. wasm_loader_emit_const(ctx, &value, false); \
  8500. LOG_OP("%f\t", value); \
  8501. } while (0)
  8502. static bool
  8503. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8504. {
  8505. if (!(ctx->p_code_compiled =
  8506. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8507. return false;
  8508. ctx->p_code_compiled_end =
  8509. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8510. /* clean up frame ref */
  8511. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8512. ctx->frame_ref = ctx->frame_ref_bottom;
  8513. ctx->stack_cell_num = 0;
  8514. #if WASM_ENABLE_GC != 0
  8515. /* clean up reftype map */
  8516. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8517. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8518. ctx->reftype_map_num = 0;
  8519. #endif
  8520. /* clean up frame csp */
  8521. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8522. ctx->frame_csp = ctx->frame_csp_bottom;
  8523. ctx->csp_num = 0;
  8524. ctx->max_csp_num = 0;
  8525. /* clean up frame offset */
  8526. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8527. ctx->frame_offset = ctx->frame_offset_bottom;
  8528. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8529. /* init preserved local offsets */
  8530. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8531. /* const buf is reserved */
  8532. return true;
  8533. }
  8534. static void
  8535. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8536. {
  8537. ctx->code_compiled_size += size;
  8538. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8539. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8540. }
  8541. }
  8542. static void
  8543. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8544. {
  8545. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8546. if (ctx->p_code_compiled) {
  8547. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8548. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8549. #endif
  8550. bh_memcpy_s(ctx->p_code_compiled,
  8551. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8552. value, size);
  8553. ctx->p_code_compiled += size;
  8554. }
  8555. else {
  8556. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8557. bh_assert((ctx->code_compiled_size & 1) == 0);
  8558. #endif
  8559. increase_compiled_code_space(ctx, size);
  8560. }
  8561. }
  8562. static void
  8563. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8564. {
  8565. if (ctx->p_code_compiled) {
  8566. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8567. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8568. #endif
  8569. STORE_U32(ctx->p_code_compiled, value);
  8570. ctx->p_code_compiled += sizeof(uint32);
  8571. }
  8572. else {
  8573. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8574. bh_assert((ctx->code_compiled_size & 1) == 0);
  8575. #endif
  8576. increase_compiled_code_space(ctx, sizeof(uint32));
  8577. }
  8578. }
  8579. static void
  8580. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8581. {
  8582. if (ctx->p_code_compiled) {
  8583. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8584. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8585. #endif
  8586. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8587. ctx->p_code_compiled += sizeof(int16);
  8588. }
  8589. else {
  8590. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8591. bh_assert((ctx->code_compiled_size & 1) == 0);
  8592. #endif
  8593. increase_compiled_code_space(ctx, sizeof(uint16));
  8594. }
  8595. }
  8596. static void
  8597. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8598. {
  8599. if (ctx->p_code_compiled) {
  8600. *(ctx->p_code_compiled) = value;
  8601. ctx->p_code_compiled += sizeof(uint8);
  8602. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8603. ctx->p_code_compiled++;
  8604. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8605. #endif
  8606. }
  8607. else {
  8608. increase_compiled_code_space(ctx, sizeof(uint8));
  8609. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8610. increase_compiled_code_space(ctx, sizeof(uint8));
  8611. bh_assert((ctx->code_compiled_size & 1) == 0);
  8612. #endif
  8613. }
  8614. }
  8615. static void
  8616. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8617. {
  8618. if (ctx->p_code_compiled) {
  8619. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8620. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8621. #endif
  8622. STORE_PTR(ctx->p_code_compiled, value);
  8623. ctx->p_code_compiled += sizeof(void *);
  8624. }
  8625. else {
  8626. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8627. bh_assert((ctx->code_compiled_size & 1) == 0);
  8628. #endif
  8629. increase_compiled_code_space(ctx, sizeof(void *));
  8630. }
  8631. }
  8632. static void
  8633. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8634. {
  8635. if (ctx->p_code_compiled) {
  8636. ctx->p_code_compiled -= size;
  8637. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8638. if (size == sizeof(uint8)) {
  8639. ctx->p_code_compiled--;
  8640. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8641. }
  8642. #endif
  8643. }
  8644. else {
  8645. ctx->code_compiled_size -= size;
  8646. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8647. if (size == sizeof(uint8)) {
  8648. ctx->code_compiled_size--;
  8649. bh_assert((ctx->code_compiled_size & 1) == 0);
  8650. }
  8651. #endif
  8652. }
  8653. }
  8654. static bool
  8655. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8656. uint32 local_index, uint32 local_type,
  8657. bool *preserved, char *error_buf,
  8658. uint32 error_buf_size)
  8659. {
  8660. uint32 i = 0;
  8661. int16 preserved_offset = (int16)local_index;
  8662. *preserved = false;
  8663. while (i < loader_ctx->stack_cell_num) {
  8664. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8665. /* move previous local into dynamic space before a set/tee_local opcode
  8666. */
  8667. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8668. if (!(*preserved)) {
  8669. *preserved = true;
  8670. skip_label();
  8671. preserved_offset = loader_ctx->preserved_local_offset;
  8672. if (loader_ctx->p_code_compiled) {
  8673. bh_assert(preserved_offset != (int16)local_index);
  8674. }
  8675. if (is_32bit_type(local_type)) {
  8676. /* Only increase preserve offset in the second traversal */
  8677. if (loader_ctx->p_code_compiled)
  8678. loader_ctx->preserved_local_offset++;
  8679. emit_label(EXT_OP_COPY_STACK_TOP);
  8680. }
  8681. #if WASM_ENABLE_SIMDE != 0
  8682. else if (local_type == VALUE_TYPE_V128) {
  8683. if (loader_ctx->p_code_compiled)
  8684. loader_ctx->preserved_local_offset += 4;
  8685. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8686. }
  8687. #endif
  8688. else {
  8689. if (loader_ctx->p_code_compiled)
  8690. loader_ctx->preserved_local_offset += 2;
  8691. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8692. }
  8693. /* overflow */
  8694. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8695. set_error_buf_v(error_buf, error_buf_size,
  8696. "too much local cells 0x%x",
  8697. loader_ctx->preserved_local_offset);
  8698. return false;
  8699. }
  8700. emit_operand(loader_ctx, local_index);
  8701. emit_operand(loader_ctx, preserved_offset);
  8702. emit_label(opcode);
  8703. }
  8704. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8705. }
  8706. if (cur_type == VALUE_TYPE_V128) {
  8707. i += 4;
  8708. }
  8709. else if (is_32bit_type(cur_type)) {
  8710. i++;
  8711. }
  8712. else {
  8713. i += 2;
  8714. }
  8715. }
  8716. (void)error_buf;
  8717. (void)error_buf_size;
  8718. return true;
  8719. }
  8720. static bool
  8721. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8722. char *error_buf, uint32 error_buf_size)
  8723. {
  8724. uint32 i = 0;
  8725. bool preserve_local;
  8726. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8727. blocks will not influence the value of these locals */
  8728. while (i < loader_ctx->stack_cell_num) {
  8729. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8730. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8731. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8732. && (cur_offset >= 0)) {
  8733. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8734. cur_type, &preserve_local,
  8735. error_buf, error_buf_size)))
  8736. return false;
  8737. }
  8738. if (cur_type == VALUE_TYPE_V128) {
  8739. i += 4;
  8740. }
  8741. else if (is_32bit_type(cur_type)) {
  8742. i++;
  8743. }
  8744. else {
  8745. i += 2;
  8746. }
  8747. }
  8748. return true;
  8749. }
  8750. static bool
  8751. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8752. uint8 *p_code_compiled, char *error_buf,
  8753. uint32 error_buf_size)
  8754. {
  8755. BranchBlockPatch *patch =
  8756. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8757. if (!patch) {
  8758. return false;
  8759. }
  8760. patch->patch_type = patch_type;
  8761. patch->code_compiled = p_code_compiled;
  8762. if (!frame_csp->patch_list) {
  8763. frame_csp->patch_list = patch;
  8764. patch->next = NULL;
  8765. }
  8766. else {
  8767. patch->next = frame_csp->patch_list;
  8768. frame_csp->patch_list = patch;
  8769. }
  8770. return true;
  8771. }
  8772. static void
  8773. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8774. {
  8775. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8776. BranchBlockPatch *node = frame_csp->patch_list;
  8777. BranchBlockPatch *node_prev = NULL, *node_next;
  8778. if (!ctx->p_code_compiled)
  8779. return;
  8780. while (node) {
  8781. node_next = node->next;
  8782. if (node->patch_type == patch_type) {
  8783. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8784. if (node_prev == NULL) {
  8785. frame_csp->patch_list = node_next;
  8786. }
  8787. else {
  8788. node_prev->next = node_next;
  8789. }
  8790. wasm_runtime_free(node);
  8791. }
  8792. else {
  8793. node_prev = node;
  8794. }
  8795. node = node_next;
  8796. }
  8797. }
  8798. static bool
  8799. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8800. bool is_br, char *error_buf, uint32 error_buf_size)
  8801. {
  8802. /* br info layout:
  8803. * a) arity of target block
  8804. * b) total cell num of arity values
  8805. * c) each arity value's cell num
  8806. * d) each arity value's src frame offset
  8807. * e) each arity values's dst dynamic offset
  8808. * f) branch target address
  8809. *
  8810. * Note: b-e are omitted when arity is 0 so that
  8811. * interpreter can recover the br info quickly.
  8812. */
  8813. BlockType *block_type = &frame_csp->block_type;
  8814. uint8 *types = NULL, cell;
  8815. #if WASM_ENABLE_GC != 0
  8816. WASMRefTypeMap *reftype_maps;
  8817. uint32 reftype_map_count;
  8818. #endif
  8819. uint32 arity = 0;
  8820. int32 i;
  8821. int16 *frame_offset = ctx->frame_offset;
  8822. uint16 dynamic_offset;
  8823. /* Note: loop's arity is different from if and block. loop's arity is
  8824. * its parameter count while if and block arity is result count.
  8825. */
  8826. #if WASM_ENABLE_GC == 0
  8827. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8828. arity = block_type_get_param_types(block_type, &types);
  8829. else
  8830. arity = block_type_get_result_types(block_type, &types);
  8831. #else
  8832. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8833. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8834. &reftype_map_count);
  8835. else
  8836. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8837. &reftype_map_count);
  8838. #endif
  8839. /* Part a */
  8840. emit_uint32(ctx, arity);
  8841. if (arity) {
  8842. /* Part b */
  8843. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8844. /* Part c */
  8845. for (i = (int32)arity - 1; i >= 0; i--) {
  8846. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8847. emit_byte(ctx, cell);
  8848. }
  8849. /* Part d */
  8850. for (i = (int32)arity - 1; i >= 0; i--) {
  8851. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8852. frame_offset -= cell;
  8853. emit_operand(ctx, *(int16 *)(frame_offset));
  8854. }
  8855. /* Part e */
  8856. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8857. /* Use start_dynamic_offset which was set in
  8858. copy_params_to_dynamic_space */
  8859. dynamic_offset = frame_csp->start_dynamic_offset
  8860. + wasm_get_cell_num(types, arity);
  8861. else
  8862. dynamic_offset =
  8863. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8864. if (is_br)
  8865. ctx->dynamic_offset = dynamic_offset;
  8866. for (i = (int32)arity - 1; i >= 0; i--) {
  8867. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8868. dynamic_offset -= cell;
  8869. emit_operand(ctx, dynamic_offset);
  8870. }
  8871. }
  8872. /* Part f */
  8873. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8874. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8875. }
  8876. else {
  8877. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8878. error_buf, error_buf_size))
  8879. return false;
  8880. /* label address, to be patched */
  8881. wasm_loader_emit_ptr(ctx, NULL);
  8882. }
  8883. return true;
  8884. }
  8885. static bool
  8886. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8887. bool disable_emit, int16 operand_offset,
  8888. char *error_buf, uint32 error_buf_size)
  8889. {
  8890. uint32 cell_num_to_push, i;
  8891. if (type == VALUE_TYPE_VOID)
  8892. return true;
  8893. /* only check memory overflow in first traverse */
  8894. if (ctx->p_code_compiled == NULL) {
  8895. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8896. return false;
  8897. }
  8898. if (disable_emit)
  8899. *(ctx->frame_offset)++ = operand_offset;
  8900. else {
  8901. emit_operand(ctx, ctx->dynamic_offset);
  8902. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8903. ctx->dynamic_offset++;
  8904. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8905. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8906. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8907. goto fail;
  8908. }
  8909. }
  8910. }
  8911. if (is_32bit_type(type))
  8912. return true;
  8913. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8914. for (i = 0; i < cell_num_to_push; i++) {
  8915. if (ctx->p_code_compiled == NULL) {
  8916. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8917. return false;
  8918. }
  8919. ctx->frame_offset++;
  8920. if (!disable_emit) {
  8921. ctx->dynamic_offset++;
  8922. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8923. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8924. if (ctx->max_dynamic_offset >= INT16_MAX)
  8925. goto fail;
  8926. }
  8927. }
  8928. }
  8929. return true;
  8930. fail:
  8931. set_error_buf(error_buf, error_buf_size,
  8932. "fast interpreter offset overflow");
  8933. return false;
  8934. }
  8935. /* This function should be in front of wasm_loader_pop_frame_ref
  8936. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8937. will be modified by wasm_loader_pop_frame_ref */
  8938. static bool
  8939. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8940. char *error_buf, uint32 error_buf_size)
  8941. {
  8942. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8943. then current block is the function block */
  8944. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8945. BranchBlock *cur_block = ctx->frame_csp - depth;
  8946. int32 available_stack_cell =
  8947. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8948. uint32 cell_num_to_pop;
  8949. /* Directly return success if current block is in stack
  8950. polymorphic state while stack is empty. */
  8951. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8952. return true;
  8953. if (type == VALUE_TYPE_VOID)
  8954. return true;
  8955. /* Change type to ANY when the stack top is ANY, so as to avoid
  8956. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8957. two offsets */
  8958. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8959. type = VALUE_TYPE_ANY;
  8960. cell_num_to_pop = wasm_value_type_cell_num(type);
  8961. /* Check the offset stack bottom to ensure the frame offset
  8962. stack will not go underflow. But we don't thrown error
  8963. and return true here, because the error msg should be
  8964. given in wasm_loader_pop_frame_ref */
  8965. if (!check_offset_pop(ctx, cell_num_to_pop))
  8966. return true;
  8967. ctx->frame_offset -= cell_num_to_pop;
  8968. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8969. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8970. ctx->dynamic_offset -= cell_num_to_pop;
  8971. emit_operand(ctx, *(ctx->frame_offset));
  8972. (void)error_buf;
  8973. (void)error_buf_size;
  8974. return true;
  8975. }
  8976. static bool
  8977. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8978. bool disable_emit, int16 operand_offset,
  8979. char *error_buf, uint32 error_buf_size)
  8980. {
  8981. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8982. error_buf, error_buf_size)))
  8983. return false;
  8984. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8985. return false;
  8986. return true;
  8987. }
  8988. static bool
  8989. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8990. char *error_buf, uint32 error_buf_size)
  8991. {
  8992. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8993. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8994. return false;
  8995. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8996. return false;
  8997. return true;
  8998. }
  8999. static bool
  9000. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  9001. uint8 type_push, uint8 type_pop,
  9002. bool disable_emit, int16 operand_offset,
  9003. char *error_buf, uint32 error_buf_size)
  9004. {
  9005. uint8 i;
  9006. for (i = 0; i < pop_cnt; i++) {
  9007. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  9008. error_buf_size))
  9009. return false;
  9010. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  9011. error_buf_size))
  9012. return false;
  9013. }
  9014. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  9015. operand_offset, error_buf,
  9016. error_buf_size))
  9017. return false;
  9018. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  9019. return false;
  9020. return true;
  9021. }
  9022. static int
  9023. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  9024. {
  9025. int64 i64_const1 = *(int64 *)p_i64_const1;
  9026. int64 i64_const2 = *(int64 *)p_i64_const2;
  9027. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  9028. }
  9029. static int
  9030. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  9031. {
  9032. int32 i32_const1 = *(int32 *)p_i32_const1;
  9033. int32 i32_const2 = *(int32 *)p_i32_const2;
  9034. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  9035. }
  9036. static int
  9037. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  9038. {
  9039. V128 v128_const1 = *(V128 *)p_v128_const1;
  9040. V128 v128_const2 = *(V128 *)p_v128_const2;
  9041. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  9042. }
  9043. static bool
  9044. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  9045. int16 *offset, char *error_buf,
  9046. uint32 error_buf_size)
  9047. {
  9048. if (!ctx->p_code_compiled) {
  9049. /* Treat i64 and f64 as the same by reading i64 value from
  9050. the raw bytes */
  9051. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  9052. /* No slot left, emit const instead */
  9053. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  9054. *offset = 0;
  9055. return true;
  9056. }
  9057. /* Traverse the list if the const num is small */
  9058. if (ctx->i64_const_num < 10) {
  9059. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  9060. if (ctx->i64_consts[i] == *(int64 *)value) {
  9061. *offset = -1;
  9062. return true;
  9063. }
  9064. }
  9065. }
  9066. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  9067. MEM_REALLOC(ctx->i64_consts,
  9068. sizeof(int64) * ctx->i64_const_max_num,
  9069. sizeof(int64) * (ctx->i64_const_max_num * 2));
  9070. ctx->i64_const_max_num *= 2;
  9071. }
  9072. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  9073. }
  9074. else if (type == VALUE_TYPE_V128) {
  9075. /* No slot left, emit const instead */
  9076. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  9077. *offset = 0;
  9078. return true;
  9079. }
  9080. /* Traverse the list if the const num is small */
  9081. if (ctx->v128_const_num < 10) {
  9082. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  9083. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  9084. == 0) {
  9085. *offset = -1;
  9086. return true;
  9087. }
  9088. }
  9089. }
  9090. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  9091. MEM_REALLOC(ctx->v128_consts,
  9092. sizeof(V128) * ctx->v128_const_max_num,
  9093. sizeof(V128) * (ctx->v128_const_max_num * 2));
  9094. ctx->v128_const_max_num *= 2;
  9095. }
  9096. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  9097. }
  9098. else {
  9099. /* Treat i32 and f32 as the same by reading i32 value from
  9100. the raw bytes */
  9101. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  9102. /* No slot left, emit const instead */
  9103. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  9104. *offset = 0;
  9105. return true;
  9106. }
  9107. /* Traverse the list if the const num is small */
  9108. if (ctx->i32_const_num < 10) {
  9109. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  9110. if (ctx->i32_consts[i] == *(int32 *)value) {
  9111. *offset = -1;
  9112. return true;
  9113. }
  9114. }
  9115. }
  9116. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  9117. MEM_REALLOC(ctx->i32_consts,
  9118. sizeof(int32) * ctx->i32_const_max_num,
  9119. sizeof(int32) * (ctx->i32_const_max_num * 2));
  9120. ctx->i32_const_max_num *= 2;
  9121. }
  9122. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  9123. }
  9124. *offset = -1;
  9125. return true;
  9126. }
  9127. else {
  9128. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  9129. int64 key = *(int64 *)value, *i64_const;
  9130. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  9131. sizeof(int64), cmp_i64_const);
  9132. if (!i64_const) { /* not found, emit const instead */
  9133. *offset = 0;
  9134. return true;
  9135. }
  9136. /* constant index is encoded as negative value */
  9137. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  9138. + (int32)(i64_const - ctx->i64_consts) * 2;
  9139. }
  9140. else if (type == VALUE_TYPE_V128) {
  9141. V128 key = *(V128 *)value, *v128_const;
  9142. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  9143. sizeof(V128), cmp_v128_const);
  9144. if (!v128_const) { /* not found, emit const instead */
  9145. *offset = 0;
  9146. return true;
  9147. }
  9148. /* constant index is encoded as negative value */
  9149. *offset = -(int32)(ctx->v128_const_num)
  9150. + (int32)(v128_const - ctx->v128_consts);
  9151. }
  9152. else {
  9153. int32 key = *(int32 *)value, *i32_const;
  9154. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  9155. sizeof(int32), cmp_i32_const);
  9156. if (!i32_const) { /* not found, emit const instead */
  9157. *offset = 0;
  9158. return true;
  9159. }
  9160. /* constant index is encoded as negative value */
  9161. *offset = -(int32)(ctx->i32_const_num)
  9162. + (int32)(i32_const - ctx->i32_consts);
  9163. }
  9164. return true;
  9165. }
  9166. fail:
  9167. return false;
  9168. }
  9169. /*
  9170. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  9171. -- Mostly used for the binary / compare operation
  9172. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  9173. -- Mostly used in block / control instructions
  9174. The POP will always emit the offset on the top of the frame_offset stack
  9175. PUSH can be used in two ways:
  9176. 1. directly PUSH:
  9177. PUSH_XXX();
  9178. will allocate a dynamic space and emit
  9179. 2. silent PUSH:
  9180. operand_offset = xxx; disable_emit = true;
  9181. PUSH_XXX();
  9182. only push the frame_offset stack, no emit
  9183. */
  9184. #define TEMPLATE_PUSH(Type) \
  9185. do { \
  9186. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9187. disable_emit, operand_offset, \
  9188. error_buf, error_buf_size)) \
  9189. goto fail; \
  9190. } while (0)
  9191. #define TEMPLATE_PUSH_REF(Type) \
  9192. do { \
  9193. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  9194. operand_offset, error_buf, \
  9195. error_buf_size)) \
  9196. goto fail; \
  9197. } while (0)
  9198. #define TEMPLATE_POP(Type) \
  9199. do { \
  9200. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9201. error_buf, error_buf_size)) \
  9202. goto fail; \
  9203. } while (0)
  9204. #define TEMPLATE_POP_REF(Type) \
  9205. do { \
  9206. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  9207. error_buf_size)) \
  9208. goto fail; \
  9209. } while (0)
  9210. #define PUSH_OFFSET_TYPE(type) \
  9211. do { \
  9212. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  9213. operand_offset, error_buf, \
  9214. error_buf_size))) \
  9215. goto fail; \
  9216. } while (0)
  9217. #define POP_OFFSET_TYPE(type) \
  9218. do { \
  9219. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  9220. error_buf_size))) \
  9221. goto fail; \
  9222. } while (0)
  9223. #define POP_AND_PUSH(type_pop, type_push) \
  9224. do { \
  9225. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9226. loader_ctx, 1, type_push, type_pop, disable_emit, \
  9227. operand_offset, error_buf, error_buf_size))) \
  9228. goto fail; \
  9229. } while (0)
  9230. /* type of POPs should be the same */
  9231. #define POP2_AND_PUSH(type_pop, type_push) \
  9232. do { \
  9233. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9234. loader_ctx, 2, type_push, type_pop, disable_emit, \
  9235. operand_offset, error_buf, error_buf_size))) \
  9236. goto fail; \
  9237. } while (0)
  9238. #else /* WASM_ENABLE_FAST_INTERP */
  9239. #define TEMPLATE_PUSH(Type) \
  9240. do { \
  9241. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9242. error_buf, error_buf_size))) \
  9243. goto fail; \
  9244. } while (0)
  9245. #define TEMPLATE_PUSH_REF(Type) \
  9246. do { \
  9247. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  9248. error_buf_size))) \
  9249. goto fail; \
  9250. } while (0)
  9251. #define TEMPLATE_POP(Type) \
  9252. do { \
  9253. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9254. error_buf, error_buf_size))) \
  9255. goto fail; \
  9256. } while (0)
  9257. #define TEMPLATE_POP_REF(Type) \
  9258. do { \
  9259. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  9260. error_buf_size))) \
  9261. goto fail; \
  9262. } while (0)
  9263. #define POP_AND_PUSH(type_pop, type_push) \
  9264. do { \
  9265. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  9266. type_pop, error_buf, \
  9267. error_buf_size))) \
  9268. goto fail; \
  9269. } while (0)
  9270. /* type of POPs should be the same */
  9271. #define POP2_AND_PUSH(type_pop, type_push) \
  9272. do { \
  9273. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  9274. type_pop, error_buf, \
  9275. error_buf_size))) \
  9276. goto fail; \
  9277. } while (0)
  9278. #endif /* WASM_ENABLE_FAST_INTERP */
  9279. #define PUSH_I32() TEMPLATE_PUSH(I32)
  9280. #define PUSH_F32() TEMPLATE_PUSH(F32)
  9281. #define PUSH_I64() TEMPLATE_PUSH(I64)
  9282. #define PUSH_F64() TEMPLATE_PUSH(F64)
  9283. #define PUSH_V128() TEMPLATE_PUSH(V128)
  9284. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  9285. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  9286. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  9287. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  9288. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  9289. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  9290. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  9291. #define POP_I32() TEMPLATE_POP(I32)
  9292. #define POP_F32() TEMPLATE_POP(F32)
  9293. #define POP_I64() TEMPLATE_POP(I64)
  9294. #define POP_F64() TEMPLATE_POP(F64)
  9295. #define POP_V128() TEMPLATE_POP(V128)
  9296. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  9297. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  9298. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  9299. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  9300. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  9301. #if WASM_ENABLE_FAST_INTERP != 0
  9302. static bool
  9303. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  9304. bool disable_emit, char *error_buf, uint32 error_buf_size)
  9305. {
  9306. int16 operand_offset = 0;
  9307. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  9308. : loader_ctx->frame_csp;
  9309. BlockType *block_type = &block->block_type;
  9310. uint8 *return_types = NULL;
  9311. #if WASM_ENABLE_GC != 0
  9312. WASMRefTypeMap *reftype_maps = NULL;
  9313. uint32 reftype_map_count;
  9314. #endif
  9315. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  9316. int32 i = 0;
  9317. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  9318. *frame_offset_org = NULL;
  9319. #if WASM_ENABLE_GC == 0
  9320. return_count = block_type_get_result_types(block_type, &return_types);
  9321. #else
  9322. return_count = block_type_get_result_types(
  9323. block_type, &return_types, &reftype_maps, &reftype_map_count);
  9324. #endif
  9325. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  9326. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  9327. if (return_count == 1) {
  9328. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  9329. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  9330. /* insert op_copy before else opcode */
  9331. if (opcode == WASM_OP_ELSE)
  9332. skip_label();
  9333. #if WASM_ENABLE_SIMDE != 0
  9334. if (cell == 4) {
  9335. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  9336. }
  9337. #endif
  9338. if (cell <= 2) {
  9339. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  9340. : EXT_OP_COPY_STACK_TOP_I64);
  9341. }
  9342. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  9343. emit_operand(loader_ctx, block->dynamic_offset);
  9344. if (opcode == WASM_OP_ELSE) {
  9345. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  9346. }
  9347. else {
  9348. loader_ctx->frame_offset -= cell;
  9349. loader_ctx->dynamic_offset = block->dynamic_offset;
  9350. PUSH_OFFSET_TYPE(return_types[0]);
  9351. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9352. }
  9353. if (opcode == WASM_OP_ELSE)
  9354. emit_label(opcode);
  9355. }
  9356. return true;
  9357. }
  9358. /* Copy stack top values to block's results which are in dynamic space.
  9359. * The instruction format:
  9360. * Part a: values count
  9361. * Part b: all values total cell num
  9362. * Part c: each value's cell_num, src offset and dst offset
  9363. * Part d: each value's src offset and dst offset
  9364. * Part e: each value's dst offset
  9365. */
  9366. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  9367. dynamic_offset = dynamic_offset_org =
  9368. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  9369. /* First traversal to get the count of values needed to be copied. */
  9370. for (i = (int32)return_count - 1; i >= 0; i--) {
  9371. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  9372. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  9373. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  9374. goto fail;
  9375. }
  9376. if (cells == 4) {
  9377. bool needs_copy = false;
  9378. int16 v128_dynamic = dynamic_offset - cells;
  9379. for (int j = 0; j < 4; j++) {
  9380. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  9381. needs_copy = true;
  9382. break;
  9383. }
  9384. }
  9385. if (needs_copy) {
  9386. value_count++;
  9387. total_cel_num += cells;
  9388. }
  9389. frame_offset -= cells;
  9390. dynamic_offset = v128_dynamic;
  9391. }
  9392. else {
  9393. frame_offset -= cells;
  9394. dynamic_offset -= cells;
  9395. if (dynamic_offset != *frame_offset) {
  9396. value_count++;
  9397. total_cel_num += cells;
  9398. }
  9399. }
  9400. }
  9401. if (value_count) {
  9402. uint32 j = 0;
  9403. uint8 *emit_data = NULL, *cells = NULL;
  9404. int16 *src_offsets = NULL;
  9405. uint16 *dst_offsets = NULL;
  9406. uint64 size =
  9407. (uint64)value_count
  9408. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  9409. /* Allocate memory for the emit data */
  9410. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9411. return false;
  9412. cells = emit_data;
  9413. src_offsets = (int16 *)(cells + value_count);
  9414. dst_offsets = (uint16 *)(src_offsets + value_count);
  9415. /* insert op_copy before else opcode */
  9416. if (opcode == WASM_OP_ELSE)
  9417. skip_label();
  9418. emit_label(EXT_OP_COPY_STACK_VALUES);
  9419. /* Part a) */
  9420. emit_uint32(loader_ctx, value_count);
  9421. /* Part b) */
  9422. emit_uint32(loader_ctx, total_cel_num);
  9423. /* Second traversal to get each value's cell num, src offset and dst
  9424. * offset. */
  9425. frame_offset = frame_offset_org;
  9426. dynamic_offset = dynamic_offset_org;
  9427. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  9428. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  9429. if (cell == 4) {
  9430. bool needs_copy = false;
  9431. int16 v128_dynamic = dynamic_offset - cell;
  9432. for (int k = 0; k < 4; k++) {
  9433. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  9434. needs_copy = true;
  9435. break;
  9436. }
  9437. }
  9438. if (needs_copy) {
  9439. cells[j] = cell;
  9440. src_offsets[j] = *(frame_offset - cell);
  9441. dst_offsets[j] = v128_dynamic;
  9442. j++;
  9443. }
  9444. frame_offset -= cell;
  9445. dynamic_offset = v128_dynamic;
  9446. }
  9447. else {
  9448. frame_offset -= cell;
  9449. dynamic_offset -= cell;
  9450. if (dynamic_offset != *frame_offset) {
  9451. cells[j] = cell;
  9452. /* src offset */
  9453. src_offsets[j] = *frame_offset;
  9454. /* dst offset */
  9455. dst_offsets[j] = dynamic_offset;
  9456. j++;
  9457. }
  9458. }
  9459. if (opcode == WASM_OP_ELSE) {
  9460. if (cell == 4) {
  9461. for (int k = 0; k < cell; k++) {
  9462. *(frame_offset + k) = dynamic_offset + k;
  9463. }
  9464. }
  9465. else {
  9466. *frame_offset = dynamic_offset;
  9467. }
  9468. }
  9469. else {
  9470. loader_ctx->frame_offset = frame_offset;
  9471. loader_ctx->dynamic_offset = dynamic_offset;
  9472. if (!(wasm_loader_push_frame_offset(
  9473. loader_ctx, return_types[i], disable_emit,
  9474. operand_offset, error_buf, error_buf_size))) {
  9475. wasm_runtime_free(emit_data);
  9476. goto fail;
  9477. }
  9478. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9479. loader_ctx->frame_offset = frame_offset_org;
  9480. loader_ctx->dynamic_offset = dynamic_offset_org;
  9481. }
  9482. }
  9483. bh_assert(j == value_count);
  9484. /* Emit the cells, src_offsets and dst_offsets */
  9485. for (j = 0; j < value_count; j++)
  9486. emit_byte(loader_ctx, cells[j]);
  9487. for (j = 0; j < value_count; j++)
  9488. emit_operand(loader_ctx, src_offsets[j]);
  9489. for (j = 0; j < value_count; j++)
  9490. emit_operand(loader_ctx, dst_offsets[j]);
  9491. if (opcode == WASM_OP_ELSE)
  9492. emit_label(opcode);
  9493. wasm_runtime_free(emit_data);
  9494. }
  9495. return true;
  9496. fail:
  9497. return false;
  9498. }
  9499. #endif /* WASM_ENABLE_FAST_INTERP */
  9500. #define PUSH_TYPE(type) \
  9501. do { \
  9502. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9503. error_buf_size))) \
  9504. goto fail; \
  9505. } while (0)
  9506. #define POP_TYPE(type) \
  9507. do { \
  9508. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9509. error_buf_size))) \
  9510. goto fail; \
  9511. } while (0)
  9512. #if WASM_ENABLE_GC == 0
  9513. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9514. do { \
  9515. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9516. _start_addr, error_buf, \
  9517. error_buf_size)) \
  9518. goto fail; \
  9519. } while (0)
  9520. #define POP_CSP() \
  9521. do { \
  9522. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9523. goto fail; \
  9524. } while (0)
  9525. #else
  9526. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9527. do { \
  9528. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9529. _start_addr, error_buf, \
  9530. error_buf_size)) \
  9531. goto fail; \
  9532. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9533. error_buf, error_buf_size)) { \
  9534. goto fail; \
  9535. } \
  9536. } while (0)
  9537. #define POP_CSP() \
  9538. do { \
  9539. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9540. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9541. goto fail; \
  9542. } while (0)
  9543. #endif /* end of WASM_ENABLE_GC == 0 */
  9544. #if WASM_ENABLE_GC == 0
  9545. #define GET_LOCAL_REFTYPE() (void)0
  9546. #else
  9547. #define GET_LOCAL_REFTYPE() \
  9548. do { \
  9549. if (wasm_is_type_multi_byte_type(local_type)) { \
  9550. WASMRefType *_ref_type; \
  9551. if (local_idx < param_count) \
  9552. _ref_type = wasm_reftype_map_find( \
  9553. param_reftype_maps, param_reftype_map_count, local_idx); \
  9554. else \
  9555. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9556. local_reftype_map_count, \
  9557. local_idx - param_count); \
  9558. bh_assert(_ref_type); \
  9559. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9560. wasm_reftype_struct_size(_ref_type)); \
  9561. } \
  9562. } while (0)
  9563. #endif /* end of WASM_ENABLE_GC == 0 */
  9564. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9565. do { \
  9566. read_leb_uint32(p, p_end, local_idx); \
  9567. if (local_idx >= param_count + local_count) { \
  9568. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9569. goto fail; \
  9570. } \
  9571. local_type = local_idx < param_count \
  9572. ? param_types[local_idx] \
  9573. : local_types[local_idx - param_count]; \
  9574. local_offset = local_offsets[local_idx]; \
  9575. GET_LOCAL_REFTYPE(); \
  9576. } while (0)
  9577. static bool
  9578. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9579. {
  9580. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9581. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9582. return false;
  9583. }
  9584. return true;
  9585. }
  9586. #define CHECK_MEMORY() \
  9587. do { \
  9588. if (!check_memory(module, error_buf, error_buf_size)) \
  9589. goto fail; \
  9590. } while (0)
  9591. static bool
  9592. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9593. uint32 error_buf_size)
  9594. {
  9595. uint8 mem_access_aligns[] = {
  9596. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9597. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9598. };
  9599. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9600. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9601. set_error_buf(error_buf, error_buf_size,
  9602. "invalid memop flags: alignment must not be larger "
  9603. "than natural");
  9604. return false;
  9605. }
  9606. return true;
  9607. }
  9608. #if WASM_ENABLE_SIMD != 0
  9609. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9610. || (WASM_ENABLE_FAST_INTERP != 0)
  9611. static bool
  9612. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9613. uint32 error_buf_size)
  9614. {
  9615. uint8 mem_access_aligns[] = {
  9616. 4, /* load */
  9617. 3, 3, 3, 3, 3, 3, /* load and extend */
  9618. 0, 1, 2, 3, /* load and splat */
  9619. 4, /* store */
  9620. };
  9621. uint8 mem_access_aligns_load_lane[] = {
  9622. 0, 1, 2, 3, /* load lane */
  9623. 0, 1, 2, 3, /* store lane */
  9624. 2, 3 /* store zero */
  9625. };
  9626. if (!((opcode <= SIMD_v128_store)
  9627. || (SIMD_v128_load8_lane <= opcode
  9628. && opcode <= SIMD_v128_load64_zero))) {
  9629. set_error_buf(error_buf, error_buf_size,
  9630. "the opcode doesn't include memarg");
  9631. return false;
  9632. }
  9633. if ((opcode <= SIMD_v128_store
  9634. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9635. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9636. && align > mem_access_aligns_load_lane[opcode
  9637. - SIMD_v128_load8_lane])) {
  9638. set_error_buf(error_buf, error_buf_size,
  9639. "invalid memop flags: alignment must not be larger "
  9640. "than natural");
  9641. return false;
  9642. }
  9643. return true;
  9644. }
  9645. static bool
  9646. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9647. uint32 error_buf_size)
  9648. {
  9649. switch (opcode) {
  9650. case SIMD_i8x16_extract_lane_s:
  9651. case SIMD_i8x16_extract_lane_u:
  9652. case SIMD_i8x16_replace_lane:
  9653. if (lane >= 16) {
  9654. goto fail;
  9655. }
  9656. break;
  9657. case SIMD_i16x8_extract_lane_s:
  9658. case SIMD_i16x8_extract_lane_u:
  9659. case SIMD_i16x8_replace_lane:
  9660. if (lane >= 8) {
  9661. goto fail;
  9662. }
  9663. break;
  9664. case SIMD_i32x4_extract_lane:
  9665. case SIMD_i32x4_replace_lane:
  9666. case SIMD_f32x4_extract_lane:
  9667. case SIMD_f32x4_replace_lane:
  9668. if (lane >= 4) {
  9669. goto fail;
  9670. }
  9671. break;
  9672. case SIMD_i64x2_extract_lane:
  9673. case SIMD_i64x2_replace_lane:
  9674. case SIMD_f64x2_extract_lane:
  9675. case SIMD_f64x2_replace_lane:
  9676. if (lane >= 2) {
  9677. goto fail;
  9678. }
  9679. break;
  9680. case SIMD_v128_load8_lane:
  9681. case SIMD_v128_load16_lane:
  9682. case SIMD_v128_load32_lane:
  9683. case SIMD_v128_load64_lane:
  9684. case SIMD_v128_store8_lane:
  9685. case SIMD_v128_store16_lane:
  9686. case SIMD_v128_store32_lane:
  9687. case SIMD_v128_store64_lane:
  9688. case SIMD_v128_load32_zero:
  9689. case SIMD_v128_load64_zero:
  9690. {
  9691. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9692. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9693. goto fail;
  9694. }
  9695. break;
  9696. }
  9697. default:
  9698. goto fail;
  9699. }
  9700. return true;
  9701. fail:
  9702. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9703. return false;
  9704. }
  9705. static bool
  9706. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9707. {
  9708. uint8 i;
  9709. for (i = 0; i != 16; ++i) {
  9710. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9711. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9712. return false;
  9713. }
  9714. }
  9715. return true;
  9716. }
  9717. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9718. #endif /* end of WASM_ENABLE_SIMD */
  9719. #if WASM_ENABLE_SHARED_MEMORY != 0
  9720. static bool
  9721. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9722. uint32 error_buf_size)
  9723. {
  9724. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9725. uint8 mem_access_aligns[] = {
  9726. 2, 3, 0, 1, 0, 1, 2,
  9727. };
  9728. uint8 expect;
  9729. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9730. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9731. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9732. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9733. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9734. }
  9735. else {
  9736. /* 7 opcodes in every group */
  9737. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9738. }
  9739. if (align != expect) {
  9740. set_error_buf(error_buf, error_buf_size,
  9741. "alignment isn't equal to natural");
  9742. return false;
  9743. }
  9744. return true;
  9745. }
  9746. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9747. static bool
  9748. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9749. char *error_buf, uint32 error_buf_size)
  9750. {
  9751. BranchBlock *target_block, *cur_block;
  9752. BlockType *target_block_type;
  9753. uint8 type, *types = NULL, *frame_ref;
  9754. uint32 arity = 0;
  9755. int32 i, available_stack_cell;
  9756. uint16 cell_num;
  9757. #if WASM_ENABLE_GC != 0
  9758. WASMRefTypeMap *frame_reftype_map;
  9759. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9760. WASMRefType *ref_type;
  9761. uint32 reftype_map_count = 0;
  9762. int32 available_reftype_map;
  9763. bool is_type_multi_byte;
  9764. #endif
  9765. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9766. uint8 *frame_ref_after_popped = NULL;
  9767. uint8 frame_ref_tmp[4] = { 0 };
  9768. uint8 *frame_ref_buf = frame_ref_tmp;
  9769. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9770. #if WASM_ENABLE_GC != 0
  9771. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9772. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9773. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9774. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9775. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9776. #endif
  9777. #if WASM_ENABLE_FAST_INTERP != 0
  9778. int16 *frame_offset_old = loader_ctx->frame_offset;
  9779. int16 *frame_offset_after_popped = NULL;
  9780. int16 frame_offset_tmp[4] = { 0 };
  9781. int16 *frame_offset_buf = frame_offset_tmp;
  9782. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9783. #endif
  9784. bool ret = false;
  9785. bh_assert(loader_ctx->csp_num > 0);
  9786. if (loader_ctx->csp_num - 1 < depth) {
  9787. set_error_buf(error_buf, error_buf_size,
  9788. "unknown label, "
  9789. "unexpected end of section or function");
  9790. return false;
  9791. }
  9792. cur_block = loader_ctx->frame_csp - 1;
  9793. target_block = loader_ctx->frame_csp - (depth + 1);
  9794. target_block_type = &target_block->block_type;
  9795. frame_ref = loader_ctx->frame_ref;
  9796. #if WASM_ENABLE_GC != 0
  9797. frame_reftype_map = loader_ctx->frame_reftype_map;
  9798. #endif
  9799. /* Note: loop's arity is different from if and block. loop's arity is
  9800. * its parameter count while if and block arity is result count.
  9801. */
  9802. #if WASM_ENABLE_GC == 0
  9803. if (target_block->label_type == LABEL_TYPE_LOOP)
  9804. arity = block_type_get_param_types(target_block_type, &types);
  9805. else
  9806. arity = block_type_get_result_types(target_block_type, &types);
  9807. #else
  9808. if (target_block->label_type == LABEL_TYPE_LOOP)
  9809. arity = block_type_get_param_types(target_block_type, &types,
  9810. &reftype_maps, &reftype_map_count);
  9811. else
  9812. arity = block_type_get_result_types(target_block_type, &types,
  9813. &reftype_maps, &reftype_map_count);
  9814. #endif
  9815. /* If the stack is in polymorphic state, just clear the stack
  9816. * and then re-push the values to make the stack top values
  9817. * match block type. */
  9818. if (cur_block->is_stack_polymorphic) {
  9819. #if WASM_ENABLE_GC != 0
  9820. int32 j = (int32)reftype_map_count - 1;
  9821. #endif
  9822. for (i = (int32)arity - 1; i >= 0; i--) {
  9823. #if WASM_ENABLE_GC != 0
  9824. if (wasm_is_type_multi_byte_type(types[i])) {
  9825. bh_assert(reftype_maps[j].index == i);
  9826. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9827. reftype_maps[j].ref_type,
  9828. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9829. j--;
  9830. }
  9831. #endif
  9832. #if WASM_ENABLE_FAST_INTERP != 0
  9833. POP_OFFSET_TYPE(types[i]);
  9834. #endif
  9835. POP_TYPE(types[i]);
  9836. }
  9837. /* Backup stack data since it may be changed in the below
  9838. push operations, and the stack data may be used when
  9839. checking other target blocks of opcode br_table */
  9840. if (opcode == WASM_OP_BR_TABLE) {
  9841. uint64 total_size;
  9842. frame_ref_after_popped = loader_ctx->frame_ref;
  9843. total_size = (uint64)sizeof(uint8)
  9844. * (frame_ref_old - frame_ref_after_popped);
  9845. if (total_size > sizeof(frame_ref_tmp)
  9846. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9847. error_buf_size))) {
  9848. goto fail;
  9849. }
  9850. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9851. frame_ref_after_popped, (uint32)total_size);
  9852. #if WASM_ENABLE_GC != 0
  9853. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9854. total_size =
  9855. (uint64)sizeof(WASMRefTypeMap)
  9856. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9857. if (total_size > sizeof(frame_reftype_map_tmp)
  9858. && !(frame_reftype_map_buf = loader_malloc(
  9859. total_size, error_buf, error_buf_size))) {
  9860. goto fail;
  9861. }
  9862. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9863. frame_reftype_map_after_popped, (uint32)total_size);
  9864. #endif
  9865. #if WASM_ENABLE_FAST_INTERP != 0
  9866. frame_offset_after_popped = loader_ctx->frame_offset;
  9867. total_size = (uint64)sizeof(int16)
  9868. * (frame_offset_old - frame_offset_after_popped);
  9869. if (total_size > sizeof(frame_offset_tmp)
  9870. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9871. error_buf_size))) {
  9872. goto fail;
  9873. }
  9874. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9875. frame_offset_after_popped, (uint32)total_size);
  9876. #endif
  9877. }
  9878. #if WASM_ENABLE_GC != 0
  9879. j = 0;
  9880. #endif
  9881. for (i = 0; i < (int32)arity; i++) {
  9882. #if WASM_ENABLE_GC != 0
  9883. if (wasm_is_type_multi_byte_type(types[i])) {
  9884. bh_assert(reftype_maps[j].index == i);
  9885. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9886. reftype_maps[j].ref_type,
  9887. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9888. j++;
  9889. }
  9890. #endif
  9891. #if WASM_ENABLE_FAST_INTERP != 0
  9892. bool disable_emit = true;
  9893. int16 operand_offset = 0;
  9894. PUSH_OFFSET_TYPE(types[i]);
  9895. #endif
  9896. PUSH_TYPE(types[i]);
  9897. }
  9898. #if WASM_ENABLE_FAST_INTERP != 0
  9899. emit_br_info(target_block, opcode == WASM_OP_BR);
  9900. #endif
  9901. /* Restore the stack data, note that frame_ref_bottom,
  9902. frame_reftype_map_bottom, frame_offset_bottom may be
  9903. re-allocated in the above push operations */
  9904. if (opcode == WASM_OP_BR_TABLE) {
  9905. uint32 total_size;
  9906. /* The stack operand num should not be smaller than before
  9907. after pop and push operations */
  9908. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9909. loader_ctx->stack_cell_num = stack_cell_num_old;
  9910. loader_ctx->frame_ref =
  9911. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9912. total_size = (uint32)(sizeof(uint8)
  9913. * (frame_ref_old - frame_ref_after_popped));
  9914. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9915. frame_ref_buf, total_size);
  9916. #if WASM_ENABLE_GC != 0
  9917. /* The stack operand num should not be smaller than before
  9918. after pop and push operations */
  9919. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9920. loader_ctx->reftype_map_num = reftype_map_num_old;
  9921. loader_ctx->frame_reftype_map =
  9922. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9923. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9924. * (frame_reftype_map_old
  9925. - frame_reftype_map_after_popped));
  9926. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9927. total_size, frame_reftype_map_buf, total_size);
  9928. #endif
  9929. #if WASM_ENABLE_FAST_INTERP != 0
  9930. loader_ctx->frame_offset =
  9931. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9932. total_size =
  9933. (uint32)(sizeof(int16)
  9934. * (frame_offset_old - frame_offset_after_popped));
  9935. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9936. total_size, frame_offset_buf, total_size);
  9937. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9938. #endif
  9939. }
  9940. ret = true;
  9941. goto cleanup_and_return;
  9942. }
  9943. available_stack_cell =
  9944. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9945. #if WASM_ENABLE_GC != 0
  9946. available_reftype_map =
  9947. (int32)(loader_ctx->reftype_map_num
  9948. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9949. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9950. #endif
  9951. /* Check stack top values match target block type */
  9952. for (i = (int32)arity - 1; i >= 0; i--) {
  9953. type = types[i];
  9954. #if WASM_ENABLE_GC != 0
  9955. ref_type = NULL;
  9956. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9957. if (is_type_multi_byte) {
  9958. bh_assert(reftype_map);
  9959. ref_type = reftype_map->ref_type;
  9960. }
  9961. #endif
  9962. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9963. break;
  9964. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9965. #if WASM_ENABLE_GC != 0
  9966. frame_reftype_map, available_reftype_map,
  9967. #endif
  9968. type,
  9969. #if WASM_ENABLE_GC != 0
  9970. ref_type,
  9971. #endif
  9972. error_buf, error_buf_size)) {
  9973. goto fail;
  9974. }
  9975. cell_num = wasm_value_type_cell_num(types[i]);
  9976. frame_ref -= cell_num;
  9977. available_stack_cell -= cell_num;
  9978. #if WASM_ENABLE_GC != 0
  9979. if (is_type_multi_byte) {
  9980. frame_reftype_map--;
  9981. available_reftype_map--;
  9982. reftype_map--;
  9983. }
  9984. #endif
  9985. }
  9986. #if WASM_ENABLE_FAST_INTERP != 0
  9987. emit_br_info(target_block, opcode == WASM_OP_BR);
  9988. #endif
  9989. ret = true;
  9990. cleanup_and_return:
  9991. fail:
  9992. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9993. wasm_runtime_free(frame_ref_buf);
  9994. #if WASM_ENABLE_GC != 0
  9995. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9996. wasm_runtime_free(frame_reftype_map_buf);
  9997. #endif
  9998. #if WASM_ENABLE_FAST_INTERP != 0
  9999. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  10000. wasm_runtime_free(frame_offset_buf);
  10001. #endif
  10002. return ret;
  10003. }
  10004. static BranchBlock *
  10005. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  10006. uint8 opcode, char *error_buf, uint32 error_buf_size)
  10007. {
  10008. uint8 *p = *p_buf, *p_end = buf_end;
  10009. BranchBlock *frame_csp_tmp;
  10010. uint32 depth;
  10011. read_leb_uint32(p, p_end, depth);
  10012. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  10013. error_buf_size)) {
  10014. goto fail;
  10015. }
  10016. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  10017. *p_buf = p;
  10018. return frame_csp_tmp;
  10019. fail:
  10020. return NULL;
  10021. }
  10022. #if WASM_ENABLE_EXCE_HANDLING != 0
  10023. static BranchBlock *
  10024. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  10025. uint8 *buf_end, char *error_buf,
  10026. uint32 error_buf_size)
  10027. {
  10028. uint8 *p = *p_buf, *p_end = buf_end;
  10029. BranchBlock *frame_csp_tmp;
  10030. uint32 depth;
  10031. read_leb_uint32(p, p_end, depth);
  10032. /*
  10033. * Note: "delegate 0" means the surrounding block, not the
  10034. * try-delegate block itself.
  10035. *
  10036. * Note: the caller hasn't popped the try-delegate frame yet.
  10037. */
  10038. bh_assert(loader_ctx->csp_num > 0);
  10039. if (loader_ctx->csp_num - 1 <= depth) {
  10040. #if WASM_ENABLE_SPEC_TEST == 0
  10041. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  10042. #else
  10043. set_error_buf(error_buf, error_buf_size, "unknown label");
  10044. #endif
  10045. goto fail;
  10046. }
  10047. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  10048. #if WASM_ENABLE_FAST_INTERP != 0
  10049. emit_br_info(frame_csp_tmp, false);
  10050. #endif
  10051. *p_buf = p;
  10052. return frame_csp_tmp;
  10053. fail:
  10054. return NULL;
  10055. }
  10056. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10057. static bool
  10058. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  10059. char *error_buf, uint32 error_buf_size)
  10060. {
  10061. BlockType *block_type = &block->block_type;
  10062. uint8 *return_types = NULL;
  10063. uint32 return_count = 0;
  10064. int32 available_stack_cell, return_cell_num, i;
  10065. uint8 *frame_ref = NULL;
  10066. #if WASM_ENABLE_GC != 0
  10067. WASMRefTypeMap *frame_reftype_map;
  10068. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  10069. WASMRefType *ref_type;
  10070. uint32 param_count, return_reftype_map_count = 0;
  10071. int32 available_reftype_map =
  10072. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  10073. #endif
  10074. available_stack_cell =
  10075. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  10076. #if WASM_ENABLE_GC == 0
  10077. return_count = block_type_get_result_types(block_type, &return_types);
  10078. #else
  10079. return_count = block_type_get_result_types(block_type, &return_types,
  10080. &return_reftype_maps,
  10081. &return_reftype_map_count);
  10082. param_count =
  10083. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  10084. (void)param_count;
  10085. #endif
  10086. return_cell_num =
  10087. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  10088. /* If the stack is in polymorphic state, just clear the stack
  10089. * and then re-push the values to make the stack top values
  10090. * match block type. */
  10091. if (block->is_stack_polymorphic) {
  10092. #if WASM_ENABLE_GC != 0
  10093. int32 j = (int32)return_reftype_map_count - 1;
  10094. #endif
  10095. for (i = (int32)return_count - 1; i >= 0; i--) {
  10096. #if WASM_ENABLE_GC != 0
  10097. if (wasm_is_type_multi_byte_type(return_types[i])) {
  10098. bh_assert(return_reftype_maps[j].index == i + param_count);
  10099. bh_memcpy_s(
  10100. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  10101. return_reftype_maps[j].ref_type,
  10102. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  10103. j--;
  10104. }
  10105. #endif
  10106. #if WASM_ENABLE_FAST_INTERP != 0
  10107. POP_OFFSET_TYPE(return_types[i]);
  10108. #endif
  10109. POP_TYPE(return_types[i]);
  10110. }
  10111. /* Check stack is empty */
  10112. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  10113. set_error_buf(
  10114. error_buf, error_buf_size,
  10115. "type mismatch: stack size does not match block type");
  10116. goto fail;
  10117. }
  10118. #if WASM_ENABLE_GC != 0
  10119. j = 0;
  10120. #endif
  10121. for (i = 0; i < (int32)return_count; i++) {
  10122. #if WASM_ENABLE_GC != 0
  10123. if (wasm_is_type_multi_byte_type(return_types[i])) {
  10124. bh_assert(return_reftype_maps[j].index == i + param_count);
  10125. bh_memcpy_s(
  10126. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  10127. return_reftype_maps[j].ref_type,
  10128. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  10129. j++;
  10130. }
  10131. #endif
  10132. #if WASM_ENABLE_FAST_INTERP != 0
  10133. bool disable_emit = true;
  10134. int16 operand_offset = 0;
  10135. PUSH_OFFSET_TYPE(return_types[i]);
  10136. #endif
  10137. PUSH_TYPE(return_types[i]);
  10138. }
  10139. return true;
  10140. }
  10141. if (available_stack_cell != return_cell_num) {
  10142. #if WASM_ENABLE_EXCE_HANDLING != 0
  10143. /* testspec: this error message format is expected by try_catch.wast */
  10144. snprintf(
  10145. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  10146. block->label_type == LABEL_TYPE_TRY
  10147. || (block->label_type == LABEL_TYPE_CATCH
  10148. && return_cell_num > 0)
  10149. ? "instruction"
  10150. : "block",
  10151. return_cell_num > 0 ? type2str(return_types[0]) : "",
  10152. " but stack has ",
  10153. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  10154. : "");
  10155. goto fail;
  10156. #else
  10157. set_error_buf(error_buf, error_buf_size,
  10158. "type mismatch: stack size does not match block type");
  10159. goto fail;
  10160. #endif
  10161. }
  10162. /* Check stack values match return types */
  10163. frame_ref = loader_ctx->frame_ref;
  10164. #if WASM_ENABLE_GC != 0
  10165. frame_reftype_map = loader_ctx->frame_reftype_map;
  10166. return_reftype_map =
  10167. return_reftype_map_count
  10168. ? return_reftype_maps + return_reftype_map_count - 1
  10169. : NULL;
  10170. #endif
  10171. for (i = (int32)return_count - 1; i >= 0; i--) {
  10172. uint8 type = return_types[i];
  10173. #if WASM_ENABLE_GC != 0
  10174. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  10175. ref_type = NULL;
  10176. if (is_type_multi_byte) {
  10177. bh_assert(return_reftype_map);
  10178. ref_type = return_reftype_map->ref_type;
  10179. }
  10180. #endif
  10181. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  10182. #if WASM_ENABLE_GC != 0
  10183. frame_reftype_map, available_reftype_map,
  10184. #endif
  10185. type,
  10186. #if WASM_ENABLE_GC != 0
  10187. ref_type,
  10188. #endif
  10189. error_buf, error_buf_size))
  10190. return false;
  10191. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  10192. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  10193. #if WASM_ENABLE_GC != 0
  10194. if (is_type_multi_byte) {
  10195. frame_reftype_map--;
  10196. available_reftype_map--;
  10197. return_reftype_map--;
  10198. }
  10199. #endif
  10200. }
  10201. return true;
  10202. fail:
  10203. return false;
  10204. }
  10205. #if WASM_ENABLE_FAST_INTERP != 0
  10206. /* Copy parameters to dynamic space.
  10207. * 1) POP original parameter out;
  10208. * 2) Push and copy original values to dynamic space.
  10209. * The copy instruction format:
  10210. * Part a: param count
  10211. * Part b: all param total cell num
  10212. * Part c: each param's cell_num, src offset and dst offset
  10213. * Part d: each param's src offset
  10214. * Part e: each param's dst offset
  10215. */
  10216. static bool
  10217. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  10218. uint32 error_buf_size)
  10219. {
  10220. bool ret = false;
  10221. int16 *frame_offset = NULL;
  10222. uint8 *cells = NULL, cell;
  10223. int16 *src_offsets = NULL;
  10224. uint8 *emit_data = NULL;
  10225. uint32 i;
  10226. BranchBlock *block = loader_ctx->frame_csp - 1;
  10227. BlockType *block_type = &block->block_type;
  10228. WASMFuncType *wasm_type = block_type->u.type;
  10229. uint32 param_count = block_type->u.type->param_count;
  10230. int16 condition_offset = 0;
  10231. bool disable_emit = false;
  10232. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  10233. int16 operand_offset = 0;
  10234. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  10235. bh_assert(size > 0);
  10236. /* For if block, we also need copy the condition operand offset. */
  10237. if (is_if_block)
  10238. size += sizeof(*cells) + sizeof(*src_offsets);
  10239. /* Allocate memory for the emit data */
  10240. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  10241. return false;
  10242. cells = emit_data;
  10243. src_offsets = (int16 *)(cells + param_count);
  10244. if (is_if_block)
  10245. condition_offset = *loader_ctx->frame_offset;
  10246. /* POP original parameter out */
  10247. for (i = 0; i < param_count; i++) {
  10248. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  10249. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  10250. }
  10251. frame_offset = loader_ctx->frame_offset;
  10252. /* Get each param's cell num and src offset */
  10253. for (i = 0; i < param_count; i++) {
  10254. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  10255. cells[i] = cell;
  10256. src_offsets[i] = *frame_offset;
  10257. frame_offset += cell;
  10258. }
  10259. /* emit copy instruction */
  10260. emit_label(EXT_OP_COPY_STACK_VALUES);
  10261. /* Part a) */
  10262. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  10263. /* Part b) */
  10264. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  10265. : wasm_type->param_cell_num);
  10266. /* Part c) */
  10267. for (i = 0; i < param_count; i++)
  10268. emit_byte(loader_ctx, cells[i]);
  10269. if (is_if_block)
  10270. emit_byte(loader_ctx, 1);
  10271. /* Part d) */
  10272. for (i = 0; i < param_count; i++)
  10273. emit_operand(loader_ctx, src_offsets[i]);
  10274. if (is_if_block)
  10275. emit_operand(loader_ctx, condition_offset);
  10276. /* Since the start offset to save the block's params and
  10277. * the start offset to save the block's results may be
  10278. * different, we remember the dynamic offset for loop block
  10279. * so that we can use it to copy the stack operands to the
  10280. * loop block's params in wasm_loader_emit_br_info. */
  10281. if (block->label_type == LABEL_TYPE_LOOP)
  10282. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10283. /* Part e) */
  10284. /* Push to dynamic space. The push will emit the dst offset. */
  10285. for (i = 0; i < param_count; i++)
  10286. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  10287. if (is_if_block)
  10288. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  10289. ret = true;
  10290. fail:
  10291. /* Free the emit data */
  10292. wasm_runtime_free(emit_data);
  10293. return ret;
  10294. }
  10295. #endif
  10296. #if WASM_ENABLE_GC == 0
  10297. #define RESET_REFTYPE_MAP_STACK() (void)0
  10298. #else
  10299. #define RESET_REFTYPE_MAP_STACK() \
  10300. do { \
  10301. loader_ctx->reftype_map_num = \
  10302. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  10303. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  10304. + loader_ctx->reftype_map_num; \
  10305. } while (0)
  10306. #endif
  10307. /* reset the stack to the state of before entering the last block */
  10308. #if WASM_ENABLE_FAST_INTERP != 0
  10309. #define RESET_STACK() \
  10310. do { \
  10311. loader_ctx->stack_cell_num = \
  10312. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10313. loader_ctx->frame_ref = \
  10314. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10315. loader_ctx->frame_offset = \
  10316. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  10317. RESET_REFTYPE_MAP_STACK(); \
  10318. } while (0)
  10319. #else
  10320. #define RESET_STACK() \
  10321. do { \
  10322. loader_ctx->stack_cell_num = \
  10323. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10324. loader_ctx->frame_ref = \
  10325. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10326. RESET_REFTYPE_MAP_STACK(); \
  10327. } while (0)
  10328. #endif
  10329. /* set current block's stack polymorphic state */
  10330. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  10331. do { \
  10332. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  10333. _cur_block->is_stack_polymorphic = flag; \
  10334. } while (0)
  10335. #define BLOCK_HAS_PARAM(block_type) \
  10336. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  10337. #define PRESERVE_LOCAL_FOR_BLOCK() \
  10338. do { \
  10339. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  10340. error_buf_size))) { \
  10341. goto fail; \
  10342. } \
  10343. } while (0)
  10344. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10345. static bool
  10346. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  10347. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  10348. uint32 error_buf_size)
  10349. {
  10350. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  10351. return false;
  10352. }
  10353. if (table_idx < module->import_table_count) {
  10354. if (p_elem_type)
  10355. *p_elem_type =
  10356. module->import_tables[table_idx].u.table.table_type.elem_type;
  10357. #if WASM_ENABLE_GC != 0
  10358. if (p_ref_type)
  10359. *((WASMRefType **)p_ref_type) =
  10360. module->import_tables[table_idx]
  10361. .u.table.table_type.elem_ref_type;
  10362. #endif
  10363. }
  10364. else {
  10365. if (p_elem_type)
  10366. *p_elem_type =
  10367. module->tables[table_idx - module->import_table_count]
  10368. .table_type.elem_type;
  10369. #if WASM_ENABLE_GC != 0
  10370. if (p_ref_type)
  10371. *((WASMRefType **)p_ref_type) =
  10372. module->tables[table_idx - module->import_table_count]
  10373. .table_type.elem_ref_type;
  10374. #endif
  10375. }
  10376. return true;
  10377. }
  10378. static bool
  10379. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  10380. uint8 *p_elem_type, void **p_elem_ref_type,
  10381. char *error_buf, uint32 error_buf_size)
  10382. {
  10383. if (table_seg_idx >= module->table_seg_count) {
  10384. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  10385. table_seg_idx);
  10386. return false;
  10387. }
  10388. if (p_elem_type) {
  10389. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  10390. }
  10391. #if WASM_ENABLE_GC != 0
  10392. if (p_elem_ref_type)
  10393. *((WASMRefType **)p_elem_ref_type) =
  10394. module->table_segments[table_seg_idx].elem_ref_type;
  10395. #endif
  10396. return true;
  10397. }
  10398. #endif
  10399. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  10400. const uint8 *
  10401. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  10402. uint32 *len)
  10403. {
  10404. WASMCustomSection *section = module->custom_section_list;
  10405. while (section) {
  10406. if ((section->name_len == strlen(name))
  10407. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  10408. if (len) {
  10409. *len = section->content_len;
  10410. }
  10411. return section->content_addr;
  10412. }
  10413. section = section->next;
  10414. }
  10415. return NULL;
  10416. }
  10417. #endif
  10418. #if 0
  10419. #define HANDLE_OPCODE(opcode) #opcode
  10420. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  10421. #undef HANDLE_OPCODE
  10422. #endif
  10423. #if WASM_ENABLE_FAST_INTERP == 0
  10424. #define pb_read_leb_uint32 read_leb_uint32
  10425. #define pb_read_leb_int32 read_leb_int32
  10426. #define pb_read_leb_int64 read_leb_int64
  10427. #define pb_read_leb_memarg read_leb_memarg
  10428. #define pb_read_leb_mem_offset read_leb_mem_offset
  10429. #else
  10430. /* Read leb without malformed format check */
  10431. static uint64
  10432. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  10433. {
  10434. uint8 *buf = *p_buf;
  10435. uint64 result = 0, byte = 0;
  10436. uint32 shift = 0;
  10437. do {
  10438. byte = *buf++;
  10439. result |= ((byte & 0x7f) << shift);
  10440. shift += 7;
  10441. } while (byte & 0x80);
  10442. if (sign && (shift < maxbits) && (byte & 0x40)) {
  10443. /* Sign extend */
  10444. result |= (~((uint64)0)) << shift;
  10445. }
  10446. *p_buf = buf;
  10447. return result;
  10448. }
  10449. #define pb_read_leb_uint32(p, p_end, res) \
  10450. do { \
  10451. if (!loader_ctx->p_code_compiled) \
  10452. /* Enable format check in the first scan */ \
  10453. read_leb_uint32(p, p_end, res); \
  10454. else \
  10455. /* Disable format check in the second scan */ \
  10456. res = (uint32)read_leb_quick(&p, 32, false); \
  10457. } while (0)
  10458. #define pb_read_leb_int32(p, p_end, res) \
  10459. do { \
  10460. if (!loader_ctx->p_code_compiled) \
  10461. /* Enable format check in the first scan */ \
  10462. read_leb_int32(p, p_end, res); \
  10463. else \
  10464. /* Disable format check in the second scan */ \
  10465. res = (int32)read_leb_quick(&p, 32, true); \
  10466. } while (0)
  10467. #define pb_read_leb_int64(p, p_end, res) \
  10468. do { \
  10469. if (!loader_ctx->p_code_compiled) \
  10470. /* Enable format check in the first scan */ \
  10471. read_leb_int64(p, p_end, res); \
  10472. else \
  10473. /* Disable format check in the second scan */ \
  10474. res = (int64)read_leb_quick(&p, 64, true); \
  10475. } while (0)
  10476. #if WASM_ENABLE_MULTI_MEMORY != 0
  10477. #define pb_read_leb_memarg read_leb_memarg
  10478. #else
  10479. #define pb_read_leb_memarg pb_read_leb_uint32
  10480. #endif
  10481. #if WASM_ENABLE_MEMORY64 != 0
  10482. #define pb_read_leb_mem_offset read_leb_mem_offset
  10483. #else
  10484. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10485. #endif
  10486. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10487. static bool
  10488. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10489. uint32 cur_func_idx, char *error_buf,
  10490. uint32 error_buf_size)
  10491. {
  10492. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10493. uint32 param_count, local_count, global_count;
  10494. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10495. table_elem_idx_type;
  10496. BlockType func_block_type;
  10497. uint16 *local_offsets, local_offset;
  10498. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10499. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10500. mem_offset_t mem_offset;
  10501. int32 i32_const = 0;
  10502. int64 i64_const;
  10503. uint8 opcode;
  10504. bool return_value = false;
  10505. WASMLoaderContext *loader_ctx;
  10506. BranchBlock *frame_csp_tmp;
  10507. #if WASM_ENABLE_GC != 0
  10508. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10509. uint32 param_reftype_map_count, local_reftype_map_count;
  10510. int32 heap_type;
  10511. WASMRefType wasm_ref_type = { 0 };
  10512. bool need_ref_type_map;
  10513. #endif
  10514. #if WASM_ENABLE_FAST_INTERP != 0
  10515. int16 operand_offset = 0;
  10516. uint8 last_op = 0;
  10517. bool disable_emit, preserve_local = false, if_condition_available = true;
  10518. float32 f32_const;
  10519. float64 f64_const;
  10520. /*
  10521. * It means that the fast interpreter detected an exception while preparing,
  10522. * typically near the block opcode, but it did not immediately trigger
  10523. * the exception. The loader should be capable of identifying it near
  10524. * the end opcode and then raising the exception.
  10525. */
  10526. bool pending_exception = false;
  10527. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10528. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10529. #endif
  10530. #if WASM_ENABLE_MEMORY64 != 0
  10531. bool is_memory64 = has_module_memory64(module);
  10532. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10533. #else
  10534. mem_offset_type = VALUE_TYPE_I32;
  10535. table_elem_idx_type = VALUE_TYPE_I32;
  10536. #endif
  10537. uint32 memidx;
  10538. global_count = module->import_global_count + module->global_count;
  10539. param_count = func->func_type->param_count;
  10540. param_types = func->func_type->types;
  10541. func_block_type.is_value_type = false;
  10542. func_block_type.u.type = func->func_type;
  10543. local_count = func->local_count;
  10544. local_types = func->local_types;
  10545. local_offsets = func->local_offsets;
  10546. #if WASM_ENABLE_GC != 0
  10547. param_reftype_maps = func->func_type->ref_type_maps;
  10548. param_reftype_map_count = func->func_type->ref_type_map_count;
  10549. local_reftype_maps = func->local_ref_type_maps;
  10550. local_reftype_map_count = func->local_ref_type_map_count;
  10551. #endif
  10552. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10553. goto fail;
  10554. }
  10555. #if WASM_ENABLE_GC != 0
  10556. loader_ctx->module = module;
  10557. loader_ctx->ref_type_set = module->ref_type_set;
  10558. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10559. #endif
  10560. #if WASM_ENABLE_FAST_INTERP != 0
  10561. /* For the first traverse, the initial value of preserved_local_offset has
  10562. * not been determined, we use the INT16_MAX to represent that a slot has
  10563. * been copied to preserve space. For second traverse, this field will be
  10564. * set to the appropriate value in wasm_loader_ctx_reinit.
  10565. * This is for Issue #1230,
  10566. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10567. * drop opcodes need to know which slots are preserved, so those slots will
  10568. * not be treated as dynamically allocated slots */
  10569. loader_ctx->preserved_local_offset = INT16_MAX;
  10570. re_scan:
  10571. if (loader_ctx->code_compiled_size > 0) {
  10572. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10573. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10574. goto fail;
  10575. }
  10576. p = func->code;
  10577. func->code_compiled = loader_ctx->p_code_compiled;
  10578. func->code_compiled_size = loader_ctx->code_compiled_size;
  10579. if (loader_ctx->i64_const_num > 0) {
  10580. int64 *i64_consts_old = loader_ctx->i64_consts;
  10581. /* Sort the i64 consts */
  10582. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10583. cmp_i64_const);
  10584. /* Remove the duplicated i64 consts */
  10585. uint32 k = 1;
  10586. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10587. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10588. i64_consts_old[k++] = i64_consts_old[i];
  10589. }
  10590. }
  10591. if (k < loader_ctx->i64_const_num) {
  10592. int64 *i64_consts_new;
  10593. /* Try to reallocate memory with a smaller size */
  10594. if ((i64_consts_new =
  10595. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10596. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10597. i64_consts_old, (uint32)sizeof(int64) * k);
  10598. /* Free the old memory */
  10599. wasm_runtime_free(i64_consts_old);
  10600. loader_ctx->i64_consts = i64_consts_new;
  10601. loader_ctx->i64_const_max_num = k;
  10602. }
  10603. loader_ctx->i64_const_num = k;
  10604. }
  10605. }
  10606. if (loader_ctx->v128_const_num > 0) {
  10607. V128 *v128_consts_old = loader_ctx->v128_consts;
  10608. /* Sort the v128 consts */
  10609. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10610. cmp_v128_const);
  10611. /* Remove the duplicated v128 consts */
  10612. uint32 k = 1;
  10613. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10614. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10615. sizeof(V128))
  10616. == 0)) {
  10617. v128_consts_old[k++] = v128_consts_old[i];
  10618. }
  10619. }
  10620. if (k < loader_ctx->v128_const_num) {
  10621. V128 *v128_consts_new;
  10622. /* Try to reallocate memory with a smaller size */
  10623. if ((v128_consts_new =
  10624. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10625. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10626. v128_consts_old, (uint32)sizeof(V128) * k);
  10627. /* Free the old memory */
  10628. wasm_runtime_free(v128_consts_old);
  10629. loader_ctx->v128_consts = v128_consts_new;
  10630. loader_ctx->v128_const_max_num = k;
  10631. }
  10632. loader_ctx->v128_const_num = k;
  10633. }
  10634. }
  10635. if (loader_ctx->i32_const_num > 0) {
  10636. int32 *i32_consts_old = loader_ctx->i32_consts;
  10637. /* Sort the i32 consts */
  10638. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10639. cmp_i32_const);
  10640. /* Remove the duplicated i32 consts */
  10641. uint32 k = 1;
  10642. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10643. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10644. i32_consts_old[k++] = i32_consts_old[i];
  10645. }
  10646. }
  10647. if (k < loader_ctx->i32_const_num) {
  10648. int32 *i32_consts_new;
  10649. /* Try to reallocate memory with a smaller size */
  10650. if ((i32_consts_new =
  10651. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10652. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10653. i32_consts_old, (uint32)sizeof(int32) * k);
  10654. /* Free the old memory */
  10655. wasm_runtime_free(i32_consts_old);
  10656. loader_ctx->i32_consts = i32_consts_new;
  10657. loader_ctx->i32_const_max_num = k;
  10658. }
  10659. loader_ctx->i32_const_num = k;
  10660. }
  10661. }
  10662. }
  10663. #endif
  10664. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10665. while (p < p_end) {
  10666. opcode = *p++;
  10667. #if WASM_ENABLE_FAST_INTERP != 0
  10668. p_org = p;
  10669. disable_emit = false;
  10670. emit_label(opcode);
  10671. #endif
  10672. switch (opcode) {
  10673. case WASM_OP_UNREACHABLE:
  10674. RESET_STACK();
  10675. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10676. break;
  10677. case WASM_OP_NOP:
  10678. #if WASM_ENABLE_FAST_INTERP != 0
  10679. skip_label();
  10680. #endif
  10681. break;
  10682. case WASM_OP_IF:
  10683. {
  10684. #if WASM_ENABLE_FAST_INTERP != 0
  10685. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10686. int32 available_stack_cell =
  10687. (int32)(loader_ctx->stack_cell_num
  10688. - parent_block->stack_cell_num);
  10689. if (available_stack_cell <= 0
  10690. && parent_block->is_stack_polymorphic)
  10691. if_condition_available = false;
  10692. else
  10693. if_condition_available = true;
  10694. PRESERVE_LOCAL_FOR_BLOCK();
  10695. #endif
  10696. #if WASM_ENABLE_GC == 0
  10697. POP_I32();
  10698. #endif
  10699. goto handle_op_block_and_loop;
  10700. }
  10701. case WASM_OP_BLOCK:
  10702. case WASM_OP_LOOP:
  10703. #if WASM_ENABLE_EXCE_HANDLING != 0
  10704. case WASM_OP_TRY:
  10705. if (opcode == WASM_OP_TRY) {
  10706. /*
  10707. * keep track of exception handlers to account for
  10708. * memory allocation
  10709. */
  10710. func->exception_handler_count++;
  10711. /*
  10712. * try is a block
  10713. * do nothing special, but execution continues to
  10714. * to handle_op_block_and_loop,
  10715. * and that be pushes the csp
  10716. */
  10717. }
  10718. #endif
  10719. #if WASM_ENABLE_FAST_INTERP != 0
  10720. PRESERVE_LOCAL_FOR_BLOCK();
  10721. #endif
  10722. handle_op_block_and_loop:
  10723. {
  10724. uint8 value_type;
  10725. BlockType block_type;
  10726. #if WASM_ENABLE_FAST_INTERP != 0
  10727. uint32 available_params = 0;
  10728. #endif
  10729. CHECK_BUF(p, p_end, 1);
  10730. value_type = read_uint8(p);
  10731. if (is_byte_a_type(value_type)) {
  10732. /* If the first byte is one of these special values:
  10733. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10734. * the single return value. */
  10735. block_type.is_value_type = true;
  10736. block_type.u.value_type.type = value_type;
  10737. #if WASM_ENABLE_WAMR_COMPILER != 0
  10738. if (value_type == VALUE_TYPE_V128)
  10739. module->is_simd_used = true;
  10740. else if (value_type == VALUE_TYPE_FUNCREF
  10741. || value_type == VALUE_TYPE_EXTERNREF)
  10742. module->is_ref_types_used = true;
  10743. #endif
  10744. #if WASM_ENABLE_GC != 0
  10745. if (value_type != VALUE_TYPE_VOID) {
  10746. p_org = p;
  10747. p--;
  10748. if (!resolve_value_type((const uint8 **)&p, p_end,
  10749. module, module->type_count,
  10750. &need_ref_type_map,
  10751. &wasm_ref_type, false,
  10752. error_buf, error_buf_size)) {
  10753. goto fail;
  10754. }
  10755. if (need_ref_type_map) {
  10756. block_type.u.value_type.ref_type_map.index = 0;
  10757. if (!(block_type.u.value_type.ref_type_map
  10758. .ref_type = reftype_set_insert(
  10759. module->ref_type_set, &wasm_ref_type,
  10760. error_buf, error_buf_size))) {
  10761. goto fail;
  10762. }
  10763. }
  10764. /* Set again as the type might be changed, e.g.
  10765. (ref null any) to anyref */
  10766. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10767. #if WASM_ENABLE_FAST_INTERP == 0
  10768. while (p_org < p) {
  10769. #if WASM_ENABLE_DEBUG_INTERP != 0
  10770. if (!record_fast_op(module, p_org, *p_org,
  10771. error_buf, error_buf_size)) {
  10772. goto fail;
  10773. }
  10774. #endif
  10775. /* Ignore extra bytes for interpreter */
  10776. *p_org++ = WASM_OP_NOP;
  10777. }
  10778. #endif
  10779. }
  10780. #endif /* end of WASM_ENABLE_GC != 0 */
  10781. }
  10782. else {
  10783. int32 type_index;
  10784. /* Resolve the leb128 encoded type index as block type */
  10785. p--;
  10786. p_org = p - 1;
  10787. pb_read_leb_int32(p, p_end, type_index);
  10788. if (!check_function_type(module, type_index, error_buf,
  10789. error_buf_size)) {
  10790. goto fail;
  10791. }
  10792. block_type.is_value_type = false;
  10793. block_type.u.type =
  10794. (WASMFuncType *)module->types[type_index];
  10795. #if WASM_ENABLE_FAST_INTERP == 0
  10796. /* If block use type index as block type, change the opcode
  10797. * to new extended opcode so that interpreter can resolve
  10798. * the block quickly.
  10799. */
  10800. #if WASM_ENABLE_DEBUG_INTERP != 0
  10801. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10802. error_buf_size)) {
  10803. goto fail;
  10804. }
  10805. #endif
  10806. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10807. #endif
  10808. }
  10809. #if WASM_ENABLE_GC != 0
  10810. if (opcode == WASM_OP_IF) {
  10811. POP_I32();
  10812. }
  10813. #endif
  10814. /* Pop block parameters from stack */
  10815. if (BLOCK_HAS_PARAM(block_type)) {
  10816. WASMFuncType *wasm_type = block_type.u.type;
  10817. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10818. #if WASM_ENABLE_FAST_INTERP != 0
  10819. uint32 cell_num;
  10820. available_params = block_type.u.type->param_count;
  10821. #endif
  10822. for (i = 0; i < block_type.u.type->param_count; i++) {
  10823. int32 available_stack_cell =
  10824. (int32)(loader_ctx->stack_cell_num
  10825. - cur_block->stack_cell_num);
  10826. if (available_stack_cell <= 0
  10827. && cur_block->is_stack_polymorphic) {
  10828. #if WASM_ENABLE_FAST_INTERP != 0
  10829. available_params = i;
  10830. #endif
  10831. break;
  10832. }
  10833. POP_TYPE(
  10834. wasm_type->types[wasm_type->param_count - i - 1]);
  10835. #if WASM_ENABLE_FAST_INTERP != 0
  10836. /* decrease the frame_offset pointer accordingly to keep
  10837. * consistent with frame_ref stack */
  10838. cell_num = wasm_value_type_cell_num(
  10839. wasm_type->types[wasm_type->param_count - i - 1]);
  10840. loader_ctx->frame_offset -= cell_num;
  10841. if (loader_ctx->frame_offset
  10842. < loader_ctx->frame_offset_bottom) {
  10843. LOG_DEBUG(
  10844. "frame_offset underflow, roll back and "
  10845. "let following stack checker report it\n");
  10846. loader_ctx->frame_offset += cell_num;
  10847. pending_exception = true;
  10848. break;
  10849. }
  10850. #endif
  10851. }
  10852. }
  10853. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10854. block_type, p);
  10855. /* Pass parameters to block */
  10856. if (BLOCK_HAS_PARAM(block_type)) {
  10857. WASMFuncType *func_type = block_type.u.type;
  10858. #if WASM_ENABLE_GC != 0
  10859. WASMRefType *ref_type;
  10860. uint32 j = 0;
  10861. #endif
  10862. for (i = 0; i < func_type->param_count; i++) {
  10863. #if WASM_ENABLE_FAST_INTERP != 0
  10864. uint32 cell_num =
  10865. wasm_value_type_cell_num(func_type->types[i]);
  10866. if (i >= available_params) {
  10867. /* make sure enough space */
  10868. if (loader_ctx->p_code_compiled == NULL) {
  10869. loader_ctx->frame_offset += cell_num;
  10870. if (!check_offset_push(loader_ctx, error_buf,
  10871. error_buf_size))
  10872. goto fail;
  10873. /* for following dummy value assignment */
  10874. loader_ctx->frame_offset -= cell_num;
  10875. }
  10876. /* If there isn't enough data on stack, push a dummy
  10877. * offset to keep the stack consistent with
  10878. * frame_ref.
  10879. * Since the stack is already in polymorphic state,
  10880. * the opcode will not be executed, so the dummy
  10881. * offset won't cause any error */
  10882. for (uint32 n = 0; n < cell_num; n++) {
  10883. *loader_ctx->frame_offset++ = 0;
  10884. }
  10885. }
  10886. else {
  10887. loader_ctx->frame_offset += cell_num;
  10888. }
  10889. #endif
  10890. #if WASM_ENABLE_GC != 0
  10891. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10892. bh_assert(func_type->ref_type_maps[j].index == i);
  10893. ref_type = func_type->ref_type_maps[j].ref_type;
  10894. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10895. ref_type,
  10896. wasm_reftype_struct_size(ref_type));
  10897. j++;
  10898. }
  10899. #endif
  10900. PUSH_TYPE(func_type->types[i]);
  10901. }
  10902. }
  10903. #if WASM_ENABLE_FAST_INTERP != 0
  10904. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10905. skip_label();
  10906. if (BLOCK_HAS_PARAM(block_type)) {
  10907. /* Make sure params are in dynamic space */
  10908. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10909. error_buf_size))
  10910. goto fail;
  10911. }
  10912. if (opcode == WASM_OP_LOOP) {
  10913. (loader_ctx->frame_csp - 1)->code_compiled =
  10914. loader_ctx->p_code_compiled;
  10915. }
  10916. }
  10917. #if WASM_ENABLE_EXCE_HANDLING != 0
  10918. else if (opcode == WASM_OP_TRY) {
  10919. skip_label();
  10920. }
  10921. #endif
  10922. else if (opcode == WASM_OP_IF) {
  10923. BranchBlock *block = loader_ctx->frame_csp - 1;
  10924. /* If block has parameters, we should make sure they are in
  10925. * dynamic space. Otherwise, when else branch is missing,
  10926. * the later opcode may consume incorrect operand offset.
  10927. * Spec case:
  10928. * (func (export "params-id") (param i32) (result i32)
  10929. * (i32.const 1)
  10930. * (i32.const 2)
  10931. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10932. * (then)) (i32.add)
  10933. * )
  10934. *
  10935. * So we should emit a copy instruction before the if.
  10936. *
  10937. * And we also need to save the parameter offsets and
  10938. * recover them before entering else branch.
  10939. *
  10940. */
  10941. if (BLOCK_HAS_PARAM(block_type)) {
  10942. uint64 size;
  10943. /* In polymorphic state, there may be no if condition on
  10944. * the stack, so the offset may not emitted */
  10945. if (if_condition_available) {
  10946. /* skip the if condition operand offset */
  10947. wasm_loader_emit_backspace(loader_ctx,
  10948. sizeof(int16));
  10949. }
  10950. /* skip the if label */
  10951. skip_label();
  10952. /* Emit a copy instruction */
  10953. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10954. error_buf_size))
  10955. goto fail;
  10956. /* Emit the if instruction */
  10957. emit_label(opcode);
  10958. /* Emit the new condition operand offset */
  10959. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10960. /* Save top param_count values of frame_offset stack, so
  10961. * that we can recover it before executing else branch
  10962. */
  10963. size = sizeof(int16)
  10964. * (uint64)block_type.u.type->param_cell_num;
  10965. if (!(block->param_frame_offsets = loader_malloc(
  10966. size, error_buf, error_buf_size)))
  10967. goto fail;
  10968. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10969. loader_ctx->frame_offset
  10970. - size / sizeof(int16),
  10971. (uint32)size);
  10972. }
  10973. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10974. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10975. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10976. }
  10977. #endif
  10978. break;
  10979. }
  10980. #if WASM_ENABLE_EXCE_HANDLING != 0
  10981. case WASM_OP_THROW:
  10982. {
  10983. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10984. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10985. uint8 label_type = cur_block->label_type;
  10986. uint32 tag_index = 0;
  10987. pb_read_leb_int32(p, p_end, tag_index);
  10988. /* check validity of tag_index against module->tag_count */
  10989. /* check tag index is within the tag index space */
  10990. if (tag_index >= module->import_tag_count + module->tag_count) {
  10991. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10992. tag_index);
  10993. goto fail;
  10994. }
  10995. /* the tag_type is stored in either the WASMTag (section tags)
  10996. * or WASMTagImport (import tag) */
  10997. WASMFuncType *tag_type = NULL;
  10998. if (tag_index < module->import_tag_count) {
  10999. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  11000. }
  11001. else {
  11002. tag_type =
  11003. module->tags[tag_index - module->import_tag_count]
  11004. ->tag_type;
  11005. }
  11006. if (tag_type->result_count != 0) {
  11007. set_error_buf(error_buf, error_buf_size,
  11008. "tag type signature does not return void");
  11009. goto fail;
  11010. }
  11011. int32 available_stack_cell =
  11012. (int32)(loader_ctx->stack_cell_num
  11013. - cur_block->stack_cell_num);
  11014. int32 tti;
  11015. /* Check stack values match return types by comparing tag param
  11016. * types with stack cells */
  11017. uint8 *frame_ref = loader_ctx->frame_ref;
  11018. #if WASM_ENABLE_GC != 0
  11019. WASMRefTypeMap *frame_reftype_map =
  11020. loader_ctx->frame_reftype_map;
  11021. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  11022. /* Temporarily set these values since they may be used in
  11023. GET_LOCAL_REFTYPE(), remember they must be restored later */
  11024. param_reftype_maps = tag_type->ref_type_maps;
  11025. /* For tag_type function, it shouldn't have result_count = 0 */
  11026. param_reftype_map_count = tag_type->ref_type_map_count;
  11027. param_count = tag_type->param_count;
  11028. #endif
  11029. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  11030. #if WASM_ENABLE_GC != 0
  11031. local_type = tag_type->types[tti];
  11032. local_idx = tti;
  11033. /* Get the wasm_ref_type if the local_type is multibyte
  11034. type */
  11035. GET_LOCAL_REFTYPE();
  11036. #endif
  11037. if (!check_stack_top_values(
  11038. loader_ctx, frame_ref, available_stack_cell,
  11039. #if WASM_ENABLE_GC != 0
  11040. frame_reftype_map, frame_reftype_map_num,
  11041. #endif
  11042. tag_type->types[tti],
  11043. #if WASM_ENABLE_GC != 0
  11044. &wasm_ref_type,
  11045. #endif
  11046. error_buf, error_buf_size)) {
  11047. snprintf(error_buf, error_buf_size,
  11048. "type mismatch: instruction requires [%s] but "
  11049. "stack has [%s]",
  11050. tag_type->param_count > 0
  11051. ? type2str(tag_type->types[tti])
  11052. : "",
  11053. available_stack_cell > 0
  11054. ? type2str(*(loader_ctx->frame_ref - 1))
  11055. : "");
  11056. goto fail;
  11057. }
  11058. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  11059. available_stack_cell -=
  11060. wasm_value_type_cell_num(tag_type->types[tti]);
  11061. }
  11062. #if WASM_ENABLE_GC != 0
  11063. /* Restore the values */
  11064. param_reftype_maps = func->func_type->ref_type_maps;
  11065. param_reftype_map_count = func->func_type->ref_type_map_count;
  11066. param_count = func->func_type->param_count;
  11067. #endif
  11068. /* throw is stack polymorphic */
  11069. (void)label_type;
  11070. RESET_STACK();
  11071. break;
  11072. }
  11073. case WASM_OP_RETHROW:
  11074. {
  11075. /* must be done before checking branch block */
  11076. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11077. /* check the target catching block: LABEL_TYPE_CATCH */
  11078. if (!(frame_csp_tmp =
  11079. check_branch_block(loader_ctx, &p, p_end, opcode,
  11080. error_buf, error_buf_size)))
  11081. goto fail;
  11082. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  11083. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  11084. /* trap according to spectest (rethrow.wast) */
  11085. set_error_buf(error_buf, error_buf_size,
  11086. "invalid rethrow label");
  11087. goto fail;
  11088. }
  11089. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11090. uint8 label_type = cur_block->label_type;
  11091. (void)label_type;
  11092. /* rethrow is stack polymorphic */
  11093. RESET_STACK();
  11094. break;
  11095. }
  11096. case WASM_OP_DELEGATE:
  11097. {
  11098. /* check target block is valid */
  11099. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  11100. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  11101. goto fail;
  11102. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11103. uint8 label_type = cur_block->label_type;
  11104. (void)label_type;
  11105. /* DELEGATE ends the block */
  11106. POP_CSP();
  11107. break;
  11108. }
  11109. case WASM_OP_CATCH:
  11110. {
  11111. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11112. uint8 label_type = cur_block->label_type;
  11113. uint32 tag_index = 0;
  11114. pb_read_leb_int32(p, p_end, tag_index);
  11115. /* check validity of tag_index against module->tag_count */
  11116. /* check tag index is within the tag index space */
  11117. if (tag_index >= module->import_tag_count + module->tag_count) {
  11118. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  11119. __FUNCTION__);
  11120. set_error_buf(error_buf, error_buf_size, "unknown tag");
  11121. goto fail;
  11122. }
  11123. /* the tag_type is stored in either the WASMTag (section tags)
  11124. * or WASMTagImport (import tag) */
  11125. WASMFuncType *func_type = NULL;
  11126. if (tag_index < module->import_tag_count) {
  11127. func_type = module->import_tags[tag_index].u.tag.tag_type;
  11128. }
  11129. else {
  11130. func_type =
  11131. module->tags[tag_index - module->import_tag_count]
  11132. ->tag_type;
  11133. }
  11134. if (func_type->result_count != 0) {
  11135. set_error_buf(error_buf, error_buf_size,
  11136. "tag type signature does not return void");
  11137. goto fail;
  11138. }
  11139. /* check validity of current label (expect LABEL_TYPE_TRY or
  11140. * LABEL_TYPE_CATCH) */
  11141. if ((LABEL_TYPE_CATCH != label_type)
  11142. && (LABEL_TYPE_TRY != label_type)) {
  11143. set_error_buf(error_buf, error_buf_size,
  11144. "Unexpected block sequence encountered.");
  11145. goto fail;
  11146. }
  11147. /*
  11148. * replace frame_csp by LABEL_TYPE_CATCH
  11149. */
  11150. cur_block->label_type = LABEL_TYPE_CATCH;
  11151. /* RESET_STACK removes the values pushed in TRY or previous
  11152. * CATCH Blocks */
  11153. RESET_STACK();
  11154. #if WASM_ENABLE_GC != 0
  11155. WASMRefType *ref_type;
  11156. uint32 j = 0;
  11157. #endif
  11158. /* push types on the stack according to caught type */
  11159. for (i = 0; i < func_type->param_count; i++) {
  11160. #if WASM_ENABLE_GC != 0
  11161. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  11162. bh_assert(func_type->ref_type_maps[j].index == i);
  11163. ref_type = func_type->ref_type_maps[j].ref_type;
  11164. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11165. ref_type,
  11166. wasm_reftype_struct_size(ref_type));
  11167. j++;
  11168. }
  11169. #endif
  11170. PUSH_TYPE(func_type->types[i]);
  11171. }
  11172. break;
  11173. }
  11174. case WASM_OP_CATCH_ALL:
  11175. {
  11176. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11177. /* expecting a TRY or CATCH, anything else will be considered an
  11178. * error */
  11179. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  11180. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  11181. set_error_buf(error_buf, error_buf_size,
  11182. "Unexpected block sequence encountered.");
  11183. goto fail;
  11184. }
  11185. /* no immediates */
  11186. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  11187. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  11188. /* RESET_STACK removes the values pushed in TRY or previous
  11189. * CATCH Blocks */
  11190. RESET_STACK();
  11191. /* catch_all has no tagtype and therefore no parameters */
  11192. break;
  11193. }
  11194. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  11195. case WASM_OP_ELSE:
  11196. handle_op_else:
  11197. {
  11198. BranchBlock *block = NULL;
  11199. BlockType block_type;
  11200. if (loader_ctx->csp_num < 2
  11201. /* the matched if isn't found */
  11202. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  11203. /* duplicated else is found */
  11204. || (loader_ctx->frame_csp - 1)->else_addr) {
  11205. set_error_buf(
  11206. error_buf, error_buf_size,
  11207. "opcode else found without matched opcode if");
  11208. goto fail;
  11209. }
  11210. block = loader_ctx->frame_csp - 1;
  11211. /* check whether if branch's stack matches its result type */
  11212. if (!check_block_stack(loader_ctx, block, error_buf,
  11213. error_buf_size))
  11214. goto fail;
  11215. block->else_addr = p - 1;
  11216. block_type = block->block_type;
  11217. #if WASM_ENABLE_GC != 0
  11218. if (!wasm_loader_init_local_use_masks(
  11219. loader_ctx, local_count, error_buf, error_buf_size)) {
  11220. goto fail;
  11221. }
  11222. #endif
  11223. #if WASM_ENABLE_FAST_INTERP != 0
  11224. /* if the result of if branch is in local or const area, add a
  11225. * copy op */
  11226. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11227. error_buf, error_buf_size)) {
  11228. goto fail;
  11229. }
  11230. emit_empty_label_addr_and_frame_ip(PATCH_END);
  11231. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  11232. #endif
  11233. RESET_STACK();
  11234. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  11235. /* Pass parameters to if-false branch */
  11236. if (BLOCK_HAS_PARAM(block_type)) {
  11237. for (i = 0; i < block_type.u.type->param_count; i++)
  11238. PUSH_TYPE(block_type.u.type->types[i]);
  11239. }
  11240. #if WASM_ENABLE_FAST_INTERP != 0
  11241. /* Recover top param_count values of frame_offset stack */
  11242. if (BLOCK_HAS_PARAM((block_type))) {
  11243. uint32 size;
  11244. size = sizeof(int16) * block_type.u.type->param_cell_num;
  11245. bh_memcpy_s(loader_ctx->frame_offset, size,
  11246. block->param_frame_offsets, size);
  11247. loader_ctx->frame_offset += (size / sizeof(int16));
  11248. }
  11249. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  11250. #endif
  11251. break;
  11252. }
  11253. case WASM_OP_END:
  11254. {
  11255. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11256. /* check whether block stack matches its result type */
  11257. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  11258. error_buf_size))
  11259. goto fail;
  11260. /* if there is no else branch, make a virtual else opcode for
  11261. easier integrity check and to copy the correct results to
  11262. the block return address for fast-interp mode:
  11263. change if block from `if ... end` to `if ... else end` */
  11264. if (cur_block->label_type == LABEL_TYPE_IF
  11265. && !cur_block->else_addr) {
  11266. opcode = WASM_OP_ELSE;
  11267. p--;
  11268. #if WASM_ENABLE_FAST_INTERP != 0
  11269. p_org = p;
  11270. skip_label();
  11271. disable_emit = false;
  11272. emit_label(opcode);
  11273. #endif
  11274. goto handle_op_else;
  11275. }
  11276. POP_CSP();
  11277. #if WASM_ENABLE_FAST_INTERP != 0
  11278. skip_label();
  11279. /* copy the result to the block return address */
  11280. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11281. error_buf, error_buf_size)) {
  11282. /* it could be tmp frame_csp allocated from opcode like
  11283. * OP_BR and not counted in loader_ctx->csp_num, it won't
  11284. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  11285. * to free the loader_ctx->frame_csp if fails */
  11286. free_label_patch_list(loader_ctx->frame_csp);
  11287. goto fail;
  11288. }
  11289. apply_label_patch(loader_ctx, 0, PATCH_END);
  11290. free_label_patch_list(loader_ctx->frame_csp);
  11291. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  11292. int32 idx;
  11293. uint8 ret_type;
  11294. emit_label(WASM_OP_RETURN);
  11295. for (idx = (int32)func->func_type->result_count - 1;
  11296. idx >= 0; idx--) {
  11297. ret_type = *(func->func_type->types
  11298. + func->func_type->param_count + idx);
  11299. POP_OFFSET_TYPE(ret_type);
  11300. }
  11301. }
  11302. #endif
  11303. if (loader_ctx->csp_num > 0) {
  11304. loader_ctx->frame_csp->end_addr = p - 1;
  11305. }
  11306. else {
  11307. /* end of function block, function will return */
  11308. if (p < p_end) {
  11309. set_error_buf(error_buf, error_buf_size,
  11310. "section size mismatch");
  11311. goto fail;
  11312. }
  11313. }
  11314. #if WASM_ENABLE_FAST_INTERP != 0
  11315. if (pending_exception) {
  11316. set_error_buf(
  11317. error_buf, error_buf_size,
  11318. "There is a pending exception needs to be handled");
  11319. goto fail;
  11320. }
  11321. #endif
  11322. break;
  11323. }
  11324. case WASM_OP_BR:
  11325. {
  11326. if (!(frame_csp_tmp =
  11327. check_branch_block(loader_ctx, &p, p_end, opcode,
  11328. error_buf, error_buf_size)))
  11329. goto fail;
  11330. RESET_STACK();
  11331. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11332. break;
  11333. }
  11334. case WASM_OP_BR_IF:
  11335. {
  11336. POP_I32();
  11337. if (!(frame_csp_tmp =
  11338. check_branch_block(loader_ctx, &p, p_end, opcode,
  11339. error_buf, error_buf_size)))
  11340. goto fail;
  11341. break;
  11342. }
  11343. case WASM_OP_BR_TABLE:
  11344. {
  11345. uint32 depth = 0, default_arity, arity = 0;
  11346. BranchBlock *target_block;
  11347. BlockType *target_block_type;
  11348. #if WASM_ENABLE_FAST_INTERP == 0
  11349. BrTableCache *br_table_cache = NULL;
  11350. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  11351. uint32 j;
  11352. #endif
  11353. pb_read_leb_uint32(p, p_end, count);
  11354. #if WASM_ENABLE_FAST_INTERP != 0
  11355. emit_uint32(loader_ctx, count);
  11356. #endif
  11357. POP_I32();
  11358. /* Get each depth and check it */
  11359. p_org = p;
  11360. for (i = 0; i <= count; i++) {
  11361. pb_read_leb_uint32(p, p_end, depth);
  11362. bh_assert(loader_ctx->csp_num > 0);
  11363. if (loader_ctx->csp_num - 1 < depth) {
  11364. set_error_buf(error_buf, error_buf_size,
  11365. "unknown label, "
  11366. "unexpected end of section or function");
  11367. goto fail;
  11368. }
  11369. }
  11370. p = p_org;
  11371. /* Get the default block's arity */
  11372. target_block = loader_ctx->frame_csp - (depth + 1);
  11373. target_block_type = &target_block->block_type;
  11374. default_arity = block_type_get_arity(target_block_type,
  11375. target_block->label_type);
  11376. #if WASM_ENABLE_FAST_INTERP == 0
  11377. p_depth_begin = p_depth = p;
  11378. #endif
  11379. for (i = 0; i <= count; i++) {
  11380. p_org = p;
  11381. pb_read_leb_uint32(p, p_end, depth);
  11382. p = p_org;
  11383. /* Get the target block's arity and check it */
  11384. target_block = loader_ctx->frame_csp - (depth + 1);
  11385. target_block_type = &target_block->block_type;
  11386. arity = block_type_get_arity(target_block_type,
  11387. target_block->label_type);
  11388. if (arity != default_arity) {
  11389. set_error_buf(error_buf, error_buf_size,
  11390. "type mismatch: br_table targets must "
  11391. "all use same result type");
  11392. goto fail;
  11393. }
  11394. if (!(frame_csp_tmp =
  11395. check_branch_block(loader_ctx, &p, p_end, opcode,
  11396. error_buf, error_buf_size))) {
  11397. goto fail;
  11398. }
  11399. #if WASM_ENABLE_FAST_INTERP == 0
  11400. if (br_table_cache) {
  11401. br_table_cache->br_depths[i] = depth;
  11402. }
  11403. else {
  11404. if (depth > 255) {
  11405. /* The depth cannot be stored in one byte,
  11406. create br_table cache to store each depth */
  11407. #if WASM_ENABLE_DEBUG_INTERP != 0
  11408. if (!record_fast_op(module, p_opcode, *p_opcode,
  11409. error_buf, error_buf_size)) {
  11410. goto fail;
  11411. }
  11412. #endif
  11413. if (!(br_table_cache = loader_malloc(
  11414. offsetof(BrTableCache, br_depths)
  11415. + sizeof(uint32)
  11416. * (uint64)(count + 1),
  11417. error_buf, error_buf_size))) {
  11418. goto fail;
  11419. }
  11420. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  11421. br_table_cache->br_table_op_addr = p_opcode;
  11422. br_table_cache->br_count = count;
  11423. /* Copy previous depths which are one byte */
  11424. for (j = 0; j < i; j++) {
  11425. br_table_cache->br_depths[j] = p_depth_begin[j];
  11426. }
  11427. br_table_cache->br_depths[i] = depth;
  11428. bh_list_insert(module->br_table_cache_list,
  11429. br_table_cache);
  11430. }
  11431. else {
  11432. /* The depth can be stored in one byte, use the
  11433. byte of the leb to store it */
  11434. *p_depth++ = (uint8)depth;
  11435. }
  11436. }
  11437. #endif
  11438. }
  11439. #if WASM_ENABLE_FAST_INTERP == 0
  11440. /* Set the tailing bytes to nop */
  11441. if (br_table_cache)
  11442. p_depth = p_depth_begin;
  11443. while (p_depth < p)
  11444. *p_depth++ = WASM_OP_NOP;
  11445. #endif
  11446. RESET_STACK();
  11447. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11448. break;
  11449. }
  11450. case WASM_OP_RETURN:
  11451. {
  11452. WASMFuncType *func_type = func->func_type;
  11453. int32 idx;
  11454. uint8 ret_type;
  11455. #if WASM_ENABLE_GC != 0
  11456. uint32 j = func_type->ref_type_map_count - 1;
  11457. #endif
  11458. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11459. idx--) {
  11460. ret_type =
  11461. *(func_type->types + func_type->param_count + idx);
  11462. #if WASM_ENABLE_GC != 0
  11463. if (wasm_is_type_multi_byte_type(ret_type)) {
  11464. WASMRefType *ref_type =
  11465. func_type->ref_type_maps[j].ref_type;
  11466. bh_assert(func_type->ref_type_maps[j].index
  11467. == func_type->param_count + idx);
  11468. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11469. ref_type,
  11470. wasm_reftype_struct_size(ref_type));
  11471. j--;
  11472. }
  11473. #endif
  11474. #if WASM_ENABLE_FAST_INTERP != 0
  11475. /* emit the offset after return opcode */
  11476. POP_OFFSET_TYPE(ret_type);
  11477. #endif
  11478. POP_TYPE(ret_type);
  11479. }
  11480. RESET_STACK();
  11481. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11482. break;
  11483. }
  11484. case WASM_OP_CALL:
  11485. #if WASM_ENABLE_TAIL_CALL != 0
  11486. case WASM_OP_RETURN_CALL:
  11487. #endif
  11488. #if WASM_ENABLE_GC != 0
  11489. case WASM_OP_CALL_REF:
  11490. case WASM_OP_RETURN_CALL_REF:
  11491. #endif
  11492. {
  11493. WASMFuncType *func_type;
  11494. uint8 type;
  11495. int32 idx;
  11496. #if WASM_ENABLE_GC != 0
  11497. WASMRefType *ref_type;
  11498. uint32 type_idx1;
  11499. int32 j;
  11500. #endif
  11501. #if WASM_ENABLE_GC != 0
  11502. if (opcode == WASM_OP_CALL_REF
  11503. || opcode == WASM_OP_RETURN_CALL_REF) {
  11504. pb_read_leb_uint32(p, p_end, type_idx1);
  11505. if (!check_type_index(module, module->type_count, type_idx1,
  11506. error_buf, error_buf_size)) {
  11507. goto fail;
  11508. }
  11509. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11510. set_error_buf(error_buf, error_buf_size,
  11511. "unknown function type");
  11512. goto fail;
  11513. }
  11514. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11515. &type_idx, error_buf,
  11516. error_buf_size)) {
  11517. goto fail;
  11518. }
  11519. if (type == VALUE_TYPE_ANY) {
  11520. type_idx = type_idx1;
  11521. }
  11522. if (!check_type_index(module, module->type_count, type_idx,
  11523. error_buf, error_buf_size)) {
  11524. goto fail;
  11525. }
  11526. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11527. set_error_buf(error_buf, error_buf_size,
  11528. "unknown function type");
  11529. goto fail;
  11530. }
  11531. if (!wasm_func_type_is_super_of(
  11532. (WASMFuncType *)module->types[type_idx1],
  11533. (WASMFuncType *)module->types[type_idx])) {
  11534. set_error_buf(error_buf, error_buf_size,
  11535. "function type mismatch");
  11536. goto fail;
  11537. }
  11538. func_type = (WASMFuncType *)module->types[type_idx];
  11539. }
  11540. else
  11541. #endif
  11542. {
  11543. pb_read_leb_uint32(p, p_end, func_idx);
  11544. #if WASM_ENABLE_FAST_INTERP != 0
  11545. /* we need to emit func_idx before arguments */
  11546. emit_uint32(loader_ctx, func_idx);
  11547. #endif
  11548. if (!check_function_index(module, func_idx, error_buf,
  11549. error_buf_size)) {
  11550. goto fail;
  11551. }
  11552. if (func_idx < module->import_function_count)
  11553. func_type = module->import_functions[func_idx]
  11554. .u.function.func_type;
  11555. else
  11556. func_type =
  11557. module
  11558. ->functions[func_idx
  11559. - module->import_function_count]
  11560. ->func_type;
  11561. }
  11562. if (func_type->param_count > 0) {
  11563. #if WASM_ENABLE_GC != 0
  11564. j = (int32)(func_type->result_ref_type_maps
  11565. - func_type->ref_type_maps - 1);
  11566. #endif
  11567. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11568. idx--) {
  11569. #if WASM_ENABLE_GC != 0
  11570. if (wasm_is_type_multi_byte_type(
  11571. func_type->types[idx])) {
  11572. ref_type = func_type->ref_type_maps[j].ref_type;
  11573. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11574. ref_type,
  11575. wasm_reftype_struct_size(ref_type));
  11576. j--;
  11577. }
  11578. #endif
  11579. #if WASM_ENABLE_FAST_INTERP != 0
  11580. POP_OFFSET_TYPE(func_type->types[idx]);
  11581. #endif
  11582. POP_TYPE(func_type->types[idx]);
  11583. }
  11584. }
  11585. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11586. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11587. #endif
  11588. #if WASM_ENABLE_GC != 0
  11589. j = (int32)(func_type->result_ref_type_maps
  11590. - func_type->ref_type_maps);
  11591. #endif
  11592. for (i = 0; i < func_type->result_count; i++) {
  11593. #if WASM_ENABLE_GC != 0
  11594. if (wasm_is_type_multi_byte_type(
  11595. func_type->types[func_type->param_count + i])) {
  11596. ref_type = func_type->ref_type_maps[j].ref_type;
  11597. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11598. ref_type,
  11599. wasm_reftype_struct_size(ref_type));
  11600. j++;
  11601. }
  11602. #endif
  11603. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11604. #if WASM_ENABLE_FAST_INTERP != 0
  11605. /* Here we emit each return value's dynamic_offset. But
  11606. * in fact these offsets are continuous, so interpreter
  11607. * only need to get the first return value's offset.
  11608. */
  11609. PUSH_OFFSET_TYPE(
  11610. func_type->types[func_type->param_count + i]);
  11611. #endif
  11612. }
  11613. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11614. }
  11615. else {
  11616. #if WASM_ENABLE_GC == 0
  11617. if (func_type->result_count
  11618. != func->func_type->result_count) {
  11619. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11620. "type mismatch: expect ",
  11621. func->func_type->result_count,
  11622. " return values but got other");
  11623. goto fail;
  11624. }
  11625. for (i = 0; i < func_type->result_count; i++) {
  11626. type = func->func_type
  11627. ->types[func->func_type->param_count + i];
  11628. if (func_type->types[func_type->param_count + i]
  11629. != type) {
  11630. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11631. "type mismatch: expect ",
  11632. type2str(type), " but got other");
  11633. goto fail;
  11634. }
  11635. }
  11636. #else
  11637. if (!wasm_func_type_result_is_subtype_of(
  11638. func_type, func->func_type, module->types,
  11639. module->type_count)) {
  11640. set_error_buf(
  11641. error_buf, error_buf_size,
  11642. "type mismatch: invalid func result types");
  11643. goto fail;
  11644. }
  11645. #endif
  11646. RESET_STACK();
  11647. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11648. }
  11649. #endif
  11650. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11651. || WASM_ENABLE_WAMR_COMPILER != 0
  11652. func->has_op_func_call = true;
  11653. #endif
  11654. (void)type;
  11655. break;
  11656. }
  11657. /*
  11658. * if disable reference type: call_indirect typeidx, 0x00
  11659. * if enable reference type: call_indirect typeidx, tableidx
  11660. */
  11661. case WASM_OP_CALL_INDIRECT:
  11662. #if WASM_ENABLE_TAIL_CALL != 0
  11663. case WASM_OP_RETURN_CALL_INDIRECT:
  11664. #endif
  11665. {
  11666. int32 idx;
  11667. WASMFuncType *func_type;
  11668. uint32 tbl_elem_type;
  11669. #if WASM_ENABLE_GC != 0
  11670. WASMRefType *elem_ref_type = NULL;
  11671. #endif
  11672. pb_read_leb_uint32(p, p_end, type_idx);
  11673. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11674. #if WASM_ENABLE_WAMR_COMPILER != 0
  11675. if (p + 1 < p_end && *p != 0x00) {
  11676. /*
  11677. * Any non-0x00 byte requires the ref types proposal.
  11678. * This is different from checking the table_idx value
  11679. * since `0x80 0x00` etc. are all valid encodings of zero.
  11680. */
  11681. module->is_ref_types_used = true;
  11682. }
  11683. #endif
  11684. pb_read_leb_uint32(p, p_end, table_idx);
  11685. #else
  11686. CHECK_BUF(p, p_end, 1);
  11687. table_idx = read_uint8(p);
  11688. #endif
  11689. if (!check_table_index(module, table_idx, error_buf,
  11690. error_buf_size)) {
  11691. goto fail;
  11692. }
  11693. tbl_elem_type =
  11694. table_idx < module->import_table_count
  11695. ? module->import_tables[table_idx]
  11696. .u.table.table_type.elem_type
  11697. : module->tables[table_idx - module->import_table_count]
  11698. .table_type.elem_type;
  11699. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11700. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11701. set_error_buf_v(error_buf, error_buf_size,
  11702. "type mismatch: instruction requires table "
  11703. "of functions but table %u has externref",
  11704. table_idx);
  11705. goto fail;
  11706. }
  11707. #elif WASM_ENABLE_GC != 0
  11708. /* Table element must match type ref null func */
  11709. elem_ref_type =
  11710. table_idx < module->import_table_count
  11711. ? module->import_tables[table_idx]
  11712. .u.table.table_type.elem_ref_type
  11713. : module->tables[table_idx - module->import_table_count]
  11714. .table_type.elem_ref_type;
  11715. if (!wasm_reftype_is_subtype_of(
  11716. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11717. module->types, module->type_count)) {
  11718. set_error_buf_v(error_buf, error_buf_size,
  11719. "type mismatch: instruction requires "
  11720. "reference type t match type ref null func"
  11721. "in table %u",
  11722. table_idx);
  11723. goto fail;
  11724. }
  11725. #else
  11726. (void)tbl_elem_type;
  11727. #endif
  11728. #if WASM_ENABLE_FAST_INTERP != 0
  11729. /* we need to emit before arguments */
  11730. #if WASM_ENABLE_TAIL_CALL != 0
  11731. emit_byte(loader_ctx, opcode);
  11732. #endif
  11733. emit_uint32(loader_ctx, type_idx);
  11734. emit_uint32(loader_ctx, table_idx);
  11735. #endif
  11736. #if WASM_ENABLE_MEMORY64 != 0
  11737. table_elem_idx_type = is_table_64bit(module, table_idx)
  11738. ? VALUE_TYPE_I64
  11739. : VALUE_TYPE_I32;
  11740. #endif
  11741. /* skip elem idx */
  11742. POP_TBL_ELEM_IDX();
  11743. if (!check_function_type(module, type_idx, error_buf,
  11744. error_buf_size)) {
  11745. goto fail;
  11746. }
  11747. func_type = (WASMFuncType *)module->types[type_idx];
  11748. if (func_type->param_count > 0) {
  11749. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11750. idx--) {
  11751. #if WASM_ENABLE_FAST_INTERP != 0
  11752. POP_OFFSET_TYPE(func_type->types[idx]);
  11753. #endif
  11754. POP_TYPE(func_type->types[idx]);
  11755. }
  11756. }
  11757. #if WASM_ENABLE_TAIL_CALL != 0
  11758. if (opcode == WASM_OP_CALL_INDIRECT) {
  11759. #endif
  11760. for (i = 0; i < func_type->result_count; i++) {
  11761. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11762. #if WASM_ENABLE_FAST_INTERP != 0
  11763. PUSH_OFFSET_TYPE(
  11764. func_type->types[func_type->param_count + i]);
  11765. #endif
  11766. }
  11767. #if WASM_ENABLE_TAIL_CALL != 0
  11768. }
  11769. else {
  11770. uint8 type;
  11771. if (func_type->result_count
  11772. != func->func_type->result_count) {
  11773. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11774. "type mismatch: expect ",
  11775. func->func_type->result_count,
  11776. " return values but got other");
  11777. goto fail;
  11778. }
  11779. for (i = 0; i < func_type->result_count; i++) {
  11780. type = func->func_type
  11781. ->types[func->func_type->param_count + i];
  11782. if (func_type->types[func_type->param_count + i]
  11783. != type) {
  11784. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11785. "type mismatch: expect ",
  11786. type2str(type), " but got other");
  11787. goto fail;
  11788. }
  11789. }
  11790. RESET_STACK();
  11791. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11792. }
  11793. #endif
  11794. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11795. || WASM_ENABLE_WAMR_COMPILER != 0
  11796. func->has_op_func_call = true;
  11797. #endif
  11798. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11799. func->has_op_call_indirect = true;
  11800. #endif
  11801. break;
  11802. }
  11803. case WASM_OP_DROP:
  11804. {
  11805. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11806. int32 available_stack_cell =
  11807. (int32)(loader_ctx->stack_cell_num
  11808. - cur_block->stack_cell_num);
  11809. if (available_stack_cell <= 0
  11810. && !cur_block->is_stack_polymorphic) {
  11811. set_error_buf(error_buf, error_buf_size,
  11812. "type mismatch, opcode drop was found "
  11813. "but stack was empty");
  11814. goto fail;
  11815. }
  11816. if (available_stack_cell > 0) {
  11817. #if WASM_ENABLE_GC != 0
  11818. if (wasm_is_type_multi_byte_type(
  11819. *(loader_ctx->frame_ref - 1))) {
  11820. bh_assert((int32)(loader_ctx->reftype_map_num
  11821. - cur_block->reftype_map_num)
  11822. > 0);
  11823. loader_ctx->frame_reftype_map--;
  11824. loader_ctx->reftype_map_num--;
  11825. }
  11826. #endif
  11827. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11828. loader_ctx->frame_ref--;
  11829. loader_ctx->stack_cell_num--;
  11830. #if WASM_ENABLE_FAST_INTERP != 0
  11831. skip_label();
  11832. loader_ctx->frame_offset--;
  11833. if ((*(loader_ctx->frame_offset)
  11834. > loader_ctx->start_dynamic_offset)
  11835. && (*(loader_ctx->frame_offset)
  11836. < loader_ctx->max_dynamic_offset))
  11837. loader_ctx->dynamic_offset--;
  11838. #endif
  11839. }
  11840. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11841. loader_ctx->frame_ref -= 2;
  11842. loader_ctx->stack_cell_num -= 2;
  11843. #if WASM_ENABLE_FAST_INTERP == 0
  11844. *(p - 1) = WASM_OP_DROP_64;
  11845. #endif
  11846. #if WASM_ENABLE_FAST_INTERP != 0
  11847. skip_label();
  11848. loader_ctx->frame_offset -= 2;
  11849. if ((*(loader_ctx->frame_offset)
  11850. > loader_ctx->start_dynamic_offset)
  11851. && (*(loader_ctx->frame_offset)
  11852. < loader_ctx->max_dynamic_offset))
  11853. loader_ctx->dynamic_offset -= 2;
  11854. #endif
  11855. }
  11856. #if WASM_ENABLE_SIMD != 0
  11857. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11858. || (WASM_ENABLE_FAST_INTERP != 0)
  11859. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11860. loader_ctx->frame_ref -= 4;
  11861. loader_ctx->stack_cell_num -= 4;
  11862. #if WASM_ENABLE_FAST_INTERP != 0
  11863. skip_label();
  11864. loader_ctx->frame_offset -= 4;
  11865. if ((*(loader_ctx->frame_offset)
  11866. > loader_ctx->start_dynamic_offset)
  11867. && (*(loader_ctx->frame_offset)
  11868. < loader_ctx->max_dynamic_offset))
  11869. loader_ctx->dynamic_offset -= 4;
  11870. #endif
  11871. }
  11872. #endif
  11873. #endif
  11874. else {
  11875. set_error_buf(error_buf, error_buf_size,
  11876. "type mismatch");
  11877. goto fail;
  11878. }
  11879. }
  11880. else {
  11881. #if WASM_ENABLE_FAST_INTERP != 0
  11882. skip_label();
  11883. #endif
  11884. }
  11885. break;
  11886. }
  11887. case WASM_OP_SELECT:
  11888. {
  11889. uint8 ref_type;
  11890. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11891. int32 available_stack_cell;
  11892. #if WASM_ENABLE_FAST_INTERP != 0
  11893. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11894. #endif
  11895. POP_I32();
  11896. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11897. - cur_block->stack_cell_num);
  11898. if (available_stack_cell <= 0
  11899. && !cur_block->is_stack_polymorphic) {
  11900. set_error_buf(error_buf, error_buf_size,
  11901. "type mismatch or invalid result arity, "
  11902. "opcode select was found "
  11903. "but stack was empty");
  11904. goto fail;
  11905. }
  11906. if (available_stack_cell > 0) {
  11907. switch (*(loader_ctx->frame_ref - 1)) {
  11908. case VALUE_TYPE_I32:
  11909. case VALUE_TYPE_F32:
  11910. case VALUE_TYPE_ANY:
  11911. break;
  11912. case VALUE_TYPE_I64:
  11913. case VALUE_TYPE_F64:
  11914. #if WASM_ENABLE_FAST_INTERP == 0
  11915. *(p - 1) = WASM_OP_SELECT_64;
  11916. #else
  11917. if (loader_ctx->p_code_compiled) {
  11918. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11919. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11920. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11921. *(void **)(p_code_compiled_tmp
  11922. - sizeof(void *)) =
  11923. handle_table[opcode_tmp];
  11924. #elif UINTPTR_MAX == UINT64_MAX
  11925. /* emit int32 relative offset in 64-bit target
  11926. */
  11927. int32 offset =
  11928. (int32)((uint8 *)handle_table[opcode_tmp]
  11929. - (uint8 *)handle_table[0]);
  11930. *(int32 *)(p_code_compiled_tmp
  11931. - sizeof(int32)) = offset;
  11932. #else
  11933. /* emit uint32 label address in 32-bit target */
  11934. *(uint32 *)(p_code_compiled_tmp
  11935. - sizeof(uint32)) =
  11936. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11937. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11938. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11939. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11940. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11941. #else
  11942. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11943. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11944. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11945. }
  11946. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11947. break;
  11948. #if WASM_ENABLE_SIMD != 0
  11949. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11950. || (WASM_ENABLE_FAST_INTERP != 0)
  11951. case VALUE_TYPE_V128:
  11952. #if WASM_ENABLE_SIMDE != 0
  11953. if (loader_ctx->p_code_compiled) {
  11954. uint8 opcode_tmp = WASM_OP_SELECT_128;
  11955. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11956. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11957. *(void **)(p_code_compiled_tmp
  11958. - sizeof(void *)) =
  11959. handle_table[opcode_tmp];
  11960. #elif UINTPTR_MAX == UINT64_MAX
  11961. /* emit int32 relative offset in 64-bit target
  11962. */
  11963. int32 offset =
  11964. (int32)((uint8 *)handle_table[opcode_tmp]
  11965. - (uint8 *)handle_table[0]);
  11966. *(int32 *)(p_code_compiled_tmp
  11967. - sizeof(int32)) = offset;
  11968. #else
  11969. /* emit uint32 label address in 32-bit target */
  11970. *(uint32 *)(p_code_compiled_tmp
  11971. - sizeof(uint32)) =
  11972. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11973. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11974. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11975. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11976. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11977. #else
  11978. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11979. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11980. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11981. }
  11982. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11983. break;
  11984. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11985. (WASM_ENABLE_FAST_INTERP != 0) */
  11986. #endif /* WASM_ENABLE_SIMD != 0 */
  11987. default:
  11988. {
  11989. set_error_buf(error_buf, error_buf_size,
  11990. "type mismatch");
  11991. goto fail;
  11992. }
  11993. }
  11994. ref_type = *(loader_ctx->frame_ref - 1);
  11995. #if WASM_ENABLE_FAST_INTERP != 0
  11996. POP_OFFSET_TYPE(ref_type);
  11997. POP_TYPE(ref_type);
  11998. POP_OFFSET_TYPE(ref_type);
  11999. POP_TYPE(ref_type);
  12000. PUSH_OFFSET_TYPE(ref_type);
  12001. PUSH_TYPE(ref_type);
  12002. #else
  12003. POP2_AND_PUSH(ref_type, ref_type);
  12004. #endif
  12005. }
  12006. else {
  12007. #if WASM_ENABLE_FAST_INTERP != 0
  12008. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  12009. #endif
  12010. PUSH_TYPE(VALUE_TYPE_ANY);
  12011. }
  12012. break;
  12013. }
  12014. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  12015. case WASM_OP_SELECT_T:
  12016. {
  12017. uint8 vec_len, type;
  12018. #if WASM_ENABLE_GC != 0
  12019. WASMRefType *ref_type = NULL;
  12020. #endif
  12021. #if WASM_ENABLE_FAST_INTERP != 0
  12022. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  12023. #endif
  12024. pb_read_leb_uint32(p, p_end, vec_len);
  12025. if (vec_len != 1) {
  12026. /* typed select must have exactly one result */
  12027. set_error_buf(error_buf, error_buf_size,
  12028. "invalid result arity");
  12029. goto fail;
  12030. }
  12031. #if WASM_ENABLE_GC == 0
  12032. CHECK_BUF(p, p_end, 1);
  12033. type = read_uint8(p);
  12034. if (!is_valid_value_type_for_interpreter(type)) {
  12035. set_error_buf(error_buf, error_buf_size,
  12036. "unknown value type");
  12037. goto fail;
  12038. }
  12039. #else
  12040. p_org = p + 1;
  12041. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  12042. module->type_count, &need_ref_type_map,
  12043. &wasm_ref_type, false, error_buf,
  12044. error_buf_size)) {
  12045. goto fail;
  12046. }
  12047. type = wasm_ref_type.ref_type;
  12048. if (need_ref_type_map) {
  12049. if (!(ref_type = reftype_set_insert(
  12050. module->ref_type_set, &wasm_ref_type, error_buf,
  12051. error_buf_size))) {
  12052. goto fail;
  12053. }
  12054. }
  12055. #if WASM_ENABLE_FAST_INTERP == 0
  12056. while (p_org < p) {
  12057. #if WASM_ENABLE_DEBUG_INTERP != 0
  12058. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12059. error_buf_size)) {
  12060. goto fail;
  12061. }
  12062. #endif
  12063. /* Ignore extra bytes for interpreter */
  12064. *p_org++ = WASM_OP_NOP;
  12065. }
  12066. #endif
  12067. #endif /* end of WASM_ENABLE_GC == 0 */
  12068. POP_I32();
  12069. #if WASM_ENABLE_FAST_INTERP != 0
  12070. if (loader_ctx->p_code_compiled) {
  12071. uint8 opcode_tmp = WASM_OP_SELECT;
  12072. if (type == VALUE_TYPE_V128) {
  12073. #if WASM_ENABLE_SIMDE != 0
  12074. opcode_tmp = WASM_OP_SELECT_128;
  12075. #else
  12076. set_error_buf(error_buf, error_buf_size,
  12077. "v128 value type requires simd feature");
  12078. #endif
  12079. }
  12080. else {
  12081. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  12082. opcode_tmp = WASM_OP_SELECT_64;
  12083. #if WASM_ENABLE_GC != 0
  12084. if (wasm_is_type_reftype(type))
  12085. opcode_tmp = WASM_OP_SELECT_T;
  12086. #endif
  12087. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  12088. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  12089. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  12090. handle_table[opcode_tmp];
  12091. #else
  12092. #if UINTPTR_MAX == UINT64_MAX
  12093. /* emit int32 relative offset in 64-bit target */
  12094. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  12095. - (uint8 *)handle_table[0]);
  12096. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  12097. offset;
  12098. #else
  12099. /* emit uint32 label address in 32-bit target */
  12100. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  12101. (uint32)(uintptr_t)handle_table[opcode_tmp];
  12102. #endif
  12103. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12104. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  12105. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  12106. *(p_code_compiled_tmp - 1) = opcode_tmp;
  12107. #else
  12108. *(p_code_compiled_tmp - 2) = opcode_tmp;
  12109. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12110. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  12111. }
  12112. }
  12113. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  12114. POP_REF(type);
  12115. #if WASM_ENABLE_GC != 0
  12116. if (need_ref_type_map) {
  12117. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12118. wasm_reftype_struct_size(ref_type));
  12119. }
  12120. #endif
  12121. POP_REF(type);
  12122. #if WASM_ENABLE_GC != 0
  12123. if (need_ref_type_map) {
  12124. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12125. wasm_reftype_struct_size(ref_type));
  12126. }
  12127. #endif
  12128. PUSH_REF(type);
  12129. #if WASM_ENABLE_WAMR_COMPILER != 0
  12130. module->is_ref_types_used = true;
  12131. #endif
  12132. (void)vec_len;
  12133. break;
  12134. }
  12135. /* table.get x. tables[x]. [it] -> [t] */
  12136. /* table.set x. tables[x]. [it t] -> [] */
  12137. case WASM_OP_TABLE_GET:
  12138. case WASM_OP_TABLE_SET:
  12139. {
  12140. uint8 decl_ref_type;
  12141. #if WASM_ENABLE_GC != 0
  12142. WASMRefType *ref_type;
  12143. #endif
  12144. pb_read_leb_uint32(p, p_end, table_idx);
  12145. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  12146. #if WASM_ENABLE_GC != 0
  12147. (void **)&ref_type,
  12148. #else
  12149. NULL,
  12150. #endif
  12151. error_buf, error_buf_size))
  12152. goto fail;
  12153. #if WASM_ENABLE_GC != 0
  12154. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  12155. bh_assert(ref_type);
  12156. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  12157. ref_type, wasm_reftype_struct_size(ref_type));
  12158. }
  12159. #endif
  12160. #if WASM_ENABLE_FAST_INTERP != 0
  12161. emit_uint32(loader_ctx, table_idx);
  12162. #endif
  12163. #if WASM_ENABLE_MEMORY64 != 0
  12164. table_elem_idx_type = is_table_64bit(module, table_idx)
  12165. ? VALUE_TYPE_I64
  12166. : VALUE_TYPE_I32;
  12167. #endif
  12168. if (opcode == WASM_OP_TABLE_GET) {
  12169. POP_TBL_ELEM_IDX();
  12170. #if WASM_ENABLE_FAST_INTERP != 0
  12171. PUSH_OFFSET_TYPE(decl_ref_type);
  12172. #endif
  12173. PUSH_TYPE(decl_ref_type);
  12174. }
  12175. else {
  12176. #if WASM_ENABLE_FAST_INTERP != 0
  12177. POP_OFFSET_TYPE(decl_ref_type);
  12178. #endif
  12179. POP_TYPE(decl_ref_type);
  12180. POP_TBL_ELEM_IDX();
  12181. }
  12182. #if WASM_ENABLE_WAMR_COMPILER != 0
  12183. module->is_ref_types_used = true;
  12184. #endif
  12185. break;
  12186. }
  12187. case WASM_OP_REF_NULL:
  12188. {
  12189. uint8 ref_type;
  12190. #if WASM_ENABLE_GC == 0
  12191. CHECK_BUF(p, p_end, 1);
  12192. ref_type = read_uint8(p);
  12193. if (ref_type != VALUE_TYPE_FUNCREF
  12194. && ref_type != VALUE_TYPE_EXTERNREF) {
  12195. set_error_buf(error_buf, error_buf_size, "type mismatch");
  12196. goto fail;
  12197. }
  12198. #else
  12199. pb_read_leb_int32(p, p_end, heap_type);
  12200. if (heap_type >= 0) {
  12201. if (!check_type_index(module, module->type_count, heap_type,
  12202. error_buf, error_buf_size)) {
  12203. goto fail;
  12204. }
  12205. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12206. true, heap_type);
  12207. ref_type = wasm_ref_type.ref_type;
  12208. }
  12209. else {
  12210. if (!wasm_is_valid_heap_type(heap_type)) {
  12211. set_error_buf(error_buf, error_buf_size,
  12212. "unknown type");
  12213. goto fail;
  12214. }
  12215. ref_type = (uint8)((int32)0x80 + heap_type);
  12216. }
  12217. #endif /* end of WASM_ENABLE_GC == 0 */
  12218. #if WASM_ENABLE_FAST_INTERP != 0
  12219. PUSH_OFFSET_TYPE(ref_type);
  12220. #endif
  12221. PUSH_TYPE(ref_type);
  12222. #if WASM_ENABLE_WAMR_COMPILER != 0
  12223. module->is_ref_types_used = true;
  12224. #endif
  12225. break;
  12226. }
  12227. case WASM_OP_REF_IS_NULL:
  12228. {
  12229. #if WASM_ENABLE_GC == 0
  12230. #if WASM_ENABLE_FAST_INTERP != 0
  12231. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12232. int32 block_stack_cell_num =
  12233. (int32)(loader_ctx->stack_cell_num
  12234. - cur_block->stack_cell_num);
  12235. if (block_stack_cell_num <= 0) {
  12236. if (!cur_block->is_stack_polymorphic) {
  12237. set_error_buf(
  12238. error_buf, error_buf_size,
  12239. "type mismatch: expect data but stack was empty");
  12240. goto fail;
  12241. }
  12242. }
  12243. else {
  12244. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  12245. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  12246. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  12247. if (!wasm_loader_pop_frame_ref_offset(
  12248. loader_ctx, *(loader_ctx->frame_ref - 1),
  12249. error_buf, error_buf_size)) {
  12250. goto fail;
  12251. }
  12252. }
  12253. else {
  12254. set_error_buf(error_buf, error_buf_size,
  12255. "type mismatch");
  12256. goto fail;
  12257. }
  12258. }
  12259. #else
  12260. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  12261. error_buf, error_buf_size)
  12262. && !wasm_loader_pop_frame_ref(loader_ctx,
  12263. VALUE_TYPE_EXTERNREF,
  12264. error_buf, error_buf_size)) {
  12265. goto fail;
  12266. }
  12267. #endif
  12268. #else /* else of WASM_ENABLE_GC == 0 */
  12269. uint8 type;
  12270. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  12271. error_buf, error_buf_size)) {
  12272. goto fail;
  12273. }
  12274. #endif
  12275. PUSH_I32();
  12276. #if WASM_ENABLE_WAMR_COMPILER != 0
  12277. module->is_ref_types_used = true;
  12278. #endif
  12279. break;
  12280. }
  12281. case WASM_OP_REF_FUNC:
  12282. {
  12283. pb_read_leb_uint32(p, p_end, func_idx);
  12284. if (!check_function_index(module, func_idx, error_buf,
  12285. error_buf_size)) {
  12286. goto fail;
  12287. }
  12288. /* Refer to a forward-declared function:
  12289. the function must be an import, exported, or present in
  12290. a table elem segment or global initializer to be used as
  12291. the operand to ref.func */
  12292. if (func_idx >= module->import_function_count) {
  12293. WASMTableSeg *table_seg = module->table_segments;
  12294. bool func_declared = false;
  12295. uint32 j;
  12296. for (i = 0; i < module->global_count; i++) {
  12297. if (module->globals[i].type.val_type
  12298. == VALUE_TYPE_FUNCREF
  12299. && module->globals[i].init_expr.init_expr_type
  12300. == INIT_EXPR_TYPE_FUNCREF_CONST
  12301. && module->globals[i].init_expr.u.unary.v.u32
  12302. == func_idx) {
  12303. func_declared = true;
  12304. break;
  12305. }
  12306. }
  12307. if (!func_declared) {
  12308. /* Check whether the function is declared in table segs,
  12309. note that it doesn't matter whether the table seg's
  12310. mode is passive, active or declarative. */
  12311. for (i = 0; i < module->table_seg_count;
  12312. i++, table_seg++) {
  12313. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  12314. #if WASM_ENABLE_GC != 0
  12315. /* elem type is (ref null? func) or
  12316. (ref null? $t) */
  12317. || ((table_seg->elem_type
  12318. == REF_TYPE_HT_NON_NULLABLE
  12319. || table_seg->elem_type
  12320. == REF_TYPE_HT_NULLABLE)
  12321. && (table_seg->elem_ref_type->ref_ht_common
  12322. .heap_type
  12323. == HEAP_TYPE_FUNC
  12324. || table_seg->elem_ref_type
  12325. ->ref_ht_common.heap_type
  12326. > 0))
  12327. #endif
  12328. ) {
  12329. for (j = 0; j < table_seg->value_count; j++) {
  12330. if (table_seg->init_values[j]
  12331. .u.unary.v.ref_index
  12332. == func_idx) {
  12333. func_declared = true;
  12334. break;
  12335. }
  12336. }
  12337. }
  12338. }
  12339. }
  12340. if (!func_declared) {
  12341. /* Check whether the function is exported */
  12342. for (i = 0; i < module->export_count; i++) {
  12343. if (module->exports[i].kind == EXPORT_KIND_FUNC
  12344. && module->exports[i].index == func_idx) {
  12345. func_declared = true;
  12346. break;
  12347. }
  12348. }
  12349. }
  12350. if (!func_declared) {
  12351. set_error_buf(error_buf, error_buf_size,
  12352. "undeclared function reference");
  12353. goto fail;
  12354. }
  12355. }
  12356. #if WASM_ENABLE_FAST_INTERP != 0
  12357. emit_uint32(loader_ctx, func_idx);
  12358. #endif
  12359. #if WASM_ENABLE_GC == 0
  12360. PUSH_FUNCREF();
  12361. #else
  12362. if (func_idx < module->import_function_count)
  12363. type_idx =
  12364. module->import_functions[func_idx].u.function.type_idx;
  12365. else
  12366. type_idx = module
  12367. ->functions[func_idx
  12368. - module->import_function_count]
  12369. ->type_idx;
  12370. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12371. false, type_idx);
  12372. PUSH_REF(wasm_ref_type.ref_type);
  12373. #endif
  12374. #if WASM_ENABLE_WAMR_COMPILER != 0
  12375. module->is_ref_types_used = true;
  12376. #endif
  12377. break;
  12378. }
  12379. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  12380. #if WASM_ENABLE_GC != 0
  12381. case WASM_OP_REF_AS_NON_NULL:
  12382. case WASM_OP_BR_ON_NULL:
  12383. {
  12384. uint8 type;
  12385. WASMRefType ref_type;
  12386. /* POP (ref null ht) and get the converted (ref ht) */
  12387. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12388. error_buf, error_buf_size)) {
  12389. goto fail;
  12390. }
  12391. if (opcode == WASM_OP_BR_ON_NULL) {
  12392. if (!(frame_csp_tmp =
  12393. check_branch_block(loader_ctx, &p, p_end, opcode,
  12394. error_buf, error_buf_size))) {
  12395. goto fail;
  12396. }
  12397. #if WASM_ENABLE_FAST_INTERP != 0
  12398. disable_emit = true;
  12399. #endif
  12400. }
  12401. /* PUSH the converted (ref ht) */
  12402. if (type != VALUE_TYPE_ANY) {
  12403. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  12404. sizeof(WASMRefType));
  12405. }
  12406. PUSH_REF(type);
  12407. break;
  12408. }
  12409. case WASM_OP_BR_ON_NON_NULL:
  12410. {
  12411. uint8 type;
  12412. WASMRefType ref_type;
  12413. uint32 available_stack_cell =
  12414. loader_ctx->stack_cell_num
  12415. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  12416. /* POP (ref null ht) and get the converted (ref ht) */
  12417. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12418. error_buf, error_buf_size)) {
  12419. goto fail;
  12420. }
  12421. #if WASM_ENABLE_FAST_INTERP != 0
  12422. disable_emit = true;
  12423. #endif
  12424. /* Temporarily PUSH back (ref ht), check brach block and
  12425. then POP it */
  12426. if (available_stack_cell
  12427. > 0) { /* stack isn't in polymorphic state */
  12428. if (type != VALUE_TYPE_ANY) {
  12429. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12430. &ref_type, sizeof(WASMRefType));
  12431. }
  12432. PUSH_REF(type);
  12433. }
  12434. if (!(frame_csp_tmp =
  12435. check_branch_block(loader_ctx, &p, p_end, opcode,
  12436. error_buf, error_buf_size))) {
  12437. goto fail;
  12438. }
  12439. if (available_stack_cell
  12440. > 0) { /* stack isn't in polymorphic state */
  12441. POP_REF(type);
  12442. #if WASM_ENABLE_FAST_INTERP != 0
  12443. /* Erase the opnd offset emitted by POP_REF() */
  12444. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12445. #endif
  12446. }
  12447. break;
  12448. }
  12449. case WASM_OP_REF_EQ:
  12450. POP_REF(REF_TYPE_EQREF);
  12451. POP_REF(REF_TYPE_EQREF);
  12452. PUSH_I32();
  12453. break;
  12454. #endif /* end of WASM_ENABLE_GC != 0 */
  12455. case WASM_OP_GET_LOCAL:
  12456. {
  12457. p_org = p - 1;
  12458. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12459. PUSH_TYPE(local_type);
  12460. #if WASM_ENABLE_GC != 0
  12461. /* Cannot get a non-nullable and unset local */
  12462. if (local_idx >= param_count
  12463. && wasm_is_reftype_htref_non_nullable(local_type)
  12464. && !wasm_loader_get_local_status(loader_ctx,
  12465. local_idx - param_count)) {
  12466. set_error_buf(error_buf, error_buf_size,
  12467. "uninitialized local");
  12468. goto fail;
  12469. }
  12470. #endif
  12471. #if WASM_ENABLE_FAST_INTERP != 0
  12472. /* Get Local is optimized out */
  12473. skip_label();
  12474. disable_emit = true;
  12475. operand_offset = local_offset;
  12476. PUSH_OFFSET_TYPE(local_type);
  12477. #else
  12478. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12479. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12480. if (local_offset < 0x80
  12481. #if WASM_ENABLE_GC != 0
  12482. && !wasm_is_type_reftype(local_type)
  12483. #endif
  12484. ) {
  12485. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12486. if (is_32bit_type(local_type)) {
  12487. *p_org++ = (uint8)local_offset;
  12488. }
  12489. else {
  12490. *p_org++ = (uint8)(local_offset | 0x80);
  12491. }
  12492. while (p_org < p) {
  12493. *p_org++ = WASM_OP_NOP;
  12494. }
  12495. }
  12496. #endif
  12497. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12498. break;
  12499. }
  12500. case WASM_OP_SET_LOCAL:
  12501. {
  12502. p_org = p - 1;
  12503. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12504. #if WASM_ENABLE_FAST_INTERP != 0
  12505. if (!(preserve_referenced_local(
  12506. loader_ctx, opcode, local_offset, local_type,
  12507. &preserve_local, error_buf, error_buf_size)))
  12508. goto fail;
  12509. if (local_offset < 256
  12510. #if WASM_ENABLE_GC != 0
  12511. && !wasm_is_type_reftype(local_type)
  12512. #endif
  12513. ) {
  12514. skip_label();
  12515. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12516. if (loader_ctx->p_code_compiled)
  12517. STORE_U16(loader_ctx->p_code_compiled - 2,
  12518. local_offset);
  12519. loader_ctx->frame_offset--;
  12520. loader_ctx->dynamic_offset--;
  12521. }
  12522. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12523. if (loader_ctx->p_code_compiled)
  12524. STORE_U16(loader_ctx->p_code_compiled - 2,
  12525. local_offset);
  12526. loader_ctx->frame_offset -= 2;
  12527. loader_ctx->dynamic_offset -= 2;
  12528. }
  12529. else {
  12530. if (is_32bit_type(local_type)) {
  12531. emit_label(EXT_OP_SET_LOCAL_FAST);
  12532. emit_byte(loader_ctx, (uint8)local_offset);
  12533. }
  12534. else if (is_64bit_type(local_type)) {
  12535. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12536. emit_byte(loader_ctx, (uint8)local_offset);
  12537. }
  12538. #if WASM_ENABLE_SIMDE != 0
  12539. else if (local_type == VALUE_TYPE_V128) {
  12540. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12541. emit_byte(loader_ctx, (uint8)local_offset);
  12542. }
  12543. #endif
  12544. else {
  12545. set_error_buf(error_buf, error_buf_size,
  12546. "unknown local type");
  12547. goto fail;
  12548. }
  12549. POP_OFFSET_TYPE(local_type);
  12550. }
  12551. }
  12552. else { /* local index larger than 255, reserve leb */
  12553. emit_uint32(loader_ctx, local_idx);
  12554. POP_OFFSET_TYPE(local_type);
  12555. }
  12556. #else
  12557. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12558. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12559. if (local_offset < 0x80
  12560. #if WASM_ENABLE_GC != 0
  12561. && !wasm_is_type_reftype(local_type)
  12562. #endif
  12563. ) {
  12564. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12565. if (is_32bit_type(local_type)) {
  12566. *p_org++ = (uint8)local_offset;
  12567. }
  12568. else {
  12569. *p_org++ = (uint8)(local_offset | 0x80);
  12570. }
  12571. while (p_org < p) {
  12572. *p_org++ = WASM_OP_NOP;
  12573. }
  12574. }
  12575. #endif
  12576. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12577. #if WASM_ENABLE_GC != 0
  12578. if (local_idx >= param_count) {
  12579. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12580. }
  12581. #endif
  12582. POP_TYPE(local_type);
  12583. break;
  12584. }
  12585. case WASM_OP_TEE_LOCAL:
  12586. {
  12587. p_org = p - 1;
  12588. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12589. #if WASM_ENABLE_FAST_INTERP != 0
  12590. /* If the stack is in polymorphic state, do fake pop and push on
  12591. offset stack to keep the depth of offset stack to be the
  12592. same with ref stack */
  12593. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12594. if (cur_block->is_stack_polymorphic) {
  12595. POP_OFFSET_TYPE(local_type);
  12596. PUSH_OFFSET_TYPE(local_type);
  12597. }
  12598. #endif
  12599. POP_TYPE(local_type);
  12600. PUSH_TYPE(local_type);
  12601. #if WASM_ENABLE_FAST_INTERP != 0
  12602. if (!(preserve_referenced_local(
  12603. loader_ctx, opcode, local_offset, local_type,
  12604. &preserve_local, error_buf, error_buf_size)))
  12605. goto fail;
  12606. if (local_offset < 256
  12607. #if WASM_ENABLE_GC != 0
  12608. && !wasm_is_type_reftype(local_type)
  12609. #endif
  12610. ) {
  12611. skip_label();
  12612. if (is_32bit_type(local_type)) {
  12613. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12614. emit_byte(loader_ctx, (uint8)local_offset);
  12615. }
  12616. #if WASM_ENABLE_SIMDE != 0
  12617. else if (local_type == VALUE_TYPE_V128) {
  12618. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12619. emit_byte(loader_ctx, (uint8)local_offset);
  12620. }
  12621. #endif
  12622. else {
  12623. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12624. emit_byte(loader_ctx, (uint8)local_offset);
  12625. }
  12626. }
  12627. else { /* local index larger than 255, reserve leb */
  12628. emit_uint32(loader_ctx, local_idx);
  12629. }
  12630. emit_operand(loader_ctx,
  12631. *(loader_ctx->frame_offset
  12632. - wasm_value_type_cell_num(local_type)));
  12633. #else
  12634. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12635. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12636. if (local_offset < 0x80
  12637. #if WASM_ENABLE_GC != 0
  12638. && !wasm_is_type_reftype(local_type)
  12639. #endif
  12640. ) {
  12641. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12642. if (is_32bit_type(local_type)) {
  12643. *p_org++ = (uint8)local_offset;
  12644. }
  12645. else {
  12646. *p_org++ = (uint8)(local_offset | 0x80);
  12647. }
  12648. while (p_org < p) {
  12649. *p_org++ = WASM_OP_NOP;
  12650. }
  12651. }
  12652. #endif
  12653. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12654. #if WASM_ENABLE_GC != 0
  12655. if (local_idx >= param_count) {
  12656. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12657. }
  12658. #endif
  12659. break;
  12660. }
  12661. case WASM_OP_GET_GLOBAL:
  12662. {
  12663. #if WASM_ENABLE_GC != 0
  12664. WASMRefType *ref_type;
  12665. #endif
  12666. p_org = p - 1;
  12667. pb_read_leb_uint32(p, p_end, global_idx);
  12668. if (global_idx >= global_count) {
  12669. set_error_buf(error_buf, error_buf_size, "unknown global");
  12670. goto fail;
  12671. }
  12672. global_type = global_idx < module->import_global_count
  12673. ? module->import_globals[global_idx]
  12674. .u.global.type.val_type
  12675. : module
  12676. ->globals[global_idx
  12677. - module->import_global_count]
  12678. .type.val_type;
  12679. #if WASM_ENABLE_GC != 0
  12680. ref_type =
  12681. global_idx < module->import_global_count
  12682. ? module->import_globals[global_idx].u.global.ref_type
  12683. : module
  12684. ->globals[global_idx
  12685. - module->import_global_count]
  12686. .ref_type;
  12687. if (wasm_is_type_multi_byte_type(global_type)) {
  12688. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12689. wasm_reftype_struct_size(ref_type));
  12690. }
  12691. #endif
  12692. PUSH_TYPE(global_type);
  12693. #if WASM_ENABLE_FAST_INTERP == 0
  12694. if (global_type == VALUE_TYPE_I64
  12695. || global_type == VALUE_TYPE_F64) {
  12696. #if WASM_ENABLE_DEBUG_INTERP != 0
  12697. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12698. error_buf_size)) {
  12699. goto fail;
  12700. }
  12701. #endif
  12702. *p_org = WASM_OP_GET_GLOBAL_64;
  12703. }
  12704. #else /* else of WASM_ENABLE_FAST_INTERP */
  12705. if (global_type == VALUE_TYPE_I64
  12706. || global_type == VALUE_TYPE_F64) {
  12707. skip_label();
  12708. emit_label(WASM_OP_GET_GLOBAL_64);
  12709. }
  12710. #if WASM_ENABLE_SIMDE != 0
  12711. if (global_type == VALUE_TYPE_V128) {
  12712. skip_label();
  12713. emit_label(WASM_OP_GET_GLOBAL_V128);
  12714. }
  12715. #endif /* end of WASM_ENABLE_SIMDE */
  12716. emit_uint32(loader_ctx, global_idx);
  12717. PUSH_OFFSET_TYPE(global_type);
  12718. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12719. break;
  12720. }
  12721. case WASM_OP_SET_GLOBAL:
  12722. {
  12723. bool is_mutable = false;
  12724. #if WASM_ENABLE_GC != 0
  12725. WASMRefType *ref_type;
  12726. #endif
  12727. p_org = p - 1;
  12728. pb_read_leb_uint32(p, p_end, global_idx);
  12729. if (global_idx >= global_count) {
  12730. set_error_buf(error_buf, error_buf_size, "unknown global");
  12731. goto fail;
  12732. }
  12733. is_mutable = global_idx < module->import_global_count
  12734. ? module->import_globals[global_idx]
  12735. .u.global.type.is_mutable
  12736. : module
  12737. ->globals[global_idx
  12738. - module->import_global_count]
  12739. .type.is_mutable;
  12740. if (!is_mutable) {
  12741. #if WASM_ENABLE_GC == 0
  12742. set_error_buf(error_buf, error_buf_size,
  12743. "global is immutable");
  12744. #else
  12745. set_error_buf(error_buf, error_buf_size,
  12746. "immutable global");
  12747. #endif
  12748. goto fail;
  12749. }
  12750. global_type = global_idx < module->import_global_count
  12751. ? module->import_globals[global_idx]
  12752. .u.global.type.val_type
  12753. : module
  12754. ->globals[global_idx
  12755. - module->import_global_count]
  12756. .type.val_type;
  12757. #if WASM_ENABLE_GC != 0
  12758. ref_type =
  12759. global_idx < module->import_global_count
  12760. ? module->import_globals[global_idx].u.global.ref_type
  12761. : module
  12762. ->globals[global_idx
  12763. - module->import_global_count]
  12764. .ref_type;
  12765. if (wasm_is_type_multi_byte_type(global_type)) {
  12766. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12767. wasm_reftype_struct_size(ref_type));
  12768. }
  12769. #endif
  12770. #if WASM_ENABLE_FAST_INTERP == 0
  12771. if (global_type == VALUE_TYPE_I64
  12772. || global_type == VALUE_TYPE_F64) {
  12773. #if WASM_ENABLE_DEBUG_INTERP != 0
  12774. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12775. error_buf_size)) {
  12776. goto fail;
  12777. }
  12778. #endif
  12779. *p_org = WASM_OP_SET_GLOBAL_64;
  12780. }
  12781. else if (module->aux_stack_size > 0
  12782. && global_idx == module->aux_stack_top_global_index) {
  12783. #if WASM_ENABLE_DEBUG_INTERP != 0
  12784. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12785. error_buf_size)) {
  12786. goto fail;
  12787. }
  12788. #endif
  12789. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12790. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12791. func->has_op_set_global_aux_stack = true;
  12792. #endif
  12793. }
  12794. #else /* else of WASM_ENABLE_FAST_INTERP */
  12795. if (global_type == VALUE_TYPE_I64
  12796. || global_type == VALUE_TYPE_F64) {
  12797. skip_label();
  12798. emit_label(WASM_OP_SET_GLOBAL_64);
  12799. }
  12800. else if (module->aux_stack_size > 0
  12801. && global_idx == module->aux_stack_top_global_index) {
  12802. skip_label();
  12803. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12804. }
  12805. #if WASM_ENABLE_SIMDE != 0
  12806. else if (global_type == VALUE_TYPE_V128) {
  12807. skip_label();
  12808. emit_label(WASM_OP_SET_GLOBAL_V128);
  12809. }
  12810. #endif /* end of WASM_ENABLE_SIMDE */
  12811. emit_uint32(loader_ctx, global_idx);
  12812. POP_OFFSET_TYPE(global_type);
  12813. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12814. POP_TYPE(global_type);
  12815. break;
  12816. }
  12817. /* load */
  12818. case WASM_OP_I32_LOAD:
  12819. case WASM_OP_I32_LOAD8_S:
  12820. case WASM_OP_I32_LOAD8_U:
  12821. case WASM_OP_I32_LOAD16_S:
  12822. case WASM_OP_I32_LOAD16_U:
  12823. case WASM_OP_I64_LOAD:
  12824. case WASM_OP_I64_LOAD8_S:
  12825. case WASM_OP_I64_LOAD8_U:
  12826. case WASM_OP_I64_LOAD16_S:
  12827. case WASM_OP_I64_LOAD16_U:
  12828. case WASM_OP_I64_LOAD32_S:
  12829. case WASM_OP_I64_LOAD32_U:
  12830. case WASM_OP_F32_LOAD:
  12831. case WASM_OP_F64_LOAD:
  12832. /* store */
  12833. case WASM_OP_I32_STORE:
  12834. case WASM_OP_I32_STORE8:
  12835. case WASM_OP_I32_STORE16:
  12836. case WASM_OP_I64_STORE:
  12837. case WASM_OP_I64_STORE8:
  12838. case WASM_OP_I64_STORE16:
  12839. case WASM_OP_I64_STORE32:
  12840. case WASM_OP_F32_STORE:
  12841. case WASM_OP_F64_STORE:
  12842. {
  12843. #if WASM_ENABLE_FAST_INTERP != 0
  12844. /* change F32/F64 into I32/I64 */
  12845. if (opcode == WASM_OP_F32_LOAD) {
  12846. skip_label();
  12847. emit_label(WASM_OP_I32_LOAD);
  12848. }
  12849. else if (opcode == WASM_OP_F64_LOAD) {
  12850. skip_label();
  12851. emit_label(WASM_OP_I64_LOAD);
  12852. }
  12853. else if (opcode == WASM_OP_F32_STORE) {
  12854. skip_label();
  12855. emit_label(WASM_OP_I32_STORE);
  12856. }
  12857. else if (opcode == WASM_OP_F64_STORE) {
  12858. skip_label();
  12859. emit_label(WASM_OP_I64_STORE);
  12860. }
  12861. #endif
  12862. CHECK_MEMORY();
  12863. pb_read_leb_memarg(p, p_end, align); /* align */
  12864. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12865. if (!check_memory_access_align(opcode, align, error_buf,
  12866. error_buf_size)) {
  12867. goto fail;
  12868. }
  12869. #if WASM_ENABLE_FAST_INTERP != 0
  12870. emit_uint32(loader_ctx, mem_offset);
  12871. #endif
  12872. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12873. func->has_memory_operations = true;
  12874. #endif
  12875. switch (opcode) {
  12876. /* load */
  12877. case WASM_OP_I32_LOAD:
  12878. case WASM_OP_I32_LOAD8_S:
  12879. case WASM_OP_I32_LOAD8_U:
  12880. case WASM_OP_I32_LOAD16_S:
  12881. case WASM_OP_I32_LOAD16_U:
  12882. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12883. break;
  12884. case WASM_OP_I64_LOAD:
  12885. case WASM_OP_I64_LOAD8_S:
  12886. case WASM_OP_I64_LOAD8_U:
  12887. case WASM_OP_I64_LOAD16_S:
  12888. case WASM_OP_I64_LOAD16_U:
  12889. case WASM_OP_I64_LOAD32_S:
  12890. case WASM_OP_I64_LOAD32_U:
  12891. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12892. break;
  12893. case WASM_OP_F32_LOAD:
  12894. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12895. break;
  12896. case WASM_OP_F64_LOAD:
  12897. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12898. break;
  12899. /* store */
  12900. case WASM_OP_I32_STORE:
  12901. case WASM_OP_I32_STORE8:
  12902. case WASM_OP_I32_STORE16:
  12903. POP_I32();
  12904. POP_MEM_OFFSET();
  12905. break;
  12906. case WASM_OP_I64_STORE:
  12907. case WASM_OP_I64_STORE8:
  12908. case WASM_OP_I64_STORE16:
  12909. case WASM_OP_I64_STORE32:
  12910. POP_I64();
  12911. POP_MEM_OFFSET();
  12912. break;
  12913. case WASM_OP_F32_STORE:
  12914. POP_F32();
  12915. POP_MEM_OFFSET();
  12916. break;
  12917. case WASM_OP_F64_STORE:
  12918. POP_F64();
  12919. POP_MEM_OFFSET();
  12920. break;
  12921. default:
  12922. break;
  12923. }
  12924. break;
  12925. }
  12926. case WASM_OP_MEMORY_SIZE:
  12927. CHECK_MEMORY();
  12928. pb_read_leb_uint32(p, p_end, memidx);
  12929. check_memidx(module, memidx);
  12930. PUSH_PAGE_COUNT();
  12931. module->possible_memory_grow = true;
  12932. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12933. func->has_memory_operations = true;
  12934. #endif
  12935. break;
  12936. case WASM_OP_MEMORY_GROW:
  12937. CHECK_MEMORY();
  12938. pb_read_leb_uint32(p, p_end, memidx);
  12939. check_memidx(module, memidx);
  12940. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12941. module->possible_memory_grow = true;
  12942. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12943. || WASM_ENABLE_WAMR_COMPILER != 0
  12944. func->has_op_memory_grow = true;
  12945. #endif
  12946. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12947. func->has_memory_operations = true;
  12948. #endif
  12949. break;
  12950. case WASM_OP_I32_CONST:
  12951. pb_read_leb_int32(p, p_end, i32_const);
  12952. #if WASM_ENABLE_FAST_INTERP != 0
  12953. skip_label();
  12954. disable_emit = true;
  12955. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12956. if (operand_offset == 0) {
  12957. disable_emit = false;
  12958. emit_label(WASM_OP_I32_CONST);
  12959. emit_uint32(loader_ctx, i32_const);
  12960. }
  12961. #else
  12962. (void)i32_const;
  12963. #endif
  12964. PUSH_I32();
  12965. break;
  12966. case WASM_OP_I64_CONST:
  12967. pb_read_leb_int64(p, p_end, i64_const);
  12968. #if WASM_ENABLE_FAST_INTERP != 0
  12969. skip_label();
  12970. disable_emit = true;
  12971. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12972. if (operand_offset == 0) {
  12973. disable_emit = false;
  12974. emit_label(WASM_OP_I64_CONST);
  12975. emit_uint64(loader_ctx, i64_const);
  12976. }
  12977. #endif
  12978. PUSH_I64();
  12979. break;
  12980. case WASM_OP_F32_CONST:
  12981. CHECK_BUF(p, p_end, sizeof(float32));
  12982. p += sizeof(float32);
  12983. #if WASM_ENABLE_FAST_INTERP != 0
  12984. skip_label();
  12985. disable_emit = true;
  12986. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12987. sizeof(float32));
  12988. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12989. if (operand_offset == 0) {
  12990. disable_emit = false;
  12991. emit_label(WASM_OP_F32_CONST);
  12992. emit_float32(loader_ctx, f32_const);
  12993. }
  12994. #endif
  12995. PUSH_F32();
  12996. break;
  12997. case WASM_OP_F64_CONST:
  12998. CHECK_BUF(p, p_end, sizeof(float64));
  12999. p += sizeof(float64);
  13000. #if WASM_ENABLE_FAST_INTERP != 0
  13001. skip_label();
  13002. disable_emit = true;
  13003. /* Some MCU may require 8-byte align */
  13004. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  13005. sizeof(float64));
  13006. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  13007. if (operand_offset == 0) {
  13008. disable_emit = false;
  13009. emit_label(WASM_OP_F64_CONST);
  13010. emit_float64(loader_ctx, f64_const);
  13011. }
  13012. #endif
  13013. PUSH_F64();
  13014. break;
  13015. case WASM_OP_I32_EQZ:
  13016. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13017. break;
  13018. case WASM_OP_I32_EQ:
  13019. case WASM_OP_I32_NE:
  13020. case WASM_OP_I32_LT_S:
  13021. case WASM_OP_I32_LT_U:
  13022. case WASM_OP_I32_GT_S:
  13023. case WASM_OP_I32_GT_U:
  13024. case WASM_OP_I32_LE_S:
  13025. case WASM_OP_I32_LE_U:
  13026. case WASM_OP_I32_GE_S:
  13027. case WASM_OP_I32_GE_U:
  13028. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13029. break;
  13030. case WASM_OP_I64_EQZ:
  13031. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13032. break;
  13033. case WASM_OP_I64_EQ:
  13034. case WASM_OP_I64_NE:
  13035. case WASM_OP_I64_LT_S:
  13036. case WASM_OP_I64_LT_U:
  13037. case WASM_OP_I64_GT_S:
  13038. case WASM_OP_I64_GT_U:
  13039. case WASM_OP_I64_LE_S:
  13040. case WASM_OP_I64_LE_U:
  13041. case WASM_OP_I64_GE_S:
  13042. case WASM_OP_I64_GE_U:
  13043. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13044. break;
  13045. case WASM_OP_F32_EQ:
  13046. case WASM_OP_F32_NE:
  13047. case WASM_OP_F32_LT:
  13048. case WASM_OP_F32_GT:
  13049. case WASM_OP_F32_LE:
  13050. case WASM_OP_F32_GE:
  13051. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13052. break;
  13053. case WASM_OP_F64_EQ:
  13054. case WASM_OP_F64_NE:
  13055. case WASM_OP_F64_LT:
  13056. case WASM_OP_F64_GT:
  13057. case WASM_OP_F64_LE:
  13058. case WASM_OP_F64_GE:
  13059. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13060. break;
  13061. case WASM_OP_I32_CLZ:
  13062. case WASM_OP_I32_CTZ:
  13063. case WASM_OP_I32_POPCNT:
  13064. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13065. break;
  13066. case WASM_OP_I32_ADD:
  13067. case WASM_OP_I32_SUB:
  13068. case WASM_OP_I32_MUL:
  13069. case WASM_OP_I32_DIV_S:
  13070. case WASM_OP_I32_DIV_U:
  13071. case WASM_OP_I32_REM_S:
  13072. case WASM_OP_I32_REM_U:
  13073. case WASM_OP_I32_AND:
  13074. case WASM_OP_I32_OR:
  13075. case WASM_OP_I32_XOR:
  13076. case WASM_OP_I32_SHL:
  13077. case WASM_OP_I32_SHR_S:
  13078. case WASM_OP_I32_SHR_U:
  13079. case WASM_OP_I32_ROTL:
  13080. case WASM_OP_I32_ROTR:
  13081. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13082. break;
  13083. case WASM_OP_I64_CLZ:
  13084. case WASM_OP_I64_CTZ:
  13085. case WASM_OP_I64_POPCNT:
  13086. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13087. break;
  13088. case WASM_OP_I64_ADD:
  13089. case WASM_OP_I64_SUB:
  13090. case WASM_OP_I64_MUL:
  13091. case WASM_OP_I64_DIV_S:
  13092. case WASM_OP_I64_DIV_U:
  13093. case WASM_OP_I64_REM_S:
  13094. case WASM_OP_I64_REM_U:
  13095. case WASM_OP_I64_AND:
  13096. case WASM_OP_I64_OR:
  13097. case WASM_OP_I64_XOR:
  13098. case WASM_OP_I64_SHL:
  13099. case WASM_OP_I64_SHR_S:
  13100. case WASM_OP_I64_SHR_U:
  13101. case WASM_OP_I64_ROTL:
  13102. case WASM_OP_I64_ROTR:
  13103. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13104. break;
  13105. case WASM_OP_F32_ABS:
  13106. case WASM_OP_F32_NEG:
  13107. case WASM_OP_F32_CEIL:
  13108. case WASM_OP_F32_FLOOR:
  13109. case WASM_OP_F32_TRUNC:
  13110. case WASM_OP_F32_NEAREST:
  13111. case WASM_OP_F32_SQRT:
  13112. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  13113. break;
  13114. case WASM_OP_F32_ADD:
  13115. case WASM_OP_F32_SUB:
  13116. case WASM_OP_F32_MUL:
  13117. case WASM_OP_F32_DIV:
  13118. case WASM_OP_F32_MIN:
  13119. case WASM_OP_F32_MAX:
  13120. case WASM_OP_F32_COPYSIGN:
  13121. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  13122. break;
  13123. case WASM_OP_F64_ABS:
  13124. case WASM_OP_F64_NEG:
  13125. case WASM_OP_F64_CEIL:
  13126. case WASM_OP_F64_FLOOR:
  13127. case WASM_OP_F64_TRUNC:
  13128. case WASM_OP_F64_NEAREST:
  13129. case WASM_OP_F64_SQRT:
  13130. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13131. break;
  13132. case WASM_OP_F64_ADD:
  13133. case WASM_OP_F64_SUB:
  13134. case WASM_OP_F64_MUL:
  13135. case WASM_OP_F64_DIV:
  13136. case WASM_OP_F64_MIN:
  13137. case WASM_OP_F64_MAX:
  13138. case WASM_OP_F64_COPYSIGN:
  13139. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13140. break;
  13141. case WASM_OP_I32_WRAP_I64:
  13142. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13143. break;
  13144. case WASM_OP_I32_TRUNC_S_F32:
  13145. case WASM_OP_I32_TRUNC_U_F32:
  13146. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13147. break;
  13148. case WASM_OP_I32_TRUNC_S_F64:
  13149. case WASM_OP_I32_TRUNC_U_F64:
  13150. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13151. break;
  13152. case WASM_OP_I64_EXTEND_S_I32:
  13153. case WASM_OP_I64_EXTEND_U_I32:
  13154. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  13155. break;
  13156. case WASM_OP_I64_TRUNC_S_F32:
  13157. case WASM_OP_I64_TRUNC_U_F32:
  13158. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13159. break;
  13160. case WASM_OP_I64_TRUNC_S_F64:
  13161. case WASM_OP_I64_TRUNC_U_F64:
  13162. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13163. break;
  13164. case WASM_OP_F32_CONVERT_S_I32:
  13165. case WASM_OP_F32_CONVERT_U_I32:
  13166. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13167. break;
  13168. case WASM_OP_F32_CONVERT_S_I64:
  13169. case WASM_OP_F32_CONVERT_U_I64:
  13170. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  13171. break;
  13172. case WASM_OP_F32_DEMOTE_F64:
  13173. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  13174. break;
  13175. case WASM_OP_F64_CONVERT_S_I32:
  13176. case WASM_OP_F64_CONVERT_U_I32:
  13177. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  13178. break;
  13179. case WASM_OP_F64_CONVERT_S_I64:
  13180. case WASM_OP_F64_CONVERT_U_I64:
  13181. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13182. break;
  13183. case WASM_OP_F64_PROMOTE_F32:
  13184. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  13185. break;
  13186. case WASM_OP_I32_REINTERPRET_F32:
  13187. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13188. break;
  13189. case WASM_OP_I64_REINTERPRET_F64:
  13190. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13191. break;
  13192. case WASM_OP_F32_REINTERPRET_I32:
  13193. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13194. break;
  13195. case WASM_OP_F64_REINTERPRET_I64:
  13196. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13197. break;
  13198. case WASM_OP_I32_EXTEND8_S:
  13199. case WASM_OP_I32_EXTEND16_S:
  13200. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13201. break;
  13202. case WASM_OP_I64_EXTEND8_S:
  13203. case WASM_OP_I64_EXTEND16_S:
  13204. case WASM_OP_I64_EXTEND32_S:
  13205. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13206. break;
  13207. #if WASM_ENABLE_GC != 0
  13208. case WASM_OP_GC_PREFIX:
  13209. {
  13210. uint32 opcode1;
  13211. pb_read_leb_uint32(p, p_end, opcode1);
  13212. #if WASM_ENABLE_FAST_INTERP != 0
  13213. emit_byte(loader_ctx, ((uint8)opcode1));
  13214. #endif
  13215. switch (opcode1) {
  13216. case WASM_OP_STRUCT_NEW:
  13217. case WASM_OP_STRUCT_NEW_DEFAULT:
  13218. {
  13219. pb_read_leb_uint32(p, p_end, type_idx);
  13220. #if WASM_ENABLE_FAST_INTERP != 0
  13221. emit_uint32(loader_ctx, type_idx);
  13222. #endif
  13223. if (!check_type_index(module, module->type_count,
  13224. type_idx, error_buf,
  13225. error_buf_size)) {
  13226. goto fail;
  13227. }
  13228. if (module->types[type_idx]->type_flag
  13229. != WASM_TYPE_STRUCT) {
  13230. set_error_buf(error_buf, error_buf_size,
  13231. "unknown struct type");
  13232. goto fail;
  13233. }
  13234. if (opcode1 == WASM_OP_STRUCT_NEW) {
  13235. int32 j, k;
  13236. uint8 value_type;
  13237. uint32 ref_type_struct_size;
  13238. WASMStructType *struct_type =
  13239. (WASMStructType *)module->types[type_idx];
  13240. k = struct_type->ref_type_map_count - 1;
  13241. for (j = struct_type->field_count - 1; j >= 0;
  13242. j--) {
  13243. value_type = struct_type->fields[j].field_type;
  13244. if (wasm_is_type_reftype(value_type)) {
  13245. if (wasm_is_type_multi_byte_type(
  13246. value_type)) {
  13247. ref_type_struct_size =
  13248. wasm_reftype_struct_size(
  13249. struct_type->ref_type_maps[k]
  13250. .ref_type);
  13251. bh_memcpy_s(
  13252. &wasm_ref_type,
  13253. (uint32)sizeof(WASMRefType),
  13254. struct_type->ref_type_maps[k]
  13255. .ref_type,
  13256. ref_type_struct_size);
  13257. k--;
  13258. }
  13259. POP_REF(value_type);
  13260. }
  13261. else {
  13262. switch (value_type) {
  13263. case VALUE_TYPE_I32:
  13264. case PACKED_TYPE_I8:
  13265. case PACKED_TYPE_I16:
  13266. POP_I32();
  13267. break;
  13268. case VALUE_TYPE_I64:
  13269. POP_I64();
  13270. break;
  13271. case VALUE_TYPE_F32:
  13272. POP_F32();
  13273. break;
  13274. case VALUE_TYPE_F64:
  13275. POP_F64();
  13276. break;
  13277. default:
  13278. set_error_buf(error_buf,
  13279. error_buf_size,
  13280. "unknown type");
  13281. goto fail;
  13282. }
  13283. }
  13284. }
  13285. }
  13286. /* PUSH struct obj, (ref $t) */
  13287. wasm_set_refheaptype_typeidx(
  13288. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13289. PUSH_REF(wasm_ref_type.ref_type);
  13290. break;
  13291. }
  13292. case WASM_OP_STRUCT_GET:
  13293. case WASM_OP_STRUCT_GET_S:
  13294. case WASM_OP_STRUCT_GET_U:
  13295. case WASM_OP_STRUCT_SET:
  13296. {
  13297. WASMStructType *struct_type;
  13298. WASMRefType *ref_type = NULL;
  13299. uint32 field_idx;
  13300. uint8 field_type;
  13301. pb_read_leb_uint32(p, p_end, type_idx);
  13302. #if WASM_ENABLE_FAST_INTERP != 0
  13303. emit_uint32(loader_ctx, type_idx);
  13304. #endif
  13305. if (!check_type_index(module, module->type_count,
  13306. type_idx, error_buf,
  13307. error_buf_size)) {
  13308. goto fail;
  13309. }
  13310. if (module->types[type_idx]->type_flag
  13311. != WASM_TYPE_STRUCT) {
  13312. set_error_buf(error_buf, error_buf_size,
  13313. "unknown struct type");
  13314. goto fail;
  13315. }
  13316. struct_type = (WASMStructType *)module->types[type_idx];
  13317. pb_read_leb_uint32(p, p_end, field_idx);
  13318. #if WASM_ENABLE_FAST_INTERP != 0
  13319. emit_uint32(loader_ctx, field_idx);
  13320. #endif
  13321. if (field_idx >= struct_type->field_count) {
  13322. set_error_buf(error_buf, error_buf_size,
  13323. "unknown struct field");
  13324. goto fail;
  13325. }
  13326. if (opcode1 == WASM_OP_STRUCT_SET
  13327. && !(struct_type->fields[field_idx].field_flags
  13328. & 1)) {
  13329. set_error_buf(error_buf, error_buf_size,
  13330. "field is immutable");
  13331. goto fail;
  13332. }
  13333. field_type = struct_type->fields[field_idx].field_type;
  13334. if (is_packed_type(field_type)) {
  13335. if (opcode1 == WASM_OP_STRUCT_GET) {
  13336. set_error_buf(error_buf, error_buf_size,
  13337. "type mismatch");
  13338. goto fail;
  13339. }
  13340. else {
  13341. field_type = VALUE_TYPE_I32;
  13342. }
  13343. }
  13344. if (wasm_is_type_multi_byte_type(field_type)) {
  13345. ref_type = wasm_reftype_map_find(
  13346. struct_type->ref_type_maps,
  13347. struct_type->ref_type_map_count, field_idx);
  13348. bh_assert(ref_type);
  13349. }
  13350. if (opcode1 == WASM_OP_STRUCT_SET) {
  13351. /* POP field */
  13352. if (wasm_is_type_multi_byte_type(field_type)) {
  13353. bh_memcpy_s(&wasm_ref_type,
  13354. (uint32)sizeof(WASMRefType),
  13355. ref_type,
  13356. wasm_reftype_struct_size(ref_type));
  13357. }
  13358. POP_REF(field_type);
  13359. /* POP struct obj, (ref null $t) */
  13360. wasm_set_refheaptype_typeidx(
  13361. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13362. POP_REF(wasm_ref_type.ref_type);
  13363. }
  13364. else {
  13365. /* POP struct obj, (ref null $t) */
  13366. wasm_set_refheaptype_typeidx(
  13367. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13368. POP_REF(wasm_ref_type.ref_type);
  13369. /* PUSH field */
  13370. if (wasm_is_type_multi_byte_type(field_type)) {
  13371. bh_memcpy_s(&wasm_ref_type,
  13372. (uint32)sizeof(WASMRefType),
  13373. ref_type,
  13374. wasm_reftype_struct_size(ref_type));
  13375. }
  13376. PUSH_REF(field_type);
  13377. }
  13378. break;
  13379. }
  13380. case WASM_OP_ARRAY_NEW:
  13381. case WASM_OP_ARRAY_NEW_DEFAULT:
  13382. case WASM_OP_ARRAY_NEW_FIXED:
  13383. case WASM_OP_ARRAY_NEW_DATA:
  13384. case WASM_OP_ARRAY_NEW_ELEM:
  13385. {
  13386. WASMArrayType *array_type;
  13387. uint8 elem_type;
  13388. uint32 u32 = 0;
  13389. pb_read_leb_uint32(p, p_end, type_idx);
  13390. #if WASM_ENABLE_FAST_INTERP != 0
  13391. emit_uint32(loader_ctx, type_idx);
  13392. #endif
  13393. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  13394. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  13395. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13396. pb_read_leb_uint32(p, p_end, u32);
  13397. #if WASM_ENABLE_FAST_INTERP != 0
  13398. emit_uint32(loader_ctx, u32);
  13399. #endif
  13400. }
  13401. if (!check_array_type(module, type_idx, error_buf,
  13402. error_buf_size)) {
  13403. goto fail;
  13404. }
  13405. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  13406. /* length */
  13407. POP_I32();
  13408. }
  13409. array_type = (WASMArrayType *)module->types[type_idx];
  13410. elem_type = array_type->elem_type;
  13411. if (opcode1 == WASM_OP_ARRAY_NEW
  13412. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13413. if (wasm_is_type_multi_byte_type(elem_type)) {
  13414. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13415. array_type->elem_ref_type,
  13416. wasm_reftype_struct_size(
  13417. array_type->elem_ref_type));
  13418. }
  13419. if (is_packed_type(elem_type)) {
  13420. elem_type = VALUE_TYPE_I32;
  13421. }
  13422. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13423. uint32 N = u32;
  13424. for (i = 0; i < N; i++) {
  13425. if (wasm_is_type_multi_byte_type(
  13426. elem_type)) {
  13427. bh_memcpy_s(
  13428. &wasm_ref_type, sizeof(WASMRefType),
  13429. array_type->elem_ref_type,
  13430. wasm_reftype_struct_size(
  13431. array_type->elem_ref_type));
  13432. }
  13433. POP_REF(elem_type);
  13434. }
  13435. }
  13436. else
  13437. POP_REF(elem_type);
  13438. }
  13439. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  13440. /* offset of data segment */
  13441. POP_I32();
  13442. if (u32 >= module->data_seg_count) {
  13443. set_error_buf(error_buf, error_buf_size,
  13444. "unknown data segment");
  13445. goto fail;
  13446. }
  13447. if (wasm_is_type_reftype(elem_type)) {
  13448. set_error_buf(error_buf, error_buf_size,
  13449. "array elem type mismatch");
  13450. goto fail;
  13451. }
  13452. }
  13453. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13454. WASMTableSeg *table_seg =
  13455. module->table_segments + u32;
  13456. /* offset of element segment */
  13457. POP_I32();
  13458. if (u32 >= module->table_seg_count) {
  13459. set_error_buf(error_buf, error_buf_size,
  13460. "unknown element segment");
  13461. goto fail;
  13462. }
  13463. if (!wasm_reftype_is_subtype_of(
  13464. table_seg->elem_type,
  13465. table_seg->elem_ref_type, elem_type,
  13466. array_type->elem_ref_type, module->types,
  13467. module->type_count)) {
  13468. set_error_buf(error_buf, error_buf_size,
  13469. "array elem type mismatch");
  13470. goto fail;
  13471. }
  13472. }
  13473. /* PUSH array obj, (ref $t) */
  13474. wasm_set_refheaptype_typeidx(
  13475. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13476. PUSH_REF(wasm_ref_type.ref_type);
  13477. break;
  13478. }
  13479. case WASM_OP_ARRAY_GET:
  13480. case WASM_OP_ARRAY_GET_S:
  13481. case WASM_OP_ARRAY_GET_U:
  13482. case WASM_OP_ARRAY_SET:
  13483. {
  13484. uint8 elem_type;
  13485. WASMArrayType *array_type;
  13486. WASMRefType *ref_type = NULL;
  13487. pb_read_leb_uint32(p, p_end, type_idx);
  13488. #if WASM_ENABLE_FAST_INTERP != 0
  13489. emit_uint32(loader_ctx, type_idx);
  13490. #endif
  13491. if (!check_array_type(module, type_idx, error_buf,
  13492. error_buf_size)) {
  13493. goto fail;
  13494. }
  13495. array_type = (WASMArrayType *)module->types[type_idx];
  13496. if (opcode1 == WASM_OP_ARRAY_SET
  13497. && !(array_type->elem_flags & 1)) {
  13498. set_error_buf(error_buf, error_buf_size,
  13499. "array is immutable");
  13500. goto fail;
  13501. }
  13502. elem_type = array_type->elem_type;
  13503. if (is_packed_type(elem_type)) {
  13504. if (opcode1 != WASM_OP_ARRAY_GET_S
  13505. && opcode1 != WASM_OP_ARRAY_GET_U
  13506. && opcode1 != WASM_OP_ARRAY_SET) {
  13507. set_error_buf(error_buf, error_buf_size,
  13508. "type mismatch");
  13509. goto fail;
  13510. }
  13511. else {
  13512. elem_type = VALUE_TYPE_I32;
  13513. }
  13514. }
  13515. ref_type = array_type->elem_ref_type;
  13516. if (opcode1 == WASM_OP_ARRAY_SET) {
  13517. /* POP elem to set */
  13518. if (wasm_is_type_multi_byte_type(elem_type)) {
  13519. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13520. ref_type,
  13521. wasm_reftype_struct_size(ref_type));
  13522. }
  13523. POP_REF(elem_type);
  13524. }
  13525. /* elem idx */
  13526. POP_I32();
  13527. /* POP array obj, (ref null $t) */
  13528. wasm_set_refheaptype_typeidx(
  13529. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13530. POP_REF(wasm_ref_type.ref_type);
  13531. if (opcode1 != WASM_OP_ARRAY_SET) {
  13532. /* PUSH elem */
  13533. if (wasm_is_type_multi_byte_type(elem_type)) {
  13534. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13535. ref_type,
  13536. wasm_reftype_struct_size(ref_type));
  13537. }
  13538. PUSH_REF(elem_type);
  13539. }
  13540. break;
  13541. }
  13542. case WASM_OP_ARRAY_LEN:
  13543. {
  13544. POP_REF(REF_TYPE_ARRAYREF);
  13545. /* length */
  13546. PUSH_I32();
  13547. break;
  13548. }
  13549. case WASM_OP_ARRAY_FILL:
  13550. {
  13551. WASMArrayType *array_type;
  13552. uint8 elem_type;
  13553. /* typeidx */
  13554. pb_read_leb_uint32(p, p_end, type_idx);
  13555. #if WASM_ENABLE_FAST_INTERP != 0
  13556. emit_uint32(loader_ctx, type_idx);
  13557. #endif
  13558. if (!check_array_type(module, type_idx, error_buf,
  13559. error_buf_size)) {
  13560. goto fail;
  13561. }
  13562. array_type = (WASMArrayType *)module->types[type_idx];
  13563. if (!(array_type->elem_flags & 1)) {
  13564. set_error_buf(error_buf, error_buf_size,
  13565. "array is immutable");
  13566. goto fail;
  13567. }
  13568. elem_type = array_type->elem_type;
  13569. if (is_packed_type(elem_type)) {
  13570. elem_type = VALUE_TYPE_I32;
  13571. }
  13572. POP_I32(); /* length */
  13573. #if WASM_ENABLE_FAST_INTERP != 0
  13574. POP_OFFSET_TYPE(elem_type);
  13575. #endif
  13576. POP_TYPE(elem_type);
  13577. POP_I32(); /* start */
  13578. /* POP array obj, (ref null $t) */
  13579. wasm_set_refheaptype_typeidx(
  13580. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13581. POP_REF(wasm_ref_type.ref_type);
  13582. break;
  13583. }
  13584. case WASM_OP_ARRAY_COPY:
  13585. {
  13586. uint32 src_type_idx;
  13587. uint8 src_elem_type, dst_elem_type;
  13588. WASMRefType src_ref_type = { 0 },
  13589. *src_elem_ref_type = NULL;
  13590. WASMRefType dst_ref_type = { 0 },
  13591. *dst_elem_ref_type = NULL;
  13592. WASMArrayType *array_type;
  13593. /* typeidx1 */
  13594. pb_read_leb_uint32(p, p_end, type_idx);
  13595. #if WASM_ENABLE_FAST_INTERP != 0
  13596. emit_uint32(loader_ctx, type_idx);
  13597. #endif
  13598. /* typeidx2 */
  13599. pb_read_leb_uint32(p, p_end, src_type_idx);
  13600. #if WASM_ENABLE_FAST_INTERP != 0
  13601. emit_uint32(loader_ctx, src_type_idx);
  13602. #endif
  13603. if (!check_array_type(module, type_idx, error_buf,
  13604. error_buf_size)) {
  13605. goto fail;
  13606. }
  13607. if (!check_array_type(module, src_type_idx, error_buf,
  13608. error_buf_size)) {
  13609. goto fail;
  13610. }
  13611. POP_I32();
  13612. POP_I32();
  13613. /* POP array obj, (ref null $t) */
  13614. wasm_set_refheaptype_typeidx(
  13615. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13616. POP_REF(wasm_ref_type.ref_type);
  13617. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13618. &wasm_ref_type,
  13619. wasm_reftype_struct_size(&wasm_ref_type));
  13620. POP_I32();
  13621. /* POP array obj, (ref null $t) */
  13622. wasm_set_refheaptype_typeidx(
  13623. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13624. POP_REF(wasm_ref_type.ref_type);
  13625. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13626. &wasm_ref_type,
  13627. wasm_reftype_struct_size(&wasm_ref_type));
  13628. array_type = (WASMArrayType *)module->types[type_idx];
  13629. if (!(array_type->elem_flags & 1)) {
  13630. set_error_buf(error_buf, error_buf_size,
  13631. "destination array is immutable");
  13632. goto fail;
  13633. }
  13634. dst_elem_type = array_type->elem_type;
  13635. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13636. dst_elem_ref_type = array_type->elem_ref_type;
  13637. }
  13638. array_type =
  13639. (WASMArrayType *)module->types[src_type_idx];
  13640. src_elem_type = array_type->elem_type;
  13641. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13642. src_elem_ref_type = array_type->elem_ref_type;
  13643. }
  13644. if (!wasm_reftype_is_subtype_of(
  13645. src_elem_type, src_elem_ref_type, dst_elem_type,
  13646. dst_elem_ref_type, module->types,
  13647. module->type_count)) {
  13648. set_error_buf(error_buf, error_buf_size,
  13649. "array types do not match");
  13650. goto fail;
  13651. }
  13652. break;
  13653. }
  13654. case WASM_OP_REF_I31:
  13655. {
  13656. POP_I32();
  13657. wasm_set_refheaptype_common(
  13658. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13659. PUSH_REF(wasm_ref_type.ref_type);
  13660. break;
  13661. }
  13662. case WASM_OP_I31_GET_S:
  13663. case WASM_OP_I31_GET_U:
  13664. {
  13665. POP_REF(REF_TYPE_I31REF);
  13666. PUSH_I32();
  13667. break;
  13668. }
  13669. case WASM_OP_REF_TEST:
  13670. case WASM_OP_REF_CAST:
  13671. case WASM_OP_REF_TEST_NULLABLE:
  13672. case WASM_OP_REF_CAST_NULLABLE:
  13673. {
  13674. uint8 type;
  13675. pb_read_leb_int32(p, p_end, heap_type);
  13676. #if WASM_ENABLE_FAST_INTERP != 0
  13677. emit_uint32(loader_ctx, (uint32)heap_type);
  13678. #endif
  13679. if (heap_type >= 0) {
  13680. if (!check_type_index(module, module->type_count,
  13681. heap_type, error_buf,
  13682. error_buf_size)) {
  13683. goto fail;
  13684. }
  13685. }
  13686. else {
  13687. if (!wasm_is_valid_heap_type(heap_type)) {
  13688. set_error_buf(error_buf, error_buf_size,
  13689. "unknown type");
  13690. goto fail;
  13691. }
  13692. }
  13693. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13694. &wasm_ref_type, error_buf,
  13695. error_buf_size)) {
  13696. goto fail;
  13697. }
  13698. if (opcode1 == WASM_OP_REF_TEST
  13699. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13700. PUSH_I32();
  13701. else {
  13702. bool nullable =
  13703. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13704. : false;
  13705. if (heap_type >= 0 || !nullable) {
  13706. wasm_set_refheaptype_typeidx(
  13707. &wasm_ref_type.ref_ht_typeidx, nullable,
  13708. heap_type);
  13709. PUSH_REF(wasm_ref_type.ref_type);
  13710. }
  13711. else {
  13712. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13713. }
  13714. }
  13715. break;
  13716. }
  13717. case WASM_OP_BR_ON_CAST:
  13718. case WASM_OP_BR_ON_CAST_FAIL:
  13719. {
  13720. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13721. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13722. uint8 type_tmp, castflags;
  13723. uint32 depth;
  13724. int32 heap_type_dst;
  13725. bool src_nullable, dst_nullable;
  13726. CHECK_BUF(p, p_end, 1);
  13727. castflags = read_uint8(p);
  13728. #if WASM_ENABLE_FAST_INTERP != 0
  13729. /* Emit heap_type firstly */
  13730. emit_byte(loader_ctx, castflags);
  13731. #endif
  13732. p_org = p;
  13733. pb_read_leb_uint32(p, p_end, depth);
  13734. pb_read_leb_int32(p, p_end, heap_type);
  13735. #if WASM_ENABLE_FAST_INTERP != 0
  13736. /* Emit heap_type firstly */
  13737. emit_uint32(loader_ctx, (uint32)heap_type);
  13738. #endif
  13739. pb_read_leb_int32(p, p_end, heap_type_dst);
  13740. #if WASM_ENABLE_FAST_INTERP != 0
  13741. /* Emit heap_type firstly */
  13742. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13743. #endif
  13744. (void)depth;
  13745. /*
  13746. * castflags should be 0~3:
  13747. * 0: (non-null, non-null)
  13748. * 1: (null, non-null)
  13749. * 2: (non-null, null)
  13750. * 3: (null, null)
  13751. */
  13752. if (castflags > 3) {
  13753. set_error_buf(error_buf, error_buf_size,
  13754. "invalid castflags");
  13755. break;
  13756. }
  13757. src_nullable =
  13758. (castflags == 1) || (castflags == 3) ? true : false;
  13759. dst_nullable =
  13760. (castflags == 2) || (castflags == 3) ? true : false;
  13761. /* Pop and backup the stack top's ref type */
  13762. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13763. &ref_type_tmp, error_buf,
  13764. error_buf_size)) {
  13765. goto fail;
  13766. }
  13767. /* The reference type rt1 must be valid */
  13768. if (!init_ref_type(module, &ref_type1, src_nullable,
  13769. heap_type, error_buf,
  13770. error_buf_size)) {
  13771. goto fail;
  13772. }
  13773. /* The reference type rt2 must be valid. */
  13774. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13775. heap_type_dst, error_buf,
  13776. error_buf_size)) {
  13777. goto fail;
  13778. }
  13779. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13780. &ref_type2);
  13781. /* The reference type rt2 must match rt1. */
  13782. if (!wasm_reftype_is_subtype_of(
  13783. ref_type2.ref_type, &ref_type2,
  13784. ref_type1.ref_type, &ref_type1, module->types,
  13785. module->type_count)) {
  13786. set_error_buf(error_buf, error_buf_size,
  13787. "type mismatch");
  13788. goto fail;
  13789. }
  13790. p = p_org;
  13791. /* Push ref type casted for branch block check */
  13792. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13793. /* The reference type rt2 must match rt′. */
  13794. type_tmp = ref_type2.ref_type;
  13795. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13796. bh_memcpy_s(
  13797. &wasm_ref_type,
  13798. wasm_reftype_struct_size(&ref_type2),
  13799. &ref_type2,
  13800. wasm_reftype_struct_size(&ref_type2));
  13801. }
  13802. }
  13803. else {
  13804. /* The reference type rt′1 must match rt′. */
  13805. type_tmp = ref_type_diff.ref_type;
  13806. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13807. bh_memcpy_s(
  13808. &wasm_ref_type,
  13809. wasm_reftype_struct_size(&ref_type_diff),
  13810. &ref_type_diff,
  13811. wasm_reftype_struct_size(&ref_type_diff));
  13812. }
  13813. }
  13814. PUSH_REF(type_tmp);
  13815. if (!(frame_csp_tmp = check_branch_block(
  13816. loader_ctx, &p, p_end, opcode, error_buf,
  13817. error_buf_size))) {
  13818. goto fail;
  13819. }
  13820. /* Ignore heap_types */
  13821. skip_leb_uint32(p, p_end);
  13822. skip_leb_uint32(p, p_end);
  13823. /* Restore the original stack top's ref type */
  13824. POP_REF(type_tmp);
  13825. #if WASM_ENABLE_FAST_INTERP != 0
  13826. /* Erase the opnd offset emitted by POP_REF() */
  13827. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13828. #endif
  13829. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13830. type_tmp = ref_type_diff.ref_type;
  13831. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13832. bh_memcpy_s(
  13833. &wasm_ref_type,
  13834. wasm_reftype_struct_size(&ref_type_diff),
  13835. &ref_type_diff,
  13836. wasm_reftype_struct_size(&ref_type_diff));
  13837. }
  13838. }
  13839. else {
  13840. type_tmp = ref_type2.ref_type;
  13841. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13842. bh_memcpy_s(
  13843. &wasm_ref_type,
  13844. wasm_reftype_struct_size(&ref_type2),
  13845. &ref_type2,
  13846. wasm_reftype_struct_size(&ref_type2));
  13847. }
  13848. }
  13849. PUSH_REF(type_tmp);
  13850. #if WASM_ENABLE_FAST_INTERP != 0
  13851. /* Erase the opnd offset emitted by PUSH_REF() */
  13852. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13853. #endif
  13854. break;
  13855. }
  13856. case WASM_OP_ANY_CONVERT_EXTERN:
  13857. {
  13858. uint8 type;
  13859. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13860. &wasm_ref_type, error_buf,
  13861. error_buf_size)) {
  13862. goto fail;
  13863. }
  13864. if (!(type == REF_TYPE_EXTERNREF
  13865. || (type == REF_TYPE_HT_NON_NULLABLE
  13866. && wasm_ref_type.ref_ht_common.heap_type
  13867. == HEAP_TYPE_EXTERN)
  13868. || type == VALUE_TYPE_ANY)) {
  13869. set_error_buf(error_buf, error_buf_size,
  13870. "type mismatch");
  13871. goto fail;
  13872. }
  13873. if (type == REF_TYPE_EXTERNREF)
  13874. type = REF_TYPE_ANYREF;
  13875. else {
  13876. wasm_ref_type.ref_ht_common.heap_type =
  13877. HEAP_TYPE_ANY;
  13878. }
  13879. PUSH_REF(type);
  13880. break;
  13881. }
  13882. case WASM_OP_EXTERN_CONVERT_ANY:
  13883. {
  13884. uint8 type;
  13885. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13886. &wasm_ref_type, error_buf,
  13887. error_buf_size)) {
  13888. goto fail;
  13889. }
  13890. if (type == REF_TYPE_EXTERNREF
  13891. || ((type == REF_TYPE_HT_NULLABLE
  13892. || type == REF_TYPE_HT_NON_NULLABLE)
  13893. && wasm_ref_type.ref_ht_common.heap_type
  13894. == HEAP_TYPE_EXTERN)) {
  13895. set_error_buf(error_buf, error_buf_size,
  13896. "type mismatch");
  13897. goto fail;
  13898. }
  13899. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13900. /* push (ref null extern) */
  13901. type = REF_TYPE_EXTERNREF;
  13902. }
  13903. else {
  13904. /* push (ref extern) */
  13905. type = REF_TYPE_HT_NON_NULLABLE;
  13906. wasm_set_refheaptype_common(
  13907. &wasm_ref_type.ref_ht_common, false,
  13908. HEAP_TYPE_EXTERN);
  13909. }
  13910. PUSH_REF(type);
  13911. break;
  13912. }
  13913. #if WASM_ENABLE_STRINGREF != 0
  13914. case WASM_OP_STRING_NEW_UTF8:
  13915. case WASM_OP_STRING_NEW_WTF16:
  13916. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13917. case WASM_OP_STRING_NEW_WTF8:
  13918. {
  13919. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13920. func->has_memory_operations = true;
  13921. #endif
  13922. pb_read_leb_uint32(p, p_end, memidx);
  13923. #if WASM_ENABLE_FAST_INTERP != 0
  13924. emit_uint32(loader_ctx, (uint32)memidx);
  13925. #endif
  13926. POP_I32();
  13927. POP_I32();
  13928. PUSH_REF(REF_TYPE_STRINGREF);
  13929. break;
  13930. }
  13931. case WASM_OP_STRING_CONST:
  13932. {
  13933. uint32 contents;
  13934. pb_read_leb_uint32(p, p_end, contents);
  13935. #if WASM_ENABLE_FAST_INTERP != 0
  13936. emit_uint32(loader_ctx, (uint32)contents);
  13937. #endif
  13938. PUSH_REF(REF_TYPE_STRINGREF);
  13939. (void)contents;
  13940. break;
  13941. }
  13942. case WASM_OP_STRING_MEASURE_UTF8:
  13943. case WASM_OP_STRING_MEASURE_WTF8:
  13944. case WASM_OP_STRING_MEASURE_WTF16:
  13945. {
  13946. POP_STRINGREF();
  13947. PUSH_I32();
  13948. break;
  13949. }
  13950. case WASM_OP_STRING_ENCODE_UTF8:
  13951. case WASM_OP_STRING_ENCODE_WTF16:
  13952. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13953. case WASM_OP_STRING_ENCODE_WTF8:
  13954. {
  13955. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13956. func->has_memory_operations = true;
  13957. #endif
  13958. pb_read_leb_uint32(p, p_end, memidx);
  13959. #if WASM_ENABLE_FAST_INTERP != 0
  13960. emit_uint32(loader_ctx, (uint32)memidx);
  13961. #endif
  13962. POP_I32();
  13963. POP_STRINGREF();
  13964. PUSH_I32();
  13965. break;
  13966. }
  13967. case WASM_OP_STRING_CONCAT:
  13968. {
  13969. POP_STRINGREF();
  13970. POP_STRINGREF();
  13971. PUSH_REF(REF_TYPE_STRINGREF);
  13972. break;
  13973. }
  13974. case WASM_OP_STRING_EQ:
  13975. {
  13976. POP_STRINGREF();
  13977. POP_STRINGREF();
  13978. PUSH_I32();
  13979. break;
  13980. }
  13981. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13982. {
  13983. POP_STRINGREF();
  13984. PUSH_I32();
  13985. break;
  13986. }
  13987. case WASM_OP_STRING_AS_WTF8:
  13988. {
  13989. POP_STRINGREF();
  13990. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13991. break;
  13992. }
  13993. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13994. {
  13995. POP_I32();
  13996. POP_I32();
  13997. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13998. PUSH_I32();
  13999. break;
  14000. }
  14001. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  14002. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  14003. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  14004. {
  14005. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14006. func->has_memory_operations = true;
  14007. #endif
  14008. pb_read_leb_uint32(p, p_end, memidx);
  14009. #if WASM_ENABLE_FAST_INTERP != 0
  14010. emit_uint32(loader_ctx, (uint32)memidx);
  14011. #endif
  14012. POP_I32();
  14013. POP_I32();
  14014. POP_I32();
  14015. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  14016. PUSH_I32();
  14017. PUSH_I32();
  14018. break;
  14019. }
  14020. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  14021. {
  14022. POP_I32();
  14023. POP_I32();
  14024. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  14025. PUSH_REF(REF_TYPE_STRINGREF);
  14026. break;
  14027. }
  14028. case WASM_OP_STRING_AS_WTF16:
  14029. {
  14030. POP_STRINGREF();
  14031. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  14032. break;
  14033. }
  14034. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  14035. {
  14036. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14037. PUSH_I32();
  14038. break;
  14039. }
  14040. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  14041. {
  14042. POP_I32();
  14043. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14044. PUSH_I32();
  14045. break;
  14046. }
  14047. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  14048. {
  14049. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14050. func->has_memory_operations = true;
  14051. #endif
  14052. pb_read_leb_uint32(p, p_end, memidx);
  14053. #if WASM_ENABLE_FAST_INTERP != 0
  14054. emit_uint32(loader_ctx, (uint32)memidx);
  14055. #endif
  14056. POP_I32();
  14057. POP_I32();
  14058. POP_I32();
  14059. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14060. PUSH_I32();
  14061. break;
  14062. }
  14063. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  14064. {
  14065. POP_I32();
  14066. POP_I32();
  14067. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14068. PUSH_REF(REF_TYPE_STRINGREF);
  14069. break;
  14070. }
  14071. case WASM_OP_STRING_AS_ITER:
  14072. {
  14073. POP_STRINGREF();
  14074. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  14075. break;
  14076. }
  14077. case WASM_OP_STRINGVIEW_ITER_NEXT:
  14078. {
  14079. POP_REF(REF_TYPE_STRINGVIEWITER);
  14080. PUSH_I32();
  14081. break;
  14082. }
  14083. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  14084. case WASM_OP_STRINGVIEW_ITER_REWIND:
  14085. {
  14086. POP_I32();
  14087. POP_REF(REF_TYPE_STRINGVIEWITER);
  14088. PUSH_I32();
  14089. break;
  14090. }
  14091. case WASM_OP_STRINGVIEW_ITER_SLICE:
  14092. {
  14093. POP_I32();
  14094. POP_REF(REF_TYPE_STRINGVIEWITER);
  14095. PUSH_REF(REF_TYPE_STRINGREF);
  14096. break;
  14097. }
  14098. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  14099. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  14100. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  14101. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  14102. {
  14103. POP_I32();
  14104. POP_I32();
  14105. POP_REF(REF_TYPE_ARRAYREF);
  14106. PUSH_REF(REF_TYPE_STRINGREF);
  14107. break;
  14108. }
  14109. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  14110. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  14111. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  14112. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  14113. {
  14114. POP_I32();
  14115. POP_REF(REF_TYPE_ARRAYREF);
  14116. POP_STRINGREF();
  14117. PUSH_I32();
  14118. break;
  14119. }
  14120. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  14121. default:
  14122. set_error_buf_v(error_buf, error_buf_size,
  14123. "%s %02x %02x", "unsupported opcode",
  14124. 0xfb, opcode1);
  14125. goto fail;
  14126. }
  14127. break;
  14128. }
  14129. #endif /* end of WASM_ENABLE_GC != 0 */
  14130. case WASM_OP_MISC_PREFIX:
  14131. {
  14132. uint32 opcode1;
  14133. pb_read_leb_uint32(p, p_end, opcode1);
  14134. #if WASM_ENABLE_FAST_INTERP != 0
  14135. emit_byte(loader_ctx, ((uint8)opcode1));
  14136. #endif
  14137. switch (opcode1) {
  14138. case WASM_OP_I32_TRUNC_SAT_S_F32:
  14139. case WASM_OP_I32_TRUNC_SAT_U_F32:
  14140. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  14141. break;
  14142. case WASM_OP_I32_TRUNC_SAT_S_F64:
  14143. case WASM_OP_I32_TRUNC_SAT_U_F64:
  14144. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  14145. break;
  14146. case WASM_OP_I64_TRUNC_SAT_S_F32:
  14147. case WASM_OP_I64_TRUNC_SAT_U_F32:
  14148. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  14149. break;
  14150. case WASM_OP_I64_TRUNC_SAT_S_F64:
  14151. case WASM_OP_I64_TRUNC_SAT_U_F64:
  14152. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  14153. break;
  14154. #if WASM_ENABLE_BULK_MEMORY != 0
  14155. case WASM_OP_MEMORY_INIT:
  14156. {
  14157. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14158. #if WASM_ENABLE_FAST_INTERP != 0
  14159. emit_uint32(loader_ctx, data_seg_idx);
  14160. #endif
  14161. if (module->import_memory_count == 0
  14162. && module->memory_count == 0)
  14163. goto fail_unknown_memory;
  14164. pb_read_leb_uint32(p, p_end, memidx);
  14165. check_memidx(module, memidx);
  14166. if (data_seg_idx >= module->data_seg_count) {
  14167. set_error_buf_v(error_buf, error_buf_size,
  14168. "unknown data segment %d",
  14169. data_seg_idx);
  14170. goto fail;
  14171. }
  14172. if (module->data_seg_count1 == 0)
  14173. goto fail_data_cnt_sec_require;
  14174. POP_I32();
  14175. POP_I32();
  14176. POP_MEM_OFFSET();
  14177. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14178. func->has_memory_operations = true;
  14179. #endif
  14180. #if WASM_ENABLE_WAMR_COMPILER != 0
  14181. module->is_bulk_memory_used = true;
  14182. #endif
  14183. break;
  14184. }
  14185. case WASM_OP_DATA_DROP:
  14186. {
  14187. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14188. #if WASM_ENABLE_FAST_INTERP != 0
  14189. emit_uint32(loader_ctx, data_seg_idx);
  14190. #endif
  14191. if (data_seg_idx >= module->data_seg_count) {
  14192. set_error_buf(error_buf, error_buf_size,
  14193. "unknown data segment");
  14194. goto fail;
  14195. }
  14196. if (module->data_seg_count1 == 0)
  14197. goto fail_data_cnt_sec_require;
  14198. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14199. func->has_memory_operations = true;
  14200. #endif
  14201. #if WASM_ENABLE_WAMR_COMPILER != 0
  14202. module->is_bulk_memory_used = true;
  14203. #endif
  14204. break;
  14205. }
  14206. case WASM_OP_MEMORY_COPY:
  14207. {
  14208. CHECK_BUF(p, p_end, sizeof(int16));
  14209. /* check both src and dst memory index */
  14210. pb_read_leb_uint32(p, p_end, memidx);
  14211. check_memidx(module, memidx);
  14212. pb_read_leb_uint32(p, p_end, memidx);
  14213. check_memidx(module, memidx);
  14214. if (module->import_memory_count == 0
  14215. && module->memory_count == 0)
  14216. goto fail_unknown_memory;
  14217. POP_MEM_OFFSET();
  14218. POP_MEM_OFFSET();
  14219. POP_MEM_OFFSET();
  14220. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14221. func->has_memory_operations = true;
  14222. #endif
  14223. #if WASM_ENABLE_WAMR_COMPILER != 0
  14224. module->is_bulk_memory_used = true;
  14225. #endif
  14226. break;
  14227. }
  14228. case WASM_OP_MEMORY_FILL:
  14229. {
  14230. pb_read_leb_uint32(p, p_end, memidx);
  14231. check_memidx(module, memidx);
  14232. if (module->import_memory_count == 0
  14233. && module->memory_count == 0) {
  14234. goto fail_unknown_memory;
  14235. }
  14236. POP_MEM_OFFSET();
  14237. POP_I32();
  14238. POP_MEM_OFFSET();
  14239. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14240. func->has_memory_operations = true;
  14241. #endif
  14242. #if WASM_ENABLE_WAMR_COMPILER != 0
  14243. module->is_bulk_memory_used = true;
  14244. #endif
  14245. break;
  14246. }
  14247. fail_unknown_memory:
  14248. set_error_buf(error_buf, error_buf_size,
  14249. "unknown memory 0");
  14250. goto fail;
  14251. fail_data_cnt_sec_require:
  14252. set_error_buf(error_buf, error_buf_size,
  14253. "data count section required");
  14254. goto fail;
  14255. #endif /* WASM_ENABLE_BULK_MEMORY */
  14256. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  14257. case WASM_OP_TABLE_INIT:
  14258. {
  14259. uint8 seg_type = 0, tbl_type = 0;
  14260. #if WASM_ENABLE_GC != 0
  14261. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  14262. #endif
  14263. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14264. pb_read_leb_uint32(p, p_end, table_idx);
  14265. if (!get_table_elem_type(module, table_idx, &tbl_type,
  14266. #if WASM_ENABLE_GC != 0
  14267. (void **)&tbl_ref_type,
  14268. #else
  14269. NULL,
  14270. #endif
  14271. error_buf, error_buf_size))
  14272. goto fail;
  14273. if (!get_table_seg_elem_type(module, table_seg_idx,
  14274. &seg_type,
  14275. #if WASM_ENABLE_GC != 0
  14276. (void **)&seg_ref_type,
  14277. #else
  14278. NULL,
  14279. #endif
  14280. error_buf, error_buf_size))
  14281. goto fail;
  14282. #if WASM_ENABLE_GC == 0
  14283. if (seg_type != tbl_type) {
  14284. set_error_buf(error_buf, error_buf_size,
  14285. "type mismatch");
  14286. goto fail;
  14287. }
  14288. #else
  14289. if (!wasm_reftype_is_subtype_of(
  14290. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  14291. module->types, module->type_count)) {
  14292. set_error_buf(error_buf, error_buf_size,
  14293. "type mismatch");
  14294. goto fail;
  14295. }
  14296. #endif
  14297. #if WASM_ENABLE_FAST_INTERP != 0
  14298. emit_uint32(loader_ctx, table_seg_idx);
  14299. emit_uint32(loader_ctx, table_idx);
  14300. #endif
  14301. POP_I32();
  14302. POP_I32();
  14303. #if WASM_ENABLE_MEMORY64 != 0
  14304. table_elem_idx_type = is_table_64bit(module, table_idx)
  14305. ? VALUE_TYPE_I64
  14306. : VALUE_TYPE_I32;
  14307. #endif
  14308. POP_TBL_ELEM_IDX();
  14309. #if WASM_ENABLE_WAMR_COMPILER != 0
  14310. module->is_ref_types_used = true;
  14311. #endif
  14312. break;
  14313. }
  14314. case WASM_OP_ELEM_DROP:
  14315. {
  14316. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14317. if (!get_table_seg_elem_type(module, table_seg_idx,
  14318. NULL, NULL, error_buf,
  14319. error_buf_size))
  14320. goto fail;
  14321. #if WASM_ENABLE_FAST_INTERP != 0
  14322. emit_uint32(loader_ctx, table_seg_idx);
  14323. #endif
  14324. #if WASM_ENABLE_WAMR_COMPILER != 0
  14325. module->is_ref_types_used = true;
  14326. #endif
  14327. break;
  14328. }
  14329. case WASM_OP_TABLE_COPY:
  14330. {
  14331. uint8 src_type, dst_type, src_tbl_idx_type,
  14332. dst_tbl_idx_type, min_tbl_idx_type;
  14333. #if WASM_ENABLE_GC != 0
  14334. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  14335. #endif
  14336. uint32 src_tbl_idx, dst_tbl_idx;
  14337. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  14338. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  14339. #if WASM_ENABLE_GC != 0
  14340. (void **)&dst_ref_type,
  14341. #else
  14342. NULL,
  14343. #endif
  14344. error_buf, error_buf_size))
  14345. goto fail;
  14346. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  14347. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  14348. #if WASM_ENABLE_GC != 0
  14349. (void **)&src_ref_type,
  14350. #else
  14351. NULL,
  14352. #endif
  14353. error_buf, error_buf_size))
  14354. goto fail;
  14355. #if WASM_ENABLE_GC == 0
  14356. if (src_type != dst_type) {
  14357. set_error_buf(error_buf, error_buf_size,
  14358. "type mismatch");
  14359. goto fail;
  14360. }
  14361. #else
  14362. if (!wasm_reftype_is_subtype_of(
  14363. src_type, src_ref_type, dst_type, dst_ref_type,
  14364. module->types, module->type_count)) {
  14365. set_error_buf(error_buf, error_buf_size,
  14366. "type mismatch");
  14367. goto fail;
  14368. }
  14369. #endif
  14370. #if WASM_ENABLE_FAST_INTERP != 0
  14371. emit_uint32(loader_ctx, dst_tbl_idx);
  14372. emit_uint32(loader_ctx, src_tbl_idx);
  14373. #endif
  14374. #if WASM_ENABLE_MEMORY64 != 0
  14375. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  14376. ? VALUE_TYPE_I64
  14377. : VALUE_TYPE_I32;
  14378. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  14379. ? VALUE_TYPE_I64
  14380. : VALUE_TYPE_I32;
  14381. min_tbl_idx_type =
  14382. (src_tbl_idx_type == VALUE_TYPE_I32
  14383. || dst_tbl_idx_type == VALUE_TYPE_I32)
  14384. ? VALUE_TYPE_I32
  14385. : VALUE_TYPE_I64;
  14386. #else
  14387. src_tbl_idx_type = VALUE_TYPE_I32;
  14388. dst_tbl_idx_type = VALUE_TYPE_I32;
  14389. min_tbl_idx_type = VALUE_TYPE_I32;
  14390. #endif
  14391. table_elem_idx_type = min_tbl_idx_type;
  14392. POP_TBL_ELEM_IDX();
  14393. table_elem_idx_type = src_tbl_idx_type;
  14394. POP_TBL_ELEM_IDX();
  14395. table_elem_idx_type = dst_tbl_idx_type;
  14396. POP_TBL_ELEM_IDX();
  14397. #if WASM_ENABLE_WAMR_COMPILER != 0
  14398. module->is_ref_types_used = true;
  14399. #endif
  14400. break;
  14401. }
  14402. case WASM_OP_TABLE_SIZE:
  14403. {
  14404. pb_read_leb_uint32(p, p_end, table_idx);
  14405. /* TODO: shall we create a new function to check
  14406. table idx instead of using below function? */
  14407. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  14408. error_buf, error_buf_size))
  14409. goto fail;
  14410. #if WASM_ENABLE_FAST_INTERP != 0
  14411. emit_uint32(loader_ctx, table_idx);
  14412. #endif
  14413. #if WASM_ENABLE_MEMORY64 != 0
  14414. table_elem_idx_type = is_table_64bit(module, table_idx)
  14415. ? VALUE_TYPE_I64
  14416. : VALUE_TYPE_I32;
  14417. #endif
  14418. PUSH_TBL_ELEM_IDX();
  14419. #if WASM_ENABLE_WAMR_COMPILER != 0
  14420. module->is_ref_types_used = true;
  14421. #endif
  14422. break;
  14423. }
  14424. case WASM_OP_TABLE_GROW:
  14425. case WASM_OP_TABLE_FILL:
  14426. {
  14427. uint8 decl_type;
  14428. #if WASM_ENABLE_GC != 0
  14429. WASMRefType *ref_type = NULL;
  14430. #endif
  14431. pb_read_leb_uint32(p, p_end, table_idx);
  14432. if (!get_table_elem_type(module, table_idx, &decl_type,
  14433. #if WASM_ENABLE_GC != 0
  14434. (void **)&ref_type,
  14435. #else
  14436. NULL,
  14437. #endif
  14438. error_buf, error_buf_size))
  14439. goto fail;
  14440. #if WASM_ENABLE_GC != 0
  14441. if (wasm_is_type_multi_byte_type(decl_type)) {
  14442. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  14443. ref_type,
  14444. wasm_reftype_struct_size(ref_type));
  14445. }
  14446. #endif
  14447. if (opcode1 == WASM_OP_TABLE_GROW) {
  14448. if (table_idx < module->import_table_count) {
  14449. module->import_tables[table_idx]
  14450. .u.table.table_type.possible_grow = true;
  14451. }
  14452. else {
  14453. module
  14454. ->tables[table_idx
  14455. - module->import_table_count]
  14456. .table_type.possible_grow = true;
  14457. }
  14458. }
  14459. #if WASM_ENABLE_FAST_INTERP != 0
  14460. emit_uint32(loader_ctx, table_idx);
  14461. #endif
  14462. #if WASM_ENABLE_MEMORY64 != 0
  14463. table_elem_idx_type = is_table_64bit(module, table_idx)
  14464. ? VALUE_TYPE_I64
  14465. : VALUE_TYPE_I32;
  14466. #endif
  14467. POP_TBL_ELEM_IDX();
  14468. #if WASM_ENABLE_FAST_INTERP != 0
  14469. POP_OFFSET_TYPE(decl_type);
  14470. #endif
  14471. POP_TYPE(decl_type);
  14472. if (opcode1 == WASM_OP_TABLE_GROW)
  14473. PUSH_TBL_ELEM_IDX();
  14474. else
  14475. POP_TBL_ELEM_IDX();
  14476. #if WASM_ENABLE_WAMR_COMPILER != 0
  14477. module->is_ref_types_used = true;
  14478. #endif
  14479. break;
  14480. }
  14481. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14482. default:
  14483. set_error_buf_v(error_buf, error_buf_size,
  14484. "%s %02x %02x", "unsupported opcode",
  14485. 0xfc, opcode1);
  14486. goto fail;
  14487. }
  14488. break;
  14489. }
  14490. #if WASM_ENABLE_SIMD != 0
  14491. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14492. || (WASM_ENABLE_FAST_INTERP != 0)
  14493. case WASM_OP_SIMD_PREFIX:
  14494. {
  14495. uint32 opcode1;
  14496. #if WASM_ENABLE_WAMR_COMPILER != 0
  14497. /* Mark the SIMD instruction is used in this module */
  14498. module->is_simd_used = true;
  14499. #endif
  14500. pb_read_leb_uint32(p, p_end, opcode1);
  14501. #if WASM_ENABLE_FAST_INTERP != 0
  14502. emit_byte(loader_ctx, opcode1);
  14503. #endif
  14504. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14505. */
  14506. switch (opcode1) {
  14507. /* memory instruction */
  14508. case SIMD_v128_load:
  14509. case SIMD_v128_load8x8_s:
  14510. case SIMD_v128_load8x8_u:
  14511. case SIMD_v128_load16x4_s:
  14512. case SIMD_v128_load16x4_u:
  14513. case SIMD_v128_load32x2_s:
  14514. case SIMD_v128_load32x2_u:
  14515. case SIMD_v128_load8_splat:
  14516. case SIMD_v128_load16_splat:
  14517. case SIMD_v128_load32_splat:
  14518. case SIMD_v128_load64_splat:
  14519. {
  14520. CHECK_MEMORY();
  14521. pb_read_leb_uint32(p, p_end, align); /* align */
  14522. if (!check_simd_memory_access_align(
  14523. opcode1, align, error_buf, error_buf_size)) {
  14524. goto fail;
  14525. }
  14526. pb_read_leb_mem_offset(p, p_end,
  14527. mem_offset); /* offset */
  14528. #if WASM_ENABLE_FAST_INTERP != 0
  14529. emit_uint32(loader_ctx, mem_offset);
  14530. #endif
  14531. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14532. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14533. func->has_memory_operations = true;
  14534. #endif
  14535. break;
  14536. }
  14537. case SIMD_v128_store:
  14538. {
  14539. CHECK_MEMORY();
  14540. pb_read_leb_uint32(p, p_end, align); /* align */
  14541. if (!check_simd_memory_access_align(
  14542. opcode1, align, error_buf, error_buf_size)) {
  14543. goto fail;
  14544. }
  14545. pb_read_leb_mem_offset(p, p_end,
  14546. mem_offset); /* offset */
  14547. #if WASM_ENABLE_FAST_INTERP != 0
  14548. emit_uint32(loader_ctx, mem_offset);
  14549. #endif
  14550. POP_V128();
  14551. POP_MEM_OFFSET();
  14552. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14553. func->has_memory_operations = true;
  14554. #endif
  14555. break;
  14556. }
  14557. /* basic operation */
  14558. case SIMD_v128_const:
  14559. {
  14560. #if WASM_ENABLE_FAST_INTERP != 0
  14561. uint64 high, low;
  14562. #endif
  14563. CHECK_BUF1(p, p_end, 16);
  14564. #if WASM_ENABLE_FAST_INTERP != 0
  14565. wasm_runtime_read_v128(p, &high, &low);
  14566. emit_uint64(loader_ctx, high);
  14567. emit_uint64(loader_ctx, low);
  14568. #endif
  14569. p += 16;
  14570. PUSH_V128();
  14571. break;
  14572. }
  14573. case SIMD_v8x16_shuffle:
  14574. {
  14575. V128 mask;
  14576. CHECK_BUF1(p, p_end, 16);
  14577. mask = read_i8x16(p, error_buf, error_buf_size);
  14578. if (!check_simd_shuffle_mask(mask, error_buf,
  14579. error_buf_size)) {
  14580. goto fail;
  14581. }
  14582. #if WASM_ENABLE_FAST_INTERP != 0
  14583. uint64 high, low;
  14584. wasm_runtime_read_v128(p, &high, &low);
  14585. emit_uint64(loader_ctx, high);
  14586. emit_uint64(loader_ctx, low);
  14587. #endif
  14588. p += 16;
  14589. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14590. break;
  14591. }
  14592. case SIMD_v8x16_swizzle:
  14593. {
  14594. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14595. break;
  14596. }
  14597. /* splat operation */
  14598. case SIMD_i8x16_splat:
  14599. case SIMD_i16x8_splat:
  14600. case SIMD_i32x4_splat:
  14601. case SIMD_i64x2_splat:
  14602. case SIMD_f32x4_splat:
  14603. case SIMD_f64x2_splat:
  14604. {
  14605. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14606. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14607. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14608. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14609. VALUE_TYPE_V128);
  14610. break;
  14611. }
  14612. /* lane operation */
  14613. case SIMD_i8x16_extract_lane_s:
  14614. case SIMD_i8x16_extract_lane_u:
  14615. case SIMD_i8x16_replace_lane:
  14616. case SIMD_i16x8_extract_lane_s:
  14617. case SIMD_i16x8_extract_lane_u:
  14618. case SIMD_i16x8_replace_lane:
  14619. case SIMD_i32x4_extract_lane:
  14620. case SIMD_i32x4_replace_lane:
  14621. case SIMD_i64x2_extract_lane:
  14622. case SIMD_i64x2_replace_lane:
  14623. case SIMD_f32x4_extract_lane:
  14624. case SIMD_f32x4_replace_lane:
  14625. case SIMD_f64x2_extract_lane:
  14626. case SIMD_f64x2_replace_lane:
  14627. {
  14628. uint8 lane;
  14629. /* clang-format off */
  14630. uint8 replace[] = {
  14631. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14632. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14633. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14634. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14635. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14636. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14637. };
  14638. uint8 push_type[] = {
  14639. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14640. VALUE_TYPE_V128,
  14641. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14642. VALUE_TYPE_V128,
  14643. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14644. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14645. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14646. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14647. };
  14648. /* clang-format on */
  14649. CHECK_BUF(p, p_end, 1);
  14650. lane = read_uint8(p);
  14651. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14652. error_buf_size)) {
  14653. goto fail;
  14654. }
  14655. #if WASM_ENABLE_FAST_INTERP != 0
  14656. emit_byte(loader_ctx, lane);
  14657. #endif
  14658. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14659. #if WASM_ENABLE_FAST_INTERP != 0
  14660. if (!(wasm_loader_pop_frame_ref_offset(
  14661. loader_ctx,
  14662. replace[opcode1
  14663. - SIMD_i8x16_extract_lane_s],
  14664. error_buf, error_buf_size)))
  14665. goto fail;
  14666. #else
  14667. if (!(wasm_loader_pop_frame_ref(
  14668. loader_ctx,
  14669. replace[opcode1
  14670. - SIMD_i8x16_extract_lane_s],
  14671. error_buf, error_buf_size)))
  14672. goto fail;
  14673. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14674. }
  14675. POP_AND_PUSH(
  14676. VALUE_TYPE_V128,
  14677. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14678. break;
  14679. }
  14680. /* i8x16 compare operation */
  14681. case SIMD_i8x16_eq:
  14682. case SIMD_i8x16_ne:
  14683. case SIMD_i8x16_lt_s:
  14684. case SIMD_i8x16_lt_u:
  14685. case SIMD_i8x16_gt_s:
  14686. case SIMD_i8x16_gt_u:
  14687. case SIMD_i8x16_le_s:
  14688. case SIMD_i8x16_le_u:
  14689. case SIMD_i8x16_ge_s:
  14690. case SIMD_i8x16_ge_u:
  14691. /* i16x8 compare operation */
  14692. case SIMD_i16x8_eq:
  14693. case SIMD_i16x8_ne:
  14694. case SIMD_i16x8_lt_s:
  14695. case SIMD_i16x8_lt_u:
  14696. case SIMD_i16x8_gt_s:
  14697. case SIMD_i16x8_gt_u:
  14698. case SIMD_i16x8_le_s:
  14699. case SIMD_i16x8_le_u:
  14700. case SIMD_i16x8_ge_s:
  14701. case SIMD_i16x8_ge_u:
  14702. /* i32x4 compare operation */
  14703. case SIMD_i32x4_eq:
  14704. case SIMD_i32x4_ne:
  14705. case SIMD_i32x4_lt_s:
  14706. case SIMD_i32x4_lt_u:
  14707. case SIMD_i32x4_gt_s:
  14708. case SIMD_i32x4_gt_u:
  14709. case SIMD_i32x4_le_s:
  14710. case SIMD_i32x4_le_u:
  14711. case SIMD_i32x4_ge_s:
  14712. case SIMD_i32x4_ge_u:
  14713. /* f32x4 compare operation */
  14714. case SIMD_f32x4_eq:
  14715. case SIMD_f32x4_ne:
  14716. case SIMD_f32x4_lt:
  14717. case SIMD_f32x4_gt:
  14718. case SIMD_f32x4_le:
  14719. case SIMD_f32x4_ge:
  14720. /* f64x2 compare operation */
  14721. case SIMD_f64x2_eq:
  14722. case SIMD_f64x2_ne:
  14723. case SIMD_f64x2_lt:
  14724. case SIMD_f64x2_gt:
  14725. case SIMD_f64x2_le:
  14726. case SIMD_f64x2_ge:
  14727. {
  14728. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14729. break;
  14730. }
  14731. /* v128 operation */
  14732. case SIMD_v128_not:
  14733. {
  14734. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14735. break;
  14736. }
  14737. case SIMD_v128_and:
  14738. case SIMD_v128_andnot:
  14739. case SIMD_v128_or:
  14740. case SIMD_v128_xor:
  14741. {
  14742. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14743. break;
  14744. }
  14745. case SIMD_v128_bitselect:
  14746. {
  14747. POP_V128();
  14748. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14749. break;
  14750. }
  14751. case SIMD_v128_any_true:
  14752. {
  14753. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14754. break;
  14755. }
  14756. /* Load Lane Operation */
  14757. case SIMD_v128_load8_lane:
  14758. case SIMD_v128_load16_lane:
  14759. case SIMD_v128_load32_lane:
  14760. case SIMD_v128_load64_lane:
  14761. case SIMD_v128_store8_lane:
  14762. case SIMD_v128_store16_lane:
  14763. case SIMD_v128_store32_lane:
  14764. case SIMD_v128_store64_lane:
  14765. {
  14766. uint8 lane;
  14767. CHECK_MEMORY();
  14768. pb_read_leb_uint32(p, p_end, align); /* align */
  14769. if (!check_simd_memory_access_align(
  14770. opcode1, align, error_buf, error_buf_size)) {
  14771. goto fail;
  14772. }
  14773. pb_read_leb_mem_offset(p, p_end,
  14774. mem_offset); /* offset */
  14775. CHECK_BUF(p, p_end, 1);
  14776. lane = read_uint8(p);
  14777. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14778. error_buf_size)) {
  14779. goto fail;
  14780. }
  14781. #if WASM_ENABLE_FAST_INTERP != 0
  14782. emit_uint32(loader_ctx, mem_offset);
  14783. #endif
  14784. POP_V128();
  14785. POP_MEM_OFFSET();
  14786. #if WASM_ENABLE_FAST_INTERP != 0
  14787. emit_byte(loader_ctx, lane);
  14788. #endif
  14789. if (opcode1 < SIMD_v128_store8_lane) {
  14790. PUSH_V128();
  14791. }
  14792. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14793. func->has_memory_operations = true;
  14794. #endif
  14795. break;
  14796. }
  14797. case SIMD_v128_load32_zero:
  14798. case SIMD_v128_load64_zero:
  14799. {
  14800. CHECK_MEMORY();
  14801. pb_read_leb_uint32(p, p_end, align); /* align */
  14802. if (!check_simd_memory_access_align(
  14803. opcode1, align, error_buf, error_buf_size)) {
  14804. goto fail;
  14805. }
  14806. pb_read_leb_mem_offset(p, p_end,
  14807. mem_offset); /* offset */
  14808. #if WASM_ENABLE_FAST_INTERP != 0
  14809. emit_uint32(loader_ctx, mem_offset);
  14810. #endif
  14811. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14812. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14813. func->has_memory_operations = true;
  14814. #endif
  14815. break;
  14816. }
  14817. /* Float conversion */
  14818. case SIMD_f32x4_demote_f64x2_zero:
  14819. case SIMD_f64x2_promote_low_f32x4_zero:
  14820. {
  14821. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14822. break;
  14823. }
  14824. /* i8x16 Operation */
  14825. case SIMD_i8x16_abs:
  14826. case SIMD_i8x16_neg:
  14827. case SIMD_i8x16_popcnt:
  14828. {
  14829. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14830. break;
  14831. }
  14832. case SIMD_i8x16_all_true:
  14833. case SIMD_i8x16_bitmask:
  14834. {
  14835. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14836. break;
  14837. }
  14838. case SIMD_i8x16_narrow_i16x8_s:
  14839. case SIMD_i8x16_narrow_i16x8_u:
  14840. {
  14841. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14842. break;
  14843. }
  14844. case SIMD_f32x4_ceil:
  14845. case SIMD_f32x4_floor:
  14846. case SIMD_f32x4_trunc:
  14847. case SIMD_f32x4_nearest:
  14848. {
  14849. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14850. break;
  14851. }
  14852. case SIMD_i8x16_shl:
  14853. case SIMD_i8x16_shr_s:
  14854. case SIMD_i8x16_shr_u:
  14855. {
  14856. POP_I32();
  14857. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14858. break;
  14859. }
  14860. case SIMD_i8x16_add:
  14861. case SIMD_i8x16_add_sat_s:
  14862. case SIMD_i8x16_add_sat_u:
  14863. case SIMD_i8x16_sub:
  14864. case SIMD_i8x16_sub_sat_s:
  14865. case SIMD_i8x16_sub_sat_u:
  14866. {
  14867. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14868. break;
  14869. }
  14870. case SIMD_f64x2_ceil:
  14871. case SIMD_f64x2_floor:
  14872. {
  14873. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14874. break;
  14875. }
  14876. case SIMD_i8x16_min_s:
  14877. case SIMD_i8x16_min_u:
  14878. case SIMD_i8x16_max_s:
  14879. case SIMD_i8x16_max_u:
  14880. {
  14881. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14882. break;
  14883. }
  14884. case SIMD_f64x2_trunc:
  14885. {
  14886. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14887. break;
  14888. }
  14889. case SIMD_i8x16_avgr_u:
  14890. {
  14891. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14892. break;
  14893. }
  14894. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14895. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14896. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14897. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14898. /* i16x8 operation */
  14899. case SIMD_i16x8_abs:
  14900. case SIMD_i16x8_neg:
  14901. {
  14902. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14903. break;
  14904. }
  14905. case SIMD_i16x8_q15mulr_sat_s:
  14906. {
  14907. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14908. break;
  14909. }
  14910. case SIMD_i16x8_all_true:
  14911. case SIMD_i16x8_bitmask:
  14912. {
  14913. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14914. break;
  14915. }
  14916. case SIMD_i16x8_narrow_i32x4_s:
  14917. case SIMD_i16x8_narrow_i32x4_u:
  14918. {
  14919. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14920. break;
  14921. }
  14922. case SIMD_i16x8_extend_low_i8x16_s:
  14923. case SIMD_i16x8_extend_high_i8x16_s:
  14924. case SIMD_i16x8_extend_low_i8x16_u:
  14925. case SIMD_i16x8_extend_high_i8x16_u:
  14926. {
  14927. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14928. break;
  14929. }
  14930. case SIMD_i16x8_shl:
  14931. case SIMD_i16x8_shr_s:
  14932. case SIMD_i16x8_shr_u:
  14933. {
  14934. POP_I32();
  14935. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14936. break;
  14937. }
  14938. case SIMD_i16x8_add:
  14939. case SIMD_i16x8_add_sat_s:
  14940. case SIMD_i16x8_add_sat_u:
  14941. case SIMD_i16x8_sub:
  14942. case SIMD_i16x8_sub_sat_s:
  14943. case SIMD_i16x8_sub_sat_u:
  14944. {
  14945. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14946. break;
  14947. }
  14948. case SIMD_f64x2_nearest:
  14949. {
  14950. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14951. break;
  14952. }
  14953. case SIMD_i16x8_mul:
  14954. case SIMD_i16x8_min_s:
  14955. case SIMD_i16x8_min_u:
  14956. case SIMD_i16x8_max_s:
  14957. case SIMD_i16x8_max_u:
  14958. case SIMD_i16x8_avgr_u:
  14959. case SIMD_i16x8_extmul_low_i8x16_s:
  14960. case SIMD_i16x8_extmul_high_i8x16_s:
  14961. case SIMD_i16x8_extmul_low_i8x16_u:
  14962. case SIMD_i16x8_extmul_high_i8x16_u:
  14963. {
  14964. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14965. break;
  14966. }
  14967. /* i32x4 operation */
  14968. case SIMD_i32x4_abs:
  14969. case SIMD_i32x4_neg:
  14970. {
  14971. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14972. break;
  14973. }
  14974. case SIMD_i32x4_all_true:
  14975. case SIMD_i32x4_bitmask:
  14976. {
  14977. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14978. break;
  14979. }
  14980. case SIMD_i32x4_extend_low_i16x8_s:
  14981. case SIMD_i32x4_extend_high_i16x8_s:
  14982. case SIMD_i32x4_extend_low_i16x8_u:
  14983. case SIMD_i32x4_extend_high_i16x8_u:
  14984. {
  14985. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14986. break;
  14987. }
  14988. case SIMD_i32x4_shl:
  14989. case SIMD_i32x4_shr_s:
  14990. case SIMD_i32x4_shr_u:
  14991. {
  14992. POP_I32();
  14993. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14994. break;
  14995. }
  14996. case SIMD_i32x4_add:
  14997. case SIMD_i32x4_sub:
  14998. case SIMD_i32x4_mul:
  14999. case SIMD_i32x4_min_s:
  15000. case SIMD_i32x4_min_u:
  15001. case SIMD_i32x4_max_s:
  15002. case SIMD_i32x4_max_u:
  15003. case SIMD_i32x4_dot_i16x8_s:
  15004. case SIMD_i32x4_extmul_low_i16x8_s:
  15005. case SIMD_i32x4_extmul_high_i16x8_s:
  15006. case SIMD_i32x4_extmul_low_i16x8_u:
  15007. case SIMD_i32x4_extmul_high_i16x8_u:
  15008. {
  15009. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15010. break;
  15011. }
  15012. /* i64x2 operation */
  15013. case SIMD_i64x2_abs:
  15014. case SIMD_i64x2_neg:
  15015. {
  15016. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15017. break;
  15018. }
  15019. case SIMD_i64x2_all_true:
  15020. case SIMD_i64x2_bitmask:
  15021. {
  15022. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  15023. break;
  15024. }
  15025. case SIMD_i64x2_extend_low_i32x4_s:
  15026. case SIMD_i64x2_extend_high_i32x4_s:
  15027. case SIMD_i64x2_extend_low_i32x4_u:
  15028. case SIMD_i64x2_extend_high_i32x4_u:
  15029. {
  15030. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15031. break;
  15032. }
  15033. case SIMD_i64x2_shl:
  15034. case SIMD_i64x2_shr_s:
  15035. case SIMD_i64x2_shr_u:
  15036. {
  15037. POP_I32();
  15038. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15039. break;
  15040. }
  15041. case SIMD_i64x2_add:
  15042. case SIMD_i64x2_sub:
  15043. case SIMD_i64x2_mul:
  15044. case SIMD_i64x2_eq:
  15045. case SIMD_i64x2_ne:
  15046. case SIMD_i64x2_lt_s:
  15047. case SIMD_i64x2_gt_s:
  15048. case SIMD_i64x2_le_s:
  15049. case SIMD_i64x2_ge_s:
  15050. case SIMD_i64x2_extmul_low_i32x4_s:
  15051. case SIMD_i64x2_extmul_high_i32x4_s:
  15052. case SIMD_i64x2_extmul_low_i32x4_u:
  15053. case SIMD_i64x2_extmul_high_i32x4_u:
  15054. {
  15055. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15056. break;
  15057. }
  15058. /* f32x4 operation */
  15059. case SIMD_f32x4_abs:
  15060. case SIMD_f32x4_neg:
  15061. case SIMD_f32x4_sqrt:
  15062. {
  15063. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15064. break;
  15065. }
  15066. case SIMD_f32x4_add:
  15067. case SIMD_f32x4_sub:
  15068. case SIMD_f32x4_mul:
  15069. case SIMD_f32x4_div:
  15070. case SIMD_f32x4_min:
  15071. case SIMD_f32x4_max:
  15072. case SIMD_f32x4_pmin:
  15073. case SIMD_f32x4_pmax:
  15074. {
  15075. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15076. break;
  15077. }
  15078. /* f64x2 operation */
  15079. case SIMD_f64x2_abs:
  15080. case SIMD_f64x2_neg:
  15081. case SIMD_f64x2_sqrt:
  15082. {
  15083. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15084. break;
  15085. }
  15086. case SIMD_f64x2_add:
  15087. case SIMD_f64x2_sub:
  15088. case SIMD_f64x2_mul:
  15089. case SIMD_f64x2_div:
  15090. case SIMD_f64x2_min:
  15091. case SIMD_f64x2_max:
  15092. case SIMD_f64x2_pmin:
  15093. case SIMD_f64x2_pmax:
  15094. {
  15095. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15096. break;
  15097. }
  15098. case SIMD_i32x4_trunc_sat_f32x4_s:
  15099. case SIMD_i32x4_trunc_sat_f32x4_u:
  15100. case SIMD_f32x4_convert_i32x4_s:
  15101. case SIMD_f32x4_convert_i32x4_u:
  15102. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  15103. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  15104. case SIMD_f64x2_convert_low_i32x4_s:
  15105. case SIMD_f64x2_convert_low_i32x4_u:
  15106. {
  15107. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15108. break;
  15109. }
  15110. default:
  15111. {
  15112. if (error_buf != NULL) {
  15113. snprintf(error_buf, error_buf_size,
  15114. "WASM module load failed: "
  15115. "invalid opcode 0xfd %02x.",
  15116. opcode1);
  15117. }
  15118. goto fail;
  15119. }
  15120. }
  15121. break;
  15122. }
  15123. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  15124. (WASM_ENABLE_FAST_INTERP != 0) */
  15125. #endif /* end of WASM_ENABLE_SIMD */
  15126. #if WASM_ENABLE_SHARED_MEMORY != 0
  15127. case WASM_OP_ATOMIC_PREFIX:
  15128. {
  15129. uint32 opcode1;
  15130. pb_read_leb_uint32(p, p_end, opcode1);
  15131. #if WASM_ENABLE_FAST_INTERP != 0
  15132. emit_byte(loader_ctx, opcode1);
  15133. #endif
  15134. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  15135. CHECK_MEMORY();
  15136. pb_read_leb_uint32(p, p_end, align); /* align */
  15137. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  15138. if (!check_memory_align_equal(opcode1, align, error_buf,
  15139. error_buf_size)) {
  15140. goto fail;
  15141. }
  15142. #if WASM_ENABLE_FAST_INTERP != 0
  15143. emit_uint32(loader_ctx, mem_offset);
  15144. #endif
  15145. }
  15146. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  15147. func->has_memory_operations = true;
  15148. #endif
  15149. switch (opcode1) {
  15150. case WASM_OP_ATOMIC_NOTIFY:
  15151. POP_I32();
  15152. POP_MEM_OFFSET();
  15153. PUSH_I32();
  15154. break;
  15155. case WASM_OP_ATOMIC_WAIT32:
  15156. POP_I64();
  15157. POP_I32();
  15158. POP_MEM_OFFSET();
  15159. PUSH_I32();
  15160. break;
  15161. case WASM_OP_ATOMIC_WAIT64:
  15162. POP_I64();
  15163. POP_I64();
  15164. POP_MEM_OFFSET();
  15165. PUSH_I32();
  15166. break;
  15167. case WASM_OP_ATOMIC_FENCE:
  15168. /* reserved byte 0x00 */
  15169. if (*p++ != 0x00) {
  15170. set_error_buf(error_buf, error_buf_size,
  15171. "zero byte expected");
  15172. goto fail;
  15173. }
  15174. break;
  15175. case WASM_OP_ATOMIC_I32_LOAD:
  15176. case WASM_OP_ATOMIC_I32_LOAD8_U:
  15177. case WASM_OP_ATOMIC_I32_LOAD16_U:
  15178. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  15179. break;
  15180. case WASM_OP_ATOMIC_I32_STORE:
  15181. case WASM_OP_ATOMIC_I32_STORE8:
  15182. case WASM_OP_ATOMIC_I32_STORE16:
  15183. POP_I32();
  15184. POP_MEM_OFFSET();
  15185. break;
  15186. case WASM_OP_ATOMIC_I64_LOAD:
  15187. case WASM_OP_ATOMIC_I64_LOAD8_U:
  15188. case WASM_OP_ATOMIC_I64_LOAD16_U:
  15189. case WASM_OP_ATOMIC_I64_LOAD32_U:
  15190. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  15191. break;
  15192. case WASM_OP_ATOMIC_I64_STORE:
  15193. case WASM_OP_ATOMIC_I64_STORE8:
  15194. case WASM_OP_ATOMIC_I64_STORE16:
  15195. case WASM_OP_ATOMIC_I64_STORE32:
  15196. POP_I64();
  15197. POP_MEM_OFFSET();
  15198. break;
  15199. case WASM_OP_ATOMIC_RMW_I32_ADD:
  15200. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  15201. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  15202. case WASM_OP_ATOMIC_RMW_I32_SUB:
  15203. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  15204. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  15205. case WASM_OP_ATOMIC_RMW_I32_AND:
  15206. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  15207. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  15208. case WASM_OP_ATOMIC_RMW_I32_OR:
  15209. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  15210. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  15211. case WASM_OP_ATOMIC_RMW_I32_XOR:
  15212. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  15213. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  15214. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  15215. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  15216. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  15217. POP_I32();
  15218. POP_MEM_OFFSET();
  15219. PUSH_I32();
  15220. break;
  15221. case WASM_OP_ATOMIC_RMW_I64_ADD:
  15222. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  15223. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  15224. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  15225. case WASM_OP_ATOMIC_RMW_I64_SUB:
  15226. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  15227. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  15228. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  15229. case WASM_OP_ATOMIC_RMW_I64_AND:
  15230. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  15231. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  15232. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  15233. case WASM_OP_ATOMIC_RMW_I64_OR:
  15234. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  15235. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  15236. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  15237. case WASM_OP_ATOMIC_RMW_I64_XOR:
  15238. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  15239. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  15240. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  15241. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  15242. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  15243. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  15244. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  15245. POP_I64();
  15246. POP_MEM_OFFSET();
  15247. PUSH_I64();
  15248. break;
  15249. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  15250. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  15251. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  15252. POP_I32();
  15253. POP_I32();
  15254. POP_MEM_OFFSET();
  15255. PUSH_I32();
  15256. break;
  15257. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  15258. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  15259. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  15260. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  15261. POP_I64();
  15262. POP_I64();
  15263. POP_MEM_OFFSET();
  15264. PUSH_I64();
  15265. break;
  15266. default:
  15267. set_error_buf_v(error_buf, error_buf_size,
  15268. "%s %02x %02x", "unsupported opcode",
  15269. 0xfe, opcode1);
  15270. goto fail;
  15271. }
  15272. break;
  15273. }
  15274. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  15275. default:
  15276. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  15277. "unsupported opcode", opcode);
  15278. goto fail;
  15279. }
  15280. #if WASM_ENABLE_FAST_INTERP != 0
  15281. last_op = opcode;
  15282. #endif
  15283. }
  15284. if (loader_ctx->csp_num > 0) {
  15285. /* unmatched end opcodes result from unbalanced control flow structures,
  15286. * for example, br_table with inconsistent target count (1 declared, 2
  15287. * given), or simply superfluous end opcodes */
  15288. set_error_buf(
  15289. error_buf, error_buf_size,
  15290. "unexpected end opcodes from unbalanced control flow structures");
  15291. goto fail;
  15292. }
  15293. #if WASM_ENABLE_FAST_INTERP != 0
  15294. if (loader_ctx->p_code_compiled == NULL)
  15295. goto re_scan;
  15296. func->const_cell_num = loader_ctx->i64_const_num * 2
  15297. + loader_ctx->v128_const_num * 4
  15298. + loader_ctx->i32_const_num;
  15299. if (func->const_cell_num > 0) {
  15300. if (!(func->consts =
  15301. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  15302. error_buf, error_buf_size)))
  15303. goto fail;
  15304. if (loader_ctx->i64_const_num > 0) {
  15305. bh_memcpy_s(func->consts,
  15306. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  15307. loader_ctx->i64_consts,
  15308. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  15309. }
  15310. if (loader_ctx->i32_const_num > 0) {
  15311. bh_memcpy_s(func->consts
  15312. + sizeof(int64) * loader_ctx->i64_const_num,
  15313. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  15314. loader_ctx->i32_consts,
  15315. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  15316. }
  15317. if (loader_ctx->v128_const_num > 0) {
  15318. bh_memcpy_s(func->consts,
  15319. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  15320. loader_ctx->v128_consts,
  15321. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  15322. }
  15323. }
  15324. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  15325. - loader_ctx->start_dynamic_offset + 1;
  15326. #else
  15327. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  15328. #endif
  15329. func->max_block_num = loader_ctx->max_csp_num;
  15330. return_value = true;
  15331. fail:
  15332. wasm_loader_ctx_destroy(loader_ctx);
  15333. (void)table_idx;
  15334. (void)table_seg_idx;
  15335. (void)data_seg_idx;
  15336. (void)i64_const;
  15337. (void)local_offset;
  15338. (void)p_org;
  15339. (void)mem_offset;
  15340. (void)align;
  15341. return return_value;
  15342. }