wasm_loader.c 583 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.mem_type.flags
  44. & MEMORY64_FLAG);
  45. else if (module->memory_count > 0)
  46. return !!(module->memories[0].flags & MEMORY64_FLAG);
  47. return false;
  48. }
  49. #endif
  50. static void
  51. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  52. {
  53. if (error_buf != NULL) {
  54. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  55. string);
  56. }
  57. }
  58. #if WASM_ENABLE_MEMORY64 != 0
  59. static void
  60. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  61. {
  62. if (error_buf != NULL) {
  63. snprintf(error_buf, error_buf_size, "offset out of range");
  64. }
  65. }
  66. #endif
  67. static void
  68. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  69. {
  70. va_list args;
  71. char buf[128];
  72. if (error_buf != NULL) {
  73. va_start(args, format);
  74. vsnprintf(buf, sizeof(buf), format, args);
  75. va_end(args);
  76. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  77. }
  78. }
  79. static bool
  80. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  81. char *error_buf, uint32 error_buf_size)
  82. {
  83. if ((uintptr_t)buf + length < (uintptr_t)buf
  84. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  85. set_error_buf(error_buf, error_buf_size,
  86. "unexpected end of section or function");
  87. return false;
  88. }
  89. return true;
  90. }
  91. static bool
  92. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  93. char *error_buf, uint32 error_buf_size)
  94. {
  95. if ((uintptr_t)buf + length < (uintptr_t)buf
  96. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  97. set_error_buf(error_buf, error_buf_size, "unexpected end");
  98. return false;
  99. }
  100. return true;
  101. }
  102. #define CHECK_BUF(buf, buf_end, length) \
  103. do { \
  104. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  105. goto fail; \
  106. } \
  107. } while (0)
  108. #define CHECK_BUF1(buf, buf_end, length) \
  109. do { \
  110. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  111. goto fail; \
  112. } \
  113. } while (0)
  114. #define skip_leb(p) while (*p++ & 0x80)
  115. #define skip_leb_int64(p, p_end) skip_leb(p)
  116. #define skip_leb_uint32(p, p_end) skip_leb(p)
  117. #define skip_leb_int32(p, p_end) skip_leb(p)
  118. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  119. static bool
  120. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  121. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  122. {
  123. const uint8 *buf = *p_buf;
  124. uint64 result = 0;
  125. uint32 shift = 0;
  126. uint32 offset = 0, bcnt = 0;
  127. uint64 byte;
  128. while (true) {
  129. /* uN or SN must not exceed ceil(N/7) bytes */
  130. if (bcnt + 1 > (maxbits + 6) / 7) {
  131. set_error_buf(error_buf, error_buf_size,
  132. "integer representation too long");
  133. return false;
  134. }
  135. CHECK_BUF(buf, buf_end, offset + 1);
  136. byte = buf[offset];
  137. offset += 1;
  138. result |= ((byte & 0x7f) << shift);
  139. shift += 7;
  140. bcnt += 1;
  141. if ((byte & 0x80) == 0) {
  142. break;
  143. }
  144. }
  145. if (!sign && maxbits == 32 && shift >= maxbits) {
  146. /* The top bits set represent values > 32 bits */
  147. if (((uint8)byte) & 0xf0)
  148. goto fail_integer_too_large;
  149. }
  150. else if (sign && maxbits == 32) {
  151. if (shift < maxbits) {
  152. /* Sign extend, second-highest bit is the sign bit */
  153. if ((uint8)byte & 0x40)
  154. result |= (~((uint64)0)) << shift;
  155. }
  156. else {
  157. /* The top bits should be a sign-extension of the sign bit */
  158. bool sign_bit_set = ((uint8)byte) & 0x8;
  159. int top_bits = ((uint8)byte) & 0xf0;
  160. if ((sign_bit_set && top_bits != 0x70)
  161. || (!sign_bit_set && top_bits != 0))
  162. goto fail_integer_too_large;
  163. }
  164. }
  165. else if (sign && maxbits == 64) {
  166. if (shift < maxbits) {
  167. /* Sign extend, second-highest bit is the sign bit */
  168. if ((uint8)byte & 0x40)
  169. result |= (~((uint64)0)) << shift;
  170. }
  171. else {
  172. /* The top bits should be a sign-extension of the sign bit */
  173. bool sign_bit_set = ((uint8)byte) & 0x1;
  174. int top_bits = ((uint8)byte) & 0xfe;
  175. if ((sign_bit_set && top_bits != 0x7e)
  176. || (!sign_bit_set && top_bits != 0))
  177. goto fail_integer_too_large;
  178. }
  179. }
  180. *p_buf += offset;
  181. *p_result = result;
  182. return true;
  183. fail_integer_too_large:
  184. set_error_buf(error_buf, error_buf_size, "integer too large");
  185. fail:
  186. return false;
  187. }
  188. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  189. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  190. #define read_leb_int64(p, p_end, res) \
  191. do { \
  192. uint64 res64; \
  193. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  194. error_buf_size)) \
  195. goto fail; \
  196. res = (int64)res64; \
  197. } while (0)
  198. #if WASM_ENABLE_MEMORY64 != 0
  199. #define read_leb_mem_offset(p, p_end, res) \
  200. do { \
  201. uint64 res64; \
  202. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  203. &res64, error_buf, error_buf_size)) { \
  204. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  205. goto fail; \
  206. } \
  207. res = (mem_offset_t)res64; \
  208. } while (0)
  209. #else
  210. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  211. #endif
  212. #define read_leb_uint32(p, p_end, res) \
  213. do { \
  214. uint64 res64; \
  215. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  216. error_buf_size)) \
  217. goto fail; \
  218. res = (uint32)res64; \
  219. } while (0)
  220. #define read_leb_int32(p, p_end, res) \
  221. do { \
  222. uint64 res64; \
  223. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  224. error_buf_size)) \
  225. goto fail; \
  226. res = (int32)res64; \
  227. } while (0)
  228. static char *
  229. type2str(uint8 type)
  230. {
  231. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  232. #if WASM_ENABLE_GC != 0
  233. char *type_str_ref[] = { "stringview_iter",
  234. "stringview_wtf16",
  235. "(ref null ht)",
  236. "(ref ht)",
  237. "", /* reserved */
  238. "stringview_wtf8",
  239. "stringref",
  240. "", /* reserved */
  241. "", /* reserved */
  242. "arrayref",
  243. "structref",
  244. "i32ref",
  245. "eqref",
  246. "anyref",
  247. "externref",
  248. "funcref",
  249. "nullref",
  250. "nullexternref",
  251. "nullfuncref" };
  252. #endif
  253. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  254. return type_str[type - VALUE_TYPE_V128];
  255. #if WASM_ENABLE_GC != 0
  256. else if (wasm_is_type_reftype(type))
  257. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  258. #endif
  259. else if (type == VALUE_TYPE_FUNCREF)
  260. return "funcref";
  261. else if (type == VALUE_TYPE_EXTERNREF)
  262. return "externref";
  263. else
  264. return "unknown type";
  265. }
  266. static bool
  267. is_32bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I32
  270. || type == VALUE_TYPE_F32
  271. /* the operand stack is in polymorphic state */
  272. || type == VALUE_TYPE_ANY
  273. #if WASM_ENABLE_GC != 0
  274. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  275. #elif WASM_ENABLE_REF_TYPES != 0
  276. /* For reference types, we use uint32 index to represent
  277. the funcref and externref */
  278. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  279. #endif
  280. )
  281. return true;
  282. return false;
  283. }
  284. static bool
  285. is_64bit_type(uint8 type)
  286. {
  287. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  288. #if WASM_ENABLE_GC != 0
  289. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  290. #endif
  291. )
  292. return true;
  293. return false;
  294. }
  295. #if WASM_ENABLE_GC != 0
  296. static bool
  297. is_packed_type(uint8 type)
  298. {
  299. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  300. }
  301. #endif
  302. static bool
  303. is_byte_a_type(uint8 type)
  304. {
  305. return (is_valid_value_type(type) || (type == VALUE_TYPE_VOID)) ? true
  306. : false;
  307. }
  308. #if WASM_ENABLE_SIMD != 0
  309. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  310. static V128
  311. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  312. {
  313. V128 result;
  314. uint8 i;
  315. for (i = 0; i != 16; ++i) {
  316. result.i8x16[i] = read_uint8(p_buf);
  317. }
  318. return result;
  319. }
  320. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  321. #endif /* end of WASM_ENABLE_SIMD */
  322. static void *
  323. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  324. {
  325. void *mem;
  326. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  327. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  328. return NULL;
  329. }
  330. memset(mem, 0, (uint32)size);
  331. return mem;
  332. }
  333. static void *
  334. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  335. uint32 error_buf_size)
  336. {
  337. uint8 *mem_new;
  338. bh_assert(size_new > size_old);
  339. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  340. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  341. memset(mem_new + size_old, 0, size_new - size_old);
  342. wasm_runtime_free(mem_old);
  343. }
  344. return mem_new;
  345. }
  346. #define MEM_REALLOC(mem, size_old, size_new) \
  347. do { \
  348. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  349. error_buf_size); \
  350. if (!mem_new) \
  351. goto fail; \
  352. mem = mem_new; \
  353. } while (0)
  354. #if WASM_ENABLE_GC != 0
  355. static bool
  356. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  357. char *error_buf, uint32 error_buf_size)
  358. {
  359. if (type_index >= type_count) {
  360. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  361. type_index);
  362. return false;
  363. }
  364. return true;
  365. }
  366. static bool
  367. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  368. uint32 error_buf_size)
  369. {
  370. if (!check_type_index(module, module->type_count, type_index, error_buf,
  371. error_buf_size)) {
  372. return false;
  373. }
  374. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  375. set_error_buf(error_buf, error_buf_size, "unkown array type");
  376. return false;
  377. }
  378. return true;
  379. }
  380. #endif
  381. static bool
  382. check_function_index(const WASMModule *module, uint32 function_index,
  383. char *error_buf, uint32 error_buf_size)
  384. {
  385. if (function_index
  386. >= module->import_function_count + module->function_count) {
  387. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  388. function_index);
  389. return false;
  390. }
  391. return true;
  392. }
  393. typedef struct InitValue {
  394. uint8 type;
  395. uint8 flag;
  396. #if WASM_ENABLE_GC != 0
  397. uint8 gc_opcode;
  398. WASMRefType ref_type;
  399. #endif
  400. WASMValue value;
  401. } InitValue;
  402. typedef struct ConstExprContext {
  403. uint32 sp;
  404. uint32 size;
  405. WASMModule *module;
  406. InitValue *stack;
  407. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  408. } ConstExprContext;
  409. static void
  410. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  411. {
  412. ctx->sp = 0;
  413. ctx->module = module;
  414. ctx->stack = ctx->data;
  415. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  416. }
  417. static bool
  418. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  419. #if WASM_ENABLE_GC != 0
  420. WASMRefType *ref_type, uint8 gc_opcode,
  421. #endif
  422. WASMValue *value, char *error_buf, uint32 error_buf_size)
  423. {
  424. InitValue *cur_value;
  425. if (ctx->sp >= ctx->size) {
  426. if (ctx->stack != ctx->data) {
  427. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  428. (ctx->size + 4) * sizeof(InitValue));
  429. }
  430. else {
  431. if (!(ctx->stack =
  432. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  433. error_buf, error_buf_size))) {
  434. goto fail;
  435. }
  436. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  437. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  438. }
  439. ctx->size += 4;
  440. }
  441. cur_value = &ctx->stack[ctx->sp++];
  442. cur_value->type = type;
  443. cur_value->flag = flag;
  444. cur_value->value = *value;
  445. #if WASM_ENABLE_GC != 0
  446. cur_value->gc_opcode = gc_opcode;
  447. if (wasm_is_type_multi_byte_type(type)) {
  448. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  449. ref_type, wasm_reftype_struct_size(ref_type));
  450. }
  451. #endif
  452. return true;
  453. fail:
  454. return false;
  455. }
  456. #if WASM_ENABLE_GC != 0
  457. static void
  458. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  459. {
  460. WASMStructNewInitValues *struct_init_values =
  461. (WASMStructNewInitValues *)data;
  462. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  463. WASMType *wasm_type;
  464. uint32 i;
  465. if (!data)
  466. return;
  467. wasm_type = module->types[struct_init_values->type_idx];
  468. /* The data can only be type of `WASMStructNewInitValues *`
  469. or `WASMArrayNewInitValues *` */
  470. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  471. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  472. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  473. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  474. WASMRefTypeMap *ref_type_map = struct_type->ref_type_maps;
  475. WASMRefType *ref_type;
  476. uint8 field_type;
  477. for (i = 0; i < struct_init_values->count; i++) {
  478. field_type = struct_type->fields[i].field_type;
  479. if (wasm_is_type_multi_byte_type(field_type))
  480. ref_type = ref_type_map->ref_type;
  481. else
  482. ref_type = NULL;
  483. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  484. REF_TYPE_STRUCTREF, NULL,
  485. module->types, module->type_count)
  486. || wasm_reftype_is_subtype_of(
  487. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  488. module->types, module->type_count)) {
  489. destroy_init_expr_data_recursive(
  490. module, struct_init_values->fields[i].data);
  491. }
  492. }
  493. }
  494. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  495. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  496. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  497. uint8 elem_type = array_type->elem_type;
  498. for (i = 0; i < array_init_values->length; i++) {
  499. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  500. REF_TYPE_STRUCTREF, NULL,
  501. module->types, module->type_count)
  502. || wasm_reftype_is_subtype_of(
  503. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  504. module->types, module->type_count)) {
  505. destroy_init_expr_data_recursive(
  506. module, array_init_values->elem_data[i].data);
  507. }
  508. }
  509. }
  510. wasm_runtime_free(data);
  511. }
  512. #endif
  513. static bool
  514. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  515. #if WASM_ENABLE_GC != 0
  516. WASMRefType *ref_type, uint8 *p_gc_opcode,
  517. #endif
  518. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  519. {
  520. InitValue *cur_value;
  521. if (ctx->sp == 0) {
  522. set_error_buf(error_buf, error_buf_size,
  523. "type mismatch: const expr stack underflow");
  524. return false;
  525. }
  526. cur_value = &ctx->stack[--ctx->sp];
  527. #if WASM_ENABLE_GC == 0
  528. if (cur_value->type != type) {
  529. set_error_buf(error_buf, error_buf_size, "type mismatch");
  530. return false;
  531. }
  532. #else
  533. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  534. ref_type, ctx->module->types,
  535. ctx->module->type_count)) {
  536. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  537. "type mismatch: expect ", type2str(type),
  538. " but got other");
  539. goto fail;
  540. }
  541. #endif
  542. if (p_flag)
  543. *p_flag = cur_value->flag;
  544. if (p_value)
  545. *p_value = cur_value->value;
  546. #if WASM_ENABLE_GC != 0
  547. if (p_gc_opcode)
  548. *p_gc_opcode = cur_value->gc_opcode;
  549. #endif
  550. return true;
  551. #if WASM_ENABLE_GC != 0
  552. fail:
  553. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  554. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  555. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  556. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  557. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  558. }
  559. return false;
  560. #endif
  561. }
  562. static void
  563. destroy_const_expr_stack(ConstExprContext *ctx)
  564. {
  565. #if WASM_ENABLE_GC != 0
  566. uint32 i;
  567. for (i = 0; i < ctx->sp; i++) {
  568. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  569. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  570. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  571. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  572. destroy_init_expr_data_recursive(ctx->module,
  573. ctx->stack[i].value.data);
  574. }
  575. }
  576. #endif
  577. if (ctx->stack != ctx->data) {
  578. wasm_runtime_free(ctx->stack);
  579. }
  580. }
  581. #if WASM_ENABLE_GC != 0
  582. static void
  583. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  584. {
  585. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  586. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  587. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  588. destroy_init_expr_data_recursive(module, expr->u.data);
  589. }
  590. }
  591. #endif /* end of WASM_ENABLE_GC != 0 */
  592. static bool
  593. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  594. InitializerExpression *init_expr, uint8 type, void *ref_type,
  595. char *error_buf, uint32 error_buf_size)
  596. {
  597. const uint8 *p = *p_buf, *p_end = buf_end;
  598. uint8 flag, *p_float;
  599. uint32 i;
  600. ConstExprContext const_expr_ctx = { 0 };
  601. WASMValue cur_value;
  602. #if WASM_ENABLE_GC != 0
  603. uint32 opcode1, type_idx;
  604. uint8 opcode;
  605. WASMRefType cur_ref_type = { 0 };
  606. #endif
  607. init_const_expr_stack(&const_expr_ctx, module);
  608. CHECK_BUF(p, p_end, 1);
  609. flag = read_uint8(p);
  610. while (flag != WASM_OP_END) {
  611. switch (flag) {
  612. /* i32.const */
  613. case INIT_EXPR_TYPE_I32_CONST:
  614. read_leb_int32(p, p_end, cur_value.i32);
  615. if (!push_const_expr_stack(
  616. &const_expr_ctx, flag, VALUE_TYPE_I32,
  617. #if WASM_ENABLE_GC != 0
  618. NULL, 0,
  619. #endif
  620. &cur_value, error_buf, error_buf_size))
  621. goto fail;
  622. break;
  623. /* i64.const */
  624. case INIT_EXPR_TYPE_I64_CONST:
  625. read_leb_int64(p, p_end, cur_value.i64);
  626. if (!push_const_expr_stack(
  627. &const_expr_ctx, flag, VALUE_TYPE_I64,
  628. #if WASM_ENABLE_GC != 0
  629. NULL, 0,
  630. #endif
  631. &cur_value, error_buf, error_buf_size))
  632. goto fail;
  633. break;
  634. /* f32.const */
  635. case INIT_EXPR_TYPE_F32_CONST:
  636. CHECK_BUF(p, p_end, 4);
  637. p_float = (uint8 *)&cur_value.f32;
  638. for (i = 0; i < sizeof(float32); i++)
  639. *p_float++ = *p++;
  640. if (!push_const_expr_stack(
  641. &const_expr_ctx, flag, VALUE_TYPE_F32,
  642. #if WASM_ENABLE_GC != 0
  643. NULL, 0,
  644. #endif
  645. &cur_value, error_buf, error_buf_size))
  646. goto fail;
  647. break;
  648. /* f64.const */
  649. case INIT_EXPR_TYPE_F64_CONST:
  650. CHECK_BUF(p, p_end, 8);
  651. p_float = (uint8 *)&cur_value.f64;
  652. for (i = 0; i < sizeof(float64); i++)
  653. *p_float++ = *p++;
  654. if (!push_const_expr_stack(
  655. &const_expr_ctx, flag, VALUE_TYPE_F64,
  656. #if WASM_ENABLE_GC != 0
  657. NULL, 0,
  658. #endif
  659. &cur_value, error_buf, error_buf_size))
  660. goto fail;
  661. break;
  662. #if WASM_ENABLE_SIMD != 0
  663. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  664. /* v128.const */
  665. case INIT_EXPR_TYPE_V128_CONST:
  666. {
  667. uint64 high, low;
  668. CHECK_BUF(p, p_end, 1);
  669. (void)read_uint8(p);
  670. CHECK_BUF(p, p_end, 16);
  671. wasm_runtime_read_v128(p, &high, &low);
  672. p += 16;
  673. cur_value.v128.i64x2[0] = high;
  674. cur_value.v128.i64x2[1] = low;
  675. if (!push_const_expr_stack(
  676. &const_expr_ctx, flag, VALUE_TYPE_V128,
  677. #if WASM_ENABLE_GC != 0
  678. NULL, 0,
  679. #endif
  680. &cur_value, error_buf, error_buf_size))
  681. goto fail;
  682. #if WASM_ENABLE_WAMR_COMPILER != 0
  683. /* If any init_expr is v128.const, mark SIMD used */
  684. module->is_simd_used = true;
  685. #endif
  686. break;
  687. }
  688. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  689. #endif /* end of WASM_ENABLE_SIMD */
  690. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  691. /* ref.func */
  692. case INIT_EXPR_TYPE_FUNCREF_CONST:
  693. {
  694. uint32 func_idx;
  695. read_leb_uint32(p, p_end, func_idx);
  696. cur_value.ref_index = func_idx;
  697. if (!check_function_index(module, func_idx, error_buf,
  698. error_buf_size)) {
  699. goto fail;
  700. }
  701. #if WASM_ENABLE_GC == 0
  702. if (!push_const_expr_stack(&const_expr_ctx, flag,
  703. VALUE_TYPE_FUNCREF, &cur_value,
  704. error_buf, error_buf_size))
  705. goto fail;
  706. #else
  707. if (func_idx < module->import_function_count) {
  708. type_idx =
  709. module->import_functions[func_idx].u.function.type_idx;
  710. }
  711. else {
  712. type_idx = module
  713. ->functions[func_idx
  714. - module->import_function_count]
  715. ->type_idx;
  716. }
  717. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  718. false, type_idx);
  719. if (!push_const_expr_stack(&const_expr_ctx, flag,
  720. cur_ref_type.ref_type, &cur_ref_type,
  721. 0, &cur_value, error_buf,
  722. error_buf_size))
  723. goto fail;
  724. #endif
  725. #if WASM_ENABLE_WAMR_COMPILER != 0
  726. module->is_ref_types_used = true;
  727. #endif
  728. break;
  729. }
  730. /* ref.null */
  731. case INIT_EXPR_TYPE_REFNULL_CONST:
  732. {
  733. uint8 type1;
  734. CHECK_BUF(p, p_end, 1);
  735. type1 = read_uint8(p);
  736. #if WASM_ENABLE_GC == 0
  737. cur_value.ref_index = NULL_REF;
  738. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  739. &cur_value, error_buf,
  740. error_buf_size))
  741. goto fail;
  742. #else
  743. cur_value.gc_obj = NULL_REF;
  744. if (!is_byte_a_type(type1)) {
  745. p--;
  746. read_leb_uint32(p, p_end, type_idx);
  747. if (!check_type_index(module, module->type_count, type_idx,
  748. error_buf, error_buf_size))
  749. goto fail;
  750. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  751. true, type_idx);
  752. if (!push_const_expr_stack(&const_expr_ctx, flag,
  753. cur_ref_type.ref_type,
  754. &cur_ref_type, 0, &cur_value,
  755. error_buf, error_buf_size))
  756. goto fail;
  757. }
  758. else {
  759. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  760. NULL, 0, &cur_value, error_buf,
  761. error_buf_size))
  762. goto fail;
  763. }
  764. #endif
  765. #if WASM_ENABLE_WAMR_COMPILER != 0
  766. module->is_ref_types_used = true;
  767. #endif
  768. break;
  769. }
  770. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  771. /* get_global */
  772. case INIT_EXPR_TYPE_GET_GLOBAL:
  773. {
  774. uint32 global_idx;
  775. uint8 global_type;
  776. read_leb_uint32(p, p_end, cur_value.global_index);
  777. global_idx = cur_value.global_index;
  778. /*
  779. * Currently, constant expressions occurring as initializers
  780. * of globals are further constrained in that contained
  781. * global.get instructions are
  782. * only allowed to refer to imported globals.
  783. *
  784. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  785. */
  786. if (global_idx >= module->import_global_count
  787. /* make spec test happy */
  788. #if WASM_ENABLE_GC != 0
  789. + module->global_count
  790. #endif
  791. ) {
  792. set_error_buf_v(error_buf, error_buf_size,
  793. "unknown global %u", global_idx);
  794. goto fail;
  795. }
  796. if (
  797. /* make spec test happy */
  798. #if WASM_ENABLE_GC != 0
  799. global_idx < module->import_global_count &&
  800. #endif
  801. module->import_globals[global_idx]
  802. .u.global.type.is_mutable) {
  803. set_error_buf_v(error_buf, error_buf_size,
  804. "constant expression required");
  805. goto fail;
  806. }
  807. if (global_idx < module->import_global_count) {
  808. global_type = module->import_globals[global_idx]
  809. .u.global.type.val_type;
  810. #if WASM_ENABLE_GC != 0
  811. if (wasm_is_type_multi_byte_type(global_type)) {
  812. WASMRefType *global_ref_type =
  813. module->import_globals[global_idx]
  814. .u.global.ref_type;
  815. bh_memcpy_s(&cur_ref_type,
  816. wasm_reftype_struct_size(global_ref_type),
  817. global_ref_type,
  818. wasm_reftype_struct_size(global_ref_type));
  819. }
  820. #endif
  821. }
  822. else {
  823. global_type =
  824. module
  825. ->globals[global_idx - module->import_global_count]
  826. .type.val_type;
  827. #if WASM_ENABLE_GC != 0
  828. if (wasm_is_type_multi_byte_type(global_type)) {
  829. WASMRefType *global_ref_type =
  830. module
  831. ->globals[global_idx
  832. - module->import_global_count]
  833. .ref_type;
  834. bh_memcpy_s(&cur_ref_type,
  835. wasm_reftype_struct_size(global_ref_type),
  836. global_ref_type,
  837. wasm_reftype_struct_size(global_ref_type));
  838. }
  839. #endif
  840. }
  841. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  842. #if WASM_ENABLE_GC != 0
  843. &cur_ref_type, 0,
  844. #endif
  845. &cur_value, error_buf,
  846. error_buf_size))
  847. goto fail;
  848. break;
  849. }
  850. #if WASM_ENABLE_GC != 0
  851. /* struct.new and array.new */
  852. case WASM_OP_GC_PREFIX:
  853. {
  854. read_leb_uint32(p, p_end, opcode1);
  855. switch (opcode1) {
  856. case WASM_OP_STRUCT_NEW:
  857. {
  858. WASMStructType *struct_type;
  859. WASMStructNewInitValues *struct_init_values = NULL;
  860. uint32 field_count;
  861. read_leb_uint32(p, p_end, type_idx);
  862. if (!check_type_index(module, module->type_count,
  863. type_idx, error_buf,
  864. error_buf_size)) {
  865. goto fail;
  866. }
  867. struct_type = (WASMStructType *)module->types[type_idx];
  868. if (struct_type->base_type.type_flag
  869. != WASM_TYPE_STRUCT) {
  870. set_error_buf(error_buf, error_buf_size,
  871. "unkown struct type");
  872. goto fail;
  873. }
  874. field_count = struct_type->field_count;
  875. if (!(struct_init_values = loader_malloc(
  876. offsetof(WASMStructNewInitValues, fields)
  877. + (uint64)field_count * sizeof(WASMValue),
  878. error_buf, error_buf_size))) {
  879. goto fail;
  880. }
  881. struct_init_values->type_idx = type_idx;
  882. struct_init_values->count = field_count;
  883. for (i = field_count; i > 0; i--) {
  884. WASMRefType *field_ref_type = NULL;
  885. uint32 field_idx = i - 1;
  886. uint8 field_type =
  887. struct_type->fields[field_idx].field_type;
  888. if (wasm_is_type_multi_byte_type(field_type)) {
  889. field_ref_type = wasm_reftype_map_find(
  890. struct_type->ref_type_maps,
  891. struct_type->ref_type_map_count, field_idx);
  892. }
  893. if (is_packed_type(field_type)) {
  894. field_type = VALUE_TYPE_I32;
  895. }
  896. if (!pop_const_expr_stack(
  897. &const_expr_ctx, NULL, field_type,
  898. field_ref_type, NULL,
  899. &struct_init_values->fields[field_idx],
  900. error_buf, error_buf_size)) {
  901. destroy_init_expr_data_recursive(
  902. module, struct_init_values);
  903. goto fail;
  904. }
  905. }
  906. cur_value.data = struct_init_values;
  907. wasm_set_refheaptype_typeidx(
  908. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  909. if (!push_const_expr_stack(
  910. &const_expr_ctx, flag, cur_ref_type.ref_type,
  911. &cur_ref_type, (uint8)opcode1, &cur_value,
  912. error_buf, error_buf_size)) {
  913. destroy_init_expr_data_recursive(
  914. module, struct_init_values);
  915. goto fail;
  916. }
  917. break;
  918. }
  919. case WASM_OP_STRUCT_NEW_DEFAULT:
  920. {
  921. read_leb_uint32(p, p_end, cur_value.type_index);
  922. type_idx = cur_value.type_index;
  923. if (!check_type_index(module, module->type_count,
  924. type_idx, error_buf,
  925. error_buf_size)) {
  926. goto fail;
  927. }
  928. if (module->types[type_idx]->type_flag
  929. != WASM_TYPE_STRUCT) {
  930. set_error_buf(error_buf, error_buf_size,
  931. "unkown struct type");
  932. goto fail;
  933. }
  934. cur_value.type_index = type_idx;
  935. wasm_set_refheaptype_typeidx(
  936. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  937. if (!push_const_expr_stack(
  938. &const_expr_ctx, flag, cur_ref_type.ref_type,
  939. &cur_ref_type, (uint8)opcode1, &cur_value,
  940. error_buf, error_buf_size)) {
  941. goto fail;
  942. }
  943. break;
  944. }
  945. case WASM_OP_ARRAY_NEW:
  946. case WASM_OP_ARRAY_NEW_DEFAULT:
  947. case WASM_OP_ARRAY_NEW_FIXED:
  948. {
  949. WASMArrayNewInitValues *array_init_values = NULL;
  950. WASMArrayType *array_type = NULL;
  951. WASMRefType *elem_ref_type = NULL;
  952. uint64 total_size;
  953. uint8 elem_type;
  954. read_leb_uint32(p, p_end, cur_value.type_index);
  955. type_idx = cur_value.type_index;
  956. if (!check_type_index(module, module->type_count,
  957. type_idx, error_buf,
  958. error_buf_size)) {
  959. goto fail;
  960. }
  961. array_type = (WASMArrayType *)module->types[type_idx];
  962. if (array_type->base_type.type_flag
  963. != WASM_TYPE_ARRAY) {
  964. set_error_buf(error_buf, error_buf_size,
  965. "unkown array type");
  966. goto fail;
  967. }
  968. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  969. elem_type = array_type->elem_type;
  970. if (wasm_is_type_multi_byte_type(elem_type)) {
  971. elem_ref_type = array_type->elem_ref_type;
  972. }
  973. if (is_packed_type(elem_type)) {
  974. elem_type = VALUE_TYPE_I32;
  975. }
  976. if (opcode1 == WASM_OP_ARRAY_NEW) {
  977. WASMValue len_val;
  978. if (!(array_init_values = loader_malloc(
  979. sizeof(WASMArrayNewInitValues),
  980. error_buf, error_buf_size))) {
  981. goto fail;
  982. }
  983. array_init_values->type_idx = type_idx;
  984. if (!pop_const_expr_stack(
  985. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  986. NULL, NULL, &len_val, error_buf,
  987. error_buf_size)) {
  988. destroy_init_expr_data_recursive(
  989. module, array_init_values);
  990. goto fail;
  991. }
  992. array_init_values->length = len_val.i32;
  993. if (!pop_const_expr_stack(
  994. &const_expr_ctx, NULL, elem_type,
  995. elem_ref_type, NULL,
  996. &array_init_values->elem_data[0],
  997. error_buf, error_buf_size)) {
  998. destroy_init_expr_data_recursive(
  999. module, array_init_values);
  1000. goto fail;
  1001. }
  1002. cur_value.data = array_init_values;
  1003. }
  1004. else {
  1005. /* WASM_OP_ARRAY_NEW_FIXED */
  1006. uint32 len;
  1007. read_leb_uint32(p, p_end, len);
  1008. total_size =
  1009. (uint64)offsetof(WASMArrayNewInitValues,
  1010. elem_data)
  1011. + (uint64)sizeof(WASMValue) * len;
  1012. if (!(array_init_values =
  1013. loader_malloc(total_size, error_buf,
  1014. error_buf_size))) {
  1015. goto fail;
  1016. }
  1017. array_init_values->type_idx = type_idx;
  1018. array_init_values->length = len;
  1019. for (i = len; i > 0; i--) {
  1020. if (!pop_const_expr_stack(
  1021. &const_expr_ctx, NULL, elem_type,
  1022. elem_ref_type, NULL,
  1023. &array_init_values
  1024. ->elem_data[i - 1],
  1025. error_buf, error_buf_size)) {
  1026. destroy_init_expr_data_recursive(
  1027. module, array_init_values);
  1028. goto fail;
  1029. }
  1030. }
  1031. cur_value.data = array_init_values;
  1032. }
  1033. }
  1034. else {
  1035. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1036. WASMValue len_val;
  1037. uint32 len;
  1038. /* POP(i32) */
  1039. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1040. VALUE_TYPE_I32, NULL,
  1041. NULL, &len_val, error_buf,
  1042. error_buf_size)) {
  1043. goto fail;
  1044. }
  1045. len = len_val.i32;
  1046. cur_value.array_new_default.type_index = type_idx;
  1047. cur_value.array_new_default.length = len;
  1048. }
  1049. wasm_set_refheaptype_typeidx(
  1050. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1051. if (!push_const_expr_stack(
  1052. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1053. &cur_ref_type, (uint8)opcode1, &cur_value,
  1054. error_buf, error_buf_size)) {
  1055. if (array_init_values) {
  1056. destroy_init_expr_data_recursive(
  1057. module, array_init_values);
  1058. }
  1059. goto fail;
  1060. }
  1061. break;
  1062. }
  1063. case WASM_OP_ANY_CONVERT_EXTERN:
  1064. {
  1065. set_error_buf(error_buf, error_buf_size,
  1066. "unsupported constant expression of "
  1067. "extern.internalize");
  1068. goto fail;
  1069. }
  1070. case WASM_OP_EXTERN_CONVERT_ANY:
  1071. {
  1072. set_error_buf(error_buf, error_buf_size,
  1073. "unsupported constant expression of "
  1074. "extern.externalize");
  1075. goto fail;
  1076. }
  1077. case WASM_OP_REF_I31:
  1078. {
  1079. /* POP(i32) */
  1080. if (!pop_const_expr_stack(
  1081. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1082. NULL, &cur_value, error_buf, error_buf_size)) {
  1083. goto fail;
  1084. }
  1085. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1086. false, HEAP_TYPE_I31);
  1087. if (!push_const_expr_stack(
  1088. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1089. &cur_ref_type, (uint8)opcode1, &cur_value,
  1090. error_buf, error_buf_size)) {
  1091. goto fail;
  1092. }
  1093. break;
  1094. }
  1095. default:
  1096. set_error_buf(
  1097. error_buf, error_buf_size,
  1098. "type mismatch or constant expression required");
  1099. goto fail;
  1100. }
  1101. break;
  1102. }
  1103. #endif /* end of WASM_ENABLE_GC != 0 */
  1104. default:
  1105. {
  1106. set_error_buf(error_buf, error_buf_size,
  1107. "illegal opcode "
  1108. "or constant expression required "
  1109. "or type mismatch");
  1110. goto fail;
  1111. }
  1112. }
  1113. CHECK_BUF(p, p_end, 1);
  1114. flag = read_uint8(p);
  1115. }
  1116. /* There should be only one value left on the init value stack */
  1117. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1118. #if WASM_ENABLE_GC != 0
  1119. ref_type, &opcode,
  1120. #endif
  1121. &cur_value, error_buf, error_buf_size)) {
  1122. goto fail;
  1123. }
  1124. if (const_expr_ctx.sp != 0) {
  1125. set_error_buf(error_buf, error_buf_size,
  1126. "type mismatch: illegal constant opcode sequence");
  1127. goto fail;
  1128. }
  1129. init_expr->init_expr_type = flag;
  1130. init_expr->u = cur_value;
  1131. #if WASM_ENABLE_GC != 0
  1132. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1133. switch (opcode) {
  1134. case WASM_OP_STRUCT_NEW:
  1135. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1136. break;
  1137. case WASM_OP_STRUCT_NEW_DEFAULT:
  1138. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1139. break;
  1140. case WASM_OP_ARRAY_NEW:
  1141. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1142. break;
  1143. case WASM_OP_ARRAY_NEW_DEFAULT:
  1144. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1145. break;
  1146. case WASM_OP_ARRAY_NEW_FIXED:
  1147. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1148. break;
  1149. case WASM_OP_REF_I31:
  1150. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1151. break;
  1152. default:
  1153. bh_assert(0);
  1154. break;
  1155. }
  1156. }
  1157. #endif /* end of WASM_ENABLE_GC != 0 */
  1158. *p_buf = p;
  1159. destroy_const_expr_stack(&const_expr_ctx);
  1160. return true;
  1161. fail:
  1162. destroy_const_expr_stack(&const_expr_ctx);
  1163. return false;
  1164. }
  1165. static bool
  1166. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1167. {
  1168. if (mutable >= 2) {
  1169. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1170. return false;
  1171. }
  1172. return true;
  1173. }
  1174. #if WASM_ENABLE_GC != 0
  1175. static void
  1176. destroy_func_type(WASMFuncType *type)
  1177. {
  1178. /* Destroy the reference type hash set */
  1179. if (type->ref_type_maps)
  1180. wasm_runtime_free(type->ref_type_maps);
  1181. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1182. && WASM_ENABLE_LAZY_JIT != 0
  1183. if (type->call_to_llvm_jit_from_fast_jit)
  1184. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1185. #endif
  1186. /* Free the type */
  1187. wasm_runtime_free(type);
  1188. }
  1189. static void
  1190. destroy_struct_type(WASMStructType *type)
  1191. {
  1192. if (type->ref_type_maps)
  1193. wasm_runtime_free(type->ref_type_maps);
  1194. wasm_runtime_free(type);
  1195. }
  1196. static void
  1197. destroy_array_type(WASMArrayType *type)
  1198. {
  1199. wasm_runtime_free(type);
  1200. }
  1201. static void
  1202. destroy_wasm_type(WASMType *type)
  1203. {
  1204. if (type->ref_count > 1) {
  1205. /* The type is referenced by other types
  1206. of current wasm module */
  1207. type->ref_count--;
  1208. return;
  1209. }
  1210. if (type->type_flag == WASM_TYPE_FUNC)
  1211. destroy_func_type((WASMFuncType *)type);
  1212. else if (type->type_flag == WASM_TYPE_STRUCT)
  1213. destroy_struct_type((WASMStructType *)type);
  1214. else if (type->type_flag == WASM_TYPE_ARRAY)
  1215. destroy_array_type((WASMArrayType *)type);
  1216. else {
  1217. bh_assert(0);
  1218. }
  1219. }
  1220. /* Resolve (ref null ht) or (ref ht) */
  1221. static bool
  1222. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1223. WASMModule *module, uint32 type_count, bool nullable,
  1224. WASMRefType *ref_type, char *error_buf,
  1225. uint32 error_buf_size)
  1226. {
  1227. const uint8 *p = *p_buf, *p_end = buf_end;
  1228. ref_type->ref_type =
  1229. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1230. ref_type->ref_ht_common.nullable = nullable;
  1231. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1232. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1233. /* heap type is (type i), i : typeidx, >= 0 */
  1234. if (!check_type_index(module, type_count,
  1235. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1236. error_buf_size)) {
  1237. return false;
  1238. }
  1239. }
  1240. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1241. /* heap type is func, extern, any, eq, i31 or data */
  1242. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1243. return false;
  1244. }
  1245. *p_buf = p;
  1246. return true;
  1247. fail:
  1248. return false;
  1249. }
  1250. static bool
  1251. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1252. WASMModule *module, uint32 type_count,
  1253. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1254. bool allow_packed_type, char *error_buf,
  1255. uint32 error_buf_size)
  1256. {
  1257. const uint8 *p = *p_buf, *p_end = buf_end;
  1258. uint8 type;
  1259. memset(ref_type, 0, sizeof(WASMRefType));
  1260. CHECK_BUF(p, p_end, 1);
  1261. type = read_uint8(p);
  1262. if (wasm_is_reftype_htref_nullable(type)) {
  1263. /* (ref null ht) */
  1264. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1265. ref_type, error_buf, error_buf_size))
  1266. return false;
  1267. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1268. *p_need_ref_type_map = true;
  1269. else {
  1270. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1271. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1272. multi-byte type to one-byte type to reduce the footprint and
  1273. the complexity of type equal/subtype checking */
  1274. ref_type->ref_type =
  1275. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1276. *p_need_ref_type_map = false;
  1277. }
  1278. }
  1279. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1280. /* (ref ht) */
  1281. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1282. ref_type, error_buf, error_buf_size))
  1283. return false;
  1284. *p_need_ref_type_map = true;
  1285. #if WASM_ENABLE_STRINGREF != 0
  1286. /* covert (ref string) to stringref */
  1287. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1288. ref_type->ref_type =
  1289. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1290. *p_need_ref_type_map = false;
  1291. }
  1292. #endif
  1293. }
  1294. else {
  1295. /* type which can be represented by one byte */
  1296. if (!is_valid_value_type(type)
  1297. && !(allow_packed_type && is_packed_type(type))) {
  1298. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1299. return false;
  1300. }
  1301. ref_type->ref_type = type;
  1302. *p_need_ref_type_map = false;
  1303. #if WASM_ENABLE_WAMR_COMPILER != 0
  1304. /* If any value's type is v128, mark the module as SIMD used */
  1305. if (type == VALUE_TYPE_V128)
  1306. module->is_simd_used = true;
  1307. #endif
  1308. }
  1309. *p_buf = p;
  1310. return true;
  1311. fail:
  1312. return false;
  1313. }
  1314. static WASMRefType *
  1315. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1316. char *error_buf, uint32 error_buf_size)
  1317. {
  1318. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1319. if (!ret) {
  1320. set_error_buf(error_buf, error_buf_size,
  1321. "insert ref type to hash set failed");
  1322. }
  1323. return ret;
  1324. }
  1325. static bool
  1326. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1327. uint32 type_count, uint32 type_idx, char *error_buf,
  1328. uint32 error_buf_size)
  1329. {
  1330. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1331. uint32 param_count, result_count, i, j = 0;
  1332. uint32 param_cell_num, ret_cell_num;
  1333. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1334. uint64 total_size;
  1335. bool need_ref_type_map;
  1336. WASMRefType ref_type;
  1337. WASMFuncType *type = NULL;
  1338. /* Parse first time to resolve param count, result count and
  1339. ref type map count */
  1340. read_leb_uint32(p, p_end, param_count);
  1341. p_org = p;
  1342. for (i = 0; i < param_count; i++) {
  1343. if (!resolve_value_type(&p, p_end, module, type_count,
  1344. &need_ref_type_map, &ref_type, false, error_buf,
  1345. error_buf_size)) {
  1346. return false;
  1347. }
  1348. if (need_ref_type_map)
  1349. ref_type_map_count++;
  1350. }
  1351. read_leb_uint32(p, p_end, result_count);
  1352. for (i = 0; i < result_count; i++) {
  1353. if (!resolve_value_type(&p, p_end, module, type_count,
  1354. &need_ref_type_map, &ref_type, false, error_buf,
  1355. error_buf_size)) {
  1356. return false;
  1357. }
  1358. if (need_ref_type_map) {
  1359. ref_type_map_count++;
  1360. result_ref_type_map_count++;
  1361. }
  1362. }
  1363. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1364. "ref type map count: %d",
  1365. type_idx, param_count, result_count, ref_type_map_count);
  1366. /* Parse second time to resolve param types, result types and
  1367. ref type map info */
  1368. p = p_org;
  1369. total_size = offsetof(WASMFuncType, types)
  1370. + sizeof(uint8) * (uint64)(param_count + result_count);
  1371. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1372. return false;
  1373. }
  1374. if (ref_type_map_count > 0) {
  1375. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1376. if (!(type->ref_type_maps =
  1377. loader_malloc(total_size, error_buf, error_buf_size))) {
  1378. goto fail;
  1379. }
  1380. }
  1381. type->base_type.type_flag = WASM_TYPE_FUNC;
  1382. type->param_count = param_count;
  1383. type->result_count = result_count;
  1384. type->ref_type_map_count = ref_type_map_count;
  1385. if (ref_type_map_count > 0) {
  1386. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1387. - result_ref_type_map_count;
  1388. }
  1389. for (i = 0; i < param_count; i++) {
  1390. if (!resolve_value_type(&p, p_end, module, type_count,
  1391. &need_ref_type_map, &ref_type, false, error_buf,
  1392. error_buf_size)) {
  1393. goto fail;
  1394. }
  1395. type->types[i] = ref_type.ref_type;
  1396. if (need_ref_type_map) {
  1397. type->ref_type_maps[j].index = i;
  1398. if (!(type->ref_type_maps[j++].ref_type =
  1399. reftype_set_insert(module->ref_type_set, &ref_type,
  1400. error_buf, error_buf_size))) {
  1401. goto fail;
  1402. }
  1403. }
  1404. }
  1405. read_leb_uint32(p, p_end, result_count);
  1406. for (i = 0; i < result_count; i++) {
  1407. if (!resolve_value_type(&p, p_end, module, type_count,
  1408. &need_ref_type_map, &ref_type, false, error_buf,
  1409. error_buf_size)) {
  1410. goto fail;
  1411. }
  1412. type->types[param_count + i] = ref_type.ref_type;
  1413. if (need_ref_type_map) {
  1414. type->ref_type_maps[j].index = param_count + i;
  1415. if (!(type->ref_type_maps[j++].ref_type =
  1416. reftype_set_insert(module->ref_type_set, &ref_type,
  1417. error_buf, error_buf_size))) {
  1418. goto fail;
  1419. }
  1420. }
  1421. }
  1422. bh_assert(j == type->ref_type_map_count);
  1423. #if TRACE_WASM_LOADER != 0
  1424. os_printf("type %d = ", type_idx);
  1425. wasm_dump_func_type(type);
  1426. #endif
  1427. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1428. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1429. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1430. set_error_buf(error_buf, error_buf_size,
  1431. "param count or result count too large");
  1432. goto fail;
  1433. }
  1434. type->param_cell_num = (uint16)param_cell_num;
  1435. type->ret_cell_num = (uint16)ret_cell_num;
  1436. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1437. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1438. #endif
  1439. #if WASM_ENABLE_WAMR_COMPILER != 0
  1440. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1441. if (type->types[i] == VALUE_TYPE_V128)
  1442. module->is_simd_used = true;
  1443. }
  1444. #endif
  1445. *p_buf = p;
  1446. module->types[type_idx] = (WASMType *)type;
  1447. return true;
  1448. fail:
  1449. if (type)
  1450. destroy_func_type(type);
  1451. return false;
  1452. }
  1453. static bool
  1454. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1455. WASMModule *module, uint32 type_count, uint32 type_idx,
  1456. char *error_buf, uint32 error_buf_size)
  1457. {
  1458. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1459. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1460. uint32 i, j = 0, offset;
  1461. uint16 *reference_table;
  1462. uint64 total_size;
  1463. uint8 mutable;
  1464. bool need_ref_type_map;
  1465. WASMRefType ref_type;
  1466. WASMStructType *type = NULL;
  1467. /* Parse first time to resolve field count and ref type map count */
  1468. read_leb_uint32(p, p_end, field_count);
  1469. p_org = p;
  1470. for (i = 0; i < field_count; i++) {
  1471. if (!resolve_value_type(&p, p_end, module, type_count,
  1472. &need_ref_type_map, &ref_type, true, error_buf,
  1473. error_buf_size)) {
  1474. return false;
  1475. }
  1476. if (need_ref_type_map)
  1477. ref_type_map_count++;
  1478. if (wasm_is_type_reftype(ref_type.ref_type))
  1479. ref_field_count++;
  1480. CHECK_BUF(p, p_end, 1);
  1481. mutable = read_uint8(p);
  1482. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1483. return false;
  1484. }
  1485. }
  1486. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1487. type_idx, field_count, ref_type_map_count);
  1488. /* Parse second time to resolve field types and ref type map info */
  1489. p = p_org;
  1490. total_size = offsetof(WASMStructType, fields)
  1491. + sizeof(WASMStructFieldType) * (uint64)field_count
  1492. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1493. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1494. return false;
  1495. }
  1496. if (ref_type_map_count > 0) {
  1497. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1498. if (!(type->ref_type_maps =
  1499. loader_malloc(total_size, error_buf, error_buf_size))) {
  1500. goto fail;
  1501. }
  1502. }
  1503. type->reference_table = reference_table =
  1504. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1505. + sizeof(WASMStructFieldType) * field_count);
  1506. *reference_table++ = ref_field_count;
  1507. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1508. type->field_count = field_count;
  1509. type->ref_type_map_count = ref_type_map_count;
  1510. offset = (uint32)offsetof(WASMStructObject, field_data);
  1511. for (i = 0; i < field_count; i++) {
  1512. if (!resolve_value_type(&p, p_end, module, type_count,
  1513. &need_ref_type_map, &ref_type, true, error_buf,
  1514. error_buf_size)) {
  1515. goto fail;
  1516. }
  1517. type->fields[i].field_type = ref_type.ref_type;
  1518. if (need_ref_type_map) {
  1519. type->ref_type_maps[j].index = i;
  1520. if (!(type->ref_type_maps[j++].ref_type =
  1521. reftype_set_insert(module->ref_type_set, &ref_type,
  1522. error_buf, error_buf_size))) {
  1523. goto fail;
  1524. }
  1525. }
  1526. CHECK_BUF(p, p_end, 1);
  1527. type->fields[i].field_flags = read_uint8(p);
  1528. type->fields[i].field_size =
  1529. (uint8)wasm_reftype_size(ref_type.ref_type);
  1530. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1531. || defined(BUILD_TARGET_X86_32))
  1532. if (type->fields[i].field_size == 2)
  1533. offset = align_uint(offset, 2);
  1534. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1535. offset = align_uint(offset, 4);
  1536. #endif
  1537. type->fields[i].field_offset = offset;
  1538. if (wasm_is_type_reftype(ref_type.ref_type))
  1539. *reference_table++ = offset;
  1540. offset += type->fields[i].field_size;
  1541. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1542. type->fields[i].field_flags, type->fields[i].field_type);
  1543. }
  1544. type->total_size = offset;
  1545. bh_assert(j == type->ref_type_map_count);
  1546. #if TRACE_WASM_LOADER != 0
  1547. os_printf("type %d = ", type_idx);
  1548. wasm_dump_struct_type(type);
  1549. #endif
  1550. *p_buf = p;
  1551. module->types[type_idx] = (WASMType *)type;
  1552. return true;
  1553. fail:
  1554. if (type)
  1555. destroy_struct_type(type);
  1556. return false;
  1557. }
  1558. static bool
  1559. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1560. WASMModule *module, uint32 type_count, uint32 type_idx,
  1561. char *error_buf, uint32 error_buf_size)
  1562. {
  1563. const uint8 *p = *p_buf, *p_end = buf_end;
  1564. uint8 mutable;
  1565. bool need_ref_type_map;
  1566. WASMRefType ref_type;
  1567. WASMArrayType *type = NULL;
  1568. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1569. &ref_type, true, error_buf, error_buf_size)) {
  1570. return false;
  1571. }
  1572. CHECK_BUF(p, p_end, 1);
  1573. mutable = read_uint8(p);
  1574. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1575. return false;
  1576. }
  1577. LOG_VERBOSE("type %u: array", type_idx);
  1578. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1579. error_buf_size))) {
  1580. return false;
  1581. }
  1582. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1583. type->elem_flags = mutable;
  1584. type->elem_type = ref_type.ref_type;
  1585. if (need_ref_type_map) {
  1586. if (!(type->elem_ref_type =
  1587. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1588. error_buf_size))) {
  1589. goto fail;
  1590. }
  1591. }
  1592. #if TRACE_WASM_LOADER != 0
  1593. os_printf("type %d = ", type_idx);
  1594. wasm_dump_array_type(type);
  1595. #endif
  1596. *p_buf = p;
  1597. module->types[type_idx] = (WASMType *)type;
  1598. return true;
  1599. fail:
  1600. if (type)
  1601. destroy_array_type(type);
  1602. return false;
  1603. }
  1604. static bool
  1605. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1606. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1607. {
  1608. if (heap_type >= 0) {
  1609. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1610. error_buf_size)) {
  1611. return false;
  1612. }
  1613. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1614. heap_type);
  1615. }
  1616. else {
  1617. if (!wasm_is_valid_heap_type(heap_type)) {
  1618. set_error_buf(error_buf, error_buf_size, "unknown type");
  1619. return false;
  1620. }
  1621. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1622. heap_type);
  1623. if (nullable) {
  1624. /* For (ref null func/extern/any/eq/i31/data),
  1625. they are same as
  1626. funcref/externref/anyref/eqref/i31ref/dataref,
  1627. we convert the multi-byte type to one-byte
  1628. type to reduce the footprint and the
  1629. complexity of type equal/subtype checking */
  1630. ref_type->ref_type =
  1631. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1632. }
  1633. }
  1634. return true;
  1635. }
  1636. static void
  1637. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1638. WASMRefType *ref_type2)
  1639. {
  1640. /**
  1641. * The difference rt1 ∖ rt2 between two reference types is defined as
  1642. * follows:
  1643. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1644. * (ref ht2) = (ref null?1 ht1)
  1645. */
  1646. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1647. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1648. ref_type1, wasm_reftype_struct_size(ref_type1));
  1649. }
  1650. else {
  1651. ref_type_diff->ref_type = ref_type1->ref_type;
  1652. }
  1653. if (ref_type2->ref_ht_common.nullable) {
  1654. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1655. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1656. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1657. (int32)ref_type_diff->ref_type - 0x80);
  1658. }
  1659. else {
  1660. ref_type_diff->ref_ht_typeidx.nullable = false;
  1661. }
  1662. }
  1663. }
  1664. #else /* else of WASM_ENABLE_GC != 0 */
  1665. static void
  1666. destroy_wasm_type(WASMType *type)
  1667. {
  1668. if (type->ref_count > 1) {
  1669. /* The type is referenced by other types
  1670. of current wasm module */
  1671. type->ref_count--;
  1672. return;
  1673. }
  1674. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1675. && WASM_ENABLE_LAZY_JIT != 0
  1676. if (type->call_to_llvm_jit_from_fast_jit)
  1677. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1678. #endif
  1679. wasm_runtime_free(type);
  1680. }
  1681. #endif /* end of WASM_ENABLE_GC != 0 */
  1682. static bool
  1683. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1684. char *error_buf, uint32 error_buf_size)
  1685. {
  1686. const uint8 *p = buf, *p_end = buf_end;
  1687. uint32 type_count, i;
  1688. uint64 total_size;
  1689. uint8 flag;
  1690. #if WASM_ENABLE_GC != 0
  1691. uint32 processed_type_count = 0;
  1692. #endif
  1693. read_leb_uint32(p, p_end, type_count);
  1694. if (type_count) {
  1695. module->type_count = type_count;
  1696. total_size = sizeof(WASMType *) * (uint64)type_count;
  1697. if (!(module->types =
  1698. loader_malloc(total_size, error_buf, error_buf_size))) {
  1699. return false;
  1700. }
  1701. #if WASM_ENABLE_GC == 0
  1702. for (i = 0; i < type_count; i++) {
  1703. WASMFuncType *type;
  1704. const uint8 *p_org;
  1705. uint32 param_count, result_count, j;
  1706. uint32 param_cell_num, ret_cell_num;
  1707. CHECK_BUF(p, p_end, 1);
  1708. flag = read_uint8(p);
  1709. if (flag != 0x60) {
  1710. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1711. return false;
  1712. }
  1713. read_leb_uint32(p, p_end, param_count);
  1714. /* Resolve param count and result count firstly */
  1715. p_org = p;
  1716. CHECK_BUF(p, p_end, param_count);
  1717. p += param_count;
  1718. read_leb_uint32(p, p_end, result_count);
  1719. CHECK_BUF(p, p_end, result_count);
  1720. p = p_org;
  1721. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1722. set_error_buf(error_buf, error_buf_size,
  1723. "param count or result count too large");
  1724. return false;
  1725. }
  1726. total_size = offsetof(WASMFuncType, types)
  1727. + sizeof(uint8) * (uint64)(param_count + result_count);
  1728. if (!(type = module->types[i] =
  1729. loader_malloc(total_size, error_buf, error_buf_size))) {
  1730. return false;
  1731. }
  1732. /* Resolve param types and result types */
  1733. type->ref_count = 1;
  1734. type->param_count = (uint16)param_count;
  1735. type->result_count = (uint16)result_count;
  1736. for (j = 0; j < param_count; j++) {
  1737. CHECK_BUF(p, p_end, 1);
  1738. type->types[j] = read_uint8(p);
  1739. }
  1740. read_leb_uint32(p, p_end, result_count);
  1741. for (j = 0; j < result_count; j++) {
  1742. CHECK_BUF(p, p_end, 1);
  1743. type->types[param_count + j] = read_uint8(p);
  1744. }
  1745. for (j = 0; j < param_count + result_count; j++) {
  1746. if (!is_valid_value_type(type->types[j])) {
  1747. set_error_buf(error_buf, error_buf_size,
  1748. "unknown value type");
  1749. return false;
  1750. }
  1751. }
  1752. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1753. ret_cell_num =
  1754. wasm_get_cell_num(type->types + param_count, result_count);
  1755. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1756. set_error_buf(error_buf, error_buf_size,
  1757. "param count or result count too large");
  1758. return false;
  1759. }
  1760. type->param_cell_num = (uint16)param_cell_num;
  1761. type->ret_cell_num = (uint16)ret_cell_num;
  1762. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1763. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1764. #endif
  1765. #if WASM_ENABLE_WAMR_COMPILER != 0
  1766. for (j = 0; j < type->param_count + type->result_count; j++) {
  1767. if (type->types[j] == VALUE_TYPE_V128)
  1768. module->is_simd_used = true;
  1769. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1770. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1771. module->is_ref_types_used = true;
  1772. }
  1773. #endif
  1774. /* If there is already a same type created, use it instead */
  1775. for (j = 0; j < i; j++) {
  1776. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1777. if (module->types[j]->ref_count == UINT16_MAX) {
  1778. set_error_buf(error_buf, error_buf_size,
  1779. "wasm type's ref count too large");
  1780. return false;
  1781. }
  1782. destroy_wasm_type(type);
  1783. module->types[i] = module->types[j];
  1784. module->types[j]->ref_count++;
  1785. break;
  1786. }
  1787. }
  1788. }
  1789. #else /* else of WASM_ENABLE_GC == 0 */
  1790. for (i = 0; i < type_count; i++) {
  1791. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1792. uint32 rec_count = 1, j;
  1793. bool is_sub_final = true;
  1794. CHECK_BUF(p, p_end, 1);
  1795. flag = read_uint8(p);
  1796. if (flag == DEFINED_TYPE_REC) {
  1797. read_leb_uint32(p, p_end, rec_count);
  1798. if (rec_count > 1) {
  1799. uint64 new_total_size;
  1800. /* integer overflow */
  1801. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1802. set_error_buf(error_buf, error_buf_size,
  1803. "recursive type count too large");
  1804. return false;
  1805. }
  1806. module->type_count += rec_count - 1;
  1807. new_total_size =
  1808. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1809. if (new_total_size > UINT32_MAX) {
  1810. set_error_buf(error_buf, error_buf_size,
  1811. "allocate memory failed");
  1812. return false;
  1813. }
  1814. MEM_REALLOC(module->types, (uint32)total_size,
  1815. (uint32)new_total_size);
  1816. total_size = new_total_size;
  1817. }
  1818. LOG_VERBOSE("Processing rec group [%d-%d]",
  1819. processed_type_count,
  1820. processed_type_count + rec_count - 1);
  1821. }
  1822. else {
  1823. p--;
  1824. }
  1825. for (j = 0; j < rec_count; j++) {
  1826. WASMType *cur_type = NULL;
  1827. CHECK_BUF(p, p_end, 1);
  1828. flag = read_uint8(p);
  1829. parent_type_idx = -1;
  1830. if (flag == DEFINED_TYPE_SUB
  1831. || flag == DEFINED_TYPE_SUB_FINAL) {
  1832. read_leb_uint32(p, p_end, super_type_count);
  1833. if (super_type_count > 1) {
  1834. set_error_buf(error_buf, error_buf_size,
  1835. "super type count too large");
  1836. return false;
  1837. }
  1838. if (super_type_count > 0) {
  1839. read_leb_uint32(p, p_end, parent_type_idx);
  1840. if (parent_type_idx >= processed_type_count + j) {
  1841. set_error_buf_v(error_buf, error_buf_size,
  1842. "unknown type %d", parent_type_idx);
  1843. return false;
  1844. }
  1845. if (module->types[parent_type_idx]->is_sub_final) {
  1846. set_error_buf(error_buf, error_buf_size,
  1847. "sub type can not inherit from "
  1848. "a final super type");
  1849. return false;
  1850. }
  1851. }
  1852. if (flag == DEFINED_TYPE_SUB)
  1853. is_sub_final = false;
  1854. CHECK_BUF(p, p_end, 1);
  1855. flag = read_uint8(p);
  1856. }
  1857. if (flag == DEFINED_TYPE_FUNC) {
  1858. if (!resolve_func_type(&p, buf_end, module,
  1859. processed_type_count + rec_count,
  1860. processed_type_count + j, error_buf,
  1861. error_buf_size)) {
  1862. return false;
  1863. }
  1864. }
  1865. else if (flag == DEFINED_TYPE_STRUCT) {
  1866. if (!resolve_struct_type(&p, buf_end, module,
  1867. processed_type_count + rec_count,
  1868. processed_type_count + j,
  1869. error_buf, error_buf_size)) {
  1870. return false;
  1871. }
  1872. }
  1873. else if (flag == DEFINED_TYPE_ARRAY) {
  1874. if (!resolve_array_type(&p, buf_end, module,
  1875. processed_type_count + rec_count,
  1876. processed_type_count + j, error_buf,
  1877. error_buf_size)) {
  1878. return false;
  1879. }
  1880. }
  1881. else {
  1882. set_error_buf(error_buf, error_buf_size,
  1883. "invalid type flag");
  1884. return false;
  1885. }
  1886. cur_type = module->types[processed_type_count + j];
  1887. cur_type->ref_count = 1;
  1888. cur_type->parent_type_idx = parent_type_idx;
  1889. cur_type->is_sub_final = is_sub_final;
  1890. cur_type->rec_count = rec_count;
  1891. cur_type->rec_idx = j;
  1892. cur_type->rec_begin_type_idx = processed_type_count;
  1893. }
  1894. /* resolve subtyping relationship in current rec group */
  1895. for (j = 0; j < rec_count; j++) {
  1896. WASMType *cur_type = module->types[processed_type_count + j];
  1897. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1898. WASMType *parent_type =
  1899. module->types[cur_type->parent_type_idx];
  1900. cur_type->parent_type = parent_type;
  1901. cur_type->root_type = parent_type->root_type;
  1902. if (parent_type->inherit_depth == UINT16_MAX) {
  1903. set_error_buf(error_buf, error_buf_size,
  1904. "parent type's inherit depth too large");
  1905. return false;
  1906. }
  1907. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1908. }
  1909. else {
  1910. cur_type->parent_type = NULL;
  1911. cur_type->root_type = cur_type;
  1912. cur_type->inherit_depth = 0;
  1913. }
  1914. }
  1915. for (j = 0; j < rec_count; j++) {
  1916. WASMType *cur_type = module->types[processed_type_count + j];
  1917. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1918. WASMType *parent_type =
  1919. module->types[cur_type->parent_type_idx];
  1920. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1921. module->types,
  1922. module->type_count)) {
  1923. set_error_buf(error_buf, error_buf_size,
  1924. "sub type does not match super type");
  1925. return false;
  1926. }
  1927. }
  1928. }
  1929. /* If there is already an equivalence type or a group of equivalence
  1930. recursive types created, use it or them instead */
  1931. for (j = 0; j < processed_type_count;) {
  1932. WASMType *src_type = module->types[j];
  1933. WASMType *cur_type = module->types[processed_type_count];
  1934. uint32 k, src_rec_count;
  1935. src_rec_count = src_type->rec_count;
  1936. if (src_rec_count != rec_count) {
  1937. /* no type equivalence */
  1938. j += src_rec_count;
  1939. continue;
  1940. }
  1941. for (k = 0; k < rec_count; k++) {
  1942. src_type = module->types[j + k];
  1943. cur_type = module->types[processed_type_count + k];
  1944. if (!wasm_type_equal(src_type, cur_type, module->types,
  1945. module->type_count)) {
  1946. break;
  1947. }
  1948. }
  1949. if (k < rec_count) {
  1950. /* no type equivalence */
  1951. j += src_rec_count;
  1952. continue;
  1953. }
  1954. /* type equivalence */
  1955. for (k = 0; k < rec_count; k++) {
  1956. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1957. set_error_buf(error_buf, error_buf_size,
  1958. "wasm type's ref count too large");
  1959. return false;
  1960. }
  1961. destroy_wasm_type(module->types[processed_type_count + k]);
  1962. module->types[processed_type_count + k] =
  1963. module->types[j + k];
  1964. module->types[j + k]->ref_count++;
  1965. }
  1966. break;
  1967. }
  1968. if (rec_count > 1) {
  1969. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1970. processed_type_count,
  1971. processed_type_count + rec_count - 1);
  1972. }
  1973. processed_type_count += rec_count;
  1974. }
  1975. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1976. * module->type_count,
  1977. error_buf, error_buf_size))) {
  1978. return false;
  1979. }
  1980. #endif /* end of WASM_ENABLE_GC == 0 */
  1981. }
  1982. if (p != p_end) {
  1983. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1984. return false;
  1985. }
  1986. LOG_VERBOSE("Load type section success.\n");
  1987. return true;
  1988. fail:
  1989. return false;
  1990. }
  1991. static void
  1992. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1993. {
  1994. uint32 default_max_size;
  1995. if (UINT32_MAX / 2 > init_size)
  1996. default_max_size = init_size * 2;
  1997. else
  1998. default_max_size = UINT32_MAX;
  1999. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2000. default_max_size = WASM_TABLE_MAX_SIZE;
  2001. if (max_size_flag) {
  2002. /* module defines the table limitation */
  2003. bh_assert(init_size <= *max_size);
  2004. if (init_size < *max_size) {
  2005. *max_size =
  2006. *max_size < default_max_size ? *max_size : default_max_size;
  2007. }
  2008. }
  2009. else {
  2010. /* partial defined table limitation, gives a default value */
  2011. *max_size = default_max_size;
  2012. }
  2013. }
  2014. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2015. /**
  2016. * Find export item of a module with export info:
  2017. * module name, field name and export kind
  2018. */
  2019. static WASMExport *
  2020. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2021. const char *field_name, uint8 export_kind,
  2022. char *error_buf, uint32 error_buf_size)
  2023. {
  2024. WASMExport *export =
  2025. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2026. export_kind, error_buf, error_buf_size);
  2027. return export;
  2028. }
  2029. #endif
  2030. #if WASM_ENABLE_MULTI_MODULE != 0
  2031. static WASMFunction *
  2032. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  2033. const WASMFuncType *expected_function_type,
  2034. char *error_buf, uint32 error_buf_size)
  2035. {
  2036. WASMModuleCommon *module_reg;
  2037. WASMFunction *function = NULL;
  2038. WASMExport *export = NULL;
  2039. WASMModule *module = NULL;
  2040. WASMFuncType *target_function_type = NULL;
  2041. module_reg = wasm_runtime_find_module_registered(module_name);
  2042. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2043. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2044. function_name);
  2045. set_error_buf(error_buf, error_buf_size, "unknown import");
  2046. return NULL;
  2047. }
  2048. module = (WASMModule *)module_reg;
  2049. export =
  2050. wasm_loader_find_export(module, module_name, function_name,
  2051. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2052. if (!export) {
  2053. return NULL;
  2054. }
  2055. /* resolve function type and function */
  2056. if (export->index < module->import_function_count) {
  2057. target_function_type =
  2058. module->import_functions[export->index].u.function.func_type;
  2059. function = module->import_functions[export->index]
  2060. .u.function.import_func_linked;
  2061. }
  2062. else {
  2063. target_function_type =
  2064. module->functions[export->index - module->import_function_count]
  2065. ->func_type;
  2066. function =
  2067. module->functions[export->index - module->import_function_count];
  2068. }
  2069. /* check function type */
  2070. if (!wasm_type_equal((WASMType *)expected_function_type,
  2071. (WASMType *)target_function_type, module->types,
  2072. module->type_count)) {
  2073. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2074. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2075. return NULL;
  2076. }
  2077. return function;
  2078. }
  2079. static WASMTable *
  2080. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2081. uint32 init_size, uint32 max_size, char *error_buf,
  2082. uint32 error_buf_size)
  2083. {
  2084. WASMModuleCommon *module_reg;
  2085. WASMTable *table = NULL;
  2086. WASMExport *export = NULL;
  2087. WASMModule *module = NULL;
  2088. module_reg = wasm_runtime_find_module_registered(module_name);
  2089. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2090. LOG_DEBUG("can not find a module named %s for table", module_name);
  2091. set_error_buf(error_buf, error_buf_size, "unknown import");
  2092. return NULL;
  2093. }
  2094. module = (WASMModule *)module_reg;
  2095. export =
  2096. wasm_loader_find_export(module, module_name, table_name,
  2097. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2098. if (!export) {
  2099. return NULL;
  2100. }
  2101. /* resolve table and check the init/max size */
  2102. if (export->index < module->import_table_count) {
  2103. table =
  2104. module->import_tables[export->index].u.table.import_table_linked;
  2105. }
  2106. else {
  2107. table = &(module->tables[export->index - module->import_table_count]);
  2108. }
  2109. if (table->table_type.init_size < init_size
  2110. || table->table_type.max_size > max_size) {
  2111. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2112. module_name, table_name, table->table_type.init_size,
  2113. table->table_type.max_size, init_size, max_size);
  2114. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2115. return NULL;
  2116. }
  2117. return table;
  2118. }
  2119. static WASMMemory *
  2120. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2121. uint32 init_page_count, uint32 max_page_count,
  2122. char *error_buf, uint32 error_buf_size)
  2123. {
  2124. WASMModuleCommon *module_reg;
  2125. WASMMemory *memory = NULL;
  2126. WASMExport *export = NULL;
  2127. WASMModule *module = NULL;
  2128. module_reg = wasm_runtime_find_module_registered(module_name);
  2129. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2130. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2131. set_error_buf(error_buf, error_buf_size, "unknown import");
  2132. return NULL;
  2133. }
  2134. module = (WASMModule *)module_reg;
  2135. export =
  2136. wasm_loader_find_export(module, module_name, memory_name,
  2137. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2138. if (!export) {
  2139. return NULL;
  2140. }
  2141. /* resolve memory and check the init/max page count */
  2142. if (export->index < module->import_memory_count) {
  2143. memory = module->import_memories[export->index]
  2144. .u.memory.import_memory_linked;
  2145. }
  2146. else {
  2147. memory =
  2148. &(module->memories[export->index - module->import_memory_count]);
  2149. }
  2150. if (memory->init_page_count < init_page_count
  2151. || memory->max_page_count > max_page_count) {
  2152. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2153. module_name, memory_name, memory->init_page_count,
  2154. memory->max_page_count, init_page_count, max_page_count);
  2155. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2156. return NULL;
  2157. }
  2158. return memory;
  2159. }
  2160. static WASMGlobal *
  2161. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2162. uint8 type, bool is_mutable, char *error_buf,
  2163. uint32 error_buf_size)
  2164. {
  2165. WASMModuleCommon *module_reg;
  2166. WASMGlobal *global = NULL;
  2167. WASMExport *export = NULL;
  2168. WASMModule *module = NULL;
  2169. module_reg = wasm_runtime_find_module_registered(module_name);
  2170. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2171. LOG_DEBUG("can not find a module named %s for global", module_name);
  2172. set_error_buf(error_buf, error_buf_size, "unknown import");
  2173. return NULL;
  2174. }
  2175. module = (WASMModule *)module_reg;
  2176. export =
  2177. wasm_loader_find_export(module, module_name, global_name,
  2178. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2179. if (!export) {
  2180. return NULL;
  2181. }
  2182. /* resolve and check the global */
  2183. if (export->index < module->import_global_count) {
  2184. global =
  2185. module->import_globals[export->index].u.global.import_global_linked;
  2186. }
  2187. else {
  2188. global =
  2189. &(module->globals[export->index - module->import_global_count]);
  2190. }
  2191. if (global->type.val_type != type
  2192. || global->type.is_mutable != is_mutable) {
  2193. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2194. module_name, global_name, global->type.val_type,
  2195. global->type.is_mutable, type, is_mutable);
  2196. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2197. return NULL;
  2198. }
  2199. return global;
  2200. }
  2201. #if WASM_ENABLE_TAGS != 0
  2202. static WASMTag *
  2203. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2204. const WASMType *expected_tag_type,
  2205. uint32 *linked_tag_index, char *error_buf,
  2206. uint32 error_buf_size)
  2207. {
  2208. WASMModuleCommon *module_reg;
  2209. WASMTag *tag = NULL;
  2210. WASMExport *export = NULL;
  2211. WASMModule *module = NULL;
  2212. module_reg = wasm_runtime_find_module_registered(module_name);
  2213. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2214. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2215. tag_name);
  2216. set_error_buf(error_buf, error_buf_size, "unknown import");
  2217. return NULL;
  2218. }
  2219. module = (WASMModule *)module_reg;
  2220. export =
  2221. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2222. error_buf, error_buf_size);
  2223. if (!export) {
  2224. return NULL;
  2225. }
  2226. /* resolve tag type and tag */
  2227. if (export->index < module->import_tag_count) {
  2228. /* importing an imported tag from the submodule */
  2229. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2230. }
  2231. else {
  2232. /* importing an section tag from the submodule */
  2233. tag = module->tags[export->index - module->import_tag_count];
  2234. }
  2235. /* check function type */
  2236. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2237. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2238. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2239. return NULL;
  2240. }
  2241. if (linked_tag_index != NULL) {
  2242. *linked_tag_index = export->index;
  2243. }
  2244. return tag;
  2245. }
  2246. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2247. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2248. static bool
  2249. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2250. const WASMModule *parent_module,
  2251. const char *sub_module_name, const char *function_name,
  2252. WASMFunctionImport *function, char *error_buf,
  2253. uint32 error_buf_size)
  2254. {
  2255. const uint8 *p = *p_buf, *p_end = buf_end;
  2256. uint32 declare_type_index = 0;
  2257. WASMFuncType *declare_func_type = NULL;
  2258. WASMFunction *linked_func = NULL;
  2259. #if WASM_ENABLE_MULTI_MODULE != 0
  2260. WASMModule *sub_module = NULL;
  2261. bool is_built_in_module = false;
  2262. #endif
  2263. const char *linked_signature = NULL;
  2264. void *linked_attachment = NULL;
  2265. bool linked_call_conv_raw = false;
  2266. bool is_native_symbol = false;
  2267. read_leb_uint32(p, p_end, declare_type_index);
  2268. *p_buf = p;
  2269. if (declare_type_index >= parent_module->type_count) {
  2270. set_error_buf(error_buf, error_buf_size, "unknown type");
  2271. return false;
  2272. }
  2273. #if WASM_ENABLE_GC != 0
  2274. function->type_idx = declare_type_index;
  2275. #endif
  2276. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2277. declare_type_index = wasm_get_smallest_type_idx(
  2278. parent_module->types, parent_module->type_count, declare_type_index);
  2279. #endif
  2280. declare_func_type =
  2281. (WASMFuncType *)parent_module->types[declare_type_index];
  2282. /* lookup registered native symbols first */
  2283. linked_func = wasm_native_resolve_symbol(
  2284. sub_module_name, function_name, declare_func_type, &linked_signature,
  2285. &linked_attachment, &linked_call_conv_raw);
  2286. if (linked_func) {
  2287. is_native_symbol = true;
  2288. }
  2289. #if WASM_ENABLE_MULTI_MODULE != 0
  2290. else {
  2291. if (!(is_built_in_module =
  2292. wasm_runtime_is_built_in_module(sub_module_name))) {
  2293. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2294. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2295. error_buf_size);
  2296. }
  2297. if (is_built_in_module || sub_module)
  2298. linked_func = wasm_loader_resolve_function(
  2299. sub_module_name, function_name, declare_func_type, error_buf,
  2300. error_buf_size);
  2301. }
  2302. #endif
  2303. function->module_name = (char *)sub_module_name;
  2304. function->field_name = (char *)function_name;
  2305. function->func_type = declare_func_type;
  2306. /* func_ptr_linked is for native registered symbol */
  2307. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2308. function->signature = linked_signature;
  2309. function->attachment = linked_attachment;
  2310. function->call_conv_raw = linked_call_conv_raw;
  2311. #if WASM_ENABLE_MULTI_MODULE != 0
  2312. function->import_module = is_native_symbol ? NULL : sub_module;
  2313. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2314. #endif
  2315. return true;
  2316. fail:
  2317. return false;
  2318. }
  2319. static bool
  2320. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2321. uint32 error_buf_size)
  2322. {
  2323. if (max_size < init_size) {
  2324. set_error_buf(error_buf, error_buf_size,
  2325. "size minimum must not be greater than maximum");
  2326. return false;
  2327. }
  2328. return true;
  2329. }
  2330. static bool
  2331. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2332. WASMModule *parent_module, const char *sub_module_name,
  2333. const char *table_name, WASMTableImport *table,
  2334. char *error_buf, uint32 error_buf_size)
  2335. {
  2336. const uint8 *p = *p_buf, *p_end = buf_end;
  2337. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2338. declare_init_size = 0, declare_max_size = 0;
  2339. #if WASM_ENABLE_MULTI_MODULE != 0
  2340. WASMModule *sub_module = NULL;
  2341. WASMTable *linked_table = NULL;
  2342. #endif
  2343. #if WASM_ENABLE_GC != 0
  2344. WASMRefType ref_type;
  2345. bool need_ref_type_map;
  2346. #endif
  2347. #if WASM_ENABLE_GC == 0
  2348. CHECK_BUF(p, p_end, 1);
  2349. /* 0x70 or 0x6F */
  2350. declare_elem_type = read_uint8(p);
  2351. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2352. #if WASM_ENABLE_REF_TYPES != 0
  2353. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2354. #endif
  2355. ) {
  2356. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2357. return false;
  2358. }
  2359. #else /* else of WASM_ENABLE_GC == 0 */
  2360. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2361. &need_ref_type_map, &ref_type, false, error_buf,
  2362. error_buf_size)) {
  2363. return false;
  2364. }
  2365. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2366. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2367. return false;
  2368. }
  2369. declare_elem_type = ref_type.ref_type;
  2370. if (need_ref_type_map) {
  2371. if (!(table->table_type.elem_ref_type =
  2372. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2373. error_buf, error_buf_size))) {
  2374. return false;
  2375. }
  2376. }
  2377. #if TRACE_WASM_LOADER != 0
  2378. os_printf("import table type: ");
  2379. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2380. os_printf("\n");
  2381. #endif
  2382. #endif /* end of WASM_ENABLE_GC == 0 */
  2383. read_leb_uint32(p, p_end, declare_max_size_flag);
  2384. if (declare_max_size_flag > 1) {
  2385. set_error_buf(error_buf, error_buf_size, "integer too large");
  2386. return false;
  2387. }
  2388. read_leb_uint32(p, p_end, declare_init_size);
  2389. if (declare_max_size_flag) {
  2390. read_leb_uint32(p, p_end, declare_max_size);
  2391. if (!check_table_max_size(declare_init_size, declare_max_size,
  2392. error_buf, error_buf_size))
  2393. return false;
  2394. }
  2395. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2396. &declare_max_size);
  2397. *p_buf = p;
  2398. #if WASM_ENABLE_MULTI_MODULE != 0
  2399. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2400. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2401. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2402. error_buf_size);
  2403. if (sub_module) {
  2404. linked_table = wasm_loader_resolve_table(
  2405. sub_module_name, table_name, declare_init_size,
  2406. declare_max_size, error_buf, error_buf_size);
  2407. if (linked_table) {
  2408. /* reset with linked table limit */
  2409. declare_elem_type = linked_table->table_type.elem_type;
  2410. declare_init_size = linked_table->table_type.init_size;
  2411. declare_max_size = linked_table->table_type.max_size;
  2412. declare_max_size_flag = linked_table->table_type.flags;
  2413. table->import_table_linked = linked_table;
  2414. table->import_module = sub_module;
  2415. }
  2416. }
  2417. }
  2418. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2419. /* (table (export "table") 10 20 funcref) */
  2420. /* we need this section working in wamrc */
  2421. if (!strcmp("spectest", sub_module_name)) {
  2422. const uint32 spectest_table_init_size = 10;
  2423. const uint32 spectest_table_max_size = 20;
  2424. if (strcmp("table", table_name)) {
  2425. set_error_buf(error_buf, error_buf_size,
  2426. "incompatible import type or unknown import");
  2427. return false;
  2428. }
  2429. if (declare_init_size > spectest_table_init_size
  2430. || declare_max_size < spectest_table_max_size) {
  2431. set_error_buf(error_buf, error_buf_size,
  2432. "incompatible import type");
  2433. return false;
  2434. }
  2435. declare_init_size = spectest_table_init_size;
  2436. declare_max_size = spectest_table_max_size;
  2437. }
  2438. /* now we believe all declaration are ok */
  2439. table->table_type.elem_type = declare_elem_type;
  2440. table->table_type.init_size = declare_init_size;
  2441. table->table_type.flags = declare_max_size_flag;
  2442. table->table_type.max_size = declare_max_size;
  2443. #if WASM_ENABLE_WAMR_COMPILER != 0
  2444. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2445. parent_module->is_ref_types_used = true;
  2446. #endif
  2447. (void)parent_module;
  2448. return true;
  2449. fail:
  2450. return false;
  2451. }
  2452. static bool
  2453. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2454. uint32 error_buf_size)
  2455. {
  2456. uint32 default_max_size =
  2457. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2458. if (!is_memory64 && init_size > default_max_size) {
  2459. set_error_buf(error_buf, error_buf_size,
  2460. "memory size must be at most 65536 pages (4GiB)");
  2461. return false;
  2462. }
  2463. #if WASM_ENABLE_MEMORY64 != 0
  2464. else if (is_memory64 && init_size > default_max_size) {
  2465. set_error_buf(
  2466. error_buf, error_buf_size,
  2467. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2468. return false;
  2469. }
  2470. #endif
  2471. return true;
  2472. }
  2473. static bool
  2474. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2475. char *error_buf, uint32 error_buf_size)
  2476. {
  2477. uint32 default_max_size =
  2478. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2479. if (max_size < init_size) {
  2480. set_error_buf(error_buf, error_buf_size,
  2481. "size minimum must not be greater than maximum");
  2482. return false;
  2483. }
  2484. if (!is_memory64 && max_size > default_max_size) {
  2485. set_error_buf(error_buf, error_buf_size,
  2486. "memory size must be at most 65536 pages (4GiB)");
  2487. return false;
  2488. }
  2489. #if WASM_ENABLE_MEMORY64 != 0
  2490. else if (is_memory64 && max_size > default_max_size) {
  2491. set_error_buf(
  2492. error_buf, error_buf_size,
  2493. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2494. return false;
  2495. }
  2496. #endif
  2497. return true;
  2498. }
  2499. static bool
  2500. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2501. WASMModule *parent_module, const char *sub_module_name,
  2502. const char *memory_name, WASMMemoryImport *memory,
  2503. char *error_buf, uint32 error_buf_size)
  2504. {
  2505. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2506. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2507. uint32 pool_size = wasm_runtime_memory_pool_size();
  2508. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2509. / DEFAULT_NUM_BYTES_PER_PAGE;
  2510. #else
  2511. uint32 max_page_count;
  2512. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2513. uint32 mem_flag = 0;
  2514. bool is_memory64 = false;
  2515. uint32 declare_init_page_count = 0;
  2516. uint32 declare_max_page_count = 0;
  2517. #if WASM_ENABLE_MULTI_MODULE != 0
  2518. WASMModule *sub_module = NULL;
  2519. WASMMemory *linked_memory = NULL;
  2520. #endif
  2521. p_org = p;
  2522. read_leb_uint32(p, p_end, mem_flag);
  2523. is_memory64 = mem_flag & MEMORY64_FLAG;
  2524. if (p - p_org > 1) {
  2525. LOG_VERBOSE("integer representation too long");
  2526. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2527. return false;
  2528. }
  2529. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2530. return false;
  2531. }
  2532. read_leb_uint32(p, p_end, declare_init_page_count);
  2533. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2534. error_buf_size)) {
  2535. return false;
  2536. }
  2537. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2538. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2539. #endif
  2540. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2541. read_leb_uint32(p, p_end, declare_max_page_count);
  2542. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2543. declare_max_page_count, error_buf,
  2544. error_buf_size)) {
  2545. return false;
  2546. }
  2547. if (declare_max_page_count > max_page_count) {
  2548. declare_max_page_count = max_page_count;
  2549. }
  2550. }
  2551. else {
  2552. /* Limit the maximum memory size to max_page_count */
  2553. declare_max_page_count = max_page_count;
  2554. }
  2555. #if WASM_ENABLE_MULTI_MODULE != 0
  2556. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2557. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2558. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2559. error_buf_size);
  2560. if (sub_module) {
  2561. linked_memory = wasm_loader_resolve_memory(
  2562. sub_module_name, memory_name, declare_init_page_count,
  2563. declare_max_page_count, error_buf, error_buf_size);
  2564. if (linked_memory) {
  2565. /**
  2566. * reset with linked memory limit
  2567. */
  2568. memory->import_module = sub_module;
  2569. memory->import_memory_linked = linked_memory;
  2570. declare_init_page_count = linked_memory->init_page_count;
  2571. declare_max_page_count = linked_memory->max_page_count;
  2572. }
  2573. }
  2574. }
  2575. #endif
  2576. /* (memory (export "memory") 1 2) */
  2577. if (!strcmp("spectest", sub_module_name)) {
  2578. uint32 spectest_memory_init_page = 1;
  2579. uint32 spectest_memory_max_page = 2;
  2580. if (strcmp("memory", memory_name)) {
  2581. set_error_buf(error_buf, error_buf_size,
  2582. "incompatible import type or unknown import");
  2583. return false;
  2584. }
  2585. if (declare_init_page_count > spectest_memory_init_page
  2586. || declare_max_page_count < spectest_memory_max_page) {
  2587. set_error_buf(error_buf, error_buf_size,
  2588. "incompatible import type");
  2589. return false;
  2590. }
  2591. declare_init_page_count = spectest_memory_init_page;
  2592. declare_max_page_count = spectest_memory_max_page;
  2593. }
  2594. #if WASM_ENABLE_WASI_TEST != 0
  2595. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2596. else if (!strcmp("foo", sub_module_name)) {
  2597. uint32 spectest_memory_init_page = 1;
  2598. uint32 spectest_memory_max_page = 1;
  2599. if (strcmp("bar", memory_name)) {
  2600. set_error_buf(error_buf, error_buf_size,
  2601. "incompatible import type or unknown import");
  2602. return false;
  2603. }
  2604. if (declare_init_page_count > spectest_memory_init_page
  2605. || declare_max_page_count < spectest_memory_max_page) {
  2606. set_error_buf(error_buf, error_buf_size,
  2607. "incompatible import type");
  2608. return false;
  2609. }
  2610. declare_init_page_count = spectest_memory_init_page;
  2611. declare_max_page_count = spectest_memory_max_page;
  2612. }
  2613. #endif
  2614. /* now we believe all declaration are ok */
  2615. memory->mem_type.flags = mem_flag;
  2616. memory->mem_type.init_page_count = declare_init_page_count;
  2617. memory->mem_type.max_page_count = declare_max_page_count;
  2618. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2619. *p_buf = p;
  2620. (void)parent_module;
  2621. return true;
  2622. fail:
  2623. return false;
  2624. }
  2625. #if WASM_ENABLE_TAGS != 0
  2626. static bool
  2627. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2628. const WASMModule *parent_module, /* this module ! */
  2629. const char *sub_module_name, const char *tag_name,
  2630. WASMTagImport *tag, /* structure to fill */
  2631. char *error_buf, uint32 error_buf_size)
  2632. {
  2633. /* attribute and type of the import statement */
  2634. uint8 declare_tag_attribute;
  2635. uint32 declare_type_index;
  2636. const uint8 *p = *p_buf, *p_end = buf_end;
  2637. #if WASM_ENABLE_MULTI_MODULE != 0
  2638. WASMModule *sub_module = NULL;
  2639. #endif
  2640. /* get the one byte attribute */
  2641. CHECK_BUF(p, p_end, 1);
  2642. declare_tag_attribute = read_uint8(p);
  2643. if (declare_tag_attribute != 0) {
  2644. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2645. goto fail;
  2646. }
  2647. /* get type */
  2648. read_leb_uint32(p, p_end, declare_type_index);
  2649. /* compare against module->types */
  2650. if (declare_type_index >= parent_module->type_count) {
  2651. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2652. goto fail;
  2653. }
  2654. WASMFuncType *declare_tag_type =
  2655. (WASMFuncType *)parent_module->types[declare_type_index];
  2656. /* check, that the type of the declared tag returns void */
  2657. if (declare_tag_type->result_count != 0) {
  2658. set_error_buf(error_buf, error_buf_size,
  2659. "tag type signature does not return void");
  2660. goto fail;
  2661. }
  2662. #if WASM_ENABLE_MULTI_MODULE != 0
  2663. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2664. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2665. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2666. error_buf_size);
  2667. if (sub_module) {
  2668. /* wasm_loader_resolve_tag checks, that the imported tag
  2669. * and the declared tag have the same type
  2670. */
  2671. uint32 linked_tag_index = 0;
  2672. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2673. sub_module_name, tag_name, declare_tag_type,
  2674. &linked_tag_index /* out */, error_buf, error_buf_size);
  2675. if (linked_tag) {
  2676. tag->import_module = sub_module;
  2677. tag->import_tag_linked = linked_tag;
  2678. tag->import_tag_index_linked = linked_tag_index;
  2679. }
  2680. }
  2681. }
  2682. #endif
  2683. /* store to module tag declarations */
  2684. tag->attribute = declare_tag_attribute;
  2685. tag->type = declare_type_index;
  2686. tag->module_name = (char *)sub_module_name;
  2687. tag->field_name = (char *)tag_name;
  2688. tag->tag_type = declare_tag_type;
  2689. *p_buf = p;
  2690. (void)parent_module;
  2691. LOG_VERBOSE("Load tag import success\n");
  2692. return true;
  2693. fail:
  2694. return false;
  2695. }
  2696. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2697. static bool
  2698. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2699. WASMModule *parent_module, char *sub_module_name,
  2700. char *global_name, WASMGlobalImport *global, char *error_buf,
  2701. uint32 error_buf_size)
  2702. {
  2703. const uint8 *p = *p_buf, *p_end = buf_end;
  2704. uint8 declare_type = 0;
  2705. uint8 declare_mutable = 0;
  2706. #if WASM_ENABLE_MULTI_MODULE != 0
  2707. WASMModule *sub_module = NULL;
  2708. WASMGlobal *linked_global = NULL;
  2709. #endif
  2710. #if WASM_ENABLE_GC != 0
  2711. WASMRefType ref_type;
  2712. bool need_ref_type_map;
  2713. #endif
  2714. bool ret = false;
  2715. #if WASM_ENABLE_GC == 0
  2716. CHECK_BUF(p, p_end, 2);
  2717. /* global type */
  2718. declare_type = read_uint8(p);
  2719. if (!is_valid_value_type(declare_type)) {
  2720. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2721. return false;
  2722. }
  2723. declare_mutable = read_uint8(p);
  2724. #else
  2725. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2726. &need_ref_type_map, &ref_type, false, error_buf,
  2727. error_buf_size)) {
  2728. return false;
  2729. }
  2730. declare_type = ref_type.ref_type;
  2731. if (need_ref_type_map) {
  2732. if (!(global->ref_type =
  2733. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2734. error_buf, error_buf_size))) {
  2735. return false;
  2736. }
  2737. }
  2738. #if TRACE_WASM_LOADER != 0
  2739. os_printf("import global type: ");
  2740. wasm_dump_value_type(declare_type, global->ref_type);
  2741. os_printf("\n");
  2742. #endif
  2743. CHECK_BUF(p, p_end, 1);
  2744. declare_mutable = read_uint8(p);
  2745. #endif /* end of WASM_ENABLE_GC == 0 */
  2746. *p_buf = p;
  2747. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2748. return false;
  2749. }
  2750. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2751. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2752. global);
  2753. if (ret) {
  2754. if (global->type.val_type != declare_type
  2755. || global->type.is_mutable != declare_mutable) {
  2756. set_error_buf(error_buf, error_buf_size,
  2757. "incompatible import type");
  2758. return false;
  2759. }
  2760. global->is_linked = true;
  2761. }
  2762. #endif
  2763. #if WASM_ENABLE_MULTI_MODULE != 0
  2764. if (!global->is_linked
  2765. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2766. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2767. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2768. error_buf_size);
  2769. if (sub_module) {
  2770. /* check sub modules */
  2771. linked_global = wasm_loader_resolve_global(
  2772. sub_module_name, global_name, declare_type, declare_mutable,
  2773. error_buf, error_buf_size);
  2774. if (linked_global) {
  2775. global->import_module = sub_module;
  2776. global->import_global_linked = linked_global;
  2777. global->is_linked = true;
  2778. }
  2779. }
  2780. }
  2781. #endif
  2782. global->module_name = sub_module_name;
  2783. global->field_name = global_name;
  2784. global->type.val_type = declare_type;
  2785. global->type.is_mutable = (declare_mutable == 1);
  2786. #if WASM_ENABLE_WAMR_COMPILER != 0
  2787. if (global->type.val_type == VALUE_TYPE_V128)
  2788. parent_module->is_simd_used = true;
  2789. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2790. parent_module->is_ref_types_used = true;
  2791. #endif
  2792. (void)parent_module;
  2793. (void)ret;
  2794. return true;
  2795. fail:
  2796. return false;
  2797. }
  2798. static bool
  2799. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2800. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2801. {
  2802. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2803. #if WASM_ENABLE_GC != 0
  2804. WASMRefType ref_type;
  2805. bool need_ref_type_map;
  2806. #endif
  2807. #if WASM_ENABLE_GC == 0
  2808. CHECK_BUF(p, p_end, 1);
  2809. /* 0x70 or 0x6F */
  2810. table->table_type.elem_type = read_uint8(p);
  2811. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2812. #if WASM_ENABLE_REF_TYPES != 0
  2813. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2814. #endif
  2815. ) {
  2816. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2817. return false;
  2818. }
  2819. #else /* else of WASM_ENABLE_GC == 0 */
  2820. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2821. &need_ref_type_map, &ref_type, false, error_buf,
  2822. error_buf_size)) {
  2823. return false;
  2824. }
  2825. table->table_type.elem_type = ref_type.ref_type;
  2826. if (need_ref_type_map) {
  2827. if (!(table->table_type.elem_ref_type =
  2828. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2829. error_buf_size))) {
  2830. return false;
  2831. }
  2832. }
  2833. #if TRACE_WASM_LOADER != 0
  2834. os_printf("table type: ");
  2835. wasm_dump_value_type(table->table_type.elem_type,
  2836. table->table_type.elem_ref_type);
  2837. os_printf("\n");
  2838. #endif
  2839. #endif /* end of WASM_ENABLE_GC == 0 */
  2840. p_org = p;
  2841. read_leb_uint32(p, p_end, table->table_type.flags);
  2842. #if WASM_ENABLE_SHARED_MEMORY == 0
  2843. if (p - p_org > 1) {
  2844. set_error_buf(error_buf, error_buf_size,
  2845. "integer representation too long");
  2846. return false;
  2847. }
  2848. if (table->table_type.flags > 1) {
  2849. set_error_buf(error_buf, error_buf_size, "integer too large");
  2850. return false;
  2851. }
  2852. #else
  2853. if (p - p_org > 1) {
  2854. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2855. return false;
  2856. }
  2857. if (table->table_type.flags == 2) {
  2858. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2859. return false;
  2860. }
  2861. if (table->table_type.flags > 1) {
  2862. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2863. return false;
  2864. }
  2865. #endif
  2866. read_leb_uint32(p, p_end, table->table_type.init_size);
  2867. if (table->table_type.flags) {
  2868. read_leb_uint32(p, p_end, table->table_type.max_size);
  2869. if (!check_table_max_size(table->table_type.init_size,
  2870. table->table_type.max_size, error_buf,
  2871. error_buf_size))
  2872. return false;
  2873. }
  2874. adjust_table_max_size(table->table_type.init_size, table->table_type.flags,
  2875. &table->table_type.max_size);
  2876. #if WASM_ENABLE_WAMR_COMPILER != 0
  2877. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2878. module->is_ref_types_used = true;
  2879. #endif
  2880. *p_buf = p;
  2881. return true;
  2882. fail:
  2883. return false;
  2884. }
  2885. static bool
  2886. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2887. char *error_buf, uint32 error_buf_size)
  2888. {
  2889. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2890. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2891. uint32 pool_size = wasm_runtime_memory_pool_size();
  2892. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2893. / DEFAULT_NUM_BYTES_PER_PAGE;
  2894. #else
  2895. uint32 max_page_count;
  2896. #endif
  2897. bool is_memory64 = false;
  2898. p_org = p;
  2899. read_leb_uint32(p, p_end, memory->flags);
  2900. is_memory64 = memory->flags & MEMORY64_FLAG;
  2901. if (p - p_org > 1) {
  2902. LOG_VERBOSE("integer representation too long");
  2903. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2904. return false;
  2905. }
  2906. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2907. false)) {
  2908. return false;
  2909. }
  2910. read_leb_uint32(p, p_end, memory->init_page_count);
  2911. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2912. error_buf_size))
  2913. return false;
  2914. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2915. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2916. #endif
  2917. if (memory->flags & 1) {
  2918. read_leb_uint32(p, p_end, memory->max_page_count);
  2919. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2920. memory->max_page_count, error_buf,
  2921. error_buf_size))
  2922. return false;
  2923. if (memory->max_page_count > max_page_count)
  2924. memory->max_page_count = max_page_count;
  2925. }
  2926. else {
  2927. /* Limit the maximum memory size to max_page_count */
  2928. memory->max_page_count = max_page_count;
  2929. }
  2930. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2931. *p_buf = p;
  2932. return true;
  2933. fail:
  2934. return false;
  2935. }
  2936. static bool
  2937. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2938. bool is_load_from_file_buf, char *error_buf,
  2939. uint32 error_buf_size)
  2940. {
  2941. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2942. uint32 import_count, name_len, type_index, i, u32, flags;
  2943. uint64 total_size;
  2944. WASMImport *import;
  2945. WASMImport *import_functions = NULL, *import_tables = NULL;
  2946. WASMImport *import_memories = NULL, *import_globals = NULL;
  2947. #if WASM_ENABLE_TAGS != 0
  2948. WASMImport *import_tags = NULL;
  2949. #endif
  2950. char *sub_module_name, *field_name;
  2951. uint8 u8, kind, global_type;
  2952. read_leb_uint32(p, p_end, import_count);
  2953. if (import_count) {
  2954. module->import_count = import_count;
  2955. total_size = sizeof(WASMImport) * (uint64)import_count;
  2956. if (!(module->imports =
  2957. loader_malloc(total_size, error_buf, error_buf_size))) {
  2958. return false;
  2959. }
  2960. p_old = p;
  2961. /* Scan firstly to get import count of each type */
  2962. for (i = 0; i < import_count; i++) {
  2963. /* module name */
  2964. read_leb_uint32(p, p_end, name_len);
  2965. CHECK_BUF(p, p_end, name_len);
  2966. p += name_len;
  2967. /* field name */
  2968. read_leb_uint32(p, p_end, name_len);
  2969. CHECK_BUF(p, p_end, name_len);
  2970. p += name_len;
  2971. CHECK_BUF(p, p_end, 1);
  2972. /* 0x00/0x01/0x02/0x03/0x04 */
  2973. kind = read_uint8(p);
  2974. switch (kind) {
  2975. case IMPORT_KIND_FUNC: /* import function */
  2976. read_leb_uint32(p, p_end, type_index);
  2977. module->import_function_count++;
  2978. break;
  2979. case IMPORT_KIND_TABLE: /* import table */
  2980. CHECK_BUF(p, p_end, 1);
  2981. /* 0x70 */
  2982. u8 = read_uint8(p);
  2983. read_leb_uint32(p, p_end, flags);
  2984. read_leb_uint32(p, p_end, u32);
  2985. if (flags & 1)
  2986. read_leb_uint32(p, p_end, u32);
  2987. module->import_table_count++;
  2988. if (module->import_table_count > 1) {
  2989. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2990. set_error_buf(error_buf, error_buf_size,
  2991. "multiple tables");
  2992. return false;
  2993. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2994. module->is_ref_types_used = true;
  2995. #endif
  2996. }
  2997. break;
  2998. case IMPORT_KIND_MEMORY: /* import memory */
  2999. read_leb_uint32(p, p_end, flags);
  3000. read_leb_uint32(p, p_end, u32);
  3001. if (flags & 1)
  3002. read_leb_uint32(p, p_end, u32);
  3003. module->import_memory_count++;
  3004. if (module->import_memory_count > 1) {
  3005. set_error_buf(error_buf, error_buf_size,
  3006. "multiple memories");
  3007. return false;
  3008. }
  3009. break;
  3010. #if WASM_ENABLE_TAGS != 0
  3011. case IMPORT_KIND_TAG: /* import tags */
  3012. /* it only counts the number of tags to import */
  3013. module->import_tag_count++;
  3014. CHECK_BUF(p, p_end, 1);
  3015. u8 = read_uint8(p);
  3016. read_leb_uint32(p, p_end, type_index);
  3017. break;
  3018. #endif
  3019. case IMPORT_KIND_GLOBAL: /* import global */
  3020. #if WASM_ENABLE_GC != 0
  3021. /* valtype */
  3022. CHECK_BUF(p, p_end, 1);
  3023. global_type = read_uint8(p);
  3024. if (wasm_is_type_multi_byte_type(global_type)) {
  3025. int32 heap_type;
  3026. read_leb_int32(p, p_end, heap_type);
  3027. (void)heap_type;
  3028. }
  3029. /* mutability */
  3030. CHECK_BUF(p, p_end, 1);
  3031. p += 1;
  3032. #else
  3033. CHECK_BUF(p, p_end, 2);
  3034. p += 2;
  3035. #endif
  3036. (void)global_type;
  3037. module->import_global_count++;
  3038. break;
  3039. default:
  3040. set_error_buf(error_buf, error_buf_size,
  3041. "invalid import kind");
  3042. return false;
  3043. }
  3044. }
  3045. if (module->import_function_count)
  3046. import_functions = module->import_functions = module->imports;
  3047. if (module->import_table_count)
  3048. import_tables = module->import_tables =
  3049. module->imports + module->import_function_count;
  3050. if (module->import_memory_count)
  3051. import_memories = module->import_memories =
  3052. module->imports + module->import_function_count
  3053. + module->import_table_count;
  3054. #if WASM_ENABLE_TAGS != 0
  3055. if (module->import_tag_count)
  3056. import_tags = module->import_tags =
  3057. module->imports + module->import_function_count
  3058. + module->import_table_count + module->import_memory_count;
  3059. if (module->import_global_count)
  3060. import_globals = module->import_globals =
  3061. module->imports + module->import_function_count
  3062. + module->import_table_count + module->import_memory_count
  3063. + module->import_tag_count;
  3064. #else
  3065. if (module->import_global_count)
  3066. import_globals = module->import_globals =
  3067. module->imports + module->import_function_count
  3068. + module->import_table_count + module->import_memory_count;
  3069. #endif
  3070. p = p_old;
  3071. /* Scan again to resolve the data */
  3072. for (i = 0; i < import_count; i++) {
  3073. /* load module name */
  3074. read_leb_uint32(p, p_end, name_len);
  3075. CHECK_BUF(p, p_end, name_len);
  3076. if (!(sub_module_name = wasm_const_str_list_insert(
  3077. p, name_len, module, is_load_from_file_buf, error_buf,
  3078. error_buf_size))) {
  3079. return false;
  3080. }
  3081. p += name_len;
  3082. /* load field name */
  3083. read_leb_uint32(p, p_end, name_len);
  3084. CHECK_BUF(p, p_end, name_len);
  3085. if (!(field_name = wasm_const_str_list_insert(
  3086. p, name_len, module, is_load_from_file_buf, error_buf,
  3087. error_buf_size))) {
  3088. return false;
  3089. }
  3090. p += name_len;
  3091. CHECK_BUF(p, p_end, 1);
  3092. /* 0x00/0x01/0x02/0x03/0x4 */
  3093. kind = read_uint8(p);
  3094. switch (kind) {
  3095. case IMPORT_KIND_FUNC: /* import function */
  3096. bh_assert(import_functions);
  3097. import = import_functions++;
  3098. if (!load_function_import(
  3099. &p, p_end, module, sub_module_name, field_name,
  3100. &import->u.function, error_buf, error_buf_size)) {
  3101. return false;
  3102. }
  3103. break;
  3104. case IMPORT_KIND_TABLE: /* import table */
  3105. bh_assert(import_tables);
  3106. import = import_tables++;
  3107. if (!load_table_import(&p, p_end, module, sub_module_name,
  3108. field_name, &import->u.table,
  3109. error_buf, error_buf_size)) {
  3110. LOG_DEBUG("can not import such a table (%s,%s)",
  3111. sub_module_name, field_name);
  3112. return false;
  3113. }
  3114. break;
  3115. case IMPORT_KIND_MEMORY: /* import memory */
  3116. bh_assert(import_memories);
  3117. import = import_memories++;
  3118. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3119. field_name, &import->u.memory,
  3120. error_buf, error_buf_size)) {
  3121. return false;
  3122. }
  3123. break;
  3124. #if WASM_ENABLE_TAGS != 0
  3125. case IMPORT_KIND_TAG:
  3126. bh_assert(import_tags);
  3127. import = import_tags++;
  3128. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3129. field_name, &import->u.tag, error_buf,
  3130. error_buf_size)) {
  3131. return false;
  3132. }
  3133. break;
  3134. #endif
  3135. case IMPORT_KIND_GLOBAL: /* import global */
  3136. bh_assert(import_globals);
  3137. import = import_globals++;
  3138. if (!load_global_import(&p, p_end, module, sub_module_name,
  3139. field_name, &import->u.global,
  3140. error_buf, error_buf_size)) {
  3141. return false;
  3142. }
  3143. break;
  3144. default:
  3145. set_error_buf(error_buf, error_buf_size,
  3146. "invalid import kind");
  3147. return false;
  3148. }
  3149. import->kind = kind;
  3150. import->u.names.module_name = sub_module_name;
  3151. import->u.names.field_name = field_name;
  3152. }
  3153. #if WASM_ENABLE_LIBC_WASI != 0
  3154. import = module->import_functions;
  3155. for (i = 0; i < module->import_function_count; i++, import++) {
  3156. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3157. || !strcmp(import->u.names.module_name,
  3158. "wasi_snapshot_preview1")) {
  3159. module->import_wasi_api = true;
  3160. break;
  3161. }
  3162. }
  3163. #endif
  3164. }
  3165. if (p != p_end) {
  3166. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3167. return false;
  3168. }
  3169. LOG_VERBOSE("Load import section success.\n");
  3170. (void)u8;
  3171. (void)u32;
  3172. (void)type_index;
  3173. return true;
  3174. fail:
  3175. return false;
  3176. }
  3177. static bool
  3178. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3179. uint32 error_buf_size)
  3180. {
  3181. WASMFuncType *param_type = func->func_type;
  3182. uint32 param_count = param_type->param_count;
  3183. uint8 *param_types = param_type->types;
  3184. uint32 local_count = func->local_count;
  3185. uint8 *local_types = func->local_types;
  3186. uint32 i, local_offset = 0;
  3187. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3188. /*
  3189. * Only allocate memory when total_size is not 0,
  3190. * or the return value of malloc(0) might be NULL on some platforms,
  3191. * which causes wasm loader return false.
  3192. */
  3193. if (total_size > 0
  3194. && !(func->local_offsets =
  3195. loader_malloc(total_size, error_buf, error_buf_size))) {
  3196. return false;
  3197. }
  3198. for (i = 0; i < param_count; i++) {
  3199. func->local_offsets[i] = (uint16)local_offset;
  3200. local_offset += wasm_value_type_cell_num(param_types[i]);
  3201. }
  3202. for (i = 0; i < local_count; i++) {
  3203. func->local_offsets[param_count + i] = (uint16)local_offset;
  3204. local_offset += wasm_value_type_cell_num(local_types[i]);
  3205. }
  3206. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3207. return true;
  3208. }
  3209. static bool
  3210. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3211. const uint8 *buf_code, const uint8 *buf_code_end,
  3212. WASMModule *module, char *error_buf,
  3213. uint32 error_buf_size)
  3214. {
  3215. const uint8 *p = buf, *p_end = buf_end;
  3216. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3217. uint32 func_count;
  3218. uint64 total_size;
  3219. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3220. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3221. uint8 type;
  3222. WASMFunction *func;
  3223. #if WASM_ENABLE_GC != 0
  3224. bool need_ref_type_map;
  3225. WASMRefType ref_type;
  3226. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3227. #endif
  3228. read_leb_uint32(p, p_end, func_count);
  3229. if (buf_code)
  3230. read_leb_uint32(p_code, buf_code_end, code_count);
  3231. if (func_count != code_count) {
  3232. set_error_buf(error_buf, error_buf_size,
  3233. "function and code section have inconsistent lengths or "
  3234. "unexpected end");
  3235. return false;
  3236. }
  3237. if (is_indices_overflow(module->import_function_count, func_count,
  3238. error_buf, error_buf_size))
  3239. return false;
  3240. if (func_count) {
  3241. module->function_count = func_count;
  3242. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3243. if (!(module->functions =
  3244. loader_malloc(total_size, error_buf, error_buf_size))) {
  3245. return false;
  3246. }
  3247. for (i = 0; i < func_count; i++) {
  3248. /* Resolve function type */
  3249. read_leb_uint32(p, p_end, type_index);
  3250. if (type_index >= module->type_count) {
  3251. set_error_buf(error_buf, error_buf_size, "unknown type");
  3252. return false;
  3253. }
  3254. #if WASM_ENABLE_GC != 0
  3255. type_index_org = type_index;
  3256. #endif
  3257. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3258. && WASM_ENABLE_GC == 0
  3259. type_index = wasm_get_smallest_type_idx(
  3260. module->types, module->type_count, type_index);
  3261. #endif
  3262. read_leb_uint32(p_code, buf_code_end, code_size);
  3263. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3264. set_error_buf(error_buf, error_buf_size,
  3265. "invalid function code size");
  3266. return false;
  3267. }
  3268. /* Resolve local set count */
  3269. p_code_end = p_code + code_size;
  3270. local_count = 0;
  3271. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3272. p_code_save = p_code;
  3273. #if WASM_ENABLE_GC != 0
  3274. ref_type_map_count = 0;
  3275. #endif
  3276. /* Calculate total local count */
  3277. for (j = 0; j < local_set_count; j++) {
  3278. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3279. if (sub_local_count > UINT32_MAX - local_count) {
  3280. set_error_buf(error_buf, error_buf_size, "too many locals");
  3281. return false;
  3282. }
  3283. #if WASM_ENABLE_GC == 0
  3284. CHECK_BUF(p_code, buf_code_end, 1);
  3285. /* 0x7F/0x7E/0x7D/0x7C */
  3286. type = read_uint8(p_code);
  3287. local_count += sub_local_count;
  3288. #if WASM_ENABLE_WAMR_COMPILER != 0
  3289. /* If any value's type is v128, mark the module as SIMD used */
  3290. if (type == VALUE_TYPE_V128)
  3291. module->is_simd_used = true;
  3292. #endif
  3293. #else
  3294. if (!resolve_value_type(&p_code, buf_code_end, module,
  3295. module->type_count, &need_ref_type_map,
  3296. &ref_type, false, error_buf,
  3297. error_buf_size)) {
  3298. return false;
  3299. }
  3300. local_count += sub_local_count;
  3301. if (need_ref_type_map)
  3302. ref_type_map_count += sub_local_count;
  3303. #endif
  3304. }
  3305. /* Alloc memory, layout: function structure + local types */
  3306. code_size = (uint32)(p_code_end - p_code);
  3307. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3308. if (!(func = module->functions[i] =
  3309. loader_malloc(total_size, error_buf, error_buf_size))) {
  3310. return false;
  3311. }
  3312. #if WASM_ENABLE_GC != 0
  3313. if (ref_type_map_count > 0) {
  3314. total_size =
  3315. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3316. if (!(func->local_ref_type_maps = loader_malloc(
  3317. total_size, error_buf, error_buf_size))) {
  3318. return false;
  3319. }
  3320. func->local_ref_type_map_count = ref_type_map_count;
  3321. }
  3322. #endif
  3323. /* Set function type, local count, code size and code body */
  3324. func->func_type = (WASMFuncType *)module->types[type_index];
  3325. func->local_count = local_count;
  3326. if (local_count > 0)
  3327. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3328. func->code_size = code_size;
  3329. /*
  3330. * we shall make a copy of code body [p_code, p_code + code_size]
  3331. * when we are worrying about inappropriate releasing behaviour.
  3332. * all code bodies are actually in a buffer which user allocates in
  3333. * his embedding environment and we don't have power on them.
  3334. * it will be like:
  3335. * code_body_cp = malloc(code_size);
  3336. * memcpy(code_body_cp, p_code, code_size);
  3337. * func->code = code_body_cp;
  3338. */
  3339. func->code = (uint8 *)p_code;
  3340. #if WASM_ENABLE_GC != 0
  3341. func->type_idx = type_index_org;
  3342. #endif
  3343. #if WASM_ENABLE_GC != 0
  3344. t = 0;
  3345. #endif
  3346. /* Load each local type */
  3347. p_code = p_code_save;
  3348. local_type_index = 0;
  3349. for (j = 0; j < local_set_count; j++) {
  3350. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3351. /* Note: sub_local_count is allowed to be 0 */
  3352. if (local_type_index > UINT32_MAX - sub_local_count
  3353. || local_type_index + sub_local_count > local_count) {
  3354. set_error_buf(error_buf, error_buf_size,
  3355. "invalid local count");
  3356. return false;
  3357. }
  3358. #if WASM_ENABLE_GC == 0
  3359. CHECK_BUF(p_code, buf_code_end, 1);
  3360. /* 0x7F/0x7E/0x7D/0x7C */
  3361. type = read_uint8(p_code);
  3362. if (!is_valid_value_type(type)) {
  3363. if (type == VALUE_TYPE_V128)
  3364. set_error_buf(error_buf, error_buf_size,
  3365. "v128 value type requires simd feature");
  3366. else if (type == VALUE_TYPE_FUNCREF
  3367. || type == VALUE_TYPE_EXTERNREF)
  3368. set_error_buf(error_buf, error_buf_size,
  3369. "ref value type requires "
  3370. "reference types feature");
  3371. else
  3372. set_error_buf_v(error_buf, error_buf_size,
  3373. "invalid local type 0x%02X", type);
  3374. return false;
  3375. }
  3376. #else
  3377. if (!resolve_value_type(&p_code, buf_code_end, module,
  3378. module->type_count, &need_ref_type_map,
  3379. &ref_type, false, error_buf,
  3380. error_buf_size)) {
  3381. return false;
  3382. }
  3383. if (need_ref_type_map) {
  3384. WASMRefType *ref_type_tmp;
  3385. if (!(ref_type_tmp = reftype_set_insert(
  3386. module->ref_type_set, &ref_type, error_buf,
  3387. error_buf_size))) {
  3388. return false;
  3389. }
  3390. for (k = 0; k < sub_local_count; k++) {
  3391. func->local_ref_type_maps[t + k].ref_type =
  3392. ref_type_tmp;
  3393. func->local_ref_type_maps[t + k].index =
  3394. local_type_index + k;
  3395. }
  3396. t += sub_local_count;
  3397. }
  3398. type = ref_type.ref_type;
  3399. #endif
  3400. for (k = 0; k < sub_local_count; k++) {
  3401. func->local_types[local_type_index++] = type;
  3402. }
  3403. #if WASM_ENABLE_WAMR_COMPILER != 0
  3404. if (type == VALUE_TYPE_V128)
  3405. module->is_simd_used = true;
  3406. else if (type == VALUE_TYPE_FUNCREF
  3407. || type == VALUE_TYPE_EXTERNREF)
  3408. module->is_ref_types_used = true;
  3409. #endif
  3410. }
  3411. bh_assert(local_type_index == func->local_count);
  3412. #if WASM_ENABLE_GC != 0
  3413. bh_assert(t == func->local_ref_type_map_count);
  3414. #if TRACE_WASM_LOADER != 0
  3415. os_printf("func %u, local types: [", i);
  3416. k = 0;
  3417. for (j = 0; j < func->local_count; j++) {
  3418. WASMRefType *ref_type_tmp = NULL;
  3419. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3420. bh_assert(j == func->local_ref_type_maps[k].index);
  3421. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3422. }
  3423. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3424. if (j < func->local_count - 1)
  3425. os_printf(" ");
  3426. }
  3427. os_printf("]\n");
  3428. #endif
  3429. #endif
  3430. func->param_cell_num = func->func_type->param_cell_num;
  3431. func->ret_cell_num = func->func_type->ret_cell_num;
  3432. local_cell_num =
  3433. wasm_get_cell_num(func->local_types, func->local_count);
  3434. if (local_cell_num > UINT16_MAX) {
  3435. set_error_buf(error_buf, error_buf_size,
  3436. "local count too large");
  3437. return false;
  3438. }
  3439. func->local_cell_num = (uint16)local_cell_num;
  3440. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3441. return false;
  3442. p_code = p_code_end;
  3443. }
  3444. }
  3445. if (p != p_end) {
  3446. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3447. return false;
  3448. }
  3449. LOG_VERBOSE("Load function section success.\n");
  3450. return true;
  3451. fail:
  3452. return false;
  3453. }
  3454. static bool
  3455. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3456. char *error_buf, uint32 error_buf_size)
  3457. {
  3458. const uint8 *p = buf, *p_end = buf_end;
  3459. uint32 table_count, i;
  3460. uint64 total_size;
  3461. WASMTable *table;
  3462. read_leb_uint32(p, p_end, table_count);
  3463. if (module->import_table_count + table_count > 1) {
  3464. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3465. /* a total of one table is allowed */
  3466. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3467. return false;
  3468. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3469. module->is_ref_types_used = true;
  3470. #endif
  3471. }
  3472. if (table_count) {
  3473. module->table_count = table_count;
  3474. total_size = sizeof(WASMTable) * (uint64)table_count;
  3475. if (!(module->tables =
  3476. loader_malloc(total_size, error_buf, error_buf_size))) {
  3477. return false;
  3478. }
  3479. /* load each table */
  3480. table = module->tables;
  3481. for (i = 0; i < table_count; i++, table++) {
  3482. #if WASM_ENABLE_GC != 0
  3483. uint8 flag;
  3484. bool has_init = false;
  3485. CHECK_BUF(buf, buf_end, 1);
  3486. flag = read_uint8(p);
  3487. if (flag == TABLE_INIT_EXPR_FLAG) {
  3488. CHECK_BUF(buf, buf_end, 1);
  3489. flag = read_uint8(p);
  3490. if (flag != 0x00) {
  3491. set_error_buf(error_buf, error_buf_size,
  3492. "invalid leading bytes for table");
  3493. return false;
  3494. }
  3495. has_init = true;
  3496. }
  3497. else {
  3498. p--;
  3499. }
  3500. #endif /* end of WASM_ENABLE_GC != 0 */
  3501. if (!load_table(&p, p_end, module, table, error_buf,
  3502. error_buf_size))
  3503. return false;
  3504. #if WASM_ENABLE_GC != 0
  3505. if (has_init) {
  3506. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3507. table->table_type.elem_type,
  3508. table->table_type.elem_ref_type, error_buf,
  3509. error_buf_size))
  3510. return false;
  3511. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3512. && table->init_expr.init_expr_type
  3513. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3514. set_error_buf(
  3515. error_buf, error_buf_size,
  3516. "unsupported initializer expression for table");
  3517. return false;
  3518. }
  3519. }
  3520. else {
  3521. if (wasm_is_reftype_htref_non_nullable(
  3522. table->table_type.elem_type)) {
  3523. set_error_buf(
  3524. error_buf, error_buf_size,
  3525. "type mismatch: non-nullable table without init expr");
  3526. return false;
  3527. }
  3528. }
  3529. #endif /* end of WASM_ENABLE_GC != 0 */
  3530. #if WASM_ENABLE_WAMR_COMPILER != 0
  3531. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3532. module->is_ref_types_used = true;
  3533. #endif
  3534. }
  3535. }
  3536. if (p != p_end) {
  3537. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3538. return false;
  3539. }
  3540. LOG_VERBOSE("Load table section success.\n");
  3541. return true;
  3542. fail:
  3543. return false;
  3544. }
  3545. static bool
  3546. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3547. char *error_buf, uint32 error_buf_size)
  3548. {
  3549. const uint8 *p = buf, *p_end = buf_end;
  3550. uint32 memory_count, i;
  3551. uint64 total_size;
  3552. WASMMemory *memory;
  3553. read_leb_uint32(p, p_end, memory_count);
  3554. /* a total of one memory is allowed */
  3555. if (module->import_memory_count + memory_count > 1) {
  3556. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3557. return false;
  3558. }
  3559. if (memory_count) {
  3560. module->memory_count = memory_count;
  3561. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3562. if (!(module->memories =
  3563. loader_malloc(total_size, error_buf, error_buf_size))) {
  3564. return false;
  3565. }
  3566. /* load each memory */
  3567. memory = module->memories;
  3568. for (i = 0; i < memory_count; i++, memory++)
  3569. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3570. return false;
  3571. }
  3572. if (p != p_end) {
  3573. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3574. return false;
  3575. }
  3576. LOG_VERBOSE("Load memory section success.\n");
  3577. return true;
  3578. fail:
  3579. return false;
  3580. }
  3581. static bool
  3582. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3583. char *error_buf, uint32 error_buf_size)
  3584. {
  3585. const uint8 *p = buf, *p_end = buf_end;
  3586. uint32 global_count, i;
  3587. uint64 total_size;
  3588. WASMGlobal *global;
  3589. uint8 mutable;
  3590. #if WASM_ENABLE_GC != 0
  3591. bool need_ref_type_map;
  3592. WASMRefType ref_type;
  3593. #endif
  3594. read_leb_uint32(p, p_end, global_count);
  3595. if (is_indices_overflow(module->import_global_count, global_count,
  3596. error_buf, error_buf_size))
  3597. return false;
  3598. module->global_count = 0;
  3599. if (global_count) {
  3600. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3601. if (!(module->globals =
  3602. loader_malloc(total_size, error_buf, error_buf_size))) {
  3603. return false;
  3604. }
  3605. global = module->globals;
  3606. for (i = 0; i < global_count; i++, global++) {
  3607. #if WASM_ENABLE_GC == 0
  3608. CHECK_BUF(p, p_end, 2);
  3609. /* global type */
  3610. global->type.val_type = read_uint8(p);
  3611. if (!is_valid_value_type(global->type.val_type)) {
  3612. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3613. return false;
  3614. }
  3615. mutable = read_uint8(p);
  3616. #else
  3617. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3618. &need_ref_type_map, &ref_type, false,
  3619. error_buf, error_buf_size)) {
  3620. return false;
  3621. }
  3622. global->type.val_type = ref_type.ref_type;
  3623. CHECK_BUF(p, p_end, 1);
  3624. mutable = read_uint8(p);
  3625. #endif /* end of WASM_ENABLE_GC */
  3626. #if WASM_ENABLE_WAMR_COMPILER != 0
  3627. if (global->type.val_type == VALUE_TYPE_V128)
  3628. module->is_simd_used = true;
  3629. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3630. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3631. module->is_ref_types_used = true;
  3632. #endif
  3633. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3634. return false;
  3635. }
  3636. global->type.is_mutable = mutable ? true : false;
  3637. /* initialize expression */
  3638. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3639. global->type.val_type,
  3640. #if WASM_ENABLE_GC == 0
  3641. NULL,
  3642. #else
  3643. &ref_type,
  3644. #endif
  3645. error_buf, error_buf_size))
  3646. return false;
  3647. #if WASM_ENABLE_GC != 0
  3648. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3649. uint8 global_type;
  3650. WASMRefType *global_ref_type;
  3651. uint32 global_idx = global->init_expr.u.global_index;
  3652. if (global->init_expr.u.global_index
  3653. >= module->import_global_count + i) {
  3654. set_error_buf(error_buf, error_buf_size, "unknown global");
  3655. return false;
  3656. }
  3657. if (global_idx < module->import_global_count) {
  3658. global_type = module->import_globals[global_idx]
  3659. .u.global.type.val_type;
  3660. global_ref_type =
  3661. module->import_globals[global_idx].u.global.ref_type;
  3662. }
  3663. else {
  3664. global_type =
  3665. module
  3666. ->globals[global_idx - module->import_global_count]
  3667. .type.val_type;
  3668. global_ref_type =
  3669. module
  3670. ->globals[global_idx - module->import_global_count]
  3671. .ref_type;
  3672. }
  3673. if (!wasm_reftype_is_subtype_of(
  3674. global_type, global_ref_type, global->type.val_type,
  3675. global->ref_type, module->types, module->type_count)) {
  3676. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3677. return false;
  3678. }
  3679. }
  3680. if (need_ref_type_map) {
  3681. if (!(global->ref_type =
  3682. reftype_set_insert(module->ref_type_set, &ref_type,
  3683. error_buf, error_buf_size))) {
  3684. return false;
  3685. }
  3686. }
  3687. #if TRACE_WASM_LOADER != 0
  3688. os_printf("global type: ");
  3689. wasm_dump_value_type(global->type, global->ref_type);
  3690. os_printf("\n");
  3691. #endif
  3692. #endif
  3693. module->global_count++;
  3694. }
  3695. bh_assert(module->global_count == global_count);
  3696. }
  3697. if (p != p_end) {
  3698. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3699. return false;
  3700. }
  3701. LOG_VERBOSE("Load global section success.\n");
  3702. return true;
  3703. fail:
  3704. return false;
  3705. }
  3706. static bool
  3707. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3708. bool is_load_from_file_buf, char *error_buf,
  3709. uint32 error_buf_size)
  3710. {
  3711. const uint8 *p = buf, *p_end = buf_end;
  3712. uint32 export_count, i, j, index;
  3713. uint64 total_size;
  3714. uint32 str_len;
  3715. WASMExport *export;
  3716. const char *name;
  3717. read_leb_uint32(p, p_end, export_count);
  3718. if (export_count) {
  3719. module->export_count = export_count;
  3720. total_size = sizeof(WASMExport) * (uint64)export_count;
  3721. if (!(module->exports =
  3722. loader_malloc(total_size, error_buf, error_buf_size))) {
  3723. return false;
  3724. }
  3725. export = module->exports;
  3726. for (i = 0; i < export_count; i++, export ++) {
  3727. #if WASM_ENABLE_THREAD_MGR == 0
  3728. if (p == p_end) {
  3729. /* export section with inconsistent count:
  3730. n export declared, but less than n given */
  3731. set_error_buf(error_buf, error_buf_size,
  3732. "length out of bounds");
  3733. return false;
  3734. }
  3735. #endif
  3736. read_leb_uint32(p, p_end, str_len);
  3737. CHECK_BUF(p, p_end, str_len);
  3738. for (j = 0; j < i; j++) {
  3739. name = module->exports[j].name;
  3740. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3741. set_error_buf(error_buf, error_buf_size,
  3742. "duplicate export name");
  3743. return false;
  3744. }
  3745. }
  3746. if (!(export->name = wasm_const_str_list_insert(
  3747. p, str_len, module, is_load_from_file_buf, error_buf,
  3748. error_buf_size))) {
  3749. return false;
  3750. }
  3751. p += str_len;
  3752. CHECK_BUF(p, p_end, 1);
  3753. export->kind = read_uint8(p);
  3754. read_leb_uint32(p, p_end, index);
  3755. export->index = index;
  3756. switch (export->kind) {
  3757. /* function index */
  3758. case EXPORT_KIND_FUNC:
  3759. if (index >= module->function_count
  3760. + module->import_function_count) {
  3761. set_error_buf(error_buf, error_buf_size,
  3762. "unknown function");
  3763. return false;
  3764. }
  3765. #if WASM_ENABLE_SIMD != 0
  3766. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3767. /* TODO: check func type, if it has v128 param or result,
  3768. report error */
  3769. #endif
  3770. #endif
  3771. break;
  3772. /* table index */
  3773. case EXPORT_KIND_TABLE:
  3774. if (index
  3775. >= module->table_count + module->import_table_count) {
  3776. set_error_buf(error_buf, error_buf_size,
  3777. "unknown table");
  3778. return false;
  3779. }
  3780. break;
  3781. /* memory index */
  3782. case EXPORT_KIND_MEMORY:
  3783. if (index
  3784. >= module->memory_count + module->import_memory_count) {
  3785. set_error_buf(error_buf, error_buf_size,
  3786. "unknown memory");
  3787. return false;
  3788. }
  3789. break;
  3790. #if WASM_ENABLE_TAGS != 0
  3791. /* export tag */
  3792. case EXPORT_KIND_TAG:
  3793. if (index >= module->tag_count + module->import_tag_count) {
  3794. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3795. return false;
  3796. }
  3797. break;
  3798. #endif
  3799. /* global index */
  3800. case EXPORT_KIND_GLOBAL:
  3801. if (index
  3802. >= module->global_count + module->import_global_count) {
  3803. set_error_buf(error_buf, error_buf_size,
  3804. "unknown global");
  3805. return false;
  3806. }
  3807. break;
  3808. default:
  3809. set_error_buf(error_buf, error_buf_size,
  3810. "invalid export kind");
  3811. return false;
  3812. }
  3813. }
  3814. }
  3815. if (p != p_end) {
  3816. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3817. return false;
  3818. }
  3819. LOG_VERBOSE("Load export section success.\n");
  3820. return true;
  3821. fail:
  3822. return false;
  3823. }
  3824. static bool
  3825. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3826. uint32 error_buf_size)
  3827. {
  3828. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3829. if (table_index != 0) {
  3830. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3831. return false;
  3832. }
  3833. #endif
  3834. if (table_index >= module->import_table_count + module->table_count) {
  3835. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3836. table_index);
  3837. return false;
  3838. }
  3839. return true;
  3840. }
  3841. static bool
  3842. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3843. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3844. {
  3845. const uint8 *p = *p_buf, *p_end = buf_end;
  3846. uint32 table_index;
  3847. read_leb_uint32(p, p_end, table_index);
  3848. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3849. return false;
  3850. }
  3851. *p_table_index = table_index;
  3852. *p_buf = p;
  3853. return true;
  3854. fail:
  3855. return false;
  3856. }
  3857. /* Element segments must match element type of table */
  3858. static bool
  3859. check_table_elem_type(WASMModule *module, uint32 table_index,
  3860. uint32 type_from_elem_seg, char *error_buf,
  3861. uint32 error_buf_size)
  3862. {
  3863. uint32 table_declared_elem_type;
  3864. if (table_index < module->import_table_count)
  3865. table_declared_elem_type =
  3866. module->import_tables[table_index].u.table.table_type.elem_type;
  3867. else
  3868. table_declared_elem_type =
  3869. (module->tables + table_index)->table_type.elem_type;
  3870. if (table_declared_elem_type == type_from_elem_seg)
  3871. return true;
  3872. #if WASM_ENABLE_GC != 0
  3873. /*
  3874. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3875. */
  3876. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3877. return true;
  3878. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3879. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3880. return true;
  3881. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3882. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3883. return true;
  3884. #endif
  3885. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3886. return false;
  3887. }
  3888. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3889. static bool
  3890. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3891. uint32 *p_elem_type,
  3892. #if WASM_ENABLE_GC != 0
  3893. WASMRefType **p_elem_ref_type,
  3894. #endif
  3895. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3896. {
  3897. const uint8 *p = *p_buf, *p_end = buf_end;
  3898. uint8 elem_type;
  3899. #if WASM_ENABLE_GC != 0
  3900. WASMRefType elem_ref_type;
  3901. bool need_ref_type_map;
  3902. #endif
  3903. CHECK_BUF(p, p_end, 1);
  3904. elem_type = read_uint8(p);
  3905. if (elemkind_zero) {
  3906. if (elem_type != 0) {
  3907. set_error_buf(error_buf, error_buf_size,
  3908. "invalid reference type or unknown type");
  3909. return false;
  3910. }
  3911. else {
  3912. *p_elem_type = VALUE_TYPE_FUNCREF;
  3913. *p_buf = p;
  3914. return true;
  3915. }
  3916. }
  3917. #if WASM_ENABLE_GC == 0
  3918. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3919. set_error_buf(error_buf, error_buf_size,
  3920. "invalid reference type or unknown type");
  3921. return false;
  3922. }
  3923. *p_elem_type = elem_type;
  3924. #else
  3925. p--;
  3926. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3927. module->type_count, &need_ref_type_map,
  3928. &elem_ref_type, false, error_buf, error_buf_size)) {
  3929. return false;
  3930. }
  3931. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3932. set_error_buf(error_buf, error_buf_size,
  3933. "invalid reference type or unknown type");
  3934. return false;
  3935. }
  3936. *p_elem_type = elem_ref_type.ref_type;
  3937. if (need_ref_type_map) {
  3938. if (!(*p_elem_ref_type =
  3939. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3940. error_buf, error_buf_size))) {
  3941. return false;
  3942. }
  3943. }
  3944. #endif
  3945. *p_buf = p;
  3946. return true;
  3947. fail:
  3948. return false;
  3949. }
  3950. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3951. static bool
  3952. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3953. WASMModule *module, WASMTableSeg *table_segment,
  3954. char *error_buf, uint32 error_buf_size)
  3955. {
  3956. const uint8 *p = *p_buf, *p_end = buf_end;
  3957. uint32 function_count, function_index = 0, i;
  3958. uint64 total_size;
  3959. read_leb_uint32(p, p_end, function_count);
  3960. table_segment->value_count = function_count;
  3961. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3962. if (total_size > 0
  3963. && !(table_segment->init_values =
  3964. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3965. error_buf_size))) {
  3966. return false;
  3967. }
  3968. for (i = 0; i < function_count; i++) {
  3969. InitializerExpression *init_expr = &table_segment->init_values[i];
  3970. read_leb_uint32(p, p_end, function_index);
  3971. if (!check_function_index(module, function_index, error_buf,
  3972. error_buf_size)) {
  3973. return false;
  3974. }
  3975. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3976. init_expr->u.ref_index = function_index;
  3977. }
  3978. *p_buf = p;
  3979. return true;
  3980. fail:
  3981. return false;
  3982. }
  3983. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3984. static bool
  3985. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3986. WASMModule *module, WASMTableSeg *table_segment,
  3987. char *error_buf, uint32 error_buf_size)
  3988. {
  3989. const uint8 *p = *p_buf, *p_end = buf_end;
  3990. uint32 ref_count, i;
  3991. uint64 total_size;
  3992. read_leb_uint32(p, p_end, ref_count);
  3993. table_segment->value_count = ref_count;
  3994. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3995. if (total_size > 0
  3996. && !(table_segment->init_values =
  3997. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3998. error_buf_size))) {
  3999. return false;
  4000. }
  4001. for (i = 0; i < ref_count; i++) {
  4002. InitializerExpression *init_expr = &table_segment->init_values[i];
  4003. if (!load_init_expr(module, &p, p_end, init_expr,
  4004. table_segment->elem_type,
  4005. #if WASM_ENABLE_GC == 0
  4006. NULL,
  4007. #else
  4008. table_segment->elem_ref_type,
  4009. #endif
  4010. error_buf, error_buf_size))
  4011. return false;
  4012. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4013. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4014. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4015. && init_expr->init_expr_type
  4016. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4017. }
  4018. *p_buf = p;
  4019. return true;
  4020. fail:
  4021. return false;
  4022. }
  4023. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4024. static bool
  4025. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4026. WASMModule *module, char *error_buf,
  4027. uint32 error_buf_size)
  4028. {
  4029. const uint8 *p = buf, *p_end = buf_end;
  4030. uint32 table_segment_count, i;
  4031. uint64 total_size;
  4032. WASMTableSeg *table_segment;
  4033. read_leb_uint32(p, p_end, table_segment_count);
  4034. if (table_segment_count) {
  4035. module->table_seg_count = table_segment_count;
  4036. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4037. if (!(module->table_segments =
  4038. loader_malloc(total_size, error_buf, error_buf_size))) {
  4039. return false;
  4040. }
  4041. table_segment = module->table_segments;
  4042. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4043. if (p >= p_end) {
  4044. set_error_buf(error_buf, error_buf_size,
  4045. "invalid value type or "
  4046. "invalid elements segment kind");
  4047. return false;
  4048. }
  4049. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4050. read_leb_uint32(p, p_end, table_segment->mode);
  4051. /* last three bits */
  4052. table_segment->mode = table_segment->mode & 0x07;
  4053. switch (table_segment->mode) {
  4054. /* elemkind/elemtype + active */
  4055. case 0:
  4056. case 4:
  4057. {
  4058. #if WASM_ENABLE_GC != 0
  4059. if (table_segment->mode == 0) {
  4060. /* vec(funcidx), set elem type to (ref func) */
  4061. WASMRefType elem_ref_type = { 0 };
  4062. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4063. wasm_set_refheaptype_common(
  4064. &elem_ref_type.ref_ht_common, false,
  4065. HEAP_TYPE_FUNC);
  4066. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4067. module->ref_type_set, &elem_ref_type,
  4068. error_buf, error_buf_size)))
  4069. return false;
  4070. }
  4071. else {
  4072. /* vec(expr), set elem type to funcref */
  4073. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4074. }
  4075. #else
  4076. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4077. #endif
  4078. table_segment->table_index = 0;
  4079. if (!check_table_index(module, table_segment->table_index,
  4080. error_buf, error_buf_size))
  4081. return false;
  4082. if (!load_init_expr(
  4083. module, &p, p_end, &table_segment->base_offset,
  4084. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4085. return false;
  4086. if (table_segment->mode == 0) {
  4087. /* vec(funcidx) */
  4088. if (!load_func_index_vec(&p, p_end, module,
  4089. table_segment, error_buf,
  4090. error_buf_size))
  4091. return false;
  4092. }
  4093. else {
  4094. /* vec(expr) */
  4095. if (!load_init_expr_vec(&p, p_end, module,
  4096. table_segment, error_buf,
  4097. error_buf_size))
  4098. return false;
  4099. }
  4100. if (!check_table_elem_type(module,
  4101. table_segment->table_index,
  4102. table_segment->elem_type,
  4103. error_buf, error_buf_size))
  4104. return false;
  4105. break;
  4106. }
  4107. /* elemkind + passive/declarative */
  4108. case 1:
  4109. case 3:
  4110. if (!load_elem_type(module, &p, p_end,
  4111. &table_segment->elem_type,
  4112. #if WASM_ENABLE_GC != 0
  4113. &table_segment->elem_ref_type,
  4114. #endif
  4115. true, error_buf, error_buf_size))
  4116. return false;
  4117. /* vec(funcidx) */
  4118. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4119. error_buf, error_buf_size))
  4120. return false;
  4121. break;
  4122. /* elemkind/elemtype + table_idx + active */
  4123. case 2:
  4124. case 6:
  4125. if (!load_table_index(&p, p_end, module,
  4126. &table_segment->table_index,
  4127. error_buf, error_buf_size))
  4128. return false;
  4129. if (!load_init_expr(
  4130. module, &p, p_end, &table_segment->base_offset,
  4131. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4132. return false;
  4133. if (!load_elem_type(module, &p, p_end,
  4134. &table_segment->elem_type,
  4135. #if WASM_ENABLE_GC != 0
  4136. &table_segment->elem_ref_type,
  4137. #endif
  4138. table_segment->mode == 2 ? true : false,
  4139. error_buf, error_buf_size))
  4140. return false;
  4141. if (table_segment->mode == 2) {
  4142. /* vec(funcidx) */
  4143. if (!load_func_index_vec(&p, p_end, module,
  4144. table_segment, error_buf,
  4145. error_buf_size))
  4146. return false;
  4147. }
  4148. else {
  4149. /* vec(expr) */
  4150. if (!load_init_expr_vec(&p, p_end, module,
  4151. table_segment, error_buf,
  4152. error_buf_size))
  4153. return false;
  4154. }
  4155. if (!check_table_elem_type(module,
  4156. table_segment->table_index,
  4157. table_segment->elem_type,
  4158. error_buf, error_buf_size))
  4159. return false;
  4160. break;
  4161. case 5:
  4162. case 7:
  4163. if (!load_elem_type(module, &p, p_end,
  4164. &table_segment->elem_type,
  4165. #if WASM_ENABLE_GC != 0
  4166. &table_segment->elem_ref_type,
  4167. #endif
  4168. false, error_buf, error_buf_size))
  4169. return false;
  4170. /* vec(expr) */
  4171. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4172. error_buf, error_buf_size))
  4173. return false;
  4174. break;
  4175. default:
  4176. set_error_buf(error_buf, error_buf_size,
  4177. "unknown element segment kind");
  4178. return false;
  4179. }
  4180. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4181. /*
  4182. * like: 00 41 05 0b 04 00 01 00 01
  4183. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4184. */
  4185. if (!load_table_index(&p, p_end, module,
  4186. &table_segment->table_index, error_buf,
  4187. error_buf_size))
  4188. return false;
  4189. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4190. VALUE_TYPE_I32, NULL, error_buf,
  4191. error_buf_size))
  4192. return false;
  4193. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4194. error_buf, error_buf_size))
  4195. return false;
  4196. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4197. if (!check_table_elem_type(module, table_segment->table_index,
  4198. table_segment->elem_type, error_buf,
  4199. error_buf_size))
  4200. return false;
  4201. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4202. #if WASM_ENABLE_WAMR_COMPILER != 0
  4203. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4204. module->is_ref_types_used = true;
  4205. #endif
  4206. }
  4207. }
  4208. if (p != p_end) {
  4209. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4210. return false;
  4211. }
  4212. LOG_VERBOSE("Load table segment section success.\n");
  4213. return true;
  4214. fail:
  4215. return false;
  4216. }
  4217. static bool
  4218. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4219. WASMModule *module,
  4220. #if WASM_ENABLE_BULK_MEMORY != 0
  4221. bool has_datacount_section,
  4222. #endif
  4223. bool clone_data_seg, char *error_buf,
  4224. uint32 error_buf_size)
  4225. {
  4226. const uint8 *p = buf, *p_end = buf_end;
  4227. uint32 data_seg_count, i, mem_index, data_seg_len;
  4228. uint64 total_size;
  4229. WASMDataSeg *dataseg;
  4230. InitializerExpression init_expr;
  4231. #if WASM_ENABLE_BULK_MEMORY != 0
  4232. bool is_passive = false;
  4233. uint32 mem_flag;
  4234. #endif
  4235. uint8 mem_offset_type = VALUE_TYPE_I32;
  4236. read_leb_uint32(p, p_end, data_seg_count);
  4237. #if WASM_ENABLE_BULK_MEMORY != 0
  4238. if (has_datacount_section && data_seg_count != module->data_seg_count1) {
  4239. set_error_buf(error_buf, error_buf_size,
  4240. "data count and data section have inconsistent lengths");
  4241. return false;
  4242. }
  4243. #endif
  4244. if (data_seg_count) {
  4245. module->data_seg_count = data_seg_count;
  4246. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4247. if (!(module->data_segments =
  4248. loader_malloc(total_size, error_buf, error_buf_size))) {
  4249. return false;
  4250. }
  4251. for (i = 0; i < data_seg_count; i++) {
  4252. read_leb_uint32(p, p_end, mem_index);
  4253. #if WASM_ENABLE_BULK_MEMORY != 0
  4254. is_passive = false;
  4255. mem_flag = mem_index & 0x03;
  4256. switch (mem_flag) {
  4257. case 0x01:
  4258. is_passive = true;
  4259. #if WASM_ENABLE_WAMR_COMPILER != 0
  4260. module->is_bulk_memory_used = true;
  4261. #endif
  4262. break;
  4263. case 0x00:
  4264. /* no memory index, treat index as 0 */
  4265. mem_index = 0;
  4266. goto check_mem_index;
  4267. case 0x02:
  4268. /* read following memory index */
  4269. read_leb_uint32(p, p_end, mem_index);
  4270. #if WASM_ENABLE_WAMR_COMPILER != 0
  4271. module->is_bulk_memory_used = true;
  4272. #endif
  4273. check_mem_index:
  4274. if (mem_index
  4275. >= module->import_memory_count + module->memory_count) {
  4276. set_error_buf_v(error_buf, error_buf_size,
  4277. "unknown memory %d", mem_index);
  4278. return false;
  4279. }
  4280. break;
  4281. case 0x03:
  4282. default:
  4283. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4284. return false;
  4285. break;
  4286. }
  4287. #else
  4288. if (mem_index
  4289. >= module->import_memory_count + module->memory_count) {
  4290. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4291. mem_index);
  4292. return false;
  4293. }
  4294. #endif /* WASM_ENABLE_BULK_MEMORY */
  4295. #if WASM_ENABLE_BULK_MEMORY != 0
  4296. if (!is_passive)
  4297. #endif
  4298. {
  4299. #if WASM_ENABLE_MEMORY64 != 0
  4300. /* This memory_flag is from memory instead of data segment */
  4301. uint8 memory_flag;
  4302. if (module->import_memory_count > 0) {
  4303. memory_flag = module->import_memories[mem_index]
  4304. .u.memory.mem_type.flags;
  4305. }
  4306. else {
  4307. memory_flag =
  4308. module
  4309. ->memories[mem_index - module->import_memory_count]
  4310. .flags;
  4311. }
  4312. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4313. : VALUE_TYPE_I32;
  4314. #else
  4315. mem_offset_type = VALUE_TYPE_I32;
  4316. #endif
  4317. }
  4318. #if WASM_ENABLE_BULK_MEMORY != 0
  4319. if (!is_passive)
  4320. #endif
  4321. if (!load_init_expr(module, &p, p_end, &init_expr,
  4322. mem_offset_type, NULL, error_buf,
  4323. error_buf_size))
  4324. return false;
  4325. read_leb_uint32(p, p_end, data_seg_len);
  4326. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4327. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4328. return false;
  4329. }
  4330. #if WASM_ENABLE_BULK_MEMORY != 0
  4331. dataseg->is_passive = is_passive;
  4332. if (!is_passive)
  4333. #endif
  4334. {
  4335. bh_memcpy_s(&dataseg->base_offset,
  4336. sizeof(InitializerExpression), &init_expr,
  4337. sizeof(InitializerExpression));
  4338. dataseg->memory_index = mem_index;
  4339. }
  4340. dataseg->data_length = data_seg_len;
  4341. CHECK_BUF(p, p_end, data_seg_len);
  4342. if (clone_data_seg) {
  4343. if (!(dataseg->data = loader_malloc(
  4344. dataseg->data_length, error_buf, error_buf_size))) {
  4345. return false;
  4346. }
  4347. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4348. data_seg_len);
  4349. }
  4350. else {
  4351. dataseg->data = (uint8 *)p;
  4352. }
  4353. dataseg->is_data_cloned = clone_data_seg;
  4354. p += data_seg_len;
  4355. }
  4356. }
  4357. if (p != p_end) {
  4358. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4359. return false;
  4360. }
  4361. LOG_VERBOSE("Load data segment section success.\n");
  4362. return true;
  4363. fail:
  4364. return false;
  4365. }
  4366. #if WASM_ENABLE_BULK_MEMORY != 0
  4367. static bool
  4368. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4369. WASMModule *module, char *error_buf,
  4370. uint32 error_buf_size)
  4371. {
  4372. const uint8 *p = buf, *p_end = buf_end;
  4373. uint32 data_seg_count1 = 0;
  4374. read_leb_uint32(p, p_end, data_seg_count1);
  4375. module->data_seg_count1 = data_seg_count1;
  4376. if (p != p_end) {
  4377. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4378. return false;
  4379. }
  4380. #if WASM_ENABLE_WAMR_COMPILER != 0
  4381. module->is_bulk_memory_used = true;
  4382. #endif
  4383. LOG_VERBOSE("Load datacount section success.\n");
  4384. return true;
  4385. fail:
  4386. return false;
  4387. }
  4388. #endif
  4389. #if WASM_ENABLE_TAGS != 0
  4390. static bool
  4391. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4392. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4393. uint32 error_buf_size)
  4394. {
  4395. (void)buf_code;
  4396. (void)buf_code_end;
  4397. const uint8 *p = buf, *p_end = buf_end;
  4398. size_t total_size = 0;
  4399. /* number of tags defined in the section */
  4400. uint32 section_tag_count = 0;
  4401. uint8 tag_attribute;
  4402. uint32 tag_type;
  4403. WASMTag *tag = NULL;
  4404. /* get tag count */
  4405. read_leb_uint32(p, p_end, section_tag_count);
  4406. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4407. error_buf, error_buf_size))
  4408. return false;
  4409. module->tag_count = section_tag_count;
  4410. if (section_tag_count) {
  4411. total_size = sizeof(WASMTag *) * module->tag_count;
  4412. if (!(module->tags =
  4413. loader_malloc(total_size, error_buf, error_buf_size))) {
  4414. return false;
  4415. }
  4416. /* load each tag, imported tags precede the tags */
  4417. uint32 tag_index;
  4418. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4419. /* get the one byte attribute */
  4420. CHECK_BUF(p, p_end, 1);
  4421. tag_attribute = read_uint8(p);
  4422. /* get type */
  4423. read_leb_uint32(p, p_end, tag_type);
  4424. /* compare against module->types */
  4425. if (tag_type >= module->type_count) {
  4426. set_error_buf(error_buf, error_buf_size, "unknown type");
  4427. return false;
  4428. }
  4429. /* get return type (must be 0) */
  4430. /* check, that the type of the referred tag returns void */
  4431. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4432. if (func_type->result_count != 0) {
  4433. set_error_buf(error_buf, error_buf_size,
  4434. "non-empty tag result type");
  4435. goto fail;
  4436. }
  4437. if (!(tag = module->tags[tag_index] = loader_malloc(
  4438. sizeof(WASMTag), error_buf, error_buf_size))) {
  4439. return false;
  4440. }
  4441. /* store to module tag declarations */
  4442. tag->attribute = tag_attribute;
  4443. tag->type = tag_type;
  4444. tag->tag_type = func_type;
  4445. }
  4446. }
  4447. if (p != p_end) {
  4448. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4449. return false;
  4450. }
  4451. LOG_VERBOSE("Load tag section success.\n");
  4452. return true;
  4453. fail:
  4454. return false;
  4455. }
  4456. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4457. static bool
  4458. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4459. const uint8 *buf_func_end, WASMModule *module,
  4460. char *error_buf, uint32 error_buf_size)
  4461. {
  4462. const uint8 *p = buf, *p_end = buf_end;
  4463. const uint8 *p_func = buf_func;
  4464. uint32 func_count = 0, code_count;
  4465. /* code has been loaded in function section, so pass it here, just check
  4466. * whether function and code section have inconsistent lengths */
  4467. read_leb_uint32(p, p_end, code_count);
  4468. if (buf_func)
  4469. read_leb_uint32(p_func, buf_func_end, func_count);
  4470. if (func_count != code_count) {
  4471. set_error_buf(error_buf, error_buf_size,
  4472. "function and code section have inconsistent lengths");
  4473. return false;
  4474. }
  4475. LOG_VERBOSE("Load code segment section success.\n");
  4476. (void)module;
  4477. return true;
  4478. fail:
  4479. return false;
  4480. }
  4481. static bool
  4482. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4483. char *error_buf, uint32 error_buf_size)
  4484. {
  4485. const uint8 *p = buf, *p_end = buf_end;
  4486. WASMFuncType *type;
  4487. uint32 start_function;
  4488. read_leb_uint32(p, p_end, start_function);
  4489. if (start_function
  4490. >= module->function_count + module->import_function_count) {
  4491. set_error_buf(error_buf, error_buf_size, "unknown function");
  4492. return false;
  4493. }
  4494. if (start_function < module->import_function_count)
  4495. type = module->import_functions[start_function].u.function.func_type;
  4496. else
  4497. type = module->functions[start_function - module->import_function_count]
  4498. ->func_type;
  4499. if (type->param_count != 0 || type->result_count != 0) {
  4500. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4501. return false;
  4502. }
  4503. module->start_function = start_function;
  4504. if (p != p_end) {
  4505. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4506. return false;
  4507. }
  4508. LOG_VERBOSE("Load start section success.\n");
  4509. return true;
  4510. fail:
  4511. return false;
  4512. }
  4513. #if WASM_ENABLE_STRINGREF != 0
  4514. static bool
  4515. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4516. WASMModule *module, bool is_load_from_file_buf,
  4517. char *error_buf, uint32 error_buf_size)
  4518. {
  4519. const uint8 *p = buf, *p_end = buf_end;
  4520. int32 deferred_count, immediate_count, string_length, i;
  4521. uint64 total_size;
  4522. read_leb_uint32(p, p_end, deferred_count);
  4523. read_leb_uint32(p, p_end, immediate_count);
  4524. /* proposal set deferred_count for future extension */
  4525. if (deferred_count != 0) {
  4526. goto fail;
  4527. }
  4528. if (immediate_count > 0) {
  4529. total_size = sizeof(char *) * (uint64)immediate_count;
  4530. if (!(module->string_literal_ptrs =
  4531. loader_malloc(total_size, error_buf, error_buf_size))) {
  4532. goto fail;
  4533. }
  4534. module->string_literal_count = immediate_count;
  4535. total_size = sizeof(uint32) * (uint64)immediate_count;
  4536. if (!(module->string_literal_lengths =
  4537. loader_malloc(total_size, error_buf, error_buf_size))) {
  4538. goto fail;
  4539. }
  4540. for (i = 0; i < immediate_count; i++) {
  4541. read_leb_uint32(p, p_end, string_length);
  4542. CHECK_BUF(p, p_end, string_length);
  4543. module->string_literal_ptrs[i] = p;
  4544. module->string_literal_lengths[i] = string_length;
  4545. p += string_length;
  4546. }
  4547. }
  4548. if (p != p_end) {
  4549. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4550. goto fail;
  4551. }
  4552. LOG_VERBOSE("Load stringref section success.\n");
  4553. return true;
  4554. fail:
  4555. return false;
  4556. }
  4557. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4558. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4559. static bool
  4560. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4561. bool is_load_from_file_buf, char *error_buf,
  4562. uint32 error_buf_size)
  4563. {
  4564. const uint8 *p = buf, *p_end = buf_end;
  4565. uint32 name_type, subsection_size;
  4566. uint32 previous_name_type = 0;
  4567. uint32 num_func_name;
  4568. uint32 func_index;
  4569. uint32 previous_func_index = ~0U;
  4570. uint32 func_name_len;
  4571. uint32 name_index;
  4572. int i = 0;
  4573. if (p >= p_end) {
  4574. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4575. return false;
  4576. }
  4577. while (p < p_end) {
  4578. read_leb_uint32(p, p_end, name_type);
  4579. if (i != 0) {
  4580. if (name_type == previous_name_type) {
  4581. set_error_buf(error_buf, error_buf_size,
  4582. "duplicate sub-section");
  4583. return false;
  4584. }
  4585. if (name_type < previous_name_type) {
  4586. set_error_buf(error_buf, error_buf_size,
  4587. "out-of-order sub-section");
  4588. return false;
  4589. }
  4590. }
  4591. previous_name_type = name_type;
  4592. read_leb_uint32(p, p_end, subsection_size);
  4593. CHECK_BUF(p, p_end, subsection_size);
  4594. switch (name_type) {
  4595. case SUB_SECTION_TYPE_FUNC:
  4596. if (subsection_size) {
  4597. read_leb_uint32(p, p_end, num_func_name);
  4598. for (name_index = 0; name_index < num_func_name;
  4599. name_index++) {
  4600. read_leb_uint32(p, p_end, func_index);
  4601. if (func_index == previous_func_index) {
  4602. set_error_buf(error_buf, error_buf_size,
  4603. "duplicate function name");
  4604. return false;
  4605. }
  4606. if (func_index < previous_func_index
  4607. && previous_func_index != ~0U) {
  4608. set_error_buf(error_buf, error_buf_size,
  4609. "out-of-order function index ");
  4610. return false;
  4611. }
  4612. previous_func_index = func_index;
  4613. read_leb_uint32(p, p_end, func_name_len);
  4614. CHECK_BUF(p, p_end, func_name_len);
  4615. /* Skip the import functions */
  4616. if (func_index >= module->import_function_count) {
  4617. func_index -= module->import_function_count;
  4618. if (func_index >= module->function_count) {
  4619. set_error_buf(error_buf, error_buf_size,
  4620. "out-of-range function index");
  4621. return false;
  4622. }
  4623. if (!(module->functions[func_index]->field_name =
  4624. wasm_const_str_list_insert(
  4625. p, func_name_len, module,
  4626. #if WASM_ENABLE_WAMR_COMPILER != 0
  4627. false,
  4628. #else
  4629. is_load_from_file_buf,
  4630. #endif
  4631. error_buf, error_buf_size))) {
  4632. return false;
  4633. }
  4634. }
  4635. p += func_name_len;
  4636. }
  4637. }
  4638. break;
  4639. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4640. */
  4641. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4642. default:
  4643. p = p + subsection_size;
  4644. break;
  4645. }
  4646. i++;
  4647. }
  4648. return true;
  4649. fail:
  4650. return false;
  4651. }
  4652. #endif
  4653. static bool
  4654. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4655. bool is_load_from_file_buf, char *error_buf,
  4656. uint32 error_buf_size)
  4657. {
  4658. const uint8 *p = buf, *p_end = buf_end;
  4659. char section_name[32];
  4660. uint32 name_len, buffer_len;
  4661. if (p >= p_end) {
  4662. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4663. return false;
  4664. }
  4665. read_leb_uint32(p, p_end, name_len);
  4666. if (p + name_len > p_end) {
  4667. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4668. return false;
  4669. }
  4670. if (!wasm_check_utf8_str(p, name_len)) {
  4671. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4672. return false;
  4673. }
  4674. buffer_len = sizeof(section_name);
  4675. memset(section_name, 0, buffer_len);
  4676. if (name_len < buffer_len) {
  4677. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4678. }
  4679. else {
  4680. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4681. memset(section_name + buffer_len - 4, '.', 3);
  4682. }
  4683. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4684. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4685. module->name_section_buf = buf;
  4686. module->name_section_buf_end = buf_end;
  4687. p += name_len;
  4688. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4689. error_buf, error_buf_size)) {
  4690. return false;
  4691. }
  4692. LOG_VERBOSE("Load custom name section success.");
  4693. }
  4694. #endif
  4695. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4696. {
  4697. WASMCustomSection *section =
  4698. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4699. if (!section) {
  4700. return false;
  4701. }
  4702. section->name_addr = (char *)p;
  4703. section->name_len = name_len;
  4704. section->content_addr = (uint8 *)(p + name_len);
  4705. section->content_len = (uint32)(p_end - p - name_len);
  4706. section->next = module->custom_section_list;
  4707. module->custom_section_list = section;
  4708. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4709. return true;
  4710. }
  4711. #endif
  4712. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4713. (void)is_load_from_file_buf;
  4714. (void)module;
  4715. return true;
  4716. fail:
  4717. return false;
  4718. }
  4719. static void
  4720. calculate_global_data_offset(WASMModule *module)
  4721. {
  4722. uint32 i, data_offset;
  4723. data_offset = 0;
  4724. for (i = 0; i < module->import_global_count; i++) {
  4725. WASMGlobalImport *import_global =
  4726. &((module->import_globals + i)->u.global);
  4727. #if WASM_ENABLE_FAST_JIT != 0
  4728. import_global->data_offset = data_offset;
  4729. #endif
  4730. data_offset += wasm_value_type_size(import_global->type.val_type);
  4731. }
  4732. for (i = 0; i < module->global_count; i++) {
  4733. WASMGlobal *global = module->globals + i;
  4734. #if WASM_ENABLE_FAST_JIT != 0
  4735. global->data_offset = data_offset;
  4736. #endif
  4737. data_offset += wasm_value_type_size(global->type.val_type);
  4738. }
  4739. module->global_data_size = data_offset;
  4740. }
  4741. #if WASM_ENABLE_FAST_JIT != 0
  4742. static bool
  4743. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4744. uint32 error_buf_size)
  4745. {
  4746. #if WASM_ENABLE_LAZY_JIT != 0
  4747. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4748. #endif
  4749. uint32 i;
  4750. if (!module->function_count)
  4751. return true;
  4752. if (!(module->fast_jit_func_ptrs =
  4753. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4754. error_buf_size))) {
  4755. return false;
  4756. }
  4757. #if WASM_ENABLE_LAZY_JIT != 0
  4758. for (i = 0; i < module->function_count; i++) {
  4759. module->fast_jit_func_ptrs[i] =
  4760. jit_globals->compile_fast_jit_and_then_call;
  4761. }
  4762. #endif
  4763. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4764. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4765. set_error_buf(error_buf, error_buf_size,
  4766. "init fast jit thread lock failed");
  4767. return false;
  4768. }
  4769. module->fast_jit_thread_locks_inited[i] = true;
  4770. }
  4771. return true;
  4772. }
  4773. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4774. #if WASM_ENABLE_JIT != 0
  4775. static bool
  4776. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4777. uint32 error_buf_size)
  4778. {
  4779. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4780. AOTCompOption option = { 0 };
  4781. char *aot_last_error;
  4782. uint64 size;
  4783. #if WASM_ENABLE_GC != 0
  4784. bool gc_enabled = true;
  4785. #else
  4786. bool gc_enabled = false;
  4787. #endif
  4788. if (module->function_count == 0)
  4789. return true;
  4790. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4791. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4792. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4793. return false;
  4794. }
  4795. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4796. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4797. os_mutex_destroy(&module->tierup_wait_lock);
  4798. return false;
  4799. }
  4800. module->tierup_wait_lock_inited = true;
  4801. #endif
  4802. size = sizeof(void *) * (uint64)module->function_count
  4803. + sizeof(bool) * (uint64)module->function_count;
  4804. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4805. return false;
  4806. }
  4807. module->func_ptrs_compiled =
  4808. (bool *)((uint8 *)module->func_ptrs
  4809. + sizeof(void *) * module->function_count);
  4810. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4811. if (!module->comp_data) {
  4812. aot_last_error = aot_get_last_error();
  4813. bh_assert(aot_last_error != NULL);
  4814. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4815. return false;
  4816. }
  4817. option.is_jit_mode = true;
  4818. option.opt_level = llvm_jit_options->opt_level;
  4819. option.size_level = llvm_jit_options->size_level;
  4820. option.segue_flags = llvm_jit_options->segue_flags;
  4821. option.quick_invoke_c_api_import =
  4822. llvm_jit_options->quick_invoke_c_api_import;
  4823. #if WASM_ENABLE_BULK_MEMORY != 0
  4824. option.enable_bulk_memory = true;
  4825. #endif
  4826. #if WASM_ENABLE_THREAD_MGR != 0
  4827. option.enable_thread_mgr = true;
  4828. #endif
  4829. #if WASM_ENABLE_TAIL_CALL != 0
  4830. option.enable_tail_call = true;
  4831. #endif
  4832. #if WASM_ENABLE_SIMD != 0
  4833. option.enable_simd = true;
  4834. #endif
  4835. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4836. option.enable_ref_types = true;
  4837. #elif WASM_ENABLE_GC != 0
  4838. option.enable_gc = true;
  4839. #endif
  4840. option.enable_aux_stack_check = true;
  4841. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4842. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4843. option.enable_aux_stack_frame = true;
  4844. #endif
  4845. #if WASM_ENABLE_PERF_PROFILING != 0
  4846. option.enable_perf_profiling = true;
  4847. #endif
  4848. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4849. option.enable_memory_profiling = true;
  4850. option.enable_stack_estimation = true;
  4851. #endif
  4852. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4853. if (!module->comp_ctx) {
  4854. aot_last_error = aot_get_last_error();
  4855. bh_assert(aot_last_error != NULL);
  4856. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4857. return false;
  4858. }
  4859. return true;
  4860. }
  4861. static bool
  4862. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4863. uint32 error_buf_size)
  4864. {
  4865. char *aot_last_error;
  4866. uint32 i;
  4867. if (module->function_count == 0)
  4868. return true;
  4869. if (!aot_compile_wasm(module->comp_ctx)) {
  4870. aot_last_error = aot_get_last_error();
  4871. bh_assert(aot_last_error != NULL);
  4872. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4873. return false;
  4874. }
  4875. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4876. if (module->orcjit_stop_compiling)
  4877. return false;
  4878. #endif
  4879. bh_print_time("Begin to lookup llvm jit functions");
  4880. for (i = 0; i < module->function_count; i++) {
  4881. LLVMOrcJITTargetAddress func_addr = 0;
  4882. LLVMErrorRef error;
  4883. char func_name[48];
  4884. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4885. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4886. func_name);
  4887. if (error != LLVMErrorSuccess) {
  4888. char *err_msg = LLVMGetErrorMessage(error);
  4889. set_error_buf_v(error_buf, error_buf_size,
  4890. "failed to compile llvm jit function: %s", err_msg);
  4891. LLVMDisposeErrorMessage(err_msg);
  4892. return false;
  4893. }
  4894. /**
  4895. * No need to lock the func_ptr[func_idx] here as it is basic
  4896. * data type, the load/store for it can be finished by one cpu
  4897. * instruction, and there can be only one cpu instruction
  4898. * loading/storing at the same time.
  4899. */
  4900. module->func_ptrs[i] = (void *)func_addr;
  4901. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4902. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4903. if (module->orcjit_stop_compiling)
  4904. return false;
  4905. #endif
  4906. }
  4907. bh_print_time("End lookup llvm jit functions");
  4908. return true;
  4909. }
  4910. #endif /* end of WASM_ENABLE_JIT != 0 */
  4911. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4912. && WASM_ENABLE_LAZY_JIT != 0
  4913. static void *
  4914. init_llvm_jit_functions_stage2_callback(void *arg)
  4915. {
  4916. WASMModule *module = (WASMModule *)arg;
  4917. char error_buf[128];
  4918. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4919. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4920. module->orcjit_stop_compiling = true;
  4921. return NULL;
  4922. }
  4923. os_mutex_lock(&module->tierup_wait_lock);
  4924. module->llvm_jit_inited = true;
  4925. os_cond_broadcast(&module->tierup_wait_cond);
  4926. os_mutex_unlock(&module->tierup_wait_lock);
  4927. return NULL;
  4928. }
  4929. #endif
  4930. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4931. /* The callback function to compile jit functions */
  4932. static void *
  4933. orcjit_thread_callback(void *arg)
  4934. {
  4935. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4936. #if WASM_ENABLE_JIT != 0
  4937. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4938. #endif
  4939. WASMModule *module = thread_arg->module;
  4940. uint32 group_idx = thread_arg->group_idx;
  4941. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4942. uint32 func_count = module->function_count;
  4943. uint32 i;
  4944. #if WASM_ENABLE_FAST_JIT != 0
  4945. /* Compile fast jit functions of this group */
  4946. for (i = group_idx; i < func_count; i += group_stride) {
  4947. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4948. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4949. break;
  4950. }
  4951. if (module->orcjit_stop_compiling) {
  4952. return NULL;
  4953. }
  4954. }
  4955. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4956. os_mutex_lock(&module->tierup_wait_lock);
  4957. module->fast_jit_ready_groups++;
  4958. os_mutex_unlock(&module->tierup_wait_lock);
  4959. #endif
  4960. #endif
  4961. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4962. && WASM_ENABLE_LAZY_JIT != 0
  4963. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4964. for (i = group_idx; i < func_count;
  4965. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4966. uint32 j;
  4967. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4968. if (i + j * group_stride < func_count) {
  4969. if (!jit_compiler_set_call_to_fast_jit(
  4970. module,
  4971. i + j * group_stride + module->import_function_count)) {
  4972. LOG_ERROR(
  4973. "failed to compile call_to_fast_jit for func %u\n",
  4974. i + j * group_stride + module->import_function_count);
  4975. module->orcjit_stop_compiling = true;
  4976. return NULL;
  4977. }
  4978. }
  4979. if (module->orcjit_stop_compiling) {
  4980. return NULL;
  4981. }
  4982. }
  4983. }
  4984. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4985. fast jit functions are compiled */
  4986. os_mutex_lock(&module->tierup_wait_lock);
  4987. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4988. && module->fast_jit_ready_groups >= group_stride)) {
  4989. os_cond_reltimedwait(&module->tierup_wait_cond,
  4990. &module->tierup_wait_lock, 10000);
  4991. if (module->orcjit_stop_compiling) {
  4992. /* init_llvm_jit_functions_stage2 failed */
  4993. os_mutex_unlock(&module->tierup_wait_lock);
  4994. return NULL;
  4995. }
  4996. }
  4997. os_mutex_unlock(&module->tierup_wait_lock);
  4998. #endif
  4999. #if WASM_ENABLE_JIT != 0
  5000. /* Compile llvm jit functions of this group */
  5001. for (i = group_idx; i < func_count;
  5002. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5003. LLVMOrcJITTargetAddress func_addr = 0;
  5004. LLVMErrorRef error;
  5005. char func_name[48];
  5006. typedef void (*F)(void);
  5007. union {
  5008. F f;
  5009. void *v;
  5010. } u;
  5011. uint32 j;
  5012. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5013. "_wrapper");
  5014. LOG_DEBUG("compile llvm jit func %s", func_name);
  5015. error =
  5016. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5017. if (error != LLVMErrorSuccess) {
  5018. char *err_msg = LLVMGetErrorMessage(error);
  5019. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5020. LLVMDisposeErrorMessage(err_msg);
  5021. break;
  5022. }
  5023. /* Call the jit wrapper function to trigger its compilation, so as
  5024. to compile the actual jit functions, since we add the latter to
  5025. function list in the PartitionFunction callback */
  5026. u.v = (void *)func_addr;
  5027. u.f();
  5028. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5029. if (i + j * group_stride < func_count) {
  5030. module->func_ptrs_compiled[i + j * group_stride] = true;
  5031. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5032. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5033. i + j * group_stride);
  5034. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5035. func_name);
  5036. if (error != LLVMErrorSuccess) {
  5037. char *err_msg = LLVMGetErrorMessage(error);
  5038. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5039. err_msg);
  5040. LLVMDisposeErrorMessage(err_msg);
  5041. /* Ignore current llvm jit func, as its func ptr is
  5042. previous set to call_to_fast_jit, which also works */
  5043. continue;
  5044. }
  5045. jit_compiler_set_llvm_jit_func_ptr(
  5046. module,
  5047. i + j * group_stride + module->import_function_count,
  5048. (void *)func_addr);
  5049. /* Try to switch to call this llvm jit function instead of
  5050. fast jit function from fast jit jitted code */
  5051. jit_compiler_set_call_to_llvm_jit(
  5052. module,
  5053. i + j * group_stride + module->import_function_count);
  5054. #endif
  5055. }
  5056. }
  5057. if (module->orcjit_stop_compiling) {
  5058. break;
  5059. }
  5060. }
  5061. #endif
  5062. return NULL;
  5063. }
  5064. static void
  5065. orcjit_stop_compile_threads(WASMModule *module)
  5066. {
  5067. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5068. / sizeof(OrcJitThreadArg));
  5069. module->orcjit_stop_compiling = true;
  5070. for (i = 0; i < thread_num; i++) {
  5071. if (module->orcjit_threads[i])
  5072. os_thread_join(module->orcjit_threads[i], NULL);
  5073. }
  5074. }
  5075. static bool
  5076. compile_jit_functions(WASMModule *module, char *error_buf,
  5077. uint32 error_buf_size)
  5078. {
  5079. uint32 thread_num =
  5080. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5081. uint32 i, j;
  5082. bh_print_time("Begin to compile jit functions");
  5083. /* Create threads to compile the jit functions */
  5084. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5085. #if WASM_ENABLE_JIT != 0
  5086. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5087. #endif
  5088. module->orcjit_thread_args[i].module = module;
  5089. module->orcjit_thread_args[i].group_idx = i;
  5090. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5091. (void *)&module->orcjit_thread_args[i],
  5092. APP_THREAD_STACK_SIZE_DEFAULT)
  5093. != 0) {
  5094. set_error_buf(error_buf, error_buf_size,
  5095. "create orcjit compile thread failed");
  5096. /* Terminate the threads created */
  5097. module->orcjit_stop_compiling = true;
  5098. for (j = 0; j < i; j++) {
  5099. os_thread_join(module->orcjit_threads[j], NULL);
  5100. }
  5101. return false;
  5102. }
  5103. }
  5104. #if WASM_ENABLE_LAZY_JIT == 0
  5105. /* Wait until all jit functions are compiled for eager mode */
  5106. for (i = 0; i < thread_num; i++) {
  5107. if (module->orcjit_threads[i])
  5108. os_thread_join(module->orcjit_threads[i], NULL);
  5109. }
  5110. #if WASM_ENABLE_FAST_JIT != 0
  5111. /* Ensure all the fast-jit functions are compiled */
  5112. for (i = 0; i < module->function_count; i++) {
  5113. if (!jit_compiler_is_compiled(module,
  5114. i + module->import_function_count)) {
  5115. set_error_buf(error_buf, error_buf_size,
  5116. "failed to compile fast jit function");
  5117. return false;
  5118. }
  5119. }
  5120. #endif
  5121. #if WASM_ENABLE_JIT != 0
  5122. /* Ensure all the llvm-jit functions are compiled */
  5123. for (i = 0; i < module->function_count; i++) {
  5124. if (!module->func_ptrs_compiled[i]) {
  5125. set_error_buf(error_buf, error_buf_size,
  5126. "failed to compile llvm jit function");
  5127. return false;
  5128. }
  5129. }
  5130. #endif
  5131. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5132. bh_print_time("End compile jit functions");
  5133. return true;
  5134. }
  5135. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5136. static bool
  5137. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5138. uint32 cur_func_idx, char *error_buf,
  5139. uint32 error_buf_size);
  5140. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5141. void **
  5142. wasm_interp_get_handle_table();
  5143. static void **handle_table;
  5144. #endif
  5145. static bool
  5146. load_from_sections(WASMModule *module, WASMSection *sections,
  5147. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5148. char *error_buf, uint32 error_buf_size)
  5149. {
  5150. WASMExport *export;
  5151. WASMSection *section = sections;
  5152. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5153. *buf_func = NULL, *buf_func_end = NULL;
  5154. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5155. WASMGlobal *aux_stack_top_global = NULL, *global;
  5156. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5157. aux_stack_top = (uint64)-1LL;
  5158. uint32 global_index, func_index, i;
  5159. uint32 aux_data_end_global_index = (uint32)-1;
  5160. uint32 aux_heap_base_global_index = (uint32)-1;
  5161. WASMFuncType *func_type;
  5162. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5163. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5164. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5165. #if WASM_ENABLE_BULK_MEMORY != 0
  5166. bool has_datacount_section = false;
  5167. #endif
  5168. /* Find code and function sections if have */
  5169. while (section) {
  5170. if (section->section_type == SECTION_TYPE_CODE) {
  5171. buf_code = section->section_body;
  5172. buf_code_end = buf_code + section->section_body_size;
  5173. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5174. module->buf_code = (uint8 *)buf_code;
  5175. module->buf_code_size = section->section_body_size;
  5176. #endif
  5177. }
  5178. else if (section->section_type == SECTION_TYPE_FUNC) {
  5179. buf_func = section->section_body;
  5180. buf_func_end = buf_func + section->section_body_size;
  5181. }
  5182. section = section->next;
  5183. }
  5184. section = sections;
  5185. while (section) {
  5186. buf = section->section_body;
  5187. buf_end = buf + section->section_body_size;
  5188. switch (section->section_type) {
  5189. case SECTION_TYPE_USER:
  5190. /* unsupported user section, ignore it. */
  5191. if (!load_user_section(buf, buf_end, module,
  5192. reuse_const_strings, error_buf,
  5193. error_buf_size))
  5194. return false;
  5195. break;
  5196. case SECTION_TYPE_TYPE:
  5197. if (!load_type_section(buf, buf_end, module, error_buf,
  5198. error_buf_size))
  5199. return false;
  5200. break;
  5201. case SECTION_TYPE_IMPORT:
  5202. if (!load_import_section(buf, buf_end, module,
  5203. reuse_const_strings, error_buf,
  5204. error_buf_size))
  5205. return false;
  5206. break;
  5207. case SECTION_TYPE_FUNC:
  5208. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5209. module, error_buf, error_buf_size))
  5210. return false;
  5211. break;
  5212. case SECTION_TYPE_TABLE:
  5213. if (!load_table_section(buf, buf_end, module, error_buf,
  5214. error_buf_size))
  5215. return false;
  5216. break;
  5217. case SECTION_TYPE_MEMORY:
  5218. if (!load_memory_section(buf, buf_end, module, error_buf,
  5219. error_buf_size))
  5220. return false;
  5221. break;
  5222. #if WASM_ENABLE_TAGS != 0
  5223. case SECTION_TYPE_TAG:
  5224. /* load tag declaration section */
  5225. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5226. module, error_buf, error_buf_size))
  5227. return false;
  5228. break;
  5229. #endif
  5230. case SECTION_TYPE_GLOBAL:
  5231. if (!load_global_section(buf, buf_end, module, error_buf,
  5232. error_buf_size))
  5233. return false;
  5234. break;
  5235. case SECTION_TYPE_EXPORT:
  5236. if (!load_export_section(buf, buf_end, module,
  5237. reuse_const_strings, error_buf,
  5238. error_buf_size))
  5239. return false;
  5240. break;
  5241. case SECTION_TYPE_START:
  5242. if (!load_start_section(buf, buf_end, module, error_buf,
  5243. error_buf_size))
  5244. return false;
  5245. break;
  5246. case SECTION_TYPE_ELEM:
  5247. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5248. error_buf_size))
  5249. return false;
  5250. break;
  5251. case SECTION_TYPE_CODE:
  5252. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5253. module, error_buf, error_buf_size))
  5254. return false;
  5255. break;
  5256. case SECTION_TYPE_DATA:
  5257. if (!load_data_segment_section(buf, buf_end, module,
  5258. #if WASM_ENABLE_BULK_MEMORY != 0
  5259. has_datacount_section,
  5260. #endif
  5261. clone_data_seg, error_buf,
  5262. error_buf_size))
  5263. return false;
  5264. break;
  5265. #if WASM_ENABLE_BULK_MEMORY != 0
  5266. case SECTION_TYPE_DATACOUNT:
  5267. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5268. error_buf_size))
  5269. return false;
  5270. has_datacount_section = true;
  5271. break;
  5272. #endif
  5273. #if WASM_ENABLE_STRINGREF != 0
  5274. case SECTION_TYPE_STRINGREF:
  5275. if (!load_stringref_section(buf, buf_end, module,
  5276. reuse_const_strings, error_buf,
  5277. error_buf_size))
  5278. return false;
  5279. break;
  5280. #endif
  5281. default:
  5282. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5283. return false;
  5284. }
  5285. section = section->next;
  5286. }
  5287. module->aux_data_end_global_index = (uint32)-1;
  5288. module->aux_heap_base_global_index = (uint32)-1;
  5289. module->aux_stack_top_global_index = (uint32)-1;
  5290. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5291. export = module->exports;
  5292. for (i = 0; i < module->export_count; i++, export ++) {
  5293. if (export->kind == EXPORT_KIND_GLOBAL) {
  5294. if (!strcmp(export->name, "__heap_base")) {
  5295. global_index = export->index - module->import_global_count;
  5296. global = module->globals + global_index;
  5297. if (global->type.val_type == VALUE_TYPE_I32
  5298. && !global->type.is_mutable
  5299. && global->init_expr.init_expr_type
  5300. == INIT_EXPR_TYPE_I32_CONST) {
  5301. aux_heap_base_global = global;
  5302. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5303. aux_heap_base_global_index = export->index;
  5304. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5305. aux_heap_base);
  5306. }
  5307. }
  5308. else if (!strcmp(export->name, "__data_end")) {
  5309. global_index = export->index - module->import_global_count;
  5310. global = module->globals + global_index;
  5311. if (global->type.val_type == VALUE_TYPE_I32
  5312. && !global->type.is_mutable
  5313. && global->init_expr.init_expr_type
  5314. == INIT_EXPR_TYPE_I32_CONST) {
  5315. aux_data_end_global = global;
  5316. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5317. aux_data_end_global_index = export->index;
  5318. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5319. aux_data_end);
  5320. aux_data_end = align_uint64(aux_data_end, 16);
  5321. }
  5322. }
  5323. /* For module compiled with -pthread option, the global is:
  5324. [0] stack_top <-- 0
  5325. [1] tls_pointer
  5326. [2] tls_size
  5327. [3] data_end <-- 3
  5328. [4] global_base
  5329. [5] heap_base <-- 5
  5330. [6] dso_handle
  5331. For module compiled without -pthread option:
  5332. [0] stack_top <-- 0
  5333. [1] data_end <-- 1
  5334. [2] global_base
  5335. [3] heap_base <-- 3
  5336. [4] dso_handle
  5337. */
  5338. if (aux_data_end_global && aux_heap_base_global
  5339. && aux_data_end <= aux_heap_base) {
  5340. module->aux_data_end_global_index = aux_data_end_global_index;
  5341. module->aux_data_end = aux_data_end;
  5342. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5343. module->aux_heap_base = aux_heap_base;
  5344. /* Resolve aux stack top global */
  5345. for (global_index = 0; global_index < module->global_count;
  5346. global_index++) {
  5347. global = module->globals + global_index;
  5348. if (global->type.is_mutable /* heap_base and data_end is
  5349. not mutable */
  5350. && global->type.val_type == VALUE_TYPE_I32
  5351. && global->init_expr.init_expr_type
  5352. == INIT_EXPR_TYPE_I32_CONST
  5353. && (uint64)(uint32)global->init_expr.u.i32
  5354. <= aux_heap_base) {
  5355. aux_stack_top_global = global;
  5356. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5357. module->aux_stack_top_global_index =
  5358. module->import_global_count + global_index;
  5359. module->aux_stack_bottom = aux_stack_top;
  5360. module->aux_stack_size =
  5361. aux_stack_top > aux_data_end
  5362. ? (uint32)(aux_stack_top - aux_data_end)
  5363. : (uint32)aux_stack_top;
  5364. LOG_VERBOSE(
  5365. "Found aux stack top global, value: %" PRIu64 ", "
  5366. "global index: %d, stack size: %d",
  5367. aux_stack_top, global_index,
  5368. module->aux_stack_size);
  5369. break;
  5370. }
  5371. }
  5372. if (!aux_stack_top_global) {
  5373. /* Auxiliary stack global isn't found, it must be unused
  5374. in the wasm app, as if it is used, the global must be
  5375. defined. Here we set it to __heap_base global and set
  5376. its size to 0. */
  5377. aux_stack_top_global = aux_heap_base_global;
  5378. aux_stack_top = aux_heap_base;
  5379. module->aux_stack_top_global_index =
  5380. module->aux_heap_base_global_index;
  5381. module->aux_stack_bottom = aux_stack_top;
  5382. module->aux_stack_size = 0;
  5383. }
  5384. break;
  5385. }
  5386. }
  5387. }
  5388. module->malloc_function = (uint32)-1;
  5389. module->free_function = (uint32)-1;
  5390. module->retain_function = (uint32)-1;
  5391. /* Resolve malloc/free function exported by wasm module */
  5392. #if WASM_ENABLE_MEMORY64 != 0
  5393. if (has_module_memory64(module))
  5394. malloc_free_io_type = VALUE_TYPE_I64;
  5395. #endif
  5396. export = module->exports;
  5397. for (i = 0; i < module->export_count; i++, export ++) {
  5398. if (export->kind == EXPORT_KIND_FUNC) {
  5399. if (!strcmp(export->name, "malloc")
  5400. && export->index >= module->import_function_count) {
  5401. func_index = export->index - module->import_function_count;
  5402. func_type = module->functions[func_index]->func_type;
  5403. if (func_type->param_count == 1 && func_type->result_count == 1
  5404. && func_type->types[0] == malloc_free_io_type
  5405. && func_type->types[1] == malloc_free_io_type) {
  5406. bh_assert(module->malloc_function == (uint32)-1);
  5407. module->malloc_function = export->index;
  5408. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5409. export->name, export->index);
  5410. }
  5411. }
  5412. else if (!strcmp(export->name, "__new")
  5413. && export->index >= module->import_function_count) {
  5414. /* __new && __pin for AssemblyScript */
  5415. func_index = export->index - module->import_function_count;
  5416. func_type = module->functions[func_index]->func_type;
  5417. if (func_type->param_count == 2 && func_type->result_count == 1
  5418. && func_type->types[0] == malloc_free_io_type
  5419. && func_type->types[1] == VALUE_TYPE_I32
  5420. && func_type->types[2] == malloc_free_io_type) {
  5421. uint32 j;
  5422. WASMExport *export_tmp;
  5423. bh_assert(module->malloc_function == (uint32)-1);
  5424. module->malloc_function = export->index;
  5425. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5426. export->name, export->index);
  5427. /* resolve retain function.
  5428. If not found, reset malloc function index */
  5429. export_tmp = module->exports;
  5430. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5431. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5432. && (!strcmp(export_tmp->name, "__retain")
  5433. || (!strcmp(export_tmp->name, "__pin")))
  5434. && (export_tmp->index
  5435. >= module->import_function_count)) {
  5436. func_index = export_tmp->index
  5437. - module->import_function_count;
  5438. func_type =
  5439. module->functions[func_index]->func_type;
  5440. if (func_type->param_count == 1
  5441. && func_type->result_count == 1
  5442. && func_type->types[0] == malloc_free_io_type
  5443. && func_type->types[1] == malloc_free_io_type) {
  5444. bh_assert(module->retain_function
  5445. == (uint32)-1);
  5446. module->retain_function = export_tmp->index;
  5447. LOG_VERBOSE("Found retain function, name: %s, "
  5448. "index: %u",
  5449. export_tmp->name,
  5450. export_tmp->index);
  5451. break;
  5452. }
  5453. }
  5454. }
  5455. if (j == module->export_count) {
  5456. module->malloc_function = (uint32)-1;
  5457. LOG_VERBOSE("Can't find retain function,"
  5458. "reset malloc function index to -1");
  5459. }
  5460. }
  5461. }
  5462. else if (((!strcmp(export->name, "free"))
  5463. || (!strcmp(export->name, "__release"))
  5464. || (!strcmp(export->name, "__unpin")))
  5465. && export->index >= module->import_function_count) {
  5466. func_index = export->index - module->import_function_count;
  5467. func_type = module->functions[func_index]->func_type;
  5468. if (func_type->param_count == 1 && func_type->result_count == 0
  5469. && func_type->types[0] == malloc_free_io_type) {
  5470. bh_assert(module->free_function == (uint32)-1);
  5471. module->free_function = export->index;
  5472. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5473. export->name, export->index);
  5474. }
  5475. }
  5476. }
  5477. }
  5478. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5479. handle_table = wasm_interp_get_handle_table();
  5480. #endif
  5481. for (i = 0; i < module->function_count; i++) {
  5482. WASMFunction *func = module->functions[i];
  5483. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5484. error_buf_size)) {
  5485. return false;
  5486. }
  5487. if (i == module->function_count - 1
  5488. && func->code + func->code_size != buf_code_end) {
  5489. set_error_buf(error_buf, error_buf_size,
  5490. "code section size mismatch");
  5491. return false;
  5492. }
  5493. }
  5494. if (!module->possible_memory_grow) {
  5495. WASMMemoryImport *memory_import;
  5496. WASMMemory *memory;
  5497. if (aux_data_end_global && aux_heap_base_global
  5498. && aux_stack_top_global) {
  5499. uint64 init_memory_size;
  5500. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5501. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5502. * valid range of uint32 */
  5503. if (shrunk_memory_size <= UINT32_MAX) {
  5504. if (module->import_memory_count) {
  5505. memory_import = &module->import_memories[0].u.memory;
  5506. init_memory_size =
  5507. (uint64)memory_import->mem_type.num_bytes_per_page
  5508. * memory_import->mem_type.init_page_count;
  5509. if (shrunk_memory_size <= init_memory_size) {
  5510. /* Reset memory info to decrease memory usage */
  5511. memory_import->mem_type.num_bytes_per_page =
  5512. (uint32)shrunk_memory_size;
  5513. memory_import->mem_type.init_page_count = 1;
  5514. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5515. shrunk_memory_size);
  5516. }
  5517. }
  5518. if (module->memory_count) {
  5519. memory = &module->memories[0];
  5520. init_memory_size = (uint64)memory->num_bytes_per_page
  5521. * memory->init_page_count;
  5522. if (shrunk_memory_size <= init_memory_size) {
  5523. /* Reset memory info to decrease memory usage */
  5524. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5525. memory->init_page_count = 1;
  5526. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5527. shrunk_memory_size);
  5528. }
  5529. }
  5530. }
  5531. }
  5532. #if WASM_ENABLE_MULTI_MODULE == 0
  5533. if (module->import_memory_count) {
  5534. memory_import = &module->import_memories[0].u.memory;
  5535. /* Only resize the memory to one big page if num_bytes_per_page is
  5536. * in valid range of uint32 */
  5537. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5538. memory_import->mem_type.num_bytes_per_page *=
  5539. memory_import->mem_type.init_page_count;
  5540. if (memory_import->mem_type.init_page_count > 0)
  5541. memory_import->mem_type.init_page_count =
  5542. memory_import->mem_type.max_page_count = 1;
  5543. else
  5544. memory_import->mem_type.init_page_count =
  5545. memory_import->mem_type.max_page_count = 0;
  5546. }
  5547. }
  5548. if (module->memory_count) {
  5549. memory = &module->memories[0];
  5550. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5551. * valid range of uint32 */
  5552. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5553. memory->num_bytes_per_page *= memory->init_page_count;
  5554. if (memory->init_page_count > 0)
  5555. memory->init_page_count = memory->max_page_count = 1;
  5556. else
  5557. memory->init_page_count = memory->max_page_count = 0;
  5558. }
  5559. }
  5560. #endif
  5561. }
  5562. calculate_global_data_offset(module);
  5563. #if WASM_ENABLE_FAST_JIT != 0
  5564. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5565. return false;
  5566. }
  5567. #endif
  5568. #if WASM_ENABLE_JIT != 0
  5569. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5570. return false;
  5571. }
  5572. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5573. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5574. return false;
  5575. }
  5576. #else
  5577. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5578. thread fast jit execution, since applying llvm optimizations in
  5579. aot_compile_wasm may cost a lot of time.
  5580. Create thread with enough native stack to apply llvm optimizations */
  5581. if (os_thread_create(&module->llvm_jit_init_thread,
  5582. init_llvm_jit_functions_stage2_callback,
  5583. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5584. != 0) {
  5585. set_error_buf(error_buf, error_buf_size,
  5586. "create orcjit compile thread failed");
  5587. return false;
  5588. }
  5589. #endif
  5590. #endif
  5591. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5592. /* Create threads to compile the jit functions */
  5593. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5594. return false;
  5595. }
  5596. #endif
  5597. #if WASM_ENABLE_MEMORY_TRACING != 0
  5598. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5599. #endif
  5600. return true;
  5601. }
  5602. static WASMModule *
  5603. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5604. {
  5605. WASMModule *module =
  5606. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5607. bh_list_status ret;
  5608. if (!module) {
  5609. return NULL;
  5610. }
  5611. module->module_type = Wasm_Module_Bytecode;
  5612. /* Set start_function to -1, means no start function */
  5613. module->start_function = (uint32)-1;
  5614. module->name = name;
  5615. module->is_binary_freeable = false;
  5616. #if WASM_ENABLE_FAST_INTERP == 0
  5617. module->br_table_cache_list = &module->br_table_cache_list_head;
  5618. ret = bh_list_init(module->br_table_cache_list);
  5619. bh_assert(ret == BH_LIST_SUCCESS);
  5620. #endif
  5621. #if WASM_ENABLE_MULTI_MODULE != 0
  5622. module->import_module_list = &module->import_module_list_head;
  5623. ret = bh_list_init(module->import_module_list);
  5624. bh_assert(ret == BH_LIST_SUCCESS);
  5625. #endif
  5626. #if WASM_ENABLE_DEBUG_INTERP != 0
  5627. ret = bh_list_init(&module->fast_opcode_list);
  5628. bh_assert(ret == BH_LIST_SUCCESS);
  5629. #endif
  5630. #if WASM_ENABLE_GC != 0
  5631. if (!(module->ref_type_set =
  5632. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5633. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5634. goto fail1;
  5635. }
  5636. if (os_mutex_init(&module->rtt_type_lock)) {
  5637. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5638. goto fail2;
  5639. }
  5640. #endif
  5641. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5642. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5643. && WASM_ENABLE_LAZY_JIT != 0)
  5644. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5645. set_error_buf(error_buf, error_buf_size,
  5646. "init instance list lock failed");
  5647. goto fail3;
  5648. }
  5649. #endif
  5650. (void)ret;
  5651. return module;
  5652. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5653. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5654. && WASM_ENABLE_LAZY_JIT != 0)
  5655. fail3:
  5656. #endif
  5657. #if WASM_ENABLE_GC != 0
  5658. os_mutex_destroy(&module->rtt_type_lock);
  5659. fail2:
  5660. bh_hash_map_destroy(module->ref_type_set);
  5661. fail1:
  5662. #endif
  5663. wasm_runtime_free(module);
  5664. return NULL;
  5665. }
  5666. #if WASM_ENABLE_DEBUG_INTERP != 0
  5667. static bool
  5668. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5669. uint32 error_buf_size)
  5670. {
  5671. WASMFastOPCodeNode *fast_op =
  5672. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5673. if (fast_op) {
  5674. fast_op->offset = pos - module->load_addr;
  5675. fast_op->orig_op = orig_op;
  5676. bh_list_insert(&module->fast_opcode_list, fast_op);
  5677. }
  5678. return fast_op ? true : false;
  5679. }
  5680. #endif
  5681. WASMModule *
  5682. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5683. uint32 error_buf_size)
  5684. {
  5685. WASMModule *module = create_module("", error_buf, error_buf_size);
  5686. if (!module)
  5687. return NULL;
  5688. if (!load_from_sections(module, section_list, false, true, error_buf,
  5689. error_buf_size)) {
  5690. wasm_loader_unload(module);
  5691. return NULL;
  5692. }
  5693. LOG_VERBOSE("Load module from sections success.\n");
  5694. return module;
  5695. }
  5696. static void
  5697. destroy_sections(WASMSection *section_list)
  5698. {
  5699. WASMSection *section = section_list, *next;
  5700. while (section) {
  5701. next = section->next;
  5702. wasm_runtime_free(section);
  5703. section = next;
  5704. }
  5705. }
  5706. /* clang-format off */
  5707. static uint8 section_ids[] = {
  5708. SECTION_TYPE_USER,
  5709. SECTION_TYPE_TYPE,
  5710. SECTION_TYPE_IMPORT,
  5711. SECTION_TYPE_FUNC,
  5712. SECTION_TYPE_TABLE,
  5713. SECTION_TYPE_MEMORY,
  5714. #if WASM_ENABLE_TAGS != 0
  5715. SECTION_TYPE_TAG,
  5716. #endif
  5717. #if WASM_ENABLE_STRINGREF != 0
  5718. /* must immediately precede the global section,
  5719. or where the global section would be */
  5720. SECTION_TYPE_STRINGREF,
  5721. #endif
  5722. SECTION_TYPE_GLOBAL,
  5723. SECTION_TYPE_EXPORT,
  5724. SECTION_TYPE_START,
  5725. SECTION_TYPE_ELEM,
  5726. #if WASM_ENABLE_BULK_MEMORY != 0
  5727. SECTION_TYPE_DATACOUNT,
  5728. #endif
  5729. SECTION_TYPE_CODE,
  5730. SECTION_TYPE_DATA
  5731. };
  5732. /* clang-format on */
  5733. static uint8
  5734. get_section_index(uint8 section_type)
  5735. {
  5736. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5737. for (uint8 i = 0; i < max_id; i++) {
  5738. if (section_type == section_ids[i])
  5739. return i;
  5740. }
  5741. return (uint8)-1;
  5742. }
  5743. static bool
  5744. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5745. char *error_buf, uint32 error_buf_size)
  5746. {
  5747. WASMSection *section_list_end = NULL, *section;
  5748. const uint8 *p = buf, *p_end = buf + size;
  5749. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5750. uint32 section_size;
  5751. bh_assert(!*p_section_list);
  5752. p += 8;
  5753. while (p < p_end) {
  5754. CHECK_BUF(p, p_end, 1);
  5755. section_type = read_uint8(p);
  5756. section_index = get_section_index(section_type);
  5757. if (section_index != (uint8)-1) {
  5758. if (section_type != SECTION_TYPE_USER) {
  5759. /* Custom sections may be inserted at any place,
  5760. while other sections must occur at most once
  5761. and in prescribed order. */
  5762. if (last_section_index != (uint8)-1
  5763. && (section_index <= last_section_index)) {
  5764. set_error_buf(error_buf, error_buf_size,
  5765. "unexpected content after last section or "
  5766. "junk after last section");
  5767. return false;
  5768. }
  5769. last_section_index = section_index;
  5770. }
  5771. read_leb_uint32(p, p_end, section_size);
  5772. CHECK_BUF1(p, p_end, section_size);
  5773. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5774. error_buf_size))) {
  5775. return false;
  5776. }
  5777. section->section_type = section_type;
  5778. section->section_body = (uint8 *)p;
  5779. section->section_body_size = section_size;
  5780. if (!section_list_end)
  5781. *p_section_list = section_list_end = section;
  5782. else {
  5783. section_list_end->next = section;
  5784. section_list_end = section;
  5785. }
  5786. p += section_size;
  5787. }
  5788. else {
  5789. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5790. return false;
  5791. }
  5792. }
  5793. return true;
  5794. fail:
  5795. return false;
  5796. }
  5797. static void
  5798. exchange32(uint8 *p_data)
  5799. {
  5800. uint8 value = *p_data;
  5801. *p_data = *(p_data + 3);
  5802. *(p_data + 3) = value;
  5803. value = *(p_data + 1);
  5804. *(p_data + 1) = *(p_data + 2);
  5805. *(p_data + 2) = value;
  5806. }
  5807. static union {
  5808. int a;
  5809. char b;
  5810. } __ue = { .a = 1 };
  5811. #define is_little_endian() (__ue.b == 1)
  5812. static bool
  5813. load(const uint8 *buf, uint32 size, WASMModule *module,
  5814. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5815. {
  5816. const uint8 *buf_end = buf + size;
  5817. const uint8 *p = buf, *p_end = buf_end;
  5818. uint32 magic_number, version;
  5819. WASMSection *section_list = NULL;
  5820. CHECK_BUF1(p, p_end, sizeof(uint32));
  5821. magic_number = read_uint32(p);
  5822. if (!is_little_endian())
  5823. exchange32((uint8 *)&magic_number);
  5824. if (magic_number != WASM_MAGIC_NUMBER) {
  5825. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5826. return false;
  5827. }
  5828. CHECK_BUF1(p, p_end, sizeof(uint32));
  5829. version = read_uint32(p);
  5830. if (!is_little_endian())
  5831. exchange32((uint8 *)&version);
  5832. if (version != WASM_CURRENT_VERSION) {
  5833. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5834. return false;
  5835. }
  5836. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5837. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5838. error_buf, error_buf_size)) {
  5839. destroy_sections(section_list);
  5840. return false;
  5841. }
  5842. destroy_sections(section_list);
  5843. return true;
  5844. fail:
  5845. return false;
  5846. }
  5847. #if WASM_ENABLE_LIBC_WASI != 0
  5848. /**
  5849. * refer to
  5850. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5851. */
  5852. static bool
  5853. check_wasi_abi_compatibility(const WASMModule *module,
  5854. #if WASM_ENABLE_MULTI_MODULE != 0
  5855. bool main_module,
  5856. #endif
  5857. char *error_buf, uint32 error_buf_size)
  5858. {
  5859. /**
  5860. * be careful with:
  5861. * wasi compatible modules(command/reactor) which don't import any wasi
  5862. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5863. * reactor can depend on nothing. At the same time, each has its own entry
  5864. * point.
  5865. *
  5866. * observations:
  5867. * - clang always injects `_start` into a command
  5868. * - clang always injects `_initialize` into a reactor
  5869. * - `iwasm -f` allows to run a function in the reactor
  5870. *
  5871. * strong assumptions:
  5872. * - no one will define either `_start` or `_initialize` on purpose
  5873. * - `_start` should always be `void _start(void)`
  5874. * - `_initialize` should always be `void _initialize(void)`
  5875. *
  5876. */
  5877. /* clang-format off */
  5878. /**
  5879. *
  5880. * | | import_wasi_api True | | import_wasi_api False | |
  5881. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5882. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5883. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5884. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5885. */
  5886. /* clang-format on */
  5887. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5888. uint32 import_function_count = module->import_function_count;
  5889. WASMFuncType *func_type;
  5890. /* (func (export "_start") (...) */
  5891. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5892. error_buf, error_buf_size);
  5893. if (start) {
  5894. if (start->index < import_function_count) {
  5895. set_error_buf(
  5896. error_buf, error_buf_size,
  5897. "the builtin _start function can not be an import function");
  5898. return false;
  5899. }
  5900. func_type =
  5901. module->functions[start->index - import_function_count]->func_type;
  5902. if (func_type->param_count || func_type->result_count) {
  5903. set_error_buf(error_buf, error_buf_size,
  5904. "the signature of builtin _start function is wrong");
  5905. return false;
  5906. }
  5907. }
  5908. else {
  5909. /* (func (export "_initialize") (...) */
  5910. initialize =
  5911. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5912. error_buf, error_buf_size);
  5913. if (initialize) {
  5914. if (initialize->index < import_function_count) {
  5915. set_error_buf(error_buf, error_buf_size,
  5916. "the builtin _initialize function can not be an "
  5917. "import function");
  5918. return false;
  5919. }
  5920. func_type =
  5921. module->functions[initialize->index - import_function_count]
  5922. ->func_type;
  5923. if (func_type->param_count || func_type->result_count) {
  5924. set_error_buf(
  5925. error_buf, error_buf_size,
  5926. "the signature of builtin _initialize function is wrong");
  5927. return false;
  5928. }
  5929. }
  5930. }
  5931. /* filter out non-wasi compatible modules */
  5932. if (!module->import_wasi_api && !start && !initialize) {
  5933. return true;
  5934. }
  5935. /* should have one at least */
  5936. if (module->import_wasi_api && !start && !initialize) {
  5937. LOG_WARNING("warning: a module with WASI apis should be either "
  5938. "a command or a reactor");
  5939. }
  5940. /*
  5941. * there is at least one of `_start` and `_initialize` in below cases.
  5942. * according to the assumption, they should be all wasi compatible
  5943. */
  5944. #if WASM_ENABLE_MULTI_MODULE != 0
  5945. /* filter out commands (with `_start`) cases */
  5946. if (start && !main_module) {
  5947. set_error_buf(
  5948. error_buf, error_buf_size,
  5949. "a command (with _start function) can not be a sub-module");
  5950. return false;
  5951. }
  5952. #endif
  5953. /*
  5954. * it is ok a reactor acts as a main module,
  5955. * so skip the check about (with `_initialize`)
  5956. */
  5957. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5958. error_buf, error_buf_size);
  5959. if (!memory
  5960. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5961. /*
  5962. * with wasi-threads, it's still an open question if a memory
  5963. * should be exported.
  5964. *
  5965. * https://github.com/WebAssembly/wasi-threads/issues/22
  5966. * https://github.com/WebAssembly/WASI/issues/502
  5967. *
  5968. * Note: this code assumes the number of memories is at most 1.
  5969. */
  5970. && module->import_memory_count == 0
  5971. #endif
  5972. ) {
  5973. set_error_buf(error_buf, error_buf_size,
  5974. "a module with WASI apis must export memory by default");
  5975. return false;
  5976. }
  5977. return true;
  5978. }
  5979. #endif
  5980. WASMModule *
  5981. wasm_loader_load(uint8 *buf, uint32 size,
  5982. #if WASM_ENABLE_MULTI_MODULE != 0
  5983. bool main_module,
  5984. #endif
  5985. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5986. {
  5987. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5988. if (!module) {
  5989. return NULL;
  5990. }
  5991. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5992. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5993. module->load_addr = (uint8 *)buf;
  5994. module->load_size = size;
  5995. #endif
  5996. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  5997. error_buf_size)) {
  5998. goto fail;
  5999. }
  6000. #if WASM_ENABLE_LIBC_WASI != 0
  6001. /* Check the WASI application ABI */
  6002. if (!check_wasi_abi_compatibility(module,
  6003. #if WASM_ENABLE_MULTI_MODULE != 0
  6004. main_module,
  6005. #endif
  6006. error_buf, error_buf_size)) {
  6007. goto fail;
  6008. }
  6009. #endif
  6010. LOG_VERBOSE("Load module success.\n");
  6011. return module;
  6012. fail:
  6013. wasm_loader_unload(module);
  6014. return NULL;
  6015. }
  6016. void
  6017. wasm_loader_unload(WASMModule *module)
  6018. {
  6019. uint32 i;
  6020. if (!module)
  6021. return;
  6022. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6023. && WASM_ENABLE_LAZY_JIT != 0
  6024. module->orcjit_stop_compiling = true;
  6025. if (module->llvm_jit_init_thread)
  6026. os_thread_join(module->llvm_jit_init_thread, NULL);
  6027. #endif
  6028. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6029. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6030. module internal data after they were freed */
  6031. orcjit_stop_compile_threads(module);
  6032. #endif
  6033. #if WASM_ENABLE_JIT != 0
  6034. if (module->func_ptrs)
  6035. wasm_runtime_free(module->func_ptrs);
  6036. if (module->comp_ctx)
  6037. aot_destroy_comp_context(module->comp_ctx);
  6038. if (module->comp_data)
  6039. aot_destroy_comp_data(module->comp_data);
  6040. #endif
  6041. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6042. && WASM_ENABLE_LAZY_JIT != 0
  6043. if (module->tierup_wait_lock_inited) {
  6044. os_mutex_destroy(&module->tierup_wait_lock);
  6045. os_cond_destroy(&module->tierup_wait_cond);
  6046. }
  6047. #endif
  6048. if (module->imports)
  6049. wasm_runtime_free(module->imports);
  6050. if (module->functions) {
  6051. for (i = 0; i < module->function_count; i++) {
  6052. if (module->functions[i]) {
  6053. if (module->functions[i]->local_offsets)
  6054. wasm_runtime_free(module->functions[i]->local_offsets);
  6055. #if WASM_ENABLE_FAST_INTERP != 0
  6056. if (module->functions[i]->code_compiled)
  6057. wasm_runtime_free(module->functions[i]->code_compiled);
  6058. if (module->functions[i]->consts)
  6059. wasm_runtime_free(module->functions[i]->consts);
  6060. #endif
  6061. #if WASM_ENABLE_FAST_JIT != 0
  6062. if (module->functions[i]->fast_jit_jitted_code) {
  6063. jit_code_cache_free(
  6064. module->functions[i]->fast_jit_jitted_code);
  6065. }
  6066. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6067. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6068. jit_code_cache_free(
  6069. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6070. }
  6071. #endif
  6072. #endif
  6073. #if WASM_ENABLE_GC != 0
  6074. if (module->functions[i]->local_ref_type_maps) {
  6075. wasm_runtime_free(
  6076. module->functions[i]->local_ref_type_maps);
  6077. }
  6078. #endif
  6079. wasm_runtime_free(module->functions[i]);
  6080. }
  6081. }
  6082. wasm_runtime_free(module->functions);
  6083. }
  6084. if (module->tables) {
  6085. #if WASM_ENABLE_GC != 0
  6086. for (i = 0; i < module->table_count; i++) {
  6087. destroy_init_expr(module, &module->tables[i].init_expr);
  6088. }
  6089. #endif
  6090. wasm_runtime_free(module->tables);
  6091. }
  6092. if (module->memories)
  6093. wasm_runtime_free(module->memories);
  6094. if (module->globals) {
  6095. #if WASM_ENABLE_GC != 0
  6096. for (i = 0; i < module->global_count; i++) {
  6097. destroy_init_expr(module, &module->globals[i].init_expr);
  6098. }
  6099. #endif
  6100. wasm_runtime_free(module->globals);
  6101. }
  6102. #if WASM_ENABLE_TAGS != 0
  6103. if (module->tags) {
  6104. for (i = 0; i < module->tag_count; i++) {
  6105. if (module->tags[i])
  6106. wasm_runtime_free(module->tags[i]);
  6107. }
  6108. wasm_runtime_free(module->tags);
  6109. }
  6110. #endif
  6111. if (module->exports)
  6112. wasm_runtime_free(module->exports);
  6113. if (module->table_segments) {
  6114. for (i = 0; i < module->table_seg_count; i++) {
  6115. if (module->table_segments[i].init_values) {
  6116. #if WASM_ENABLE_GC != 0
  6117. uint32 j;
  6118. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6119. destroy_init_expr(
  6120. module, &module->table_segments[i].init_values[j]);
  6121. }
  6122. #endif
  6123. wasm_runtime_free(module->table_segments[i].init_values);
  6124. }
  6125. }
  6126. wasm_runtime_free(module->table_segments);
  6127. }
  6128. if (module->data_segments) {
  6129. for (i = 0; i < module->data_seg_count; i++) {
  6130. if (module->data_segments[i]) {
  6131. if (module->data_segments[i]->is_data_cloned)
  6132. wasm_runtime_free(module->data_segments[i]->data);
  6133. wasm_runtime_free(module->data_segments[i]);
  6134. }
  6135. }
  6136. wasm_runtime_free(module->data_segments);
  6137. }
  6138. if (module->types) {
  6139. for (i = 0; i < module->type_count; i++) {
  6140. if (module->types[i])
  6141. destroy_wasm_type(module->types[i]);
  6142. }
  6143. wasm_runtime_free(module->types);
  6144. }
  6145. if (module->const_str_list) {
  6146. StringNode *node = module->const_str_list, *node_next;
  6147. while (node) {
  6148. node_next = node->next;
  6149. wasm_runtime_free(node);
  6150. node = node_next;
  6151. }
  6152. }
  6153. #if WASM_ENABLE_STRINGREF != 0
  6154. if (module->string_literal_ptrs) {
  6155. wasm_runtime_free((void *)module->string_literal_ptrs);
  6156. }
  6157. if (module->string_literal_lengths) {
  6158. wasm_runtime_free(module->string_literal_lengths);
  6159. }
  6160. #endif
  6161. #if WASM_ENABLE_FAST_INTERP == 0
  6162. if (module->br_table_cache_list) {
  6163. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6164. BrTableCache *node_next;
  6165. while (node) {
  6166. node_next = bh_list_elem_next(node);
  6167. wasm_runtime_free(node);
  6168. node = node_next;
  6169. }
  6170. }
  6171. #endif
  6172. #if WASM_ENABLE_MULTI_MODULE != 0
  6173. /* just release the sub module list */
  6174. if (module->import_module_list) {
  6175. WASMRegisteredModule *node =
  6176. bh_list_first_elem(module->import_module_list);
  6177. while (node) {
  6178. WASMRegisteredModule *next = bh_list_elem_next(node);
  6179. bh_list_remove(module->import_module_list, node);
  6180. /*
  6181. * unload(sub_module) will be triggered during runtime_destroy().
  6182. * every module in the global module list will be unloaded one by
  6183. * one. so don't worry.
  6184. */
  6185. wasm_runtime_free(node);
  6186. /*
  6187. * the module file reading buffer will be released
  6188. * in runtime_destroy()
  6189. */
  6190. node = next;
  6191. }
  6192. }
  6193. #endif
  6194. #if WASM_ENABLE_DEBUG_INTERP != 0
  6195. WASMFastOPCodeNode *fast_opcode =
  6196. bh_list_first_elem(&module->fast_opcode_list);
  6197. while (fast_opcode) {
  6198. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6199. wasm_runtime_free(fast_opcode);
  6200. fast_opcode = next;
  6201. }
  6202. #endif
  6203. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6204. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6205. && WASM_ENABLE_LAZY_JIT != 0)
  6206. os_mutex_destroy(&module->instance_list_lock);
  6207. #endif
  6208. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6209. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6210. #endif
  6211. #if WASM_ENABLE_FAST_JIT != 0
  6212. if (module->fast_jit_func_ptrs) {
  6213. wasm_runtime_free(module->fast_jit_func_ptrs);
  6214. }
  6215. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6216. if (module->fast_jit_thread_locks_inited[i]) {
  6217. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6218. }
  6219. }
  6220. #endif
  6221. #if WASM_ENABLE_GC != 0
  6222. os_mutex_destroy(&module->rtt_type_lock);
  6223. bh_hash_map_destroy(module->ref_type_set);
  6224. if (module->rtt_types) {
  6225. for (i = 0; i < module->type_count; i++) {
  6226. if (module->rtt_types[i])
  6227. wasm_runtime_free(module->rtt_types[i]);
  6228. }
  6229. wasm_runtime_free(module->rtt_types);
  6230. }
  6231. #if WASM_ENABLE_STRINGREF != 0
  6232. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6233. if (module->stringref_rtts[i])
  6234. wasm_runtime_free(module->stringref_rtts[i]);
  6235. }
  6236. #endif
  6237. #endif
  6238. wasm_runtime_free(module);
  6239. }
  6240. bool
  6241. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6242. const uint8 *start_addr, const uint8 *code_end_addr,
  6243. uint8 label_type, uint8 **p_else_addr,
  6244. uint8 **p_end_addr)
  6245. {
  6246. const uint8 *p = start_addr, *p_end = code_end_addr;
  6247. uint8 *else_addr = NULL;
  6248. char error_buf[128];
  6249. uint32 block_nested_depth = 1, count, i, j, t;
  6250. uint32 error_buf_size = sizeof(error_buf);
  6251. uint8 opcode, u8;
  6252. BlockAddr block_stack[16] = { { 0 } }, *block;
  6253. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6254. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6255. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6256. if (block[j].start_addr == start_addr) {
  6257. /* Cache hit */
  6258. *p_else_addr = block[j].else_addr;
  6259. *p_end_addr = block[j].end_addr;
  6260. return true;
  6261. }
  6262. }
  6263. /* Cache unhit */
  6264. block_stack[0].start_addr = start_addr;
  6265. while (p < code_end_addr) {
  6266. opcode = *p++;
  6267. #if WASM_ENABLE_DEBUG_INTERP != 0
  6268. op_break_retry:
  6269. #endif
  6270. switch (opcode) {
  6271. case WASM_OP_UNREACHABLE:
  6272. case WASM_OP_NOP:
  6273. break;
  6274. #if WASM_ENABLE_EXCE_HANDLING != 0
  6275. case WASM_OP_TRY:
  6276. u8 = read_uint8(p);
  6277. if (block_nested_depth
  6278. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6279. block_stack[block_nested_depth].start_addr = p;
  6280. block_stack[block_nested_depth].else_addr = NULL;
  6281. }
  6282. block_nested_depth++;
  6283. break;
  6284. case EXT_OP_TRY:
  6285. skip_leb_uint32(p, p_end);
  6286. if (block_nested_depth
  6287. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6288. block_stack[block_nested_depth].start_addr = p;
  6289. block_stack[block_nested_depth].else_addr = NULL;
  6290. }
  6291. block_nested_depth++;
  6292. break;
  6293. case WASM_OP_CATCH:
  6294. if (block_nested_depth == 1) {
  6295. *p_end_addr = (uint8 *)(p - 1);
  6296. /* stop search and return the address of the catch block */
  6297. return true;
  6298. }
  6299. break;
  6300. case WASM_OP_CATCH_ALL:
  6301. if (block_nested_depth == 1) {
  6302. *p_end_addr = (uint8 *)(p - 1);
  6303. /* stop search and return the address of the catch_all block
  6304. */
  6305. return true;
  6306. }
  6307. break;
  6308. case WASM_OP_THROW:
  6309. /* skip tag_index */
  6310. skip_leb(p);
  6311. break;
  6312. case WASM_OP_RETHROW:
  6313. /* skip depth */
  6314. skip_leb(p);
  6315. break;
  6316. case WASM_OP_DELEGATE:
  6317. if (block_nested_depth == 1) {
  6318. *p_end_addr = (uint8 *)(p - 1);
  6319. return true;
  6320. }
  6321. else {
  6322. skip_leb(p);
  6323. /* the DELEGATE opcode ends the tryblock, */
  6324. block_nested_depth--;
  6325. if (block_nested_depth
  6326. < sizeof(block_stack) / sizeof(BlockAddr))
  6327. block_stack[block_nested_depth].end_addr =
  6328. (uint8 *)(p - 1);
  6329. }
  6330. break;
  6331. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6332. case WASM_OP_BLOCK:
  6333. case WASM_OP_LOOP:
  6334. case WASM_OP_IF:
  6335. {
  6336. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6337. u8 = read_uint8(p);
  6338. if (is_byte_a_type(u8)) {
  6339. #if WASM_ENABLE_GC != 0
  6340. if (wasm_is_type_multi_byte_type(u8)) {
  6341. /* the possible extra bytes of GC ref type have been
  6342. modified to OP_NOP, no need to resolve them again */
  6343. }
  6344. #endif
  6345. }
  6346. else {
  6347. p--;
  6348. /* block type */
  6349. skip_leb_int32(p, p_end);
  6350. }
  6351. if (block_nested_depth
  6352. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6353. block_stack[block_nested_depth].start_addr = p;
  6354. block_stack[block_nested_depth].else_addr = NULL;
  6355. }
  6356. block_nested_depth++;
  6357. break;
  6358. }
  6359. case EXT_OP_BLOCK:
  6360. case EXT_OP_LOOP:
  6361. case EXT_OP_IF:
  6362. /* block type */
  6363. skip_leb_int32(p, p_end);
  6364. if (block_nested_depth
  6365. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6366. block_stack[block_nested_depth].start_addr = p;
  6367. block_stack[block_nested_depth].else_addr = NULL;
  6368. }
  6369. block_nested_depth++;
  6370. break;
  6371. case WASM_OP_ELSE:
  6372. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6373. else_addr = (uint8 *)(p - 1);
  6374. if (block_nested_depth - 1
  6375. < sizeof(block_stack) / sizeof(BlockAddr))
  6376. block_stack[block_nested_depth - 1].else_addr =
  6377. (uint8 *)(p - 1);
  6378. break;
  6379. case WASM_OP_END:
  6380. if (block_nested_depth == 1) {
  6381. if (label_type == LABEL_TYPE_IF)
  6382. *p_else_addr = else_addr;
  6383. *p_end_addr = (uint8 *)(p - 1);
  6384. block_stack[0].end_addr = (uint8 *)(p - 1);
  6385. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6386. t++) {
  6387. start_addr = block_stack[t].start_addr;
  6388. if (start_addr) {
  6389. i = ((uintptr_t)start_addr)
  6390. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6391. block =
  6392. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6393. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6394. if (!block[j].start_addr)
  6395. break;
  6396. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6397. memmove(block + 1, block,
  6398. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6399. * sizeof(BlockAddr));
  6400. j = 0;
  6401. }
  6402. block[j].start_addr = block_stack[t].start_addr;
  6403. block[j].else_addr = block_stack[t].else_addr;
  6404. block[j].end_addr = block_stack[t].end_addr;
  6405. }
  6406. else
  6407. break;
  6408. }
  6409. return true;
  6410. }
  6411. else {
  6412. block_nested_depth--;
  6413. if (block_nested_depth
  6414. < sizeof(block_stack) / sizeof(BlockAddr))
  6415. block_stack[block_nested_depth].end_addr =
  6416. (uint8 *)(p - 1);
  6417. }
  6418. break;
  6419. case WASM_OP_BR:
  6420. case WASM_OP_BR_IF:
  6421. skip_leb_uint32(p, p_end); /* labelidx */
  6422. break;
  6423. case WASM_OP_BR_TABLE:
  6424. read_leb_uint32(p, p_end, count); /* label num */
  6425. #if WASM_ENABLE_FAST_INTERP != 0
  6426. for (i = 0; i <= count; i++) /* labelidxs */
  6427. skip_leb_uint32(p, p_end);
  6428. #else
  6429. p += count + 1;
  6430. while (*p == WASM_OP_NOP)
  6431. p++;
  6432. #endif
  6433. break;
  6434. #if WASM_ENABLE_FAST_INTERP == 0
  6435. case EXT_OP_BR_TABLE_CACHE:
  6436. read_leb_uint32(p, p_end, count); /* label num */
  6437. while (*p == WASM_OP_NOP)
  6438. p++;
  6439. break;
  6440. #endif
  6441. case WASM_OP_RETURN:
  6442. break;
  6443. case WASM_OP_CALL:
  6444. #if WASM_ENABLE_TAIL_CALL != 0
  6445. case WASM_OP_RETURN_CALL:
  6446. #endif
  6447. skip_leb_uint32(p, p_end); /* funcidx */
  6448. break;
  6449. case WASM_OP_CALL_INDIRECT:
  6450. #if WASM_ENABLE_TAIL_CALL != 0
  6451. case WASM_OP_RETURN_CALL_INDIRECT:
  6452. #endif
  6453. skip_leb_uint32(p, p_end); /* typeidx */
  6454. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6455. u8 = read_uint8(p); /* 0x00 */
  6456. #else
  6457. skip_leb_uint32(p, p_end); /* tableidx */
  6458. #endif
  6459. break;
  6460. #if WASM_ENABLE_GC != 0
  6461. case WASM_OP_CALL_REF:
  6462. case WASM_OP_RETURN_CALL_REF:
  6463. skip_leb_uint32(p, p_end); /* typeidx */
  6464. break;
  6465. #endif
  6466. case WASM_OP_DROP:
  6467. case WASM_OP_SELECT:
  6468. case WASM_OP_DROP_64:
  6469. case WASM_OP_SELECT_64:
  6470. break;
  6471. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6472. case WASM_OP_SELECT_T:
  6473. {
  6474. skip_leb_uint32(p, p_end); /* vec length */
  6475. u8 = read_uint8(p); /* typeidx */
  6476. /* the possible extra bytes of GC ref type have been
  6477. modified to OP_NOP, no need to resolve them again */
  6478. break;
  6479. }
  6480. case WASM_OP_TABLE_GET:
  6481. case WASM_OP_TABLE_SET:
  6482. skip_leb_uint32(p, p_end); /* table index */
  6483. break;
  6484. case WASM_OP_REF_NULL:
  6485. {
  6486. u8 = read_uint8(p); /* type */
  6487. if (is_byte_a_type(u8)) {
  6488. #if WASM_ENABLE_GC != 0
  6489. if (wasm_is_type_multi_byte_type(u8)) {
  6490. /* the possible extra bytes of GC ref type have been
  6491. modified to OP_NOP, no need to resolve them again */
  6492. }
  6493. #endif
  6494. }
  6495. else {
  6496. p--;
  6497. skip_leb_uint32(p, p_end);
  6498. }
  6499. break;
  6500. }
  6501. case WASM_OP_REF_IS_NULL:
  6502. break;
  6503. case WASM_OP_REF_FUNC:
  6504. skip_leb_uint32(p, p_end); /* func index */
  6505. break;
  6506. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6507. #if WASM_ENABLE_GC != 0
  6508. case WASM_OP_REF_AS_NON_NULL:
  6509. case WASM_OP_REF_EQ:
  6510. break;
  6511. case WASM_OP_BR_ON_NULL:
  6512. case WASM_OP_BR_ON_NON_NULL:
  6513. skip_leb_uint32(p, p_end); /* label index */
  6514. break;
  6515. #endif /* end of WASM_ENABLE_GC != 0 */
  6516. case WASM_OP_GET_LOCAL:
  6517. case WASM_OP_SET_LOCAL:
  6518. case WASM_OP_TEE_LOCAL:
  6519. case WASM_OP_GET_GLOBAL:
  6520. case WASM_OP_SET_GLOBAL:
  6521. case WASM_OP_GET_GLOBAL_64:
  6522. case WASM_OP_SET_GLOBAL_64:
  6523. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6524. skip_leb_uint32(p, p_end); /* local index */
  6525. break;
  6526. case EXT_OP_GET_LOCAL_FAST:
  6527. case EXT_OP_SET_LOCAL_FAST:
  6528. case EXT_OP_TEE_LOCAL_FAST:
  6529. CHECK_BUF(p, p_end, 1);
  6530. p++;
  6531. break;
  6532. case WASM_OP_I32_LOAD:
  6533. case WASM_OP_I64_LOAD:
  6534. case WASM_OP_F32_LOAD:
  6535. case WASM_OP_F64_LOAD:
  6536. case WASM_OP_I32_LOAD8_S:
  6537. case WASM_OP_I32_LOAD8_U:
  6538. case WASM_OP_I32_LOAD16_S:
  6539. case WASM_OP_I32_LOAD16_U:
  6540. case WASM_OP_I64_LOAD8_S:
  6541. case WASM_OP_I64_LOAD8_U:
  6542. case WASM_OP_I64_LOAD16_S:
  6543. case WASM_OP_I64_LOAD16_U:
  6544. case WASM_OP_I64_LOAD32_S:
  6545. case WASM_OP_I64_LOAD32_U:
  6546. case WASM_OP_I32_STORE:
  6547. case WASM_OP_I64_STORE:
  6548. case WASM_OP_F32_STORE:
  6549. case WASM_OP_F64_STORE:
  6550. case WASM_OP_I32_STORE8:
  6551. case WASM_OP_I32_STORE16:
  6552. case WASM_OP_I64_STORE8:
  6553. case WASM_OP_I64_STORE16:
  6554. case WASM_OP_I64_STORE32:
  6555. skip_leb_uint32(p, p_end); /* align */
  6556. skip_leb_mem_offset(p, p_end); /* offset */
  6557. break;
  6558. case WASM_OP_MEMORY_SIZE:
  6559. case WASM_OP_MEMORY_GROW:
  6560. skip_leb_uint32(p, p_end); /* 0x00 */
  6561. break;
  6562. case WASM_OP_I32_CONST:
  6563. skip_leb_int32(p, p_end);
  6564. break;
  6565. case WASM_OP_I64_CONST:
  6566. skip_leb_int64(p, p_end);
  6567. break;
  6568. case WASM_OP_F32_CONST:
  6569. p += sizeof(float32);
  6570. break;
  6571. case WASM_OP_F64_CONST:
  6572. p += sizeof(float64);
  6573. break;
  6574. case WASM_OP_I32_EQZ:
  6575. case WASM_OP_I32_EQ:
  6576. case WASM_OP_I32_NE:
  6577. case WASM_OP_I32_LT_S:
  6578. case WASM_OP_I32_LT_U:
  6579. case WASM_OP_I32_GT_S:
  6580. case WASM_OP_I32_GT_U:
  6581. case WASM_OP_I32_LE_S:
  6582. case WASM_OP_I32_LE_U:
  6583. case WASM_OP_I32_GE_S:
  6584. case WASM_OP_I32_GE_U:
  6585. case WASM_OP_I64_EQZ:
  6586. case WASM_OP_I64_EQ:
  6587. case WASM_OP_I64_NE:
  6588. case WASM_OP_I64_LT_S:
  6589. case WASM_OP_I64_LT_U:
  6590. case WASM_OP_I64_GT_S:
  6591. case WASM_OP_I64_GT_U:
  6592. case WASM_OP_I64_LE_S:
  6593. case WASM_OP_I64_LE_U:
  6594. case WASM_OP_I64_GE_S:
  6595. case WASM_OP_I64_GE_U:
  6596. case WASM_OP_F32_EQ:
  6597. case WASM_OP_F32_NE:
  6598. case WASM_OP_F32_LT:
  6599. case WASM_OP_F32_GT:
  6600. case WASM_OP_F32_LE:
  6601. case WASM_OP_F32_GE:
  6602. case WASM_OP_F64_EQ:
  6603. case WASM_OP_F64_NE:
  6604. case WASM_OP_F64_LT:
  6605. case WASM_OP_F64_GT:
  6606. case WASM_OP_F64_LE:
  6607. case WASM_OP_F64_GE:
  6608. case WASM_OP_I32_CLZ:
  6609. case WASM_OP_I32_CTZ:
  6610. case WASM_OP_I32_POPCNT:
  6611. case WASM_OP_I32_ADD:
  6612. case WASM_OP_I32_SUB:
  6613. case WASM_OP_I32_MUL:
  6614. case WASM_OP_I32_DIV_S:
  6615. case WASM_OP_I32_DIV_U:
  6616. case WASM_OP_I32_REM_S:
  6617. case WASM_OP_I32_REM_U:
  6618. case WASM_OP_I32_AND:
  6619. case WASM_OP_I32_OR:
  6620. case WASM_OP_I32_XOR:
  6621. case WASM_OP_I32_SHL:
  6622. case WASM_OP_I32_SHR_S:
  6623. case WASM_OP_I32_SHR_U:
  6624. case WASM_OP_I32_ROTL:
  6625. case WASM_OP_I32_ROTR:
  6626. case WASM_OP_I64_CLZ:
  6627. case WASM_OP_I64_CTZ:
  6628. case WASM_OP_I64_POPCNT:
  6629. case WASM_OP_I64_ADD:
  6630. case WASM_OP_I64_SUB:
  6631. case WASM_OP_I64_MUL:
  6632. case WASM_OP_I64_DIV_S:
  6633. case WASM_OP_I64_DIV_U:
  6634. case WASM_OP_I64_REM_S:
  6635. case WASM_OP_I64_REM_U:
  6636. case WASM_OP_I64_AND:
  6637. case WASM_OP_I64_OR:
  6638. case WASM_OP_I64_XOR:
  6639. case WASM_OP_I64_SHL:
  6640. case WASM_OP_I64_SHR_S:
  6641. case WASM_OP_I64_SHR_U:
  6642. case WASM_OP_I64_ROTL:
  6643. case WASM_OP_I64_ROTR:
  6644. case WASM_OP_F32_ABS:
  6645. case WASM_OP_F32_NEG:
  6646. case WASM_OP_F32_CEIL:
  6647. case WASM_OP_F32_FLOOR:
  6648. case WASM_OP_F32_TRUNC:
  6649. case WASM_OP_F32_NEAREST:
  6650. case WASM_OP_F32_SQRT:
  6651. case WASM_OP_F32_ADD:
  6652. case WASM_OP_F32_SUB:
  6653. case WASM_OP_F32_MUL:
  6654. case WASM_OP_F32_DIV:
  6655. case WASM_OP_F32_MIN:
  6656. case WASM_OP_F32_MAX:
  6657. case WASM_OP_F32_COPYSIGN:
  6658. case WASM_OP_F64_ABS:
  6659. case WASM_OP_F64_NEG:
  6660. case WASM_OP_F64_CEIL:
  6661. case WASM_OP_F64_FLOOR:
  6662. case WASM_OP_F64_TRUNC:
  6663. case WASM_OP_F64_NEAREST:
  6664. case WASM_OP_F64_SQRT:
  6665. case WASM_OP_F64_ADD:
  6666. case WASM_OP_F64_SUB:
  6667. case WASM_OP_F64_MUL:
  6668. case WASM_OP_F64_DIV:
  6669. case WASM_OP_F64_MIN:
  6670. case WASM_OP_F64_MAX:
  6671. case WASM_OP_F64_COPYSIGN:
  6672. case WASM_OP_I32_WRAP_I64:
  6673. case WASM_OP_I32_TRUNC_S_F32:
  6674. case WASM_OP_I32_TRUNC_U_F32:
  6675. case WASM_OP_I32_TRUNC_S_F64:
  6676. case WASM_OP_I32_TRUNC_U_F64:
  6677. case WASM_OP_I64_EXTEND_S_I32:
  6678. case WASM_OP_I64_EXTEND_U_I32:
  6679. case WASM_OP_I64_TRUNC_S_F32:
  6680. case WASM_OP_I64_TRUNC_U_F32:
  6681. case WASM_OP_I64_TRUNC_S_F64:
  6682. case WASM_OP_I64_TRUNC_U_F64:
  6683. case WASM_OP_F32_CONVERT_S_I32:
  6684. case WASM_OP_F32_CONVERT_U_I32:
  6685. case WASM_OP_F32_CONVERT_S_I64:
  6686. case WASM_OP_F32_CONVERT_U_I64:
  6687. case WASM_OP_F32_DEMOTE_F64:
  6688. case WASM_OP_F64_CONVERT_S_I32:
  6689. case WASM_OP_F64_CONVERT_U_I32:
  6690. case WASM_OP_F64_CONVERT_S_I64:
  6691. case WASM_OP_F64_CONVERT_U_I64:
  6692. case WASM_OP_F64_PROMOTE_F32:
  6693. case WASM_OP_I32_REINTERPRET_F32:
  6694. case WASM_OP_I64_REINTERPRET_F64:
  6695. case WASM_OP_F32_REINTERPRET_I32:
  6696. case WASM_OP_F64_REINTERPRET_I64:
  6697. case WASM_OP_I32_EXTEND8_S:
  6698. case WASM_OP_I32_EXTEND16_S:
  6699. case WASM_OP_I64_EXTEND8_S:
  6700. case WASM_OP_I64_EXTEND16_S:
  6701. case WASM_OP_I64_EXTEND32_S:
  6702. break;
  6703. #if WASM_ENABLE_GC != 0
  6704. case WASM_OP_GC_PREFIX:
  6705. {
  6706. uint32 opcode1;
  6707. read_leb_uint32(p, p_end, opcode1);
  6708. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6709. is no larger than UINT8_MAX */
  6710. opcode = (uint8)opcode1;
  6711. switch (opcode) {
  6712. case WASM_OP_STRUCT_NEW:
  6713. case WASM_OP_STRUCT_NEW_DEFAULT:
  6714. skip_leb_uint32(p, p_end); /* typeidx */
  6715. break;
  6716. case WASM_OP_STRUCT_GET:
  6717. case WASM_OP_STRUCT_GET_S:
  6718. case WASM_OP_STRUCT_GET_U:
  6719. case WASM_OP_STRUCT_SET:
  6720. skip_leb_uint32(p, p_end); /* typeidx */
  6721. skip_leb_uint32(p, p_end); /* fieldidx */
  6722. break;
  6723. case WASM_OP_ARRAY_NEW:
  6724. case WASM_OP_ARRAY_NEW_DEFAULT:
  6725. case WASM_OP_ARRAY_GET:
  6726. case WASM_OP_ARRAY_GET_S:
  6727. case WASM_OP_ARRAY_GET_U:
  6728. case WASM_OP_ARRAY_SET:
  6729. case WASM_OP_ARRAY_FILL:
  6730. skip_leb_uint32(p, p_end); /* typeidx */
  6731. break;
  6732. case WASM_OP_ARRAY_COPY:
  6733. skip_leb_uint32(p, p_end); /* typeidx1 */
  6734. skip_leb_uint32(p, p_end); /* typeidx2 */
  6735. break;
  6736. case WASM_OP_ARRAY_LEN:
  6737. break;
  6738. case WASM_OP_ARRAY_NEW_FIXED:
  6739. case WASM_OP_ARRAY_NEW_DATA:
  6740. case WASM_OP_ARRAY_NEW_ELEM:
  6741. skip_leb_uint32(p, p_end); /* typeidx */
  6742. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6743. break;
  6744. case WASM_OP_REF_I31:
  6745. case WASM_OP_I31_GET_S:
  6746. case WASM_OP_I31_GET_U:
  6747. break;
  6748. case WASM_OP_REF_TEST:
  6749. case WASM_OP_REF_CAST:
  6750. case WASM_OP_REF_TEST_NULLABLE:
  6751. case WASM_OP_REF_CAST_NULLABLE:
  6752. skip_leb_int32(p, p_end); /* heaptype */
  6753. break;
  6754. case WASM_OP_BR_ON_CAST:
  6755. case WASM_OP_BR_ON_CAST_FAIL:
  6756. p += sizeof(uint8); /* castflag */
  6757. skip_leb_uint32(p, p_end); /* labelidx */
  6758. skip_leb_int32(p, p_end); /* heaptype */
  6759. skip_leb_int32(p, p_end); /* heaptype2 */
  6760. break;
  6761. case WASM_OP_ANY_CONVERT_EXTERN:
  6762. case WASM_OP_EXTERN_CONVERT_ANY:
  6763. break;
  6764. #if WASM_ENABLE_STRINGREF != 0
  6765. case WASM_OP_STRING_NEW_UTF8:
  6766. case WASM_OP_STRING_NEW_WTF16:
  6767. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6768. case WASM_OP_STRING_NEW_WTF8:
  6769. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6770. break;
  6771. case WASM_OP_STRING_CONST:
  6772. skip_leb_int32(p, p_end); /* contents */
  6773. break;
  6774. case WASM_OP_STRING_MEASURE_UTF8:
  6775. case WASM_OP_STRING_MEASURE_WTF8:
  6776. case WASM_OP_STRING_MEASURE_WTF16:
  6777. break;
  6778. case WASM_OP_STRING_ENCODE_UTF8:
  6779. case WASM_OP_STRING_ENCODE_WTF16:
  6780. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6781. case WASM_OP_STRING_ENCODE_WTF8:
  6782. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6783. break;
  6784. case WASM_OP_STRING_CONCAT:
  6785. case WASM_OP_STRING_EQ:
  6786. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6787. case WASM_OP_STRING_AS_WTF8:
  6788. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6789. break;
  6790. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6791. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6792. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6793. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6794. break;
  6795. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6796. case WASM_OP_STRING_AS_WTF16:
  6797. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6798. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6799. break;
  6800. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6801. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6802. break;
  6803. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6804. case WASM_OP_STRING_AS_ITER:
  6805. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6806. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6807. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6808. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6809. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6810. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6811. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6812. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6813. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6814. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6815. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6816. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6817. break;
  6818. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6819. default:
  6820. return false;
  6821. }
  6822. break;
  6823. }
  6824. #endif /* end of WASM_ENABLE_GC != 0 */
  6825. case WASM_OP_MISC_PREFIX:
  6826. {
  6827. uint32 opcode1;
  6828. read_leb_uint32(p, p_end, opcode1);
  6829. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6830. is no larger than UINT8_MAX */
  6831. opcode = (uint8)opcode1;
  6832. switch (opcode) {
  6833. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6834. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6835. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6836. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6837. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6838. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6839. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6840. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6841. break;
  6842. #if WASM_ENABLE_BULK_MEMORY != 0
  6843. case WASM_OP_MEMORY_INIT:
  6844. skip_leb_uint32(p, p_end);
  6845. /* skip memory idx */
  6846. p++;
  6847. break;
  6848. case WASM_OP_DATA_DROP:
  6849. skip_leb_uint32(p, p_end);
  6850. break;
  6851. case WASM_OP_MEMORY_COPY:
  6852. /* skip two memory idx */
  6853. p += 2;
  6854. break;
  6855. case WASM_OP_MEMORY_FILL:
  6856. /* skip memory idx */
  6857. p++;
  6858. break;
  6859. #endif /* WASM_ENABLE_BULK_MEMORY */
  6860. #if WASM_ENABLE_REF_TYPES != 0
  6861. case WASM_OP_TABLE_INIT:
  6862. case WASM_OP_TABLE_COPY:
  6863. /* tableidx */
  6864. skip_leb_uint32(p, p_end);
  6865. /* elemidx */
  6866. skip_leb_uint32(p, p_end);
  6867. break;
  6868. case WASM_OP_ELEM_DROP:
  6869. /* elemidx */
  6870. skip_leb_uint32(p, p_end);
  6871. break;
  6872. case WASM_OP_TABLE_SIZE:
  6873. case WASM_OP_TABLE_GROW:
  6874. case WASM_OP_TABLE_FILL:
  6875. skip_leb_uint32(p, p_end); /* table idx */
  6876. break;
  6877. #endif /* WASM_ENABLE_REF_TYPES */
  6878. default:
  6879. return false;
  6880. }
  6881. break;
  6882. }
  6883. #if WASM_ENABLE_SIMD != 0
  6884. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6885. case WASM_OP_SIMD_PREFIX:
  6886. {
  6887. uint32 opcode1;
  6888. read_leb_uint32(p, p_end, opcode1);
  6889. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6890. is no larger than UINT8_MAX */
  6891. opcode = (uint8)opcode1;
  6892. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6893. */
  6894. switch (opcode) {
  6895. case SIMD_v128_load:
  6896. case SIMD_v128_load8x8_s:
  6897. case SIMD_v128_load8x8_u:
  6898. case SIMD_v128_load16x4_s:
  6899. case SIMD_v128_load16x4_u:
  6900. case SIMD_v128_load32x2_s:
  6901. case SIMD_v128_load32x2_u:
  6902. case SIMD_v128_load8_splat:
  6903. case SIMD_v128_load16_splat:
  6904. case SIMD_v128_load32_splat:
  6905. case SIMD_v128_load64_splat:
  6906. case SIMD_v128_store:
  6907. /* memarg align */
  6908. skip_leb_uint32(p, p_end);
  6909. /* memarg offset */
  6910. skip_leb_mem_offset(p, p_end);
  6911. break;
  6912. case SIMD_v128_const:
  6913. case SIMD_v8x16_shuffle:
  6914. /* immByte[16] immLaneId[16] */
  6915. CHECK_BUF1(p, p_end, 16);
  6916. p += 16;
  6917. break;
  6918. case SIMD_i8x16_extract_lane_s:
  6919. case SIMD_i8x16_extract_lane_u:
  6920. case SIMD_i8x16_replace_lane:
  6921. case SIMD_i16x8_extract_lane_s:
  6922. case SIMD_i16x8_extract_lane_u:
  6923. case SIMD_i16x8_replace_lane:
  6924. case SIMD_i32x4_extract_lane:
  6925. case SIMD_i32x4_replace_lane:
  6926. case SIMD_i64x2_extract_lane:
  6927. case SIMD_i64x2_replace_lane:
  6928. case SIMD_f32x4_extract_lane:
  6929. case SIMD_f32x4_replace_lane:
  6930. case SIMD_f64x2_extract_lane:
  6931. case SIMD_f64x2_replace_lane:
  6932. /* ImmLaneId */
  6933. CHECK_BUF(p, p_end, 1);
  6934. p++;
  6935. break;
  6936. case SIMD_v128_load8_lane:
  6937. case SIMD_v128_load16_lane:
  6938. case SIMD_v128_load32_lane:
  6939. case SIMD_v128_load64_lane:
  6940. case SIMD_v128_store8_lane:
  6941. case SIMD_v128_store16_lane:
  6942. case SIMD_v128_store32_lane:
  6943. case SIMD_v128_store64_lane:
  6944. /* memarg align */
  6945. skip_leb_uint32(p, p_end);
  6946. /* memarg offset */
  6947. skip_leb_mem_offset(p, p_end);
  6948. /* ImmLaneId */
  6949. CHECK_BUF(p, p_end, 1);
  6950. p++;
  6951. break;
  6952. case SIMD_v128_load32_zero:
  6953. case SIMD_v128_load64_zero:
  6954. /* memarg align */
  6955. skip_leb_uint32(p, p_end);
  6956. /* memarg offset */
  6957. skip_leb_mem_offset(p, p_end);
  6958. break;
  6959. default:
  6960. /*
  6961. * since latest SIMD specific used almost every value
  6962. * from 0x00 to 0xff, the default branch will present
  6963. * all opcodes without imm
  6964. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6965. */
  6966. break;
  6967. }
  6968. break;
  6969. }
  6970. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6971. #endif /* end of WASM_ENABLE_SIMD */
  6972. #if WASM_ENABLE_SHARED_MEMORY != 0
  6973. case WASM_OP_ATOMIC_PREFIX:
  6974. {
  6975. /* TODO: memory64 offset type changes */
  6976. uint32 opcode1;
  6977. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6978. read_leb_uint32(p, p_end, opcode1);
  6979. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6980. is no larger than UINT8_MAX */
  6981. opcode = (uint8)opcode1;
  6982. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6983. skip_leb_uint32(p, p_end); /* align */
  6984. skip_leb_mem_offset(p, p_end); /* offset */
  6985. }
  6986. else {
  6987. /* atomic.fence doesn't have memarg */
  6988. p++;
  6989. }
  6990. break;
  6991. }
  6992. #endif
  6993. #if WASM_ENABLE_DEBUG_INTERP != 0
  6994. case DEBUG_OP_BREAK:
  6995. {
  6996. WASMDebugInstance *debug_instance =
  6997. wasm_exec_env_get_instance(exec_env);
  6998. char original_opcode[1];
  6999. uint64 size = 1;
  7000. WASMModuleInstance *module_inst =
  7001. (WASMModuleInstance *)exec_env->module_inst;
  7002. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7003. ? (p - 1) - module_inst->module->load_addr
  7004. : ~0;
  7005. if (debug_instance) {
  7006. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7007. original_opcode, &size)
  7008. && size == 1) {
  7009. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7010. "with %02x: ",
  7011. original_opcode[0]);
  7012. opcode = original_opcode[0];
  7013. goto op_break_retry;
  7014. }
  7015. }
  7016. break;
  7017. }
  7018. #endif
  7019. default:
  7020. return false;
  7021. }
  7022. }
  7023. (void)u8;
  7024. (void)exec_env;
  7025. return false;
  7026. fail:
  7027. return false;
  7028. }
  7029. #if WASM_ENABLE_FAST_INTERP != 0
  7030. #if WASM_DEBUG_PREPROCESSOR != 0
  7031. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7032. #else
  7033. #define LOG_OP(...) (void)0
  7034. #endif
  7035. #define PATCH_ELSE 0
  7036. #define PATCH_END 1
  7037. typedef struct BranchBlockPatch {
  7038. struct BranchBlockPatch *next;
  7039. uint8 patch_type;
  7040. uint8 *code_compiled;
  7041. } BranchBlockPatch;
  7042. #endif
  7043. typedef struct BranchBlock {
  7044. uint8 label_type;
  7045. BlockType block_type;
  7046. uint8 *start_addr;
  7047. uint8 *else_addr;
  7048. uint8 *end_addr;
  7049. uint32 stack_cell_num;
  7050. #if WASM_ENABLE_GC != 0
  7051. uint32 reftype_map_num;
  7052. /* Indicate which local is used inside current block, used to validate
  7053. * local.get with non-nullable ref types */
  7054. uint8 *local_use_mask;
  7055. uint32 local_use_mask_size;
  7056. #endif
  7057. #if WASM_ENABLE_FAST_INTERP != 0
  7058. uint16 dynamic_offset;
  7059. uint8 *code_compiled;
  7060. BranchBlockPatch *patch_list;
  7061. /* This is used to save params frame_offset of of if block */
  7062. int16 *param_frame_offsets;
  7063. /* This is used to recover the dynamic offset for else branch,
  7064. * and also to remember the start offset of dynamic space which
  7065. * stores the block arguments for loop block, so we can use it
  7066. * to copy the stack operands to the loop block's arguments in
  7067. * wasm_loader_emit_br_info for opcode br. */
  7068. uint16 start_dynamic_offset;
  7069. #endif
  7070. /* Indicate the operand stack is in polymorphic state.
  7071. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7072. * to polymorphic state until the block's 'end' opcode is processed.
  7073. * If stack is in polymorphic state and stack is empty, instruction can
  7074. * pop any type of value directly without decreasing stack top pointer
  7075. * and stack cell num. */
  7076. bool is_stack_polymorphic;
  7077. } BranchBlock;
  7078. typedef struct WASMLoaderContext {
  7079. /* frame ref stack */
  7080. uint8 *frame_ref;
  7081. uint8 *frame_ref_bottom;
  7082. uint8 *frame_ref_boundary;
  7083. uint32 frame_ref_size;
  7084. uint32 stack_cell_num;
  7085. uint32 max_stack_cell_num;
  7086. #if WASM_ENABLE_GC != 0
  7087. /* frame reftype map stack */
  7088. WASMRefTypeMap *frame_reftype_map;
  7089. WASMRefTypeMap *frame_reftype_map_bottom;
  7090. WASMRefTypeMap *frame_reftype_map_boundary;
  7091. uint32 frame_reftype_map_size;
  7092. uint32 reftype_map_num;
  7093. uint32 max_reftype_map_num;
  7094. /* Current module */
  7095. WASMModule *module;
  7096. /* Current module's ref_type_set */
  7097. HashMap *ref_type_set;
  7098. /* Always point to local variable ref_type of
  7099. wasm_loader_prepare_bytecode */
  7100. WASMRefType *ref_type_tmp;
  7101. #endif
  7102. /* frame csp stack */
  7103. BranchBlock *frame_csp;
  7104. BranchBlock *frame_csp_bottom;
  7105. BranchBlock *frame_csp_boundary;
  7106. uint32 frame_csp_size;
  7107. uint32 csp_num;
  7108. uint32 max_csp_num;
  7109. #if WASM_ENABLE_FAST_INTERP != 0
  7110. /* frame offset stack */
  7111. int16 *frame_offset;
  7112. int16 *frame_offset_bottom;
  7113. int16 *frame_offset_boundary;
  7114. uint32 frame_offset_size;
  7115. int16 dynamic_offset;
  7116. int16 start_dynamic_offset;
  7117. int16 max_dynamic_offset;
  7118. /* preserved local offset */
  7119. int16 preserved_local_offset;
  7120. /* const buffer */
  7121. uint8 *const_buf;
  7122. uint16 num_const;
  7123. uint16 const_cell_num;
  7124. uint32 const_buf_size;
  7125. /* processed code */
  7126. uint8 *p_code_compiled;
  7127. uint8 *p_code_compiled_end;
  7128. uint32 code_compiled_size;
  7129. /* If the last opcode will be dropped, the peak memory usage will be larger
  7130. * than the final code_compiled_size, we record the peak size to ensure
  7131. * there will not be invalid memory access during second traverse */
  7132. uint32 code_compiled_peak_size;
  7133. #endif
  7134. } WASMLoaderContext;
  7135. typedef struct Const {
  7136. WASMValue value;
  7137. uint16 slot_index;
  7138. uint8 value_type;
  7139. } Const;
  7140. #define CHECK_CSP_PUSH() \
  7141. do { \
  7142. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7143. MEM_REALLOC( \
  7144. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7145. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7146. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7147. ctx->frame_csp_boundary = \
  7148. ctx->frame_csp_bottom \
  7149. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7150. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7151. } \
  7152. } while (0)
  7153. #define CHECK_CSP_POP() \
  7154. do { \
  7155. if (ctx->csp_num < 1) { \
  7156. set_error_buf(error_buf, error_buf_size, \
  7157. "type mismatch: " \
  7158. "expect data but block stack was empty"); \
  7159. goto fail; \
  7160. } \
  7161. } while (0)
  7162. #if WASM_ENABLE_FAST_INTERP != 0
  7163. static bool
  7164. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7165. uint32 error_buf_size)
  7166. {
  7167. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7168. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7169. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7170. ctx->frame_offset_size + 16);
  7171. ctx->frame_offset_size += 16;
  7172. ctx->frame_offset_boundary =
  7173. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7174. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7175. }
  7176. return true;
  7177. fail:
  7178. return false;
  7179. }
  7180. static bool
  7181. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7182. {
  7183. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7184. return false;
  7185. return true;
  7186. }
  7187. static void
  7188. free_label_patch_list(BranchBlock *frame_csp)
  7189. {
  7190. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7191. BranchBlockPatch *next;
  7192. while (label_patch != NULL) {
  7193. next = label_patch->next;
  7194. wasm_runtime_free(label_patch);
  7195. label_patch = next;
  7196. }
  7197. frame_csp->patch_list = NULL;
  7198. }
  7199. static void
  7200. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7201. {
  7202. BranchBlock *tmp_csp = frame_csp;
  7203. uint32 i;
  7204. for (i = 0; i < csp_num; i++) {
  7205. free_label_patch_list(tmp_csp);
  7206. tmp_csp++;
  7207. }
  7208. }
  7209. static void
  7210. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7211. {
  7212. BranchBlock *tmp_csp = frame_csp;
  7213. uint32 i;
  7214. for (i = 0; i < csp_num; i++) {
  7215. if (tmp_csp->param_frame_offsets)
  7216. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7217. tmp_csp++;
  7218. }
  7219. }
  7220. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7221. #if WASM_ENABLE_GC != 0
  7222. static bool
  7223. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7224. char *error_buf, uint32 error_buf_size)
  7225. {
  7226. BranchBlock *current_csp = ctx->frame_csp - 1;
  7227. uint32 local_mask_size;
  7228. if (local_count == 0) {
  7229. current_csp->local_use_mask_size = 0;
  7230. return true;
  7231. }
  7232. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7233. * else branch, we don't need to allocate memory again */
  7234. if (!current_csp->local_use_mask) {
  7235. local_mask_size = (local_count + 7) / sizeof(uint8);
  7236. if (!(current_csp->local_use_mask =
  7237. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7238. return false;
  7239. }
  7240. current_csp->local_use_mask_size = local_mask_size;
  7241. }
  7242. else {
  7243. local_mask_size = current_csp->local_use_mask_size;
  7244. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7245. }
  7246. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7247. /* For non-function blocks, inherit the use status from parent block */
  7248. BranchBlock *parent_csp = current_csp - 1;
  7249. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7250. bh_assert(parent_csp->local_use_mask);
  7251. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7252. parent_csp->local_use_mask, local_mask_size);
  7253. }
  7254. return true;
  7255. }
  7256. static void
  7257. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7258. {
  7259. BranchBlock *current_csp = ctx->frame_csp - 1;
  7260. bh_assert(current_csp->local_use_mask
  7261. || current_csp->local_use_mask_size == 0);
  7262. if (current_csp->local_use_mask) {
  7263. wasm_runtime_free(current_csp->local_use_mask);
  7264. }
  7265. current_csp->local_use_mask = NULL;
  7266. current_csp->local_use_mask_size = 0;
  7267. }
  7268. static void
  7269. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7270. {
  7271. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7272. uint32 i;
  7273. for (i = 0; i < ctx->csp_num; i++) {
  7274. if (tmp_csp->local_use_mask) {
  7275. wasm_runtime_free(tmp_csp->local_use_mask);
  7276. tmp_csp->local_use_mask = NULL;
  7277. tmp_csp->local_use_mask_size = 0;
  7278. }
  7279. tmp_csp++;
  7280. }
  7281. }
  7282. static void
  7283. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7284. {
  7285. BranchBlock *current_csp = ctx->frame_csp - 1;
  7286. uint32 byte_offset = index / sizeof(uint8);
  7287. uint32 bit_offset = index % sizeof(uint8);
  7288. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7289. bh_assert(current_csp->local_use_mask);
  7290. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7291. }
  7292. static bool
  7293. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7294. {
  7295. BranchBlock *current_csp = ctx->frame_csp - 1;
  7296. uint32 byte_offset = index / sizeof(uint8);
  7297. uint32 bit_offset = index % sizeof(uint8);
  7298. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7299. bh_assert(current_csp->local_use_mask);
  7300. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7301. ? true
  7302. : false;
  7303. }
  7304. #endif /* end of WASM_ENABLE_GC != 0 */
  7305. static void
  7306. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7307. {
  7308. if (ctx) {
  7309. if (ctx->frame_ref_bottom)
  7310. wasm_runtime_free(ctx->frame_ref_bottom);
  7311. #if WASM_ENABLE_GC != 0
  7312. if (ctx->frame_reftype_map_bottom)
  7313. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7314. #endif
  7315. if (ctx->frame_csp_bottom) {
  7316. #if WASM_ENABLE_FAST_INTERP != 0
  7317. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7318. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7319. ctx->csp_num);
  7320. #endif
  7321. #if WASM_ENABLE_GC != 0
  7322. wasm_loader_clean_all_local_use_masks(ctx);
  7323. #endif
  7324. wasm_runtime_free(ctx->frame_csp_bottom);
  7325. }
  7326. #if WASM_ENABLE_FAST_INTERP != 0
  7327. if (ctx->frame_offset_bottom)
  7328. wasm_runtime_free(ctx->frame_offset_bottom);
  7329. if (ctx->const_buf)
  7330. wasm_runtime_free(ctx->const_buf);
  7331. #endif
  7332. wasm_runtime_free(ctx);
  7333. }
  7334. }
  7335. static WASMLoaderContext *
  7336. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7337. {
  7338. WASMLoaderContext *loader_ctx =
  7339. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7340. if (!loader_ctx)
  7341. return NULL;
  7342. loader_ctx->frame_ref_size = 32;
  7343. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7344. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7345. goto fail;
  7346. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7347. #if WASM_ENABLE_GC != 0
  7348. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7349. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7350. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7351. error_buf_size)))
  7352. goto fail;
  7353. loader_ctx->frame_reftype_map_boundary =
  7354. loader_ctx->frame_reftype_map_bottom + 16;
  7355. #endif
  7356. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7357. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7358. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7359. goto fail;
  7360. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7361. #if WASM_ENABLE_EXCE_HANDLING != 0
  7362. func->exception_handler_count = 0;
  7363. #endif
  7364. #if WASM_ENABLE_FAST_INTERP != 0
  7365. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7366. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7367. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7368. error_buf_size)))
  7369. goto fail;
  7370. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7371. loader_ctx->num_const = 0;
  7372. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7373. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7374. error_buf, error_buf_size)))
  7375. goto fail;
  7376. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7377. set_error_buf(error_buf, error_buf_size,
  7378. "fast interpreter offset overflow");
  7379. goto fail;
  7380. }
  7381. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7382. loader_ctx->max_dynamic_offset =
  7383. func->param_cell_num + func->local_cell_num;
  7384. #endif
  7385. return loader_ctx;
  7386. fail:
  7387. wasm_loader_ctx_destroy(loader_ctx);
  7388. return NULL;
  7389. }
  7390. static bool
  7391. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7392. uint32 error_buf_size)
  7393. {
  7394. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7395. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7396. /* Increase the frame ref stack */
  7397. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7398. ctx->frame_ref_size + 16);
  7399. ctx->frame_ref_size += 16;
  7400. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7401. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7402. }
  7403. #if WASM_ENABLE_GC != 0
  7404. if (wasm_is_type_multi_byte_type(type)
  7405. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7406. /* Increase the frame reftype map stack */
  7407. bh_assert(
  7408. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7409. * sizeof(WASMRefTypeMap))
  7410. == ctx->frame_reftype_map_size);
  7411. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7412. ctx->frame_reftype_map_size
  7413. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7414. ctx->frame_reftype_map =
  7415. ctx->frame_reftype_map_bottom
  7416. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7417. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7418. ctx->frame_reftype_map_boundary =
  7419. ctx->frame_reftype_map_bottom
  7420. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7421. }
  7422. #endif
  7423. return true;
  7424. fail:
  7425. return false;
  7426. }
  7427. static bool
  7428. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7429. uint32 error_buf_size)
  7430. {
  7431. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7432. uint32 i;
  7433. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7434. return false;
  7435. #if WASM_ENABLE_GC != 0
  7436. if (wasm_is_type_multi_byte_type(type)) {
  7437. WASMRefType *ref_type;
  7438. if (!(ref_type =
  7439. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7440. error_buf, error_buf_size))) {
  7441. return false;
  7442. }
  7443. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7444. /* Increase the frame reftype map stack */
  7445. bh_assert((uint32)((ctx->frame_reftype_map
  7446. - ctx->frame_reftype_map_bottom)
  7447. * sizeof(WASMRefTypeMap))
  7448. == ctx->frame_reftype_map_size);
  7449. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7450. ctx->frame_reftype_map_size,
  7451. ctx->frame_reftype_map_size
  7452. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7453. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7454. + ctx->frame_reftype_map_size
  7455. / ((uint32)sizeof(WASMRefTypeMap));
  7456. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7457. ctx->frame_reftype_map_boundary =
  7458. ctx->frame_reftype_map_bottom
  7459. + ctx->frame_reftype_map_size
  7460. / ((uint32)sizeof(WASMRefTypeMap));
  7461. }
  7462. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7463. ctx->frame_reftype_map->ref_type = ref_type;
  7464. ctx->frame_reftype_map++;
  7465. ctx->reftype_map_num++;
  7466. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7467. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7468. }
  7469. #endif
  7470. for (i = 0; i < type_cell_num; i++)
  7471. *ctx->frame_ref++ = type;
  7472. ctx->stack_cell_num += type_cell_num;
  7473. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7474. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7475. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7476. set_error_buf(error_buf, error_buf_size,
  7477. "operand stack depth limit exceeded");
  7478. return false;
  7479. }
  7480. }
  7481. return true;
  7482. #if WASM_ENABLE_GC != 0
  7483. fail:
  7484. return false;
  7485. #endif
  7486. }
  7487. static bool
  7488. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7489. int32 stack_cell_num,
  7490. #if WASM_ENABLE_GC != 0
  7491. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7492. #endif
  7493. uint8 type,
  7494. #if WASM_ENABLE_GC != 0
  7495. WASMRefType *ref_type,
  7496. #endif
  7497. char *error_buf, uint32 error_buf_size)
  7498. {
  7499. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7500. #if WASM_ENABLE_GC != 0
  7501. WASMRefType *frame_reftype = NULL;
  7502. #endif
  7503. if (stack_cell_num < type_cell_num) {
  7504. set_error_buf(error_buf, error_buf_size,
  7505. "type mismatch: expect data but stack was empty");
  7506. return false;
  7507. }
  7508. #if WASM_ENABLE_GC == 0
  7509. for (i = 0; i < type_cell_num; i++) {
  7510. if (*(frame_ref - 1 - i) != type) {
  7511. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7512. "type mismatch: expect ", type2str(type),
  7513. " but got other");
  7514. return false;
  7515. }
  7516. }
  7517. #else
  7518. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7519. bh_assert(reftype_map_num > 0);
  7520. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7521. }
  7522. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7523. ref_type, ctx->module->types,
  7524. ctx->module->type_count)) {
  7525. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7526. "type mismatch: expect ", type2str(type),
  7527. " but got other");
  7528. return false;
  7529. }
  7530. for (i = 0; i < type_cell_num - 1; i++) {
  7531. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7532. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7533. "type mismatch: expect ", type2str(type),
  7534. " but got other");
  7535. return false;
  7536. }
  7537. }
  7538. #endif
  7539. return true;
  7540. }
  7541. static bool
  7542. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7543. uint32 error_buf_size)
  7544. {
  7545. int32 block_stack_cell_num =
  7546. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7547. #if WASM_ENABLE_GC != 0
  7548. int32 reftype_map_num =
  7549. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7550. #endif
  7551. if (block_stack_cell_num > 0) {
  7552. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7553. /* the stack top is a value of any type, return success */
  7554. return true;
  7555. }
  7556. #if WASM_ENABLE_GC != 0
  7557. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7558. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7559. WASMRefType *stack_top_ref_type = NULL;
  7560. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7561. bh_assert(reftype_map_num > 0);
  7562. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7563. }
  7564. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7565. ctx->ref_type_tmp, ctx->module->types,
  7566. ctx->module->type_count)) {
  7567. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7568. uint32 ref_type_struct_size =
  7569. wasm_reftype_struct_size(stack_top_ref_type);
  7570. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7571. stack_top_ref_type, ref_type_struct_size);
  7572. }
  7573. return true;
  7574. }
  7575. }
  7576. #endif
  7577. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7578. #if WASM_ENABLE_GC != 0
  7579. ctx->frame_reftype_map, reftype_map_num,
  7580. #endif
  7581. type,
  7582. #if WASM_ENABLE_GC != 0
  7583. ctx->ref_type_tmp,
  7584. #endif
  7585. error_buf, error_buf_size)) {
  7586. return false;
  7587. }
  7588. return true;
  7589. }
  7590. static bool
  7591. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7592. uint32 error_buf_size)
  7593. {
  7594. BranchBlock *cur_block = ctx->frame_csp - 1;
  7595. int32 available_stack_cell =
  7596. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7597. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7598. /* Directly return success if current block is in stack
  7599. polymorphic state while stack is empty. */
  7600. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7601. return true;
  7602. if (type == VALUE_TYPE_VOID)
  7603. return true;
  7604. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7605. return false;
  7606. bh_assert(available_stack_cell > 0);
  7607. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7608. type = VALUE_TYPE_ANY;
  7609. cell_num_to_pop = 1;
  7610. }
  7611. ctx->frame_ref -= cell_num_to_pop;
  7612. ctx->stack_cell_num -= cell_num_to_pop;
  7613. #if WASM_ENABLE_GC != 0
  7614. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7615. ctx->frame_reftype_map--;
  7616. ctx->reftype_map_num--;
  7617. }
  7618. #endif
  7619. return true;
  7620. }
  7621. #if WASM_ENABLE_GC != 0
  7622. /* Get the stack top element of current block */
  7623. static bool
  7624. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7625. WASMRefType **p_ref_type, char *error_buf,
  7626. uint32 error_buf_size)
  7627. {
  7628. BranchBlock *cur_block = ctx->frame_csp - 1;
  7629. int32 available_stack_cell =
  7630. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7631. if (available_stack_cell <= 0) {
  7632. /* Directly return success if current block is in stack
  7633. polymorphic state while stack is empty. */
  7634. if (cur_block->is_stack_polymorphic) {
  7635. *p_type = VALUE_TYPE_ANY;
  7636. return true;
  7637. }
  7638. else {
  7639. set_error_buf(
  7640. error_buf, error_buf_size,
  7641. "type mismatch: expect data but block stack was empty");
  7642. return false;
  7643. }
  7644. }
  7645. *p_type = *(ctx->frame_ref - 1);
  7646. if (wasm_is_type_multi_byte_type(*p_type)) {
  7647. int32 available_reftype_map =
  7648. (int32)(ctx->reftype_map_num
  7649. - (ctx->frame_csp - 1)->reftype_map_num);
  7650. bh_assert(available_reftype_map > 0);
  7651. (void)available_reftype_map;
  7652. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7653. }
  7654. return true;
  7655. }
  7656. #if WASM_ENABLE_FAST_INTERP != 0
  7657. static bool
  7658. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7659. char *error_buf, uint32 error_buf_size);
  7660. #endif
  7661. /* Check whether the stack top elem is a heap object, and if yes,
  7662. pop and return it */
  7663. static bool
  7664. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7665. WASMRefType *ref_ht_ret, char *error_buf,
  7666. uint32 error_buf_size)
  7667. {
  7668. uint8 type = 0;
  7669. WASMRefType *ref_type = NULL;
  7670. /* Get stack top element */
  7671. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7672. error_buf_size)) {
  7673. return false;
  7674. }
  7675. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7676. /* stack top isn't a ref type */
  7677. && !wasm_is_type_reftype(type)) {
  7678. set_error_buf(error_buf, error_buf_size,
  7679. "type mismatch: expect heap object but got others");
  7680. return false;
  7681. }
  7682. /* POP stack top */
  7683. if (wasm_is_type_multi_byte_type(type)) {
  7684. bh_assert(ref_type);
  7685. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7686. wasm_reftype_struct_size(ref_type));
  7687. }
  7688. #if WASM_ENABLE_FAST_INTERP != 0
  7689. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7690. error_buf_size)) {
  7691. return false;
  7692. }
  7693. #else
  7694. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7695. return false;
  7696. }
  7697. #endif
  7698. if (p_type)
  7699. *p_type = type;
  7700. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7701. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7702. wasm_reftype_struct_size(ref_type));
  7703. }
  7704. return true;
  7705. }
  7706. /* Check whether the stack top elem is subtype of (ref null ht),
  7707. and if yes, pop it and return the converted (ref ht) */
  7708. static bool
  7709. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7710. WASMRefType *ref_ht_ret, char *error_buf,
  7711. uint32 error_buf_size)
  7712. {
  7713. uint8 type = 0;
  7714. WASMRefType ref_type = { 0 };
  7715. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7716. error_buf_size)) {
  7717. return false;
  7718. }
  7719. /* Convert to related (ref ht) and return */
  7720. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7721. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7722. */
  7723. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7724. HEAP_TYPE_ARRAY
  7725. + (type - REF_TYPE_ARRAYREF));
  7726. type = ref_ht_ret->ref_type;
  7727. }
  7728. else if (wasm_is_reftype_htref_nullable(type)
  7729. || wasm_is_reftype_htref_non_nullable(type)) {
  7730. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7731. wasm_reftype_struct_size(&ref_type));
  7732. /* Convert to (ref ht) */
  7733. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7734. ref_ht_ret->ref_ht_common.nullable = false;
  7735. type = ref_ht_ret->ref_type;
  7736. }
  7737. *p_type = type;
  7738. return true;
  7739. }
  7740. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7741. and if yes, pop it and return the type_idx */
  7742. static bool
  7743. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7744. uint32 *p_type_idx, char *error_buf,
  7745. uint32 error_buf_size)
  7746. {
  7747. uint8 type = 0;
  7748. int32 type_idx = -1;
  7749. WASMRefType *ref_type = NULL;
  7750. /* Get stack top element */
  7751. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7752. error_buf_size)) {
  7753. return false;
  7754. }
  7755. if (type != VALUE_TYPE_ANY) {
  7756. /* stack top isn't (ref null $t) */
  7757. if (!((wasm_is_reftype_htref_nullable(type)
  7758. || wasm_is_reftype_htref_non_nullable(type))
  7759. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7760. set_error_buf(error_buf, error_buf_size,
  7761. "type mismatch: expect (ref null $t) but got others");
  7762. return false;
  7763. }
  7764. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7765. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7766. wasm_reftype_struct_size(ref_type));
  7767. }
  7768. /* POP stack top */
  7769. #if WASM_ENABLE_FAST_INTERP != 0
  7770. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7771. error_buf_size)) {
  7772. return false;
  7773. }
  7774. #else
  7775. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7776. return false;
  7777. }
  7778. #endif
  7779. /* Convert to type_idx and return */
  7780. *p_type = type;
  7781. if (type != VALUE_TYPE_ANY)
  7782. *p_type_idx = (uint32)type_idx;
  7783. return true;
  7784. }
  7785. #endif /* WASM_ENABLE_GC != 0 */
  7786. #if WASM_ENABLE_FAST_INTERP == 0
  7787. static bool
  7788. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7789. uint8 type_push, uint8 type_pop, char *error_buf,
  7790. uint32 error_buf_size)
  7791. {
  7792. for (int i = 0; i < pop_cnt; i++) {
  7793. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7794. error_buf_size))
  7795. return false;
  7796. }
  7797. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7798. return false;
  7799. return true;
  7800. }
  7801. #endif
  7802. static bool
  7803. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7804. BlockType block_type, uint8 *start_addr,
  7805. char *error_buf, uint32 error_buf_size)
  7806. {
  7807. CHECK_CSP_PUSH();
  7808. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7809. ctx->frame_csp->label_type = label_type;
  7810. ctx->frame_csp->block_type = block_type;
  7811. ctx->frame_csp->start_addr = start_addr;
  7812. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7813. #if WASM_ENABLE_GC != 0
  7814. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7815. #endif
  7816. #if WASM_ENABLE_FAST_INTERP != 0
  7817. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7818. ctx->frame_csp->patch_list = NULL;
  7819. #endif
  7820. ctx->frame_csp++;
  7821. ctx->csp_num++;
  7822. if (ctx->csp_num > ctx->max_csp_num) {
  7823. ctx->max_csp_num = ctx->csp_num;
  7824. if (ctx->max_csp_num > UINT16_MAX) {
  7825. set_error_buf(error_buf, error_buf_size,
  7826. "label stack depth limit exceeded");
  7827. return false;
  7828. }
  7829. }
  7830. return true;
  7831. fail:
  7832. return false;
  7833. }
  7834. static bool
  7835. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7836. uint32 error_buf_size)
  7837. {
  7838. CHECK_CSP_POP();
  7839. #if WASM_ENABLE_FAST_INTERP != 0
  7840. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7841. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7842. #endif
  7843. ctx->frame_csp--;
  7844. ctx->csp_num--;
  7845. return true;
  7846. fail:
  7847. return false;
  7848. }
  7849. #if WASM_ENABLE_FAST_INTERP != 0
  7850. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7851. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7852. #define emit_label(opcode) \
  7853. do { \
  7854. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7855. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7856. } while (0)
  7857. #define skip_label() \
  7858. do { \
  7859. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7860. LOG_OP("\ndelete last op\n"); \
  7861. } while (0)
  7862. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7863. #if UINTPTR_MAX == UINT64_MAX
  7864. #define emit_label(opcode) \
  7865. do { \
  7866. int32 offset = \
  7867. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7868. /* emit int32 relative offset in 64-bit target */ \
  7869. wasm_loader_emit_uint32(loader_ctx, offset); \
  7870. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7871. } while (0)
  7872. #else
  7873. #define emit_label(opcode) \
  7874. do { \
  7875. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7876. /* emit uint32 label address in 32-bit target */ \
  7877. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7878. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7879. } while (0)
  7880. #endif
  7881. #define skip_label() \
  7882. do { \
  7883. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7884. LOG_OP("\ndelete last op\n"); \
  7885. } while (0)
  7886. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7887. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7888. #define emit_label(opcode) \
  7889. do { \
  7890. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7891. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7892. } while (0)
  7893. #define skip_label() \
  7894. do { \
  7895. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7896. LOG_OP("\ndelete last op\n"); \
  7897. } while (0)
  7898. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7899. #define emit_empty_label_addr_and_frame_ip(type) \
  7900. do { \
  7901. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7902. loader_ctx->p_code_compiled, error_buf, \
  7903. error_buf_size)) \
  7904. goto fail; \
  7905. /* label address, to be patched */ \
  7906. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7907. } while (0)
  7908. #define emit_br_info(frame_csp, is_br) \
  7909. do { \
  7910. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7911. error_buf_size)) \
  7912. goto fail; \
  7913. } while (0)
  7914. #define LAST_OP_OUTPUT_I32() \
  7915. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7916. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7917. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7918. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7919. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7920. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7921. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7922. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7923. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7924. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7925. || (last_op == EXT_OP_COPY_STACK_TOP)
  7926. #define LAST_OP_OUTPUT_I64() \
  7927. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7928. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7929. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7930. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7931. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7932. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7933. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7934. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7935. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7936. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7937. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7938. #define GET_CONST_OFFSET(type, val) \
  7939. do { \
  7940. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7941. &operand_offset, error_buf, \
  7942. error_buf_size))) \
  7943. goto fail; \
  7944. } while (0)
  7945. #define GET_CONST_F32_OFFSET(type, fval) \
  7946. do { \
  7947. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7948. &operand_offset, error_buf, \
  7949. error_buf_size))) \
  7950. goto fail; \
  7951. } while (0)
  7952. #define GET_CONST_F64_OFFSET(type, fval) \
  7953. do { \
  7954. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7955. &operand_offset, error_buf, \
  7956. error_buf_size))) \
  7957. goto fail; \
  7958. } while (0)
  7959. #define emit_operand(ctx, offset) \
  7960. do { \
  7961. wasm_loader_emit_int16(ctx, offset); \
  7962. LOG_OP("%d\t", offset); \
  7963. } while (0)
  7964. #define emit_byte(ctx, byte) \
  7965. do { \
  7966. wasm_loader_emit_uint8(ctx, byte); \
  7967. LOG_OP("%d\t", byte); \
  7968. } while (0)
  7969. #define emit_uint32(ctx, value) \
  7970. do { \
  7971. wasm_loader_emit_uint32(ctx, value); \
  7972. LOG_OP("%d\t", value); \
  7973. } while (0)
  7974. #define emit_uint64(ctx, value) \
  7975. do { \
  7976. wasm_loader_emit_const(ctx, &value, false); \
  7977. LOG_OP("%lld\t", value); \
  7978. } while (0)
  7979. #define emit_float32(ctx, value) \
  7980. do { \
  7981. wasm_loader_emit_const(ctx, &value, true); \
  7982. LOG_OP("%f\t", value); \
  7983. } while (0)
  7984. #define emit_float64(ctx, value) \
  7985. do { \
  7986. wasm_loader_emit_const(ctx, &value, false); \
  7987. LOG_OP("%f\t", value); \
  7988. } while (0)
  7989. static bool
  7990. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7991. {
  7992. if (!(ctx->p_code_compiled =
  7993. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7994. return false;
  7995. ctx->p_code_compiled_end =
  7996. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7997. /* clean up frame ref */
  7998. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7999. ctx->frame_ref = ctx->frame_ref_bottom;
  8000. ctx->stack_cell_num = 0;
  8001. #if WASM_ENABLE_GC != 0
  8002. /* clean up reftype map */
  8003. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8004. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8005. ctx->reftype_map_num = 0;
  8006. #endif
  8007. /* clean up frame csp */
  8008. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8009. ctx->frame_csp = ctx->frame_csp_bottom;
  8010. ctx->csp_num = 0;
  8011. ctx->max_csp_num = 0;
  8012. /* clean up frame offset */
  8013. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8014. ctx->frame_offset = ctx->frame_offset_bottom;
  8015. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8016. /* init preserved local offsets */
  8017. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8018. /* const buf is reserved */
  8019. return true;
  8020. }
  8021. static void
  8022. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8023. {
  8024. ctx->code_compiled_size += size;
  8025. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8026. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8027. }
  8028. }
  8029. static void
  8030. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8031. {
  8032. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8033. if (ctx->p_code_compiled) {
  8034. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8035. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8036. #endif
  8037. bh_memcpy_s(ctx->p_code_compiled,
  8038. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8039. value, size);
  8040. ctx->p_code_compiled += size;
  8041. }
  8042. else {
  8043. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8044. bh_assert((ctx->code_compiled_size & 1) == 0);
  8045. #endif
  8046. increase_compiled_code_space(ctx, size);
  8047. }
  8048. }
  8049. static void
  8050. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8051. {
  8052. if (ctx->p_code_compiled) {
  8053. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8054. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8055. #endif
  8056. STORE_U32(ctx->p_code_compiled, value);
  8057. ctx->p_code_compiled += sizeof(uint32);
  8058. }
  8059. else {
  8060. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8061. bh_assert((ctx->code_compiled_size & 1) == 0);
  8062. #endif
  8063. increase_compiled_code_space(ctx, sizeof(uint32));
  8064. }
  8065. }
  8066. static void
  8067. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8068. {
  8069. if (ctx->p_code_compiled) {
  8070. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8071. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8072. #endif
  8073. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8074. ctx->p_code_compiled += sizeof(int16);
  8075. }
  8076. else {
  8077. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8078. bh_assert((ctx->code_compiled_size & 1) == 0);
  8079. #endif
  8080. increase_compiled_code_space(ctx, sizeof(uint16));
  8081. }
  8082. }
  8083. static void
  8084. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8085. {
  8086. if (ctx->p_code_compiled) {
  8087. *(ctx->p_code_compiled) = value;
  8088. ctx->p_code_compiled += sizeof(uint8);
  8089. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8090. ctx->p_code_compiled++;
  8091. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8092. #endif
  8093. }
  8094. else {
  8095. increase_compiled_code_space(ctx, sizeof(uint8));
  8096. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8097. increase_compiled_code_space(ctx, sizeof(uint8));
  8098. bh_assert((ctx->code_compiled_size & 1) == 0);
  8099. #endif
  8100. }
  8101. }
  8102. static void
  8103. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8104. {
  8105. if (ctx->p_code_compiled) {
  8106. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8107. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8108. #endif
  8109. STORE_PTR(ctx->p_code_compiled, value);
  8110. ctx->p_code_compiled += sizeof(void *);
  8111. }
  8112. else {
  8113. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8114. bh_assert((ctx->code_compiled_size & 1) == 0);
  8115. #endif
  8116. increase_compiled_code_space(ctx, sizeof(void *));
  8117. }
  8118. }
  8119. static void
  8120. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8121. {
  8122. if (ctx->p_code_compiled) {
  8123. ctx->p_code_compiled -= size;
  8124. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8125. if (size == sizeof(uint8)) {
  8126. ctx->p_code_compiled--;
  8127. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8128. }
  8129. #endif
  8130. }
  8131. else {
  8132. ctx->code_compiled_size -= size;
  8133. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8134. if (size == sizeof(uint8)) {
  8135. ctx->code_compiled_size--;
  8136. bh_assert((ctx->code_compiled_size & 1) == 0);
  8137. }
  8138. #endif
  8139. }
  8140. }
  8141. static bool
  8142. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8143. uint32 local_index, uint32 local_type,
  8144. bool *preserved, char *error_buf,
  8145. uint32 error_buf_size)
  8146. {
  8147. uint32 i = 0;
  8148. int16 preserved_offset = (int16)local_index;
  8149. *preserved = false;
  8150. while (i < loader_ctx->stack_cell_num) {
  8151. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8152. /* move previous local into dynamic space before a set/tee_local opcode
  8153. */
  8154. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8155. if (!(*preserved)) {
  8156. *preserved = true;
  8157. skip_label();
  8158. preserved_offset = loader_ctx->preserved_local_offset;
  8159. if (loader_ctx->p_code_compiled) {
  8160. bh_assert(preserved_offset != (int16)local_index);
  8161. }
  8162. if (is_32bit_type(local_type)) {
  8163. /* Only increase preserve offset in the second traversal */
  8164. if (loader_ctx->p_code_compiled)
  8165. loader_ctx->preserved_local_offset++;
  8166. emit_label(EXT_OP_COPY_STACK_TOP);
  8167. }
  8168. else {
  8169. if (loader_ctx->p_code_compiled)
  8170. loader_ctx->preserved_local_offset += 2;
  8171. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8172. }
  8173. emit_operand(loader_ctx, local_index);
  8174. emit_operand(loader_ctx, preserved_offset);
  8175. emit_label(opcode);
  8176. }
  8177. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8178. }
  8179. if (is_32bit_type(cur_type))
  8180. i++;
  8181. else
  8182. i += 2;
  8183. }
  8184. (void)error_buf;
  8185. (void)error_buf_size;
  8186. return true;
  8187. }
  8188. static bool
  8189. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8190. char *error_buf, uint32 error_buf_size)
  8191. {
  8192. uint32 i = 0;
  8193. bool preserve_local;
  8194. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8195. blocks will not influence the value of these locals */
  8196. while (i < loader_ctx->stack_cell_num) {
  8197. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8198. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8199. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8200. && (cur_offset >= 0)) {
  8201. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8202. cur_type, &preserve_local,
  8203. error_buf, error_buf_size)))
  8204. return false;
  8205. }
  8206. if (is_32bit_type(cur_type)) {
  8207. i++;
  8208. }
  8209. else {
  8210. i += 2;
  8211. }
  8212. }
  8213. return true;
  8214. }
  8215. static bool
  8216. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8217. uint8 *p_code_compiled, char *error_buf,
  8218. uint32 error_buf_size)
  8219. {
  8220. BranchBlockPatch *patch =
  8221. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8222. if (!patch) {
  8223. return false;
  8224. }
  8225. patch->patch_type = patch_type;
  8226. patch->code_compiled = p_code_compiled;
  8227. if (!frame_csp->patch_list) {
  8228. frame_csp->patch_list = patch;
  8229. patch->next = NULL;
  8230. }
  8231. else {
  8232. patch->next = frame_csp->patch_list;
  8233. frame_csp->patch_list = patch;
  8234. }
  8235. return true;
  8236. }
  8237. static void
  8238. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8239. {
  8240. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8241. BranchBlockPatch *node = frame_csp->patch_list;
  8242. BranchBlockPatch *node_prev = NULL, *node_next;
  8243. if (!ctx->p_code_compiled)
  8244. return;
  8245. while (node) {
  8246. node_next = node->next;
  8247. if (node->patch_type == patch_type) {
  8248. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8249. if (node_prev == NULL) {
  8250. frame_csp->patch_list = node_next;
  8251. }
  8252. else {
  8253. node_prev->next = node_next;
  8254. }
  8255. wasm_runtime_free(node);
  8256. }
  8257. else {
  8258. node_prev = node;
  8259. }
  8260. node = node_next;
  8261. }
  8262. }
  8263. static bool
  8264. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8265. bool is_br, char *error_buf, uint32 error_buf_size)
  8266. {
  8267. /* br info layout:
  8268. * a) arity of target block
  8269. * b) total cell num of arity values
  8270. * c) each arity value's cell num
  8271. * d) each arity value's src frame offset
  8272. * e) each arity values's dst dynamic offset
  8273. * f) branch target address
  8274. *
  8275. * Note: b-e are omitted when arity is 0 so that
  8276. * interpreter can recover the br info quickly.
  8277. */
  8278. BlockType *block_type = &frame_csp->block_type;
  8279. uint8 *types = NULL, cell;
  8280. #if WASM_ENABLE_GC != 0
  8281. WASMRefTypeMap *reftype_maps;
  8282. uint32 reftype_map_count;
  8283. #endif
  8284. uint32 arity = 0;
  8285. int32 i;
  8286. int16 *frame_offset = ctx->frame_offset;
  8287. uint16 dynamic_offset;
  8288. /* Note: loop's arity is different from if and block. loop's arity is
  8289. * its parameter count while if and block arity is result count.
  8290. */
  8291. #if WASM_ENABLE_GC == 0
  8292. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8293. arity = block_type_get_param_types(block_type, &types);
  8294. else
  8295. arity = block_type_get_result_types(block_type, &types);
  8296. #else
  8297. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8298. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8299. &reftype_map_count);
  8300. else
  8301. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8302. &reftype_map_count);
  8303. #endif
  8304. /* Part a */
  8305. emit_uint32(ctx, arity);
  8306. if (arity) {
  8307. /* Part b */
  8308. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8309. /* Part c */
  8310. for (i = (int32)arity - 1; i >= 0; i--) {
  8311. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8312. emit_byte(ctx, cell);
  8313. }
  8314. /* Part d */
  8315. for (i = (int32)arity - 1; i >= 0; i--) {
  8316. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8317. frame_offset -= cell;
  8318. emit_operand(ctx, *(int16 *)(frame_offset));
  8319. }
  8320. /* Part e */
  8321. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8322. /* Use start_dynamic_offset which was set in
  8323. copy_params_to_dynamic_space */
  8324. dynamic_offset = frame_csp->start_dynamic_offset
  8325. + wasm_get_cell_num(types, arity);
  8326. else
  8327. dynamic_offset =
  8328. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8329. if (is_br)
  8330. ctx->dynamic_offset = dynamic_offset;
  8331. for (i = (int32)arity - 1; i >= 0; i--) {
  8332. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8333. dynamic_offset -= cell;
  8334. emit_operand(ctx, dynamic_offset);
  8335. }
  8336. }
  8337. /* Part f */
  8338. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8339. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8340. }
  8341. else {
  8342. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8343. error_buf, error_buf_size))
  8344. return false;
  8345. /* label address, to be patched */
  8346. wasm_loader_emit_ptr(ctx, NULL);
  8347. }
  8348. return true;
  8349. }
  8350. static bool
  8351. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8352. bool disable_emit, int16 operand_offset,
  8353. char *error_buf, uint32 error_buf_size)
  8354. {
  8355. uint32 cell_num_to_push, i;
  8356. if (type == VALUE_TYPE_VOID)
  8357. return true;
  8358. /* only check memory overflow in first traverse */
  8359. if (ctx->p_code_compiled == NULL) {
  8360. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8361. return false;
  8362. }
  8363. if (disable_emit)
  8364. *(ctx->frame_offset)++ = operand_offset;
  8365. else {
  8366. emit_operand(ctx, ctx->dynamic_offset);
  8367. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8368. ctx->dynamic_offset++;
  8369. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8370. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8371. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8372. goto fail;
  8373. }
  8374. }
  8375. }
  8376. if (is_32bit_type(type))
  8377. return true;
  8378. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8379. for (i = 0; i < cell_num_to_push; i++) {
  8380. if (ctx->p_code_compiled == NULL) {
  8381. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8382. return false;
  8383. }
  8384. ctx->frame_offset++;
  8385. if (!disable_emit) {
  8386. ctx->dynamic_offset++;
  8387. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8388. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8389. if (ctx->max_dynamic_offset >= INT16_MAX)
  8390. goto fail;
  8391. }
  8392. }
  8393. }
  8394. return true;
  8395. fail:
  8396. set_error_buf(error_buf, error_buf_size,
  8397. "fast interpreter offset overflow");
  8398. return false;
  8399. }
  8400. /* This function should be in front of wasm_loader_pop_frame_ref
  8401. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8402. will be modified by wasm_loader_pop_frame_ref */
  8403. static bool
  8404. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8405. char *error_buf, uint32 error_buf_size)
  8406. {
  8407. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8408. then current block is the function block */
  8409. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8410. BranchBlock *cur_block = ctx->frame_csp - depth;
  8411. int32 available_stack_cell =
  8412. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8413. uint32 cell_num_to_pop;
  8414. /* Directly return success if current block is in stack
  8415. polymorphic state while stack is empty. */
  8416. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8417. return true;
  8418. if (type == VALUE_TYPE_VOID)
  8419. return true;
  8420. /* Change type to ANY when the stack top is ANY, so as to avoid
  8421. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8422. two offsets */
  8423. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8424. type = VALUE_TYPE_ANY;
  8425. cell_num_to_pop = wasm_value_type_cell_num(type);
  8426. /* Check the offset stack bottom to ensure the frame offset
  8427. stack will not go underflow. But we don't thrown error
  8428. and return true here, because the error msg should be
  8429. given in wasm_loader_pop_frame_ref */
  8430. if (!check_offset_pop(ctx, cell_num_to_pop))
  8431. return true;
  8432. ctx->frame_offset -= cell_num_to_pop;
  8433. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8434. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8435. ctx->dynamic_offset -= cell_num_to_pop;
  8436. emit_operand(ctx, *(ctx->frame_offset));
  8437. (void)error_buf;
  8438. (void)error_buf_size;
  8439. return true;
  8440. }
  8441. static bool
  8442. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8443. bool disable_emit, int16 operand_offset,
  8444. char *error_buf, uint32 error_buf_size)
  8445. {
  8446. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8447. error_buf, error_buf_size)))
  8448. return false;
  8449. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8450. return false;
  8451. return true;
  8452. }
  8453. static bool
  8454. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8455. char *error_buf, uint32 error_buf_size)
  8456. {
  8457. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8458. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8459. return false;
  8460. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8461. return false;
  8462. return true;
  8463. }
  8464. static bool
  8465. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8466. uint8 type_push, uint8 type_pop,
  8467. bool disable_emit, int16 operand_offset,
  8468. char *error_buf, uint32 error_buf_size)
  8469. {
  8470. uint8 i;
  8471. for (i = 0; i < pop_cnt; i++) {
  8472. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8473. error_buf_size))
  8474. return false;
  8475. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8476. error_buf_size))
  8477. return false;
  8478. }
  8479. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8480. operand_offset, error_buf,
  8481. error_buf_size))
  8482. return false;
  8483. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8484. return false;
  8485. return true;
  8486. }
  8487. static bool
  8488. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8489. int16 *offset, char *error_buf,
  8490. uint32 error_buf_size)
  8491. {
  8492. int8 bytes_to_increase;
  8493. int16 operand_offset = 0;
  8494. Const *c;
  8495. /* Search existing constant */
  8496. for (c = (Const *)ctx->const_buf;
  8497. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8498. /* TODO: handle v128 type? */
  8499. if ((type == c->value_type)
  8500. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8501. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8502. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8503. || (type == VALUE_TYPE_FUNCREF
  8504. && *(int32 *)value == c->value.i32)
  8505. || (type == VALUE_TYPE_EXTERNREF
  8506. && *(int32 *)value == c->value.i32)
  8507. #endif
  8508. || (type == VALUE_TYPE_F64
  8509. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8510. || (type == VALUE_TYPE_F32
  8511. && (0
  8512. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8513. operand_offset = c->slot_index;
  8514. break;
  8515. }
  8516. if (is_32bit_type(c->value_type))
  8517. operand_offset += 1;
  8518. else
  8519. operand_offset += 2;
  8520. }
  8521. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8522. /* New constant, append to the const buffer */
  8523. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8524. bytes_to_increase = 2;
  8525. }
  8526. else {
  8527. bytes_to_increase = 1;
  8528. }
  8529. /* The max cell num of const buffer is 32768 since the valid index range
  8530. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8531. * full */
  8532. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8533. *offset = 0;
  8534. return true;
  8535. }
  8536. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8537. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8538. ctx->const_buf_size + 4 * sizeof(Const));
  8539. ctx->const_buf_size += 4 * sizeof(Const);
  8540. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8541. }
  8542. c->value_type = type;
  8543. switch (type) {
  8544. case VALUE_TYPE_F64:
  8545. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8546. sizeof(float64));
  8547. ctx->const_cell_num += 2;
  8548. /* The const buf will be reversed, we use the second cell */
  8549. /* of the i64/f64 const so the final offset is correct */
  8550. operand_offset++;
  8551. break;
  8552. case VALUE_TYPE_I64:
  8553. c->value.i64 = *(int64 *)value;
  8554. ctx->const_cell_num += 2;
  8555. operand_offset++;
  8556. break;
  8557. case VALUE_TYPE_F32:
  8558. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8559. sizeof(float32));
  8560. ctx->const_cell_num++;
  8561. break;
  8562. case VALUE_TYPE_I32:
  8563. c->value.i32 = *(int32 *)value;
  8564. ctx->const_cell_num++;
  8565. break;
  8566. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8567. case VALUE_TYPE_EXTERNREF:
  8568. case VALUE_TYPE_FUNCREF:
  8569. c->value.i32 = *(int32 *)value;
  8570. ctx->const_cell_num++;
  8571. break;
  8572. #endif
  8573. default:
  8574. break;
  8575. }
  8576. c->slot_index = operand_offset;
  8577. ctx->num_const++;
  8578. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8579. (int64)c->value.i64);
  8580. }
  8581. /* use negative index for const */
  8582. operand_offset = -(operand_offset + 1);
  8583. *offset = operand_offset;
  8584. return true;
  8585. fail:
  8586. return false;
  8587. }
  8588. /*
  8589. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8590. -- Mostly used for the binary / compare operation
  8591. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8592. -- Mostly used in block / control instructions
  8593. The POP will always emit the offset on the top of the frame_offset stack
  8594. PUSH can be used in two ways:
  8595. 1. directly PUSH:
  8596. PUSH_XXX();
  8597. will allocate a dynamic space and emit
  8598. 2. silent PUSH:
  8599. operand_offset = xxx; disable_emit = true;
  8600. PUSH_XXX();
  8601. only push the frame_offset stack, no emit
  8602. */
  8603. #define TEMPLATE_PUSH(Type) \
  8604. do { \
  8605. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8606. disable_emit, operand_offset, \
  8607. error_buf, error_buf_size)) \
  8608. goto fail; \
  8609. } while (0)
  8610. #define TEMPLATE_PUSH_REF(Type) \
  8611. do { \
  8612. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8613. operand_offset, error_buf, \
  8614. error_buf_size)) \
  8615. goto fail; \
  8616. } while (0)
  8617. #define TEMPLATE_POP(Type) \
  8618. do { \
  8619. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8620. error_buf, error_buf_size)) \
  8621. goto fail; \
  8622. } while (0)
  8623. #define TEMPLATE_POP_REF(Type) \
  8624. do { \
  8625. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8626. error_buf_size)) \
  8627. goto fail; \
  8628. } while (0)
  8629. #define PUSH_OFFSET_TYPE(type) \
  8630. do { \
  8631. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8632. operand_offset, error_buf, \
  8633. error_buf_size))) \
  8634. goto fail; \
  8635. } while (0)
  8636. #define POP_OFFSET_TYPE(type) \
  8637. do { \
  8638. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8639. error_buf_size))) \
  8640. goto fail; \
  8641. } while (0)
  8642. #define POP_AND_PUSH(type_pop, type_push) \
  8643. do { \
  8644. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8645. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8646. operand_offset, error_buf, error_buf_size))) \
  8647. goto fail; \
  8648. } while (0)
  8649. /* type of POPs should be the same */
  8650. #define POP2_AND_PUSH(type_pop, type_push) \
  8651. do { \
  8652. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8653. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8654. operand_offset, error_buf, error_buf_size))) \
  8655. goto fail; \
  8656. } while (0)
  8657. #else /* WASM_ENABLE_FAST_INTERP */
  8658. #define TEMPLATE_PUSH(Type) \
  8659. do { \
  8660. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8661. error_buf, error_buf_size))) \
  8662. goto fail; \
  8663. } while (0)
  8664. #define TEMPLATE_PUSH_REF(Type) \
  8665. do { \
  8666. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8667. error_buf_size))) \
  8668. goto fail; \
  8669. } while (0)
  8670. #define TEMPLATE_POP(Type) \
  8671. do { \
  8672. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8673. error_buf, error_buf_size))) \
  8674. goto fail; \
  8675. } while (0)
  8676. #define TEMPLATE_POP_REF(Type) \
  8677. do { \
  8678. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8679. error_buf_size))) \
  8680. goto fail; \
  8681. } while (0)
  8682. #define POP_AND_PUSH(type_pop, type_push) \
  8683. do { \
  8684. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8685. type_pop, error_buf, \
  8686. error_buf_size))) \
  8687. goto fail; \
  8688. } while (0)
  8689. /* type of POPs should be the same */
  8690. #define POP2_AND_PUSH(type_pop, type_push) \
  8691. do { \
  8692. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8693. type_pop, error_buf, \
  8694. error_buf_size))) \
  8695. goto fail; \
  8696. } while (0)
  8697. #endif /* WASM_ENABLE_FAST_INTERP */
  8698. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8699. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8700. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8701. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8702. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8703. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8704. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8705. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8706. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8707. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8708. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8709. #define POP_I32() TEMPLATE_POP(I32)
  8710. #define POP_F32() TEMPLATE_POP(F32)
  8711. #define POP_I64() TEMPLATE_POP(I64)
  8712. #define POP_F64() TEMPLATE_POP(F64)
  8713. #define POP_V128() TEMPLATE_POP(V128)
  8714. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8715. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8716. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8717. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8718. #if WASM_ENABLE_FAST_INTERP != 0
  8719. static bool
  8720. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8721. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8722. {
  8723. int16 operand_offset = 0;
  8724. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8725. : loader_ctx->frame_csp;
  8726. BlockType *block_type = &block->block_type;
  8727. uint8 *return_types = NULL;
  8728. #if WASM_ENABLE_GC != 0
  8729. WASMRefTypeMap *reftype_maps = NULL;
  8730. uint32 reftype_map_count;
  8731. #endif
  8732. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8733. int32 i = 0;
  8734. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8735. *frame_offset_org = NULL;
  8736. #if WASM_ENABLE_GC == 0
  8737. return_count = block_type_get_result_types(block_type, &return_types);
  8738. #else
  8739. return_count = block_type_get_result_types(
  8740. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8741. #endif
  8742. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8743. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8744. if (return_count == 1) {
  8745. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8746. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8747. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8748. /* insert op_copy before else opcode */
  8749. if (opcode == WASM_OP_ELSE)
  8750. skip_label();
  8751. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8752. : EXT_OP_COPY_STACK_TOP_I64);
  8753. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8754. emit_operand(loader_ctx, block->dynamic_offset);
  8755. if (opcode == WASM_OP_ELSE) {
  8756. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8757. }
  8758. else {
  8759. loader_ctx->frame_offset -= cell;
  8760. loader_ctx->dynamic_offset = block->dynamic_offset;
  8761. PUSH_OFFSET_TYPE(return_types[0]);
  8762. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8763. }
  8764. if (opcode == WASM_OP_ELSE)
  8765. emit_label(opcode);
  8766. }
  8767. return true;
  8768. }
  8769. /* Copy stack top values to block's results which are in dynamic space.
  8770. * The instruction format:
  8771. * Part a: values count
  8772. * Part b: all values total cell num
  8773. * Part c: each value's cell_num, src offset and dst offset
  8774. * Part d: each value's src offset and dst offset
  8775. * Part e: each value's dst offset
  8776. */
  8777. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8778. dynamic_offset = dynamic_offset_org =
  8779. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8780. /* First traversal to get the count of values needed to be copied. */
  8781. for (i = (int32)return_count - 1; i >= 0; i--) {
  8782. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8783. frame_offset -= cells;
  8784. dynamic_offset -= cells;
  8785. if (dynamic_offset != *frame_offset) {
  8786. value_count++;
  8787. total_cel_num += cells;
  8788. }
  8789. }
  8790. if (value_count) {
  8791. uint32 j = 0;
  8792. uint8 *emit_data = NULL, *cells = NULL;
  8793. int16 *src_offsets = NULL;
  8794. uint16 *dst_offsets = NULL;
  8795. uint64 size =
  8796. (uint64)value_count
  8797. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8798. /* Allocate memory for the emit data */
  8799. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8800. return false;
  8801. cells = emit_data;
  8802. src_offsets = (int16 *)(cells + value_count);
  8803. dst_offsets = (uint16 *)(src_offsets + value_count);
  8804. /* insert op_copy before else opcode */
  8805. if (opcode == WASM_OP_ELSE)
  8806. skip_label();
  8807. emit_label(EXT_OP_COPY_STACK_VALUES);
  8808. /* Part a) */
  8809. emit_uint32(loader_ctx, value_count);
  8810. /* Part b) */
  8811. emit_uint32(loader_ctx, total_cel_num);
  8812. /* Second traversal to get each value's cell num, src offset and dst
  8813. * offset. */
  8814. frame_offset = frame_offset_org;
  8815. dynamic_offset = dynamic_offset_org;
  8816. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8817. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8818. frame_offset -= cell;
  8819. dynamic_offset -= cell;
  8820. if (dynamic_offset != *frame_offset) {
  8821. /* cell num */
  8822. cells[j] = cell;
  8823. /* src offset */
  8824. src_offsets[j] = *frame_offset;
  8825. /* dst offset */
  8826. dst_offsets[j] = dynamic_offset;
  8827. j++;
  8828. }
  8829. if (opcode == WASM_OP_ELSE) {
  8830. *frame_offset = dynamic_offset;
  8831. }
  8832. else {
  8833. loader_ctx->frame_offset = frame_offset;
  8834. loader_ctx->dynamic_offset = dynamic_offset;
  8835. PUSH_OFFSET_TYPE(return_types[i]);
  8836. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8837. loader_ctx->frame_offset = frame_offset_org;
  8838. loader_ctx->dynamic_offset = dynamic_offset_org;
  8839. }
  8840. }
  8841. bh_assert(j == value_count);
  8842. /* Emit the cells, src_offsets and dst_offsets */
  8843. for (j = 0; j < value_count; j++)
  8844. emit_byte(loader_ctx, cells[j]);
  8845. for (j = 0; j < value_count; j++)
  8846. emit_operand(loader_ctx, src_offsets[j]);
  8847. for (j = 0; j < value_count; j++)
  8848. emit_operand(loader_ctx, dst_offsets[j]);
  8849. if (opcode == WASM_OP_ELSE)
  8850. emit_label(opcode);
  8851. wasm_runtime_free(emit_data);
  8852. }
  8853. return true;
  8854. fail:
  8855. return false;
  8856. }
  8857. #endif /* WASM_ENABLE_FAST_INTERP */
  8858. #define RESERVE_BLOCK_RET() \
  8859. do { \
  8860. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8861. error_buf_size)) \
  8862. goto fail; \
  8863. } while (0)
  8864. #define PUSH_TYPE(type) \
  8865. do { \
  8866. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8867. error_buf_size))) \
  8868. goto fail; \
  8869. } while (0)
  8870. #define POP_TYPE(type) \
  8871. do { \
  8872. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8873. error_buf_size))) \
  8874. goto fail; \
  8875. } while (0)
  8876. #if WASM_ENABLE_GC == 0
  8877. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8878. do { \
  8879. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8880. _start_addr, error_buf, \
  8881. error_buf_size)) \
  8882. goto fail; \
  8883. } while (0)
  8884. #define POP_CSP() \
  8885. do { \
  8886. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8887. goto fail; \
  8888. } while (0)
  8889. #else
  8890. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8891. do { \
  8892. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8893. _start_addr, error_buf, \
  8894. error_buf_size)) \
  8895. goto fail; \
  8896. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8897. error_buf, error_buf_size)) { \
  8898. goto fail; \
  8899. } \
  8900. } while (0)
  8901. #define POP_CSP() \
  8902. do { \
  8903. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8904. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8905. goto fail; \
  8906. } while (0)
  8907. #endif /* end of WASM_ENABLE_GC == 0 */
  8908. #if WASM_ENABLE_GC == 0
  8909. #define GET_LOCAL_REFTYPE() (void)0
  8910. #else
  8911. #define GET_LOCAL_REFTYPE() \
  8912. do { \
  8913. if (wasm_is_type_multi_byte_type(local_type)) { \
  8914. WASMRefType *_ref_type; \
  8915. if (local_idx < param_count) \
  8916. _ref_type = wasm_reftype_map_find( \
  8917. param_reftype_maps, param_reftype_map_count, local_idx); \
  8918. else \
  8919. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8920. local_reftype_map_count, \
  8921. local_idx - param_count); \
  8922. bh_assert(_ref_type); \
  8923. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8924. wasm_reftype_struct_size(_ref_type)); \
  8925. } \
  8926. } while (0)
  8927. #endif /* end of WASM_ENABLE_GC == 0 */
  8928. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8929. do { \
  8930. read_leb_uint32(p, p_end, local_idx); \
  8931. if (local_idx >= param_count + local_count) { \
  8932. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8933. goto fail; \
  8934. } \
  8935. local_type = local_idx < param_count \
  8936. ? param_types[local_idx] \
  8937. : local_types[local_idx - param_count]; \
  8938. local_offset = local_offsets[local_idx]; \
  8939. GET_LOCAL_REFTYPE(); \
  8940. } while (0)
  8941. static bool
  8942. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8943. {
  8944. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8945. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8946. return false;
  8947. }
  8948. return true;
  8949. }
  8950. #define CHECK_MEMORY() \
  8951. do { \
  8952. if (!check_memory(module, error_buf, error_buf_size)) \
  8953. goto fail; \
  8954. } while (0)
  8955. static bool
  8956. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8957. uint32 error_buf_size)
  8958. {
  8959. uint8 mem_access_aligns[] = {
  8960. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8961. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8962. };
  8963. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8964. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8965. set_error_buf(error_buf, error_buf_size,
  8966. "alignment must not be larger than natural");
  8967. return false;
  8968. }
  8969. return true;
  8970. }
  8971. #if WASM_ENABLE_SIMD != 0
  8972. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8973. static bool
  8974. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8975. uint32 error_buf_size)
  8976. {
  8977. uint8 mem_access_aligns[] = {
  8978. 4, /* load */
  8979. 3, 3, 3, 3, 3, 3, /* load and extend */
  8980. 0, 1, 2, 3, /* load and splat */
  8981. 4, /* store */
  8982. };
  8983. uint8 mem_access_aligns_load_lane[] = {
  8984. 0, 1, 2, 3, /* load lane */
  8985. 0, 1, 2, 3, /* store lane */
  8986. 2, 3 /* store zero */
  8987. };
  8988. if (!((opcode <= SIMD_v128_store)
  8989. || (SIMD_v128_load8_lane <= opcode
  8990. && opcode <= SIMD_v128_load64_zero))) {
  8991. set_error_buf(error_buf, error_buf_size,
  8992. "the opcode doesn't include memarg");
  8993. return false;
  8994. }
  8995. if ((opcode <= SIMD_v128_store
  8996. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8997. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8998. && align > mem_access_aligns_load_lane[opcode
  8999. - SIMD_v128_load8_lane])) {
  9000. set_error_buf(error_buf, error_buf_size,
  9001. "alignment must not be larger than natural");
  9002. return false;
  9003. }
  9004. return true;
  9005. }
  9006. static bool
  9007. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9008. uint32 error_buf_size)
  9009. {
  9010. switch (opcode) {
  9011. case SIMD_i8x16_extract_lane_s:
  9012. case SIMD_i8x16_extract_lane_u:
  9013. case SIMD_i8x16_replace_lane:
  9014. if (lane >= 16) {
  9015. goto fail;
  9016. }
  9017. break;
  9018. case SIMD_i16x8_extract_lane_s:
  9019. case SIMD_i16x8_extract_lane_u:
  9020. case SIMD_i16x8_replace_lane:
  9021. if (lane >= 8) {
  9022. goto fail;
  9023. }
  9024. break;
  9025. case SIMD_i32x4_extract_lane:
  9026. case SIMD_i32x4_replace_lane:
  9027. case SIMD_f32x4_extract_lane:
  9028. case SIMD_f32x4_replace_lane:
  9029. if (lane >= 4) {
  9030. goto fail;
  9031. }
  9032. break;
  9033. case SIMD_i64x2_extract_lane:
  9034. case SIMD_i64x2_replace_lane:
  9035. case SIMD_f64x2_extract_lane:
  9036. case SIMD_f64x2_replace_lane:
  9037. if (lane >= 2) {
  9038. goto fail;
  9039. }
  9040. break;
  9041. case SIMD_v128_load8_lane:
  9042. case SIMD_v128_load16_lane:
  9043. case SIMD_v128_load32_lane:
  9044. case SIMD_v128_load64_lane:
  9045. case SIMD_v128_store8_lane:
  9046. case SIMD_v128_store16_lane:
  9047. case SIMD_v128_store32_lane:
  9048. case SIMD_v128_store64_lane:
  9049. case SIMD_v128_load32_zero:
  9050. case SIMD_v128_load64_zero:
  9051. {
  9052. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9053. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9054. goto fail;
  9055. }
  9056. break;
  9057. }
  9058. default:
  9059. goto fail;
  9060. }
  9061. return true;
  9062. fail:
  9063. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9064. return false;
  9065. }
  9066. static bool
  9067. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9068. {
  9069. uint8 i;
  9070. for (i = 0; i != 16; ++i) {
  9071. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9072. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9073. return false;
  9074. }
  9075. }
  9076. return true;
  9077. }
  9078. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9079. #endif /* end of WASM_ENABLE_SIMD */
  9080. #if WASM_ENABLE_SHARED_MEMORY != 0
  9081. static bool
  9082. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9083. uint32 error_buf_size)
  9084. {
  9085. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9086. uint8 mem_access_aligns[] = {
  9087. 2, 3, 0, 1, 0, 1, 2,
  9088. };
  9089. uint8 expect;
  9090. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9091. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9092. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9093. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9094. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9095. }
  9096. else {
  9097. /* 7 opcodes in every group */
  9098. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9099. }
  9100. if (align != expect) {
  9101. set_error_buf(error_buf, error_buf_size,
  9102. "alignment isn't equal to natural");
  9103. return false;
  9104. }
  9105. return true;
  9106. }
  9107. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9108. static bool
  9109. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9110. char *error_buf, uint32 error_buf_size)
  9111. {
  9112. BranchBlock *target_block, *cur_block;
  9113. BlockType *target_block_type;
  9114. uint8 type, *types = NULL, *frame_ref;
  9115. uint32 arity = 0;
  9116. int32 i, available_stack_cell;
  9117. uint16 cell_num;
  9118. #if WASM_ENABLE_GC != 0
  9119. WASMRefTypeMap *frame_reftype_map;
  9120. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9121. WASMRefType *ref_type;
  9122. uint32 reftype_map_count = 0;
  9123. int32 available_reftype_map;
  9124. bool is_type_multi_byte;
  9125. #endif
  9126. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9127. uint8 *frame_ref_after_popped = NULL;
  9128. uint8 frame_ref_tmp[4] = { 0 };
  9129. uint8 *frame_ref_buf = frame_ref_tmp;
  9130. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9131. #if WASM_ENABLE_GC != 0
  9132. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9133. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9134. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9135. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9136. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9137. #endif
  9138. #if WASM_ENABLE_FAST_INTERP != 0
  9139. int16 *frame_offset_old = loader_ctx->frame_offset;
  9140. int16 *frame_offset_after_popped = NULL;
  9141. int16 frame_offset_tmp[4] = { 0 };
  9142. int16 *frame_offset_buf = frame_offset_tmp;
  9143. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9144. #endif
  9145. bool ret = false;
  9146. bh_assert(loader_ctx->csp_num > 0);
  9147. if (loader_ctx->csp_num - 1 < depth) {
  9148. set_error_buf(error_buf, error_buf_size,
  9149. "unknown label, "
  9150. "unexpected end of section or function");
  9151. return false;
  9152. }
  9153. cur_block = loader_ctx->frame_csp - 1;
  9154. target_block = loader_ctx->frame_csp - (depth + 1);
  9155. target_block_type = &target_block->block_type;
  9156. frame_ref = loader_ctx->frame_ref;
  9157. #if WASM_ENABLE_GC != 0
  9158. frame_reftype_map = loader_ctx->frame_reftype_map;
  9159. #endif
  9160. /* Note: loop's arity is different from if and block. loop's arity is
  9161. * its parameter count while if and block arity is result count.
  9162. */
  9163. #if WASM_ENABLE_GC == 0
  9164. if (target_block->label_type == LABEL_TYPE_LOOP)
  9165. arity = block_type_get_param_types(target_block_type, &types);
  9166. else
  9167. arity = block_type_get_result_types(target_block_type, &types);
  9168. #else
  9169. if (target_block->label_type == LABEL_TYPE_LOOP)
  9170. arity = block_type_get_param_types(target_block_type, &types,
  9171. &reftype_maps, &reftype_map_count);
  9172. else
  9173. arity = block_type_get_result_types(target_block_type, &types,
  9174. &reftype_maps, &reftype_map_count);
  9175. #endif
  9176. /* If the stack is in polymorphic state, just clear the stack
  9177. * and then re-push the values to make the stack top values
  9178. * match block type. */
  9179. if (cur_block->is_stack_polymorphic) {
  9180. #if WASM_ENABLE_GC != 0
  9181. int32 j = reftype_map_count - 1;
  9182. #endif
  9183. for (i = (int32)arity - 1; i >= 0; i--) {
  9184. #if WASM_ENABLE_GC != 0
  9185. if (wasm_is_type_multi_byte_type(types[i])) {
  9186. bh_assert(reftype_maps[j].index == i);
  9187. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9188. reftype_maps[j].ref_type,
  9189. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9190. j--;
  9191. }
  9192. #endif
  9193. #if WASM_ENABLE_FAST_INTERP != 0
  9194. POP_OFFSET_TYPE(types[i]);
  9195. #endif
  9196. POP_TYPE(types[i]);
  9197. }
  9198. /* Backup stack data since it may be changed in the below
  9199. push operations, and the stack data may be used when
  9200. checking other target blocks of opcode br_table */
  9201. if (opcode == WASM_OP_BR_TABLE) {
  9202. uint64 total_size;
  9203. frame_ref_after_popped = loader_ctx->frame_ref;
  9204. total_size = (uint64)sizeof(uint8)
  9205. * (frame_ref_old - frame_ref_after_popped);
  9206. if (total_size > sizeof(frame_ref_tmp)
  9207. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9208. error_buf_size))) {
  9209. goto fail;
  9210. }
  9211. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9212. frame_ref_after_popped, (uint32)total_size);
  9213. #if WASM_ENABLE_GC != 0
  9214. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9215. total_size =
  9216. (uint64)sizeof(WASMRefTypeMap)
  9217. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9218. if (total_size > sizeof(frame_reftype_map_tmp)
  9219. && !(frame_reftype_map_buf = loader_malloc(
  9220. total_size, error_buf, error_buf_size))) {
  9221. goto fail;
  9222. }
  9223. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9224. frame_reftype_map_after_popped, (uint32)total_size);
  9225. #endif
  9226. #if WASM_ENABLE_FAST_INTERP != 0
  9227. frame_offset_after_popped = loader_ctx->frame_offset;
  9228. total_size = (uint64)sizeof(int16)
  9229. * (frame_offset_old - frame_offset_after_popped);
  9230. if (total_size > sizeof(frame_offset_tmp)
  9231. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9232. error_buf_size))) {
  9233. goto fail;
  9234. }
  9235. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9236. frame_offset_after_popped, (uint32)total_size);
  9237. #endif
  9238. }
  9239. #if WASM_ENABLE_GC != 0
  9240. j = 0;
  9241. #endif
  9242. for (i = 0; i < (int32)arity; i++) {
  9243. #if WASM_ENABLE_GC != 0
  9244. if (wasm_is_type_multi_byte_type(types[i])) {
  9245. bh_assert(reftype_maps[j].index == i);
  9246. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9247. reftype_maps[j].ref_type,
  9248. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9249. j++;
  9250. }
  9251. #endif
  9252. #if WASM_ENABLE_FAST_INTERP != 0
  9253. bool disable_emit = true;
  9254. int16 operand_offset = 0;
  9255. PUSH_OFFSET_TYPE(types[i]);
  9256. #endif
  9257. PUSH_TYPE(types[i]);
  9258. }
  9259. #if WASM_ENABLE_FAST_INTERP != 0
  9260. emit_br_info(target_block, opcode == WASM_OP_BR);
  9261. #endif
  9262. /* Restore the stack data, note that frame_ref_bottom,
  9263. frame_reftype_map_bottom, frame_offset_bottom may be
  9264. re-allocated in the above push operations */
  9265. if (opcode == WASM_OP_BR_TABLE) {
  9266. uint32 total_size;
  9267. /* The stack operand num should not be smaller than before
  9268. after pop and push operations */
  9269. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9270. loader_ctx->stack_cell_num = stack_cell_num_old;
  9271. loader_ctx->frame_ref =
  9272. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9273. total_size = (uint32)(sizeof(uint8)
  9274. * (frame_ref_old - frame_ref_after_popped));
  9275. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9276. frame_ref_buf, total_size);
  9277. #if WASM_ENABLE_GC != 0
  9278. /* The stack operand num should not be smaller than before
  9279. after pop and push operations */
  9280. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9281. loader_ctx->reftype_map_num = reftype_map_num_old;
  9282. loader_ctx->frame_reftype_map =
  9283. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9284. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9285. * (frame_reftype_map_old
  9286. - frame_reftype_map_after_popped));
  9287. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9288. total_size, frame_reftype_map_buf, total_size);
  9289. #endif
  9290. #if WASM_ENABLE_FAST_INTERP != 0
  9291. loader_ctx->frame_offset =
  9292. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9293. total_size =
  9294. (uint32)(sizeof(int16)
  9295. * (frame_offset_old - frame_offset_after_popped));
  9296. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9297. total_size, frame_offset_buf, total_size);
  9298. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9299. #endif
  9300. }
  9301. ret = true;
  9302. goto cleanup_and_return;
  9303. }
  9304. available_stack_cell =
  9305. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9306. #if WASM_ENABLE_GC != 0
  9307. available_reftype_map =
  9308. (int32)(loader_ctx->reftype_map_num
  9309. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9310. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9311. #endif
  9312. /* Check stack top values match target block type */
  9313. for (i = (int32)arity - 1; i >= 0; i--) {
  9314. type = types[i];
  9315. #if WASM_ENABLE_GC != 0
  9316. ref_type = NULL;
  9317. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9318. if (is_type_multi_byte) {
  9319. bh_assert(reftype_map);
  9320. ref_type = reftype_map->ref_type;
  9321. }
  9322. #endif
  9323. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9324. break;
  9325. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9326. #if WASM_ENABLE_GC != 0
  9327. frame_reftype_map, available_reftype_map,
  9328. #endif
  9329. type,
  9330. #if WASM_ENABLE_GC != 0
  9331. ref_type,
  9332. #endif
  9333. error_buf, error_buf_size)) {
  9334. goto fail;
  9335. }
  9336. cell_num = wasm_value_type_cell_num(types[i]);
  9337. frame_ref -= cell_num;
  9338. available_stack_cell -= cell_num;
  9339. #if WASM_ENABLE_GC != 0
  9340. if (is_type_multi_byte) {
  9341. frame_reftype_map--;
  9342. available_reftype_map--;
  9343. reftype_map--;
  9344. }
  9345. #endif
  9346. }
  9347. #if WASM_ENABLE_FAST_INTERP != 0
  9348. emit_br_info(target_block, opcode == WASM_OP_BR);
  9349. #endif
  9350. ret = true;
  9351. cleanup_and_return:
  9352. fail:
  9353. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9354. wasm_runtime_free(frame_ref_buf);
  9355. #if WASM_ENABLE_GC != 0
  9356. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9357. wasm_runtime_free(frame_reftype_map_buf);
  9358. #endif
  9359. #if WASM_ENABLE_FAST_INTERP != 0
  9360. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9361. wasm_runtime_free(frame_offset_buf);
  9362. #endif
  9363. return ret;
  9364. }
  9365. static BranchBlock *
  9366. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9367. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9368. {
  9369. uint8 *p = *p_buf, *p_end = buf_end;
  9370. BranchBlock *frame_csp_tmp;
  9371. uint32 depth;
  9372. read_leb_uint32(p, p_end, depth);
  9373. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9374. error_buf_size)) {
  9375. goto fail;
  9376. }
  9377. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9378. *p_buf = p;
  9379. return frame_csp_tmp;
  9380. fail:
  9381. return NULL;
  9382. }
  9383. #if WASM_ENABLE_EXCE_HANDLING != 0
  9384. static BranchBlock *
  9385. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9386. uint8 *buf_end, char *error_buf,
  9387. uint32 error_buf_size)
  9388. {
  9389. uint8 *p = *p_buf, *p_end = buf_end;
  9390. BranchBlock *frame_csp_tmp;
  9391. uint32 depth;
  9392. read_leb_uint32(p, p_end, depth);
  9393. /*
  9394. * Note: "delegate 0" means the surrounding block, not the
  9395. * try-delegate block itself.
  9396. *
  9397. * Note: the caller hasn't popped the try-delegate frame yet.
  9398. */
  9399. bh_assert(loader_ctx->csp_num > 0);
  9400. if (loader_ctx->csp_num - 1 <= depth) {
  9401. #if WASM_ENABLE_SPEC_TEST == 0
  9402. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9403. #else
  9404. set_error_buf(error_buf, error_buf_size, "unknown label");
  9405. #endif
  9406. goto fail;
  9407. }
  9408. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9409. #if WASM_ENABLE_FAST_INTERP != 0
  9410. emit_br_info(frame_csp_tmp, false);
  9411. #endif
  9412. *p_buf = p;
  9413. return frame_csp_tmp;
  9414. fail:
  9415. return NULL;
  9416. }
  9417. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9418. static bool
  9419. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9420. char *error_buf, uint32 error_buf_size)
  9421. {
  9422. BlockType *block_type = &block->block_type;
  9423. uint8 *return_types = NULL;
  9424. uint32 return_count = 0;
  9425. int32 available_stack_cell, return_cell_num, i;
  9426. uint8 *frame_ref = NULL;
  9427. #if WASM_ENABLE_GC != 0
  9428. WASMRefTypeMap *frame_reftype_map;
  9429. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9430. WASMRefType *ref_type;
  9431. uint32 param_count, return_reftype_map_count = 0;
  9432. int32 available_reftype_map =
  9433. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9434. #endif
  9435. available_stack_cell =
  9436. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9437. #if WASM_ENABLE_GC == 0
  9438. return_count = block_type_get_result_types(block_type, &return_types);
  9439. #else
  9440. return_count = block_type_get_result_types(block_type, &return_types,
  9441. &return_reftype_maps,
  9442. &return_reftype_map_count);
  9443. param_count =
  9444. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9445. (void)param_count;
  9446. #endif
  9447. return_cell_num =
  9448. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9449. /* If the stack is in polymorphic state, just clear the stack
  9450. * and then re-push the values to make the stack top values
  9451. * match block type. */
  9452. if (block->is_stack_polymorphic) {
  9453. #if WASM_ENABLE_GC != 0
  9454. int32 j = return_reftype_map_count - 1;
  9455. #endif
  9456. for (i = (int32)return_count - 1; i >= 0; i--) {
  9457. #if WASM_ENABLE_GC != 0
  9458. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9459. bh_assert(return_reftype_maps[j].index == i + param_count);
  9460. bh_memcpy_s(
  9461. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9462. return_reftype_maps[j].ref_type,
  9463. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9464. j--;
  9465. }
  9466. #endif
  9467. #if WASM_ENABLE_FAST_INTERP != 0
  9468. POP_OFFSET_TYPE(return_types[i]);
  9469. #endif
  9470. POP_TYPE(return_types[i]);
  9471. }
  9472. /* Check stack is empty */
  9473. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9474. set_error_buf(
  9475. error_buf, error_buf_size,
  9476. "type mismatch: stack size does not match block type");
  9477. goto fail;
  9478. }
  9479. #if WASM_ENABLE_GC != 0
  9480. j = 0;
  9481. #endif
  9482. for (i = 0; i < (int32)return_count; i++) {
  9483. #if WASM_ENABLE_GC != 0
  9484. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9485. bh_assert(return_reftype_maps[j].index == i + param_count);
  9486. bh_memcpy_s(
  9487. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9488. return_reftype_maps[j].ref_type,
  9489. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9490. j++;
  9491. }
  9492. #endif
  9493. #if WASM_ENABLE_FAST_INTERP != 0
  9494. bool disable_emit = true;
  9495. int16 operand_offset = 0;
  9496. PUSH_OFFSET_TYPE(return_types[i]);
  9497. #endif
  9498. PUSH_TYPE(return_types[i]);
  9499. }
  9500. return true;
  9501. }
  9502. if (available_stack_cell != return_cell_num) {
  9503. #if WASM_ENABLE_EXCE_HANDLING != 0
  9504. /* testspec: this error message format is expected by try_catch.wast */
  9505. snprintf(
  9506. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9507. block->label_type == LABEL_TYPE_TRY
  9508. || (block->label_type == LABEL_TYPE_CATCH
  9509. && return_cell_num > 0)
  9510. ? "instruction"
  9511. : "block",
  9512. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9513. " but stack has ",
  9514. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9515. : "");
  9516. goto fail;
  9517. #else
  9518. set_error_buf(error_buf, error_buf_size,
  9519. "type mismatch: stack size does not match block type");
  9520. goto fail;
  9521. #endif
  9522. }
  9523. /* Check stack values match return types */
  9524. frame_ref = loader_ctx->frame_ref;
  9525. #if WASM_ENABLE_GC != 0
  9526. frame_reftype_map = loader_ctx->frame_reftype_map;
  9527. return_reftype_map =
  9528. return_reftype_map_count
  9529. ? return_reftype_maps + return_reftype_map_count - 1
  9530. : NULL;
  9531. #endif
  9532. for (i = (int32)return_count - 1; i >= 0; i--) {
  9533. uint8 type = return_types[i];
  9534. #if WASM_ENABLE_GC != 0
  9535. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9536. ref_type = NULL;
  9537. if (is_type_multi_byte) {
  9538. bh_assert(return_reftype_map);
  9539. ref_type = return_reftype_map->ref_type;
  9540. }
  9541. #endif
  9542. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9543. #if WASM_ENABLE_GC != 0
  9544. frame_reftype_map, available_reftype_map,
  9545. #endif
  9546. type,
  9547. #if WASM_ENABLE_GC != 0
  9548. ref_type,
  9549. #endif
  9550. error_buf, error_buf_size))
  9551. return false;
  9552. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9553. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9554. #if WASM_ENABLE_GC != 0
  9555. if (is_type_multi_byte) {
  9556. frame_reftype_map--;
  9557. available_reftype_map--;
  9558. return_reftype_map--;
  9559. }
  9560. #endif
  9561. }
  9562. return true;
  9563. fail:
  9564. return false;
  9565. }
  9566. #if WASM_ENABLE_FAST_INTERP != 0
  9567. /* Copy parameters to dynamic space.
  9568. * 1) POP original parameter out;
  9569. * 2) Push and copy original values to dynamic space.
  9570. * The copy instruction format:
  9571. * Part a: param count
  9572. * Part b: all param total cell num
  9573. * Part c: each param's cell_num, src offset and dst offset
  9574. * Part d: each param's src offset
  9575. * Part e: each param's dst offset
  9576. */
  9577. static bool
  9578. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9579. uint32 error_buf_size)
  9580. {
  9581. bool ret = false;
  9582. int16 *frame_offset = NULL;
  9583. uint8 *cells = NULL, cell;
  9584. int16 *src_offsets = NULL;
  9585. uint8 *emit_data = NULL;
  9586. uint32 i;
  9587. BranchBlock *block = loader_ctx->frame_csp - 1;
  9588. BlockType *block_type = &block->block_type;
  9589. WASMFuncType *wasm_type = block_type->u.type;
  9590. uint32 param_count = block_type->u.type->param_count;
  9591. int16 condition_offset = 0;
  9592. bool disable_emit = false;
  9593. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9594. int16 operand_offset = 0;
  9595. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9596. bh_assert(size > 0);
  9597. /* For if block, we also need copy the condition operand offset. */
  9598. if (is_if_block)
  9599. size += sizeof(*cells) + sizeof(*src_offsets);
  9600. /* Allocate memory for the emit data */
  9601. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9602. return false;
  9603. cells = emit_data;
  9604. src_offsets = (int16 *)(cells + param_count);
  9605. if (is_if_block)
  9606. condition_offset = *loader_ctx->frame_offset;
  9607. /* POP original parameter out */
  9608. for (i = 0; i < param_count; i++) {
  9609. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9610. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9611. }
  9612. frame_offset = loader_ctx->frame_offset;
  9613. /* Get each param's cell num and src offset */
  9614. for (i = 0; i < param_count; i++) {
  9615. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9616. cells[i] = cell;
  9617. src_offsets[i] = *frame_offset;
  9618. frame_offset += cell;
  9619. }
  9620. /* emit copy instruction */
  9621. emit_label(EXT_OP_COPY_STACK_VALUES);
  9622. /* Part a) */
  9623. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9624. /* Part b) */
  9625. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9626. : wasm_type->param_cell_num);
  9627. /* Part c) */
  9628. for (i = 0; i < param_count; i++)
  9629. emit_byte(loader_ctx, cells[i]);
  9630. if (is_if_block)
  9631. emit_byte(loader_ctx, 1);
  9632. /* Part d) */
  9633. for (i = 0; i < param_count; i++)
  9634. emit_operand(loader_ctx, src_offsets[i]);
  9635. if (is_if_block)
  9636. emit_operand(loader_ctx, condition_offset);
  9637. /* Since the start offset to save the block's params and
  9638. * the start offset to save the block's results may be
  9639. * different, we remember the dynamic offset for loop block
  9640. * so that we can use it to copy the stack operands to the
  9641. * loop block's params in wasm_loader_emit_br_info. */
  9642. if (block->label_type == LABEL_TYPE_LOOP)
  9643. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9644. /* Part e) */
  9645. /* Push to dynamic space. The push will emit the dst offset. */
  9646. for (i = 0; i < param_count; i++)
  9647. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9648. if (is_if_block)
  9649. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9650. ret = true;
  9651. fail:
  9652. /* Free the emit data */
  9653. wasm_runtime_free(emit_data);
  9654. return ret;
  9655. }
  9656. #endif
  9657. #if WASM_ENABLE_GC == 0
  9658. #define RESET_REFTYPE_MAP_STACK() (void)0
  9659. #else
  9660. #define RESET_REFTYPE_MAP_STACK() \
  9661. do { \
  9662. loader_ctx->reftype_map_num = \
  9663. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9664. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9665. + loader_ctx->reftype_map_num; \
  9666. } while (0)
  9667. #endif
  9668. /* reset the stack to the state of before entering the last block */
  9669. #if WASM_ENABLE_FAST_INTERP != 0
  9670. #define RESET_STACK() \
  9671. do { \
  9672. loader_ctx->stack_cell_num = \
  9673. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9674. loader_ctx->frame_ref = \
  9675. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9676. loader_ctx->frame_offset = \
  9677. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9678. RESET_REFTYPE_MAP_STACK(); \
  9679. } while (0)
  9680. #else
  9681. #define RESET_STACK() \
  9682. do { \
  9683. loader_ctx->stack_cell_num = \
  9684. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9685. loader_ctx->frame_ref = \
  9686. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9687. RESET_REFTYPE_MAP_STACK(); \
  9688. } while (0)
  9689. #endif
  9690. /* set current block's stack polymorphic state */
  9691. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9692. do { \
  9693. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9694. _cur_block->is_stack_polymorphic = flag; \
  9695. } while (0)
  9696. #define BLOCK_HAS_PARAM(block_type) \
  9697. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9698. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9699. do { \
  9700. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9701. error_buf_size))) { \
  9702. goto fail; \
  9703. } \
  9704. } while (0)
  9705. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9706. static bool
  9707. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9708. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9709. uint32 error_buf_size)
  9710. {
  9711. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9712. return false;
  9713. }
  9714. if (table_idx < module->import_table_count) {
  9715. if (p_elem_type)
  9716. *p_elem_type =
  9717. module->import_tables[table_idx].u.table.table_type.elem_type;
  9718. #if WASM_ENABLE_GC != 0
  9719. if (p_ref_type)
  9720. *((WASMRefType **)p_ref_type) =
  9721. module->import_tables[table_idx]
  9722. .u.table.table_type.elem_ref_type;
  9723. #endif
  9724. }
  9725. else {
  9726. if (p_elem_type)
  9727. *p_elem_type =
  9728. module->tables[module->import_table_count + table_idx]
  9729. .table_type.elem_type;
  9730. #if WASM_ENABLE_GC != 0
  9731. if (p_ref_type)
  9732. *((WASMRefType **)p_ref_type) =
  9733. module->tables[module->import_table_count + table_idx]
  9734. .table_type.elem_ref_type;
  9735. #endif
  9736. }
  9737. return true;
  9738. }
  9739. static bool
  9740. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9741. uint8 *p_elem_type, void **p_elem_ref_type,
  9742. char *error_buf, uint32 error_buf_size)
  9743. {
  9744. if (table_seg_idx >= module->table_seg_count) {
  9745. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9746. table_seg_idx);
  9747. return false;
  9748. }
  9749. if (p_elem_type) {
  9750. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9751. }
  9752. #if WASM_ENABLE_GC != 0
  9753. if (p_elem_ref_type)
  9754. *((WASMRefType **)p_elem_ref_type) =
  9755. module->table_segments[table_seg_idx].elem_ref_type;
  9756. #endif
  9757. return true;
  9758. }
  9759. #endif
  9760. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9761. const uint8 *
  9762. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9763. uint32 *len)
  9764. {
  9765. WASMCustomSection *section = module->custom_section_list;
  9766. while (section) {
  9767. if ((section->name_len == strlen(name))
  9768. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9769. if (len) {
  9770. *len = section->content_len;
  9771. }
  9772. return section->content_addr;
  9773. }
  9774. section = section->next;
  9775. }
  9776. return NULL;
  9777. }
  9778. #endif
  9779. #if 0
  9780. #define HANDLE_OPCODE(opcode) #opcode
  9781. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9782. #undef HANDLE_OPCODE
  9783. #endif
  9784. static bool
  9785. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9786. uint32 cur_func_idx, char *error_buf,
  9787. uint32 error_buf_size)
  9788. {
  9789. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9790. uint32 param_count, local_count, global_count;
  9791. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9792. BlockType func_block_type;
  9793. uint16 *local_offsets, local_offset;
  9794. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9795. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9796. mem_offset_t mem_offset;
  9797. int32 i32_const = 0;
  9798. int64 i64_const;
  9799. uint8 opcode;
  9800. bool return_value = false;
  9801. WASMLoaderContext *loader_ctx;
  9802. BranchBlock *frame_csp_tmp;
  9803. #if WASM_ENABLE_GC != 0
  9804. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9805. uint32 param_reftype_map_count, local_reftype_map_count;
  9806. int32 heap_type;
  9807. WASMRefType wasm_ref_type = { 0 };
  9808. bool need_ref_type_map;
  9809. #endif
  9810. #if WASM_ENABLE_FAST_INTERP != 0
  9811. uint8 *func_const_end, *func_const = NULL;
  9812. int16 operand_offset = 0;
  9813. uint8 last_op = 0;
  9814. bool disable_emit, preserve_local = false, if_condition_available = true;
  9815. float32 f32_const;
  9816. float64 f64_const;
  9817. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9818. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9819. #endif
  9820. #if WASM_ENABLE_MEMORY64 != 0
  9821. bool is_memory64 = has_module_memory64(module);
  9822. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9823. #else
  9824. mem_offset_type = VALUE_TYPE_I32;
  9825. #endif
  9826. global_count = module->import_global_count + module->global_count;
  9827. param_count = func->func_type->param_count;
  9828. param_types = func->func_type->types;
  9829. func_block_type.is_value_type = false;
  9830. func_block_type.u.type = func->func_type;
  9831. local_count = func->local_count;
  9832. local_types = func->local_types;
  9833. local_offsets = func->local_offsets;
  9834. #if WASM_ENABLE_GC != 0
  9835. param_reftype_maps = func->func_type->ref_type_maps;
  9836. param_reftype_map_count = func->func_type->ref_type_map_count;
  9837. local_reftype_maps = func->local_ref_type_maps;
  9838. local_reftype_map_count = func->local_ref_type_map_count;
  9839. #endif
  9840. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9841. goto fail;
  9842. }
  9843. #if WASM_ENABLE_GC != 0
  9844. loader_ctx->module = module;
  9845. loader_ctx->ref_type_set = module->ref_type_set;
  9846. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9847. #endif
  9848. #if WASM_ENABLE_FAST_INTERP != 0
  9849. /* For the first traverse, the initial value of preserved_local_offset has
  9850. * not been determined, we use the INT16_MAX to represent that a slot has
  9851. * been copied to preserve space. For second traverse, this field will be
  9852. * set to the appropriate value in wasm_loader_ctx_reinit.
  9853. * This is for Issue #1230,
  9854. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9855. * drop opcodes need to know which slots are preserved, so those slots will
  9856. * not be treated as dynamically allocated slots */
  9857. loader_ctx->preserved_local_offset = INT16_MAX;
  9858. re_scan:
  9859. if (loader_ctx->code_compiled_size > 0) {
  9860. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9861. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9862. goto fail;
  9863. }
  9864. p = func->code;
  9865. func->code_compiled = loader_ctx->p_code_compiled;
  9866. func->code_compiled_size = loader_ctx->code_compiled_size;
  9867. }
  9868. #endif
  9869. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9870. while (p < p_end) {
  9871. opcode = *p++;
  9872. #if WASM_ENABLE_FAST_INTERP != 0
  9873. p_org = p;
  9874. disable_emit = false;
  9875. emit_label(opcode);
  9876. #endif
  9877. switch (opcode) {
  9878. case WASM_OP_UNREACHABLE:
  9879. RESET_STACK();
  9880. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9881. break;
  9882. case WASM_OP_NOP:
  9883. #if WASM_ENABLE_FAST_INTERP != 0
  9884. skip_label();
  9885. #endif
  9886. break;
  9887. case WASM_OP_IF:
  9888. {
  9889. #if WASM_ENABLE_FAST_INTERP != 0
  9890. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9891. int32 available_stack_cell =
  9892. (int32)(loader_ctx->stack_cell_num
  9893. - parent_block->stack_cell_num);
  9894. if (available_stack_cell <= 0
  9895. && parent_block->is_stack_polymorphic)
  9896. if_condition_available = false;
  9897. else
  9898. if_condition_available = true;
  9899. PRESERVE_LOCAL_FOR_BLOCK();
  9900. #endif
  9901. #if WASM_ENABLE_GC == 0
  9902. POP_I32();
  9903. #endif
  9904. goto handle_op_block_and_loop;
  9905. }
  9906. case WASM_OP_BLOCK:
  9907. case WASM_OP_LOOP:
  9908. #if WASM_ENABLE_EXCE_HANDLING != 0
  9909. case WASM_OP_TRY:
  9910. if (opcode == WASM_OP_TRY) {
  9911. /*
  9912. * keep track of exception handlers to account for
  9913. * memory allocation
  9914. */
  9915. func->exception_handler_count++;
  9916. /*
  9917. * try is a block
  9918. * do nothing special, but execution continues to
  9919. * to handle_op_block_and_loop,
  9920. * and that be pushes the csp
  9921. */
  9922. }
  9923. #endif
  9924. #if WASM_ENABLE_FAST_INTERP != 0
  9925. PRESERVE_LOCAL_FOR_BLOCK();
  9926. #endif
  9927. handle_op_block_and_loop:
  9928. {
  9929. uint8 value_type;
  9930. BlockType block_type;
  9931. #if WASM_ENABLE_FAST_INTERP != 0
  9932. uint32 available_params = 0;
  9933. #endif
  9934. CHECK_BUF(p, p_end, 1);
  9935. value_type = read_uint8(p);
  9936. if (is_byte_a_type(value_type)) {
  9937. /* If the first byte is one of these special values:
  9938. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9939. * the single return value. */
  9940. block_type.is_value_type = true;
  9941. block_type.u.value_type.type = value_type;
  9942. #if WASM_ENABLE_WAMR_COMPILER != 0
  9943. if (value_type == VALUE_TYPE_V128)
  9944. module->is_simd_used = true;
  9945. else if (value_type == VALUE_TYPE_FUNCREF
  9946. || value_type == VALUE_TYPE_EXTERNREF)
  9947. module->is_ref_types_used = true;
  9948. #endif
  9949. #if WASM_ENABLE_GC != 0
  9950. if (value_type != VALUE_TYPE_VOID) {
  9951. p_org = p;
  9952. p--;
  9953. if (!resolve_value_type((const uint8 **)&p, p_end,
  9954. module, module->type_count,
  9955. &need_ref_type_map,
  9956. &wasm_ref_type, false,
  9957. error_buf, error_buf_size)) {
  9958. goto fail;
  9959. }
  9960. if (need_ref_type_map) {
  9961. block_type.u.value_type.ref_type_map.index = 0;
  9962. if (!(block_type.u.value_type.ref_type_map
  9963. .ref_type = reftype_set_insert(
  9964. module->ref_type_set, &wasm_ref_type,
  9965. error_buf, error_buf_size))) {
  9966. goto fail;
  9967. }
  9968. }
  9969. /* Set again as the type might be changed, e.g.
  9970. (ref null any) to anyref */
  9971. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9972. #if WASM_ENABLE_FAST_INTERP == 0
  9973. while (p_org < p) {
  9974. #if WASM_ENABLE_DEBUG_INTERP != 0
  9975. if (!record_fast_op(module, p_org, *p_org,
  9976. error_buf, error_buf_size)) {
  9977. goto fail;
  9978. }
  9979. #endif
  9980. /* Ignore extra bytes for interpreter */
  9981. *p_org++ = WASM_OP_NOP;
  9982. }
  9983. #endif
  9984. }
  9985. #endif /* end of WASM_ENABLE_GC != 0 */
  9986. }
  9987. else {
  9988. int32 type_index;
  9989. /* Resolve the leb128 encoded type index as block type */
  9990. p--;
  9991. p_org = p - 1;
  9992. read_leb_int32(p, p_end, type_index);
  9993. if ((uint32)type_index >= module->type_count) {
  9994. set_error_buf(error_buf, error_buf_size,
  9995. "unknown type");
  9996. goto fail;
  9997. }
  9998. block_type.is_value_type = false;
  9999. block_type.u.type =
  10000. (WASMFuncType *)module->types[type_index];
  10001. #if WASM_ENABLE_FAST_INTERP == 0
  10002. /* If block use type index as block type, change the opcode
  10003. * to new extended opcode so that interpreter can resolve
  10004. * the block quickly.
  10005. */
  10006. #if WASM_ENABLE_DEBUG_INTERP != 0
  10007. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10008. error_buf_size)) {
  10009. goto fail;
  10010. }
  10011. #endif
  10012. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10013. #endif
  10014. }
  10015. #if WASM_ENABLE_GC != 0
  10016. if (opcode == WASM_OP_IF) {
  10017. POP_I32();
  10018. }
  10019. #endif
  10020. /* Pop block parameters from stack */
  10021. if (BLOCK_HAS_PARAM(block_type)) {
  10022. WASMFuncType *wasm_type = block_type.u.type;
  10023. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10024. #if WASM_ENABLE_FAST_INTERP != 0
  10025. uint32 cell_num;
  10026. available_params = block_type.u.type->param_count;
  10027. #endif
  10028. for (i = 0; i < block_type.u.type->param_count; i++) {
  10029. int32 available_stack_cell =
  10030. (int32)(loader_ctx->stack_cell_num
  10031. - cur_block->stack_cell_num);
  10032. if (available_stack_cell <= 0
  10033. && cur_block->is_stack_polymorphic) {
  10034. #if WASM_ENABLE_FAST_INTERP != 0
  10035. available_params = i;
  10036. #endif
  10037. break;
  10038. }
  10039. POP_TYPE(
  10040. wasm_type->types[wasm_type->param_count - i - 1]);
  10041. #if WASM_ENABLE_FAST_INTERP != 0
  10042. /* decrease the frame_offset pointer accordingly to keep
  10043. * consistent with frame_ref stack */
  10044. cell_num = wasm_value_type_cell_num(
  10045. wasm_type->types[wasm_type->param_count - i - 1]);
  10046. loader_ctx->frame_offset -= cell_num;
  10047. #endif
  10048. }
  10049. }
  10050. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10051. block_type, p);
  10052. /* Pass parameters to block */
  10053. if (BLOCK_HAS_PARAM(block_type)) {
  10054. for (i = 0; i < block_type.u.type->param_count; i++) {
  10055. #if WASM_ENABLE_FAST_INTERP != 0
  10056. uint32 cell_num = wasm_value_type_cell_num(
  10057. block_type.u.type->types[i]);
  10058. if (i >= available_params) {
  10059. /* If there isn't enough data on stack, push a dummy
  10060. * offset to keep the stack consistent with
  10061. * frame_ref.
  10062. * Since the stack is already in polymorphic state,
  10063. * the opcode will not be executed, so the dummy
  10064. * offset won't cause any error */
  10065. uint32 n;
  10066. for (n = 0; n < cell_num; n++) {
  10067. if (loader_ctx->p_code_compiled == NULL) {
  10068. if (!check_offset_push(loader_ctx,
  10069. error_buf,
  10070. error_buf_size))
  10071. goto fail;
  10072. }
  10073. *loader_ctx->frame_offset++ = 0;
  10074. }
  10075. }
  10076. else {
  10077. loader_ctx->frame_offset += cell_num;
  10078. }
  10079. #endif
  10080. PUSH_TYPE(block_type.u.type->types[i]);
  10081. }
  10082. }
  10083. #if WASM_ENABLE_FAST_INTERP != 0
  10084. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10085. skip_label();
  10086. if (BLOCK_HAS_PARAM(block_type)) {
  10087. /* Make sure params are in dynamic space */
  10088. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10089. error_buf_size))
  10090. goto fail;
  10091. }
  10092. if (opcode == WASM_OP_LOOP) {
  10093. (loader_ctx->frame_csp - 1)->code_compiled =
  10094. loader_ctx->p_code_compiled;
  10095. }
  10096. }
  10097. #if WASM_ENABLE_EXCE_HANDLING != 0
  10098. else if (opcode == WASM_OP_TRY) {
  10099. skip_label();
  10100. }
  10101. #endif
  10102. else if (opcode == WASM_OP_IF) {
  10103. BranchBlock *block = loader_ctx->frame_csp - 1;
  10104. /* If block has parameters, we should make sure they are in
  10105. * dynamic space. Otherwise, when else branch is missing,
  10106. * the later opcode may consume incorrect operand offset.
  10107. * Spec case:
  10108. * (func (export "params-id") (param i32) (result i32)
  10109. * (i32.const 1)
  10110. * (i32.const 2)
  10111. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10112. * (then)) (i32.add)
  10113. * )
  10114. *
  10115. * So we should emit a copy instruction before the if.
  10116. *
  10117. * And we also need to save the parameter offsets and
  10118. * recover them before entering else branch.
  10119. *
  10120. */
  10121. if (BLOCK_HAS_PARAM(block_type)) {
  10122. uint64 size;
  10123. /* In polymorphic state, there may be no if condition on
  10124. * the stack, so the offset may not emitted */
  10125. if (if_condition_available) {
  10126. /* skip the if condition operand offset */
  10127. wasm_loader_emit_backspace(loader_ctx,
  10128. sizeof(int16));
  10129. }
  10130. /* skip the if label */
  10131. skip_label();
  10132. /* Emit a copy instruction */
  10133. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10134. error_buf_size))
  10135. goto fail;
  10136. /* Emit the if instruction */
  10137. emit_label(opcode);
  10138. /* Emit the new condition operand offset */
  10139. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10140. /* Save top param_count values of frame_offset stack, so
  10141. * that we can recover it before executing else branch
  10142. */
  10143. size = sizeof(int16)
  10144. * (uint64)block_type.u.type->param_cell_num;
  10145. if (!(block->param_frame_offsets = loader_malloc(
  10146. size, error_buf, error_buf_size)))
  10147. goto fail;
  10148. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10149. loader_ctx->frame_offset
  10150. - size / sizeof(int16),
  10151. (uint32)size);
  10152. }
  10153. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10154. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10155. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10156. }
  10157. #endif
  10158. break;
  10159. }
  10160. #if WASM_ENABLE_EXCE_HANDLING != 0
  10161. case WASM_OP_THROW:
  10162. {
  10163. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10164. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10165. uint8 label_type = cur_block->label_type;
  10166. uint32 tag_index = 0;
  10167. read_leb_int32(p, p_end, tag_index);
  10168. /* check validity of tag_index against module->tag_count */
  10169. /* check tag index is within the tag index space */
  10170. if (tag_index >= module->import_tag_count + module->tag_count) {
  10171. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10172. tag_index);
  10173. goto fail;
  10174. }
  10175. /* the tag_type is stored in either the WASMTag (section tags)
  10176. * or WASMTagImport (import tag) */
  10177. WASMFuncType *tag_type = NULL;
  10178. if (tag_index < module->import_tag_count) {
  10179. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10180. }
  10181. else {
  10182. tag_type =
  10183. module->tags[tag_index - module->import_tag_count]
  10184. ->tag_type;
  10185. }
  10186. if (tag_type->result_count != 0) {
  10187. set_error_buf(error_buf, error_buf_size,
  10188. "tag type signature does not return void");
  10189. goto fail;
  10190. }
  10191. int32 available_stack_cell =
  10192. (int32)(loader_ctx->stack_cell_num
  10193. - cur_block->stack_cell_num);
  10194. /* Check stack values match return types by comparing tag param
  10195. * types with stack cells */
  10196. uint8 *frame_ref = loader_ctx->frame_ref;
  10197. #if WASM_ENABLE_GC != 0
  10198. WASMRefTypeMap *frame_reftype_map =
  10199. loader_ctx->frame_reftype_map;
  10200. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10201. param_reftype_maps = tag_type->ref_type_maps;
  10202. /* For tag_type function, it shouldn't have result_count = 0 */
  10203. param_reftype_map_count = tag_type->ref_type_map_count;
  10204. param_count = (int32)tag_type->param_count;
  10205. #endif
  10206. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  10207. tti--) {
  10208. #if WASM_ENABLE_GC != 0
  10209. local_type = tag_type->types[tti];
  10210. local_idx = tti;
  10211. /* Get the wasm_ref_type if the local_type is multibyte
  10212. * type */
  10213. GET_LOCAL_REFTYPE();
  10214. #endif
  10215. if (!check_stack_top_values(
  10216. loader_ctx, frame_ref, available_stack_cell,
  10217. #if WASM_ENABLE_GC != 0
  10218. frame_reftype_map, frame_reftype_map_num,
  10219. #endif
  10220. tag_type->types[tti],
  10221. #if WASM_ENABLE_GC != 0
  10222. &wasm_ref_type,
  10223. #endif
  10224. error_buf, error_buf_size)) {
  10225. snprintf(error_buf, error_buf_size,
  10226. "type mismatch: instruction requires [%s] but "
  10227. "stack has [%s]",
  10228. tag_type->param_count > 0
  10229. ? type2str(tag_type->types[tti])
  10230. : "",
  10231. available_stack_cell > 0
  10232. ? type2str(*(loader_ctx->frame_ref - 1))
  10233. : "");
  10234. goto fail;
  10235. }
  10236. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10237. available_stack_cell -=
  10238. wasm_value_type_cell_num(tag_type->types[tti]);
  10239. }
  10240. /* throw is stack polymorphic */
  10241. (void)label_type;
  10242. RESET_STACK();
  10243. break;
  10244. }
  10245. case WASM_OP_RETHROW:
  10246. {
  10247. /* must be done before checking branch block */
  10248. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10249. /* check the target catching block: LABEL_TYPE_CATCH */
  10250. if (!(frame_csp_tmp =
  10251. check_branch_block(loader_ctx, &p, p_end, opcode,
  10252. error_buf, error_buf_size)))
  10253. goto fail;
  10254. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10255. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10256. /* trap according to spectest (rethrow.wast) */
  10257. set_error_buf(error_buf, error_buf_size,
  10258. "invalid rethrow label");
  10259. goto fail;
  10260. }
  10261. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10262. uint8 label_type = cur_block->label_type;
  10263. (void)label_type;
  10264. /* rethrow is stack polymorphic */
  10265. RESET_STACK();
  10266. break;
  10267. }
  10268. case WASM_OP_DELEGATE:
  10269. {
  10270. /* check target block is valid */
  10271. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10272. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10273. goto fail;
  10274. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10275. uint8 label_type = cur_block->label_type;
  10276. (void)label_type;
  10277. /* DELEGATE ends the block */
  10278. POP_CSP();
  10279. break;
  10280. }
  10281. case WASM_OP_CATCH:
  10282. {
  10283. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10284. uint8 label_type = cur_block->label_type;
  10285. uint32 tag_index = 0;
  10286. read_leb_int32(p, p_end, tag_index);
  10287. /* check validity of tag_index against module->tag_count */
  10288. /* check tag index is within the tag index space */
  10289. if (tag_index >= module->import_tag_count + module->tag_count) {
  10290. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10291. __FUNCTION__);
  10292. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10293. goto fail;
  10294. }
  10295. /* the tag_type is stored in either the WASMTag (section tags)
  10296. * or WASMTagImport (import tag) */
  10297. WASMFuncType *func_type = NULL;
  10298. if (tag_index < module->import_tag_count) {
  10299. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10300. }
  10301. else {
  10302. func_type =
  10303. module->tags[tag_index - module->import_tag_count]
  10304. ->tag_type;
  10305. }
  10306. if (func_type->result_count != 0) {
  10307. set_error_buf(error_buf, error_buf_size,
  10308. "tag type signature does not return void");
  10309. goto fail;
  10310. }
  10311. /* check validity of current label (expect LABEL_TYPE_TRY or
  10312. * LABEL_TYPE_CATCH) */
  10313. if ((LABEL_TYPE_CATCH != label_type)
  10314. && (LABEL_TYPE_TRY != label_type)) {
  10315. set_error_buf(error_buf, error_buf_size,
  10316. "Unexpected block sequence encountered.");
  10317. goto fail;
  10318. }
  10319. BlockType new_block_type;
  10320. new_block_type.is_value_type = false;
  10321. new_block_type.u.type = func_type;
  10322. /*
  10323. * replace frame_csp by LABEL_TYPE_CATCH
  10324. */
  10325. cur_block->label_type = LABEL_TYPE_CATCH;
  10326. /* RESET_STACK removes the values pushed in TRY or pervious
  10327. * CATCH Blocks */
  10328. RESET_STACK();
  10329. /* push types on the stack according to caught type */
  10330. if (BLOCK_HAS_PARAM(new_block_type)) {
  10331. for (i = 0; i < new_block_type.u.type->param_count; i++)
  10332. PUSH_TYPE(new_block_type.u.type->types[i]);
  10333. }
  10334. break;
  10335. }
  10336. case WASM_OP_CATCH_ALL:
  10337. {
  10338. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10339. /* expecting a TRY or CATCH, anything else will be considered an
  10340. * error */
  10341. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10342. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10343. set_error_buf(error_buf, error_buf_size,
  10344. "Unexpected block sequence encountered.");
  10345. goto fail;
  10346. }
  10347. /* no immediates */
  10348. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10349. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10350. /* RESET_STACK removes the values pushed in TRY or pervious
  10351. * CATCH Blocks */
  10352. RESET_STACK();
  10353. /* catch_all has no tagtype and therefore no parameters */
  10354. break;
  10355. }
  10356. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10357. case WASM_OP_ELSE:
  10358. handle_op_else:
  10359. {
  10360. BranchBlock *block = NULL;
  10361. BlockType block_type;
  10362. if (loader_ctx->csp_num < 2
  10363. /* the matched if isn't found */
  10364. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10365. /* duplicated else is found */
  10366. || (loader_ctx->frame_csp - 1)->else_addr) {
  10367. set_error_buf(
  10368. error_buf, error_buf_size,
  10369. "opcode else found without matched opcode if");
  10370. goto fail;
  10371. }
  10372. block = loader_ctx->frame_csp - 1;
  10373. /* check whether if branch's stack matches its result type */
  10374. if (!check_block_stack(loader_ctx, block, error_buf,
  10375. error_buf_size))
  10376. goto fail;
  10377. block->else_addr = p - 1;
  10378. block_type = block->block_type;
  10379. #if WASM_ENABLE_GC != 0
  10380. if (!wasm_loader_init_local_use_masks(
  10381. loader_ctx, local_count, error_buf, error_buf_size)) {
  10382. goto fail;
  10383. }
  10384. #endif
  10385. #if WASM_ENABLE_FAST_INTERP != 0
  10386. /* if the result of if branch is in local or const area, add a
  10387. * copy op */
  10388. RESERVE_BLOCK_RET();
  10389. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10390. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10391. #endif
  10392. RESET_STACK();
  10393. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10394. /* Pass parameters to if-false branch */
  10395. if (BLOCK_HAS_PARAM(block_type)) {
  10396. for (i = 0; i < block_type.u.type->param_count; i++)
  10397. PUSH_TYPE(block_type.u.type->types[i]);
  10398. }
  10399. #if WASM_ENABLE_FAST_INTERP != 0
  10400. /* Recover top param_count values of frame_offset stack */
  10401. if (BLOCK_HAS_PARAM((block_type))) {
  10402. uint32 size;
  10403. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10404. bh_memcpy_s(loader_ctx->frame_offset, size,
  10405. block->param_frame_offsets, size);
  10406. loader_ctx->frame_offset += (size / sizeof(int16));
  10407. }
  10408. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10409. #endif
  10410. break;
  10411. }
  10412. case WASM_OP_END:
  10413. {
  10414. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10415. /* check whether block stack matches its result type */
  10416. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10417. error_buf_size))
  10418. goto fail;
  10419. /* if there is no else branch, make a virtual else opcode for
  10420. easier integrity check and to copy the correct results to
  10421. the block return address for fast-interp mode:
  10422. change if block from `if ... end` to `if ... else end` */
  10423. if (cur_block->label_type == LABEL_TYPE_IF
  10424. && !cur_block->else_addr) {
  10425. opcode = WASM_OP_ELSE;
  10426. p--;
  10427. #if WASM_ENABLE_FAST_INTERP != 0
  10428. p_org = p;
  10429. skip_label();
  10430. disable_emit = false;
  10431. emit_label(opcode);
  10432. #endif
  10433. goto handle_op_else;
  10434. }
  10435. POP_CSP();
  10436. #if WASM_ENABLE_FAST_INTERP != 0
  10437. skip_label();
  10438. /* copy the result to the block return address */
  10439. RESERVE_BLOCK_RET();
  10440. apply_label_patch(loader_ctx, 0, PATCH_END);
  10441. free_label_patch_list(loader_ctx->frame_csp);
  10442. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10443. int32 idx;
  10444. uint8 ret_type;
  10445. emit_label(WASM_OP_RETURN);
  10446. for (idx = (int32)func->func_type->result_count - 1;
  10447. idx >= 0; idx--) {
  10448. ret_type = *(func->func_type->types
  10449. + func->func_type->param_count + idx);
  10450. POP_OFFSET_TYPE(ret_type);
  10451. }
  10452. }
  10453. #endif
  10454. if (loader_ctx->csp_num > 0) {
  10455. loader_ctx->frame_csp->end_addr = p - 1;
  10456. }
  10457. else {
  10458. /* end of function block, function will return */
  10459. if (p < p_end) {
  10460. set_error_buf(error_buf, error_buf_size,
  10461. "section size mismatch");
  10462. goto fail;
  10463. }
  10464. }
  10465. break;
  10466. }
  10467. case WASM_OP_BR:
  10468. {
  10469. if (!(frame_csp_tmp =
  10470. check_branch_block(loader_ctx, &p, p_end, opcode,
  10471. error_buf, error_buf_size)))
  10472. goto fail;
  10473. RESET_STACK();
  10474. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10475. break;
  10476. }
  10477. case WASM_OP_BR_IF:
  10478. {
  10479. POP_I32();
  10480. if (!(frame_csp_tmp =
  10481. check_branch_block(loader_ctx, &p, p_end, opcode,
  10482. error_buf, error_buf_size)))
  10483. goto fail;
  10484. break;
  10485. }
  10486. case WASM_OP_BR_TABLE:
  10487. {
  10488. uint32 depth = 0, default_arity, arity = 0;
  10489. BranchBlock *target_block;
  10490. BlockType *target_block_type;
  10491. #if WASM_ENABLE_FAST_INTERP == 0
  10492. BrTableCache *br_table_cache = NULL;
  10493. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10494. uint32 j;
  10495. #endif
  10496. read_leb_uint32(p, p_end, count);
  10497. #if WASM_ENABLE_FAST_INTERP != 0
  10498. emit_uint32(loader_ctx, count);
  10499. #endif
  10500. POP_I32();
  10501. /* Get each depth and check it */
  10502. p_org = p;
  10503. for (i = 0; i <= count; i++) {
  10504. read_leb_uint32(p, p_end, depth);
  10505. bh_assert(loader_ctx->csp_num > 0);
  10506. if (loader_ctx->csp_num - 1 < depth) {
  10507. set_error_buf(error_buf, error_buf_size,
  10508. "unknown label, "
  10509. "unexpected end of section or function");
  10510. goto fail;
  10511. }
  10512. }
  10513. p = p_org;
  10514. /* Get the default block's arity */
  10515. target_block = loader_ctx->frame_csp - (depth + 1);
  10516. target_block_type = &target_block->block_type;
  10517. default_arity = block_type_get_arity(target_block_type,
  10518. target_block->label_type);
  10519. #if WASM_ENABLE_FAST_INTERP == 0
  10520. p_depth_begin = p_depth = p;
  10521. #endif
  10522. for (i = 0; i <= count; i++) {
  10523. p_org = p;
  10524. read_leb_uint32(p, p_end, depth);
  10525. p = p_org;
  10526. /* Get the target block's arity and check it */
  10527. target_block = loader_ctx->frame_csp - (depth + 1);
  10528. target_block_type = &target_block->block_type;
  10529. arity = block_type_get_arity(target_block_type,
  10530. target_block->label_type);
  10531. if (arity != default_arity) {
  10532. set_error_buf(error_buf, error_buf_size,
  10533. "type mismatch: br_table targets must "
  10534. "all use same result type");
  10535. goto fail;
  10536. }
  10537. if (!(frame_csp_tmp =
  10538. check_branch_block(loader_ctx, &p, p_end, opcode,
  10539. error_buf, error_buf_size))) {
  10540. goto fail;
  10541. }
  10542. #if WASM_ENABLE_FAST_INTERP == 0
  10543. if (br_table_cache) {
  10544. br_table_cache->br_depths[i] = depth;
  10545. }
  10546. else {
  10547. if (depth > 255) {
  10548. /* The depth cannot be stored in one byte,
  10549. create br_table cache to store each depth */
  10550. #if WASM_ENABLE_DEBUG_INTERP != 0
  10551. if (!record_fast_op(module, p_opcode, *p_opcode,
  10552. error_buf, error_buf_size)) {
  10553. goto fail;
  10554. }
  10555. #endif
  10556. if (!(br_table_cache = loader_malloc(
  10557. offsetof(BrTableCache, br_depths)
  10558. + sizeof(uint32)
  10559. * (uint64)(count + 1),
  10560. error_buf, error_buf_size))) {
  10561. goto fail;
  10562. }
  10563. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10564. br_table_cache->br_table_op_addr = p_opcode;
  10565. br_table_cache->br_count = count;
  10566. /* Copy previous depths which are one byte */
  10567. for (j = 0; j < i; j++) {
  10568. br_table_cache->br_depths[j] = p_depth_begin[j];
  10569. }
  10570. br_table_cache->br_depths[i] = depth;
  10571. bh_list_insert(module->br_table_cache_list,
  10572. br_table_cache);
  10573. }
  10574. else {
  10575. /* The depth can be stored in one byte, use the
  10576. byte of the leb to store it */
  10577. *p_depth++ = (uint8)depth;
  10578. }
  10579. }
  10580. #endif
  10581. }
  10582. #if WASM_ENABLE_FAST_INTERP == 0
  10583. /* Set the tailing bytes to nop */
  10584. if (br_table_cache)
  10585. p_depth = p_depth_begin;
  10586. while (p_depth < p)
  10587. *p_depth++ = WASM_OP_NOP;
  10588. #endif
  10589. RESET_STACK();
  10590. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10591. break;
  10592. }
  10593. case WASM_OP_RETURN:
  10594. {
  10595. WASMFuncType *func_type = func->func_type;
  10596. int32 idx;
  10597. uint8 ret_type;
  10598. #if WASM_ENABLE_GC != 0
  10599. uint32 j = func_type->ref_type_map_count - 1;
  10600. #endif
  10601. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10602. idx--) {
  10603. ret_type =
  10604. *(func_type->types + func_type->param_count + idx);
  10605. #if WASM_ENABLE_GC != 0
  10606. if (wasm_is_type_multi_byte_type(ret_type)) {
  10607. WASMRefType *ref_type =
  10608. func_type->ref_type_maps[j].ref_type;
  10609. bh_assert(func_type->ref_type_maps[j].index
  10610. == func_type->param_count + idx);
  10611. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10612. ref_type,
  10613. wasm_reftype_struct_size(ref_type));
  10614. j--;
  10615. }
  10616. #endif
  10617. #if WASM_ENABLE_FAST_INTERP != 0
  10618. /* emit the offset after return opcode */
  10619. POP_OFFSET_TYPE(ret_type);
  10620. #endif
  10621. POP_TYPE(ret_type);
  10622. }
  10623. RESET_STACK();
  10624. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10625. break;
  10626. }
  10627. case WASM_OP_CALL:
  10628. #if WASM_ENABLE_TAIL_CALL != 0
  10629. case WASM_OP_RETURN_CALL:
  10630. #endif
  10631. #if WASM_ENABLE_GC != 0
  10632. case WASM_OP_CALL_REF:
  10633. case WASM_OP_RETURN_CALL_REF:
  10634. #endif
  10635. {
  10636. WASMFuncType *func_type;
  10637. uint8 type;
  10638. int32 idx;
  10639. #if WASM_ENABLE_GC != 0
  10640. WASMRefType *ref_type;
  10641. uint32 type_idx1;
  10642. int32 j;
  10643. #endif
  10644. #if WASM_ENABLE_GC != 0
  10645. if (opcode == WASM_OP_CALL_REF
  10646. || opcode == WASM_OP_RETURN_CALL_REF) {
  10647. read_leb_uint32(p, p_end, type_idx1);
  10648. if (!check_type_index(module, module->type_count, type_idx1,
  10649. error_buf, error_buf_size)) {
  10650. goto fail;
  10651. }
  10652. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10653. set_error_buf(error_buf, error_buf_size,
  10654. "unkown function type");
  10655. goto fail;
  10656. }
  10657. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10658. &type_idx, error_buf,
  10659. error_buf_size)) {
  10660. goto fail;
  10661. }
  10662. if (type == VALUE_TYPE_ANY) {
  10663. type_idx = type_idx1;
  10664. }
  10665. if (!check_type_index(module, module->type_count, type_idx,
  10666. error_buf, error_buf_size)) {
  10667. goto fail;
  10668. }
  10669. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10670. set_error_buf(error_buf, error_buf_size,
  10671. "unkown function type");
  10672. goto fail;
  10673. }
  10674. if (!wasm_func_type_is_super_of(
  10675. (WASMFuncType *)module->types[type_idx1],
  10676. (WASMFuncType *)module->types[type_idx])) {
  10677. set_error_buf(error_buf, error_buf_size,
  10678. "function type mismatch");
  10679. goto fail;
  10680. }
  10681. func_type = (WASMFuncType *)module->types[type_idx];
  10682. }
  10683. else
  10684. #endif
  10685. {
  10686. read_leb_uint32(p, p_end, func_idx);
  10687. #if WASM_ENABLE_FAST_INTERP != 0
  10688. /* we need to emit func_idx before arguments */
  10689. emit_uint32(loader_ctx, func_idx);
  10690. #endif
  10691. if (!check_function_index(module, func_idx, error_buf,
  10692. error_buf_size)) {
  10693. goto fail;
  10694. }
  10695. if (func_idx < module->import_function_count)
  10696. func_type = module->import_functions[func_idx]
  10697. .u.function.func_type;
  10698. else
  10699. func_type =
  10700. module
  10701. ->functions[func_idx
  10702. - module->import_function_count]
  10703. ->func_type;
  10704. }
  10705. if (func_type->param_count > 0) {
  10706. #if WASM_ENABLE_GC != 0
  10707. j = (int32)(func_type->result_ref_type_maps
  10708. - func_type->ref_type_maps - 1);
  10709. #endif
  10710. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10711. idx--) {
  10712. #if WASM_ENABLE_GC != 0
  10713. if (wasm_is_type_multi_byte_type(
  10714. func_type->types[idx])) {
  10715. ref_type = func_type->ref_type_maps[j].ref_type;
  10716. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10717. ref_type,
  10718. wasm_reftype_struct_size(ref_type));
  10719. j--;
  10720. }
  10721. #endif
  10722. #if WASM_ENABLE_FAST_INTERP != 0
  10723. POP_OFFSET_TYPE(func_type->types[idx]);
  10724. #endif
  10725. POP_TYPE(func_type->types[idx]);
  10726. }
  10727. }
  10728. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10729. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10730. #endif
  10731. #if WASM_ENABLE_GC != 0
  10732. j = (int32)(func_type->result_ref_type_maps
  10733. - func_type->ref_type_maps);
  10734. #endif
  10735. for (i = 0; i < func_type->result_count; i++) {
  10736. #if WASM_ENABLE_GC != 0
  10737. if (wasm_is_type_multi_byte_type(
  10738. func_type->types[func_type->param_count + i])) {
  10739. ref_type = func_type->ref_type_maps[j].ref_type;
  10740. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10741. ref_type,
  10742. wasm_reftype_struct_size(ref_type));
  10743. j++;
  10744. }
  10745. #endif
  10746. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10747. #if WASM_ENABLE_FAST_INTERP != 0
  10748. /* Here we emit each return value's dynamic_offset. But
  10749. * in fact these offsets are continuous, so interpreter
  10750. * only need to get the first return value's offset.
  10751. */
  10752. PUSH_OFFSET_TYPE(
  10753. func_type->types[func_type->param_count + i]);
  10754. #endif
  10755. }
  10756. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10757. }
  10758. else {
  10759. #if WASM_ENABLE_GC == 0
  10760. if (func_type->result_count
  10761. != func->func_type->result_count) {
  10762. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10763. "type mismatch: expect ",
  10764. func->func_type->result_count,
  10765. " return values but got other");
  10766. goto fail;
  10767. }
  10768. for (i = 0; i < func_type->result_count; i++) {
  10769. type = func->func_type
  10770. ->types[func->func_type->param_count + i];
  10771. if (func_type->types[func_type->param_count + i]
  10772. != type) {
  10773. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10774. "type mismatch: expect ",
  10775. type2str(type), " but got other");
  10776. goto fail;
  10777. }
  10778. }
  10779. #else
  10780. if (!wasm_func_type_result_is_subtype_of(
  10781. func_type, func->func_type, module->types,
  10782. module->type_count)) {
  10783. set_error_buf(
  10784. error_buf, error_buf_size,
  10785. "type mismatch: invalid func result types");
  10786. goto fail;
  10787. }
  10788. #endif
  10789. RESET_STACK();
  10790. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10791. }
  10792. #endif
  10793. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10794. || WASM_ENABLE_WAMR_COMPILER != 0
  10795. func->has_op_func_call = true;
  10796. #endif
  10797. (void)type;
  10798. break;
  10799. }
  10800. /*
  10801. * if disable reference type: call_indirect typeidx, 0x00
  10802. * if enable reference type: call_indirect typeidx, tableidx
  10803. */
  10804. case WASM_OP_CALL_INDIRECT:
  10805. #if WASM_ENABLE_TAIL_CALL != 0
  10806. case WASM_OP_RETURN_CALL_INDIRECT:
  10807. #endif
  10808. {
  10809. int32 idx;
  10810. WASMFuncType *func_type;
  10811. read_leb_uint32(p, p_end, type_idx);
  10812. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10813. read_leb_uint32(p, p_end, table_idx);
  10814. #else
  10815. CHECK_BUF(p, p_end, 1);
  10816. table_idx = read_uint8(p);
  10817. #endif
  10818. if (!check_table_index(module, table_idx, error_buf,
  10819. error_buf_size)) {
  10820. goto fail;
  10821. }
  10822. #if WASM_ENABLE_FAST_INTERP != 0
  10823. /* we need to emit before arguments */
  10824. #if WASM_ENABLE_TAIL_CALL != 0
  10825. emit_byte(loader_ctx, opcode);
  10826. #endif
  10827. emit_uint32(loader_ctx, type_idx);
  10828. emit_uint32(loader_ctx, table_idx);
  10829. #endif
  10830. /* skip elem idx */
  10831. POP_I32();
  10832. if (type_idx >= module->type_count) {
  10833. set_error_buf(error_buf, error_buf_size, "unknown type");
  10834. goto fail;
  10835. }
  10836. func_type = (WASMFuncType *)module->types[type_idx];
  10837. if (func_type->param_count > 0) {
  10838. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10839. idx--) {
  10840. #if WASM_ENABLE_FAST_INTERP != 0
  10841. POP_OFFSET_TYPE(func_type->types[idx]);
  10842. #endif
  10843. POP_TYPE(func_type->types[idx]);
  10844. }
  10845. }
  10846. #if WASM_ENABLE_TAIL_CALL != 0
  10847. if (opcode == WASM_OP_CALL_INDIRECT) {
  10848. #endif
  10849. for (i = 0; i < func_type->result_count; i++) {
  10850. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10851. #if WASM_ENABLE_FAST_INTERP != 0
  10852. PUSH_OFFSET_TYPE(
  10853. func_type->types[func_type->param_count + i]);
  10854. #endif
  10855. }
  10856. #if WASM_ENABLE_TAIL_CALL != 0
  10857. }
  10858. else {
  10859. uint8 type;
  10860. if (func_type->result_count
  10861. != func->func_type->result_count) {
  10862. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10863. "type mismatch: expect ",
  10864. func->func_type->result_count,
  10865. " return values but got other");
  10866. goto fail;
  10867. }
  10868. for (i = 0; i < func_type->result_count; i++) {
  10869. type = func->func_type
  10870. ->types[func->func_type->param_count + i];
  10871. if (func_type->types[func_type->param_count + i]
  10872. != type) {
  10873. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10874. "type mismatch: expect ",
  10875. type2str(type), " but got other");
  10876. goto fail;
  10877. }
  10878. }
  10879. RESET_STACK();
  10880. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10881. }
  10882. #endif
  10883. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10884. || WASM_ENABLE_WAMR_COMPILER != 0
  10885. func->has_op_func_call = true;
  10886. #endif
  10887. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10888. func->has_op_call_indirect = true;
  10889. #endif
  10890. break;
  10891. }
  10892. case WASM_OP_DROP:
  10893. {
  10894. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10895. int32 available_stack_cell =
  10896. (int32)(loader_ctx->stack_cell_num
  10897. - cur_block->stack_cell_num);
  10898. if (available_stack_cell <= 0
  10899. && !cur_block->is_stack_polymorphic) {
  10900. set_error_buf(error_buf, error_buf_size,
  10901. "type mismatch, opcode drop was found "
  10902. "but stack was empty");
  10903. goto fail;
  10904. }
  10905. if (available_stack_cell > 0) {
  10906. #if WASM_ENABLE_GC != 0
  10907. if (wasm_is_type_multi_byte_type(
  10908. *(loader_ctx->frame_ref - 1))) {
  10909. bh_assert((int32)(loader_ctx->reftype_map_num
  10910. - cur_block->reftype_map_num)
  10911. > 0);
  10912. loader_ctx->frame_reftype_map--;
  10913. loader_ctx->reftype_map_num--;
  10914. }
  10915. #endif
  10916. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10917. loader_ctx->frame_ref--;
  10918. loader_ctx->stack_cell_num--;
  10919. #if WASM_ENABLE_FAST_INTERP != 0
  10920. skip_label();
  10921. loader_ctx->frame_offset--;
  10922. if ((*(loader_ctx->frame_offset)
  10923. > loader_ctx->start_dynamic_offset)
  10924. && (*(loader_ctx->frame_offset)
  10925. < loader_ctx->max_dynamic_offset))
  10926. loader_ctx->dynamic_offset--;
  10927. #endif
  10928. }
  10929. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10930. loader_ctx->frame_ref -= 2;
  10931. loader_ctx->stack_cell_num -= 2;
  10932. #if WASM_ENABLE_FAST_INTERP == 0
  10933. *(p - 1) = WASM_OP_DROP_64;
  10934. #endif
  10935. #if WASM_ENABLE_FAST_INTERP != 0
  10936. skip_label();
  10937. loader_ctx->frame_offset -= 2;
  10938. if ((*(loader_ctx->frame_offset)
  10939. > loader_ctx->start_dynamic_offset)
  10940. && (*(loader_ctx->frame_offset)
  10941. < loader_ctx->max_dynamic_offset))
  10942. loader_ctx->dynamic_offset -= 2;
  10943. #endif
  10944. }
  10945. #if WASM_ENABLE_SIMD != 0
  10946. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10947. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10948. loader_ctx->frame_ref -= 4;
  10949. loader_ctx->stack_cell_num -= 4;
  10950. }
  10951. #endif
  10952. #endif
  10953. else {
  10954. set_error_buf(error_buf, error_buf_size,
  10955. "type mismatch");
  10956. goto fail;
  10957. }
  10958. }
  10959. else {
  10960. #if WASM_ENABLE_FAST_INTERP != 0
  10961. skip_label();
  10962. #endif
  10963. }
  10964. break;
  10965. }
  10966. case WASM_OP_SELECT:
  10967. {
  10968. uint8 ref_type;
  10969. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10970. int32 available_stack_cell;
  10971. #if WASM_ENABLE_FAST_INTERP != 0
  10972. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10973. #endif
  10974. POP_I32();
  10975. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10976. - cur_block->stack_cell_num);
  10977. if (available_stack_cell <= 0
  10978. && !cur_block->is_stack_polymorphic) {
  10979. set_error_buf(error_buf, error_buf_size,
  10980. "type mismatch or invalid result arity, "
  10981. "opcode select was found "
  10982. "but stack was empty");
  10983. goto fail;
  10984. }
  10985. if (available_stack_cell > 0) {
  10986. switch (*(loader_ctx->frame_ref - 1)) {
  10987. case VALUE_TYPE_I32:
  10988. case VALUE_TYPE_F32:
  10989. case VALUE_TYPE_ANY:
  10990. break;
  10991. case VALUE_TYPE_I64:
  10992. case VALUE_TYPE_F64:
  10993. #if WASM_ENABLE_FAST_INTERP == 0
  10994. *(p - 1) = WASM_OP_SELECT_64;
  10995. #endif
  10996. #if WASM_ENABLE_FAST_INTERP != 0
  10997. if (loader_ctx->p_code_compiled) {
  10998. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10999. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11000. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11001. *(void **)(p_code_compiled_tmp
  11002. - sizeof(void *)) =
  11003. handle_table[opcode_tmp];
  11004. #else
  11005. #if UINTPTR_MAX == UINT64_MAX
  11006. /* emit int32 relative offset in 64-bit target
  11007. */
  11008. int32 offset =
  11009. (int32)((uint8 *)handle_table[opcode_tmp]
  11010. - (uint8 *)handle_table[0]);
  11011. *(int32 *)(p_code_compiled_tmp
  11012. - sizeof(int32)) = offset;
  11013. #else
  11014. /* emit uint32 label address in 32-bit target */
  11015. *(uint32 *)(p_code_compiled_tmp
  11016. - sizeof(uint32)) =
  11017. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11018. #endif
  11019. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11020. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11021. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11022. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11023. #else
  11024. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11025. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11026. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11027. }
  11028. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11029. break;
  11030. #if WASM_ENABLE_SIMD != 0
  11031. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  11032. case VALUE_TYPE_V128:
  11033. break;
  11034. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  11035. #endif /* WASM_ENABLE_SIMD != 0 */
  11036. default:
  11037. {
  11038. set_error_buf(error_buf, error_buf_size,
  11039. "type mismatch");
  11040. goto fail;
  11041. }
  11042. }
  11043. ref_type = *(loader_ctx->frame_ref - 1);
  11044. #if WASM_ENABLE_FAST_INTERP != 0
  11045. POP_OFFSET_TYPE(ref_type);
  11046. POP_TYPE(ref_type);
  11047. POP_OFFSET_TYPE(ref_type);
  11048. POP_TYPE(ref_type);
  11049. PUSH_OFFSET_TYPE(ref_type);
  11050. PUSH_TYPE(ref_type);
  11051. #else
  11052. POP2_AND_PUSH(ref_type, ref_type);
  11053. #endif
  11054. }
  11055. else {
  11056. #if WASM_ENABLE_FAST_INTERP != 0
  11057. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11058. #endif
  11059. PUSH_TYPE(VALUE_TYPE_ANY);
  11060. }
  11061. break;
  11062. }
  11063. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11064. case WASM_OP_SELECT_T:
  11065. {
  11066. uint8 vec_len, type;
  11067. #if WASM_ENABLE_GC != 0
  11068. WASMRefType *ref_type = NULL;
  11069. #endif
  11070. #if WASM_ENABLE_FAST_INTERP != 0
  11071. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11072. #endif
  11073. read_leb_uint32(p, p_end, vec_len);
  11074. if (vec_len != 1) {
  11075. /* typed select must have exactly one result */
  11076. set_error_buf(error_buf, error_buf_size,
  11077. "invalid result arity");
  11078. goto fail;
  11079. }
  11080. #if WASM_ENABLE_GC == 0
  11081. CHECK_BUF(p, p_end, 1);
  11082. type = read_uint8(p);
  11083. if (!is_valid_value_type(type)) {
  11084. set_error_buf(error_buf, error_buf_size,
  11085. "unknown value type");
  11086. goto fail;
  11087. }
  11088. #else
  11089. p_org = p + 1;
  11090. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11091. module->type_count, &need_ref_type_map,
  11092. &wasm_ref_type, false, error_buf,
  11093. error_buf_size)) {
  11094. goto fail;
  11095. }
  11096. type = wasm_ref_type.ref_type;
  11097. if (need_ref_type_map) {
  11098. if (!(ref_type = reftype_set_insert(
  11099. module->ref_type_set, &wasm_ref_type, error_buf,
  11100. error_buf_size))) {
  11101. goto fail;
  11102. }
  11103. }
  11104. #if WASM_ENABLE_FAST_INTERP == 0
  11105. while (p_org < p) {
  11106. #if WASM_ENABLE_DEBUG_INTERP != 0
  11107. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11108. error_buf_size)) {
  11109. goto fail;
  11110. }
  11111. #endif
  11112. /* Ignore extra bytes for interpreter */
  11113. *p_org++ = WASM_OP_NOP;
  11114. }
  11115. #endif
  11116. #endif /* end of WASM_ENABLE_GC == 0 */
  11117. POP_I32();
  11118. #if WASM_ENABLE_FAST_INTERP != 0
  11119. if (loader_ctx->p_code_compiled) {
  11120. uint8 opcode_tmp = WASM_OP_SELECT;
  11121. if (type == VALUE_TYPE_V128) {
  11122. #if (WASM_ENABLE_SIMD == 0) \
  11123. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  11124. set_error_buf(error_buf, error_buf_size,
  11125. "SIMD v128 type isn't supported");
  11126. goto fail;
  11127. #endif
  11128. }
  11129. else {
  11130. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11131. opcode_tmp = WASM_OP_SELECT_64;
  11132. #if WASM_ENABLE_GC != 0
  11133. if (wasm_is_type_reftype(type))
  11134. opcode_tmp = WASM_OP_SELECT_T;
  11135. #endif
  11136. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11137. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11138. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11139. handle_table[opcode_tmp];
  11140. #else
  11141. #if UINTPTR_MAX == UINT64_MAX
  11142. /* emit int32 relative offset in 64-bit target */
  11143. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11144. - (uint8 *)handle_table[0]);
  11145. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11146. offset;
  11147. #else
  11148. /* emit uint32 label address in 32-bit target */
  11149. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11150. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11151. #endif
  11152. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11153. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11154. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11155. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11156. #else
  11157. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11158. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11159. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11160. }
  11161. }
  11162. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11163. POP_REF(type);
  11164. #if WASM_ENABLE_GC != 0
  11165. if (need_ref_type_map) {
  11166. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11167. wasm_reftype_struct_size(ref_type));
  11168. }
  11169. #endif
  11170. POP_REF(type);
  11171. #if WASM_ENABLE_GC != 0
  11172. if (need_ref_type_map) {
  11173. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11174. wasm_reftype_struct_size(ref_type));
  11175. }
  11176. #endif
  11177. PUSH_REF(type);
  11178. #if WASM_ENABLE_WAMR_COMPILER != 0
  11179. module->is_ref_types_used = true;
  11180. #endif
  11181. (void)vec_len;
  11182. break;
  11183. }
  11184. /* table.get x. tables[x]. [i32] -> [t] */
  11185. /* table.set x. tables[x]. [i32 t] -> [] */
  11186. case WASM_OP_TABLE_GET:
  11187. case WASM_OP_TABLE_SET:
  11188. {
  11189. uint8 decl_ref_type;
  11190. #if WASM_ENABLE_GC != 0
  11191. WASMRefType *ref_type;
  11192. #endif
  11193. read_leb_uint32(p, p_end, table_idx);
  11194. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11195. #if WASM_ENABLE_GC != 0
  11196. (void **)&ref_type,
  11197. #else
  11198. NULL,
  11199. #endif
  11200. error_buf, error_buf_size))
  11201. goto fail;
  11202. #if WASM_ENABLE_GC != 0
  11203. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11204. bh_assert(ref_type);
  11205. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11206. ref_type, wasm_reftype_struct_size(ref_type));
  11207. }
  11208. #endif
  11209. #if WASM_ENABLE_FAST_INTERP != 0
  11210. emit_uint32(loader_ctx, table_idx);
  11211. #endif
  11212. if (opcode == WASM_OP_TABLE_GET) {
  11213. POP_I32();
  11214. #if WASM_ENABLE_FAST_INTERP != 0
  11215. PUSH_OFFSET_TYPE(decl_ref_type);
  11216. #endif
  11217. PUSH_TYPE(decl_ref_type);
  11218. }
  11219. else {
  11220. #if WASM_ENABLE_FAST_INTERP != 0
  11221. POP_OFFSET_TYPE(decl_ref_type);
  11222. #endif
  11223. POP_TYPE(decl_ref_type);
  11224. POP_I32();
  11225. }
  11226. #if WASM_ENABLE_WAMR_COMPILER != 0
  11227. module->is_ref_types_used = true;
  11228. #endif
  11229. break;
  11230. }
  11231. case WASM_OP_REF_NULL:
  11232. {
  11233. uint8 ref_type;
  11234. #if WASM_ENABLE_GC == 0
  11235. CHECK_BUF(p, p_end, 1);
  11236. ref_type = read_uint8(p);
  11237. if (ref_type != VALUE_TYPE_FUNCREF
  11238. && ref_type != VALUE_TYPE_EXTERNREF) {
  11239. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11240. goto fail;
  11241. }
  11242. #else
  11243. read_leb_int32(p, p_end, heap_type);
  11244. if (heap_type >= 0) {
  11245. if (!check_type_index(module, module->type_count, heap_type,
  11246. error_buf, error_buf_size)) {
  11247. goto fail;
  11248. }
  11249. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11250. true, heap_type);
  11251. ref_type = wasm_ref_type.ref_type;
  11252. }
  11253. else {
  11254. if (!wasm_is_valid_heap_type(heap_type)) {
  11255. set_error_buf(error_buf, error_buf_size,
  11256. "unknown type");
  11257. goto fail;
  11258. }
  11259. ref_type = (uint8)((int32)0x80 + heap_type);
  11260. }
  11261. #endif /* end of WASM_ENABLE_GC == 0 */
  11262. #if WASM_ENABLE_FAST_INTERP != 0
  11263. PUSH_OFFSET_TYPE(ref_type);
  11264. #endif
  11265. PUSH_TYPE(ref_type);
  11266. #if WASM_ENABLE_WAMR_COMPILER != 0
  11267. module->is_ref_types_used = true;
  11268. #endif
  11269. break;
  11270. }
  11271. case WASM_OP_REF_IS_NULL:
  11272. {
  11273. #if WASM_ENABLE_GC == 0
  11274. #if WASM_ENABLE_FAST_INTERP != 0
  11275. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11276. int32 block_stack_cell_num =
  11277. (int32)(loader_ctx->stack_cell_num
  11278. - cur_block->stack_cell_num);
  11279. if (block_stack_cell_num <= 0) {
  11280. if (!cur_block->is_stack_polymorphic) {
  11281. set_error_buf(
  11282. error_buf, error_buf_size,
  11283. "type mismatch: expect data but stack was empty");
  11284. goto fail;
  11285. }
  11286. }
  11287. else {
  11288. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11289. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11290. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11291. if (!wasm_loader_pop_frame_ref_offset(
  11292. loader_ctx, *(loader_ctx->frame_ref - 1),
  11293. error_buf, error_buf_size)) {
  11294. goto fail;
  11295. }
  11296. }
  11297. else {
  11298. set_error_buf(error_buf, error_buf_size,
  11299. "type mismatch");
  11300. goto fail;
  11301. }
  11302. }
  11303. #else
  11304. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11305. error_buf, error_buf_size)
  11306. && !wasm_loader_pop_frame_ref(loader_ctx,
  11307. VALUE_TYPE_EXTERNREF,
  11308. error_buf, error_buf_size)) {
  11309. goto fail;
  11310. }
  11311. #endif
  11312. #else /* else of WASM_ENABLE_GC == 0 */
  11313. uint8 type;
  11314. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11315. error_buf, error_buf_size)) {
  11316. goto fail;
  11317. }
  11318. #endif
  11319. PUSH_I32();
  11320. #if WASM_ENABLE_WAMR_COMPILER != 0
  11321. module->is_ref_types_used = true;
  11322. #endif
  11323. break;
  11324. }
  11325. case WASM_OP_REF_FUNC:
  11326. {
  11327. read_leb_uint32(p, p_end, func_idx);
  11328. if (!check_function_index(module, func_idx, error_buf,
  11329. error_buf_size)) {
  11330. goto fail;
  11331. }
  11332. /* Refer to a forward-declared function:
  11333. the function must be an import, exported, or present in
  11334. a table elem segment or global initializer to be used as
  11335. the operand to ref.func */
  11336. if (func_idx >= module->import_function_count) {
  11337. WASMTableSeg *table_seg = module->table_segments;
  11338. bool func_declared = false;
  11339. uint32 j;
  11340. for (i = 0; i < module->global_count; i++) {
  11341. if (module->globals[i].type.val_type
  11342. == VALUE_TYPE_FUNCREF
  11343. && module->globals[i].init_expr.init_expr_type
  11344. == INIT_EXPR_TYPE_FUNCREF_CONST
  11345. && module->globals[i].init_expr.u.u32 == func_idx) {
  11346. func_declared = true;
  11347. break;
  11348. }
  11349. }
  11350. if (!func_declared) {
  11351. /* Check whether the function is declared in table segs,
  11352. note that it doesn't matter whether the table seg's
  11353. mode is passive, active or declarative. */
  11354. for (i = 0; i < module->table_seg_count;
  11355. i++, table_seg++) {
  11356. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11357. #if WASM_ENABLE_GC != 0
  11358. /* elem type is (ref null? func) or
  11359. (ref null? $t) */
  11360. || ((table_seg->elem_type
  11361. == REF_TYPE_HT_NON_NULLABLE
  11362. || table_seg->elem_type
  11363. == REF_TYPE_HT_NULLABLE)
  11364. && (table_seg->elem_ref_type->ref_ht_common
  11365. .heap_type
  11366. == HEAP_TYPE_FUNC
  11367. || table_seg->elem_ref_type
  11368. ->ref_ht_common.heap_type
  11369. > 0))
  11370. #endif
  11371. ) {
  11372. for (j = 0; j < table_seg->value_count; j++) {
  11373. if (table_seg->init_values[j].u.ref_index
  11374. == func_idx) {
  11375. func_declared = true;
  11376. break;
  11377. }
  11378. }
  11379. }
  11380. }
  11381. }
  11382. if (!func_declared) {
  11383. /* Check whether the function is exported */
  11384. for (i = 0; i < module->export_count; i++) {
  11385. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11386. && module->exports[i].index == func_idx) {
  11387. func_declared = true;
  11388. break;
  11389. }
  11390. }
  11391. }
  11392. if (!func_declared) {
  11393. set_error_buf(error_buf, error_buf_size,
  11394. "undeclared function reference");
  11395. goto fail;
  11396. }
  11397. }
  11398. #if WASM_ENABLE_FAST_INTERP != 0
  11399. emit_uint32(loader_ctx, func_idx);
  11400. #endif
  11401. #if WASM_ENABLE_GC == 0
  11402. PUSH_FUNCREF();
  11403. #else
  11404. if (func_idx < module->import_function_count)
  11405. type_idx =
  11406. module->import_functions[func_idx].u.function.type_idx;
  11407. else
  11408. type_idx = module
  11409. ->functions[func_idx
  11410. - module->import_function_count]
  11411. ->type_idx;
  11412. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11413. false, type_idx);
  11414. PUSH_REF(wasm_ref_type.ref_type);
  11415. #endif
  11416. #if WASM_ENABLE_WAMR_COMPILER != 0
  11417. module->is_ref_types_used = true;
  11418. #endif
  11419. break;
  11420. }
  11421. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11422. #if WASM_ENABLE_GC != 0
  11423. case WASM_OP_REF_AS_NON_NULL:
  11424. case WASM_OP_BR_ON_NULL:
  11425. {
  11426. uint8 type;
  11427. WASMRefType ref_type;
  11428. /* POP (ref null ht) and get the converted (ref ht) */
  11429. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11430. error_buf, error_buf_size)) {
  11431. goto fail;
  11432. }
  11433. if (opcode == WASM_OP_BR_ON_NULL) {
  11434. if (!(frame_csp_tmp =
  11435. check_branch_block(loader_ctx, &p, p_end, opcode,
  11436. error_buf, error_buf_size))) {
  11437. goto fail;
  11438. }
  11439. #if WASM_ENABLE_FAST_INTERP != 0
  11440. disable_emit = true;
  11441. #endif
  11442. }
  11443. /* PUSH the converted (ref ht) */
  11444. if (type != VALUE_TYPE_ANY) {
  11445. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11446. sizeof(WASMRefType));
  11447. }
  11448. PUSH_REF(type);
  11449. break;
  11450. }
  11451. case WASM_OP_BR_ON_NON_NULL:
  11452. {
  11453. uint8 type;
  11454. WASMRefType ref_type;
  11455. uint32 available_stack_cell =
  11456. loader_ctx->stack_cell_num
  11457. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11458. /* POP (ref null ht) and get the converted (ref ht) */
  11459. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11460. error_buf, error_buf_size)) {
  11461. goto fail;
  11462. }
  11463. #if WASM_ENABLE_FAST_INTERP != 0
  11464. disable_emit = true;
  11465. #endif
  11466. /* Temporarily PUSH back (ref ht), check brach block and
  11467. then POP it */
  11468. if (available_stack_cell
  11469. > 0) { /* stack isn't in polymorphic state */
  11470. if (type != VALUE_TYPE_ANY) {
  11471. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11472. &ref_type, sizeof(WASMRefType));
  11473. }
  11474. PUSH_REF(type);
  11475. }
  11476. if (!(frame_csp_tmp =
  11477. check_branch_block(loader_ctx, &p, p_end, opcode,
  11478. error_buf, error_buf_size))) {
  11479. goto fail;
  11480. }
  11481. if (available_stack_cell
  11482. > 0) { /* stack isn't in polymorphic state */
  11483. POP_REF(type);
  11484. #if WASM_ENABLE_FAST_INTERP != 0
  11485. /* Erase the opnd offset emitted by POP_REF() */
  11486. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11487. #endif
  11488. }
  11489. break;
  11490. }
  11491. case WASM_OP_REF_EQ:
  11492. POP_REF(REF_TYPE_EQREF);
  11493. POP_REF(REF_TYPE_EQREF);
  11494. PUSH_I32();
  11495. break;
  11496. #endif /* end of WASM_ENABLE_GC != 0 */
  11497. case WASM_OP_GET_LOCAL:
  11498. {
  11499. p_org = p - 1;
  11500. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11501. PUSH_TYPE(local_type);
  11502. #if WASM_ENABLE_GC != 0
  11503. /* Cannot get a non-nullable and unset local */
  11504. if (local_idx >= param_count
  11505. && wasm_is_reftype_htref_non_nullable(local_type)
  11506. && !wasm_loader_get_local_status(loader_ctx,
  11507. local_idx - param_count)) {
  11508. set_error_buf(error_buf, error_buf_size,
  11509. "uninitialized local");
  11510. goto fail;
  11511. }
  11512. #endif
  11513. #if WASM_ENABLE_FAST_INTERP != 0
  11514. /* Get Local is optimized out */
  11515. skip_label();
  11516. disable_emit = true;
  11517. operand_offset = local_offset;
  11518. PUSH_OFFSET_TYPE(local_type);
  11519. #else
  11520. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11521. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11522. if (local_offset < 0x80
  11523. #if WASM_ENABLE_GC != 0
  11524. && !wasm_is_type_reftype(local_type)
  11525. #endif
  11526. ) {
  11527. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11528. if (is_32bit_type(local_type)) {
  11529. *p_org++ = (uint8)local_offset;
  11530. }
  11531. else {
  11532. *p_org++ = (uint8)(local_offset | 0x80);
  11533. }
  11534. while (p_org < p) {
  11535. *p_org++ = WASM_OP_NOP;
  11536. }
  11537. }
  11538. #endif
  11539. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11540. break;
  11541. }
  11542. case WASM_OP_SET_LOCAL:
  11543. {
  11544. p_org = p - 1;
  11545. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11546. #if WASM_ENABLE_FAST_INTERP != 0
  11547. if (!(preserve_referenced_local(
  11548. loader_ctx, opcode, local_offset, local_type,
  11549. &preserve_local, error_buf, error_buf_size)))
  11550. goto fail;
  11551. if (local_offset < 256
  11552. #if WASM_ENABLE_GC != 0
  11553. && !wasm_is_type_reftype(local_type)
  11554. #endif
  11555. ) {
  11556. skip_label();
  11557. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11558. if (loader_ctx->p_code_compiled)
  11559. STORE_U16(loader_ctx->p_code_compiled - 2,
  11560. local_offset);
  11561. loader_ctx->frame_offset--;
  11562. loader_ctx->dynamic_offset--;
  11563. }
  11564. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11565. if (loader_ctx->p_code_compiled)
  11566. STORE_U16(loader_ctx->p_code_compiled - 2,
  11567. local_offset);
  11568. loader_ctx->frame_offset -= 2;
  11569. loader_ctx->dynamic_offset -= 2;
  11570. }
  11571. else {
  11572. if (is_32bit_type(local_type)) {
  11573. emit_label(EXT_OP_SET_LOCAL_FAST);
  11574. emit_byte(loader_ctx, (uint8)local_offset);
  11575. }
  11576. else {
  11577. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11578. emit_byte(loader_ctx, (uint8)local_offset);
  11579. }
  11580. POP_OFFSET_TYPE(local_type);
  11581. }
  11582. }
  11583. else { /* local index larger than 255, reserve leb */
  11584. emit_uint32(loader_ctx, local_idx);
  11585. POP_OFFSET_TYPE(local_type);
  11586. }
  11587. #else
  11588. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11589. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11590. if (local_offset < 0x80
  11591. #if WASM_ENABLE_GC != 0
  11592. && !wasm_is_type_reftype(local_type)
  11593. #endif
  11594. ) {
  11595. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11596. if (is_32bit_type(local_type)) {
  11597. *p_org++ = (uint8)local_offset;
  11598. }
  11599. else {
  11600. *p_org++ = (uint8)(local_offset | 0x80);
  11601. }
  11602. while (p_org < p) {
  11603. *p_org++ = WASM_OP_NOP;
  11604. }
  11605. }
  11606. #endif
  11607. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11608. #if WASM_ENABLE_GC != 0
  11609. if (local_idx >= param_count) {
  11610. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11611. }
  11612. #endif
  11613. POP_TYPE(local_type);
  11614. break;
  11615. }
  11616. case WASM_OP_TEE_LOCAL:
  11617. {
  11618. p_org = p - 1;
  11619. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11620. #if WASM_ENABLE_FAST_INTERP != 0
  11621. /* If the stack is in polymorphic state, do fake pop and push on
  11622. offset stack to keep the depth of offset stack to be the
  11623. same with ref stack */
  11624. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11625. if (cur_block->is_stack_polymorphic) {
  11626. POP_OFFSET_TYPE(local_type);
  11627. PUSH_OFFSET_TYPE(local_type);
  11628. }
  11629. #endif
  11630. POP_TYPE(local_type);
  11631. PUSH_TYPE(local_type);
  11632. #if WASM_ENABLE_FAST_INTERP != 0
  11633. if (!(preserve_referenced_local(
  11634. loader_ctx, opcode, local_offset, local_type,
  11635. &preserve_local, error_buf, error_buf_size)))
  11636. goto fail;
  11637. if (local_offset < 256
  11638. #if WASM_ENABLE_GC != 0
  11639. && !wasm_is_type_reftype(local_type)
  11640. #endif
  11641. ) {
  11642. skip_label();
  11643. if (is_32bit_type(local_type)) {
  11644. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11645. emit_byte(loader_ctx, (uint8)local_offset);
  11646. }
  11647. else {
  11648. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11649. emit_byte(loader_ctx, (uint8)local_offset);
  11650. }
  11651. }
  11652. else { /* local index larger than 255, reserve leb */
  11653. emit_uint32(loader_ctx, local_idx);
  11654. }
  11655. emit_operand(loader_ctx,
  11656. *(loader_ctx->frame_offset
  11657. - wasm_value_type_cell_num(local_type)));
  11658. #else
  11659. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11660. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11661. if (local_offset < 0x80
  11662. #if WASM_ENABLE_GC != 0
  11663. && !wasm_is_type_reftype(local_type)
  11664. #endif
  11665. ) {
  11666. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11667. if (is_32bit_type(local_type)) {
  11668. *p_org++ = (uint8)local_offset;
  11669. }
  11670. else {
  11671. *p_org++ = (uint8)(local_offset | 0x80);
  11672. }
  11673. while (p_org < p) {
  11674. *p_org++ = WASM_OP_NOP;
  11675. }
  11676. }
  11677. #endif
  11678. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11679. #if WASM_ENABLE_GC != 0
  11680. if (local_idx >= param_count) {
  11681. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11682. }
  11683. #endif
  11684. break;
  11685. }
  11686. case WASM_OP_GET_GLOBAL:
  11687. {
  11688. #if WASM_ENABLE_GC != 0
  11689. WASMRefType *ref_type;
  11690. #endif
  11691. p_org = p - 1;
  11692. read_leb_uint32(p, p_end, global_idx);
  11693. if (global_idx >= global_count) {
  11694. set_error_buf(error_buf, error_buf_size, "unknown global");
  11695. goto fail;
  11696. }
  11697. global_type = global_idx < module->import_global_count
  11698. ? module->import_globals[global_idx]
  11699. .u.global.type.val_type
  11700. : module
  11701. ->globals[global_idx
  11702. - module->import_global_count]
  11703. .type.val_type;
  11704. #if WASM_ENABLE_GC != 0
  11705. ref_type =
  11706. global_idx < module->import_global_count
  11707. ? module->import_globals[global_idx].u.global.ref_type
  11708. : module
  11709. ->globals[global_idx
  11710. - module->import_global_count]
  11711. .ref_type;
  11712. if (wasm_is_type_multi_byte_type(global_type)) {
  11713. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11714. wasm_reftype_struct_size(ref_type));
  11715. }
  11716. #endif
  11717. PUSH_TYPE(global_type);
  11718. #if WASM_ENABLE_FAST_INTERP == 0
  11719. if (global_type == VALUE_TYPE_I64
  11720. || global_type == VALUE_TYPE_F64) {
  11721. #if WASM_ENABLE_DEBUG_INTERP != 0
  11722. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11723. error_buf_size)) {
  11724. goto fail;
  11725. }
  11726. #endif
  11727. *p_org = WASM_OP_GET_GLOBAL_64;
  11728. }
  11729. #else /* else of WASM_ENABLE_FAST_INTERP */
  11730. if (global_type == VALUE_TYPE_I64
  11731. || global_type == VALUE_TYPE_F64) {
  11732. skip_label();
  11733. emit_label(WASM_OP_GET_GLOBAL_64);
  11734. }
  11735. emit_uint32(loader_ctx, global_idx);
  11736. PUSH_OFFSET_TYPE(global_type);
  11737. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11738. break;
  11739. }
  11740. case WASM_OP_SET_GLOBAL:
  11741. {
  11742. bool is_mutable = false;
  11743. #if WASM_ENABLE_GC != 0
  11744. WASMRefType *ref_type;
  11745. #endif
  11746. p_org = p - 1;
  11747. read_leb_uint32(p, p_end, global_idx);
  11748. if (global_idx >= global_count) {
  11749. set_error_buf(error_buf, error_buf_size, "unknown global");
  11750. goto fail;
  11751. }
  11752. is_mutable = global_idx < module->import_global_count
  11753. ? module->import_globals[global_idx]
  11754. .u.global.type.is_mutable
  11755. : module
  11756. ->globals[global_idx
  11757. - module->import_global_count]
  11758. .type.is_mutable;
  11759. if (!is_mutable) {
  11760. #if WASM_ENABLE_GC == 0
  11761. set_error_buf(error_buf, error_buf_size,
  11762. "global is immutable");
  11763. #else
  11764. set_error_buf(error_buf, error_buf_size,
  11765. "immutable global");
  11766. #endif
  11767. goto fail;
  11768. }
  11769. global_type = global_idx < module->import_global_count
  11770. ? module->import_globals[global_idx]
  11771. .u.global.type.val_type
  11772. : module
  11773. ->globals[global_idx
  11774. - module->import_global_count]
  11775. .type.val_type;
  11776. #if WASM_ENABLE_GC != 0
  11777. ref_type =
  11778. global_idx < module->import_global_count
  11779. ? module->import_globals[global_idx].u.global.ref_type
  11780. : module
  11781. ->globals[global_idx
  11782. - module->import_global_count]
  11783. .ref_type;
  11784. if (wasm_is_type_multi_byte_type(global_type)) {
  11785. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11786. wasm_reftype_struct_size(ref_type));
  11787. }
  11788. #endif
  11789. #if WASM_ENABLE_FAST_INTERP == 0
  11790. if (global_type == VALUE_TYPE_I64
  11791. || global_type == VALUE_TYPE_F64) {
  11792. #if WASM_ENABLE_DEBUG_INTERP != 0
  11793. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11794. error_buf_size)) {
  11795. goto fail;
  11796. }
  11797. #endif
  11798. *p_org = WASM_OP_SET_GLOBAL_64;
  11799. }
  11800. else if (module->aux_stack_size > 0
  11801. && global_idx == module->aux_stack_top_global_index) {
  11802. #if WASM_ENABLE_DEBUG_INTERP != 0
  11803. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11804. error_buf_size)) {
  11805. goto fail;
  11806. }
  11807. #endif
  11808. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11809. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11810. func->has_op_set_global_aux_stack = true;
  11811. #endif
  11812. }
  11813. #else /* else of WASM_ENABLE_FAST_INTERP */
  11814. if (global_type == VALUE_TYPE_I64
  11815. || global_type == VALUE_TYPE_F64) {
  11816. skip_label();
  11817. emit_label(WASM_OP_SET_GLOBAL_64);
  11818. }
  11819. else if (module->aux_stack_size > 0
  11820. && global_idx == module->aux_stack_top_global_index) {
  11821. skip_label();
  11822. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11823. }
  11824. emit_uint32(loader_ctx, global_idx);
  11825. POP_OFFSET_TYPE(global_type);
  11826. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11827. POP_TYPE(global_type);
  11828. break;
  11829. }
  11830. /* load */
  11831. case WASM_OP_I32_LOAD:
  11832. case WASM_OP_I32_LOAD8_S:
  11833. case WASM_OP_I32_LOAD8_U:
  11834. case WASM_OP_I32_LOAD16_S:
  11835. case WASM_OP_I32_LOAD16_U:
  11836. case WASM_OP_I64_LOAD:
  11837. case WASM_OP_I64_LOAD8_S:
  11838. case WASM_OP_I64_LOAD8_U:
  11839. case WASM_OP_I64_LOAD16_S:
  11840. case WASM_OP_I64_LOAD16_U:
  11841. case WASM_OP_I64_LOAD32_S:
  11842. case WASM_OP_I64_LOAD32_U:
  11843. case WASM_OP_F32_LOAD:
  11844. case WASM_OP_F64_LOAD:
  11845. /* store */
  11846. case WASM_OP_I32_STORE:
  11847. case WASM_OP_I32_STORE8:
  11848. case WASM_OP_I32_STORE16:
  11849. case WASM_OP_I64_STORE:
  11850. case WASM_OP_I64_STORE8:
  11851. case WASM_OP_I64_STORE16:
  11852. case WASM_OP_I64_STORE32:
  11853. case WASM_OP_F32_STORE:
  11854. case WASM_OP_F64_STORE:
  11855. {
  11856. #if WASM_ENABLE_FAST_INTERP != 0
  11857. /* change F32/F64 into I32/I64 */
  11858. if (opcode == WASM_OP_F32_LOAD) {
  11859. skip_label();
  11860. emit_label(WASM_OP_I32_LOAD);
  11861. }
  11862. else if (opcode == WASM_OP_F64_LOAD) {
  11863. skip_label();
  11864. emit_label(WASM_OP_I64_LOAD);
  11865. }
  11866. else if (opcode == WASM_OP_F32_STORE) {
  11867. skip_label();
  11868. emit_label(WASM_OP_I32_STORE);
  11869. }
  11870. else if (opcode == WASM_OP_F64_STORE) {
  11871. skip_label();
  11872. emit_label(WASM_OP_I64_STORE);
  11873. }
  11874. #endif
  11875. CHECK_MEMORY();
  11876. read_leb_uint32(p, p_end, align); /* align */
  11877. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11878. if (!check_memory_access_align(opcode, align, error_buf,
  11879. error_buf_size)) {
  11880. goto fail;
  11881. }
  11882. #if WASM_ENABLE_FAST_INTERP != 0
  11883. emit_uint32(loader_ctx, mem_offset);
  11884. #endif
  11885. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11886. func->has_memory_operations = true;
  11887. #endif
  11888. switch (opcode) {
  11889. /* load */
  11890. case WASM_OP_I32_LOAD:
  11891. case WASM_OP_I32_LOAD8_S:
  11892. case WASM_OP_I32_LOAD8_U:
  11893. case WASM_OP_I32_LOAD16_S:
  11894. case WASM_OP_I32_LOAD16_U:
  11895. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11896. break;
  11897. case WASM_OP_I64_LOAD:
  11898. case WASM_OP_I64_LOAD8_S:
  11899. case WASM_OP_I64_LOAD8_U:
  11900. case WASM_OP_I64_LOAD16_S:
  11901. case WASM_OP_I64_LOAD16_U:
  11902. case WASM_OP_I64_LOAD32_S:
  11903. case WASM_OP_I64_LOAD32_U:
  11904. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11905. break;
  11906. case WASM_OP_F32_LOAD:
  11907. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11908. break;
  11909. case WASM_OP_F64_LOAD:
  11910. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11911. break;
  11912. /* store */
  11913. case WASM_OP_I32_STORE:
  11914. case WASM_OP_I32_STORE8:
  11915. case WASM_OP_I32_STORE16:
  11916. POP_I32();
  11917. POP_MEM_OFFSET();
  11918. break;
  11919. case WASM_OP_I64_STORE:
  11920. case WASM_OP_I64_STORE8:
  11921. case WASM_OP_I64_STORE16:
  11922. case WASM_OP_I64_STORE32:
  11923. POP_I64();
  11924. POP_MEM_OFFSET();
  11925. break;
  11926. case WASM_OP_F32_STORE:
  11927. POP_F32();
  11928. POP_MEM_OFFSET();
  11929. break;
  11930. case WASM_OP_F64_STORE:
  11931. POP_F64();
  11932. POP_MEM_OFFSET();
  11933. break;
  11934. default:
  11935. break;
  11936. }
  11937. break;
  11938. }
  11939. case WASM_OP_MEMORY_SIZE:
  11940. CHECK_MEMORY();
  11941. /* reserved byte 0x00 */
  11942. if (*p++ != 0x00) {
  11943. set_error_buf(error_buf, error_buf_size,
  11944. "zero byte expected");
  11945. goto fail;
  11946. }
  11947. PUSH_PAGE_COUNT();
  11948. module->possible_memory_grow = true;
  11949. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11950. func->has_memory_operations = true;
  11951. #endif
  11952. break;
  11953. case WASM_OP_MEMORY_GROW:
  11954. CHECK_MEMORY();
  11955. /* reserved byte 0x00 */
  11956. if (*p++ != 0x00) {
  11957. set_error_buf(error_buf, error_buf_size,
  11958. "zero byte expected");
  11959. goto fail;
  11960. }
  11961. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11962. module->possible_memory_grow = true;
  11963. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11964. || WASM_ENABLE_WAMR_COMPILER != 0
  11965. func->has_op_memory_grow = true;
  11966. #endif
  11967. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11968. func->has_memory_operations = true;
  11969. #endif
  11970. break;
  11971. case WASM_OP_I32_CONST:
  11972. read_leb_int32(p, p_end, i32_const);
  11973. #if WASM_ENABLE_FAST_INTERP != 0
  11974. skip_label();
  11975. disable_emit = true;
  11976. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11977. if (operand_offset == 0) {
  11978. disable_emit = false;
  11979. emit_label(WASM_OP_I32_CONST);
  11980. emit_uint32(loader_ctx, i32_const);
  11981. }
  11982. #else
  11983. (void)i32_const;
  11984. #endif
  11985. PUSH_I32();
  11986. break;
  11987. case WASM_OP_I64_CONST:
  11988. read_leb_int64(p, p_end, i64_const);
  11989. #if WASM_ENABLE_FAST_INTERP != 0
  11990. skip_label();
  11991. disable_emit = true;
  11992. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11993. if (operand_offset == 0) {
  11994. disable_emit = false;
  11995. emit_label(WASM_OP_I64_CONST);
  11996. emit_uint64(loader_ctx, i64_const);
  11997. }
  11998. #endif
  11999. PUSH_I64();
  12000. break;
  12001. case WASM_OP_F32_CONST:
  12002. CHECK_BUF(p, p_end, sizeof(float32));
  12003. p += sizeof(float32);
  12004. #if WASM_ENABLE_FAST_INTERP != 0
  12005. skip_label();
  12006. disable_emit = true;
  12007. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12008. sizeof(float32));
  12009. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12010. if (operand_offset == 0) {
  12011. disable_emit = false;
  12012. emit_label(WASM_OP_F32_CONST);
  12013. emit_float32(loader_ctx, f32_const);
  12014. }
  12015. #endif
  12016. PUSH_F32();
  12017. break;
  12018. case WASM_OP_F64_CONST:
  12019. CHECK_BUF(p, p_end, sizeof(float64));
  12020. p += sizeof(float64);
  12021. #if WASM_ENABLE_FAST_INTERP != 0
  12022. skip_label();
  12023. disable_emit = true;
  12024. /* Some MCU may require 8-byte align */
  12025. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12026. sizeof(float64));
  12027. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12028. if (operand_offset == 0) {
  12029. disable_emit = false;
  12030. emit_label(WASM_OP_F64_CONST);
  12031. emit_float64(loader_ctx, f64_const);
  12032. }
  12033. #endif
  12034. PUSH_F64();
  12035. break;
  12036. case WASM_OP_I32_EQZ:
  12037. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12038. break;
  12039. case WASM_OP_I32_EQ:
  12040. case WASM_OP_I32_NE:
  12041. case WASM_OP_I32_LT_S:
  12042. case WASM_OP_I32_LT_U:
  12043. case WASM_OP_I32_GT_S:
  12044. case WASM_OP_I32_GT_U:
  12045. case WASM_OP_I32_LE_S:
  12046. case WASM_OP_I32_LE_U:
  12047. case WASM_OP_I32_GE_S:
  12048. case WASM_OP_I32_GE_U:
  12049. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12050. break;
  12051. case WASM_OP_I64_EQZ:
  12052. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12053. break;
  12054. case WASM_OP_I64_EQ:
  12055. case WASM_OP_I64_NE:
  12056. case WASM_OP_I64_LT_S:
  12057. case WASM_OP_I64_LT_U:
  12058. case WASM_OP_I64_GT_S:
  12059. case WASM_OP_I64_GT_U:
  12060. case WASM_OP_I64_LE_S:
  12061. case WASM_OP_I64_LE_U:
  12062. case WASM_OP_I64_GE_S:
  12063. case WASM_OP_I64_GE_U:
  12064. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12065. break;
  12066. case WASM_OP_F32_EQ:
  12067. case WASM_OP_F32_NE:
  12068. case WASM_OP_F32_LT:
  12069. case WASM_OP_F32_GT:
  12070. case WASM_OP_F32_LE:
  12071. case WASM_OP_F32_GE:
  12072. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12073. break;
  12074. case WASM_OP_F64_EQ:
  12075. case WASM_OP_F64_NE:
  12076. case WASM_OP_F64_LT:
  12077. case WASM_OP_F64_GT:
  12078. case WASM_OP_F64_LE:
  12079. case WASM_OP_F64_GE:
  12080. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12081. break;
  12082. case WASM_OP_I32_CLZ:
  12083. case WASM_OP_I32_CTZ:
  12084. case WASM_OP_I32_POPCNT:
  12085. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12086. break;
  12087. case WASM_OP_I32_ADD:
  12088. case WASM_OP_I32_SUB:
  12089. case WASM_OP_I32_MUL:
  12090. case WASM_OP_I32_DIV_S:
  12091. case WASM_OP_I32_DIV_U:
  12092. case WASM_OP_I32_REM_S:
  12093. case WASM_OP_I32_REM_U:
  12094. case WASM_OP_I32_AND:
  12095. case WASM_OP_I32_OR:
  12096. case WASM_OP_I32_XOR:
  12097. case WASM_OP_I32_SHL:
  12098. case WASM_OP_I32_SHR_S:
  12099. case WASM_OP_I32_SHR_U:
  12100. case WASM_OP_I32_ROTL:
  12101. case WASM_OP_I32_ROTR:
  12102. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12103. break;
  12104. case WASM_OP_I64_CLZ:
  12105. case WASM_OP_I64_CTZ:
  12106. case WASM_OP_I64_POPCNT:
  12107. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12108. break;
  12109. case WASM_OP_I64_ADD:
  12110. case WASM_OP_I64_SUB:
  12111. case WASM_OP_I64_MUL:
  12112. case WASM_OP_I64_DIV_S:
  12113. case WASM_OP_I64_DIV_U:
  12114. case WASM_OP_I64_REM_S:
  12115. case WASM_OP_I64_REM_U:
  12116. case WASM_OP_I64_AND:
  12117. case WASM_OP_I64_OR:
  12118. case WASM_OP_I64_XOR:
  12119. case WASM_OP_I64_SHL:
  12120. case WASM_OP_I64_SHR_S:
  12121. case WASM_OP_I64_SHR_U:
  12122. case WASM_OP_I64_ROTL:
  12123. case WASM_OP_I64_ROTR:
  12124. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12125. break;
  12126. case WASM_OP_F32_ABS:
  12127. case WASM_OP_F32_NEG:
  12128. case WASM_OP_F32_CEIL:
  12129. case WASM_OP_F32_FLOOR:
  12130. case WASM_OP_F32_TRUNC:
  12131. case WASM_OP_F32_NEAREST:
  12132. case WASM_OP_F32_SQRT:
  12133. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12134. break;
  12135. case WASM_OP_F32_ADD:
  12136. case WASM_OP_F32_SUB:
  12137. case WASM_OP_F32_MUL:
  12138. case WASM_OP_F32_DIV:
  12139. case WASM_OP_F32_MIN:
  12140. case WASM_OP_F32_MAX:
  12141. case WASM_OP_F32_COPYSIGN:
  12142. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12143. break;
  12144. case WASM_OP_F64_ABS:
  12145. case WASM_OP_F64_NEG:
  12146. case WASM_OP_F64_CEIL:
  12147. case WASM_OP_F64_FLOOR:
  12148. case WASM_OP_F64_TRUNC:
  12149. case WASM_OP_F64_NEAREST:
  12150. case WASM_OP_F64_SQRT:
  12151. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12152. break;
  12153. case WASM_OP_F64_ADD:
  12154. case WASM_OP_F64_SUB:
  12155. case WASM_OP_F64_MUL:
  12156. case WASM_OP_F64_DIV:
  12157. case WASM_OP_F64_MIN:
  12158. case WASM_OP_F64_MAX:
  12159. case WASM_OP_F64_COPYSIGN:
  12160. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12161. break;
  12162. case WASM_OP_I32_WRAP_I64:
  12163. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12164. break;
  12165. case WASM_OP_I32_TRUNC_S_F32:
  12166. case WASM_OP_I32_TRUNC_U_F32:
  12167. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12168. break;
  12169. case WASM_OP_I32_TRUNC_S_F64:
  12170. case WASM_OP_I32_TRUNC_U_F64:
  12171. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12172. break;
  12173. case WASM_OP_I64_EXTEND_S_I32:
  12174. case WASM_OP_I64_EXTEND_U_I32:
  12175. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12176. break;
  12177. case WASM_OP_I64_TRUNC_S_F32:
  12178. case WASM_OP_I64_TRUNC_U_F32:
  12179. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12180. break;
  12181. case WASM_OP_I64_TRUNC_S_F64:
  12182. case WASM_OP_I64_TRUNC_U_F64:
  12183. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12184. break;
  12185. case WASM_OP_F32_CONVERT_S_I32:
  12186. case WASM_OP_F32_CONVERT_U_I32:
  12187. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12188. break;
  12189. case WASM_OP_F32_CONVERT_S_I64:
  12190. case WASM_OP_F32_CONVERT_U_I64:
  12191. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12192. break;
  12193. case WASM_OP_F32_DEMOTE_F64:
  12194. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12195. break;
  12196. case WASM_OP_F64_CONVERT_S_I32:
  12197. case WASM_OP_F64_CONVERT_U_I32:
  12198. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12199. break;
  12200. case WASM_OP_F64_CONVERT_S_I64:
  12201. case WASM_OP_F64_CONVERT_U_I64:
  12202. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12203. break;
  12204. case WASM_OP_F64_PROMOTE_F32:
  12205. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12206. break;
  12207. case WASM_OP_I32_REINTERPRET_F32:
  12208. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12209. break;
  12210. case WASM_OP_I64_REINTERPRET_F64:
  12211. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12212. break;
  12213. case WASM_OP_F32_REINTERPRET_I32:
  12214. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12215. break;
  12216. case WASM_OP_F64_REINTERPRET_I64:
  12217. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12218. break;
  12219. case WASM_OP_I32_EXTEND8_S:
  12220. case WASM_OP_I32_EXTEND16_S:
  12221. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12222. break;
  12223. case WASM_OP_I64_EXTEND8_S:
  12224. case WASM_OP_I64_EXTEND16_S:
  12225. case WASM_OP_I64_EXTEND32_S:
  12226. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12227. break;
  12228. #if WASM_ENABLE_GC != 0
  12229. case WASM_OP_GC_PREFIX:
  12230. {
  12231. uint32 opcode1;
  12232. read_leb_uint32(p, p_end, opcode1);
  12233. #if WASM_ENABLE_FAST_INTERP != 0
  12234. emit_byte(loader_ctx, ((uint8)opcode1));
  12235. #endif
  12236. switch (opcode1) {
  12237. case WASM_OP_STRUCT_NEW:
  12238. case WASM_OP_STRUCT_NEW_DEFAULT:
  12239. {
  12240. read_leb_uint32(p, p_end, type_idx);
  12241. #if WASM_ENABLE_FAST_INTERP != 0
  12242. emit_uint32(loader_ctx, type_idx);
  12243. #endif
  12244. if (!check_type_index(module, module->type_count,
  12245. type_idx, error_buf,
  12246. error_buf_size)) {
  12247. goto fail;
  12248. }
  12249. if (module->types[type_idx]->type_flag
  12250. != WASM_TYPE_STRUCT) {
  12251. set_error_buf(error_buf, error_buf_size,
  12252. "unkown struct type");
  12253. goto fail;
  12254. }
  12255. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12256. int32 j, k;
  12257. uint8 value_type;
  12258. uint32 ref_type_struct_size;
  12259. WASMStructType *struct_type =
  12260. (WASMStructType *)module->types[type_idx];
  12261. k = struct_type->ref_type_map_count - 1;
  12262. for (j = struct_type->field_count - 1; j >= 0;
  12263. j--) {
  12264. value_type = struct_type->fields[j].field_type;
  12265. if (wasm_is_type_reftype(value_type)) {
  12266. if (wasm_is_type_multi_byte_type(
  12267. value_type)) {
  12268. ref_type_struct_size =
  12269. wasm_reftype_struct_size(
  12270. struct_type->ref_type_maps[k]
  12271. .ref_type);
  12272. bh_memcpy_s(
  12273. &wasm_ref_type,
  12274. (uint32)sizeof(WASMRefType),
  12275. struct_type->ref_type_maps[k]
  12276. .ref_type,
  12277. ref_type_struct_size);
  12278. k--;
  12279. }
  12280. POP_REF(value_type);
  12281. }
  12282. else {
  12283. switch (value_type) {
  12284. case VALUE_TYPE_I32:
  12285. case PACKED_TYPE_I8:
  12286. case PACKED_TYPE_I16:
  12287. POP_I32();
  12288. break;
  12289. case VALUE_TYPE_I64:
  12290. POP_I64();
  12291. break;
  12292. case VALUE_TYPE_F32:
  12293. POP_F32();
  12294. break;
  12295. case VALUE_TYPE_F64:
  12296. POP_F64();
  12297. break;
  12298. default:
  12299. set_error_buf(error_buf,
  12300. error_buf_size,
  12301. "unknown type");
  12302. goto fail;
  12303. }
  12304. }
  12305. }
  12306. }
  12307. /* PUSH struct obj, (ref $t) */
  12308. wasm_set_refheaptype_typeidx(
  12309. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12310. PUSH_REF(wasm_ref_type.ref_type);
  12311. break;
  12312. }
  12313. case WASM_OP_STRUCT_GET:
  12314. case WASM_OP_STRUCT_GET_S:
  12315. case WASM_OP_STRUCT_GET_U:
  12316. case WASM_OP_STRUCT_SET:
  12317. {
  12318. WASMStructType *struct_type;
  12319. WASMRefType *ref_type = NULL;
  12320. uint32 field_idx;
  12321. uint8 field_type;
  12322. read_leb_uint32(p, p_end, type_idx);
  12323. #if WASM_ENABLE_FAST_INTERP != 0
  12324. emit_uint32(loader_ctx, type_idx);
  12325. #endif
  12326. if (!check_type_index(module, module->type_count,
  12327. type_idx, error_buf,
  12328. error_buf_size)) {
  12329. goto fail;
  12330. }
  12331. if (module->types[type_idx]->type_flag
  12332. != WASM_TYPE_STRUCT) {
  12333. set_error_buf(error_buf, error_buf_size,
  12334. "unknown struct type");
  12335. goto fail;
  12336. }
  12337. struct_type = (WASMStructType *)module->types[type_idx];
  12338. read_leb_uint32(p, p_end, field_idx);
  12339. #if WASM_ENABLE_FAST_INTERP != 0
  12340. emit_uint32(loader_ctx, field_idx);
  12341. #endif
  12342. if (field_idx >= struct_type->field_count) {
  12343. set_error_buf(error_buf, error_buf_size,
  12344. "unknown struct field");
  12345. goto fail;
  12346. }
  12347. if (opcode1 == WASM_OP_STRUCT_SET
  12348. && !(struct_type->fields[field_idx].field_flags
  12349. & 1)) {
  12350. set_error_buf(error_buf, error_buf_size,
  12351. "field is immutable");
  12352. goto fail;
  12353. }
  12354. field_type = struct_type->fields[field_idx].field_type;
  12355. if (is_packed_type(field_type)) {
  12356. if (opcode1 == WASM_OP_STRUCT_GET) {
  12357. set_error_buf(error_buf, error_buf_size,
  12358. "type mismatch");
  12359. goto fail;
  12360. }
  12361. else {
  12362. field_type = VALUE_TYPE_I32;
  12363. }
  12364. }
  12365. if (wasm_is_type_multi_byte_type(field_type)) {
  12366. ref_type = wasm_reftype_map_find(
  12367. struct_type->ref_type_maps,
  12368. struct_type->ref_type_map_count, field_idx);
  12369. bh_assert(ref_type);
  12370. }
  12371. if (opcode1 == WASM_OP_STRUCT_SET) {
  12372. /* POP field */
  12373. if (wasm_is_type_multi_byte_type(field_type)) {
  12374. bh_memcpy_s(&wasm_ref_type,
  12375. (uint32)sizeof(WASMRefType),
  12376. ref_type,
  12377. wasm_reftype_struct_size(ref_type));
  12378. }
  12379. POP_REF(field_type);
  12380. /* POP struct obj, (ref null $t) */
  12381. wasm_set_refheaptype_typeidx(
  12382. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12383. POP_REF(wasm_ref_type.ref_type);
  12384. }
  12385. else {
  12386. /* POP struct obj, (ref null $t) */
  12387. wasm_set_refheaptype_typeidx(
  12388. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12389. POP_REF(wasm_ref_type.ref_type);
  12390. /* PUSH field */
  12391. if (wasm_is_type_multi_byte_type(field_type)) {
  12392. bh_memcpy_s(&wasm_ref_type,
  12393. (uint32)sizeof(WASMRefType),
  12394. ref_type,
  12395. wasm_reftype_struct_size(ref_type));
  12396. }
  12397. PUSH_REF(field_type);
  12398. }
  12399. break;
  12400. }
  12401. case WASM_OP_ARRAY_NEW:
  12402. case WASM_OP_ARRAY_NEW_DEFAULT:
  12403. case WASM_OP_ARRAY_NEW_FIXED:
  12404. case WASM_OP_ARRAY_NEW_DATA:
  12405. case WASM_OP_ARRAY_NEW_ELEM:
  12406. {
  12407. WASMArrayType *array_type;
  12408. uint8 elem_type;
  12409. uint32 u32 = 0;
  12410. read_leb_uint32(p, p_end, type_idx);
  12411. #if WASM_ENABLE_FAST_INTERP != 0
  12412. emit_uint32(loader_ctx, type_idx);
  12413. #endif
  12414. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12415. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12416. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12417. read_leb_uint32(p, p_end, u32);
  12418. #if WASM_ENABLE_FAST_INTERP != 0
  12419. emit_uint32(loader_ctx, u32);
  12420. #endif
  12421. }
  12422. if (!check_array_type(module, type_idx, error_buf,
  12423. error_buf_size)) {
  12424. goto fail;
  12425. }
  12426. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12427. /* length */
  12428. POP_I32();
  12429. }
  12430. array_type = (WASMArrayType *)module->types[type_idx];
  12431. elem_type = array_type->elem_type;
  12432. if (opcode1 == WASM_OP_ARRAY_NEW
  12433. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12434. if (wasm_is_type_multi_byte_type(elem_type)) {
  12435. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12436. array_type->elem_ref_type,
  12437. wasm_reftype_struct_size(
  12438. array_type->elem_ref_type));
  12439. }
  12440. if (is_packed_type(elem_type)) {
  12441. elem_type = VALUE_TYPE_I32;
  12442. }
  12443. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12444. uint32 N = u32;
  12445. for (i = 0; i < N; i++) {
  12446. if (wasm_is_type_multi_byte_type(
  12447. elem_type)) {
  12448. bh_memcpy_s(
  12449. &wasm_ref_type, sizeof(WASMRefType),
  12450. array_type->elem_ref_type,
  12451. wasm_reftype_struct_size(
  12452. array_type->elem_ref_type));
  12453. }
  12454. POP_REF(elem_type);
  12455. }
  12456. }
  12457. else
  12458. POP_REF(elem_type);
  12459. }
  12460. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12461. /* offset of data segment */
  12462. POP_I32();
  12463. if (u32 >= module->data_seg_count) {
  12464. set_error_buf(error_buf, error_buf_size,
  12465. "unknown data segment");
  12466. goto fail;
  12467. }
  12468. if (wasm_is_type_reftype(elem_type)) {
  12469. set_error_buf(error_buf, error_buf_size,
  12470. "array elem type mismatch");
  12471. goto fail;
  12472. }
  12473. }
  12474. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12475. WASMTableSeg *table_seg =
  12476. module->table_segments + u32;
  12477. /* offset of element segment */
  12478. POP_I32();
  12479. if (u32 >= module->table_seg_count) {
  12480. set_error_buf(error_buf, error_buf_size,
  12481. "unknown element segment");
  12482. goto fail;
  12483. }
  12484. if (!wasm_reftype_is_subtype_of(
  12485. table_seg->elem_type,
  12486. table_seg->elem_ref_type, elem_type,
  12487. array_type->elem_ref_type, module->types,
  12488. module->type_count)) {
  12489. set_error_buf(error_buf, error_buf_size,
  12490. "array elem type mismatch");
  12491. goto fail;
  12492. }
  12493. }
  12494. /* PUSH array obj, (ref $t) */
  12495. wasm_set_refheaptype_typeidx(
  12496. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12497. PUSH_REF(wasm_ref_type.ref_type);
  12498. break;
  12499. }
  12500. case WASM_OP_ARRAY_GET:
  12501. case WASM_OP_ARRAY_GET_S:
  12502. case WASM_OP_ARRAY_GET_U:
  12503. case WASM_OP_ARRAY_SET:
  12504. {
  12505. uint8 elem_type;
  12506. WASMArrayType *array_type;
  12507. WASMRefType *ref_type = NULL;
  12508. read_leb_uint32(p, p_end, type_idx);
  12509. #if WASM_ENABLE_FAST_INTERP != 0
  12510. emit_uint32(loader_ctx, type_idx);
  12511. #endif
  12512. if (!check_array_type(module, type_idx, error_buf,
  12513. error_buf_size)) {
  12514. goto fail;
  12515. }
  12516. array_type = (WASMArrayType *)module->types[type_idx];
  12517. if (opcode1 == WASM_OP_ARRAY_SET
  12518. && !(array_type->elem_flags & 1)) {
  12519. set_error_buf(error_buf, error_buf_size,
  12520. "array is immutable");
  12521. goto fail;
  12522. }
  12523. elem_type = array_type->elem_type;
  12524. if (is_packed_type(elem_type)) {
  12525. if (opcode1 != WASM_OP_ARRAY_GET_S
  12526. && opcode1 != WASM_OP_ARRAY_GET_U
  12527. && opcode1 != WASM_OP_ARRAY_SET) {
  12528. set_error_buf(error_buf, error_buf_size,
  12529. "type mismatch");
  12530. goto fail;
  12531. }
  12532. else {
  12533. elem_type = VALUE_TYPE_I32;
  12534. }
  12535. }
  12536. ref_type = array_type->elem_ref_type;
  12537. if (opcode1 == WASM_OP_ARRAY_SET) {
  12538. /* POP elem to set */
  12539. if (wasm_is_type_multi_byte_type(elem_type)) {
  12540. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12541. ref_type,
  12542. wasm_reftype_struct_size(ref_type));
  12543. }
  12544. POP_REF(elem_type);
  12545. }
  12546. /* elem idx */
  12547. POP_I32();
  12548. /* POP array obj, (ref null $t) */
  12549. wasm_set_refheaptype_typeidx(
  12550. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12551. POP_REF(wasm_ref_type.ref_type);
  12552. if (opcode1 != WASM_OP_ARRAY_SET) {
  12553. /* PUSH elem */
  12554. if (wasm_is_type_multi_byte_type(elem_type)) {
  12555. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12556. ref_type,
  12557. wasm_reftype_struct_size(ref_type));
  12558. }
  12559. PUSH_REF(elem_type);
  12560. }
  12561. break;
  12562. }
  12563. case WASM_OP_ARRAY_LEN:
  12564. {
  12565. POP_REF(REF_TYPE_ARRAYREF);
  12566. /* length */
  12567. PUSH_I32();
  12568. break;
  12569. }
  12570. case WASM_OP_ARRAY_FILL:
  12571. {
  12572. WASMArrayType *array_type;
  12573. uint8 elem_type;
  12574. /* typeidx */
  12575. read_leb_uint32(p, p_end, type_idx);
  12576. #if WASM_ENABLE_FAST_INTERP != 0
  12577. emit_uint32(loader_ctx, type_idx);
  12578. #endif
  12579. if (!check_array_type(module, type_idx, error_buf,
  12580. error_buf_size)) {
  12581. goto fail;
  12582. }
  12583. array_type = (WASMArrayType *)module->types[type_idx];
  12584. if (!(array_type->elem_flags & 1)) {
  12585. set_error_buf(error_buf, error_buf_size,
  12586. "array is immutable");
  12587. goto fail;
  12588. }
  12589. elem_type = array_type->elem_type;
  12590. if (is_packed_type(elem_type)) {
  12591. elem_type = VALUE_TYPE_I32;
  12592. }
  12593. POP_I32(); /* length */
  12594. #if WASM_ENABLE_FAST_INTERP != 0
  12595. POP_OFFSET_TYPE(elem_type);
  12596. #endif
  12597. POP_TYPE(elem_type);
  12598. POP_I32(); /* start */
  12599. /* POP array obj, (ref null $t) */
  12600. wasm_set_refheaptype_typeidx(
  12601. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12602. POP_REF(wasm_ref_type.ref_type);
  12603. break;
  12604. }
  12605. case WASM_OP_ARRAY_COPY:
  12606. {
  12607. uint32 src_type_idx;
  12608. uint8 src_elem_type, dst_elem_type;
  12609. WASMRefType src_ref_type = { 0 },
  12610. *src_elem_ref_type = NULL;
  12611. WASMRefType dst_ref_type = { 0 },
  12612. *dst_elem_ref_type = NULL;
  12613. WASMArrayType *array_type;
  12614. /* typeidx1 */
  12615. read_leb_uint32(p, p_end, type_idx);
  12616. #if WASM_ENABLE_FAST_INTERP != 0
  12617. emit_uint32(loader_ctx, type_idx);
  12618. #endif
  12619. /* typeidx2 */
  12620. read_leb_uint32(p, p_end, src_type_idx);
  12621. #if WASM_ENABLE_FAST_INTERP != 0
  12622. emit_uint32(loader_ctx, src_type_idx);
  12623. #endif
  12624. if (!check_array_type(module, type_idx, error_buf,
  12625. error_buf_size)) {
  12626. goto fail;
  12627. }
  12628. if (!check_array_type(module, src_type_idx, error_buf,
  12629. error_buf_size)) {
  12630. goto fail;
  12631. }
  12632. POP_I32();
  12633. POP_I32();
  12634. /* POP array obj, (ref null $t) */
  12635. wasm_set_refheaptype_typeidx(
  12636. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12637. POP_REF(wasm_ref_type.ref_type);
  12638. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12639. &wasm_ref_type,
  12640. wasm_reftype_struct_size(&wasm_ref_type));
  12641. POP_I32();
  12642. /* POP array obj, (ref null $t) */
  12643. wasm_set_refheaptype_typeidx(
  12644. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12645. POP_REF(wasm_ref_type.ref_type);
  12646. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12647. &wasm_ref_type,
  12648. wasm_reftype_struct_size(&wasm_ref_type));
  12649. array_type = (WASMArrayType *)module->types[type_idx];
  12650. if (!(array_type->elem_flags & 1)) {
  12651. set_error_buf(error_buf, error_buf_size,
  12652. "destination array is immutable");
  12653. goto fail;
  12654. }
  12655. dst_elem_type = array_type->elem_type;
  12656. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12657. dst_elem_ref_type = array_type->elem_ref_type;
  12658. }
  12659. array_type =
  12660. (WASMArrayType *)module->types[src_type_idx];
  12661. src_elem_type = array_type->elem_type;
  12662. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12663. src_elem_ref_type = array_type->elem_ref_type;
  12664. }
  12665. if (!wasm_reftype_is_subtype_of(
  12666. src_elem_type, src_elem_ref_type, dst_elem_type,
  12667. dst_elem_ref_type, module->types,
  12668. module->type_count)) {
  12669. set_error_buf(error_buf, error_buf_size,
  12670. "array types do not match");
  12671. goto fail;
  12672. }
  12673. break;
  12674. }
  12675. case WASM_OP_REF_I31:
  12676. {
  12677. POP_I32();
  12678. wasm_set_refheaptype_common(
  12679. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12680. PUSH_REF(wasm_ref_type.ref_type);
  12681. break;
  12682. }
  12683. case WASM_OP_I31_GET_S:
  12684. case WASM_OP_I31_GET_U:
  12685. {
  12686. POP_REF(REF_TYPE_I31REF);
  12687. PUSH_I32();
  12688. break;
  12689. }
  12690. case WASM_OP_REF_TEST:
  12691. case WASM_OP_REF_CAST:
  12692. case WASM_OP_REF_TEST_NULLABLE:
  12693. case WASM_OP_REF_CAST_NULLABLE:
  12694. {
  12695. uint8 type;
  12696. read_leb_int32(p, p_end, heap_type);
  12697. #if WASM_ENABLE_FAST_INTERP != 0
  12698. emit_uint32(loader_ctx, (uint32)heap_type);
  12699. #endif
  12700. if (heap_type >= 0) {
  12701. if (!check_type_index(module, module->type_count,
  12702. heap_type, error_buf,
  12703. error_buf_size)) {
  12704. goto fail;
  12705. }
  12706. }
  12707. else {
  12708. if (!wasm_is_valid_heap_type(heap_type)) {
  12709. set_error_buf(error_buf, error_buf_size,
  12710. "unknown type");
  12711. goto fail;
  12712. }
  12713. }
  12714. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12715. &wasm_ref_type, error_buf,
  12716. error_buf_size)) {
  12717. goto fail;
  12718. }
  12719. if (opcode1 == WASM_OP_REF_TEST
  12720. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12721. PUSH_I32();
  12722. else {
  12723. bool nullable =
  12724. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12725. : false;
  12726. if (heap_type >= 0 || !nullable) {
  12727. wasm_set_refheaptype_typeidx(
  12728. &wasm_ref_type.ref_ht_typeidx, nullable,
  12729. heap_type);
  12730. PUSH_REF(wasm_ref_type.ref_type);
  12731. }
  12732. else {
  12733. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12734. }
  12735. }
  12736. break;
  12737. }
  12738. case WASM_OP_BR_ON_CAST:
  12739. case WASM_OP_BR_ON_CAST_FAIL:
  12740. {
  12741. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12742. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12743. uint8 type_tmp, castflags;
  12744. uint32 depth;
  12745. int32 heap_type_dst;
  12746. bool src_nullable, dst_nullable;
  12747. CHECK_BUF(p, p_end, 1);
  12748. castflags = read_uint8(p);
  12749. #if WASM_ENABLE_FAST_INTERP != 0
  12750. /* Emit heap_type firstly */
  12751. emit_byte(loader_ctx, castflags);
  12752. #endif
  12753. p_org = p;
  12754. read_leb_uint32(p, p_end, depth);
  12755. read_leb_int32(p, p_end, heap_type);
  12756. #if WASM_ENABLE_FAST_INTERP != 0
  12757. /* Emit heap_type firstly */
  12758. emit_uint32(loader_ctx, (uint32)heap_type);
  12759. #endif
  12760. read_leb_int32(p, p_end, heap_type_dst);
  12761. #if WASM_ENABLE_FAST_INTERP != 0
  12762. /* Emit heap_type firstly */
  12763. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12764. #endif
  12765. (void)depth;
  12766. /*
  12767. * castflags should be 0~3:
  12768. * 0: (non-null, non-null)
  12769. * 1: (null, non-null)
  12770. * 2: (non-null, null)
  12771. * 3: (null, null)
  12772. */
  12773. if (castflags > 3) {
  12774. set_error_buf(error_buf, error_buf_size,
  12775. "invalid castflags");
  12776. break;
  12777. }
  12778. src_nullable =
  12779. (castflags == 1) || (castflags == 3) ? true : false;
  12780. dst_nullable =
  12781. (castflags == 2) || (castflags == 3) ? true : false;
  12782. /* Pop and backup the stack top's ref type */
  12783. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12784. &ref_type_tmp, error_buf,
  12785. error_buf_size)) {
  12786. goto fail;
  12787. }
  12788. /* The reference type rt1 must be valid */
  12789. if (!init_ref_type(module, &ref_type1, src_nullable,
  12790. heap_type, error_buf,
  12791. error_buf_size)) {
  12792. goto fail;
  12793. }
  12794. /* The reference type rt2 must be valid. */
  12795. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12796. heap_type_dst, error_buf,
  12797. error_buf_size)) {
  12798. goto fail;
  12799. }
  12800. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12801. &ref_type2);
  12802. /* The reference type rt2 must match rt1. */
  12803. if (!wasm_reftype_is_subtype_of(
  12804. ref_type2.ref_type, &ref_type2,
  12805. ref_type1.ref_type, &ref_type1, module->types,
  12806. module->type_count)) {
  12807. set_error_buf(error_buf, error_buf_size,
  12808. "type mismatch");
  12809. goto fail;
  12810. }
  12811. p = p_org;
  12812. /* Push ref type casted for branch block check */
  12813. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12814. /* The reference type rt2 must match rt′. */
  12815. type_tmp = ref_type2.ref_type;
  12816. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12817. bh_memcpy_s(
  12818. &wasm_ref_type,
  12819. wasm_reftype_struct_size(&ref_type2),
  12820. &ref_type2,
  12821. wasm_reftype_struct_size(&ref_type2));
  12822. }
  12823. }
  12824. else {
  12825. /* The reference type rt′1 must match rt′. */
  12826. type_tmp = ref_type_diff.ref_type;
  12827. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12828. bh_memcpy_s(
  12829. &wasm_ref_type,
  12830. wasm_reftype_struct_size(&ref_type_diff),
  12831. &ref_type_diff,
  12832. wasm_reftype_struct_size(&ref_type_diff));
  12833. }
  12834. }
  12835. PUSH_REF(type_tmp);
  12836. if (!(frame_csp_tmp = check_branch_block(
  12837. loader_ctx, &p, p_end, opcode, error_buf,
  12838. error_buf_size))) {
  12839. goto fail;
  12840. }
  12841. /* Ignore heap_types */
  12842. skip_leb_uint32(p, p_end);
  12843. skip_leb_uint32(p, p_end);
  12844. /* Restore the original stack top's ref type */
  12845. POP_REF(type_tmp);
  12846. #if WASM_ENABLE_FAST_INTERP != 0
  12847. /* Erase the opnd offset emitted by POP_REF() */
  12848. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12849. #endif
  12850. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12851. type_tmp = ref_type_diff.ref_type;
  12852. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12853. bh_memcpy_s(
  12854. &wasm_ref_type,
  12855. wasm_reftype_struct_size(&ref_type_diff),
  12856. &ref_type_diff,
  12857. wasm_reftype_struct_size(&ref_type_diff));
  12858. }
  12859. }
  12860. else {
  12861. type_tmp = ref_type2.ref_type;
  12862. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12863. bh_memcpy_s(
  12864. &wasm_ref_type,
  12865. wasm_reftype_struct_size(&ref_type2),
  12866. &ref_type2,
  12867. wasm_reftype_struct_size(&ref_type2));
  12868. }
  12869. }
  12870. PUSH_REF(type_tmp);
  12871. #if WASM_ENABLE_FAST_INTERP != 0
  12872. /* Erase the opnd offset emitted by PUSH_REF() */
  12873. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12874. #endif
  12875. break;
  12876. }
  12877. case WASM_OP_ANY_CONVERT_EXTERN:
  12878. {
  12879. uint8 type;
  12880. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12881. &wasm_ref_type, error_buf,
  12882. error_buf_size)) {
  12883. goto fail;
  12884. }
  12885. if (!(type == REF_TYPE_EXTERNREF
  12886. || (type == REF_TYPE_HT_NON_NULLABLE
  12887. && wasm_ref_type.ref_ht_common.heap_type
  12888. == HEAP_TYPE_EXTERN)
  12889. || type == VALUE_TYPE_ANY)) {
  12890. set_error_buf(error_buf, error_buf_size,
  12891. "type mismatch");
  12892. goto fail;
  12893. }
  12894. if (type == REF_TYPE_EXTERNREF)
  12895. type = REF_TYPE_ANYREF;
  12896. else {
  12897. wasm_ref_type.ref_ht_common.heap_type =
  12898. HEAP_TYPE_ANY;
  12899. }
  12900. PUSH_REF(type);
  12901. break;
  12902. }
  12903. case WASM_OP_EXTERN_CONVERT_ANY:
  12904. {
  12905. uint8 type;
  12906. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12907. &wasm_ref_type, error_buf,
  12908. error_buf_size)) {
  12909. goto fail;
  12910. }
  12911. if (type == REF_TYPE_EXTERNREF
  12912. || ((type == REF_TYPE_HT_NULLABLE
  12913. || type == REF_TYPE_HT_NON_NULLABLE)
  12914. && wasm_ref_type.ref_ht_common.heap_type
  12915. == HEAP_TYPE_EXTERN)) {
  12916. set_error_buf(error_buf, error_buf_size,
  12917. "type mismatch");
  12918. goto fail;
  12919. }
  12920. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12921. /* push (ref null extern) */
  12922. type = REF_TYPE_EXTERNREF;
  12923. }
  12924. else {
  12925. /* push (ref extern) */
  12926. type = REF_TYPE_HT_NON_NULLABLE;
  12927. wasm_set_refheaptype_common(
  12928. &wasm_ref_type.ref_ht_common, false,
  12929. HEAP_TYPE_EXTERN);
  12930. }
  12931. PUSH_REF(type);
  12932. break;
  12933. }
  12934. #if WASM_ENABLE_STRINGREF != 0
  12935. case WASM_OP_STRING_NEW_UTF8:
  12936. case WASM_OP_STRING_NEW_WTF16:
  12937. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12938. case WASM_OP_STRING_NEW_WTF8:
  12939. {
  12940. uint32 memidx;
  12941. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12942. func->has_memory_operations = true;
  12943. #endif
  12944. read_leb_uint32(p, p_end, memidx);
  12945. #if WASM_ENABLE_FAST_INTERP != 0
  12946. emit_uint32(loader_ctx, (uint32)memidx);
  12947. #endif
  12948. POP_I32();
  12949. POP_I32();
  12950. PUSH_REF(REF_TYPE_STRINGREF);
  12951. (void)memidx;
  12952. break;
  12953. }
  12954. case WASM_OP_STRING_CONST:
  12955. {
  12956. uint32 contents;
  12957. read_leb_uint32(p, p_end, contents);
  12958. #if WASM_ENABLE_FAST_INTERP != 0
  12959. emit_uint32(loader_ctx, (uint32)contents);
  12960. #endif
  12961. PUSH_REF(REF_TYPE_STRINGREF);
  12962. (void)contents;
  12963. break;
  12964. }
  12965. case WASM_OP_STRING_MEASURE_UTF8:
  12966. case WASM_OP_STRING_MEASURE_WTF8:
  12967. case WASM_OP_STRING_MEASURE_WTF16:
  12968. {
  12969. POP_STRINGREF();
  12970. PUSH_I32();
  12971. break;
  12972. }
  12973. case WASM_OP_STRING_ENCODE_UTF8:
  12974. case WASM_OP_STRING_ENCODE_WTF16:
  12975. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12976. case WASM_OP_STRING_ENCODE_WTF8:
  12977. {
  12978. uint32 memidx;
  12979. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12980. func->has_memory_operations = true;
  12981. #endif
  12982. read_leb_uint32(p, p_end, memidx);
  12983. #if WASM_ENABLE_FAST_INTERP != 0
  12984. emit_uint32(loader_ctx, (uint32)memidx);
  12985. #endif
  12986. POP_I32();
  12987. POP_STRINGREF();
  12988. PUSH_I32();
  12989. (void)memidx;
  12990. break;
  12991. }
  12992. case WASM_OP_STRING_CONCAT:
  12993. {
  12994. POP_STRINGREF();
  12995. POP_STRINGREF();
  12996. PUSH_REF(REF_TYPE_STRINGREF);
  12997. break;
  12998. }
  12999. case WASM_OP_STRING_EQ:
  13000. {
  13001. POP_STRINGREF();
  13002. POP_STRINGREF();
  13003. PUSH_I32();
  13004. break;
  13005. }
  13006. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13007. {
  13008. POP_STRINGREF();
  13009. PUSH_I32();
  13010. break;
  13011. }
  13012. case WASM_OP_STRING_AS_WTF8:
  13013. {
  13014. POP_STRINGREF();
  13015. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13016. break;
  13017. }
  13018. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13019. {
  13020. POP_I32();
  13021. POP_I32();
  13022. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13023. PUSH_I32();
  13024. break;
  13025. }
  13026. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13027. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13028. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13029. {
  13030. uint32 memidx;
  13031. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13032. func->has_memory_operations = true;
  13033. #endif
  13034. read_leb_uint32(p, p_end, memidx);
  13035. #if WASM_ENABLE_FAST_INTERP != 0
  13036. emit_uint32(loader_ctx, (uint32)memidx);
  13037. #endif
  13038. POP_I32();
  13039. POP_I32();
  13040. POP_I32();
  13041. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13042. PUSH_I32();
  13043. PUSH_I32();
  13044. (void)memidx;
  13045. break;
  13046. }
  13047. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13048. {
  13049. POP_I32();
  13050. POP_I32();
  13051. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13052. PUSH_REF(REF_TYPE_STRINGREF);
  13053. break;
  13054. }
  13055. case WASM_OP_STRING_AS_WTF16:
  13056. {
  13057. POP_STRINGREF();
  13058. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13059. break;
  13060. }
  13061. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13062. {
  13063. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13064. PUSH_I32();
  13065. break;
  13066. }
  13067. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13068. {
  13069. POP_I32();
  13070. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13071. PUSH_I32();
  13072. break;
  13073. }
  13074. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13075. {
  13076. uint32 memidx;
  13077. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13078. func->has_memory_operations = true;
  13079. #endif
  13080. read_leb_uint32(p, p_end, memidx);
  13081. #if WASM_ENABLE_FAST_INTERP != 0
  13082. emit_uint32(loader_ctx, (uint32)memidx);
  13083. #endif
  13084. POP_I32();
  13085. POP_I32();
  13086. POP_I32();
  13087. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13088. PUSH_I32();
  13089. (void)memidx;
  13090. break;
  13091. }
  13092. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13093. {
  13094. POP_I32();
  13095. POP_I32();
  13096. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13097. PUSH_REF(REF_TYPE_STRINGREF);
  13098. break;
  13099. }
  13100. case WASM_OP_STRING_AS_ITER:
  13101. {
  13102. POP_STRINGREF();
  13103. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13104. break;
  13105. }
  13106. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13107. {
  13108. POP_REF(REF_TYPE_STRINGVIEWITER);
  13109. PUSH_I32();
  13110. break;
  13111. }
  13112. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13113. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13114. {
  13115. POP_I32();
  13116. POP_REF(REF_TYPE_STRINGVIEWITER);
  13117. PUSH_I32();
  13118. break;
  13119. }
  13120. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13121. {
  13122. POP_I32();
  13123. POP_REF(REF_TYPE_STRINGVIEWITER);
  13124. PUSH_REF(REF_TYPE_STRINGREF);
  13125. break;
  13126. }
  13127. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13128. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13129. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13130. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13131. {
  13132. POP_I32();
  13133. POP_I32();
  13134. POP_REF(REF_TYPE_ARRAYREF);
  13135. PUSH_REF(REF_TYPE_STRINGREF);
  13136. break;
  13137. }
  13138. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13139. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13140. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13141. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13142. {
  13143. POP_I32();
  13144. POP_REF(REF_TYPE_ARRAYREF);
  13145. POP_STRINGREF();
  13146. PUSH_I32();
  13147. break;
  13148. }
  13149. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13150. default:
  13151. set_error_buf_v(error_buf, error_buf_size,
  13152. "%s %02x %02x", "unsupported opcode",
  13153. 0xfb, opcode1);
  13154. goto fail;
  13155. }
  13156. break;
  13157. }
  13158. #endif /* end of WASM_ENABLE_GC != 0 */
  13159. case WASM_OP_MISC_PREFIX:
  13160. {
  13161. uint32 opcode1;
  13162. read_leb_uint32(p, p_end, opcode1);
  13163. #if WASM_ENABLE_FAST_INTERP != 0
  13164. emit_byte(loader_ctx, ((uint8)opcode1));
  13165. #endif
  13166. switch (opcode1) {
  13167. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13168. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13169. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13170. break;
  13171. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13172. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13173. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13174. break;
  13175. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13176. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13177. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13178. break;
  13179. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13180. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13181. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13182. break;
  13183. #if WASM_ENABLE_BULK_MEMORY != 0
  13184. case WASM_OP_MEMORY_INIT:
  13185. {
  13186. read_leb_uint32(p, p_end, data_seg_idx);
  13187. #if WASM_ENABLE_FAST_INTERP != 0
  13188. emit_uint32(loader_ctx, data_seg_idx);
  13189. #endif
  13190. if (module->import_memory_count == 0
  13191. && module->memory_count == 0)
  13192. goto fail_unknown_memory;
  13193. if (*p++ != 0x00)
  13194. goto fail_zero_byte_expected;
  13195. if (data_seg_idx >= module->data_seg_count) {
  13196. set_error_buf_v(error_buf, error_buf_size,
  13197. "unknown data segment %d",
  13198. data_seg_idx);
  13199. goto fail;
  13200. }
  13201. if (module->data_seg_count1 == 0)
  13202. goto fail_data_cnt_sec_require;
  13203. POP_I32();
  13204. POP_I32();
  13205. POP_MEM_OFFSET();
  13206. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13207. func->has_memory_operations = true;
  13208. #endif
  13209. #if WASM_ENABLE_WAMR_COMPILER != 0
  13210. module->is_bulk_memory_used = true;
  13211. #endif
  13212. break;
  13213. }
  13214. case WASM_OP_DATA_DROP:
  13215. {
  13216. read_leb_uint32(p, p_end, data_seg_idx);
  13217. #if WASM_ENABLE_FAST_INTERP != 0
  13218. emit_uint32(loader_ctx, data_seg_idx);
  13219. #endif
  13220. if (data_seg_idx >= module->data_seg_count) {
  13221. set_error_buf(error_buf, error_buf_size,
  13222. "unknown data segment");
  13223. goto fail;
  13224. }
  13225. if (module->data_seg_count1 == 0)
  13226. goto fail_data_cnt_sec_require;
  13227. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13228. func->has_memory_operations = true;
  13229. #endif
  13230. #if WASM_ENABLE_WAMR_COMPILER != 0
  13231. module->is_bulk_memory_used = true;
  13232. #endif
  13233. break;
  13234. }
  13235. case WASM_OP_MEMORY_COPY:
  13236. {
  13237. CHECK_BUF(p, p_end, sizeof(int16));
  13238. /* both src and dst memory index should be 0 */
  13239. if (*(int16 *)p != 0x0000)
  13240. goto fail_zero_byte_expected;
  13241. p += 2;
  13242. if (module->import_memory_count == 0
  13243. && module->memory_count == 0)
  13244. goto fail_unknown_memory;
  13245. POP_MEM_OFFSET();
  13246. POP_MEM_OFFSET();
  13247. POP_MEM_OFFSET();
  13248. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13249. func->has_memory_operations = true;
  13250. #endif
  13251. #if WASM_ENABLE_WAMR_COMPILER != 0
  13252. module->is_bulk_memory_used = true;
  13253. #endif
  13254. break;
  13255. }
  13256. case WASM_OP_MEMORY_FILL:
  13257. {
  13258. if (*p++ != 0x00) {
  13259. goto fail_zero_byte_expected;
  13260. }
  13261. if (module->import_memory_count == 0
  13262. && module->memory_count == 0) {
  13263. goto fail_unknown_memory;
  13264. }
  13265. POP_MEM_OFFSET();
  13266. POP_I32();
  13267. POP_MEM_OFFSET();
  13268. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13269. func->has_memory_operations = true;
  13270. #endif
  13271. #if WASM_ENABLE_WAMR_COMPILER != 0
  13272. module->is_bulk_memory_used = true;
  13273. #endif
  13274. break;
  13275. }
  13276. fail_zero_byte_expected:
  13277. set_error_buf(error_buf, error_buf_size,
  13278. "zero byte expected");
  13279. goto fail;
  13280. fail_unknown_memory:
  13281. set_error_buf(error_buf, error_buf_size,
  13282. "unknown memory 0");
  13283. goto fail;
  13284. fail_data_cnt_sec_require:
  13285. set_error_buf(error_buf, error_buf_size,
  13286. "data count section required");
  13287. goto fail;
  13288. #endif /* WASM_ENABLE_BULK_MEMORY */
  13289. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13290. case WASM_OP_TABLE_INIT:
  13291. {
  13292. uint8 seg_type = 0, tbl_type = 0;
  13293. #if WASM_ENABLE_GC != 0
  13294. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13295. #endif
  13296. read_leb_uint32(p, p_end, table_seg_idx);
  13297. read_leb_uint32(p, p_end, table_idx);
  13298. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13299. #if WASM_ENABLE_GC != 0
  13300. (void **)&tbl_ref_type,
  13301. #else
  13302. NULL,
  13303. #endif
  13304. error_buf, error_buf_size))
  13305. goto fail;
  13306. if (!get_table_seg_elem_type(module, table_seg_idx,
  13307. &seg_type,
  13308. #if WASM_ENABLE_GC != 0
  13309. (void **)&seg_ref_type,
  13310. #else
  13311. NULL,
  13312. #endif
  13313. error_buf, error_buf_size))
  13314. goto fail;
  13315. #if WASM_ENABLE_GC == 0
  13316. if (seg_type != tbl_type) {
  13317. set_error_buf(error_buf, error_buf_size,
  13318. "type mismatch");
  13319. goto fail;
  13320. }
  13321. #else
  13322. if (!wasm_reftype_is_subtype_of(
  13323. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13324. module->types, module->type_count)) {
  13325. set_error_buf(error_buf, error_buf_size,
  13326. "type mismatch");
  13327. goto fail;
  13328. }
  13329. #endif
  13330. #if WASM_ENABLE_FAST_INTERP != 0
  13331. emit_uint32(loader_ctx, table_seg_idx);
  13332. emit_uint32(loader_ctx, table_idx);
  13333. #endif
  13334. POP_I32();
  13335. POP_I32();
  13336. POP_I32();
  13337. #if WASM_ENABLE_WAMR_COMPILER != 0
  13338. module->is_ref_types_used = true;
  13339. #endif
  13340. break;
  13341. }
  13342. case WASM_OP_ELEM_DROP:
  13343. {
  13344. read_leb_uint32(p, p_end, table_seg_idx);
  13345. if (!get_table_seg_elem_type(module, table_seg_idx,
  13346. NULL, NULL, error_buf,
  13347. error_buf_size))
  13348. goto fail;
  13349. #if WASM_ENABLE_FAST_INTERP != 0
  13350. emit_uint32(loader_ctx, table_seg_idx);
  13351. #endif
  13352. #if WASM_ENABLE_WAMR_COMPILER != 0
  13353. module->is_ref_types_used = true;
  13354. #endif
  13355. break;
  13356. }
  13357. case WASM_OP_TABLE_COPY:
  13358. {
  13359. uint8 src_type, dst_type;
  13360. #if WASM_ENABLE_GC != 0
  13361. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13362. #endif
  13363. uint32 src_tbl_idx, dst_tbl_idx;
  13364. read_leb_uint32(p, p_end, dst_tbl_idx);
  13365. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13366. #if WASM_ENABLE_GC != 0
  13367. (void **)&dst_ref_type,
  13368. #else
  13369. NULL,
  13370. #endif
  13371. error_buf, error_buf_size))
  13372. goto fail;
  13373. read_leb_uint32(p, p_end, src_tbl_idx);
  13374. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13375. #if WASM_ENABLE_GC != 0
  13376. (void **)&src_ref_type,
  13377. #else
  13378. NULL,
  13379. #endif
  13380. error_buf, error_buf_size))
  13381. goto fail;
  13382. #if WASM_ENABLE_GC == 0
  13383. if (src_type != dst_type) {
  13384. set_error_buf(error_buf, error_buf_size,
  13385. "type mismatch");
  13386. goto fail;
  13387. }
  13388. #else
  13389. if (!wasm_reftype_is_subtype_of(
  13390. src_type, src_ref_type, dst_type, dst_ref_type,
  13391. module->types, module->type_count)) {
  13392. set_error_buf(error_buf, error_buf_size,
  13393. "type mismatch");
  13394. goto fail;
  13395. }
  13396. #endif
  13397. #if WASM_ENABLE_FAST_INTERP != 0
  13398. emit_uint32(loader_ctx, dst_tbl_idx);
  13399. emit_uint32(loader_ctx, src_tbl_idx);
  13400. #endif
  13401. POP_I32();
  13402. POP_I32();
  13403. POP_I32();
  13404. #if WASM_ENABLE_WAMR_COMPILER != 0
  13405. module->is_ref_types_used = true;
  13406. #endif
  13407. break;
  13408. }
  13409. case WASM_OP_TABLE_SIZE:
  13410. {
  13411. read_leb_uint32(p, p_end, table_idx);
  13412. /* TODO: shall we create a new function to check
  13413. table idx instead of using below function? */
  13414. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13415. error_buf, error_buf_size))
  13416. goto fail;
  13417. #if WASM_ENABLE_FAST_INTERP != 0
  13418. emit_uint32(loader_ctx, table_idx);
  13419. #endif
  13420. PUSH_I32();
  13421. #if WASM_ENABLE_WAMR_COMPILER != 0
  13422. module->is_ref_types_used = true;
  13423. #endif
  13424. break;
  13425. }
  13426. case WASM_OP_TABLE_GROW:
  13427. case WASM_OP_TABLE_FILL:
  13428. {
  13429. uint8 decl_type;
  13430. #if WASM_ENABLE_GC != 0
  13431. WASMRefType *ref_type = NULL;
  13432. #endif
  13433. read_leb_uint32(p, p_end, table_idx);
  13434. if (!get_table_elem_type(module, table_idx, &decl_type,
  13435. #if WASM_ENABLE_GC != 0
  13436. (void **)&ref_type,
  13437. #else
  13438. NULL,
  13439. #endif
  13440. error_buf, error_buf_size))
  13441. goto fail;
  13442. #if WASM_ENABLE_GC != 0
  13443. if (wasm_is_type_multi_byte_type(decl_type)) {
  13444. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13445. ref_type,
  13446. wasm_reftype_struct_size(ref_type));
  13447. }
  13448. #endif
  13449. if (opcode1 == WASM_OP_TABLE_GROW) {
  13450. if (table_idx < module->import_table_count) {
  13451. module->import_tables[table_idx]
  13452. .u.table.table_type.possible_grow = true;
  13453. }
  13454. else {
  13455. module
  13456. ->tables[table_idx
  13457. - module->import_table_count]
  13458. .table_type.possible_grow = true;
  13459. }
  13460. }
  13461. #if WASM_ENABLE_FAST_INTERP != 0
  13462. emit_uint32(loader_ctx, table_idx);
  13463. #endif
  13464. POP_I32();
  13465. #if WASM_ENABLE_FAST_INTERP != 0
  13466. POP_OFFSET_TYPE(decl_type);
  13467. #endif
  13468. POP_TYPE(decl_type);
  13469. if (opcode1 == WASM_OP_TABLE_GROW)
  13470. PUSH_I32();
  13471. else
  13472. POP_I32();
  13473. #if WASM_ENABLE_WAMR_COMPILER != 0
  13474. module->is_ref_types_used = true;
  13475. #endif
  13476. break;
  13477. }
  13478. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13479. default:
  13480. set_error_buf_v(error_buf, error_buf_size,
  13481. "%s %02x %02x", "unsupported opcode",
  13482. 0xfc, opcode1);
  13483. goto fail;
  13484. }
  13485. break;
  13486. }
  13487. #if WASM_ENABLE_SIMD != 0
  13488. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13489. case WASM_OP_SIMD_PREFIX:
  13490. {
  13491. /* TODO: memory64 offset type changes */
  13492. uint32 opcode1;
  13493. #if WASM_ENABLE_WAMR_COMPILER != 0
  13494. /* Mark the SIMD instruction is used in this module */
  13495. module->is_simd_used = true;
  13496. #endif
  13497. read_leb_uint32(p, p_end, opcode1);
  13498. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13499. */
  13500. switch (opcode1) {
  13501. /* memory instruction */
  13502. case SIMD_v128_load:
  13503. case SIMD_v128_load8x8_s:
  13504. case SIMD_v128_load8x8_u:
  13505. case SIMD_v128_load16x4_s:
  13506. case SIMD_v128_load16x4_u:
  13507. case SIMD_v128_load32x2_s:
  13508. case SIMD_v128_load32x2_u:
  13509. case SIMD_v128_load8_splat:
  13510. case SIMD_v128_load16_splat:
  13511. case SIMD_v128_load32_splat:
  13512. case SIMD_v128_load64_splat:
  13513. {
  13514. CHECK_MEMORY();
  13515. read_leb_uint32(p, p_end, align); /* align */
  13516. if (!check_simd_memory_access_align(
  13517. opcode1, align, error_buf, error_buf_size)) {
  13518. goto fail;
  13519. }
  13520. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13521. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13522. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13523. func->has_memory_operations = true;
  13524. #endif
  13525. break;
  13526. }
  13527. case SIMD_v128_store:
  13528. {
  13529. CHECK_MEMORY();
  13530. read_leb_uint32(p, p_end, align); /* align */
  13531. if (!check_simd_memory_access_align(
  13532. opcode1, align, error_buf, error_buf_size)) {
  13533. goto fail;
  13534. }
  13535. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13536. POP_V128();
  13537. POP_MEM_OFFSET();
  13538. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13539. func->has_memory_operations = true;
  13540. #endif
  13541. break;
  13542. }
  13543. /* basic operation */
  13544. case SIMD_v128_const:
  13545. {
  13546. CHECK_BUF1(p, p_end, 16);
  13547. p += 16;
  13548. PUSH_V128();
  13549. break;
  13550. }
  13551. case SIMD_v8x16_shuffle:
  13552. {
  13553. V128 mask;
  13554. CHECK_BUF1(p, p_end, 16);
  13555. mask = read_i8x16(p, error_buf, error_buf_size);
  13556. p += 16;
  13557. if (!check_simd_shuffle_mask(mask, error_buf,
  13558. error_buf_size)) {
  13559. goto fail;
  13560. }
  13561. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13562. break;
  13563. }
  13564. case SIMD_v8x16_swizzle:
  13565. {
  13566. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13567. break;
  13568. }
  13569. /* splat operation */
  13570. case SIMD_i8x16_splat:
  13571. case SIMD_i16x8_splat:
  13572. case SIMD_i32x4_splat:
  13573. case SIMD_i64x2_splat:
  13574. case SIMD_f32x4_splat:
  13575. case SIMD_f64x2_splat:
  13576. {
  13577. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13578. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13579. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13580. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13581. VALUE_TYPE_V128);
  13582. break;
  13583. }
  13584. /* lane operation */
  13585. case SIMD_i8x16_extract_lane_s:
  13586. case SIMD_i8x16_extract_lane_u:
  13587. case SIMD_i8x16_replace_lane:
  13588. case SIMD_i16x8_extract_lane_s:
  13589. case SIMD_i16x8_extract_lane_u:
  13590. case SIMD_i16x8_replace_lane:
  13591. case SIMD_i32x4_extract_lane:
  13592. case SIMD_i32x4_replace_lane:
  13593. case SIMD_i64x2_extract_lane:
  13594. case SIMD_i64x2_replace_lane:
  13595. case SIMD_f32x4_extract_lane:
  13596. case SIMD_f32x4_replace_lane:
  13597. case SIMD_f64x2_extract_lane:
  13598. case SIMD_f64x2_replace_lane:
  13599. {
  13600. uint8 lane;
  13601. /* clang-format off */
  13602. uint8 replace[] = {
  13603. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13604. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13605. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13606. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13607. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13608. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13609. };
  13610. uint8 push_type[] = {
  13611. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13612. VALUE_TYPE_V128,
  13613. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13614. VALUE_TYPE_V128,
  13615. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13616. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13617. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13618. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13619. };
  13620. /* clang-format on */
  13621. CHECK_BUF(p, p_end, 1);
  13622. lane = read_uint8(p);
  13623. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13624. error_buf_size)) {
  13625. goto fail;
  13626. }
  13627. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13628. if (!(wasm_loader_pop_frame_ref(
  13629. loader_ctx,
  13630. replace[opcode1
  13631. - SIMD_i8x16_extract_lane_s],
  13632. error_buf, error_buf_size)))
  13633. goto fail;
  13634. }
  13635. POP_AND_PUSH(
  13636. VALUE_TYPE_V128,
  13637. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13638. break;
  13639. }
  13640. /* i8x16 compare operation */
  13641. case SIMD_i8x16_eq:
  13642. case SIMD_i8x16_ne:
  13643. case SIMD_i8x16_lt_s:
  13644. case SIMD_i8x16_lt_u:
  13645. case SIMD_i8x16_gt_s:
  13646. case SIMD_i8x16_gt_u:
  13647. case SIMD_i8x16_le_s:
  13648. case SIMD_i8x16_le_u:
  13649. case SIMD_i8x16_ge_s:
  13650. case SIMD_i8x16_ge_u:
  13651. /* i16x8 compare operation */
  13652. case SIMD_i16x8_eq:
  13653. case SIMD_i16x8_ne:
  13654. case SIMD_i16x8_lt_s:
  13655. case SIMD_i16x8_lt_u:
  13656. case SIMD_i16x8_gt_s:
  13657. case SIMD_i16x8_gt_u:
  13658. case SIMD_i16x8_le_s:
  13659. case SIMD_i16x8_le_u:
  13660. case SIMD_i16x8_ge_s:
  13661. case SIMD_i16x8_ge_u:
  13662. /* i32x4 compare operation */
  13663. case SIMD_i32x4_eq:
  13664. case SIMD_i32x4_ne:
  13665. case SIMD_i32x4_lt_s:
  13666. case SIMD_i32x4_lt_u:
  13667. case SIMD_i32x4_gt_s:
  13668. case SIMD_i32x4_gt_u:
  13669. case SIMD_i32x4_le_s:
  13670. case SIMD_i32x4_le_u:
  13671. case SIMD_i32x4_ge_s:
  13672. case SIMD_i32x4_ge_u:
  13673. /* f32x4 compare operation */
  13674. case SIMD_f32x4_eq:
  13675. case SIMD_f32x4_ne:
  13676. case SIMD_f32x4_lt:
  13677. case SIMD_f32x4_gt:
  13678. case SIMD_f32x4_le:
  13679. case SIMD_f32x4_ge:
  13680. /* f64x2 compare operation */
  13681. case SIMD_f64x2_eq:
  13682. case SIMD_f64x2_ne:
  13683. case SIMD_f64x2_lt:
  13684. case SIMD_f64x2_gt:
  13685. case SIMD_f64x2_le:
  13686. case SIMD_f64x2_ge:
  13687. {
  13688. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13689. break;
  13690. }
  13691. /* v128 operation */
  13692. case SIMD_v128_not:
  13693. {
  13694. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13695. break;
  13696. }
  13697. case SIMD_v128_and:
  13698. case SIMD_v128_andnot:
  13699. case SIMD_v128_or:
  13700. case SIMD_v128_xor:
  13701. {
  13702. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13703. break;
  13704. }
  13705. case SIMD_v128_bitselect:
  13706. {
  13707. POP_V128();
  13708. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13709. break;
  13710. }
  13711. case SIMD_v128_any_true:
  13712. {
  13713. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13714. break;
  13715. }
  13716. /* Load Lane Operation */
  13717. case SIMD_v128_load8_lane:
  13718. case SIMD_v128_load16_lane:
  13719. case SIMD_v128_load32_lane:
  13720. case SIMD_v128_load64_lane:
  13721. case SIMD_v128_store8_lane:
  13722. case SIMD_v128_store16_lane:
  13723. case SIMD_v128_store32_lane:
  13724. case SIMD_v128_store64_lane:
  13725. {
  13726. uint8 lane;
  13727. CHECK_MEMORY();
  13728. read_leb_uint32(p, p_end, align); /* align */
  13729. if (!check_simd_memory_access_align(
  13730. opcode1, align, error_buf, error_buf_size)) {
  13731. goto fail;
  13732. }
  13733. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13734. CHECK_BUF(p, p_end, 1);
  13735. lane = read_uint8(p);
  13736. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13737. error_buf_size)) {
  13738. goto fail;
  13739. }
  13740. POP_V128();
  13741. POP_MEM_OFFSET();
  13742. if (opcode1 < SIMD_v128_store8_lane) {
  13743. PUSH_V128();
  13744. }
  13745. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13746. func->has_memory_operations = true;
  13747. #endif
  13748. break;
  13749. }
  13750. case SIMD_v128_load32_zero:
  13751. case SIMD_v128_load64_zero:
  13752. {
  13753. CHECK_MEMORY();
  13754. read_leb_uint32(p, p_end, align); /* align */
  13755. if (!check_simd_memory_access_align(
  13756. opcode1, align, error_buf, error_buf_size)) {
  13757. goto fail;
  13758. }
  13759. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13760. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13761. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13762. func->has_memory_operations = true;
  13763. #endif
  13764. break;
  13765. }
  13766. /* Float conversion */
  13767. case SIMD_f32x4_demote_f64x2_zero:
  13768. case SIMD_f64x2_promote_low_f32x4_zero:
  13769. {
  13770. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13771. break;
  13772. }
  13773. /* i8x16 Operation */
  13774. case SIMD_i8x16_abs:
  13775. case SIMD_i8x16_neg:
  13776. case SIMD_i8x16_popcnt:
  13777. {
  13778. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13779. break;
  13780. }
  13781. case SIMD_i8x16_all_true:
  13782. case SIMD_i8x16_bitmask:
  13783. {
  13784. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13785. break;
  13786. }
  13787. case SIMD_i8x16_narrow_i16x8_s:
  13788. case SIMD_i8x16_narrow_i16x8_u:
  13789. {
  13790. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13791. break;
  13792. }
  13793. case SIMD_f32x4_ceil:
  13794. case SIMD_f32x4_floor:
  13795. case SIMD_f32x4_trunc:
  13796. case SIMD_f32x4_nearest:
  13797. {
  13798. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13799. break;
  13800. }
  13801. case SIMD_i8x16_shl:
  13802. case SIMD_i8x16_shr_s:
  13803. case SIMD_i8x16_shr_u:
  13804. {
  13805. POP_I32();
  13806. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13807. break;
  13808. }
  13809. case SIMD_i8x16_add:
  13810. case SIMD_i8x16_add_sat_s:
  13811. case SIMD_i8x16_add_sat_u:
  13812. case SIMD_i8x16_sub:
  13813. case SIMD_i8x16_sub_sat_s:
  13814. case SIMD_i8x16_sub_sat_u:
  13815. {
  13816. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13817. break;
  13818. }
  13819. case SIMD_f64x2_ceil:
  13820. case SIMD_f64x2_floor:
  13821. {
  13822. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13823. break;
  13824. }
  13825. case SIMD_i8x16_min_s:
  13826. case SIMD_i8x16_min_u:
  13827. case SIMD_i8x16_max_s:
  13828. case SIMD_i8x16_max_u:
  13829. {
  13830. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13831. break;
  13832. }
  13833. case SIMD_f64x2_trunc:
  13834. {
  13835. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13836. break;
  13837. }
  13838. case SIMD_i8x16_avgr_u:
  13839. {
  13840. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13841. break;
  13842. }
  13843. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13844. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13845. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13846. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13847. /* i16x8 operation */
  13848. case SIMD_i16x8_abs:
  13849. case SIMD_i16x8_neg:
  13850. {
  13851. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13852. break;
  13853. }
  13854. case SIMD_i16x8_q15mulr_sat_s:
  13855. {
  13856. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13857. break;
  13858. }
  13859. case SIMD_i16x8_all_true:
  13860. case SIMD_i16x8_bitmask:
  13861. {
  13862. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13863. break;
  13864. }
  13865. case SIMD_i16x8_narrow_i32x4_s:
  13866. case SIMD_i16x8_narrow_i32x4_u:
  13867. {
  13868. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13869. break;
  13870. }
  13871. case SIMD_i16x8_extend_low_i8x16_s:
  13872. case SIMD_i16x8_extend_high_i8x16_s:
  13873. case SIMD_i16x8_extend_low_i8x16_u:
  13874. case SIMD_i16x8_extend_high_i8x16_u:
  13875. {
  13876. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13877. break;
  13878. }
  13879. case SIMD_i16x8_shl:
  13880. case SIMD_i16x8_shr_s:
  13881. case SIMD_i16x8_shr_u:
  13882. {
  13883. POP_I32();
  13884. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13885. break;
  13886. }
  13887. case SIMD_i16x8_add:
  13888. case SIMD_i16x8_add_sat_s:
  13889. case SIMD_i16x8_add_sat_u:
  13890. case SIMD_i16x8_sub:
  13891. case SIMD_i16x8_sub_sat_s:
  13892. case SIMD_i16x8_sub_sat_u:
  13893. {
  13894. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13895. break;
  13896. }
  13897. case SIMD_f64x2_nearest:
  13898. {
  13899. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13900. break;
  13901. }
  13902. case SIMD_i16x8_mul:
  13903. case SIMD_i16x8_min_s:
  13904. case SIMD_i16x8_min_u:
  13905. case SIMD_i16x8_max_s:
  13906. case SIMD_i16x8_max_u:
  13907. case SIMD_i16x8_avgr_u:
  13908. case SIMD_i16x8_extmul_low_i8x16_s:
  13909. case SIMD_i16x8_extmul_high_i8x16_s:
  13910. case SIMD_i16x8_extmul_low_i8x16_u:
  13911. case SIMD_i16x8_extmul_high_i8x16_u:
  13912. {
  13913. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13914. break;
  13915. }
  13916. /* i32x4 operation */
  13917. case SIMD_i32x4_abs:
  13918. case SIMD_i32x4_neg:
  13919. {
  13920. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13921. break;
  13922. }
  13923. case SIMD_i32x4_all_true:
  13924. case SIMD_i32x4_bitmask:
  13925. {
  13926. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13927. break;
  13928. }
  13929. case SIMD_i32x4_extend_low_i16x8_s:
  13930. case SIMD_i32x4_extend_high_i16x8_s:
  13931. case SIMD_i32x4_extend_low_i16x8_u:
  13932. case SIMD_i32x4_extend_high_i16x8_u:
  13933. {
  13934. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13935. break;
  13936. }
  13937. case SIMD_i32x4_shl:
  13938. case SIMD_i32x4_shr_s:
  13939. case SIMD_i32x4_shr_u:
  13940. {
  13941. POP_I32();
  13942. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13943. break;
  13944. }
  13945. case SIMD_i32x4_add:
  13946. case SIMD_i32x4_sub:
  13947. case SIMD_i32x4_mul:
  13948. case SIMD_i32x4_min_s:
  13949. case SIMD_i32x4_min_u:
  13950. case SIMD_i32x4_max_s:
  13951. case SIMD_i32x4_max_u:
  13952. case SIMD_i32x4_dot_i16x8_s:
  13953. case SIMD_i32x4_extmul_low_i16x8_s:
  13954. case SIMD_i32x4_extmul_high_i16x8_s:
  13955. case SIMD_i32x4_extmul_low_i16x8_u:
  13956. case SIMD_i32x4_extmul_high_i16x8_u:
  13957. {
  13958. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13959. break;
  13960. }
  13961. /* i64x2 operation */
  13962. case SIMD_i64x2_abs:
  13963. case SIMD_i64x2_neg:
  13964. {
  13965. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13966. break;
  13967. }
  13968. case SIMD_i64x2_all_true:
  13969. case SIMD_i64x2_bitmask:
  13970. {
  13971. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13972. break;
  13973. }
  13974. case SIMD_i64x2_extend_low_i32x4_s:
  13975. case SIMD_i64x2_extend_high_i32x4_s:
  13976. case SIMD_i64x2_extend_low_i32x4_u:
  13977. case SIMD_i64x2_extend_high_i32x4_u:
  13978. {
  13979. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13980. break;
  13981. }
  13982. case SIMD_i64x2_shl:
  13983. case SIMD_i64x2_shr_s:
  13984. case SIMD_i64x2_shr_u:
  13985. {
  13986. POP_I32();
  13987. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13988. break;
  13989. }
  13990. case SIMD_i64x2_add:
  13991. case SIMD_i64x2_sub:
  13992. case SIMD_i64x2_mul:
  13993. case SIMD_i64x2_eq:
  13994. case SIMD_i64x2_ne:
  13995. case SIMD_i64x2_lt_s:
  13996. case SIMD_i64x2_gt_s:
  13997. case SIMD_i64x2_le_s:
  13998. case SIMD_i64x2_ge_s:
  13999. case SIMD_i64x2_extmul_low_i32x4_s:
  14000. case SIMD_i64x2_extmul_high_i32x4_s:
  14001. case SIMD_i64x2_extmul_low_i32x4_u:
  14002. case SIMD_i64x2_extmul_high_i32x4_u:
  14003. {
  14004. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14005. break;
  14006. }
  14007. /* f32x4 operation */
  14008. case SIMD_f32x4_abs:
  14009. case SIMD_f32x4_neg:
  14010. case SIMD_f32x4_sqrt:
  14011. {
  14012. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14013. break;
  14014. }
  14015. case SIMD_f32x4_add:
  14016. case SIMD_f32x4_sub:
  14017. case SIMD_f32x4_mul:
  14018. case SIMD_f32x4_div:
  14019. case SIMD_f32x4_min:
  14020. case SIMD_f32x4_max:
  14021. case SIMD_f32x4_pmin:
  14022. case SIMD_f32x4_pmax:
  14023. {
  14024. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14025. break;
  14026. }
  14027. /* f64x2 operation */
  14028. case SIMD_f64x2_abs:
  14029. case SIMD_f64x2_neg:
  14030. case SIMD_f64x2_sqrt:
  14031. {
  14032. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14033. break;
  14034. }
  14035. case SIMD_f64x2_add:
  14036. case SIMD_f64x2_sub:
  14037. case SIMD_f64x2_mul:
  14038. case SIMD_f64x2_div:
  14039. case SIMD_f64x2_min:
  14040. case SIMD_f64x2_max:
  14041. case SIMD_f64x2_pmin:
  14042. case SIMD_f64x2_pmax:
  14043. {
  14044. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14045. break;
  14046. }
  14047. case SIMD_i32x4_trunc_sat_f32x4_s:
  14048. case SIMD_i32x4_trunc_sat_f32x4_u:
  14049. case SIMD_f32x4_convert_i32x4_s:
  14050. case SIMD_f32x4_convert_i32x4_u:
  14051. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14052. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14053. case SIMD_f64x2_convert_low_i32x4_s:
  14054. case SIMD_f64x2_convert_low_i32x4_u:
  14055. {
  14056. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14057. break;
  14058. }
  14059. default:
  14060. {
  14061. if (error_buf != NULL) {
  14062. snprintf(error_buf, error_buf_size,
  14063. "WASM module load failed: "
  14064. "invalid opcode 0xfd %02x.",
  14065. opcode1);
  14066. }
  14067. goto fail;
  14068. }
  14069. }
  14070. break;
  14071. }
  14072. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  14073. #endif /* end of WASM_ENABLE_SIMD */
  14074. #if WASM_ENABLE_SHARED_MEMORY != 0
  14075. case WASM_OP_ATOMIC_PREFIX:
  14076. {
  14077. uint32 opcode1;
  14078. read_leb_uint32(p, p_end, opcode1);
  14079. #if WASM_ENABLE_FAST_INTERP != 0
  14080. emit_byte(loader_ctx, opcode1);
  14081. #endif
  14082. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14083. CHECK_MEMORY();
  14084. read_leb_uint32(p, p_end, align); /* align */
  14085. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14086. if (!check_memory_align_equal(opcode1, align, error_buf,
  14087. error_buf_size)) {
  14088. goto fail;
  14089. }
  14090. #if WASM_ENABLE_FAST_INTERP != 0
  14091. emit_uint32(loader_ctx, mem_offset);
  14092. #endif
  14093. }
  14094. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14095. func->has_memory_operations = true;
  14096. #endif
  14097. switch (opcode1) {
  14098. case WASM_OP_ATOMIC_NOTIFY:
  14099. POP_I32();
  14100. POP_MEM_OFFSET();
  14101. PUSH_I32();
  14102. break;
  14103. case WASM_OP_ATOMIC_WAIT32:
  14104. POP_I64();
  14105. POP_I32();
  14106. POP_MEM_OFFSET();
  14107. PUSH_I32();
  14108. break;
  14109. case WASM_OP_ATOMIC_WAIT64:
  14110. POP_I64();
  14111. POP_I64();
  14112. POP_MEM_OFFSET();
  14113. PUSH_I32();
  14114. break;
  14115. case WASM_OP_ATOMIC_FENCE:
  14116. /* reserved byte 0x00 */
  14117. if (*p++ != 0x00) {
  14118. set_error_buf(error_buf, error_buf_size,
  14119. "zero byte expected");
  14120. goto fail;
  14121. }
  14122. break;
  14123. case WASM_OP_ATOMIC_I32_LOAD:
  14124. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14125. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14126. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14127. break;
  14128. case WASM_OP_ATOMIC_I32_STORE:
  14129. case WASM_OP_ATOMIC_I32_STORE8:
  14130. case WASM_OP_ATOMIC_I32_STORE16:
  14131. POP_I32();
  14132. POP_MEM_OFFSET();
  14133. break;
  14134. case WASM_OP_ATOMIC_I64_LOAD:
  14135. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14136. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14137. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14138. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14139. break;
  14140. case WASM_OP_ATOMIC_I64_STORE:
  14141. case WASM_OP_ATOMIC_I64_STORE8:
  14142. case WASM_OP_ATOMIC_I64_STORE16:
  14143. case WASM_OP_ATOMIC_I64_STORE32:
  14144. POP_I64();
  14145. POP_MEM_OFFSET();
  14146. break;
  14147. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14148. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14149. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14150. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14151. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14152. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14153. case WASM_OP_ATOMIC_RMW_I32_AND:
  14154. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14155. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14156. case WASM_OP_ATOMIC_RMW_I32_OR:
  14157. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14158. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14159. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14160. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14161. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14162. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14163. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14164. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14165. POP_I32();
  14166. POP_MEM_OFFSET();
  14167. PUSH_I32();
  14168. break;
  14169. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14170. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14171. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14172. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14173. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14174. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14175. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14176. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14177. case WASM_OP_ATOMIC_RMW_I64_AND:
  14178. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14179. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14180. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14181. case WASM_OP_ATOMIC_RMW_I64_OR:
  14182. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14183. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14184. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14185. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14186. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14187. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14188. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14189. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14190. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14191. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14192. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14193. POP_I64();
  14194. POP_MEM_OFFSET();
  14195. PUSH_I64();
  14196. break;
  14197. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14198. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14199. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14200. POP_I32();
  14201. POP_I32();
  14202. POP_MEM_OFFSET();
  14203. PUSH_I32();
  14204. break;
  14205. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14206. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14207. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14208. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14209. POP_I64();
  14210. POP_I64();
  14211. POP_MEM_OFFSET();
  14212. PUSH_I64();
  14213. break;
  14214. default:
  14215. set_error_buf_v(error_buf, error_buf_size,
  14216. "%s %02x %02x", "unsupported opcode",
  14217. 0xfe, opcode1);
  14218. goto fail;
  14219. }
  14220. break;
  14221. }
  14222. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14223. default:
  14224. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14225. "unsupported opcode", opcode);
  14226. goto fail;
  14227. }
  14228. #if WASM_ENABLE_FAST_INTERP != 0
  14229. last_op = opcode;
  14230. #endif
  14231. }
  14232. if (loader_ctx->csp_num > 0) {
  14233. if (cur_func_idx < module->function_count - 1)
  14234. /* Function with missing end marker (between two functions) */
  14235. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14236. else
  14237. /* Function with missing end marker
  14238. (at EOF or end of code sections) */
  14239. set_error_buf(error_buf, error_buf_size,
  14240. "unexpected end of section or function, "
  14241. "or section size mismatch");
  14242. goto fail;
  14243. }
  14244. #if WASM_ENABLE_FAST_INTERP != 0
  14245. if (loader_ctx->p_code_compiled == NULL)
  14246. goto re_scan;
  14247. func->const_cell_num = loader_ctx->const_cell_num;
  14248. if (func->const_cell_num > 0) {
  14249. int32 j;
  14250. if (!(func->consts = func_const = loader_malloc(
  14251. func->const_cell_num * 4, error_buf, error_buf_size)))
  14252. goto fail;
  14253. func_const_end = func->consts + func->const_cell_num * 4;
  14254. /* reverse the const buf */
  14255. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14256. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14257. if (c->value_type == VALUE_TYPE_F64
  14258. || c->value_type == VALUE_TYPE_I64) {
  14259. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14260. &(c->value.f64), (uint32)sizeof(int64));
  14261. func_const += sizeof(int64);
  14262. }
  14263. else {
  14264. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14265. &(c->value.f32), (uint32)sizeof(int32));
  14266. func_const += sizeof(int32);
  14267. }
  14268. }
  14269. }
  14270. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14271. - loader_ctx->start_dynamic_offset + 1;
  14272. #else
  14273. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14274. #endif
  14275. func->max_block_num = loader_ctx->max_csp_num;
  14276. return_value = true;
  14277. fail:
  14278. wasm_loader_ctx_destroy(loader_ctx);
  14279. (void)table_idx;
  14280. (void)table_seg_idx;
  14281. (void)data_seg_idx;
  14282. (void)i64_const;
  14283. (void)local_offset;
  14284. (void)p_org;
  14285. (void)mem_offset;
  14286. (void)align;
  14287. return return_value;
  14288. }