wasm_loader.c 631 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_platform.h"
  7. #include "wasm.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_runtime.h"
  10. #include "wasm_loader_common.h"
  11. #include "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.mem_type.flags
  43. & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. static bool
  49. is_table_64bit(WASMModule *module, uint32 table_idx)
  50. {
  51. if (table_idx < module->import_table_count)
  52. return !!(module->import_tables[table_idx].u.table.table_type.flags
  53. & TABLE64_FLAG);
  54. else
  55. return !!(module->tables[table_idx - module->import_table_count]
  56. .table_type.flags
  57. & TABLE64_FLAG);
  58. return false;
  59. }
  60. #endif
  61. static void
  62. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  63. {
  64. wasm_loader_set_error_buf(error_buf, error_buf_size, string, false);
  65. }
  66. #if WASM_ENABLE_MEMORY64 != 0
  67. static void
  68. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  69. {
  70. if (error_buf != NULL) {
  71. snprintf(error_buf, error_buf_size, "offset out of range");
  72. }
  73. }
  74. #endif
  75. static void
  76. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  77. {
  78. va_list args;
  79. char buf[128];
  80. if (error_buf != NULL) {
  81. va_start(args, format);
  82. vsnprintf(buf, sizeof(buf), format, args);
  83. va_end(args);
  84. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  85. }
  86. }
  87. static bool
  88. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  89. char *error_buf, uint32 error_buf_size)
  90. {
  91. if ((uintptr_t)buf + length < (uintptr_t)buf
  92. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  93. set_error_buf(error_buf, error_buf_size,
  94. "unexpected end of section or function");
  95. return false;
  96. }
  97. return true;
  98. }
  99. static bool
  100. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  101. char *error_buf, uint32 error_buf_size)
  102. {
  103. if ((uintptr_t)buf + length < (uintptr_t)buf
  104. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  105. set_error_buf(error_buf, error_buf_size, "unexpected end");
  106. return false;
  107. }
  108. return true;
  109. }
  110. #define CHECK_BUF(buf, buf_end, length) \
  111. do { \
  112. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  113. goto fail; \
  114. } \
  115. } while (0)
  116. #define CHECK_BUF1(buf, buf_end, length) \
  117. do { \
  118. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  119. goto fail; \
  120. } \
  121. } while (0)
  122. #define skip_leb(p) while (*p++ & 0x80)
  123. #define skip_leb_int64(p, p_end) skip_leb(p)
  124. #define skip_leb_uint32(p, p_end) skip_leb(p)
  125. #define skip_leb_int32(p, p_end) skip_leb(p)
  126. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  127. #define skip_leb_memidx(p, p_end) skip_leb(p)
  128. #if WASM_ENABLE_MULTI_MEMORY == 0
  129. #define skip_leb_align(p, p_end) skip_leb(p)
  130. #else
  131. /* Skip the following memidx if applicable */
  132. #define skip_leb_align(p, p_end) \
  133. do { \
  134. if (*p++ & OPT_MEMIDX_FLAG) \
  135. skip_leb_uint32(p, p_end); \
  136. } while (0)
  137. #endif
  138. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  139. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  140. #define read_leb_int64(p, p_end, res) \
  141. do { \
  142. uint64 res64; \
  143. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  144. error_buf_size)) \
  145. goto fail; \
  146. res = (int64)res64; \
  147. } while (0)
  148. #if WASM_ENABLE_MEMORY64 != 0
  149. #define read_leb_mem_offset(p, p_end, res) \
  150. do { \
  151. uint64 res64; \
  152. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  153. &res64, error_buf, error_buf_size)) { \
  154. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  155. goto fail; \
  156. } \
  157. res = (mem_offset_t)res64; \
  158. } while (0)
  159. #else
  160. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  161. #endif
  162. #define read_leb_uint32(p, p_end, res) \
  163. do { \
  164. uint64 res64; \
  165. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  166. error_buf_size)) \
  167. goto fail; \
  168. res = (uint32)res64; \
  169. } while (0)
  170. #define read_leb_int32(p, p_end, res) \
  171. do { \
  172. uint64 res64; \
  173. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  174. error_buf_size)) \
  175. goto fail; \
  176. res = (int32)res64; \
  177. } while (0)
  178. #if WASM_ENABLE_MULTI_MEMORY != 0
  179. #define check_memidx(module, memidx) \
  180. do { \
  181. if (memidx >= module->import_memory_count + module->memory_count) { \
  182. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d", \
  183. memidx); \
  184. goto fail; \
  185. } \
  186. } while (0)
  187. /* Bit 6(0x40) indicating the optional memidx, and reset bit 6 for
  188. * alignment check */
  189. #define read_leb_memarg(p, p_end, res) \
  190. do { \
  191. read_leb_uint32(p, p_end, res); \
  192. if (res & OPT_MEMIDX_FLAG) { \
  193. res &= ~OPT_MEMIDX_FLAG; \
  194. read_leb_uint32(p, p_end, memidx); /* memidx */ \
  195. check_memidx(module, memidx); \
  196. } \
  197. } while (0)
  198. #else
  199. /* reserved byte 0x00 */
  200. #define check_memidx(module, memidx) \
  201. do { \
  202. (void)module; \
  203. if (memidx != 0) { \
  204. set_error_buf(error_buf, error_buf_size, "zero byte expected"); \
  205. goto fail; \
  206. } \
  207. } while (0)
  208. #define read_leb_memarg(p, p_end, res) read_leb_uint32(p, p_end, res)
  209. #endif
  210. static char *
  211. type2str(uint8 type)
  212. {
  213. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  214. #if WASM_ENABLE_GC != 0
  215. char *type_str_ref[] = { "stringview_iter",
  216. "stringview_wtf16",
  217. "(ref null ht)",
  218. "(ref ht)",
  219. "", /* reserved */
  220. "stringview_wtf8",
  221. "stringref",
  222. "", /* reserved */
  223. "", /* reserved */
  224. "arrayref",
  225. "structref",
  226. "i31ref",
  227. "eqref",
  228. "anyref",
  229. "externref",
  230. "funcref",
  231. "nullref",
  232. "nullexternref",
  233. "nullfuncref" };
  234. #endif
  235. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  236. return type_str[type - VALUE_TYPE_V128];
  237. #if WASM_ENABLE_GC != 0
  238. else if (wasm_is_type_reftype(type))
  239. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  240. #endif
  241. else if (type == VALUE_TYPE_FUNCREF)
  242. return "funcref";
  243. else if (type == VALUE_TYPE_EXTERNREF)
  244. return "externref";
  245. else
  246. return "unknown type";
  247. }
  248. static bool
  249. is_32bit_type(uint8 type)
  250. {
  251. if (type == VALUE_TYPE_I32
  252. || type == VALUE_TYPE_F32
  253. /* the operand stack is in polymorphic state */
  254. || type == VALUE_TYPE_ANY
  255. #if WASM_ENABLE_GC != 0
  256. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  257. #elif WASM_ENABLE_REF_TYPES != 0
  258. /* For reference types, we use uint32 index to represent
  259. the funcref and externref */
  260. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  261. #endif
  262. )
  263. return true;
  264. return false;
  265. }
  266. static bool
  267. is_64bit_type(uint8 type)
  268. {
  269. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  270. #if WASM_ENABLE_GC != 0
  271. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  272. #endif
  273. )
  274. return true;
  275. return false;
  276. }
  277. #if WASM_ENABLE_GC != 0
  278. static bool
  279. is_packed_type(uint8 type)
  280. {
  281. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  282. }
  283. #endif
  284. static bool
  285. is_byte_a_type(uint8 type)
  286. {
  287. return (is_valid_value_type_for_interpreter(type)
  288. || (type == VALUE_TYPE_VOID))
  289. ? true
  290. : false;
  291. }
  292. #if WASM_ENABLE_SIMD != 0
  293. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  294. || (WASM_ENABLE_FAST_INTERP != 0)
  295. static V128
  296. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  297. {
  298. V128 result;
  299. uint8 i;
  300. for (i = 0; i != 16; ++i) {
  301. result.i8x16[i] = read_uint8(p_buf);
  302. }
  303. return result;
  304. }
  305. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  306. (WASM_ENABLE_FAST_INTERP != 0) */
  307. #endif /* end of WASM_ENABLE_SIMD */
  308. static void *
  309. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  310. {
  311. void *mem;
  312. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  313. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  314. return NULL;
  315. }
  316. memset(mem, 0, (uint32)size);
  317. return mem;
  318. }
  319. static void *
  320. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  321. uint32 error_buf_size)
  322. {
  323. uint8 *mem_new;
  324. bh_assert(size_new > size_old);
  325. if ((mem_new = wasm_runtime_realloc(mem_old, size_new))) {
  326. memset(mem_new + size_old, 0, size_new - size_old);
  327. return mem_new;
  328. }
  329. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  330. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  331. wasm_runtime_free(mem_old);
  332. }
  333. return mem_new;
  334. }
  335. #define MEM_REALLOC(mem, size_old, size_new) \
  336. do { \
  337. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  338. error_buf_size); \
  339. if (!mem_new) \
  340. goto fail; \
  341. mem = mem_new; \
  342. } while (0)
  343. static bool
  344. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  345. char *error_buf, uint32 error_buf_size)
  346. {
  347. if (type_index >= type_count) {
  348. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  349. type_index);
  350. return false;
  351. }
  352. return true;
  353. }
  354. #if WASM_ENABLE_GC != 0
  355. static bool
  356. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  357. uint32 error_buf_size)
  358. {
  359. if (!check_type_index(module, module->type_count, type_index, error_buf,
  360. error_buf_size)) {
  361. return false;
  362. }
  363. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  364. set_error_buf(error_buf, error_buf_size, "unknown array type");
  365. return false;
  366. }
  367. return true;
  368. }
  369. #endif
  370. /*
  371. * if no GC is enabled, an valid type is always a function type.
  372. * but if GC is enabled, we need to check the type flag
  373. */
  374. static bool
  375. check_function_type(const WASMModule *module, uint32 type_index,
  376. char *error_buf, uint32 error_buf_size)
  377. {
  378. if (!check_type_index(module, module->type_count, type_index, error_buf,
  379. error_buf_size)) {
  380. return false;
  381. }
  382. #if WASM_ENABLE_GC != 0
  383. if (module->types[type_index]->type_flag != WASM_TYPE_FUNC) {
  384. set_error_buf(error_buf, error_buf_size, "unknown function type");
  385. return false;
  386. }
  387. #endif
  388. return true;
  389. }
  390. static bool
  391. check_function_index(const WASMModule *module, uint32 function_index,
  392. char *error_buf, uint32 error_buf_size)
  393. {
  394. if (function_index
  395. >= module->import_function_count + module->function_count) {
  396. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  397. function_index);
  398. return false;
  399. }
  400. return true;
  401. }
  402. typedef struct InitValue {
  403. uint8 type;
  404. uint8 flag;
  405. #if WASM_ENABLE_GC != 0
  406. uint8 gc_opcode;
  407. WASMRefType ref_type;
  408. #endif
  409. WASMValue value;
  410. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  411. InitializerExpression *expr;
  412. #endif
  413. } InitValue;
  414. typedef struct ConstExprContext {
  415. uint32 sp;
  416. uint32 size;
  417. WASMModule *module;
  418. InitValue *stack;
  419. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  420. } ConstExprContext;
  421. static void
  422. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  423. {
  424. ctx->sp = 0;
  425. ctx->module = module;
  426. ctx->stack = ctx->data;
  427. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  428. }
  429. static bool
  430. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  431. #if WASM_ENABLE_GC != 0
  432. WASMRefType *ref_type, uint8 gc_opcode,
  433. #endif
  434. WASMValue *value,
  435. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  436. InitializerExpression *expr,
  437. #endif
  438. char *error_buf, uint32 error_buf_size)
  439. {
  440. InitValue *cur_value;
  441. if (ctx->sp >= ctx->size) {
  442. if (ctx->stack != ctx->data) {
  443. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  444. (ctx->size + 4) * sizeof(InitValue));
  445. }
  446. else {
  447. if (!(ctx->stack =
  448. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  449. error_buf, error_buf_size))) {
  450. goto fail;
  451. }
  452. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  453. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  454. }
  455. ctx->size += 4;
  456. }
  457. cur_value = &ctx->stack[ctx->sp++];
  458. cur_value->type = type;
  459. cur_value->flag = flag;
  460. cur_value->value = *value;
  461. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  462. cur_value->expr = expr;
  463. #endif
  464. #if WASM_ENABLE_GC != 0
  465. cur_value->gc_opcode = gc_opcode;
  466. if (wasm_is_type_multi_byte_type(type)) {
  467. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  468. ref_type, wasm_reftype_struct_size(ref_type));
  469. }
  470. #endif
  471. return true;
  472. fail:
  473. return false;
  474. }
  475. #if WASM_ENABLE_GC != 0
  476. static void
  477. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  478. {
  479. WASMStructNewInitValues *struct_init_values =
  480. (WASMStructNewInitValues *)data;
  481. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  482. WASMType *wasm_type;
  483. uint32 i;
  484. if (!data)
  485. return;
  486. wasm_type = module->types[struct_init_values->type_idx];
  487. /* The data can only be type of `WASMStructNewInitValues *`
  488. or `WASMArrayNewInitValues *` */
  489. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  490. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  491. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  492. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  493. WASMRefType *ref_type;
  494. uint8 field_type;
  495. uint16 ref_type_map_index = 0;
  496. for (i = 0; i < struct_init_values->count; i++) {
  497. field_type = struct_type->fields[i].field_type;
  498. if (wasm_is_type_multi_byte_type(field_type))
  499. ref_type =
  500. struct_type->ref_type_maps[ref_type_map_index++].ref_type;
  501. else
  502. ref_type = NULL;
  503. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  504. REF_TYPE_STRUCTREF, NULL,
  505. module->types, module->type_count)
  506. || wasm_reftype_is_subtype_of(
  507. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  508. module->types, module->type_count)) {
  509. destroy_init_expr_data_recursive(
  510. module, struct_init_values->fields[i].data);
  511. }
  512. }
  513. }
  514. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  515. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  516. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  517. uint8 elem_type = array_type->elem_type;
  518. for (i = 0; i < array_init_values->length; i++) {
  519. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  520. REF_TYPE_STRUCTREF, NULL,
  521. module->types, module->type_count)
  522. || wasm_reftype_is_subtype_of(
  523. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  524. module->types, module->type_count)) {
  525. destroy_init_expr_data_recursive(
  526. module, array_init_values->elem_data[i].data);
  527. }
  528. }
  529. }
  530. wasm_runtime_free(data);
  531. }
  532. #endif
  533. static bool
  534. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  535. #if WASM_ENABLE_GC != 0
  536. WASMRefType *ref_type, uint8 *p_gc_opcode,
  537. #endif
  538. WASMValue *p_value,
  539. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  540. InitializerExpression **p_expr,
  541. #endif
  542. char *error_buf, uint32 error_buf_size)
  543. {
  544. InitValue *cur_value;
  545. if (ctx->sp == 0) {
  546. set_error_buf(error_buf, error_buf_size,
  547. "type mismatch: const expr stack underflow");
  548. return false;
  549. }
  550. cur_value = &ctx->stack[--ctx->sp];
  551. #if WASM_ENABLE_GC == 0
  552. if (cur_value->type != type) {
  553. set_error_buf(error_buf, error_buf_size, "type mismatch");
  554. return false;
  555. }
  556. #else
  557. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  558. ref_type, ctx->module->types,
  559. ctx->module->type_count)) {
  560. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  561. "type mismatch: expect ", type2str(type),
  562. " but got other");
  563. goto fail;
  564. }
  565. #endif
  566. if (p_flag)
  567. *p_flag = cur_value->flag;
  568. if (p_value)
  569. *p_value = cur_value->value;
  570. #if WASM_ENABLE_GC != 0
  571. if (p_gc_opcode)
  572. *p_gc_opcode = cur_value->gc_opcode;
  573. #endif
  574. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  575. if (p_expr)
  576. *p_expr = cur_value->expr;
  577. #endif
  578. return true;
  579. #if WASM_ENABLE_GC != 0
  580. fail:
  581. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  582. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  583. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  584. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  585. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  586. }
  587. return false;
  588. #endif
  589. }
  590. static void
  591. destroy_const_expr_stack(ConstExprContext *ctx, bool free_exprs)
  592. {
  593. #if WASM_ENABLE_GC != 0
  594. uint32 i;
  595. for (i = 0; i < ctx->sp; i++) {
  596. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  597. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  598. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  599. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  600. destroy_init_expr_data_recursive(ctx->module,
  601. ctx->stack[i].value.data);
  602. }
  603. }
  604. #endif
  605. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  606. if (free_exprs) {
  607. for (uint32 j = 0; j < ctx->sp; j++) {
  608. if (is_expr_binary_op(ctx->stack[j].expr->init_expr_type)) {
  609. destroy_init_expr_recursive(ctx->stack[j].expr);
  610. ctx->stack[j].expr = NULL;
  611. }
  612. }
  613. }
  614. #endif
  615. if (ctx->stack != ctx->data) {
  616. wasm_runtime_free(ctx->stack);
  617. }
  618. }
  619. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  620. static void
  621. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  622. {
  623. #if WASM_ENABLE_GC != 0
  624. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  625. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  626. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  627. destroy_init_expr_data_recursive(module, expr->u.unary.v.data);
  628. }
  629. #endif
  630. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  631. /* free left expr and right exprs for binary operand */
  632. if (!is_expr_binary_op(expr->init_expr_type)) {
  633. return;
  634. }
  635. if (expr->u.binary.l_expr) {
  636. destroy_init_expr_recursive(expr->u.binary.l_expr);
  637. }
  638. if (expr->u.binary.r_expr) {
  639. destroy_init_expr_recursive(expr->u.binary.r_expr);
  640. }
  641. expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
  642. #endif
  643. }
  644. #endif
  645. /* for init expr
  646. * (data (i32.add (i32.const 0) (i32.sub (i32.const 1) (i32.const 2)))),
  647. * the binary format is
  648. * 0x11: 41 00 ; i32.const 0
  649. * 0x13: 41 01 ; i32.const 1
  650. * 0x15: 41 02 ; i32.const 2
  651. * 0x17: 6b ; i32.sub
  652. * 0x18: 6a ; i32.add
  653. * for traversal: read opcodes and push them onto the stack. When encountering
  654. * a binary opcode, pop two values from the stack which become the left and
  655. * right child nodes of this binary operation node.
  656. */
  657. static bool
  658. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  659. InitializerExpression *init_expr, uint8 type, void *ref_type,
  660. char *error_buf, uint32 error_buf_size)
  661. {
  662. const uint8 *p = *p_buf, *p_end = buf_end;
  663. uint8 flag, *p_float;
  664. uint32 i;
  665. ConstExprContext const_expr_ctx = { 0 };
  666. WASMValue cur_value;
  667. #if WASM_ENABLE_GC != 0
  668. uint32 opcode1, type_idx;
  669. uint8 opcode;
  670. WASMRefType cur_ref_type = { 0 };
  671. #endif
  672. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  673. InitializerExpression *cur_expr = NULL;
  674. #endif
  675. init_const_expr_stack(&const_expr_ctx, module);
  676. CHECK_BUF(p, p_end, 1);
  677. flag = read_uint8(p);
  678. while (flag != WASM_OP_END) {
  679. switch (flag) {
  680. /* i32.const */
  681. case INIT_EXPR_TYPE_I32_CONST:
  682. read_leb_int32(p, p_end, cur_value.i32);
  683. if (!push_const_expr_stack(&const_expr_ctx, flag,
  684. VALUE_TYPE_I32,
  685. #if WASM_ENABLE_GC != 0
  686. NULL, 0,
  687. #endif
  688. &cur_value,
  689. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  690. NULL,
  691. #endif
  692. error_buf, error_buf_size))
  693. goto fail;
  694. break;
  695. /* i64.const */
  696. case INIT_EXPR_TYPE_I64_CONST:
  697. read_leb_int64(p, p_end, cur_value.i64);
  698. if (!push_const_expr_stack(&const_expr_ctx, flag,
  699. VALUE_TYPE_I64,
  700. #if WASM_ENABLE_GC != 0
  701. NULL, 0,
  702. #endif
  703. &cur_value,
  704. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  705. NULL,
  706. #endif
  707. error_buf, error_buf_size))
  708. goto fail;
  709. break;
  710. /* f32.const */
  711. case INIT_EXPR_TYPE_F32_CONST:
  712. CHECK_BUF(p, p_end, 4);
  713. p_float = (uint8 *)&cur_value.f32;
  714. for (i = 0; i < sizeof(float32); i++)
  715. *p_float++ = *p++;
  716. if (!push_const_expr_stack(&const_expr_ctx, flag,
  717. VALUE_TYPE_F32,
  718. #if WASM_ENABLE_GC != 0
  719. NULL, 0,
  720. #endif
  721. &cur_value,
  722. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  723. NULL,
  724. #endif
  725. error_buf, error_buf_size))
  726. goto fail;
  727. break;
  728. /* f64.const */
  729. case INIT_EXPR_TYPE_F64_CONST:
  730. CHECK_BUF(p, p_end, 8);
  731. p_float = (uint8 *)&cur_value.f64;
  732. for (i = 0; i < sizeof(float64); i++)
  733. *p_float++ = *p++;
  734. if (!push_const_expr_stack(&const_expr_ctx, flag,
  735. VALUE_TYPE_F64,
  736. #if WASM_ENABLE_GC != 0
  737. NULL, 0,
  738. #endif
  739. &cur_value,
  740. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  741. NULL,
  742. #endif
  743. error_buf, error_buf_size))
  744. goto fail;
  745. break;
  746. #if WASM_ENABLE_SIMD != 0
  747. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  748. || (WASM_ENABLE_FAST_INTERP != 0)
  749. /* v128.const */
  750. case INIT_EXPR_TYPE_V128_CONST:
  751. {
  752. uint64 high, low;
  753. CHECK_BUF(p, p_end, 1);
  754. (void)read_uint8(p);
  755. CHECK_BUF(p, p_end, 16);
  756. wasm_runtime_read_v128(p, &high, &low);
  757. p += 16;
  758. cur_value.v128.i64x2[0] = high;
  759. cur_value.v128.i64x2[1] = low;
  760. if (!push_const_expr_stack(&const_expr_ctx, flag,
  761. VALUE_TYPE_V128,
  762. #if WASM_ENABLE_GC != 0
  763. NULL, 0,
  764. #endif
  765. &cur_value,
  766. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  767. NULL,
  768. #endif
  769. error_buf, error_buf_size))
  770. goto fail;
  771. #if WASM_ENABLE_WAMR_COMPILER != 0
  772. /* If any init_expr is v128.const, mark SIMD used */
  773. module->is_simd_used = true;
  774. #endif
  775. break;
  776. }
  777. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  778. (WASM_ENABLE_FAST_INTERP != 0) */
  779. #endif /* end of WASM_ENABLE_SIMD */
  780. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  781. case INIT_EXPR_TYPE_I32_ADD:
  782. case INIT_EXPR_TYPE_I32_SUB:
  783. case INIT_EXPR_TYPE_I32_MUL:
  784. case INIT_EXPR_TYPE_I64_ADD:
  785. case INIT_EXPR_TYPE_I64_SUB:
  786. case INIT_EXPR_TYPE_I64_MUL:
  787. {
  788. InitializerExpression *l_expr, *r_expr;
  789. WASMValue l_value, r_value;
  790. uint8 l_flag, r_flag;
  791. uint8 value_type;
  792. if (flag == INIT_EXPR_TYPE_I32_ADD
  793. || flag == INIT_EXPR_TYPE_I32_SUB
  794. || flag == INIT_EXPR_TYPE_I32_MUL) {
  795. value_type = VALUE_TYPE_I32;
  796. }
  797. else {
  798. value_type = VALUE_TYPE_I64;
  799. }
  800. /* If right flag indicates a binary operation, right expr will
  801. * be popped from stack. Otherwise, allocate a new expr for
  802. * right expr. Same for left expr.
  803. */
  804. if (!(pop_const_expr_stack(&const_expr_ctx, &r_flag, value_type,
  805. #if WASM_ENABLE_GC != 0
  806. NULL, NULL,
  807. #endif
  808. &r_value, &r_expr, error_buf,
  809. error_buf_size))) {
  810. goto fail;
  811. }
  812. if (!is_expr_binary_op(r_flag)) {
  813. if (!(r_expr = loader_malloc(sizeof(InitializerExpression),
  814. error_buf, error_buf_size))) {
  815. goto fail;
  816. }
  817. r_expr->init_expr_type = r_flag;
  818. r_expr->u.unary.v = r_value;
  819. }
  820. if (!(pop_const_expr_stack(&const_expr_ctx, &l_flag, value_type,
  821. #if WASM_ENABLE_GC != 0
  822. NULL, NULL,
  823. #endif
  824. &l_value, &l_expr, error_buf,
  825. error_buf_size))) {
  826. destroy_init_expr_recursive(r_expr);
  827. goto fail;
  828. }
  829. if (!is_expr_binary_op(l_flag)) {
  830. if (!(l_expr = loader_malloc(sizeof(InitializerExpression),
  831. error_buf, error_buf_size))) {
  832. destroy_init_expr_recursive(r_expr);
  833. goto fail;
  834. }
  835. l_expr->init_expr_type = l_flag;
  836. l_expr->u.unary.v = l_value;
  837. }
  838. if (!(cur_expr = loader_malloc(sizeof(InitializerExpression),
  839. error_buf, error_buf_size))) {
  840. destroy_init_expr_recursive(l_expr);
  841. destroy_init_expr_recursive(r_expr);
  842. goto fail;
  843. }
  844. cur_expr->init_expr_type = flag;
  845. cur_expr->u.binary.l_expr = l_expr;
  846. cur_expr->u.binary.r_expr = r_expr;
  847. if (!push_const_expr_stack(&const_expr_ctx, flag, value_type,
  848. #if WASM_ENABLE_GC != 0
  849. NULL, 0,
  850. #endif
  851. &cur_value, cur_expr, error_buf,
  852. error_buf_size)) {
  853. destroy_init_expr_recursive(cur_expr);
  854. goto fail;
  855. }
  856. break;
  857. }
  858. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR */
  859. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  860. /* ref.func */
  861. case INIT_EXPR_TYPE_FUNCREF_CONST:
  862. {
  863. uint32 func_idx;
  864. read_leb_uint32(p, p_end, func_idx);
  865. cur_value.ref_index = func_idx;
  866. if (!check_function_index(module, func_idx, error_buf,
  867. error_buf_size)) {
  868. goto fail;
  869. }
  870. #if WASM_ENABLE_GC == 0
  871. if (!push_const_expr_stack(&const_expr_ctx, flag,
  872. VALUE_TYPE_FUNCREF, &cur_value,
  873. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  874. NULL,
  875. #endif
  876. error_buf, error_buf_size))
  877. goto fail;
  878. #else
  879. if (func_idx < module->import_function_count) {
  880. type_idx =
  881. module->import_functions[func_idx].u.function.type_idx;
  882. }
  883. else {
  884. type_idx = module
  885. ->functions[func_idx
  886. - module->import_function_count]
  887. ->type_idx;
  888. }
  889. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  890. false, type_idx);
  891. if (!push_const_expr_stack(&const_expr_ctx, flag,
  892. cur_ref_type.ref_type, &cur_ref_type,
  893. 0, &cur_value,
  894. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  895. NULL,
  896. #endif
  897. error_buf, error_buf_size))
  898. goto fail;
  899. #endif
  900. #if WASM_ENABLE_WAMR_COMPILER != 0
  901. module->is_ref_types_used = true;
  902. #endif
  903. break;
  904. }
  905. /* ref.null */
  906. case INIT_EXPR_TYPE_REFNULL_CONST:
  907. {
  908. #if WASM_ENABLE_GC == 0
  909. uint8 type1;
  910. CHECK_BUF(p, p_end, 1);
  911. type1 = read_uint8(p);
  912. cur_value.ref_index = NULL_REF;
  913. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  914. &cur_value,
  915. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  916. NULL,
  917. #endif
  918. error_buf, error_buf_size))
  919. goto fail;
  920. #else
  921. /*
  922. * According to the current GC SPEC rules, the heap_type must be
  923. * validated when ref.null is used. It can be an absheaptype,
  924. * or the type C.types[type_idx] must be defined in the context.
  925. */
  926. int32 heap_type;
  927. read_leb_int32(p, p_end, heap_type);
  928. cur_value.gc_obj = NULL_REF;
  929. /*
  930. * The current check of heap_type can deterministically infer
  931. * the result of the previous condition
  932. * `(!is_byte_a_type(type1) ||
  933. * wasm_is_type_multi_byte_type(type1))`. Therefore, the
  934. * original condition is redundant and has been removed.
  935. *
  936. * This logic is consistent with the implementation of the
  937. * `WASM_OP_REF_NULL` case in the `wasm_loader_prepare_bytecode`
  938. * function.
  939. */
  940. if (heap_type >= 0) {
  941. if (!check_type_index(module, module->type_count, heap_type,
  942. error_buf, error_buf_size)) {
  943. goto fail;
  944. }
  945. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  946. true, heap_type);
  947. if (!push_const_expr_stack(&const_expr_ctx, flag,
  948. cur_ref_type.ref_type,
  949. &cur_ref_type, 0, &cur_value,
  950. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  951. NULL,
  952. #endif
  953. error_buf, error_buf_size))
  954. goto fail;
  955. }
  956. else {
  957. if (!wasm_is_valid_heap_type(heap_type)) {
  958. set_error_buf_v(error_buf, error_buf_size,
  959. "unknown type %d", heap_type);
  960. goto fail;
  961. }
  962. cur_ref_type.ref_ht_common.ref_type =
  963. (uint8)((int32)0x80 + heap_type);
  964. if (!push_const_expr_stack(&const_expr_ctx, flag,
  965. cur_ref_type.ref_type, NULL, 0,
  966. &cur_value,
  967. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  968. NULL,
  969. #endif
  970. error_buf, error_buf_size))
  971. goto fail;
  972. }
  973. #endif
  974. #if WASM_ENABLE_WAMR_COMPILER != 0
  975. module->is_ref_types_used = true;
  976. #endif
  977. break;
  978. }
  979. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  980. /* get_global */
  981. case INIT_EXPR_TYPE_GET_GLOBAL:
  982. {
  983. uint32 global_idx;
  984. uint8 global_type;
  985. read_leb_uint32(p, p_end, cur_value.global_index);
  986. global_idx = cur_value.global_index;
  987. /*
  988. * Currently, constant expressions occurring as initializers
  989. * of globals are further constrained in that contained
  990. * global.get instructions are
  991. * only allowed to refer to imported globals.
  992. *
  993. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  994. */
  995. if (global_idx >= module->import_global_count
  996. /* make spec test happy */
  997. #if WASM_ENABLE_GC != 0
  998. + module->global_count
  999. #endif
  1000. ) {
  1001. set_error_buf_v(error_buf, error_buf_size,
  1002. "unknown global %u", global_idx);
  1003. goto fail;
  1004. }
  1005. if (
  1006. /* make spec test happy */
  1007. #if WASM_ENABLE_GC != 0
  1008. global_idx < module->import_global_count &&
  1009. #endif
  1010. module->import_globals[global_idx]
  1011. .u.global.type.is_mutable) {
  1012. set_error_buf_v(error_buf, error_buf_size,
  1013. "constant expression required");
  1014. goto fail;
  1015. }
  1016. if (global_idx < module->import_global_count) {
  1017. global_type = module->import_globals[global_idx]
  1018. .u.global.type.val_type;
  1019. #if WASM_ENABLE_GC != 0
  1020. if (wasm_is_type_multi_byte_type(global_type)) {
  1021. WASMRefType *global_ref_type =
  1022. module->import_globals[global_idx]
  1023. .u.global.ref_type;
  1024. bh_memcpy_s(&cur_ref_type,
  1025. wasm_reftype_struct_size(global_ref_type),
  1026. global_ref_type,
  1027. wasm_reftype_struct_size(global_ref_type));
  1028. }
  1029. #endif
  1030. }
  1031. else {
  1032. global_type =
  1033. module
  1034. ->globals[global_idx - module->import_global_count]
  1035. .type.val_type;
  1036. #if WASM_ENABLE_GC != 0
  1037. if (wasm_is_type_multi_byte_type(global_type)) {
  1038. WASMRefType *global_ref_type =
  1039. module
  1040. ->globals[global_idx
  1041. - module->import_global_count]
  1042. .ref_type;
  1043. bh_memcpy_s(&cur_ref_type,
  1044. wasm_reftype_struct_size(global_ref_type),
  1045. global_ref_type,
  1046. wasm_reftype_struct_size(global_ref_type));
  1047. }
  1048. #endif
  1049. }
  1050. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  1051. #if WASM_ENABLE_GC != 0
  1052. &cur_ref_type, 0,
  1053. #endif
  1054. &cur_value,
  1055. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1056. NULL,
  1057. #endif
  1058. error_buf, error_buf_size))
  1059. goto fail;
  1060. break;
  1061. }
  1062. #if WASM_ENABLE_GC != 0
  1063. /* struct.new and array.new */
  1064. case WASM_OP_GC_PREFIX:
  1065. {
  1066. read_leb_uint32(p, p_end, opcode1);
  1067. switch (opcode1) {
  1068. case WASM_OP_STRUCT_NEW:
  1069. {
  1070. WASMStructType *struct_type;
  1071. WASMStructNewInitValues *struct_init_values = NULL;
  1072. uint32 field_count;
  1073. read_leb_uint32(p, p_end, type_idx);
  1074. if (!check_type_index(module, module->type_count,
  1075. type_idx, error_buf,
  1076. error_buf_size)) {
  1077. goto fail;
  1078. }
  1079. struct_type = (WASMStructType *)module->types[type_idx];
  1080. if (struct_type->base_type.type_flag
  1081. != WASM_TYPE_STRUCT) {
  1082. set_error_buf(error_buf, error_buf_size,
  1083. "unknown struct type");
  1084. goto fail;
  1085. }
  1086. field_count = struct_type->field_count;
  1087. if (!(struct_init_values = loader_malloc(
  1088. offsetof(WASMStructNewInitValues, fields)
  1089. + (uint64)field_count * sizeof(WASMValue),
  1090. error_buf, error_buf_size))) {
  1091. goto fail;
  1092. }
  1093. struct_init_values->type_idx = type_idx;
  1094. struct_init_values->count = field_count;
  1095. for (i = field_count; i > 0; i--) {
  1096. WASMRefType *field_ref_type = NULL;
  1097. uint32 field_idx = i - 1;
  1098. uint8 field_type =
  1099. struct_type->fields[field_idx].field_type;
  1100. if (wasm_is_type_multi_byte_type(field_type)) {
  1101. field_ref_type = wasm_reftype_map_find(
  1102. struct_type->ref_type_maps,
  1103. struct_type->ref_type_map_count, field_idx);
  1104. }
  1105. if (is_packed_type(field_type)) {
  1106. field_type = VALUE_TYPE_I32;
  1107. }
  1108. if (!pop_const_expr_stack(
  1109. &const_expr_ctx, NULL, field_type,
  1110. field_ref_type, NULL,
  1111. &struct_init_values->fields[field_idx],
  1112. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1113. NULL,
  1114. #endif
  1115. error_buf, error_buf_size)) {
  1116. destroy_init_expr_data_recursive(
  1117. module, struct_init_values);
  1118. goto fail;
  1119. }
  1120. }
  1121. cur_value.data = struct_init_values;
  1122. wasm_set_refheaptype_typeidx(
  1123. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1124. if (!push_const_expr_stack(
  1125. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1126. &cur_ref_type, (uint8)opcode1, &cur_value,
  1127. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1128. NULL,
  1129. #endif
  1130. error_buf, error_buf_size)) {
  1131. destroy_init_expr_data_recursive(
  1132. module, struct_init_values);
  1133. goto fail;
  1134. }
  1135. break;
  1136. }
  1137. case WASM_OP_STRUCT_NEW_DEFAULT:
  1138. {
  1139. read_leb_uint32(p, p_end, cur_value.type_index);
  1140. type_idx = cur_value.type_index;
  1141. if (!check_type_index(module, module->type_count,
  1142. type_idx, error_buf,
  1143. error_buf_size)) {
  1144. goto fail;
  1145. }
  1146. if (module->types[type_idx]->type_flag
  1147. != WASM_TYPE_STRUCT) {
  1148. set_error_buf(error_buf, error_buf_size,
  1149. "unknown struct type");
  1150. goto fail;
  1151. }
  1152. cur_value.type_index = type_idx;
  1153. cur_value.data = NULL;
  1154. wasm_set_refheaptype_typeidx(
  1155. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1156. if (!push_const_expr_stack(
  1157. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1158. &cur_ref_type, (uint8)opcode1, &cur_value,
  1159. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1160. NULL,
  1161. #endif
  1162. error_buf, error_buf_size)) {
  1163. goto fail;
  1164. }
  1165. break;
  1166. }
  1167. case WASM_OP_ARRAY_NEW:
  1168. case WASM_OP_ARRAY_NEW_DEFAULT:
  1169. case WASM_OP_ARRAY_NEW_FIXED:
  1170. {
  1171. WASMArrayNewInitValues *array_init_values = NULL;
  1172. WASMArrayType *array_type = NULL;
  1173. WASMRefType *elem_ref_type = NULL;
  1174. uint64 total_size;
  1175. uint8 elem_type;
  1176. read_leb_uint32(p, p_end, cur_value.type_index);
  1177. type_idx = cur_value.type_index;
  1178. if (!check_type_index(module, module->type_count,
  1179. type_idx, error_buf,
  1180. error_buf_size)) {
  1181. goto fail;
  1182. }
  1183. array_type = (WASMArrayType *)module->types[type_idx];
  1184. if (array_type->base_type.type_flag
  1185. != WASM_TYPE_ARRAY) {
  1186. set_error_buf(error_buf, error_buf_size,
  1187. "unknown array type");
  1188. goto fail;
  1189. }
  1190. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  1191. elem_type = array_type->elem_type;
  1192. if (wasm_is_type_multi_byte_type(elem_type)) {
  1193. elem_ref_type = array_type->elem_ref_type;
  1194. }
  1195. if (is_packed_type(elem_type)) {
  1196. elem_type = VALUE_TYPE_I32;
  1197. }
  1198. if (opcode1 == WASM_OP_ARRAY_NEW) {
  1199. WASMValue len_val = { 0 };
  1200. uint64 size = 0;
  1201. if (!pop_const_expr_stack(
  1202. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  1203. NULL, NULL, &len_val,
  1204. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1205. NULL,
  1206. #endif
  1207. error_buf, error_buf_size)) {
  1208. goto fail;
  1209. }
  1210. size =
  1211. sizeof(WASMArrayNewInitValues)
  1212. + sizeof(WASMValue) * (uint64)len_val.i32;
  1213. if (!(array_init_values = loader_malloc(
  1214. size, error_buf, error_buf_size))) {
  1215. goto fail;
  1216. }
  1217. array_init_values->type_idx = type_idx;
  1218. array_init_values->length = len_val.i32;
  1219. if (!pop_const_expr_stack(
  1220. &const_expr_ctx, NULL, elem_type,
  1221. elem_ref_type, NULL,
  1222. &array_init_values->elem_data[0],
  1223. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1224. NULL,
  1225. #endif
  1226. error_buf, error_buf_size)) {
  1227. destroy_init_expr_data_recursive(
  1228. module, array_init_values);
  1229. goto fail;
  1230. }
  1231. cur_value.data = array_init_values;
  1232. }
  1233. else {
  1234. /* WASM_OP_ARRAY_NEW_FIXED */
  1235. uint32 len;
  1236. read_leb_uint32(p, p_end, len);
  1237. total_size =
  1238. (uint64)offsetof(WASMArrayNewInitValues,
  1239. elem_data)
  1240. + (uint64)sizeof(WASMValue) * len;
  1241. if (!(array_init_values =
  1242. loader_malloc(total_size, error_buf,
  1243. error_buf_size))) {
  1244. goto fail;
  1245. }
  1246. array_init_values->type_idx = type_idx;
  1247. array_init_values->length = len;
  1248. for (i = len; i > 0; i--) {
  1249. if (!pop_const_expr_stack(
  1250. &const_expr_ctx, NULL, elem_type,
  1251. elem_ref_type, NULL,
  1252. &array_init_values
  1253. ->elem_data[i - 1],
  1254. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1255. NULL,
  1256. #endif
  1257. error_buf, error_buf_size)) {
  1258. destroy_init_expr_data_recursive(
  1259. module, array_init_values);
  1260. goto fail;
  1261. }
  1262. }
  1263. cur_value.data = array_init_values;
  1264. }
  1265. }
  1266. else {
  1267. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1268. WASMValue len_val;
  1269. uint32 len;
  1270. /* POP(i32) */
  1271. if (!pop_const_expr_stack(
  1272. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1273. NULL, &len_val,
  1274. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1275. NULL,
  1276. #endif
  1277. error_buf, error_buf_size)) {
  1278. goto fail;
  1279. }
  1280. len = len_val.i32;
  1281. cur_value.array_new_default.type_index = type_idx;
  1282. cur_value.array_new_default.length = len;
  1283. }
  1284. wasm_set_refheaptype_typeidx(
  1285. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1286. if (!push_const_expr_stack(
  1287. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1288. &cur_ref_type, (uint8)opcode1, &cur_value,
  1289. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1290. NULL,
  1291. #endif
  1292. error_buf, error_buf_size)) {
  1293. if (array_init_values) {
  1294. destroy_init_expr_data_recursive(
  1295. module, array_init_values);
  1296. }
  1297. goto fail;
  1298. }
  1299. break;
  1300. }
  1301. case WASM_OP_ANY_CONVERT_EXTERN:
  1302. {
  1303. set_error_buf(error_buf, error_buf_size,
  1304. "unsupported constant expression of "
  1305. "extern.internalize");
  1306. goto fail;
  1307. }
  1308. case WASM_OP_EXTERN_CONVERT_ANY:
  1309. {
  1310. set_error_buf(error_buf, error_buf_size,
  1311. "unsupported constant expression of "
  1312. "extern.externalize");
  1313. goto fail;
  1314. }
  1315. case WASM_OP_REF_I31:
  1316. {
  1317. /* POP(i32) */
  1318. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1319. VALUE_TYPE_I32, NULL, NULL,
  1320. &cur_value,
  1321. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1322. NULL,
  1323. #endif
  1324. error_buf, error_buf_size)) {
  1325. goto fail;
  1326. }
  1327. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1328. false, HEAP_TYPE_I31);
  1329. if (!push_const_expr_stack(
  1330. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1331. &cur_ref_type, (uint8)opcode1, &cur_value,
  1332. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1333. NULL,
  1334. #endif
  1335. error_buf, error_buf_size)) {
  1336. goto fail;
  1337. }
  1338. break;
  1339. }
  1340. default:
  1341. set_error_buf(
  1342. error_buf, error_buf_size,
  1343. "type mismatch or constant expression required");
  1344. goto fail;
  1345. }
  1346. break;
  1347. }
  1348. #endif /* end of WASM_ENABLE_GC != 0 */
  1349. default:
  1350. {
  1351. set_error_buf(error_buf, error_buf_size,
  1352. "illegal opcode "
  1353. "or constant expression required "
  1354. "or type mismatch");
  1355. goto fail;
  1356. }
  1357. }
  1358. CHECK_BUF(p, p_end, 1);
  1359. flag = read_uint8(p);
  1360. }
  1361. /* There should be only one value left on the init value stack */
  1362. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1363. #if WASM_ENABLE_GC != 0
  1364. ref_type, &opcode,
  1365. #endif
  1366. &cur_value,
  1367. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1368. &cur_expr,
  1369. #endif
  1370. error_buf, error_buf_size)) {
  1371. goto fail;
  1372. }
  1373. if (const_expr_ctx.sp != 0) {
  1374. set_error_buf(error_buf, error_buf_size,
  1375. "type mismatch: illegal constant opcode sequence");
  1376. goto fail;
  1377. }
  1378. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  1379. if (cur_expr != NULL) {
  1380. bh_memcpy_s(init_expr, sizeof(InitializerExpression), cur_expr,
  1381. sizeof(InitializerExpression));
  1382. wasm_runtime_free(cur_expr);
  1383. }
  1384. else {
  1385. init_expr->init_expr_type = flag;
  1386. init_expr->u.unary.v = cur_value;
  1387. }
  1388. #else
  1389. init_expr->init_expr_type = flag;
  1390. init_expr->u.unary.v = cur_value;
  1391. #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
  1392. #if WASM_ENABLE_GC != 0
  1393. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1394. switch (opcode) {
  1395. case WASM_OP_STRUCT_NEW:
  1396. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1397. break;
  1398. case WASM_OP_STRUCT_NEW_DEFAULT:
  1399. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1400. break;
  1401. case WASM_OP_ARRAY_NEW:
  1402. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1403. break;
  1404. case WASM_OP_ARRAY_NEW_DEFAULT:
  1405. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1406. break;
  1407. case WASM_OP_ARRAY_NEW_FIXED:
  1408. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1409. break;
  1410. case WASM_OP_REF_I31:
  1411. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1412. break;
  1413. default:
  1414. bh_assert(0);
  1415. break;
  1416. }
  1417. }
  1418. #endif /* end of WASM_ENABLE_GC != 0 */
  1419. *p_buf = p;
  1420. destroy_const_expr_stack(&const_expr_ctx, false);
  1421. return true;
  1422. fail:
  1423. destroy_const_expr_stack(&const_expr_ctx, true);
  1424. return false;
  1425. }
  1426. static bool
  1427. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1428. {
  1429. if (mutable >= 2) {
  1430. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1431. return false;
  1432. }
  1433. return true;
  1434. }
  1435. #if WASM_ENABLE_GC != 0
  1436. static void
  1437. destroy_func_type(WASMFuncType *type)
  1438. {
  1439. /* Destroy the reference type hash set */
  1440. if (type->ref_type_maps)
  1441. wasm_runtime_free(type->ref_type_maps);
  1442. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1443. && WASM_ENABLE_LAZY_JIT != 0
  1444. if (type->call_to_llvm_jit_from_fast_jit)
  1445. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1446. #endif
  1447. /* Free the type */
  1448. wasm_runtime_free(type);
  1449. }
  1450. static void
  1451. destroy_struct_type(WASMStructType *type)
  1452. {
  1453. if (type->ref_type_maps)
  1454. wasm_runtime_free(type->ref_type_maps);
  1455. wasm_runtime_free(type);
  1456. }
  1457. static void
  1458. destroy_array_type(WASMArrayType *type)
  1459. {
  1460. wasm_runtime_free(type);
  1461. }
  1462. static void
  1463. destroy_wasm_type(WASMType *type)
  1464. {
  1465. if (type->ref_count > 1) {
  1466. /* The type is referenced by other types
  1467. of current wasm module */
  1468. type->ref_count--;
  1469. return;
  1470. }
  1471. if (type->type_flag == WASM_TYPE_FUNC)
  1472. destroy_func_type((WASMFuncType *)type);
  1473. else if (type->type_flag == WASM_TYPE_STRUCT)
  1474. destroy_struct_type((WASMStructType *)type);
  1475. else if (type->type_flag == WASM_TYPE_ARRAY)
  1476. destroy_array_type((WASMArrayType *)type);
  1477. else {
  1478. bh_assert(0);
  1479. }
  1480. }
  1481. /* Resolve (ref null ht) or (ref ht) */
  1482. static bool
  1483. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1484. WASMModule *module, uint32 type_count, bool nullable,
  1485. WASMRefType *ref_type, char *error_buf,
  1486. uint32 error_buf_size)
  1487. {
  1488. const uint8 *p = *p_buf, *p_end = buf_end;
  1489. ref_type->ref_type =
  1490. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1491. ref_type->ref_ht_common.nullable = nullable;
  1492. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1493. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1494. /* heap type is (type i), i : typeidx, >= 0 */
  1495. if (!check_type_index(module, type_count,
  1496. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1497. error_buf_size)) {
  1498. return false;
  1499. }
  1500. }
  1501. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1502. /* heap type is func, extern, any, eq, i31 or data */
  1503. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1504. return false;
  1505. }
  1506. *p_buf = p;
  1507. return true;
  1508. fail:
  1509. return false;
  1510. }
  1511. static bool
  1512. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1513. WASMModule *module, uint32 type_count,
  1514. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1515. bool allow_packed_type, char *error_buf,
  1516. uint32 error_buf_size)
  1517. {
  1518. const uint8 *p = *p_buf, *p_end = buf_end;
  1519. uint8 type;
  1520. memset(ref_type, 0, sizeof(WASMRefType));
  1521. CHECK_BUF(p, p_end, 1);
  1522. type = read_uint8(p);
  1523. if (wasm_is_reftype_htref_nullable(type)) {
  1524. /* (ref null ht) */
  1525. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1526. ref_type, error_buf, error_buf_size))
  1527. return false;
  1528. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1529. *p_need_ref_type_map = true;
  1530. else {
  1531. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1532. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1533. multi-byte type to one-byte type to reduce the footprint and
  1534. the complexity of type equal/subtype checking */
  1535. ref_type->ref_type =
  1536. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1537. *p_need_ref_type_map = false;
  1538. }
  1539. }
  1540. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1541. /* (ref ht) */
  1542. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1543. ref_type, error_buf, error_buf_size))
  1544. return false;
  1545. *p_need_ref_type_map = true;
  1546. #if WASM_ENABLE_STRINGREF != 0
  1547. /* covert (ref string) to stringref */
  1548. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1549. ref_type->ref_type =
  1550. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1551. *p_need_ref_type_map = false;
  1552. }
  1553. #endif
  1554. }
  1555. else {
  1556. /* type which can be represented by one byte */
  1557. if (!is_valid_value_type_for_interpreter(type)
  1558. && !(allow_packed_type && is_packed_type(type))) {
  1559. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1560. return false;
  1561. }
  1562. ref_type->ref_type = type;
  1563. *p_need_ref_type_map = false;
  1564. #if WASM_ENABLE_WAMR_COMPILER != 0
  1565. /* If any value's type is v128, mark the module as SIMD used */
  1566. if (type == VALUE_TYPE_V128)
  1567. module->is_simd_used = true;
  1568. #endif
  1569. }
  1570. *p_buf = p;
  1571. return true;
  1572. fail:
  1573. return false;
  1574. }
  1575. static WASMRefType *
  1576. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1577. char *error_buf, uint32 error_buf_size)
  1578. {
  1579. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1580. if (!ret) {
  1581. set_error_buf(error_buf, error_buf_size,
  1582. "insert ref type to hash set failed");
  1583. }
  1584. return ret;
  1585. }
  1586. static bool
  1587. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1588. uint32 type_count, uint32 type_idx, char *error_buf,
  1589. uint32 error_buf_size)
  1590. {
  1591. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1592. uint32 param_count, result_count, i, j = 0;
  1593. uint32 param_cell_num, ret_cell_num;
  1594. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1595. uint64 total_size;
  1596. bool need_ref_type_map;
  1597. WASMRefType ref_type;
  1598. WASMFuncType *type = NULL;
  1599. /* Parse first time to resolve param count, result count and
  1600. ref type map count */
  1601. read_leb_uint32(p, p_end, param_count);
  1602. p_org = p;
  1603. for (i = 0; i < param_count; i++) {
  1604. if (!resolve_value_type(&p, p_end, module, type_count,
  1605. &need_ref_type_map, &ref_type, false, error_buf,
  1606. error_buf_size)) {
  1607. return false;
  1608. }
  1609. if (need_ref_type_map)
  1610. ref_type_map_count++;
  1611. }
  1612. read_leb_uint32(p, p_end, result_count);
  1613. for (i = 0; i < result_count; i++) {
  1614. if (!resolve_value_type(&p, p_end, module, type_count,
  1615. &need_ref_type_map, &ref_type, false, error_buf,
  1616. error_buf_size)) {
  1617. return false;
  1618. }
  1619. if (need_ref_type_map) {
  1620. ref_type_map_count++;
  1621. result_ref_type_map_count++;
  1622. }
  1623. }
  1624. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1625. "ref type map count: %d",
  1626. type_idx, param_count, result_count, ref_type_map_count);
  1627. /* Parse second time to resolve param types, result types and
  1628. ref type map info */
  1629. p = p_org;
  1630. total_size = offsetof(WASMFuncType, types)
  1631. + sizeof(uint8) * (uint64)(param_count + result_count);
  1632. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1633. return false;
  1634. }
  1635. if (ref_type_map_count > 0) {
  1636. if (ref_type_map_count > UINT16_MAX) {
  1637. set_error_buf(error_buf, error_buf_size,
  1638. "ref type count too large");
  1639. return false;
  1640. }
  1641. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1642. if (!(type->ref_type_maps =
  1643. loader_malloc(total_size, error_buf, error_buf_size))) {
  1644. goto fail;
  1645. }
  1646. }
  1647. type->base_type.type_flag = WASM_TYPE_FUNC;
  1648. type->param_count = param_count;
  1649. type->result_count = result_count;
  1650. type->ref_type_map_count = ref_type_map_count;
  1651. if (ref_type_map_count > 0) {
  1652. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1653. - result_ref_type_map_count;
  1654. }
  1655. for (i = 0; i < param_count; i++) {
  1656. if (!resolve_value_type(&p, p_end, module, type_count,
  1657. &need_ref_type_map, &ref_type, false, error_buf,
  1658. error_buf_size)) {
  1659. goto fail;
  1660. }
  1661. type->types[i] = ref_type.ref_type;
  1662. if (need_ref_type_map) {
  1663. type->ref_type_maps[j].index = i;
  1664. if (!(type->ref_type_maps[j++].ref_type =
  1665. reftype_set_insert(module->ref_type_set, &ref_type,
  1666. error_buf, error_buf_size))) {
  1667. goto fail;
  1668. }
  1669. }
  1670. }
  1671. read_leb_uint32(p, p_end, result_count);
  1672. for (i = 0; i < result_count; i++) {
  1673. if (!resolve_value_type(&p, p_end, module, type_count,
  1674. &need_ref_type_map, &ref_type, false, error_buf,
  1675. error_buf_size)) {
  1676. goto fail;
  1677. }
  1678. type->types[param_count + i] = ref_type.ref_type;
  1679. if (need_ref_type_map) {
  1680. type->ref_type_maps[j].index = param_count + i;
  1681. if (!(type->ref_type_maps[j++].ref_type =
  1682. reftype_set_insert(module->ref_type_set, &ref_type,
  1683. error_buf, error_buf_size))) {
  1684. goto fail;
  1685. }
  1686. }
  1687. }
  1688. bh_assert(j == type->ref_type_map_count);
  1689. #if TRACE_WASM_LOADER != 0
  1690. os_printf("type %d = ", type_idx);
  1691. wasm_dump_func_type(type);
  1692. #endif
  1693. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1694. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1695. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1696. set_error_buf(error_buf, error_buf_size,
  1697. "param count or result count too large");
  1698. goto fail;
  1699. }
  1700. type->param_cell_num = (uint16)param_cell_num;
  1701. type->ret_cell_num = (uint16)ret_cell_num;
  1702. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1703. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1704. #endif
  1705. #if WASM_ENABLE_WAMR_COMPILER != 0
  1706. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1707. if (type->types[i] == VALUE_TYPE_V128)
  1708. module->is_simd_used = true;
  1709. }
  1710. #endif
  1711. *p_buf = p;
  1712. module->types[type_idx] = (WASMType *)type;
  1713. return true;
  1714. fail:
  1715. if (type)
  1716. destroy_func_type(type);
  1717. return false;
  1718. }
  1719. static bool
  1720. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1721. WASMModule *module, uint32 type_count, uint32 type_idx,
  1722. char *error_buf, uint32 error_buf_size)
  1723. {
  1724. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1725. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1726. uint32 i, j = 0, offset;
  1727. uint16 *reference_table;
  1728. uint64 total_size;
  1729. uint8 mutable;
  1730. bool need_ref_type_map;
  1731. WASMRefType ref_type;
  1732. WASMStructType *type = NULL;
  1733. /* Parse first time to resolve field count and ref type map count */
  1734. read_leb_uint32(p, p_end, field_count);
  1735. p_org = p;
  1736. for (i = 0; i < field_count; i++) {
  1737. if (!resolve_value_type(&p, p_end, module, type_count,
  1738. &need_ref_type_map, &ref_type, true, error_buf,
  1739. error_buf_size)) {
  1740. return false;
  1741. }
  1742. if (need_ref_type_map)
  1743. ref_type_map_count++;
  1744. if (wasm_is_reftype_anyref(ref_type.ref_type)) {
  1745. LOG_ERROR("Not support using anyref in struct fields");
  1746. return false;
  1747. }
  1748. if (wasm_is_type_reftype(ref_type.ref_type))
  1749. ref_field_count++;
  1750. CHECK_BUF(p, p_end, 1);
  1751. mutable = read_uint8(p);
  1752. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1753. return false;
  1754. }
  1755. }
  1756. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1757. type_idx, field_count, ref_type_map_count);
  1758. /* Parse second time to resolve field types and ref type map info */
  1759. p = p_org;
  1760. total_size = offsetof(WASMStructType, fields)
  1761. + sizeof(WASMStructFieldType) * (uint64)field_count
  1762. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1763. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1764. return false;
  1765. }
  1766. if (ref_type_map_count > 0) {
  1767. if (ref_type_map_count > UINT16_MAX) {
  1768. set_error_buf(error_buf, error_buf_size,
  1769. "ref type count too large");
  1770. return false;
  1771. }
  1772. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1773. if (!(type->ref_type_maps =
  1774. loader_malloc(total_size, error_buf, error_buf_size))) {
  1775. goto fail;
  1776. }
  1777. }
  1778. type->reference_table = reference_table =
  1779. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1780. + sizeof(WASMStructFieldType) * field_count);
  1781. *reference_table++ = ref_field_count;
  1782. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1783. type->field_count = field_count;
  1784. type->ref_type_map_count = ref_type_map_count;
  1785. offset = (uint32)offsetof(WASMStructObject, field_data);
  1786. for (i = 0; i < field_count; i++) {
  1787. if (!resolve_value_type(&p, p_end, module, type_count,
  1788. &need_ref_type_map, &ref_type, true, error_buf,
  1789. error_buf_size)) {
  1790. goto fail;
  1791. }
  1792. if (!is_valid_field_type(ref_type.ref_type)) {
  1793. set_error_buf(error_buf, error_buf_size, "invalid field type");
  1794. goto fail;
  1795. }
  1796. type->fields[i].field_type = ref_type.ref_type;
  1797. if (need_ref_type_map) {
  1798. type->ref_type_maps[j].index = i;
  1799. if (!(type->ref_type_maps[j++].ref_type =
  1800. reftype_set_insert(module->ref_type_set, &ref_type,
  1801. error_buf, error_buf_size))) {
  1802. goto fail;
  1803. }
  1804. }
  1805. CHECK_BUF(p, p_end, 1);
  1806. type->fields[i].field_flags = read_uint8(p);
  1807. type->fields[i].field_size =
  1808. (uint8)wasm_reftype_size(ref_type.ref_type);
  1809. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1810. || defined(BUILD_TARGET_X86_32))
  1811. if (type->fields[i].field_size == 2)
  1812. offset = align_uint(offset, 2);
  1813. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1814. offset = align_uint(offset, 4);
  1815. #endif
  1816. type->fields[i].field_offset = offset;
  1817. if (wasm_is_type_reftype(ref_type.ref_type))
  1818. *reference_table++ = offset;
  1819. offset += type->fields[i].field_size;
  1820. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1821. type->fields[i].field_flags, type->fields[i].field_type);
  1822. }
  1823. type->total_size = offset;
  1824. bh_assert(j == type->ref_type_map_count);
  1825. #if TRACE_WASM_LOADER != 0
  1826. os_printf("type %d = ", type_idx);
  1827. wasm_dump_struct_type(type);
  1828. #endif
  1829. *p_buf = p;
  1830. module->types[type_idx] = (WASMType *)type;
  1831. return true;
  1832. fail:
  1833. if (type)
  1834. destroy_struct_type(type);
  1835. return false;
  1836. }
  1837. static bool
  1838. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1839. WASMModule *module, uint32 type_count, uint32 type_idx,
  1840. char *error_buf, uint32 error_buf_size)
  1841. {
  1842. const uint8 *p = *p_buf, *p_end = buf_end;
  1843. uint8 mutable;
  1844. bool need_ref_type_map;
  1845. WASMRefType ref_type;
  1846. WASMArrayType *type = NULL;
  1847. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1848. &ref_type, true, error_buf, error_buf_size)) {
  1849. return false;
  1850. }
  1851. if (wasm_is_reftype_anyref(ref_type.ref_type)) {
  1852. LOG_ERROR("Not support using anyref in array element type");
  1853. return false;
  1854. }
  1855. CHECK_BUF(p, p_end, 1);
  1856. mutable = read_uint8(p);
  1857. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1858. return false;
  1859. }
  1860. LOG_VERBOSE("type %u: array", type_idx);
  1861. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1862. error_buf_size))) {
  1863. return false;
  1864. }
  1865. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1866. type->elem_flags = mutable;
  1867. type->elem_type = ref_type.ref_type;
  1868. if (need_ref_type_map) {
  1869. if (!(type->elem_ref_type =
  1870. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1871. error_buf_size))) {
  1872. goto fail;
  1873. }
  1874. }
  1875. #if TRACE_WASM_LOADER != 0
  1876. os_printf("type %d = ", type_idx);
  1877. wasm_dump_array_type(type);
  1878. #endif
  1879. *p_buf = p;
  1880. module->types[type_idx] = (WASMType *)type;
  1881. return true;
  1882. fail:
  1883. if (type)
  1884. destroy_array_type(type);
  1885. return false;
  1886. }
  1887. static bool
  1888. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1889. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1890. {
  1891. if (heap_type >= 0) {
  1892. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1893. error_buf_size)) {
  1894. return false;
  1895. }
  1896. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1897. heap_type);
  1898. }
  1899. else {
  1900. if (!wasm_is_valid_heap_type(heap_type)) {
  1901. set_error_buf(error_buf, error_buf_size, "unknown type");
  1902. return false;
  1903. }
  1904. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1905. heap_type);
  1906. if (nullable) {
  1907. /* For (ref null func/extern/any/eq/i31/data),
  1908. they are same as
  1909. funcref/externref/anyref/eqref/i31ref/dataref,
  1910. we convert the multi-byte type to one-byte
  1911. type to reduce the footprint and the
  1912. complexity of type equal/subtype checking */
  1913. ref_type->ref_type =
  1914. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1915. }
  1916. }
  1917. return true;
  1918. }
  1919. static void
  1920. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1921. WASMRefType *ref_type2)
  1922. {
  1923. /**
  1924. * The difference rt1 ∖ rt2 between two reference types is defined as
  1925. * follows:
  1926. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1927. * (ref ht2) = (ref null?1 ht1)
  1928. */
  1929. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1930. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1931. ref_type1, wasm_reftype_struct_size(ref_type1));
  1932. }
  1933. else {
  1934. ref_type_diff->ref_type = ref_type1->ref_type;
  1935. }
  1936. if (ref_type2->ref_ht_common.nullable) {
  1937. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1938. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1939. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1940. (int32)ref_type_diff->ref_type - 0x80);
  1941. }
  1942. else {
  1943. ref_type_diff->ref_ht_typeidx.nullable = false;
  1944. }
  1945. }
  1946. }
  1947. #else /* else of WASM_ENABLE_GC != 0 */
  1948. static void
  1949. destroy_wasm_type(WASMType *type)
  1950. {
  1951. if (type->ref_count > 1) {
  1952. /* The type is referenced by other types
  1953. of current wasm module */
  1954. type->ref_count--;
  1955. return;
  1956. }
  1957. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1958. && WASM_ENABLE_LAZY_JIT != 0
  1959. if (type->call_to_llvm_jit_from_fast_jit)
  1960. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1961. #endif
  1962. wasm_runtime_free(type);
  1963. }
  1964. #endif /* end of WASM_ENABLE_GC != 0 */
  1965. static bool
  1966. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1967. char *error_buf, uint32 error_buf_size)
  1968. {
  1969. const uint8 *p = buf, *p_end = buf_end;
  1970. uint32 type_count, i;
  1971. uint64 total_size;
  1972. uint8 flag;
  1973. #if WASM_ENABLE_GC != 0
  1974. uint32 processed_type_count = 0;
  1975. #endif
  1976. read_leb_uint32(p, p_end, type_count);
  1977. if (type_count) {
  1978. module->type_count = type_count;
  1979. total_size = sizeof(WASMType *) * (uint64)type_count;
  1980. if (!(module->types =
  1981. loader_malloc(total_size, error_buf, error_buf_size))) {
  1982. return false;
  1983. }
  1984. #if WASM_ENABLE_GC == 0
  1985. for (i = 0; i < type_count; i++) {
  1986. WASMFuncType *type;
  1987. const uint8 *p_org;
  1988. uint32 param_count, result_count, j;
  1989. uint32 param_cell_num, ret_cell_num;
  1990. CHECK_BUF(p, p_end, 1);
  1991. flag = read_uint8(p);
  1992. if (flag != 0x60) {
  1993. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1994. return false;
  1995. }
  1996. read_leb_uint32(p, p_end, param_count);
  1997. /* Resolve param count and result count firstly */
  1998. p_org = p;
  1999. CHECK_BUF(p, p_end, param_count);
  2000. p += param_count;
  2001. read_leb_uint32(p, p_end, result_count);
  2002. CHECK_BUF(p, p_end, result_count);
  2003. p = p_org;
  2004. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  2005. set_error_buf(error_buf, error_buf_size,
  2006. "param count or result count too large");
  2007. return false;
  2008. }
  2009. total_size = offsetof(WASMFuncType, types)
  2010. + sizeof(uint8) * (uint64)(param_count + result_count);
  2011. if (!(type = module->types[i] =
  2012. loader_malloc(total_size, error_buf, error_buf_size))) {
  2013. return false;
  2014. }
  2015. /* Resolve param types and result types */
  2016. type->ref_count = 1;
  2017. type->param_count = (uint16)param_count;
  2018. type->result_count = (uint16)result_count;
  2019. for (j = 0; j < param_count; j++) {
  2020. CHECK_BUF(p, p_end, 1);
  2021. type->types[j] = read_uint8(p);
  2022. }
  2023. read_leb_uint32(p, p_end, result_count);
  2024. for (j = 0; j < result_count; j++) {
  2025. CHECK_BUF(p, p_end, 1);
  2026. type->types[param_count + j] = read_uint8(p);
  2027. }
  2028. for (j = 0; j < param_count + result_count; j++) {
  2029. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  2030. set_error_buf(error_buf, error_buf_size,
  2031. "unknown value type");
  2032. return false;
  2033. }
  2034. }
  2035. param_cell_num = wasm_get_cell_num(type->types, param_count);
  2036. ret_cell_num =
  2037. wasm_get_cell_num(type->types + param_count, result_count);
  2038. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  2039. set_error_buf(error_buf, error_buf_size,
  2040. "param count or result count too large");
  2041. return false;
  2042. }
  2043. type->param_cell_num = (uint16)param_cell_num;
  2044. type->ret_cell_num = (uint16)ret_cell_num;
  2045. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  2046. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  2047. #endif
  2048. #if WASM_ENABLE_WAMR_COMPILER != 0
  2049. for (j = 0; j < type->param_count + type->result_count; j++) {
  2050. if (type->types[j] == VALUE_TYPE_V128)
  2051. module->is_simd_used = true;
  2052. else if (type->types[j] == VALUE_TYPE_FUNCREF
  2053. || type->types[j] == VALUE_TYPE_EXTERNREF)
  2054. module->is_ref_types_used = true;
  2055. }
  2056. #endif
  2057. /* If there is already a same type created, use it instead */
  2058. for (j = 0; j < i; j++) {
  2059. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  2060. if (module->types[j]->ref_count == UINT16_MAX) {
  2061. set_error_buf(error_buf, error_buf_size,
  2062. "wasm type's ref count too large");
  2063. return false;
  2064. }
  2065. destroy_wasm_type(type);
  2066. module->types[i] = module->types[j];
  2067. module->types[j]->ref_count++;
  2068. break;
  2069. }
  2070. }
  2071. }
  2072. #else /* else of WASM_ENABLE_GC == 0 */
  2073. for (i = 0; i < type_count; i++) {
  2074. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  2075. uint32 rec_count = 1, j;
  2076. bool is_sub_final = true;
  2077. CHECK_BUF(p, p_end, 1);
  2078. flag = read_uint8(p);
  2079. if (flag == DEFINED_TYPE_REC) {
  2080. read_leb_uint32(p, p_end, rec_count);
  2081. if (rec_count > 1) {
  2082. uint64 new_total_size;
  2083. /* integer overflow */
  2084. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  2085. set_error_buf(error_buf, error_buf_size,
  2086. "recursive type count too large");
  2087. return false;
  2088. }
  2089. new_total_size =
  2090. sizeof(WASMFuncType *)
  2091. * (uint64)(module->type_count + rec_count - 1);
  2092. if (new_total_size > UINT32_MAX) {
  2093. set_error_buf(error_buf, error_buf_size,
  2094. "allocate memory failed");
  2095. return false;
  2096. }
  2097. MEM_REALLOC(module->types, (uint32)total_size,
  2098. (uint32)new_total_size);
  2099. module->type_count += rec_count - 1;
  2100. total_size = new_total_size;
  2101. }
  2102. if (rec_count < 1) {
  2103. LOG_VERBOSE("Processing 0-entry rec group");
  2104. }
  2105. else {
  2106. LOG_VERBOSE("Processing rec group [%d-%d]",
  2107. processed_type_count,
  2108. processed_type_count + rec_count - 1);
  2109. }
  2110. }
  2111. else {
  2112. p--;
  2113. }
  2114. for (j = 0; j < rec_count; j++) {
  2115. WASMType *cur_type = NULL;
  2116. CHECK_BUF(p, p_end, 1);
  2117. flag = read_uint8(p);
  2118. parent_type_idx = -1;
  2119. if (flag == DEFINED_TYPE_SUB
  2120. || flag == DEFINED_TYPE_SUB_FINAL) {
  2121. read_leb_uint32(p, p_end, super_type_count);
  2122. if (super_type_count > 1) {
  2123. set_error_buf(error_buf, error_buf_size,
  2124. "super type count too large");
  2125. return false;
  2126. }
  2127. if (super_type_count > 0) {
  2128. read_leb_uint32(p, p_end, parent_type_idx);
  2129. if (parent_type_idx >= processed_type_count + j) {
  2130. set_error_buf_v(error_buf, error_buf_size,
  2131. "unknown type %d", parent_type_idx);
  2132. return false;
  2133. }
  2134. if (module->types[parent_type_idx]->is_sub_final) {
  2135. set_error_buf(error_buf, error_buf_size,
  2136. "sub type can not inherit from "
  2137. "a final super type");
  2138. return false;
  2139. }
  2140. }
  2141. if (flag == DEFINED_TYPE_SUB)
  2142. is_sub_final = false;
  2143. CHECK_BUF(p, p_end, 1);
  2144. flag = read_uint8(p);
  2145. }
  2146. if (flag == DEFINED_TYPE_FUNC) {
  2147. if (!resolve_func_type(&p, buf_end, module,
  2148. processed_type_count + rec_count,
  2149. processed_type_count + j, error_buf,
  2150. error_buf_size)) {
  2151. return false;
  2152. }
  2153. }
  2154. else if (flag == DEFINED_TYPE_STRUCT) {
  2155. if (!resolve_struct_type(&p, buf_end, module,
  2156. processed_type_count + rec_count,
  2157. processed_type_count + j,
  2158. error_buf, error_buf_size)) {
  2159. return false;
  2160. }
  2161. }
  2162. else if (flag == DEFINED_TYPE_ARRAY) {
  2163. if (!resolve_array_type(&p, buf_end, module,
  2164. processed_type_count + rec_count,
  2165. processed_type_count + j, error_buf,
  2166. error_buf_size)) {
  2167. return false;
  2168. }
  2169. }
  2170. else {
  2171. set_error_buf(error_buf, error_buf_size,
  2172. "invalid type flag");
  2173. return false;
  2174. }
  2175. cur_type = module->types[processed_type_count + j];
  2176. cur_type->ref_count = 1;
  2177. cur_type->parent_type_idx = parent_type_idx;
  2178. cur_type->is_sub_final = is_sub_final;
  2179. cur_type->rec_count = rec_count;
  2180. cur_type->rec_idx = j;
  2181. cur_type->rec_begin_type_idx = processed_type_count;
  2182. }
  2183. /* resolve subtyping relationship in current rec group */
  2184. for (j = 0; j < rec_count; j++) {
  2185. WASMType *cur_type = module->types[processed_type_count + j];
  2186. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2187. WASMType *parent_type =
  2188. module->types[cur_type->parent_type_idx];
  2189. cur_type->parent_type = parent_type;
  2190. cur_type->root_type = parent_type->root_type;
  2191. if (parent_type->inherit_depth == UINT16_MAX) {
  2192. set_error_buf(error_buf, error_buf_size,
  2193. "parent type's inherit depth too large");
  2194. return false;
  2195. }
  2196. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  2197. }
  2198. else {
  2199. cur_type->parent_type = NULL;
  2200. cur_type->root_type = cur_type;
  2201. cur_type->inherit_depth = 0;
  2202. }
  2203. }
  2204. for (j = 0; j < rec_count; j++) {
  2205. WASMType *cur_type = module->types[processed_type_count + j];
  2206. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  2207. WASMType *parent_type =
  2208. module->types[cur_type->parent_type_idx];
  2209. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  2210. module->types,
  2211. module->type_count)) {
  2212. set_error_buf_v(error_buf, error_buf_size,
  2213. "sub type %u does not match super type",
  2214. processed_type_count + j);
  2215. return false;
  2216. }
  2217. }
  2218. }
  2219. /* If there is already an equivalence type or a group of equivalence
  2220. recursive types created, use it or them instead */
  2221. for (j = 0; j < processed_type_count;) {
  2222. WASMType *src_type = module->types[j];
  2223. WASMType *cur_type = module->types[processed_type_count];
  2224. uint32 k, src_rec_count;
  2225. src_rec_count = src_type->rec_count;
  2226. if (src_rec_count != rec_count) {
  2227. /* no type equivalence */
  2228. j += src_rec_count;
  2229. continue;
  2230. }
  2231. for (k = 0; k < rec_count; k++) {
  2232. src_type = module->types[j + k];
  2233. cur_type = module->types[processed_type_count + k];
  2234. if (!wasm_type_equal(src_type, cur_type, module->types,
  2235. module->type_count)) {
  2236. break;
  2237. }
  2238. }
  2239. if (k < rec_count) {
  2240. /* no type equivalence */
  2241. j += src_rec_count;
  2242. continue;
  2243. }
  2244. /* type equivalence */
  2245. for (k = 0; k < rec_count; k++) {
  2246. if (module->types[j + k]->ref_count == UINT16_MAX) {
  2247. set_error_buf(error_buf, error_buf_size,
  2248. "wasm type's ref count too large");
  2249. return false;
  2250. }
  2251. destroy_wasm_type(module->types[processed_type_count + k]);
  2252. module->types[processed_type_count + k] =
  2253. module->types[j + k];
  2254. module->types[j + k]->ref_count++;
  2255. }
  2256. break;
  2257. }
  2258. if (rec_count > 1) {
  2259. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  2260. processed_type_count,
  2261. processed_type_count + rec_count - 1);
  2262. }
  2263. processed_type_count += rec_count;
  2264. }
  2265. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  2266. * module->type_count,
  2267. error_buf, error_buf_size))) {
  2268. return false;
  2269. }
  2270. #endif /* end of WASM_ENABLE_GC == 0 */
  2271. }
  2272. for (i = 0; i < module->type_count; i++) {
  2273. if (module->types[i] == NULL) {
  2274. set_error_buf_v(error_buf, error_buf_size, "unknown type %d", i);
  2275. return false;
  2276. }
  2277. }
  2278. if (p != p_end) {
  2279. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  2280. return false;
  2281. }
  2282. LOG_VERBOSE("Load type section success.\n");
  2283. return true;
  2284. fail:
  2285. return false;
  2286. }
  2287. static void
  2288. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  2289. uint32 *max_size)
  2290. {
  2291. uint32 default_max_size;
  2292. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  2293. * ABI unchanged */
  2294. (void)is_table64;
  2295. if (UINT32_MAX / 2 > init_size)
  2296. default_max_size = init_size * 2;
  2297. else
  2298. default_max_size = UINT32_MAX;
  2299. if (default_max_size < WASM_TABLE_MAX_SIZE)
  2300. default_max_size = WASM_TABLE_MAX_SIZE;
  2301. if (max_size_flag) {
  2302. /* module defines the table limitation */
  2303. bh_assert(init_size <= *max_size);
  2304. if (init_size < *max_size) {
  2305. *max_size =
  2306. *max_size < default_max_size ? *max_size : default_max_size;
  2307. }
  2308. }
  2309. else {
  2310. /* partial defined table limitation, gives a default value */
  2311. *max_size = default_max_size;
  2312. }
  2313. }
  2314. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2315. /**
  2316. * Find export item of a module with export info:
  2317. * module name, field name and export kind
  2318. */
  2319. static WASMExport *
  2320. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2321. const char *field_name, uint8 export_kind,
  2322. char *error_buf, uint32 error_buf_size)
  2323. {
  2324. WASMExport *export =
  2325. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2326. export_kind, error_buf, error_buf_size);
  2327. return export;
  2328. }
  2329. #endif
  2330. #if WASM_ENABLE_MULTI_MODULE != 0
  2331. static WASMTable *
  2332. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2333. uint32 init_size, uint32 max_size, char *error_buf,
  2334. uint32 error_buf_size)
  2335. {
  2336. WASMModuleCommon *module_reg;
  2337. WASMTable *table = NULL;
  2338. WASMExport *export = NULL;
  2339. WASMModule *module = NULL;
  2340. module_reg = wasm_runtime_find_module_registered(module_name);
  2341. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2342. LOG_DEBUG("can not find a module named %s for table", module_name);
  2343. set_error_buf(error_buf, error_buf_size, "unknown import");
  2344. return NULL;
  2345. }
  2346. module = (WASMModule *)module_reg;
  2347. export =
  2348. wasm_loader_find_export(module, module_name, table_name,
  2349. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2350. if (!export) {
  2351. return NULL;
  2352. }
  2353. /* resolve table and check the init/max size */
  2354. if (export->index < module->import_table_count) {
  2355. table =
  2356. module->import_tables[export->index].u.table.import_table_linked;
  2357. }
  2358. else {
  2359. table = &(module->tables[export->index - module->import_table_count]);
  2360. }
  2361. if (table->table_type.init_size < init_size
  2362. || table->table_type.max_size > max_size) {
  2363. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2364. module_name, table_name, table->table_type.init_size,
  2365. table->table_type.max_size, init_size, max_size);
  2366. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2367. return NULL;
  2368. }
  2369. return table;
  2370. }
  2371. static WASMMemory *
  2372. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2373. uint32 init_page_count, uint32 max_page_count,
  2374. char *error_buf, uint32 error_buf_size)
  2375. {
  2376. WASMModuleCommon *module_reg;
  2377. WASMMemory *memory = NULL;
  2378. WASMExport *export = NULL;
  2379. WASMModule *module = NULL;
  2380. module_reg = wasm_runtime_find_module_registered(module_name);
  2381. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2382. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2383. set_error_buf(error_buf, error_buf_size, "unknown import");
  2384. return NULL;
  2385. }
  2386. module = (WASMModule *)module_reg;
  2387. export =
  2388. wasm_loader_find_export(module, module_name, memory_name,
  2389. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2390. if (!export) {
  2391. return NULL;
  2392. }
  2393. /* resolve memory and check the init/max page count */
  2394. if (export->index < module->import_memory_count) {
  2395. memory = module->import_memories[export->index]
  2396. .u.memory.import_memory_linked;
  2397. }
  2398. else {
  2399. memory =
  2400. &(module->memories[export->index - module->import_memory_count]);
  2401. }
  2402. if (memory->init_page_count < init_page_count
  2403. || memory->max_page_count > max_page_count) {
  2404. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2405. module_name, memory_name, memory->init_page_count,
  2406. memory->max_page_count, init_page_count, max_page_count);
  2407. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2408. return NULL;
  2409. }
  2410. return memory;
  2411. }
  2412. static WASMGlobal *
  2413. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2414. uint8 type, bool is_mutable, char *error_buf,
  2415. uint32 error_buf_size)
  2416. {
  2417. WASMModuleCommon *module_reg;
  2418. WASMGlobal *global = NULL;
  2419. WASMExport *export = NULL;
  2420. WASMModule *module = NULL;
  2421. module_reg = wasm_runtime_find_module_registered(module_name);
  2422. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2423. LOG_DEBUG("can not find a module named %s for global", module_name);
  2424. set_error_buf(error_buf, error_buf_size, "unknown import");
  2425. return NULL;
  2426. }
  2427. module = (WASMModule *)module_reg;
  2428. export =
  2429. wasm_loader_find_export(module, module_name, global_name,
  2430. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2431. if (!export) {
  2432. return NULL;
  2433. }
  2434. /* resolve and check the global */
  2435. if (export->index < module->import_global_count) {
  2436. global =
  2437. module->import_globals[export->index].u.global.import_global_linked;
  2438. }
  2439. else {
  2440. global =
  2441. &(module->globals[export->index - module->import_global_count]);
  2442. }
  2443. if (global->type.val_type != type
  2444. || global->type.is_mutable != is_mutable) {
  2445. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2446. module_name, global_name, global->type.val_type,
  2447. global->type.is_mutable, type, is_mutable);
  2448. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2449. return NULL;
  2450. }
  2451. return global;
  2452. }
  2453. #if WASM_ENABLE_TAGS != 0
  2454. static WASMTag *
  2455. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2456. const WASMType *expected_tag_type,
  2457. uint32 *linked_tag_index, char *error_buf,
  2458. uint32 error_buf_size)
  2459. {
  2460. WASMModuleCommon *module_reg;
  2461. WASMTag *tag = NULL;
  2462. WASMExport *export = NULL;
  2463. WASMModule *module = NULL;
  2464. module_reg = wasm_runtime_find_module_registered(module_name);
  2465. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2466. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2467. tag_name);
  2468. set_error_buf(error_buf, error_buf_size, "unknown import");
  2469. return NULL;
  2470. }
  2471. module = (WASMModule *)module_reg;
  2472. export =
  2473. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2474. error_buf, error_buf_size);
  2475. if (!export) {
  2476. return NULL;
  2477. }
  2478. /* resolve tag type and tag */
  2479. if (export->index < module->import_tag_count) {
  2480. /* importing an imported tag from the submodule */
  2481. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2482. }
  2483. else {
  2484. /* importing an section tag from the submodule */
  2485. tag = module->tags[export->index - module->import_tag_count];
  2486. }
  2487. /* check function type */
  2488. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2489. module->type_count)) {
  2490. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2491. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2492. return NULL;
  2493. }
  2494. if (linked_tag_index != NULL) {
  2495. *linked_tag_index = export->index;
  2496. }
  2497. return tag;
  2498. }
  2499. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2500. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2501. static bool
  2502. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2503. const WASMModule *parent_module,
  2504. const char *sub_module_name, const char *function_name,
  2505. WASMFunctionImport *function, bool no_resolve,
  2506. char *error_buf, uint32 error_buf_size)
  2507. {
  2508. const uint8 *p = *p_buf, *p_end = buf_end;
  2509. uint32 declare_type_index = 0;
  2510. read_leb_uint32(p, p_end, declare_type_index);
  2511. *p_buf = p;
  2512. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2513. error_buf_size)) {
  2514. return false;
  2515. }
  2516. #if WASM_ENABLE_GC != 0
  2517. function->type_idx = declare_type_index;
  2518. #endif
  2519. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2520. declare_type_index = wasm_get_smallest_type_idx(
  2521. parent_module->types, parent_module->type_count, declare_type_index);
  2522. #endif
  2523. function->func_type =
  2524. (WASMFuncType *)parent_module->types[declare_type_index];
  2525. function->module_name = (char *)sub_module_name;
  2526. function->field_name = (char *)function_name;
  2527. function->attachment = NULL;
  2528. function->signature = NULL;
  2529. function->call_conv_raw = false;
  2530. /* lookup registered native symbols first */
  2531. if (!no_resolve) {
  2532. wasm_resolve_import_func(parent_module, function);
  2533. }
  2534. return true;
  2535. fail:
  2536. return false;
  2537. }
  2538. static bool
  2539. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2540. uint32 error_buf_size)
  2541. {
  2542. if (max_size < init_size) {
  2543. set_error_buf(error_buf, error_buf_size,
  2544. "size minimum must not be greater than maximum");
  2545. return false;
  2546. }
  2547. return true;
  2548. }
  2549. static bool
  2550. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2551. WASMModule *parent_module, const char *sub_module_name,
  2552. const char *table_name, WASMTableImport *table,
  2553. char *error_buf, uint32 error_buf_size)
  2554. {
  2555. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2556. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2557. declare_max_size = 0;
  2558. #if WASM_ENABLE_MULTI_MODULE != 0
  2559. WASMModule *sub_module = NULL;
  2560. WASMTable *linked_table = NULL;
  2561. #endif
  2562. #if WASM_ENABLE_GC != 0
  2563. WASMRefType ref_type;
  2564. bool need_ref_type_map;
  2565. #endif
  2566. bool is_table64 = false;
  2567. #if WASM_ENABLE_GC == 0
  2568. CHECK_BUF(p, p_end, 1);
  2569. /* 0x70 or 0x6F */
  2570. declare_elem_type = read_uint8(p);
  2571. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2572. #if WASM_ENABLE_REF_TYPES != 0
  2573. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2574. #endif
  2575. ) {
  2576. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2577. return false;
  2578. }
  2579. #else /* else of WASM_ENABLE_GC == 0 */
  2580. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2581. &need_ref_type_map, &ref_type, false, error_buf,
  2582. error_buf_size)) {
  2583. return false;
  2584. }
  2585. if (!wasm_is_type_reftype(ref_type.ref_type)
  2586. || wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2587. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2588. return false;
  2589. }
  2590. declare_elem_type = ref_type.ref_type;
  2591. if (need_ref_type_map) {
  2592. if (!(table->table_type.elem_ref_type =
  2593. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2594. error_buf, error_buf_size))) {
  2595. return false;
  2596. }
  2597. }
  2598. #if TRACE_WASM_LOADER != 0
  2599. os_printf("import table type: ");
  2600. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2601. os_printf("\n");
  2602. #endif
  2603. #endif /* end of WASM_ENABLE_GC == 0 */
  2604. p_org = p;
  2605. read_leb_uint32(p, p_end, table_flag);
  2606. is_table64 = table_flag & TABLE64_FLAG;
  2607. if (p - p_org > 1) {
  2608. LOG_VERBOSE("integer representation too long(import table)");
  2609. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2610. return false;
  2611. }
  2612. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2613. return false;
  2614. }
  2615. read_leb_uint32(p, p_end, declare_init_size);
  2616. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2617. read_leb_uint32(p, p_end, declare_max_size);
  2618. if (!check_table_max_size(declare_init_size, declare_max_size,
  2619. error_buf, error_buf_size))
  2620. return false;
  2621. }
  2622. adjust_table_max_size(is_table64, declare_init_size,
  2623. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2624. *p_buf = p;
  2625. #if WASM_ENABLE_MULTI_MODULE != 0
  2626. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2627. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2628. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2629. error_buf_size);
  2630. if (sub_module) {
  2631. linked_table = wasm_loader_resolve_table(
  2632. sub_module_name, table_name, declare_init_size,
  2633. declare_max_size, error_buf, error_buf_size);
  2634. if (linked_table) {
  2635. /* reset with linked table limit */
  2636. declare_elem_type = linked_table->table_type.elem_type;
  2637. declare_init_size = linked_table->table_type.init_size;
  2638. declare_max_size = linked_table->table_type.max_size;
  2639. table_flag = linked_table->table_type.flags;
  2640. table->import_table_linked = linked_table;
  2641. table->import_module = sub_module;
  2642. }
  2643. }
  2644. }
  2645. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2646. /* (table (export "table") 10 20 funcref) */
  2647. /* (table (export "table64") 10 20 funcref) */
  2648. /* we need this section working in wamrc */
  2649. if (!strcmp("spectest", sub_module_name)) {
  2650. const uint32 spectest_table_init_size = 10;
  2651. const uint32 spectest_table_max_size = 20;
  2652. if (strcmp("table", table_name)
  2653. #if WASM_ENABLE_MEMORY64 != 0
  2654. && strcmp("table64", table_name)
  2655. #endif
  2656. ) {
  2657. set_error_buf(error_buf, error_buf_size,
  2658. "incompatible import type or unknown import");
  2659. return false;
  2660. }
  2661. if (declare_init_size > spectest_table_init_size
  2662. || declare_max_size < spectest_table_max_size) {
  2663. set_error_buf(error_buf, error_buf_size,
  2664. "incompatible import type");
  2665. return false;
  2666. }
  2667. declare_init_size = spectest_table_init_size;
  2668. declare_max_size = spectest_table_max_size;
  2669. }
  2670. /* now we believe all declaration are ok */
  2671. table->table_type.elem_type = declare_elem_type;
  2672. table->table_type.init_size = declare_init_size;
  2673. table->table_type.flags = table_flag;
  2674. table->table_type.max_size = declare_max_size;
  2675. #if WASM_ENABLE_WAMR_COMPILER != 0
  2676. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2677. parent_module->is_ref_types_used = true;
  2678. #endif
  2679. (void)parent_module;
  2680. return true;
  2681. fail:
  2682. return false;
  2683. }
  2684. static bool
  2685. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2686. uint32 error_buf_size)
  2687. {
  2688. uint32 default_max_size =
  2689. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2690. if (!is_memory64 && init_size > default_max_size) {
  2691. set_error_buf(error_buf, error_buf_size,
  2692. "memory size must be at most 65536 pages (4GiB)");
  2693. return false;
  2694. }
  2695. #if WASM_ENABLE_MEMORY64 != 0
  2696. else if (is_memory64 && init_size > default_max_size) {
  2697. set_error_buf(
  2698. error_buf, error_buf_size,
  2699. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2700. return false;
  2701. }
  2702. #endif
  2703. return true;
  2704. }
  2705. static bool
  2706. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2707. char *error_buf, uint32 error_buf_size)
  2708. {
  2709. uint32 default_max_size =
  2710. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2711. if (max_size < init_size) {
  2712. set_error_buf(error_buf, error_buf_size,
  2713. "size minimum must not be greater than maximum");
  2714. return false;
  2715. }
  2716. if (!is_memory64 && max_size > default_max_size) {
  2717. set_error_buf(error_buf, error_buf_size,
  2718. "memory size must be at most 65536 pages (4GiB)");
  2719. return false;
  2720. }
  2721. #if WASM_ENABLE_MEMORY64 != 0
  2722. else if (is_memory64 && max_size > default_max_size) {
  2723. set_error_buf(
  2724. error_buf, error_buf_size,
  2725. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2726. return false;
  2727. }
  2728. #endif
  2729. return true;
  2730. }
  2731. static bool
  2732. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2733. WASMModule *parent_module, const char *sub_module_name,
  2734. const char *memory_name, WASMMemoryImport *memory,
  2735. char *error_buf, uint32 error_buf_size)
  2736. {
  2737. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2738. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2739. uint32 pool_size = wasm_runtime_memory_pool_size();
  2740. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2741. / DEFAULT_NUM_BYTES_PER_PAGE;
  2742. #else
  2743. uint32 max_page_count;
  2744. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2745. uint32 mem_flag = 0;
  2746. bool is_memory64 = false;
  2747. uint32 declare_init_page_count = 0;
  2748. uint32 declare_max_page_count = 0;
  2749. #if WASM_ENABLE_MULTI_MODULE != 0
  2750. WASMModule *sub_module = NULL;
  2751. WASMMemory *linked_memory = NULL;
  2752. #endif
  2753. p_org = p;
  2754. read_leb_uint32(p, p_end, mem_flag);
  2755. is_memory64 = mem_flag & MEMORY64_FLAG;
  2756. if (p - p_org > 1) {
  2757. LOG_VERBOSE("integer representation too long(import memory)");
  2758. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2759. return false;
  2760. }
  2761. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2762. return false;
  2763. }
  2764. read_leb_uint32(p, p_end, declare_init_page_count);
  2765. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2766. error_buf_size)) {
  2767. return false;
  2768. }
  2769. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2770. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2771. #endif
  2772. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2773. read_leb_uint32(p, p_end, declare_max_page_count);
  2774. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2775. declare_max_page_count, error_buf,
  2776. error_buf_size)) {
  2777. return false;
  2778. }
  2779. if (declare_max_page_count > max_page_count) {
  2780. declare_max_page_count = max_page_count;
  2781. }
  2782. }
  2783. else {
  2784. /* Limit the maximum memory size to max_page_count */
  2785. declare_max_page_count = max_page_count;
  2786. }
  2787. #if WASM_ENABLE_MULTI_MODULE != 0
  2788. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2789. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2790. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2791. error_buf_size);
  2792. if (sub_module) {
  2793. linked_memory = wasm_loader_resolve_memory(
  2794. sub_module_name, memory_name, declare_init_page_count,
  2795. declare_max_page_count, error_buf, error_buf_size);
  2796. if (linked_memory) {
  2797. /**
  2798. * reset with linked memory limit
  2799. */
  2800. memory->import_module = sub_module;
  2801. memory->import_memory_linked = linked_memory;
  2802. declare_init_page_count = linked_memory->init_page_count;
  2803. declare_max_page_count = linked_memory->max_page_count;
  2804. }
  2805. }
  2806. }
  2807. #endif
  2808. /* (memory (export "memory") 1 2) */
  2809. if (!strcmp("spectest", sub_module_name)) {
  2810. uint32 spectest_memory_init_page = 1;
  2811. uint32 spectest_memory_max_page = 2;
  2812. if (strcmp("memory", memory_name)) {
  2813. set_error_buf(error_buf, error_buf_size,
  2814. "incompatible import type or unknown import");
  2815. return false;
  2816. }
  2817. if (declare_init_page_count > spectest_memory_init_page
  2818. || declare_max_page_count < spectest_memory_max_page) {
  2819. set_error_buf(error_buf, error_buf_size,
  2820. "incompatible import type");
  2821. return false;
  2822. }
  2823. declare_init_page_count = spectest_memory_init_page;
  2824. declare_max_page_count = spectest_memory_max_page;
  2825. }
  2826. #if WASM_ENABLE_WASI_TEST != 0
  2827. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2828. else if (!strcmp("foo", sub_module_name)) {
  2829. uint32 spectest_memory_init_page = 1;
  2830. uint32 spectest_memory_max_page = 1;
  2831. if (strcmp("bar", memory_name)) {
  2832. set_error_buf(error_buf, error_buf_size,
  2833. "incompatible import type or unknown import");
  2834. return false;
  2835. }
  2836. if (declare_init_page_count > spectest_memory_init_page
  2837. || declare_max_page_count < spectest_memory_max_page) {
  2838. set_error_buf(error_buf, error_buf_size,
  2839. "incompatible import type");
  2840. return false;
  2841. }
  2842. declare_init_page_count = spectest_memory_init_page;
  2843. declare_max_page_count = spectest_memory_max_page;
  2844. }
  2845. #endif
  2846. /* now we believe all declaration are ok */
  2847. memory->mem_type.flags = mem_flag;
  2848. memory->mem_type.init_page_count = declare_init_page_count;
  2849. memory->mem_type.max_page_count = declare_max_page_count;
  2850. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2851. *p_buf = p;
  2852. (void)parent_module;
  2853. return true;
  2854. fail:
  2855. return false;
  2856. }
  2857. #if WASM_ENABLE_TAGS != 0
  2858. static bool
  2859. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2860. const WASMModule *parent_module, /* this module ! */
  2861. const char *sub_module_name, const char *tag_name,
  2862. WASMTagImport *tag, /* structure to fill */
  2863. char *error_buf, uint32 error_buf_size)
  2864. {
  2865. /* attribute and type of the import statement */
  2866. uint8 declare_tag_attribute;
  2867. uint32 declare_type_index;
  2868. const uint8 *p = *p_buf, *p_end = buf_end;
  2869. #if WASM_ENABLE_MULTI_MODULE != 0
  2870. WASMModule *sub_module = NULL;
  2871. #endif
  2872. /* get the one byte attribute */
  2873. CHECK_BUF(p, p_end, 1);
  2874. declare_tag_attribute = read_uint8(p);
  2875. if (declare_tag_attribute != 0) {
  2876. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2877. goto fail;
  2878. }
  2879. /* get type */
  2880. read_leb_uint32(p, p_end, declare_type_index);
  2881. /* compare against module->types */
  2882. if (!check_function_type(parent_module, declare_type_index, error_buf,
  2883. error_buf_size)) {
  2884. goto fail;
  2885. }
  2886. WASMFuncType *declare_tag_type =
  2887. (WASMFuncType *)parent_module->types[declare_type_index];
  2888. /* check, that the type of the declared tag returns void */
  2889. if (declare_tag_type->result_count != 0) {
  2890. set_error_buf(error_buf, error_buf_size,
  2891. "tag type signature does not return void");
  2892. goto fail;
  2893. }
  2894. #if WASM_ENABLE_MULTI_MODULE != 0
  2895. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2896. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2897. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2898. error_buf_size);
  2899. if (sub_module) {
  2900. /* wasm_loader_resolve_tag checks, that the imported tag
  2901. * and the declared tag have the same type
  2902. */
  2903. uint32 linked_tag_index = 0;
  2904. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2905. sub_module_name, tag_name, declare_tag_type,
  2906. &linked_tag_index /* out */, error_buf, error_buf_size);
  2907. if (linked_tag) {
  2908. tag->import_module = sub_module;
  2909. tag->import_tag_linked = linked_tag;
  2910. tag->import_tag_index_linked = linked_tag_index;
  2911. }
  2912. }
  2913. }
  2914. #endif
  2915. /* store to module tag declarations */
  2916. tag->attribute = declare_tag_attribute;
  2917. tag->type = declare_type_index;
  2918. tag->module_name = (char *)sub_module_name;
  2919. tag->field_name = (char *)tag_name;
  2920. tag->tag_type = declare_tag_type;
  2921. *p_buf = p;
  2922. (void)parent_module;
  2923. LOG_VERBOSE("Load tag import success\n");
  2924. return true;
  2925. fail:
  2926. return false;
  2927. }
  2928. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2929. static bool
  2930. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2931. WASMModule *parent_module, char *sub_module_name,
  2932. char *global_name, WASMGlobalImport *global, char *error_buf,
  2933. uint32 error_buf_size)
  2934. {
  2935. const uint8 *p = *p_buf, *p_end = buf_end;
  2936. uint8 declare_type = 0;
  2937. uint8 declare_mutable = 0;
  2938. #if WASM_ENABLE_MULTI_MODULE != 0
  2939. WASMModule *sub_module = NULL;
  2940. WASMGlobal *linked_global = NULL;
  2941. #endif
  2942. #if WASM_ENABLE_GC != 0
  2943. WASMRefType ref_type;
  2944. bool need_ref_type_map;
  2945. #endif
  2946. bool ret = false;
  2947. #if WASM_ENABLE_GC == 0
  2948. CHECK_BUF(p, p_end, 2);
  2949. /* global type */
  2950. declare_type = read_uint8(p);
  2951. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2952. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2953. return false;
  2954. }
  2955. declare_mutable = read_uint8(p);
  2956. #else
  2957. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2958. &need_ref_type_map, &ref_type, false, error_buf,
  2959. error_buf_size)) {
  2960. return false;
  2961. }
  2962. declare_type = ref_type.ref_type;
  2963. if (need_ref_type_map) {
  2964. if (!(global->ref_type =
  2965. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2966. error_buf, error_buf_size))) {
  2967. return false;
  2968. }
  2969. }
  2970. #if TRACE_WASM_LOADER != 0
  2971. os_printf("import global type: ");
  2972. wasm_dump_value_type(declare_type, global->ref_type);
  2973. os_printf("\n");
  2974. #endif
  2975. CHECK_BUF(p, p_end, 1);
  2976. declare_mutable = read_uint8(p);
  2977. #endif /* end of WASM_ENABLE_GC == 0 */
  2978. *p_buf = p;
  2979. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2980. return false;
  2981. }
  2982. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2983. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2984. global);
  2985. if (ret) {
  2986. if (global->type.val_type != declare_type
  2987. || global->type.is_mutable != declare_mutable) {
  2988. set_error_buf(error_buf, error_buf_size,
  2989. "incompatible import type");
  2990. return false;
  2991. }
  2992. global->is_linked = true;
  2993. }
  2994. #endif
  2995. #if WASM_ENABLE_MULTI_MODULE != 0
  2996. if (!global->is_linked
  2997. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2998. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2999. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  3000. error_buf_size);
  3001. if (sub_module) {
  3002. /* check sub modules */
  3003. linked_global = wasm_loader_resolve_global(
  3004. sub_module_name, global_name, declare_type, declare_mutable,
  3005. error_buf, error_buf_size);
  3006. if (linked_global) {
  3007. global->import_module = sub_module;
  3008. global->import_global_linked = linked_global;
  3009. global->is_linked = true;
  3010. }
  3011. }
  3012. }
  3013. #endif
  3014. global->module_name = sub_module_name;
  3015. global->field_name = global_name;
  3016. global->type.val_type = declare_type;
  3017. global->type.is_mutable = (declare_mutable == 1);
  3018. #if WASM_ENABLE_WAMR_COMPILER != 0
  3019. if (global->type.val_type == VALUE_TYPE_V128)
  3020. parent_module->is_simd_used = true;
  3021. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  3022. parent_module->is_ref_types_used = true;
  3023. #endif
  3024. (void)parent_module;
  3025. (void)ret;
  3026. return true;
  3027. fail:
  3028. return false;
  3029. }
  3030. static bool
  3031. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3032. WASMTable *table, char *error_buf, uint32 error_buf_size)
  3033. {
  3034. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3035. #if WASM_ENABLE_GC != 0
  3036. WASMRefType ref_type;
  3037. bool need_ref_type_map;
  3038. #endif
  3039. bool is_table64 = false;
  3040. #if WASM_ENABLE_GC == 0
  3041. CHECK_BUF(p, p_end, 1);
  3042. /* 0x70 or 0x6F */
  3043. table->table_type.elem_type = read_uint8(p);
  3044. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  3045. #if WASM_ENABLE_REF_TYPES != 0
  3046. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  3047. #endif
  3048. ) {
  3049. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  3050. return false;
  3051. }
  3052. #else /* else of WASM_ENABLE_GC == 0 */
  3053. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3054. &need_ref_type_map, &ref_type, false, error_buf,
  3055. error_buf_size)) {
  3056. return false;
  3057. }
  3058. /*
  3059. * TODO: add this validator
  3060. * `wasm_is_reftype_htref_non_nullable(ref_type.ref_type)`
  3061. * after sync up with the latest GC spec
  3062. */
  3063. if (!wasm_is_type_reftype(ref_type.ref_type)) {
  3064. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3065. return false;
  3066. }
  3067. table->table_type.elem_type = ref_type.ref_type;
  3068. if (need_ref_type_map) {
  3069. if (!(table->table_type.elem_ref_type =
  3070. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  3071. error_buf_size))) {
  3072. return false;
  3073. }
  3074. }
  3075. #if TRACE_WASM_LOADER != 0
  3076. os_printf("table type: ");
  3077. wasm_dump_value_type(table->table_type.elem_type,
  3078. table->table_type.elem_ref_type);
  3079. os_printf("\n");
  3080. #endif
  3081. #endif /* end of WASM_ENABLE_GC == 0 */
  3082. p_org = p;
  3083. read_leb_uint32(p, p_end, table->table_type.flags);
  3084. is_table64 = table->table_type.flags & TABLE64_FLAG;
  3085. if (p - p_org > 1) {
  3086. LOG_VERBOSE("integer representation too long(table)");
  3087. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3088. return false;
  3089. }
  3090. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  3091. error_buf_size, false)) {
  3092. return false;
  3093. }
  3094. read_leb_uint32(p, p_end, table->table_type.init_size);
  3095. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  3096. read_leb_uint32(p, p_end, table->table_type.max_size);
  3097. if (!check_table_max_size(table->table_type.init_size,
  3098. table->table_type.max_size, error_buf,
  3099. error_buf_size))
  3100. return false;
  3101. }
  3102. adjust_table_max_size(is_table64, table->table_type.init_size,
  3103. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  3104. &table->table_type.max_size);
  3105. #if WASM_ENABLE_WAMR_COMPILER != 0
  3106. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3107. module->is_ref_types_used = true;
  3108. #endif
  3109. *p_buf = p;
  3110. return true;
  3111. fail:
  3112. return false;
  3113. }
  3114. static bool
  3115. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  3116. char *error_buf, uint32 error_buf_size)
  3117. {
  3118. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  3119. #if WASM_ENABLE_APP_FRAMEWORK != 0
  3120. uint32 pool_size = wasm_runtime_memory_pool_size();
  3121. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  3122. / DEFAULT_NUM_BYTES_PER_PAGE;
  3123. #else
  3124. uint32 max_page_count;
  3125. #endif
  3126. bool is_memory64 = false;
  3127. p_org = p;
  3128. read_leb_uint32(p, p_end, memory->flags);
  3129. is_memory64 = memory->flags & MEMORY64_FLAG;
  3130. if (p - p_org > 1) {
  3131. LOG_VERBOSE("integer representation too long(memory)");
  3132. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  3133. return false;
  3134. }
  3135. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  3136. false)) {
  3137. return false;
  3138. }
  3139. read_leb_uint32(p, p_end, memory->init_page_count);
  3140. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  3141. error_buf_size))
  3142. return false;
  3143. #if WASM_ENABLE_APP_FRAMEWORK == 0
  3144. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  3145. #endif
  3146. if (memory->flags & 1) {
  3147. read_leb_uint32(p, p_end, memory->max_page_count);
  3148. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  3149. memory->max_page_count, error_buf,
  3150. error_buf_size))
  3151. return false;
  3152. if (memory->max_page_count > max_page_count)
  3153. memory->max_page_count = max_page_count;
  3154. }
  3155. else {
  3156. /* Limit the maximum memory size to max_page_count */
  3157. memory->max_page_count = max_page_count;
  3158. }
  3159. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  3160. *p_buf = p;
  3161. return true;
  3162. fail:
  3163. return false;
  3164. }
  3165. static int
  3166. cmp_export_name(const void *a, const void *b)
  3167. {
  3168. return strcmp(*(char **)a, *(char **)b);
  3169. }
  3170. static bool
  3171. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3172. bool is_load_from_file_buf, bool no_resolve,
  3173. char *error_buf, uint32 error_buf_size)
  3174. {
  3175. const uint8 *p = buf, *p_end = buf_end, *p_old;
  3176. uint32 import_count, name_len, type_index, i, u32, flags;
  3177. uint64 total_size;
  3178. WASMImport *import;
  3179. WASMImport *import_functions = NULL, *import_tables = NULL;
  3180. WASMImport *import_memories = NULL, *import_globals = NULL;
  3181. #if WASM_ENABLE_TAGS != 0
  3182. WASMImport *import_tags = NULL;
  3183. #endif
  3184. char *sub_module_name, *field_name;
  3185. uint8 u8, kind, global_type;
  3186. read_leb_uint32(p, p_end, import_count);
  3187. if (import_count) {
  3188. module->import_count = import_count;
  3189. total_size = sizeof(WASMImport) * (uint64)import_count;
  3190. if (!(module->imports =
  3191. loader_malloc(total_size, error_buf, error_buf_size))) {
  3192. return false;
  3193. }
  3194. p_old = p;
  3195. /* Scan firstly to get import count of each type */
  3196. for (i = 0; i < import_count; i++) {
  3197. /* module name */
  3198. read_leb_uint32(p, p_end, name_len);
  3199. CHECK_BUF(p, p_end, name_len);
  3200. p += name_len;
  3201. /* field name */
  3202. read_leb_uint32(p, p_end, name_len);
  3203. CHECK_BUF(p, p_end, name_len);
  3204. p += name_len;
  3205. CHECK_BUF(p, p_end, 1);
  3206. /* 0x00/0x01/0x02/0x03/0x04 */
  3207. kind = read_uint8(p);
  3208. switch (kind) {
  3209. case IMPORT_KIND_FUNC: /* import function */
  3210. read_leb_uint32(p, p_end, type_index);
  3211. module->import_function_count++;
  3212. break;
  3213. case IMPORT_KIND_TABLE: /* import table */
  3214. CHECK_BUF(p, p_end, 1);
  3215. /* 0x70 */
  3216. u8 = read_uint8(p);
  3217. #if WASM_ENABLE_GC != 0
  3218. if (wasm_is_reftype_htref_nullable(u8)) {
  3219. int32 heap_type;
  3220. read_leb_int32(p, p_end, heap_type);
  3221. (void)heap_type;
  3222. }
  3223. #endif
  3224. read_leb_uint32(p, p_end, flags);
  3225. read_leb_uint32(p, p_end, u32);
  3226. if (flags & 1)
  3227. read_leb_uint32(p, p_end, u32);
  3228. module->import_table_count++;
  3229. if (module->import_table_count > 1) {
  3230. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3231. set_error_buf(error_buf, error_buf_size,
  3232. "multiple tables");
  3233. return false;
  3234. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3235. module->is_ref_types_used = true;
  3236. #endif
  3237. }
  3238. break;
  3239. case IMPORT_KIND_MEMORY: /* import memory */
  3240. read_leb_uint32(p, p_end, flags);
  3241. read_leb_uint32(p, p_end, u32);
  3242. if (flags & 1)
  3243. read_leb_uint32(p, p_end, u32);
  3244. module->import_memory_count++;
  3245. #if WASM_ENABLE_MULTI_MEMORY == 0
  3246. if (module->import_memory_count > 1) {
  3247. set_error_buf(error_buf, error_buf_size,
  3248. "multiple memories");
  3249. return false;
  3250. }
  3251. #endif
  3252. break;
  3253. #if WASM_ENABLE_TAGS != 0
  3254. case IMPORT_KIND_TAG: /* import tags */
  3255. /* it only counts the number of tags to import */
  3256. module->import_tag_count++;
  3257. CHECK_BUF(p, p_end, 1);
  3258. u8 = read_uint8(p);
  3259. read_leb_uint32(p, p_end, type_index);
  3260. break;
  3261. #endif
  3262. case IMPORT_KIND_GLOBAL: /* import global */
  3263. #if WASM_ENABLE_GC != 0
  3264. /* valtype */
  3265. CHECK_BUF(p, p_end, 1);
  3266. global_type = read_uint8(p);
  3267. if (wasm_is_reftype_htref_nullable(global_type)
  3268. || wasm_is_reftype_htref_non_nullable(global_type)) {
  3269. int32 heap_type;
  3270. read_leb_int32(p, p_end, heap_type);
  3271. (void)heap_type;
  3272. }
  3273. /* mutability */
  3274. CHECK_BUF(p, p_end, 1);
  3275. p += 1;
  3276. #else
  3277. CHECK_BUF(p, p_end, 2);
  3278. p += 2;
  3279. #endif
  3280. (void)global_type;
  3281. module->import_global_count++;
  3282. break;
  3283. default:
  3284. set_error_buf(error_buf, error_buf_size,
  3285. "invalid import kind");
  3286. return false;
  3287. }
  3288. }
  3289. if (module->import_function_count)
  3290. import_functions = module->import_functions = module->imports;
  3291. if (module->import_table_count)
  3292. import_tables = module->import_tables =
  3293. module->imports + module->import_function_count;
  3294. if (module->import_memory_count)
  3295. import_memories = module->import_memories =
  3296. module->imports + module->import_function_count
  3297. + module->import_table_count;
  3298. #if WASM_ENABLE_TAGS != 0
  3299. if (module->import_tag_count)
  3300. import_tags = module->import_tags =
  3301. module->imports + module->import_function_count
  3302. + module->import_table_count + module->import_memory_count;
  3303. if (module->import_global_count)
  3304. import_globals = module->import_globals =
  3305. module->imports + module->import_function_count
  3306. + module->import_table_count + module->import_memory_count
  3307. + module->import_tag_count;
  3308. #else
  3309. if (module->import_global_count)
  3310. import_globals = module->import_globals =
  3311. module->imports + module->import_function_count
  3312. + module->import_table_count + module->import_memory_count;
  3313. #endif
  3314. p = p_old;
  3315. /* Scan again to resolve the data */
  3316. for (i = 0; i < import_count; i++) {
  3317. /* load module name */
  3318. read_leb_uint32(p, p_end, name_len);
  3319. CHECK_BUF(p, p_end, name_len);
  3320. if (!(sub_module_name = wasm_const_str_list_insert(
  3321. p, name_len, module, is_load_from_file_buf, error_buf,
  3322. error_buf_size))) {
  3323. return false;
  3324. }
  3325. p += name_len;
  3326. /* load field name */
  3327. read_leb_uint32(p, p_end, name_len);
  3328. CHECK_BUF(p, p_end, name_len);
  3329. if (!(field_name = wasm_const_str_list_insert(
  3330. p, name_len, module, is_load_from_file_buf, error_buf,
  3331. error_buf_size))) {
  3332. return false;
  3333. }
  3334. p += name_len;
  3335. CHECK_BUF(p, p_end, 1);
  3336. /* 0x00/0x01/0x02/0x03/0x4 */
  3337. kind = read_uint8(p);
  3338. switch (kind) {
  3339. case IMPORT_KIND_FUNC: /* import function */
  3340. bh_assert(import_functions);
  3341. import = import_functions++;
  3342. if (!load_function_import(&p, p_end, module,
  3343. sub_module_name, field_name,
  3344. &import->u.function, no_resolve,
  3345. error_buf, error_buf_size)) {
  3346. return false;
  3347. }
  3348. break;
  3349. case IMPORT_KIND_TABLE: /* import table */
  3350. bh_assert(import_tables);
  3351. import = import_tables++;
  3352. if (!load_table_import(&p, p_end, module, sub_module_name,
  3353. field_name, &import->u.table,
  3354. error_buf, error_buf_size)) {
  3355. LOG_DEBUG("can not import such a table (%s,%s)",
  3356. sub_module_name, field_name);
  3357. return false;
  3358. }
  3359. break;
  3360. case IMPORT_KIND_MEMORY: /* import memory */
  3361. bh_assert(import_memories);
  3362. import = import_memories++;
  3363. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3364. field_name, &import->u.memory,
  3365. error_buf, error_buf_size)) {
  3366. return false;
  3367. }
  3368. break;
  3369. #if WASM_ENABLE_TAGS != 0
  3370. case IMPORT_KIND_TAG:
  3371. bh_assert(import_tags);
  3372. import = import_tags++;
  3373. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3374. field_name, &import->u.tag, error_buf,
  3375. error_buf_size)) {
  3376. return false;
  3377. }
  3378. break;
  3379. #endif
  3380. case IMPORT_KIND_GLOBAL: /* import global */
  3381. bh_assert(import_globals);
  3382. import = import_globals++;
  3383. if (!load_global_import(&p, p_end, module, sub_module_name,
  3384. field_name, &import->u.global,
  3385. error_buf, error_buf_size)) {
  3386. return false;
  3387. }
  3388. break;
  3389. default:
  3390. set_error_buf(error_buf, error_buf_size,
  3391. "invalid import kind");
  3392. return false;
  3393. }
  3394. import->kind = kind;
  3395. import->u.names.module_name = sub_module_name;
  3396. import->u.names.field_name = field_name;
  3397. }
  3398. #if WASM_ENABLE_LIBC_WASI != 0
  3399. import = module->import_functions;
  3400. for (i = 0; i < module->import_function_count; i++, import++) {
  3401. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3402. || !strcmp(import->u.names.module_name,
  3403. "wasi_snapshot_preview1")) {
  3404. module->import_wasi_api = true;
  3405. break;
  3406. }
  3407. }
  3408. #endif
  3409. }
  3410. if (p != p_end) {
  3411. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3412. return false;
  3413. }
  3414. LOG_VERBOSE("Load import section success.\n");
  3415. (void)u8;
  3416. (void)u32;
  3417. (void)type_index;
  3418. return true;
  3419. fail:
  3420. return false;
  3421. }
  3422. static bool
  3423. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3424. uint32 error_buf_size)
  3425. {
  3426. WASMFuncType *param_type = func->func_type;
  3427. uint32 param_count = param_type->param_count;
  3428. uint8 *param_types = param_type->types;
  3429. uint32 local_count = func->local_count;
  3430. uint8 *local_types = func->local_types;
  3431. uint32 i, local_offset = 0;
  3432. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3433. /*
  3434. * Only allocate memory when total_size is not 0,
  3435. * or the return value of malloc(0) might be NULL on some platforms,
  3436. * which causes wasm loader return false.
  3437. */
  3438. if (total_size > 0
  3439. && !(func->local_offsets =
  3440. loader_malloc(total_size, error_buf, error_buf_size))) {
  3441. return false;
  3442. }
  3443. for (i = 0; i < param_count; i++) {
  3444. func->local_offsets[i] = (uint16)local_offset;
  3445. local_offset += wasm_value_type_cell_num(param_types[i]);
  3446. }
  3447. for (i = 0; i < local_count; i++) {
  3448. func->local_offsets[param_count + i] = (uint16)local_offset;
  3449. local_offset += wasm_value_type_cell_num(local_types[i]);
  3450. }
  3451. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3452. return true;
  3453. }
  3454. static bool
  3455. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3456. const uint8 *buf_code, const uint8 *buf_code_end,
  3457. WASMModule *module, char *error_buf,
  3458. uint32 error_buf_size)
  3459. {
  3460. const uint8 *p = buf, *p_end = buf_end;
  3461. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3462. uint32 func_count;
  3463. uint64 total_size;
  3464. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3465. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3466. uint8 type;
  3467. WASMFunction *func;
  3468. #if WASM_ENABLE_GC != 0
  3469. bool need_ref_type_map;
  3470. WASMRefType ref_type;
  3471. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3472. #endif
  3473. read_leb_uint32(p, p_end, func_count);
  3474. if (buf_code)
  3475. read_leb_uint32(p_code, buf_code_end, code_count);
  3476. if (func_count != code_count) {
  3477. set_error_buf(error_buf, error_buf_size,
  3478. "function and code section have inconsistent lengths or "
  3479. "unexpected end");
  3480. return false;
  3481. }
  3482. if (is_indices_overflow(module->import_function_count, func_count,
  3483. error_buf, error_buf_size))
  3484. return false;
  3485. if (func_count) {
  3486. module->function_count = func_count;
  3487. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3488. if (!(module->functions =
  3489. loader_malloc(total_size, error_buf, error_buf_size))) {
  3490. return false;
  3491. }
  3492. for (i = 0; i < func_count; i++) {
  3493. /* Resolve function type */
  3494. read_leb_uint32(p, p_end, type_index);
  3495. if (!check_function_type(module, type_index, error_buf,
  3496. error_buf_size)) {
  3497. return false;
  3498. }
  3499. #if WASM_ENABLE_GC != 0
  3500. type_index_org = type_index;
  3501. #endif
  3502. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3503. && WASM_ENABLE_GC == 0
  3504. type_index = wasm_get_smallest_type_idx(
  3505. module->types, module->type_count, type_index);
  3506. #endif
  3507. read_leb_uint32(p_code, buf_code_end, code_size);
  3508. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3509. set_error_buf(error_buf, error_buf_size,
  3510. "invalid function code size");
  3511. return false;
  3512. }
  3513. /* Resolve local set count */
  3514. p_code_end = p_code + code_size;
  3515. #if WASM_ENABLE_BRANCH_HINTS != 0
  3516. uint8 *p_body_start = (uint8 *)p_code;
  3517. #endif
  3518. local_count = 0;
  3519. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3520. p_code_save = p_code;
  3521. #if WASM_ENABLE_GC != 0
  3522. ref_type_map_count = 0;
  3523. #endif
  3524. /* Calculate total local count */
  3525. for (j = 0; j < local_set_count; j++) {
  3526. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3527. if (sub_local_count > UINT32_MAX - local_count) {
  3528. set_error_buf(error_buf, error_buf_size, "too many locals");
  3529. return false;
  3530. }
  3531. #if WASM_ENABLE_GC == 0
  3532. CHECK_BUF(p_code, buf_code_end, 1);
  3533. /* 0x7F/0x7E/0x7D/0x7C */
  3534. type = read_uint8(p_code);
  3535. local_count += sub_local_count;
  3536. #if WASM_ENABLE_WAMR_COMPILER != 0
  3537. /* If any value's type is v128, mark the module as SIMD used */
  3538. if (type == VALUE_TYPE_V128)
  3539. module->is_simd_used = true;
  3540. #endif
  3541. #else
  3542. if (!resolve_value_type(&p_code, buf_code_end, module,
  3543. module->type_count, &need_ref_type_map,
  3544. &ref_type, false, error_buf,
  3545. error_buf_size)) {
  3546. return false;
  3547. }
  3548. local_count += sub_local_count;
  3549. if (need_ref_type_map)
  3550. ref_type_map_count += sub_local_count;
  3551. #endif
  3552. }
  3553. /* Code size in code entry can't be smaller than size of vec(locals)
  3554. * + expr(at least 1 for opcode end). And expressions are encoded by
  3555. * their instruction sequence terminated with an explicit 0x0B
  3556. * opcode for end. */
  3557. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3558. set_error_buf(
  3559. error_buf, error_buf_size,
  3560. "section size mismatch: function body END opcode expected");
  3561. return false;
  3562. }
  3563. /* Alloc memory, layout: function structure + local types */
  3564. code_size = (uint32)(p_code_end - p_code);
  3565. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3566. if (!(func = module->functions[i] =
  3567. loader_malloc(total_size, error_buf, error_buf_size))) {
  3568. return false;
  3569. }
  3570. #if WASM_ENABLE_GC != 0
  3571. if (ref_type_map_count > 0) {
  3572. if (ref_type_map_count > UINT16_MAX) {
  3573. set_error_buf(error_buf, error_buf_size,
  3574. "ref type count too large");
  3575. return false;
  3576. }
  3577. total_size =
  3578. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3579. if (!(func->local_ref_type_maps = loader_malloc(
  3580. total_size, error_buf, error_buf_size))) {
  3581. return false;
  3582. }
  3583. func->local_ref_type_map_count = ref_type_map_count;
  3584. }
  3585. #endif
  3586. /* Set function type, local count, code size and code body */
  3587. func->func_type = (WASMFuncType *)module->types[type_index];
  3588. func->local_count = local_count;
  3589. if (local_count > 0)
  3590. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3591. func->code_size = code_size;
  3592. #if WASM_ENABLE_BRANCH_HINTS != 0
  3593. func->code_body_begin = p_body_start;
  3594. #endif
  3595. /*
  3596. * we shall make a copy of code body [p_code, p_code + code_size]
  3597. * when we are worrying about inappropriate releasing behaviour.
  3598. * all code bodies are actually in a buffer which user allocates in
  3599. * their embedding environment and we don't have power over them.
  3600. * it will be like:
  3601. * code_body_cp = malloc(code_size);
  3602. * memcpy(code_body_cp, p_code, code_size);
  3603. * func->code = code_body_cp;
  3604. */
  3605. func->code = (uint8 *)p_code;
  3606. #if WASM_ENABLE_GC != 0
  3607. func->type_idx = type_index_org;
  3608. #endif
  3609. #if WASM_ENABLE_GC != 0
  3610. t = 0;
  3611. #endif
  3612. /* Load each local type */
  3613. p_code = p_code_save;
  3614. local_type_index = 0;
  3615. for (j = 0; j < local_set_count; j++) {
  3616. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3617. /* Note: sub_local_count is allowed to be 0 */
  3618. if (local_type_index > UINT32_MAX - sub_local_count
  3619. || local_type_index + sub_local_count > local_count) {
  3620. set_error_buf(error_buf, error_buf_size,
  3621. "invalid local count");
  3622. return false;
  3623. }
  3624. #if WASM_ENABLE_GC == 0
  3625. CHECK_BUF(p_code, buf_code_end, 1);
  3626. /* 0x7F/0x7E/0x7D/0x7C */
  3627. type = read_uint8(p_code);
  3628. if (!is_valid_value_type_for_interpreter(type)) {
  3629. if (type == VALUE_TYPE_V128)
  3630. set_error_buf(error_buf, error_buf_size,
  3631. "v128 value type requires simd feature");
  3632. else if (type == VALUE_TYPE_FUNCREF
  3633. || type == VALUE_TYPE_EXTERNREF)
  3634. set_error_buf(error_buf, error_buf_size,
  3635. "ref value type requires "
  3636. "reference types feature");
  3637. else
  3638. set_error_buf_v(error_buf, error_buf_size,
  3639. "invalid local type 0x%02X", type);
  3640. return false;
  3641. }
  3642. #else
  3643. if (!resolve_value_type(&p_code, buf_code_end, module,
  3644. module->type_count, &need_ref_type_map,
  3645. &ref_type, false, error_buf,
  3646. error_buf_size)) {
  3647. return false;
  3648. }
  3649. if (need_ref_type_map) {
  3650. WASMRefType *ref_type_tmp;
  3651. if (!(ref_type_tmp = reftype_set_insert(
  3652. module->ref_type_set, &ref_type, error_buf,
  3653. error_buf_size))) {
  3654. return false;
  3655. }
  3656. for (k = 0; k < sub_local_count; k++) {
  3657. func->local_ref_type_maps[t + k].ref_type =
  3658. ref_type_tmp;
  3659. func->local_ref_type_maps[t + k].index =
  3660. local_type_index + k;
  3661. }
  3662. t += sub_local_count;
  3663. }
  3664. type = ref_type.ref_type;
  3665. #endif
  3666. for (k = 0; k < sub_local_count; k++) {
  3667. func->local_types[local_type_index++] = type;
  3668. }
  3669. #if WASM_ENABLE_WAMR_COMPILER != 0
  3670. if (type == VALUE_TYPE_V128)
  3671. module->is_simd_used = true;
  3672. else if (type == VALUE_TYPE_FUNCREF
  3673. || type == VALUE_TYPE_EXTERNREF)
  3674. module->is_ref_types_used = true;
  3675. #endif
  3676. }
  3677. bh_assert(local_type_index == func->local_count);
  3678. #if WASM_ENABLE_GC != 0
  3679. bh_assert(t == func->local_ref_type_map_count);
  3680. #if TRACE_WASM_LOADER != 0
  3681. os_printf("func %u, local types: [", i);
  3682. k = 0;
  3683. for (j = 0; j < func->local_count; j++) {
  3684. WASMRefType *ref_type_tmp = NULL;
  3685. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3686. bh_assert(j == func->local_ref_type_maps[k].index);
  3687. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3688. }
  3689. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3690. if (j < func->local_count - 1)
  3691. os_printf(" ");
  3692. }
  3693. os_printf("]\n");
  3694. #endif
  3695. #endif
  3696. func->param_cell_num = func->func_type->param_cell_num;
  3697. func->ret_cell_num = func->func_type->ret_cell_num;
  3698. local_cell_num =
  3699. wasm_get_cell_num(func->local_types, func->local_count);
  3700. if (local_cell_num > UINT16_MAX) {
  3701. set_error_buf(error_buf, error_buf_size,
  3702. "local count too large");
  3703. return false;
  3704. }
  3705. func->local_cell_num = (uint16)local_cell_num;
  3706. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3707. return false;
  3708. p_code = p_code_end;
  3709. }
  3710. }
  3711. if (p != p_end) {
  3712. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3713. return false;
  3714. }
  3715. LOG_VERBOSE("Load function section success.\n");
  3716. return true;
  3717. fail:
  3718. return false;
  3719. }
  3720. static bool
  3721. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3722. char *error_buf, uint32 error_buf_size)
  3723. {
  3724. const uint8 *p = buf, *p_end = buf_end;
  3725. uint32 table_count, i;
  3726. uint64 total_size;
  3727. WASMTable *table;
  3728. read_leb_uint32(p, p_end, table_count);
  3729. if (module->import_table_count + table_count > 1) {
  3730. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3731. /* a total of one table is allowed */
  3732. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3733. return false;
  3734. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3735. module->is_ref_types_used = true;
  3736. #endif
  3737. }
  3738. if (table_count) {
  3739. module->table_count = table_count;
  3740. total_size = sizeof(WASMTable) * (uint64)table_count;
  3741. if (!(module->tables =
  3742. loader_malloc(total_size, error_buf, error_buf_size))) {
  3743. return false;
  3744. }
  3745. /* load each table */
  3746. table = module->tables;
  3747. for (i = 0; i < table_count; i++, table++) {
  3748. #if WASM_ENABLE_GC != 0
  3749. uint8 flag;
  3750. bool has_init = false;
  3751. CHECK_BUF(p, p_end, 1);
  3752. flag = read_uint8(p);
  3753. if (flag == TABLE_INIT_EXPR_FLAG) {
  3754. CHECK_BUF(p, p_end, 1);
  3755. flag = read_uint8(p);
  3756. if (flag != 0x00) {
  3757. set_error_buf(error_buf, error_buf_size,
  3758. "invalid leading bytes for table");
  3759. return false;
  3760. }
  3761. has_init = true;
  3762. }
  3763. else {
  3764. p--;
  3765. }
  3766. #endif /* end of WASM_ENABLE_GC != 0 */
  3767. if (!load_table(&p, p_end, module, table, error_buf,
  3768. error_buf_size))
  3769. return false;
  3770. #if WASM_ENABLE_GC != 0
  3771. if (has_init) {
  3772. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3773. table->table_type.elem_type,
  3774. table->table_type.elem_ref_type, error_buf,
  3775. error_buf_size))
  3776. return false;
  3777. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3778. && table->init_expr.init_expr_type
  3779. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3780. set_error_buf(
  3781. error_buf, error_buf_size,
  3782. "unsupported initializer expression for table");
  3783. return false;
  3784. }
  3785. }
  3786. else {
  3787. if (wasm_is_reftype_htref_non_nullable(
  3788. table->table_type.elem_type)) {
  3789. set_error_buf(
  3790. error_buf, error_buf_size,
  3791. "type mismatch: non-nullable table without init expr");
  3792. return false;
  3793. }
  3794. }
  3795. #endif /* end of WASM_ENABLE_GC != 0 */
  3796. #if WASM_ENABLE_WAMR_COMPILER != 0
  3797. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3798. module->is_ref_types_used = true;
  3799. #endif
  3800. }
  3801. }
  3802. if (p != p_end) {
  3803. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3804. return false;
  3805. }
  3806. LOG_VERBOSE("Load table section success.\n");
  3807. return true;
  3808. fail:
  3809. return false;
  3810. }
  3811. static bool
  3812. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3813. char *error_buf, uint32 error_buf_size)
  3814. {
  3815. const uint8 *p = buf, *p_end = buf_end;
  3816. uint32 memory_count, i;
  3817. uint64 total_size;
  3818. WASMMemory *memory;
  3819. read_leb_uint32(p, p_end, memory_count);
  3820. #if WASM_ENABLE_MULTI_MEMORY == 0
  3821. /* a total of one memory is allowed */
  3822. if (module->import_memory_count + memory_count > 1) {
  3823. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3824. return false;
  3825. }
  3826. #endif
  3827. if (memory_count) {
  3828. module->memory_count = memory_count;
  3829. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3830. if (!(module->memories =
  3831. loader_malloc(total_size, error_buf, error_buf_size))) {
  3832. return false;
  3833. }
  3834. /* load each memory */
  3835. memory = module->memories;
  3836. for (i = 0; i < memory_count; i++, memory++)
  3837. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3838. return false;
  3839. }
  3840. if (p != p_end) {
  3841. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3842. return false;
  3843. }
  3844. LOG_VERBOSE("Load memory section success.\n");
  3845. return true;
  3846. fail:
  3847. return false;
  3848. }
  3849. static bool
  3850. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3851. char *error_buf, uint32 error_buf_size)
  3852. {
  3853. const uint8 *p = buf, *p_end = buf_end;
  3854. uint32 global_count, i;
  3855. uint64 total_size;
  3856. WASMGlobal *global;
  3857. uint8 mutable;
  3858. #if WASM_ENABLE_GC != 0
  3859. bool need_ref_type_map;
  3860. WASMRefType ref_type;
  3861. #endif
  3862. read_leb_uint32(p, p_end, global_count);
  3863. if (is_indices_overflow(module->import_global_count, global_count,
  3864. error_buf, error_buf_size))
  3865. return false;
  3866. module->global_count = 0;
  3867. if (global_count) {
  3868. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3869. if (!(module->globals =
  3870. loader_malloc(total_size, error_buf, error_buf_size))) {
  3871. return false;
  3872. }
  3873. global = module->globals;
  3874. for (i = 0; i < global_count; i++, global++) {
  3875. #if WASM_ENABLE_GC == 0
  3876. CHECK_BUF(p, p_end, 2);
  3877. /* global type */
  3878. global->type.val_type = read_uint8(p);
  3879. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3880. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3881. return false;
  3882. }
  3883. mutable = read_uint8(p);
  3884. #else
  3885. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3886. &need_ref_type_map, &ref_type, false,
  3887. error_buf, error_buf_size)) {
  3888. return false;
  3889. }
  3890. global->type.val_type = ref_type.ref_type;
  3891. CHECK_BUF(p, p_end, 1);
  3892. mutable = read_uint8(p);
  3893. #endif /* end of WASM_ENABLE_GC */
  3894. #if WASM_ENABLE_WAMR_COMPILER != 0
  3895. if (global->type.val_type == VALUE_TYPE_V128)
  3896. module->is_simd_used = true;
  3897. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3898. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3899. module->is_ref_types_used = true;
  3900. #endif
  3901. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3902. return false;
  3903. }
  3904. global->type.is_mutable = mutable ? true : false;
  3905. /* initialize expression */
  3906. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3907. global->type.val_type,
  3908. #if WASM_ENABLE_GC == 0
  3909. NULL,
  3910. #else
  3911. &ref_type,
  3912. #endif
  3913. error_buf, error_buf_size))
  3914. return false;
  3915. #if WASM_ENABLE_GC != 0
  3916. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3917. uint8 global_type;
  3918. WASMRefType *global_ref_type;
  3919. uint32 global_idx = global->init_expr.u.unary.v.global_index;
  3920. if (global->init_expr.u.unary.v.global_index
  3921. >= module->import_global_count + i) {
  3922. set_error_buf(error_buf, error_buf_size, "unknown global");
  3923. return false;
  3924. }
  3925. if (global_idx < module->import_global_count) {
  3926. global_type = module->import_globals[global_idx]
  3927. .u.global.type.val_type;
  3928. global_ref_type =
  3929. module->import_globals[global_idx].u.global.ref_type;
  3930. }
  3931. else {
  3932. global_type =
  3933. module
  3934. ->globals[global_idx - module->import_global_count]
  3935. .type.val_type;
  3936. global_ref_type =
  3937. module
  3938. ->globals[global_idx - module->import_global_count]
  3939. .ref_type;
  3940. }
  3941. if (!wasm_reftype_is_subtype_of(
  3942. global_type, global_ref_type, global->type.val_type,
  3943. global->ref_type, module->types, module->type_count)) {
  3944. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3945. return false;
  3946. }
  3947. }
  3948. if (need_ref_type_map) {
  3949. if (!(global->ref_type =
  3950. reftype_set_insert(module->ref_type_set, &ref_type,
  3951. error_buf, error_buf_size))) {
  3952. return false;
  3953. }
  3954. }
  3955. #if TRACE_WASM_LOADER != 0
  3956. os_printf("global type: ");
  3957. wasm_dump_value_type(global->type, global->ref_type);
  3958. os_printf("\n");
  3959. #endif
  3960. #endif
  3961. module->global_count++;
  3962. }
  3963. bh_assert(module->global_count == global_count);
  3964. }
  3965. if (p != p_end) {
  3966. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3967. return false;
  3968. }
  3969. LOG_VERBOSE("Load global section success.\n");
  3970. return true;
  3971. fail:
  3972. return false;
  3973. }
  3974. static bool
  3975. check_duplicate_exports(WASMModule *module, char *error_buf,
  3976. uint32 error_buf_size)
  3977. {
  3978. uint32 i;
  3979. bool result = false;
  3980. char *names_buf[32], **names = names_buf;
  3981. if (module->export_count > 32) {
  3982. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3983. error_buf_size);
  3984. if (!names) {
  3985. return result;
  3986. }
  3987. }
  3988. for (i = 0; i < module->export_count; i++) {
  3989. names[i] = module->exports[i].name;
  3990. }
  3991. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3992. for (i = 1; i < module->export_count; i++) {
  3993. if (!strcmp(names[i], names[i - 1])) {
  3994. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3995. goto cleanup;
  3996. }
  3997. }
  3998. result = true;
  3999. cleanup:
  4000. if (module->export_count > 32) {
  4001. wasm_runtime_free(names);
  4002. }
  4003. return result;
  4004. }
  4005. static bool
  4006. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4007. bool is_load_from_file_buf, char *error_buf,
  4008. uint32 error_buf_size)
  4009. {
  4010. const uint8 *p = buf, *p_end = buf_end;
  4011. uint32 export_count, i, index;
  4012. uint64 total_size;
  4013. uint32 str_len;
  4014. WASMExport *export;
  4015. read_leb_uint32(p, p_end, export_count);
  4016. if (export_count) {
  4017. module->export_count = export_count;
  4018. total_size = sizeof(WASMExport) * (uint64)export_count;
  4019. if (!(module->exports =
  4020. loader_malloc(total_size, error_buf, error_buf_size))) {
  4021. return false;
  4022. }
  4023. export = module->exports;
  4024. for (i = 0; i < export_count; i++, export ++) {
  4025. #if WASM_ENABLE_THREAD_MGR == 0
  4026. if (p == p_end) {
  4027. /* export section with inconsistent count:
  4028. n export declared, but less than n given */
  4029. set_error_buf(error_buf, error_buf_size,
  4030. "length out of bounds");
  4031. return false;
  4032. }
  4033. #endif
  4034. read_leb_uint32(p, p_end, str_len);
  4035. CHECK_BUF(p, p_end, str_len);
  4036. if (!(export->name = wasm_const_str_list_insert(
  4037. p, str_len, module, is_load_from_file_buf, error_buf,
  4038. error_buf_size))) {
  4039. return false;
  4040. }
  4041. p += str_len;
  4042. CHECK_BUF(p, p_end, 1);
  4043. export->kind = read_uint8(p);
  4044. read_leb_uint32(p, p_end, index);
  4045. export->index = index;
  4046. switch (export->kind) {
  4047. /* function index */
  4048. case EXPORT_KIND_FUNC:
  4049. if (index >= module->function_count
  4050. + module->import_function_count) {
  4051. set_error_buf(error_buf, error_buf_size,
  4052. "unknown function");
  4053. return false;
  4054. }
  4055. #if WASM_ENABLE_SIMD != 0
  4056. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  4057. || (WASM_ENABLE_FAST_INTERP != 0)
  4058. /* TODO: check func type, if it has v128 param or result,
  4059. report error */
  4060. #endif
  4061. #endif
  4062. break;
  4063. /* table index */
  4064. case EXPORT_KIND_TABLE:
  4065. if (index
  4066. >= module->table_count + module->import_table_count) {
  4067. set_error_buf(error_buf, error_buf_size,
  4068. "unknown table");
  4069. return false;
  4070. }
  4071. break;
  4072. /* memory index */
  4073. case EXPORT_KIND_MEMORY:
  4074. if (index
  4075. >= module->memory_count + module->import_memory_count) {
  4076. set_error_buf(error_buf, error_buf_size,
  4077. "unknown memory");
  4078. return false;
  4079. }
  4080. break;
  4081. #if WASM_ENABLE_TAGS != 0
  4082. /* export tag */
  4083. case EXPORT_KIND_TAG:
  4084. if (index >= module->tag_count + module->import_tag_count) {
  4085. set_error_buf(error_buf, error_buf_size, "unknown tag");
  4086. return false;
  4087. }
  4088. break;
  4089. #endif
  4090. /* global index */
  4091. case EXPORT_KIND_GLOBAL:
  4092. if (index
  4093. >= module->global_count + module->import_global_count) {
  4094. set_error_buf(error_buf, error_buf_size,
  4095. "unknown global");
  4096. return false;
  4097. }
  4098. break;
  4099. default:
  4100. set_error_buf(error_buf, error_buf_size,
  4101. "invalid export kind");
  4102. return false;
  4103. }
  4104. }
  4105. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  4106. return false;
  4107. }
  4108. }
  4109. if (p != p_end) {
  4110. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4111. return false;
  4112. }
  4113. LOG_VERBOSE("Load export section success.\n");
  4114. return true;
  4115. fail:
  4116. return false;
  4117. }
  4118. static bool
  4119. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  4120. uint32 error_buf_size)
  4121. {
  4122. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  4123. if (table_index != 0) {
  4124. set_error_buf(
  4125. error_buf, error_buf_size,
  4126. "zero byte expected. The module uses reference types feature "
  4127. "which is disabled in the runtime.");
  4128. return false;
  4129. }
  4130. #endif
  4131. if (table_index >= module->import_table_count + module->table_count) {
  4132. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  4133. table_index);
  4134. return false;
  4135. }
  4136. return true;
  4137. }
  4138. static bool
  4139. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  4140. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  4141. {
  4142. const uint8 *p = *p_buf, *p_end = buf_end;
  4143. uint32 table_index;
  4144. read_leb_uint32(p, p_end, table_index);
  4145. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  4146. return false;
  4147. }
  4148. *p_table_index = table_index;
  4149. *p_buf = p;
  4150. return true;
  4151. fail:
  4152. return false;
  4153. }
  4154. /* Element segments must match element type of table */
  4155. static bool
  4156. check_table_elem_type(WASMModule *module, uint32 table_index,
  4157. uint32 type_from_elem_seg, char *error_buf,
  4158. uint32 error_buf_size)
  4159. {
  4160. uint32 table_declared_elem_type;
  4161. if (table_index < module->import_table_count)
  4162. table_declared_elem_type =
  4163. module->import_tables[table_index].u.table.table_type.elem_type;
  4164. else
  4165. table_declared_elem_type =
  4166. module->tables[table_index - module->import_table_count]
  4167. .table_type.elem_type;
  4168. if (table_declared_elem_type == type_from_elem_seg)
  4169. return true;
  4170. #if WASM_ENABLE_GC != 0
  4171. /*
  4172. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  4173. */
  4174. if (table_declared_elem_type == REF_TYPE_ANYREF)
  4175. return true;
  4176. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  4177. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4178. return true;
  4179. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  4180. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  4181. return true;
  4182. #endif
  4183. set_error_buf(error_buf, error_buf_size, "type mismatch");
  4184. return false;
  4185. }
  4186. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4187. static bool
  4188. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  4189. uint32 *p_elem_type,
  4190. #if WASM_ENABLE_GC != 0
  4191. WASMRefType **p_elem_ref_type,
  4192. #endif
  4193. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  4194. {
  4195. const uint8 *p = *p_buf, *p_end = buf_end;
  4196. uint8 elem_type;
  4197. #if WASM_ENABLE_GC != 0
  4198. WASMRefType elem_ref_type;
  4199. bool need_ref_type_map;
  4200. #endif
  4201. CHECK_BUF(p, p_end, 1);
  4202. elem_type = read_uint8(p);
  4203. if (elemkind_zero) {
  4204. if (elem_type != 0) {
  4205. set_error_buf(error_buf, error_buf_size,
  4206. "invalid reference type or unknown type");
  4207. return false;
  4208. }
  4209. else {
  4210. *p_elem_type = VALUE_TYPE_FUNCREF;
  4211. *p_buf = p;
  4212. return true;
  4213. }
  4214. }
  4215. #if WASM_ENABLE_GC == 0
  4216. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  4217. set_error_buf(error_buf, error_buf_size,
  4218. "invalid reference type or unknown type");
  4219. return false;
  4220. }
  4221. *p_elem_type = elem_type;
  4222. #else
  4223. p--;
  4224. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  4225. module->type_count, &need_ref_type_map,
  4226. &elem_ref_type, false, error_buf, error_buf_size)) {
  4227. return false;
  4228. }
  4229. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  4230. set_error_buf(error_buf, error_buf_size,
  4231. "invalid reference type or unknown type");
  4232. return false;
  4233. }
  4234. *p_elem_type = elem_ref_type.ref_type;
  4235. if (need_ref_type_map) {
  4236. if (!(*p_elem_ref_type =
  4237. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  4238. error_buf, error_buf_size))) {
  4239. return false;
  4240. }
  4241. }
  4242. #endif
  4243. *p_buf = p;
  4244. return true;
  4245. fail:
  4246. return false;
  4247. }
  4248. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4249. static bool
  4250. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  4251. WASMModule *module, WASMTableSeg *table_segment,
  4252. char *error_buf, uint32 error_buf_size)
  4253. {
  4254. const uint8 *p = *p_buf, *p_end = buf_end;
  4255. uint32 function_count, function_index = 0, i;
  4256. uint64 total_size;
  4257. read_leb_uint32(p, p_end, function_count);
  4258. table_segment->value_count = function_count;
  4259. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  4260. if (total_size > 0
  4261. && !(table_segment->init_values =
  4262. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4263. error_buf_size))) {
  4264. return false;
  4265. }
  4266. for (i = 0; i < function_count; i++) {
  4267. InitializerExpression *init_expr = &table_segment->init_values[i];
  4268. read_leb_uint32(p, p_end, function_index);
  4269. if (!check_function_index(module, function_index, error_buf,
  4270. error_buf_size)) {
  4271. return false;
  4272. }
  4273. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  4274. init_expr->u.unary.v.ref_index = function_index;
  4275. }
  4276. *p_buf = p;
  4277. return true;
  4278. fail:
  4279. return false;
  4280. }
  4281. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  4282. static bool
  4283. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  4284. WASMModule *module, WASMTableSeg *table_segment,
  4285. char *error_buf, uint32 error_buf_size)
  4286. {
  4287. const uint8 *p = *p_buf, *p_end = buf_end;
  4288. uint32 ref_count, i;
  4289. uint64 total_size;
  4290. read_leb_uint32(p, p_end, ref_count);
  4291. table_segment->value_count = ref_count;
  4292. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  4293. if (total_size > 0
  4294. && !(table_segment->init_values =
  4295. (InitializerExpression *)loader_malloc(total_size, error_buf,
  4296. error_buf_size))) {
  4297. return false;
  4298. }
  4299. for (i = 0; i < ref_count; i++) {
  4300. InitializerExpression *init_expr = &table_segment->init_values[i];
  4301. if (!load_init_expr(module, &p, p_end, init_expr,
  4302. table_segment->elem_type,
  4303. #if WASM_ENABLE_GC == 0
  4304. NULL,
  4305. #else
  4306. table_segment->elem_ref_type,
  4307. #endif
  4308. error_buf, error_buf_size))
  4309. return false;
  4310. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  4311. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  4312. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  4313. && init_expr->init_expr_type
  4314. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  4315. }
  4316. *p_buf = p;
  4317. return true;
  4318. fail:
  4319. return false;
  4320. }
  4321. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  4322. static bool
  4323. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  4324. WASMModule *module, char *error_buf,
  4325. uint32 error_buf_size)
  4326. {
  4327. const uint8 *p = buf, *p_end = buf_end;
  4328. uint8 table_elem_idx_type;
  4329. uint32 table_segment_count, i;
  4330. uint64 total_size;
  4331. WASMTableSeg *table_segment;
  4332. read_leb_uint32(p, p_end, table_segment_count);
  4333. if (table_segment_count) {
  4334. module->table_seg_count = table_segment_count;
  4335. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4336. if (!(module->table_segments =
  4337. loader_malloc(total_size, error_buf, error_buf_size))) {
  4338. return false;
  4339. }
  4340. table_segment = module->table_segments;
  4341. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4342. if (p >= p_end) {
  4343. set_error_buf(error_buf, error_buf_size,
  4344. "invalid value type or "
  4345. "invalid elements segment kind");
  4346. return false;
  4347. }
  4348. table_elem_idx_type = VALUE_TYPE_I32;
  4349. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4350. read_leb_uint32(p, p_end, table_segment->mode);
  4351. /* last three bits */
  4352. table_segment->mode = table_segment->mode & 0x07;
  4353. switch (table_segment->mode) {
  4354. /* elemkind/elemtype + active */
  4355. case 0:
  4356. case 4:
  4357. {
  4358. #if WASM_ENABLE_GC != 0
  4359. if (table_segment->mode == 0) {
  4360. /* vec(funcidx), set elem type to (ref func) */
  4361. WASMRefType elem_ref_type = { 0 };
  4362. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4363. wasm_set_refheaptype_common(
  4364. &elem_ref_type.ref_ht_common, false,
  4365. HEAP_TYPE_FUNC);
  4366. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4367. module->ref_type_set, &elem_ref_type,
  4368. error_buf, error_buf_size)))
  4369. return false;
  4370. }
  4371. else {
  4372. /* vec(expr), set elem type to funcref */
  4373. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4374. }
  4375. #else
  4376. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4377. #endif
  4378. table_segment->table_index = 0;
  4379. if (!check_table_index(module, table_segment->table_index,
  4380. error_buf, error_buf_size))
  4381. return false;
  4382. #if WASM_ENABLE_MEMORY64 != 0
  4383. table_elem_idx_type =
  4384. is_table_64bit(module, table_segment->table_index)
  4385. ? VALUE_TYPE_I64
  4386. : VALUE_TYPE_I32;
  4387. #endif
  4388. if (!load_init_expr(module, &p, p_end,
  4389. &table_segment->base_offset,
  4390. table_elem_idx_type, NULL, error_buf,
  4391. error_buf_size))
  4392. return false;
  4393. if (table_segment->mode == 0) {
  4394. /* vec(funcidx) */
  4395. if (!load_func_index_vec(&p, p_end, module,
  4396. table_segment, error_buf,
  4397. error_buf_size))
  4398. return false;
  4399. }
  4400. else {
  4401. /* vec(expr) */
  4402. if (!load_init_expr_vec(&p, p_end, module,
  4403. table_segment, error_buf,
  4404. error_buf_size))
  4405. return false;
  4406. }
  4407. if (!check_table_elem_type(module,
  4408. table_segment->table_index,
  4409. table_segment->elem_type,
  4410. error_buf, error_buf_size))
  4411. return false;
  4412. break;
  4413. }
  4414. /* elemkind + passive/declarative */
  4415. case 1:
  4416. case 3:
  4417. if (!load_elem_type(module, &p, p_end,
  4418. &table_segment->elem_type,
  4419. #if WASM_ENABLE_GC != 0
  4420. &table_segment->elem_ref_type,
  4421. #endif
  4422. true, error_buf, error_buf_size))
  4423. return false;
  4424. /* vec(funcidx) */
  4425. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4426. error_buf, error_buf_size))
  4427. return false;
  4428. break;
  4429. /* elemkind/elemtype + table_idx + active */
  4430. case 2:
  4431. case 6:
  4432. if (!load_table_index(&p, p_end, module,
  4433. &table_segment->table_index,
  4434. error_buf, error_buf_size))
  4435. return false;
  4436. #if WASM_ENABLE_MEMORY64 != 0
  4437. table_elem_idx_type =
  4438. is_table_64bit(module, table_segment->table_index)
  4439. ? VALUE_TYPE_I64
  4440. : VALUE_TYPE_I32;
  4441. #endif
  4442. if (!load_init_expr(module, &p, p_end,
  4443. &table_segment->base_offset,
  4444. table_elem_idx_type, NULL, error_buf,
  4445. error_buf_size))
  4446. return false;
  4447. if (!load_elem_type(module, &p, p_end,
  4448. &table_segment->elem_type,
  4449. #if WASM_ENABLE_GC != 0
  4450. &table_segment->elem_ref_type,
  4451. #endif
  4452. table_segment->mode == 2 ? true : false,
  4453. error_buf, error_buf_size))
  4454. return false;
  4455. if (table_segment->mode == 2) {
  4456. /* vec(funcidx) */
  4457. if (!load_func_index_vec(&p, p_end, module,
  4458. table_segment, error_buf,
  4459. error_buf_size))
  4460. return false;
  4461. }
  4462. else {
  4463. /* vec(expr) */
  4464. if (!load_init_expr_vec(&p, p_end, module,
  4465. table_segment, error_buf,
  4466. error_buf_size))
  4467. return false;
  4468. }
  4469. if (!check_table_elem_type(module,
  4470. table_segment->table_index,
  4471. table_segment->elem_type,
  4472. error_buf, error_buf_size))
  4473. return false;
  4474. break;
  4475. case 5:
  4476. case 7:
  4477. if (!load_elem_type(module, &p, p_end,
  4478. &table_segment->elem_type,
  4479. #if WASM_ENABLE_GC != 0
  4480. &table_segment->elem_ref_type,
  4481. #endif
  4482. false, error_buf, error_buf_size))
  4483. return false;
  4484. /* vec(expr) */
  4485. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4486. error_buf, error_buf_size))
  4487. return false;
  4488. break;
  4489. default:
  4490. set_error_buf(error_buf, error_buf_size,
  4491. "unknown element segment kind");
  4492. return false;
  4493. }
  4494. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4495. /*
  4496. * like: 00 41 05 0b 04 00 01 00 01
  4497. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4498. */
  4499. if (!load_table_index(&p, p_end, module,
  4500. &table_segment->table_index, error_buf,
  4501. error_buf_size))
  4502. return false;
  4503. #if WASM_ENABLE_MEMORY64 != 0
  4504. table_elem_idx_type =
  4505. is_table_64bit(module, table_segment->table_index)
  4506. ? VALUE_TYPE_I64
  4507. : VALUE_TYPE_I32;
  4508. #endif
  4509. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4510. table_elem_idx_type, NULL, error_buf,
  4511. error_buf_size))
  4512. return false;
  4513. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4514. error_buf, error_buf_size))
  4515. return false;
  4516. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4517. if (!check_table_elem_type(module, table_segment->table_index,
  4518. table_segment->elem_type, error_buf,
  4519. error_buf_size))
  4520. return false;
  4521. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4522. #if WASM_ENABLE_MEMORY64 != 0
  4523. if (table_elem_idx_type == VALUE_TYPE_I64
  4524. && table_segment->base_offset.u.unary.v.u64 > UINT32_MAX) {
  4525. set_error_buf(error_buf, error_buf_size,
  4526. "In table64, table base offset can't be "
  4527. "larger than UINT32_MAX");
  4528. return false;
  4529. }
  4530. #endif
  4531. #if WASM_ENABLE_WAMR_COMPILER != 0
  4532. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4533. module->is_ref_types_used = true;
  4534. #endif
  4535. }
  4536. }
  4537. if (p != p_end) {
  4538. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4539. return false;
  4540. }
  4541. LOG_VERBOSE("Load table segment section success.\n");
  4542. return true;
  4543. fail:
  4544. return false;
  4545. }
  4546. #if WASM_ENABLE_BULK_MEMORY != 0
  4547. static bool
  4548. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4549. int data_seg_len, char *error_buf,
  4550. uint32 error_buf_size)
  4551. {
  4552. if (has_datacount_section && datacount_len != data_seg_len) {
  4553. set_error_buf(error_buf, error_buf_size,
  4554. "data count and data section have inconsistent lengths");
  4555. return false;
  4556. }
  4557. return true;
  4558. }
  4559. #endif
  4560. static bool
  4561. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4562. WASMModule *module,
  4563. #if WASM_ENABLE_BULK_MEMORY != 0
  4564. bool has_datacount_section,
  4565. #endif
  4566. bool clone_data_seg, char *error_buf,
  4567. uint32 error_buf_size)
  4568. {
  4569. const uint8 *p = buf, *p_end = buf_end;
  4570. uint32 data_seg_count, i, mem_index, data_seg_len;
  4571. uint64 total_size;
  4572. WASMDataSeg *dataseg;
  4573. InitializerExpression init_expr;
  4574. #if WASM_ENABLE_BULK_MEMORY != 0
  4575. bool is_passive = false;
  4576. uint32 mem_flag;
  4577. #endif
  4578. uint8 mem_offset_type = VALUE_TYPE_I32;
  4579. read_leb_uint32(p, p_end, data_seg_count);
  4580. #if WASM_ENABLE_BULK_MEMORY != 0
  4581. if (!check_data_count_consistency(has_datacount_section,
  4582. module->data_seg_count1, data_seg_count,
  4583. error_buf, error_buf_size)) {
  4584. return false;
  4585. }
  4586. #endif
  4587. if (data_seg_count) {
  4588. module->data_seg_count = data_seg_count;
  4589. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4590. if (!(module->data_segments =
  4591. loader_malloc(total_size, error_buf, error_buf_size))) {
  4592. return false;
  4593. }
  4594. for (i = 0; i < data_seg_count; i++) {
  4595. read_leb_uint32(p, p_end, mem_index);
  4596. #if WASM_ENABLE_BULK_MEMORY != 0
  4597. is_passive = false;
  4598. mem_flag = mem_index & 0x03;
  4599. switch (mem_flag) {
  4600. case 0x01:
  4601. is_passive = true;
  4602. #if WASM_ENABLE_WAMR_COMPILER != 0
  4603. module->is_bulk_memory_used = true;
  4604. #endif
  4605. break;
  4606. case 0x00:
  4607. /* no memory index, treat index as 0 */
  4608. mem_index = 0;
  4609. goto check_mem_index;
  4610. case 0x02:
  4611. /* read following memory index */
  4612. read_leb_uint32(p, p_end, mem_index);
  4613. #if WASM_ENABLE_WAMR_COMPILER != 0
  4614. module->is_bulk_memory_used = true;
  4615. #endif
  4616. check_mem_index:
  4617. if (mem_index
  4618. >= module->import_memory_count + module->memory_count) {
  4619. set_error_buf_v(error_buf, error_buf_size,
  4620. "unknown memory %d", mem_index);
  4621. return false;
  4622. }
  4623. break;
  4624. case 0x03:
  4625. default:
  4626. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4627. return false;
  4628. break;
  4629. }
  4630. #else
  4631. if (mem_index
  4632. >= module->import_memory_count + module->memory_count) {
  4633. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4634. mem_index);
  4635. return false;
  4636. }
  4637. #endif /* WASM_ENABLE_BULK_MEMORY */
  4638. #if WASM_ENABLE_BULK_MEMORY != 0
  4639. if (!is_passive)
  4640. #endif
  4641. {
  4642. #if WASM_ENABLE_MEMORY64 != 0
  4643. /* This memory_flag is from memory instead of data segment */
  4644. uint8 memory_flag;
  4645. if (module->import_memory_count > 0) {
  4646. memory_flag = module->import_memories[mem_index]
  4647. .u.memory.mem_type.flags;
  4648. }
  4649. else {
  4650. memory_flag =
  4651. module
  4652. ->memories[mem_index - module->import_memory_count]
  4653. .flags;
  4654. }
  4655. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4656. : VALUE_TYPE_I32;
  4657. #else
  4658. mem_offset_type = VALUE_TYPE_I32;
  4659. #endif
  4660. }
  4661. #if WASM_ENABLE_BULK_MEMORY != 0
  4662. if (!is_passive)
  4663. #endif
  4664. if (!load_init_expr(module, &p, p_end, &init_expr,
  4665. mem_offset_type, NULL, error_buf,
  4666. error_buf_size))
  4667. return false;
  4668. read_leb_uint32(p, p_end, data_seg_len);
  4669. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4670. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4671. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  4672. destroy_init_expr(module, &init_expr);
  4673. #endif
  4674. return false;
  4675. }
  4676. #if WASM_ENABLE_BULK_MEMORY != 0
  4677. dataseg->is_passive = is_passive;
  4678. if (!is_passive)
  4679. #endif
  4680. {
  4681. bh_memcpy_s(&dataseg->base_offset,
  4682. sizeof(InitializerExpression), &init_expr,
  4683. sizeof(InitializerExpression));
  4684. dataseg->memory_index = mem_index;
  4685. }
  4686. dataseg->data_length = data_seg_len;
  4687. CHECK_BUF(p, p_end, data_seg_len);
  4688. if (clone_data_seg) {
  4689. if (!(dataseg->data = loader_malloc(
  4690. dataseg->data_length, error_buf, error_buf_size))) {
  4691. return false;
  4692. }
  4693. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4694. data_seg_len);
  4695. }
  4696. else {
  4697. dataseg->data = (uint8 *)p;
  4698. }
  4699. dataseg->is_data_cloned = clone_data_seg;
  4700. p += data_seg_len;
  4701. }
  4702. }
  4703. if (p != p_end) {
  4704. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4705. return false;
  4706. }
  4707. LOG_VERBOSE("Load data segment section success.\n");
  4708. return true;
  4709. fail:
  4710. return false;
  4711. }
  4712. #if WASM_ENABLE_BULK_MEMORY != 0
  4713. static bool
  4714. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4715. WASMModule *module, char *error_buf,
  4716. uint32 error_buf_size)
  4717. {
  4718. const uint8 *p = buf, *p_end = buf_end;
  4719. uint32 data_seg_count1 = 0;
  4720. read_leb_uint32(p, p_end, data_seg_count1);
  4721. module->data_seg_count1 = data_seg_count1;
  4722. if (p != p_end) {
  4723. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4724. return false;
  4725. }
  4726. #if WASM_ENABLE_WAMR_COMPILER != 0
  4727. module->is_bulk_memory_used = true;
  4728. #endif
  4729. LOG_VERBOSE("Load datacount section success.\n");
  4730. return true;
  4731. fail:
  4732. return false;
  4733. }
  4734. #endif
  4735. #if WASM_ENABLE_TAGS != 0
  4736. static bool
  4737. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4738. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4739. uint32 error_buf_size)
  4740. {
  4741. (void)buf_code;
  4742. (void)buf_code_end;
  4743. const uint8 *p = buf, *p_end = buf_end;
  4744. size_t total_size = 0;
  4745. /* number of tags defined in the section */
  4746. uint32 section_tag_count = 0;
  4747. uint8 tag_attribute;
  4748. uint32 tag_type;
  4749. WASMTag *tag = NULL;
  4750. /* get tag count */
  4751. read_leb_uint32(p, p_end, section_tag_count);
  4752. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4753. error_buf, error_buf_size))
  4754. return false;
  4755. module->tag_count = section_tag_count;
  4756. if (section_tag_count) {
  4757. total_size = sizeof(WASMTag *) * module->tag_count;
  4758. if (!(module->tags =
  4759. loader_malloc(total_size, error_buf, error_buf_size))) {
  4760. return false;
  4761. }
  4762. /* load each tag, imported tags precede the tags */
  4763. uint32 tag_index;
  4764. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4765. /* get the one byte attribute */
  4766. CHECK_BUF(p, p_end, 1);
  4767. tag_attribute = read_uint8(p);
  4768. /* get type */
  4769. read_leb_uint32(p, p_end, tag_type);
  4770. /* compare against module->types */
  4771. if (!check_function_type(module, tag_type, error_buf,
  4772. error_buf_size)) {
  4773. return false;
  4774. }
  4775. /* get return type (must be 0) */
  4776. /* check, that the type of the referred tag returns void */
  4777. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4778. if (func_type->result_count != 0) {
  4779. set_error_buf(error_buf, error_buf_size,
  4780. "non-empty tag result type");
  4781. goto fail;
  4782. }
  4783. if (!(tag = module->tags[tag_index] = loader_malloc(
  4784. sizeof(WASMTag), error_buf, error_buf_size))) {
  4785. return false;
  4786. }
  4787. /* store to module tag declarations */
  4788. tag->attribute = tag_attribute;
  4789. tag->type = tag_type;
  4790. tag->tag_type = func_type;
  4791. }
  4792. }
  4793. if (p != p_end) {
  4794. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4795. return false;
  4796. }
  4797. LOG_VERBOSE("Load tag section success.\n");
  4798. return true;
  4799. fail:
  4800. return false;
  4801. }
  4802. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4803. static bool
  4804. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4805. const uint8 *buf_func_end, WASMModule *module,
  4806. char *error_buf, uint32 error_buf_size)
  4807. {
  4808. const uint8 *p = buf, *p_end = buf_end;
  4809. const uint8 *p_func = buf_func;
  4810. uint32 func_count = 0, code_count;
  4811. /* code has been loaded in function section, so pass it here, just check
  4812. * whether function and code section have inconsistent lengths */
  4813. read_leb_uint32(p, p_end, code_count);
  4814. if (buf_func)
  4815. read_leb_uint32(p_func, buf_func_end, func_count);
  4816. if (func_count != code_count) {
  4817. set_error_buf(error_buf, error_buf_size,
  4818. "function and code section have inconsistent lengths");
  4819. return false;
  4820. }
  4821. LOG_VERBOSE("Load code segment section success.\n");
  4822. (void)module;
  4823. return true;
  4824. fail:
  4825. return false;
  4826. }
  4827. static bool
  4828. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4829. char *error_buf, uint32 error_buf_size)
  4830. {
  4831. const uint8 *p = buf, *p_end = buf_end;
  4832. WASMFuncType *type;
  4833. uint32 start_function;
  4834. read_leb_uint32(p, p_end, start_function);
  4835. if (start_function
  4836. >= module->function_count + module->import_function_count) {
  4837. set_error_buf(error_buf, error_buf_size, "unknown function");
  4838. return false;
  4839. }
  4840. if (start_function < module->import_function_count)
  4841. type = module->import_functions[start_function].u.function.func_type;
  4842. else
  4843. type = module->functions[start_function - module->import_function_count]
  4844. ->func_type;
  4845. if (type->param_count != 0 || type->result_count != 0) {
  4846. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4847. return false;
  4848. }
  4849. module->start_function = start_function;
  4850. if (p != p_end) {
  4851. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4852. return false;
  4853. }
  4854. LOG_VERBOSE("Load start section success.\n");
  4855. return true;
  4856. fail:
  4857. return false;
  4858. }
  4859. #if WASM_ENABLE_STRINGREF != 0
  4860. static bool
  4861. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4862. WASMModule *module, bool is_load_from_file_buf,
  4863. char *error_buf, uint32 error_buf_size)
  4864. {
  4865. const uint8 *p = buf, *p_end = buf_end;
  4866. int32 deferred_count, immediate_count, string_length, i;
  4867. uint64 total_size;
  4868. read_leb_uint32(p, p_end, deferred_count);
  4869. read_leb_uint32(p, p_end, immediate_count);
  4870. /* proposal set deferred_count for future extension */
  4871. if (deferred_count != 0) {
  4872. goto fail;
  4873. }
  4874. if (immediate_count > 0) {
  4875. total_size = sizeof(char *) * (uint64)immediate_count;
  4876. if (!(module->string_literal_ptrs =
  4877. loader_malloc(total_size, error_buf, error_buf_size))) {
  4878. goto fail;
  4879. }
  4880. module->string_literal_count = immediate_count;
  4881. total_size = sizeof(uint32) * (uint64)immediate_count;
  4882. if (!(module->string_literal_lengths =
  4883. loader_malloc(total_size, error_buf, error_buf_size))) {
  4884. goto fail;
  4885. }
  4886. for (i = 0; i < immediate_count; i++) {
  4887. read_leb_uint32(p, p_end, string_length);
  4888. CHECK_BUF(p, p_end, string_length);
  4889. module->string_literal_ptrs[i] = p;
  4890. module->string_literal_lengths[i] = string_length;
  4891. p += string_length;
  4892. }
  4893. }
  4894. if (p != p_end) {
  4895. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4896. goto fail;
  4897. }
  4898. LOG_VERBOSE("Load stringref section success.\n");
  4899. return true;
  4900. fail:
  4901. return false;
  4902. }
  4903. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4904. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4905. static bool
  4906. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4907. bool is_load_from_file_buf, char *error_buf,
  4908. uint32 error_buf_size)
  4909. {
  4910. const uint8 *p = buf, *p_end = buf_end;
  4911. uint32 name_type, subsection_size;
  4912. uint32 previous_name_type = 0;
  4913. uint32 num_func_name;
  4914. uint32 func_index;
  4915. uint32 previous_func_index = ~0U;
  4916. uint32 func_name_len;
  4917. uint32 name_index;
  4918. int i = 0;
  4919. if (p >= p_end) {
  4920. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4921. return false;
  4922. }
  4923. while (p < p_end) {
  4924. read_leb_uint32(p, p_end, name_type);
  4925. if (i != 0) {
  4926. if (name_type == previous_name_type) {
  4927. set_error_buf(error_buf, error_buf_size,
  4928. "duplicate sub-section");
  4929. return false;
  4930. }
  4931. if (name_type < previous_name_type) {
  4932. set_error_buf(error_buf, error_buf_size,
  4933. "out-of-order sub-section");
  4934. return false;
  4935. }
  4936. }
  4937. previous_name_type = name_type;
  4938. read_leb_uint32(p, p_end, subsection_size);
  4939. CHECK_BUF(p, p_end, subsection_size);
  4940. switch (name_type) {
  4941. case SUB_SECTION_TYPE_FUNC:
  4942. if (subsection_size) {
  4943. read_leb_uint32(p, p_end, num_func_name);
  4944. for (name_index = 0; name_index < num_func_name;
  4945. name_index++) {
  4946. read_leb_uint32(p, p_end, func_index);
  4947. if (func_index == previous_func_index) {
  4948. set_error_buf(error_buf, error_buf_size,
  4949. "duplicate function name");
  4950. return false;
  4951. }
  4952. if (func_index < previous_func_index
  4953. && previous_func_index != ~0U) {
  4954. set_error_buf(error_buf, error_buf_size,
  4955. "out-of-order function index ");
  4956. return false;
  4957. }
  4958. previous_func_index = func_index;
  4959. read_leb_uint32(p, p_end, func_name_len);
  4960. CHECK_BUF(p, p_end, func_name_len);
  4961. /* Skip the import functions */
  4962. if (func_index >= module->import_function_count) {
  4963. func_index -= module->import_function_count;
  4964. if (func_index >= module->function_count) {
  4965. set_error_buf(error_buf, error_buf_size,
  4966. "out-of-range function index");
  4967. return false;
  4968. }
  4969. if (!(module->functions[func_index]->field_name =
  4970. wasm_const_str_list_insert(
  4971. p, func_name_len, module,
  4972. #if WASM_ENABLE_WAMR_COMPILER != 0
  4973. false,
  4974. #else
  4975. is_load_from_file_buf,
  4976. #endif
  4977. error_buf, error_buf_size))) {
  4978. return false;
  4979. }
  4980. }
  4981. p += func_name_len;
  4982. }
  4983. }
  4984. break;
  4985. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4986. */
  4987. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4988. default:
  4989. p = p + subsection_size;
  4990. break;
  4991. }
  4992. i++;
  4993. }
  4994. return true;
  4995. fail:
  4996. return false;
  4997. }
  4998. #endif
  4999. #if WASM_ENABLE_BRANCH_HINTS != 0
  5000. static bool
  5001. handle_branch_hint_section(const uint8 *buf, const uint8 *buf_end,
  5002. WASMModule *module, char *error_buf,
  5003. uint32 error_buf_size)
  5004. {
  5005. if (module->function_hints == NULL) {
  5006. module->function_hints = loader_malloc(
  5007. sizeof(struct WASMCompilationHint) * module->function_count,
  5008. error_buf, error_buf_size);
  5009. }
  5010. uint32 numFunctionHints = 0;
  5011. read_leb_uint32(buf, buf_end, numFunctionHints);
  5012. for (uint32 i = 0; i < numFunctionHints; ++i) {
  5013. uint32 func_idx;
  5014. read_leb_uint32(buf, buf_end, func_idx);
  5015. if (!check_function_index(module, func_idx, error_buf,
  5016. error_buf_size)) {
  5017. goto fail;
  5018. }
  5019. if (func_idx < module->import_function_count) {
  5020. set_error_buf(error_buf, error_buf_size,
  5021. "branch hint for imported function is not allowed");
  5022. goto fail;
  5023. }
  5024. struct WASMCompilationHint *current_hint =
  5025. (struct WASMCompilationHint *)&module
  5026. ->function_hints[func_idx - module->import_function_count];
  5027. while (current_hint->next != NULL) {
  5028. current_hint = current_hint->next;
  5029. }
  5030. uint32 num_hints;
  5031. read_leb_uint32(buf, buf_end, num_hints);
  5032. struct WASMCompilationHintBranchHint *new_hints = loader_malloc(
  5033. sizeof(struct WASMCompilationHintBranchHint) * num_hints, error_buf,
  5034. error_buf_size);
  5035. for (uint32 j = 0; j < num_hints; ++j) {
  5036. struct WASMCompilationHintBranchHint *new_hint = &new_hints[j];
  5037. new_hint->next = NULL;
  5038. new_hint->type = WASM_COMPILATION_BRANCH_HINT;
  5039. read_leb_uint32(buf, buf_end, new_hint->offset);
  5040. uint32 size;
  5041. read_leb_uint32(buf, buf_end, size);
  5042. if (size != 1) {
  5043. set_error_buf_v(error_buf, error_buf_size,
  5044. "invalid branch hint size, expected 1, got %d.",
  5045. size);
  5046. wasm_runtime_free(new_hint);
  5047. goto fail;
  5048. }
  5049. uint8 data = *buf++;
  5050. if (data == 0x00)
  5051. new_hint->is_likely = false;
  5052. else if (data == 0x01)
  5053. new_hint->is_likely = true;
  5054. else {
  5055. set_error_buf_v(error_buf, error_buf_size,
  5056. "invalid branch hint, expected 0 or 1, got %d",
  5057. data);
  5058. wasm_runtime_free(new_hint);
  5059. goto fail;
  5060. }
  5061. current_hint->next = (struct WASMCompilationHint *)new_hint;
  5062. current_hint = (struct WASMCompilationHint *)new_hint;
  5063. }
  5064. }
  5065. if (buf != buf_end) {
  5066. set_error_buf(error_buf, error_buf_size,
  5067. "invalid branch hint section, not filled until end");
  5068. goto fail;
  5069. }
  5070. return true;
  5071. fail:
  5072. return false;
  5073. }
  5074. #endif
  5075. static bool
  5076. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  5077. bool is_load_from_file_buf, char *error_buf,
  5078. uint32 error_buf_size)
  5079. {
  5080. const uint8 *p = buf, *p_end = buf_end;
  5081. char section_name[32];
  5082. uint32 name_len, buffer_len;
  5083. if (p >= p_end) {
  5084. set_error_buf(error_buf, error_buf_size, "unexpected end");
  5085. return false;
  5086. }
  5087. read_leb_uint32(p, p_end, name_len);
  5088. if (p + name_len > p_end) {
  5089. set_error_buf(error_buf, error_buf_size, "unexpected end");
  5090. return false;
  5091. }
  5092. if (!wasm_check_utf8_str(p, name_len)) {
  5093. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  5094. return false;
  5095. }
  5096. buffer_len = sizeof(section_name);
  5097. memset(section_name, 0, buffer_len);
  5098. if (name_len < buffer_len) {
  5099. bh_memcpy_s(section_name, buffer_len, p, name_len);
  5100. }
  5101. else {
  5102. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  5103. memset(section_name + buffer_len - 4, '.', 3);
  5104. }
  5105. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  5106. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  5107. module->name_section_buf = buf;
  5108. module->name_section_buf_end = buf_end;
  5109. p += name_len;
  5110. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  5111. error_buf, error_buf_size)) {
  5112. return false;
  5113. }
  5114. LOG_VERBOSE("Load custom name section success.");
  5115. }
  5116. #endif
  5117. #if WASM_ENABLE_BRANCH_HINTS != 0
  5118. if (name_len == 25
  5119. && memcmp((const char *)p, "metadata.code.branch_hint", 25) == 0) {
  5120. p += name_len;
  5121. if (!handle_branch_hint_section(p, p_end, module, error_buf,
  5122. error_buf_size)) {
  5123. return false;
  5124. }
  5125. LOG_VERBOSE("Load branch hint section success.");
  5126. }
  5127. #else
  5128. if (name_len == 25
  5129. && memcmp((const char *)p, "metadata.code.branch_hint", 25) == 0) {
  5130. LOG_VERBOSE("Found branch hint section, but branch hints are disabled "
  5131. "in this build, skipping.");
  5132. }
  5133. #endif
  5134. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  5135. {
  5136. WASMCustomSection *section =
  5137. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  5138. if (!section) {
  5139. return false;
  5140. }
  5141. section->name_addr = (char *)p;
  5142. section->name_len = name_len;
  5143. section->content_addr = (uint8 *)(p + name_len);
  5144. section->content_len = (uint32)(p_end - p - name_len);
  5145. section->next = module->custom_section_list;
  5146. module->custom_section_list = section;
  5147. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  5148. return true;
  5149. }
  5150. #endif
  5151. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  5152. (void)is_load_from_file_buf;
  5153. (void)module;
  5154. return true;
  5155. fail:
  5156. return false;
  5157. }
  5158. static void
  5159. calculate_global_data_offset(WASMModule *module)
  5160. {
  5161. uint32 i, data_offset;
  5162. data_offset = 0;
  5163. for (i = 0; i < module->import_global_count; i++) {
  5164. WASMGlobalImport *import_global =
  5165. &((module->import_globals + i)->u.global);
  5166. #if WASM_ENABLE_FAST_JIT != 0
  5167. import_global->data_offset = data_offset;
  5168. #endif
  5169. data_offset += wasm_value_type_size(import_global->type.val_type);
  5170. }
  5171. for (i = 0; i < module->global_count; i++) {
  5172. WASMGlobal *global = module->globals + i;
  5173. #if WASM_ENABLE_FAST_JIT != 0
  5174. global->data_offset = data_offset;
  5175. #endif
  5176. data_offset += wasm_value_type_size(global->type.val_type);
  5177. }
  5178. module->global_data_size = data_offset;
  5179. }
  5180. #if WASM_ENABLE_FAST_JIT != 0
  5181. static bool
  5182. init_fast_jit_functions(WASMModule *module, char *error_buf,
  5183. uint32 error_buf_size)
  5184. {
  5185. #if WASM_ENABLE_LAZY_JIT != 0
  5186. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  5187. #endif
  5188. uint32 i;
  5189. if (!module->function_count)
  5190. return true;
  5191. if (!(module->fast_jit_func_ptrs =
  5192. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  5193. error_buf_size))) {
  5194. return false;
  5195. }
  5196. #if WASM_ENABLE_LAZY_JIT != 0
  5197. for (i = 0; i < module->function_count; i++) {
  5198. module->fast_jit_func_ptrs[i] =
  5199. jit_globals->compile_fast_jit_and_then_call;
  5200. }
  5201. #endif
  5202. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  5203. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  5204. set_error_buf(error_buf, error_buf_size,
  5205. "init fast jit thread lock failed");
  5206. return false;
  5207. }
  5208. module->fast_jit_thread_locks_inited[i] = true;
  5209. }
  5210. return true;
  5211. }
  5212. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  5213. #if WASM_ENABLE_JIT != 0
  5214. static bool
  5215. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  5216. uint32 error_buf_size)
  5217. {
  5218. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  5219. AOTCompOption option = { 0 };
  5220. char *aot_last_error;
  5221. uint64 size;
  5222. #if WASM_ENABLE_GC != 0
  5223. bool gc_enabled = true;
  5224. #else
  5225. bool gc_enabled = false;
  5226. #endif
  5227. if (module->function_count == 0)
  5228. return true;
  5229. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5230. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  5231. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  5232. return false;
  5233. }
  5234. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  5235. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  5236. os_mutex_destroy(&module->tierup_wait_lock);
  5237. return false;
  5238. }
  5239. module->tierup_wait_lock_inited = true;
  5240. #endif
  5241. size = sizeof(void *) * (uint64)module->function_count
  5242. + sizeof(bool) * (uint64)module->function_count;
  5243. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  5244. return false;
  5245. }
  5246. module->func_ptrs_compiled =
  5247. (bool *)((uint8 *)module->func_ptrs
  5248. + sizeof(void *) * module->function_count);
  5249. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  5250. if (!module->comp_data) {
  5251. aot_last_error = aot_get_last_error();
  5252. bh_assert(aot_last_error != NULL);
  5253. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5254. return false;
  5255. }
  5256. option.is_jit_mode = true;
  5257. option.opt_level = llvm_jit_options->opt_level;
  5258. option.size_level = llvm_jit_options->size_level;
  5259. option.segue_flags = llvm_jit_options->segue_flags;
  5260. option.quick_invoke_c_api_import =
  5261. llvm_jit_options->quick_invoke_c_api_import;
  5262. #if WASM_ENABLE_BULK_MEMORY != 0
  5263. option.enable_bulk_memory = true;
  5264. #endif
  5265. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  5266. option.enable_bulk_memory_opt = true;
  5267. #endif
  5268. #if WASM_ENABLE_THREAD_MGR != 0
  5269. option.enable_thread_mgr = true;
  5270. #endif
  5271. #if WASM_ENABLE_TAIL_CALL != 0
  5272. option.enable_tail_call = true;
  5273. #endif
  5274. #if WASM_ENABLE_SIMD != 0
  5275. option.enable_simd = true;
  5276. #endif
  5277. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  5278. option.enable_ref_types = true;
  5279. #elif WASM_ENABLE_GC != 0
  5280. option.enable_gc = true;
  5281. #endif
  5282. #if WASM_ENABLE_CALL_INDIRECT_OVERLONG != 0
  5283. option.enable_call_indirect_overlong = true;
  5284. #endif
  5285. option.enable_aux_stack_check = true;
  5286. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  5287. || WASM_ENABLE_AOT_STACK_FRAME != 0
  5288. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  5289. aot_call_stack_features_init_default(&option.call_stack_features);
  5290. #endif
  5291. #if WASM_ENABLE_PERF_PROFILING != 0
  5292. option.enable_perf_profiling = true;
  5293. #endif
  5294. #if WASM_ENABLE_MEMORY_PROFILING != 0
  5295. option.enable_memory_profiling = true;
  5296. option.enable_stack_estimation = true;
  5297. #endif
  5298. #if WASM_ENABLE_SHARED_HEAP != 0
  5299. option.enable_shared_heap = true;
  5300. #endif
  5301. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  5302. if (!module->comp_ctx) {
  5303. aot_last_error = aot_get_last_error();
  5304. bh_assert(aot_last_error != NULL);
  5305. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5306. return false;
  5307. }
  5308. return true;
  5309. }
  5310. static bool
  5311. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  5312. uint32 error_buf_size)
  5313. {
  5314. char *aot_last_error;
  5315. uint32 i;
  5316. if (module->function_count == 0)
  5317. return true;
  5318. if (!aot_compile_wasm(module->comp_ctx)) {
  5319. aot_last_error = aot_get_last_error();
  5320. bh_assert(aot_last_error != NULL);
  5321. set_error_buf(error_buf, error_buf_size, aot_last_error);
  5322. return false;
  5323. }
  5324. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5325. if (module->orcjit_stop_compiling)
  5326. return false;
  5327. #endif
  5328. bh_print_time("Begin to lookup llvm jit functions");
  5329. for (i = 0; i < module->function_count; i++) {
  5330. LLVMOrcJITTargetAddress func_addr = 0;
  5331. LLVMErrorRef error;
  5332. char func_name[48];
  5333. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  5334. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  5335. func_name);
  5336. if (error != LLVMErrorSuccess) {
  5337. char *err_msg = LLVMGetErrorMessage(error);
  5338. set_error_buf_v(error_buf, error_buf_size,
  5339. "failed to compile llvm jit function: %s", err_msg);
  5340. LLVMDisposeErrorMessage(err_msg);
  5341. return false;
  5342. }
  5343. /**
  5344. * No need to lock the func_ptr[func_idx] here as it is basic
  5345. * data type, the load/store for it can be finished by one cpu
  5346. * instruction, and there can be only one cpu instruction
  5347. * loading/storing at the same time.
  5348. */
  5349. module->func_ptrs[i] = (void *)func_addr;
  5350. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5351. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  5352. if (module->orcjit_stop_compiling)
  5353. return false;
  5354. #endif
  5355. }
  5356. bh_print_time("End lookup llvm jit functions");
  5357. return true;
  5358. }
  5359. #endif /* end of WASM_ENABLE_JIT != 0 */
  5360. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5361. && WASM_ENABLE_LAZY_JIT != 0
  5362. static void *
  5363. init_llvm_jit_functions_stage2_callback(void *arg)
  5364. {
  5365. WASMModule *module = (WASMModule *)arg;
  5366. char error_buf[128];
  5367. uint32 error_buf_size = (uint32)sizeof(error_buf);
  5368. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5369. module->orcjit_stop_compiling = true;
  5370. return NULL;
  5371. }
  5372. os_mutex_lock(&module->tierup_wait_lock);
  5373. module->llvm_jit_inited = true;
  5374. os_cond_broadcast(&module->tierup_wait_cond);
  5375. os_mutex_unlock(&module->tierup_wait_lock);
  5376. return NULL;
  5377. }
  5378. #endif
  5379. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5380. /* The callback function to compile jit functions */
  5381. static void *
  5382. orcjit_thread_callback(void *arg)
  5383. {
  5384. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  5385. #if WASM_ENABLE_JIT != 0
  5386. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  5387. #endif
  5388. WASMModule *module = thread_arg->module;
  5389. uint32 group_idx = thread_arg->group_idx;
  5390. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  5391. uint32 func_count = module->function_count;
  5392. uint32 i;
  5393. #if WASM_ENABLE_FAST_JIT != 0
  5394. /* Compile fast jit functions of this group */
  5395. for (i = group_idx; i < func_count; i += group_stride) {
  5396. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  5397. LOG_ERROR("failed to compile fast jit function %u\n", i);
  5398. break;
  5399. }
  5400. if (module->orcjit_stop_compiling) {
  5401. return NULL;
  5402. }
  5403. }
  5404. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5405. os_mutex_lock(&module->tierup_wait_lock);
  5406. module->fast_jit_ready_groups++;
  5407. os_mutex_unlock(&module->tierup_wait_lock);
  5408. #endif
  5409. #endif
  5410. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5411. && WASM_ENABLE_LAZY_JIT != 0
  5412. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  5413. for (i = group_idx; i < func_count;
  5414. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5415. uint32 j;
  5416. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5417. if (i + j * group_stride < func_count) {
  5418. if (!jit_compiler_set_call_to_fast_jit(
  5419. module,
  5420. i + j * group_stride + module->import_function_count)) {
  5421. LOG_ERROR(
  5422. "failed to compile call_to_fast_jit for func %u\n",
  5423. i + j * group_stride + module->import_function_count);
  5424. module->orcjit_stop_compiling = true;
  5425. return NULL;
  5426. }
  5427. }
  5428. if (module->orcjit_stop_compiling) {
  5429. return NULL;
  5430. }
  5431. }
  5432. }
  5433. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5434. fast jit functions are compiled */
  5435. os_mutex_lock(&module->tierup_wait_lock);
  5436. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5437. && module->fast_jit_ready_groups >= group_stride)) {
  5438. os_cond_reltimedwait(&module->tierup_wait_cond,
  5439. &module->tierup_wait_lock, 10000);
  5440. if (module->orcjit_stop_compiling) {
  5441. /* init_llvm_jit_functions_stage2 failed */
  5442. os_mutex_unlock(&module->tierup_wait_lock);
  5443. return NULL;
  5444. }
  5445. }
  5446. os_mutex_unlock(&module->tierup_wait_lock);
  5447. #endif
  5448. #if WASM_ENABLE_JIT != 0
  5449. /* Compile llvm jit functions of this group */
  5450. for (i = group_idx; i < func_count;
  5451. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5452. LLVMOrcJITTargetAddress func_addr = 0;
  5453. LLVMErrorRef error;
  5454. char func_name[48];
  5455. typedef void (*F)(void);
  5456. union {
  5457. F f;
  5458. void *v;
  5459. } u;
  5460. uint32 j;
  5461. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5462. "_wrapper");
  5463. LOG_DEBUG("compile llvm jit func %s", func_name);
  5464. error =
  5465. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5466. if (error != LLVMErrorSuccess) {
  5467. char *err_msg = LLVMGetErrorMessage(error);
  5468. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5469. LLVMDisposeErrorMessage(err_msg);
  5470. break;
  5471. }
  5472. /* Call the jit wrapper function to trigger its compilation, so as
  5473. to compile the actual jit functions, since we add the latter to
  5474. function list in the PartitionFunction callback */
  5475. u.v = (void *)func_addr;
  5476. u.f();
  5477. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5478. if (i + j * group_stride < func_count) {
  5479. module->func_ptrs_compiled[i + j * group_stride] = true;
  5480. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5481. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5482. i + j * group_stride);
  5483. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5484. func_name);
  5485. if (error != LLVMErrorSuccess) {
  5486. char *err_msg = LLVMGetErrorMessage(error);
  5487. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5488. err_msg);
  5489. LLVMDisposeErrorMessage(err_msg);
  5490. /* Ignore current llvm jit func, as its func ptr is
  5491. previous set to call_to_fast_jit, which also works */
  5492. continue;
  5493. }
  5494. jit_compiler_set_llvm_jit_func_ptr(
  5495. module,
  5496. i + j * group_stride + module->import_function_count,
  5497. (void *)func_addr);
  5498. /* Try to switch to call this llvm jit function instead of
  5499. fast jit function from fast jit jitted code */
  5500. jit_compiler_set_call_to_llvm_jit(
  5501. module,
  5502. i + j * group_stride + module->import_function_count);
  5503. #endif
  5504. }
  5505. }
  5506. if (module->orcjit_stop_compiling) {
  5507. break;
  5508. }
  5509. }
  5510. #endif
  5511. return NULL;
  5512. }
  5513. static void
  5514. orcjit_stop_compile_threads(WASMModule *module)
  5515. {
  5516. #if WASM_ENABLE_LAZY_JIT != 0
  5517. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5518. / sizeof(OrcJitThreadArg));
  5519. module->orcjit_stop_compiling = true;
  5520. for (i = 0; i < thread_num; i++) {
  5521. if (module->orcjit_threads[i])
  5522. os_thread_join(module->orcjit_threads[i], NULL);
  5523. }
  5524. #endif
  5525. }
  5526. static bool
  5527. compile_jit_functions(WASMModule *module, char *error_buf,
  5528. uint32 error_buf_size)
  5529. {
  5530. uint32 thread_num =
  5531. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5532. uint32 i, j;
  5533. bh_print_time("Begin to compile jit functions");
  5534. /* Create threads to compile the jit functions */
  5535. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5536. #if WASM_ENABLE_JIT != 0
  5537. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5538. #endif
  5539. module->orcjit_thread_args[i].module = module;
  5540. module->orcjit_thread_args[i].group_idx = i;
  5541. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5542. (void *)&module->orcjit_thread_args[i],
  5543. APP_THREAD_STACK_SIZE_DEFAULT)
  5544. != 0) {
  5545. set_error_buf(error_buf, error_buf_size,
  5546. "create orcjit compile thread failed");
  5547. /* Terminate the threads created */
  5548. module->orcjit_stop_compiling = true;
  5549. for (j = 0; j < i; j++) {
  5550. os_thread_join(module->orcjit_threads[j], NULL);
  5551. }
  5552. return false;
  5553. }
  5554. }
  5555. #if WASM_ENABLE_LAZY_JIT == 0
  5556. /* Wait until all jit functions are compiled for eager mode */
  5557. for (i = 0; i < thread_num; i++) {
  5558. if (module->orcjit_threads[i])
  5559. os_thread_join(module->orcjit_threads[i], NULL);
  5560. }
  5561. #if WASM_ENABLE_FAST_JIT != 0
  5562. /* Ensure all the fast-jit functions are compiled */
  5563. for (i = 0; i < module->function_count; i++) {
  5564. if (!jit_compiler_is_compiled(module,
  5565. i + module->import_function_count)) {
  5566. set_error_buf(error_buf, error_buf_size,
  5567. "failed to compile fast jit function");
  5568. return false;
  5569. }
  5570. }
  5571. #endif
  5572. #if WASM_ENABLE_JIT != 0
  5573. /* Ensure all the llvm-jit functions are compiled */
  5574. for (i = 0; i < module->function_count; i++) {
  5575. if (!module->func_ptrs_compiled[i]) {
  5576. set_error_buf(error_buf, error_buf_size,
  5577. "failed to compile llvm jit function");
  5578. return false;
  5579. }
  5580. }
  5581. #endif
  5582. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5583. bh_print_time("End compile jit functions");
  5584. return true;
  5585. }
  5586. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5587. static bool
  5588. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5589. uint32 cur_func_idx, char *error_buf,
  5590. uint32 error_buf_size);
  5591. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5592. void **
  5593. wasm_interp_get_handle_table(void);
  5594. static void **handle_table;
  5595. #endif
  5596. static bool
  5597. load_from_sections(WASMModule *module, WASMSection *sections,
  5598. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5599. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5600. {
  5601. WASMExport *export;
  5602. WASMSection *section = sections;
  5603. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5604. *buf_func = NULL, *buf_func_end = NULL;
  5605. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5606. WASMGlobal *aux_stack_top_global = NULL, *global;
  5607. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5608. aux_stack_top = (uint64)-1LL;
  5609. uint32 global_index, func_index, i;
  5610. uint32 aux_data_end_global_index = (uint32)-1;
  5611. uint32 aux_heap_base_global_index = (uint32)-1;
  5612. WASMFuncType *func_type;
  5613. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5614. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5615. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5616. #if WASM_ENABLE_BULK_MEMORY != 0
  5617. bool has_datacount_section = false;
  5618. #endif
  5619. /* Find code and function sections if have */
  5620. while (section) {
  5621. if (section->section_type == SECTION_TYPE_CODE) {
  5622. buf_code = section->section_body;
  5623. buf_code_end = buf_code + section->section_body_size;
  5624. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5625. module->buf_code = (uint8 *)buf_code;
  5626. module->buf_code_size = section->section_body_size;
  5627. #endif
  5628. }
  5629. else if (section->section_type == SECTION_TYPE_FUNC) {
  5630. buf_func = section->section_body;
  5631. buf_func_end = buf_func + section->section_body_size;
  5632. }
  5633. section = section->next;
  5634. }
  5635. section = sections;
  5636. while (section) {
  5637. buf = section->section_body;
  5638. buf_end = buf + section->section_body_size;
  5639. switch (section->section_type) {
  5640. case SECTION_TYPE_USER:
  5641. /* unsupported user section, ignore it. */
  5642. if (!load_user_section(buf, buf_end, module,
  5643. reuse_const_strings, error_buf,
  5644. error_buf_size))
  5645. return false;
  5646. break;
  5647. case SECTION_TYPE_TYPE:
  5648. if (!load_type_section(buf, buf_end, module, error_buf,
  5649. error_buf_size))
  5650. return false;
  5651. break;
  5652. case SECTION_TYPE_IMPORT:
  5653. if (!load_import_section(buf, buf_end, module,
  5654. reuse_const_strings, no_resolve,
  5655. error_buf, error_buf_size))
  5656. return false;
  5657. break;
  5658. case SECTION_TYPE_FUNC:
  5659. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5660. module, error_buf, error_buf_size))
  5661. return false;
  5662. break;
  5663. case SECTION_TYPE_TABLE:
  5664. if (!load_table_section(buf, buf_end, module, error_buf,
  5665. error_buf_size))
  5666. return false;
  5667. break;
  5668. case SECTION_TYPE_MEMORY:
  5669. if (!load_memory_section(buf, buf_end, module, error_buf,
  5670. error_buf_size))
  5671. return false;
  5672. break;
  5673. #if WASM_ENABLE_TAGS != 0
  5674. case SECTION_TYPE_TAG:
  5675. /* load tag declaration section */
  5676. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5677. module, error_buf, error_buf_size))
  5678. return false;
  5679. break;
  5680. #endif
  5681. case SECTION_TYPE_GLOBAL:
  5682. if (!load_global_section(buf, buf_end, module, error_buf,
  5683. error_buf_size))
  5684. return false;
  5685. break;
  5686. case SECTION_TYPE_EXPORT:
  5687. if (!load_export_section(buf, buf_end, module,
  5688. reuse_const_strings, error_buf,
  5689. error_buf_size))
  5690. return false;
  5691. break;
  5692. case SECTION_TYPE_START:
  5693. if (!load_start_section(buf, buf_end, module, error_buf,
  5694. error_buf_size))
  5695. return false;
  5696. break;
  5697. case SECTION_TYPE_ELEM:
  5698. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5699. error_buf_size))
  5700. return false;
  5701. break;
  5702. case SECTION_TYPE_CODE:
  5703. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5704. module, error_buf, error_buf_size))
  5705. return false;
  5706. break;
  5707. case SECTION_TYPE_DATA:
  5708. if (!load_data_segment_section(buf, buf_end, module,
  5709. #if WASM_ENABLE_BULK_MEMORY != 0
  5710. has_datacount_section,
  5711. #endif
  5712. clone_data_seg, error_buf,
  5713. error_buf_size))
  5714. return false;
  5715. break;
  5716. #if WASM_ENABLE_BULK_MEMORY != 0
  5717. case SECTION_TYPE_DATACOUNT:
  5718. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5719. error_buf_size))
  5720. return false;
  5721. has_datacount_section = true;
  5722. break;
  5723. #endif
  5724. #if WASM_ENABLE_STRINGREF != 0
  5725. case SECTION_TYPE_STRINGREF:
  5726. if (!load_stringref_section(buf, buf_end, module,
  5727. reuse_const_strings, error_buf,
  5728. error_buf_size))
  5729. return false;
  5730. break;
  5731. #endif
  5732. default:
  5733. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5734. return false;
  5735. }
  5736. section = section->next;
  5737. }
  5738. #if WASM_ENABLE_BULK_MEMORY != 0
  5739. if (!check_data_count_consistency(
  5740. has_datacount_section, module->data_seg_count1,
  5741. module->data_seg_count, error_buf, error_buf_size)) {
  5742. return false;
  5743. }
  5744. #endif
  5745. module->aux_data_end_global_index = (uint32)-1;
  5746. module->aux_heap_base_global_index = (uint32)-1;
  5747. module->aux_stack_top_global_index = (uint32)-1;
  5748. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5749. export = module->exports;
  5750. for (i = 0; i < module->export_count; i++, export ++) {
  5751. if (export->kind == EXPORT_KIND_GLOBAL) {
  5752. if (!strcmp(export->name, "__heap_base")) {
  5753. if (export->index < module->import_global_count) {
  5754. LOG_DEBUG("Skip the process if __heap_base is imported "
  5755. "instead of being a local global");
  5756. continue;
  5757. }
  5758. /* only process linker-generated symbols */
  5759. global_index = export->index - module->import_global_count;
  5760. global = module->globals + global_index;
  5761. if (global->type.val_type == VALUE_TYPE_I32
  5762. && !global->type.is_mutable
  5763. && global->init_expr.init_expr_type
  5764. == INIT_EXPR_TYPE_I32_CONST) {
  5765. aux_heap_base_global = global;
  5766. aux_heap_base =
  5767. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5768. aux_heap_base_global_index = export->index;
  5769. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5770. aux_heap_base);
  5771. }
  5772. }
  5773. else if (!strcmp(export->name, "__data_end")) {
  5774. if (export->index < module->import_global_count) {
  5775. LOG_DEBUG("Skip the process if __data_end is imported "
  5776. "instead of being a local global");
  5777. continue;
  5778. }
  5779. /* only process linker-generated symbols */
  5780. global_index = export->index - module->import_global_count;
  5781. global = module->globals + global_index;
  5782. if (global->type.val_type == VALUE_TYPE_I32
  5783. && !global->type.is_mutable
  5784. && global->init_expr.init_expr_type
  5785. == INIT_EXPR_TYPE_I32_CONST) {
  5786. aux_data_end_global = global;
  5787. aux_data_end =
  5788. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5789. aux_data_end_global_index = export->index;
  5790. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5791. aux_data_end);
  5792. aux_data_end = align_uint64(aux_data_end, 16);
  5793. }
  5794. }
  5795. /* For module compiled with -pthread option, the global is:
  5796. [0] stack_top <-- 0
  5797. [1] tls_pointer
  5798. [2] tls_size
  5799. [3] data_end <-- 3
  5800. [4] global_base
  5801. [5] heap_base <-- 5
  5802. [6] dso_handle
  5803. For module compiled without -pthread option:
  5804. [0] stack_top <-- 0
  5805. [1] data_end <-- 1
  5806. [2] global_base
  5807. [3] heap_base <-- 3
  5808. [4] dso_handle
  5809. */
  5810. if (aux_data_end_global && aux_heap_base_global
  5811. && aux_data_end <= aux_heap_base) {
  5812. module->aux_data_end_global_index = aux_data_end_global_index;
  5813. module->aux_data_end = aux_data_end;
  5814. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5815. module->aux_heap_base = aux_heap_base;
  5816. /* Resolve aux stack top global */
  5817. for (global_index = 0; global_index < module->global_count;
  5818. global_index++) {
  5819. global = module->globals + global_index;
  5820. if (global->type.is_mutable /* heap_base and data_end is
  5821. not mutable */
  5822. && global->type.val_type == VALUE_TYPE_I32
  5823. && global->init_expr.init_expr_type
  5824. == INIT_EXPR_TYPE_I32_CONST
  5825. && (uint64)(uint32)global->init_expr.u.unary.v.i32
  5826. <= aux_heap_base) {
  5827. aux_stack_top_global = global;
  5828. aux_stack_top =
  5829. (uint64)(uint32)global->init_expr.u.unary.v.i32;
  5830. module->aux_stack_top_global_index =
  5831. module->import_global_count + global_index;
  5832. module->aux_stack_bottom = aux_stack_top;
  5833. module->aux_stack_size =
  5834. aux_stack_top > aux_data_end
  5835. ? (uint32)(aux_stack_top - aux_data_end)
  5836. : (uint32)aux_stack_top;
  5837. LOG_VERBOSE(
  5838. "Found aux stack top global, value: %" PRIu64 ", "
  5839. "global index: %d, stack size: %d",
  5840. aux_stack_top, global_index,
  5841. module->aux_stack_size);
  5842. break;
  5843. }
  5844. }
  5845. if (!aux_stack_top_global) {
  5846. /* Auxiliary stack global isn't found, it must be unused
  5847. in the wasm app, as if it is used, the global must be
  5848. defined. Here we set it to __heap_base global and set
  5849. its size to 0. */
  5850. aux_stack_top_global = aux_heap_base_global;
  5851. aux_stack_top = aux_heap_base;
  5852. module->aux_stack_top_global_index =
  5853. module->aux_heap_base_global_index;
  5854. module->aux_stack_bottom = aux_stack_top;
  5855. module->aux_stack_size = 0;
  5856. }
  5857. break;
  5858. }
  5859. }
  5860. }
  5861. module->malloc_function = (uint32)-1;
  5862. module->free_function = (uint32)-1;
  5863. module->retain_function = (uint32)-1;
  5864. /* Resolve malloc/free function exported by wasm module */
  5865. #if WASM_ENABLE_MEMORY64 != 0
  5866. if (has_module_memory64(module))
  5867. malloc_free_io_type = VALUE_TYPE_I64;
  5868. #endif
  5869. export = module->exports;
  5870. for (i = 0; i < module->export_count; i++, export ++) {
  5871. if (export->kind == EXPORT_KIND_FUNC) {
  5872. if (!strcmp(export->name, "malloc")
  5873. && export->index >= module->import_function_count) {
  5874. func_index = export->index - module->import_function_count;
  5875. func_type = module->functions[func_index]->func_type;
  5876. if (func_type->param_count == 1 && func_type->result_count == 1
  5877. && func_type->types[0] == malloc_free_io_type
  5878. && func_type->types[1] == malloc_free_io_type) {
  5879. bh_assert(module->malloc_function == (uint32)-1);
  5880. module->malloc_function = export->index;
  5881. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5882. export->name, export->index);
  5883. }
  5884. }
  5885. else if (!strcmp(export->name, "__new")
  5886. && export->index >= module->import_function_count) {
  5887. /* __new && __pin for AssemblyScript */
  5888. func_index = export->index - module->import_function_count;
  5889. func_type = module->functions[func_index]->func_type;
  5890. if (func_type->param_count == 2 && func_type->result_count == 1
  5891. && func_type->types[0] == malloc_free_io_type
  5892. && func_type->types[1] == VALUE_TYPE_I32
  5893. && func_type->types[2] == malloc_free_io_type) {
  5894. uint32 j;
  5895. WASMExport *export_tmp;
  5896. bh_assert(module->malloc_function == (uint32)-1);
  5897. module->malloc_function = export->index;
  5898. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5899. export->name, export->index);
  5900. /* resolve retain function.
  5901. If not found, reset malloc function index */
  5902. export_tmp = module->exports;
  5903. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5904. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5905. && (!strcmp(export_tmp->name, "__retain")
  5906. || (!strcmp(export_tmp->name, "__pin")))
  5907. && (export_tmp->index
  5908. >= module->import_function_count)) {
  5909. func_index = export_tmp->index
  5910. - module->import_function_count;
  5911. func_type =
  5912. module->functions[func_index]->func_type;
  5913. if (func_type->param_count == 1
  5914. && func_type->result_count == 1
  5915. && func_type->types[0] == malloc_free_io_type
  5916. && func_type->types[1] == malloc_free_io_type) {
  5917. bh_assert(module->retain_function
  5918. == (uint32)-1);
  5919. module->retain_function = export_tmp->index;
  5920. LOG_VERBOSE("Found retain function, name: %s, "
  5921. "index: %u",
  5922. export_tmp->name,
  5923. export_tmp->index);
  5924. break;
  5925. }
  5926. }
  5927. }
  5928. if (j == module->export_count) {
  5929. module->malloc_function = (uint32)-1;
  5930. LOG_VERBOSE("Can't find retain function,"
  5931. "reset malloc function index to -1");
  5932. }
  5933. }
  5934. }
  5935. else if (((!strcmp(export->name, "free"))
  5936. || (!strcmp(export->name, "__release"))
  5937. || (!strcmp(export->name, "__unpin")))
  5938. && export->index >= module->import_function_count) {
  5939. func_index = export->index - module->import_function_count;
  5940. func_type = module->functions[func_index]->func_type;
  5941. if (func_type->param_count == 1 && func_type->result_count == 0
  5942. && func_type->types[0] == malloc_free_io_type) {
  5943. bh_assert(module->free_function == (uint32)-1);
  5944. module->free_function = export->index;
  5945. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5946. export->name, export->index);
  5947. }
  5948. }
  5949. }
  5950. }
  5951. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5952. handle_table = wasm_interp_get_handle_table();
  5953. #endif
  5954. for (i = 0; i < module->function_count; i++) {
  5955. WASMFunction *func = module->functions[i];
  5956. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5957. error_buf_size)) {
  5958. return false;
  5959. }
  5960. if (i == module->function_count - 1
  5961. && func->code + func->code_size != buf_code_end) {
  5962. set_error_buf(error_buf, error_buf_size,
  5963. "code section size mismatch");
  5964. return false;
  5965. }
  5966. }
  5967. if (!module->possible_memory_grow) {
  5968. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5969. if (aux_data_end_global && aux_heap_base_global
  5970. && aux_stack_top_global) {
  5971. uint64 init_memory_size;
  5972. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5973. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5974. * valid range of uint32 */
  5975. if (shrunk_memory_size <= UINT32_MAX) {
  5976. if (module->import_memory_count) {
  5977. WASMMemoryImport *memory_import =
  5978. &module->import_memories[0].u.memory;
  5979. init_memory_size =
  5980. (uint64)memory_import->mem_type.num_bytes_per_page
  5981. * memory_import->mem_type.init_page_count;
  5982. if (shrunk_memory_size <= init_memory_size) {
  5983. /* Reset memory info to decrease memory usage */
  5984. memory_import->mem_type.num_bytes_per_page =
  5985. (uint32)shrunk_memory_size;
  5986. memory_import->mem_type.init_page_count = 1;
  5987. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5988. shrunk_memory_size);
  5989. }
  5990. }
  5991. if (module->memory_count) {
  5992. WASMMemory *memory = &module->memories[0];
  5993. init_memory_size = (uint64)memory->num_bytes_per_page
  5994. * memory->init_page_count;
  5995. if (shrunk_memory_size <= init_memory_size) {
  5996. /* Reset memory info to decrease memory usage */
  5997. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5998. memory->init_page_count = 1;
  5999. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  6000. shrunk_memory_size);
  6001. }
  6002. }
  6003. }
  6004. }
  6005. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  6006. #if WASM_ENABLE_MULTI_MODULE == 0
  6007. if (module->import_memory_count) {
  6008. WASMMemoryImport *memory_import =
  6009. &module->import_memories[0].u.memory;
  6010. /* Only resize the memory to one big page if num_bytes_per_page is
  6011. * in valid range of uint32 */
  6012. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  6013. memory_import->mem_type.num_bytes_per_page *=
  6014. memory_import->mem_type.init_page_count;
  6015. if (memory_import->mem_type.init_page_count > 0)
  6016. memory_import->mem_type.init_page_count =
  6017. memory_import->mem_type.max_page_count = 1;
  6018. else
  6019. memory_import->mem_type.init_page_count =
  6020. memory_import->mem_type.max_page_count = 0;
  6021. }
  6022. }
  6023. if (module->memory_count) {
  6024. WASMMemory *memory = &module->memories[0];
  6025. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  6026. * valid range of uint32 */
  6027. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  6028. memory->num_bytes_per_page *= memory->init_page_count;
  6029. if (memory->init_page_count > 0)
  6030. memory->init_page_count = memory->max_page_count = 1;
  6031. else
  6032. memory->init_page_count = memory->max_page_count = 0;
  6033. }
  6034. }
  6035. #endif
  6036. }
  6037. #if WASM_ENABLE_MEMORY64 != 0
  6038. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  6039. false))
  6040. return false;
  6041. #endif
  6042. calculate_global_data_offset(module);
  6043. #if WASM_ENABLE_FAST_JIT != 0
  6044. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  6045. return false;
  6046. }
  6047. #endif
  6048. #if WASM_ENABLE_JIT != 0
  6049. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  6050. return false;
  6051. }
  6052. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  6053. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  6054. return false;
  6055. }
  6056. #else
  6057. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  6058. thread fast jit execution, since applying llvm optimizations in
  6059. aot_compile_wasm may cost a lot of time.
  6060. Create thread with enough native stack to apply llvm optimizations */
  6061. if (os_thread_create(&module->llvm_jit_init_thread,
  6062. init_llvm_jit_functions_stage2_callback,
  6063. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  6064. != 0) {
  6065. set_error_buf(error_buf, error_buf_size,
  6066. "create orcjit compile thread failed");
  6067. return false;
  6068. }
  6069. #endif
  6070. #endif
  6071. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6072. /* Create threads to compile the jit functions */
  6073. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  6074. return false;
  6075. }
  6076. #endif
  6077. #if WASM_ENABLE_MEMORY_TRACING != 0
  6078. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  6079. #endif
  6080. return true;
  6081. }
  6082. static WASMModule *
  6083. create_module(char *name, char *error_buf, uint32 error_buf_size)
  6084. {
  6085. WASMModule *module =
  6086. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  6087. bh_list_status ret;
  6088. if (!module) {
  6089. return NULL;
  6090. }
  6091. module->module_type = Wasm_Module_Bytecode;
  6092. /* Set start_function to -1, means no start function */
  6093. module->start_function = (uint32)-1;
  6094. module->name = name;
  6095. module->is_binary_freeable = false;
  6096. #if WASM_ENABLE_FAST_INTERP == 0
  6097. module->br_table_cache_list = &module->br_table_cache_list_head;
  6098. ret = bh_list_init(module->br_table_cache_list);
  6099. bh_assert(ret == BH_LIST_SUCCESS);
  6100. #endif
  6101. #if WASM_ENABLE_MULTI_MODULE != 0
  6102. module->import_module_list = &module->import_module_list_head;
  6103. ret = bh_list_init(module->import_module_list);
  6104. bh_assert(ret == BH_LIST_SUCCESS);
  6105. #endif
  6106. #if WASM_ENABLE_DEBUG_INTERP != 0
  6107. ret = bh_list_init(&module->fast_opcode_list);
  6108. bh_assert(ret == BH_LIST_SUCCESS);
  6109. #endif
  6110. #if WASM_ENABLE_GC != 0
  6111. if (!(module->ref_type_set =
  6112. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  6113. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  6114. goto fail1;
  6115. }
  6116. if (os_mutex_init(&module->rtt_type_lock)) {
  6117. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  6118. goto fail2;
  6119. }
  6120. #endif
  6121. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6122. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6123. && WASM_ENABLE_LAZY_JIT != 0)
  6124. if (os_mutex_init(&module->instance_list_lock) != 0) {
  6125. set_error_buf(error_buf, error_buf_size,
  6126. "init instance list lock failed");
  6127. goto fail3;
  6128. }
  6129. #endif
  6130. #if WASM_ENABLE_LIBC_WASI != 0
  6131. wasi_args_set_defaults(&module->wasi_args);
  6132. #endif /* WASM_ENABLE_LIBC_WASI != 0 */
  6133. (void)ret;
  6134. return module;
  6135. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6136. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  6137. && WASM_ENABLE_LAZY_JIT != 0)
  6138. fail3:
  6139. #endif
  6140. #if WASM_ENABLE_GC != 0
  6141. os_mutex_destroy(&module->rtt_type_lock);
  6142. fail2:
  6143. bh_hash_map_destroy(module->ref_type_set);
  6144. fail1:
  6145. #endif
  6146. wasm_runtime_free(module);
  6147. return NULL;
  6148. }
  6149. #if WASM_ENABLE_DEBUG_INTERP != 0
  6150. static bool
  6151. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  6152. uint32 error_buf_size)
  6153. {
  6154. WASMFastOPCodeNode *fast_op =
  6155. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  6156. if (fast_op) {
  6157. fast_op->offset = pos - module->load_addr;
  6158. fast_op->orig_op = orig_op;
  6159. bh_list_insert(&module->fast_opcode_list, fast_op);
  6160. }
  6161. return fast_op ? true : false;
  6162. }
  6163. #endif
  6164. WASMModule *
  6165. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  6166. uint32 error_buf_size)
  6167. {
  6168. WASMModule *module = create_module("", error_buf, error_buf_size);
  6169. if (!module)
  6170. return NULL;
  6171. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  6172. error_buf_size)) {
  6173. wasm_loader_unload(module);
  6174. return NULL;
  6175. }
  6176. LOG_VERBOSE("Load module from sections success.\n");
  6177. return module;
  6178. }
  6179. static void
  6180. destroy_sections(WASMSection *section_list)
  6181. {
  6182. WASMSection *section = section_list, *next;
  6183. while (section) {
  6184. next = section->next;
  6185. wasm_runtime_free(section);
  6186. section = next;
  6187. }
  6188. }
  6189. /* clang-format off */
  6190. static uint8 section_ids[] = {
  6191. SECTION_TYPE_USER,
  6192. SECTION_TYPE_TYPE,
  6193. SECTION_TYPE_IMPORT,
  6194. SECTION_TYPE_FUNC,
  6195. SECTION_TYPE_TABLE,
  6196. SECTION_TYPE_MEMORY,
  6197. #if WASM_ENABLE_TAGS != 0
  6198. SECTION_TYPE_TAG,
  6199. #endif
  6200. #if WASM_ENABLE_STRINGREF != 0
  6201. /* must immediately precede the global section,
  6202. or where the global section would be */
  6203. SECTION_TYPE_STRINGREF,
  6204. #endif
  6205. SECTION_TYPE_GLOBAL,
  6206. SECTION_TYPE_EXPORT,
  6207. SECTION_TYPE_START,
  6208. SECTION_TYPE_ELEM,
  6209. #if WASM_ENABLE_BULK_MEMORY != 0
  6210. SECTION_TYPE_DATACOUNT,
  6211. #endif
  6212. SECTION_TYPE_CODE,
  6213. SECTION_TYPE_DATA
  6214. };
  6215. /* clang-format on */
  6216. static uint8
  6217. get_section_index(uint8 section_type)
  6218. {
  6219. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  6220. for (uint8 i = 0; i < max_id; i++) {
  6221. if (section_type == section_ids[i])
  6222. return i;
  6223. }
  6224. return (uint8)-1;
  6225. }
  6226. static bool
  6227. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  6228. char *error_buf, uint32 error_buf_size)
  6229. {
  6230. WASMSection *section_list_end = NULL, *section;
  6231. const uint8 *p = buf, *p_end = buf + size;
  6232. uint8 section_type, section_index, last_section_index = (uint8)-1;
  6233. uint32 section_size;
  6234. bh_assert(!*p_section_list);
  6235. p += 8;
  6236. while (p < p_end) {
  6237. CHECK_BUF(p, p_end, 1);
  6238. section_type = read_uint8(p);
  6239. section_index = get_section_index(section_type);
  6240. if (section_index != (uint8)-1) {
  6241. if (section_type != SECTION_TYPE_USER) {
  6242. /* Custom sections may be inserted at any place,
  6243. while other sections must occur at most once
  6244. and in prescribed order. */
  6245. if (last_section_index != (uint8)-1
  6246. && (section_index <= last_section_index)) {
  6247. set_error_buf(error_buf, error_buf_size,
  6248. "unexpected content after last section or "
  6249. "junk after last section");
  6250. return false;
  6251. }
  6252. last_section_index = section_index;
  6253. }
  6254. read_leb_uint32(p, p_end, section_size);
  6255. CHECK_BUF1(p, p_end, section_size);
  6256. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  6257. error_buf_size))) {
  6258. return false;
  6259. }
  6260. section->section_type = section_type;
  6261. section->section_body = (uint8 *)p;
  6262. section->section_body_size = section_size;
  6263. if (!section_list_end)
  6264. *p_section_list = section_list_end = section;
  6265. else {
  6266. section_list_end->next = section;
  6267. section_list_end = section;
  6268. }
  6269. p += section_size;
  6270. }
  6271. else {
  6272. set_error_buf(error_buf, error_buf_size, "invalid section id");
  6273. return false;
  6274. }
  6275. }
  6276. return true;
  6277. fail:
  6278. return false;
  6279. }
  6280. static void
  6281. exchange32(uint8 *p_data)
  6282. {
  6283. uint8 value = *p_data;
  6284. *p_data = *(p_data + 3);
  6285. *(p_data + 3) = value;
  6286. value = *(p_data + 1);
  6287. *(p_data + 1) = *(p_data + 2);
  6288. *(p_data + 2) = value;
  6289. }
  6290. static union {
  6291. int a;
  6292. char b;
  6293. } __ue = { .a = 1 };
  6294. #define is_little_endian() (__ue.b == 1)
  6295. static bool
  6296. load(const uint8 *buf, uint32 size, WASMModule *module,
  6297. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  6298. uint32 error_buf_size)
  6299. {
  6300. const uint8 *buf_end = buf + size;
  6301. const uint8 *p = buf, *p_end = buf_end;
  6302. uint32 magic_number, version;
  6303. WASMSection *section_list = NULL;
  6304. CHECK_BUF1(p, p_end, sizeof(uint32));
  6305. magic_number = read_uint32(p);
  6306. if (!is_little_endian())
  6307. exchange32((uint8 *)&magic_number);
  6308. if (magic_number != WASM_MAGIC_NUMBER) {
  6309. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  6310. return false;
  6311. }
  6312. CHECK_BUF1(p, p_end, sizeof(uint32));
  6313. version = read_uint32(p);
  6314. if (!is_little_endian())
  6315. exchange32((uint8 *)&version);
  6316. if (version != WASM_CURRENT_VERSION) {
  6317. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  6318. return false;
  6319. }
  6320. module->package_version = version;
  6321. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  6322. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  6323. no_resolve, error_buf, error_buf_size)) {
  6324. destroy_sections(section_list);
  6325. return false;
  6326. }
  6327. destroy_sections(section_list);
  6328. return true;
  6329. fail:
  6330. return false;
  6331. }
  6332. #if WASM_ENABLE_LIBC_WASI != 0
  6333. /**
  6334. * refer to
  6335. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  6336. */
  6337. static bool
  6338. check_wasi_abi_compatibility(const WASMModule *module,
  6339. #if WASM_ENABLE_MULTI_MODULE != 0
  6340. bool main_module,
  6341. #endif
  6342. char *error_buf, uint32 error_buf_size)
  6343. {
  6344. /**
  6345. * be careful with:
  6346. * wasi compatible modules(command/reactor) which don't import any wasi
  6347. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  6348. * reactor can depend on nothing. At the same time, each has its own entry
  6349. * point.
  6350. *
  6351. * observations:
  6352. * - clang always injects `_start` into a command
  6353. * - clang always injects `_initialize` into a reactor
  6354. * - `iwasm -f` allows to run a function in the reactor
  6355. *
  6356. * strong assumptions:
  6357. * - no one will define either `_start` or `_initialize` on purpose
  6358. * - `_start` should always be `void _start(void)`
  6359. * - `_initialize` should always be `void _initialize(void)`
  6360. *
  6361. */
  6362. /* clang-format off */
  6363. /**
  6364. *
  6365. * | | import_wasi_api True | | import_wasi_api False | |
  6366. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  6367. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  6368. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  6369. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  6370. */
  6371. /* clang-format on */
  6372. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  6373. uint32 import_function_count = module->import_function_count;
  6374. WASMFuncType *func_type;
  6375. /* (func (export "_start") (...) */
  6376. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  6377. error_buf, error_buf_size);
  6378. if (start) {
  6379. if (start->index < import_function_count) {
  6380. set_error_buf(
  6381. error_buf, error_buf_size,
  6382. "the builtin _start function can not be an import function");
  6383. return false;
  6384. }
  6385. func_type =
  6386. module->functions[start->index - import_function_count]->func_type;
  6387. if (func_type->param_count || func_type->result_count) {
  6388. set_error_buf(error_buf, error_buf_size,
  6389. "the signature of builtin _start function is wrong");
  6390. return false;
  6391. }
  6392. }
  6393. else {
  6394. /* (func (export "_initialize") (...) */
  6395. initialize =
  6396. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  6397. error_buf, error_buf_size);
  6398. if (initialize) {
  6399. if (initialize->index < import_function_count) {
  6400. set_error_buf(error_buf, error_buf_size,
  6401. "the builtin _initialize function can not be an "
  6402. "import function");
  6403. return false;
  6404. }
  6405. func_type =
  6406. module->functions[initialize->index - import_function_count]
  6407. ->func_type;
  6408. if (func_type->param_count || func_type->result_count) {
  6409. set_error_buf(
  6410. error_buf, error_buf_size,
  6411. "the signature of builtin _initialize function is wrong");
  6412. return false;
  6413. }
  6414. }
  6415. }
  6416. /* filter out non-wasi compatible modules */
  6417. if (!module->import_wasi_api && !start && !initialize) {
  6418. return true;
  6419. }
  6420. /* should have one at least */
  6421. if (module->import_wasi_api && !start && !initialize) {
  6422. LOG_WARNING("warning: a module with WASI apis should be either "
  6423. "a command or a reactor");
  6424. }
  6425. /*
  6426. * there is at least one of `_start` and `_initialize` in below cases.
  6427. * according to the assumption, they should be all wasi compatible
  6428. */
  6429. #if WASM_ENABLE_MULTI_MODULE != 0
  6430. /* filter out commands (with `_start`) cases */
  6431. if (start && !main_module) {
  6432. set_error_buf(
  6433. error_buf, error_buf_size,
  6434. "a command (with _start function) can not be a sub-module");
  6435. return false;
  6436. }
  6437. #endif
  6438. /*
  6439. * it is ok a reactor acts as a main module,
  6440. * so skip the check about (with `_initialize`)
  6441. */
  6442. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6443. error_buf, error_buf_size);
  6444. if (!memory
  6445. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6446. /*
  6447. * with wasi-threads, it's still an open question if a memory
  6448. * should be exported.
  6449. *
  6450. * https://github.com/WebAssembly/wasi-threads/issues/22
  6451. * https://github.com/WebAssembly/WASI/issues/502
  6452. *
  6453. * Note: this code assumes the number of memories is at most 1.
  6454. */
  6455. && module->import_memory_count == 0
  6456. #endif
  6457. ) {
  6458. set_error_buf(error_buf, error_buf_size,
  6459. "a module with WASI apis must export memory by default");
  6460. return false;
  6461. }
  6462. return true;
  6463. }
  6464. #endif
  6465. WASMModule *
  6466. wasm_loader_load(uint8 *buf, uint32 size,
  6467. #if WASM_ENABLE_MULTI_MODULE != 0
  6468. bool main_module,
  6469. #endif
  6470. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6471. {
  6472. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6473. if (!module) {
  6474. return NULL;
  6475. }
  6476. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6477. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6478. module->load_addr = (uint8 *)buf;
  6479. module->load_size = size;
  6480. #endif
  6481. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6482. error_buf, error_buf_size)) {
  6483. goto fail;
  6484. }
  6485. #if WASM_ENABLE_LIBC_WASI != 0
  6486. /* Check the WASI application ABI */
  6487. if (!check_wasi_abi_compatibility(module,
  6488. #if WASM_ENABLE_MULTI_MODULE != 0
  6489. main_module,
  6490. #endif
  6491. error_buf, error_buf_size)) {
  6492. goto fail;
  6493. }
  6494. #endif
  6495. LOG_VERBOSE("Load module success.\n");
  6496. return module;
  6497. fail:
  6498. wasm_loader_unload(module);
  6499. return NULL;
  6500. }
  6501. void
  6502. wasm_loader_unload(WASMModule *module)
  6503. {
  6504. uint32 i;
  6505. if (!module)
  6506. return;
  6507. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6508. && WASM_ENABLE_LAZY_JIT != 0
  6509. module->orcjit_stop_compiling = true;
  6510. if (module->llvm_jit_init_thread)
  6511. os_thread_join(module->llvm_jit_init_thread, NULL);
  6512. #endif
  6513. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6514. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6515. module internal data after they were freed */
  6516. orcjit_stop_compile_threads(module);
  6517. #endif
  6518. #if WASM_ENABLE_JIT != 0
  6519. if (module->func_ptrs)
  6520. wasm_runtime_free(module->func_ptrs);
  6521. if (module->comp_ctx)
  6522. aot_destroy_comp_context(module->comp_ctx);
  6523. if (module->comp_data)
  6524. aot_destroy_comp_data(module->comp_data);
  6525. #endif
  6526. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6527. && WASM_ENABLE_LAZY_JIT != 0
  6528. if (module->tierup_wait_lock_inited) {
  6529. os_mutex_destroy(&module->tierup_wait_lock);
  6530. os_cond_destroy(&module->tierup_wait_cond);
  6531. }
  6532. #endif
  6533. if (module->imports)
  6534. wasm_runtime_free(module->imports);
  6535. if (module->functions) {
  6536. for (i = 0; i < module->function_count; i++) {
  6537. if (module->functions[i]) {
  6538. if (module->functions[i]->local_offsets)
  6539. wasm_runtime_free(module->functions[i]->local_offsets);
  6540. #if WASM_ENABLE_FAST_INTERP != 0
  6541. if (module->functions[i]->code_compiled)
  6542. wasm_runtime_free(module->functions[i]->code_compiled);
  6543. if (module->functions[i]->consts)
  6544. wasm_runtime_free(module->functions[i]->consts);
  6545. #endif
  6546. #if WASM_ENABLE_FAST_JIT != 0
  6547. if (module->functions[i]->fast_jit_jitted_code) {
  6548. jit_code_cache_free(
  6549. module->functions[i]->fast_jit_jitted_code);
  6550. }
  6551. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6552. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6553. jit_code_cache_free(
  6554. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6555. }
  6556. #endif
  6557. #endif
  6558. #if WASM_ENABLE_GC != 0
  6559. if (module->functions[i]->local_ref_type_maps) {
  6560. wasm_runtime_free(
  6561. module->functions[i]->local_ref_type_maps);
  6562. }
  6563. #endif
  6564. wasm_runtime_free(module->functions[i]);
  6565. }
  6566. }
  6567. wasm_runtime_free(module->functions);
  6568. }
  6569. if (module->tables) {
  6570. #if WASM_ENABLE_GC != 0
  6571. for (i = 0; i < module->table_count; i++) {
  6572. destroy_init_expr(module, &module->tables[i].init_expr);
  6573. }
  6574. #endif
  6575. wasm_runtime_free(module->tables);
  6576. }
  6577. if (module->memories)
  6578. wasm_runtime_free(module->memories);
  6579. if (module->globals) {
  6580. #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6581. for (i = 0; i < module->global_count; i++) {
  6582. destroy_init_expr(module, &module->globals[i].init_expr);
  6583. }
  6584. #endif
  6585. wasm_runtime_free(module->globals);
  6586. }
  6587. #if WASM_ENABLE_TAGS != 0
  6588. if (module->tags) {
  6589. for (i = 0; i < module->tag_count; i++) {
  6590. if (module->tags[i])
  6591. wasm_runtime_free(module->tags[i]);
  6592. }
  6593. wasm_runtime_free(module->tags);
  6594. }
  6595. #endif
  6596. if (module->exports)
  6597. wasm_runtime_free(module->exports);
  6598. if (module->table_segments) {
  6599. for (i = 0; i < module->table_seg_count; i++) {
  6600. if (module->table_segments[i].init_values) {
  6601. #if WASM_ENABLE_GC != 0
  6602. uint32 j;
  6603. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6604. destroy_init_expr(
  6605. module, &module->table_segments[i].init_values[j]);
  6606. }
  6607. #endif
  6608. wasm_runtime_free(module->table_segments[i].init_values);
  6609. }
  6610. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6611. destroy_init_expr(module, &module->table_segments[i].base_offset);
  6612. #endif
  6613. }
  6614. wasm_runtime_free(module->table_segments);
  6615. }
  6616. if (module->data_segments) {
  6617. for (i = 0; i < module->data_seg_count; i++) {
  6618. if (module->data_segments[i]) {
  6619. if (module->data_segments[i]->is_data_cloned)
  6620. wasm_runtime_free(module->data_segments[i]->data);
  6621. #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
  6622. destroy_init_expr(module,
  6623. &(module->data_segments[i]->base_offset));
  6624. #endif
  6625. wasm_runtime_free(module->data_segments[i]);
  6626. }
  6627. }
  6628. wasm_runtime_free(module->data_segments);
  6629. }
  6630. if (module->types) {
  6631. for (i = 0; i < module->type_count; i++) {
  6632. if (module->types[i])
  6633. destroy_wasm_type(module->types[i]);
  6634. }
  6635. wasm_runtime_free(module->types);
  6636. }
  6637. if (module->const_str_list) {
  6638. StringNode *node = module->const_str_list, *node_next;
  6639. while (node) {
  6640. node_next = node->next;
  6641. wasm_runtime_free(node);
  6642. node = node_next;
  6643. }
  6644. }
  6645. #if WASM_ENABLE_STRINGREF != 0
  6646. if (module->string_literal_ptrs) {
  6647. wasm_runtime_free((void *)module->string_literal_ptrs);
  6648. }
  6649. if (module->string_literal_lengths) {
  6650. wasm_runtime_free(module->string_literal_lengths);
  6651. }
  6652. #endif
  6653. #if WASM_ENABLE_FAST_INTERP == 0
  6654. if (module->br_table_cache_list) {
  6655. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6656. BrTableCache *node_next;
  6657. while (node) {
  6658. node_next = bh_list_elem_next(node);
  6659. wasm_runtime_free(node);
  6660. node = node_next;
  6661. }
  6662. }
  6663. #endif
  6664. #if WASM_ENABLE_MULTI_MODULE != 0
  6665. /* just release the sub module list */
  6666. if (module->import_module_list) {
  6667. WASMRegisteredModule *node =
  6668. bh_list_first_elem(module->import_module_list);
  6669. while (node) {
  6670. WASMRegisteredModule *next = bh_list_elem_next(node);
  6671. bh_list_remove(module->import_module_list, node);
  6672. /*
  6673. * unload(sub_module) will be triggered during runtime_destroy().
  6674. * every module in the global module list will be unloaded one by
  6675. * one. so don't worry.
  6676. */
  6677. wasm_runtime_free(node);
  6678. /*
  6679. * the module file reading buffer will be released
  6680. * in runtime_destroy()
  6681. */
  6682. node = next;
  6683. }
  6684. }
  6685. #endif
  6686. #if WASM_ENABLE_DEBUG_INTERP != 0
  6687. WASMFastOPCodeNode *fast_opcode =
  6688. bh_list_first_elem(&module->fast_opcode_list);
  6689. while (fast_opcode) {
  6690. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6691. wasm_runtime_free(fast_opcode);
  6692. fast_opcode = next;
  6693. }
  6694. #endif
  6695. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6696. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6697. && WASM_ENABLE_LAZY_JIT != 0)
  6698. os_mutex_destroy(&module->instance_list_lock);
  6699. #endif
  6700. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6701. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6702. #endif
  6703. #if WASM_ENABLE_FAST_JIT != 0
  6704. if (module->fast_jit_func_ptrs) {
  6705. wasm_runtime_free(module->fast_jit_func_ptrs);
  6706. }
  6707. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6708. if (module->fast_jit_thread_locks_inited[i]) {
  6709. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6710. }
  6711. }
  6712. #endif
  6713. #if WASM_ENABLE_GC != 0
  6714. os_mutex_destroy(&module->rtt_type_lock);
  6715. bh_hash_map_destroy(module->ref_type_set);
  6716. if (module->rtt_types) {
  6717. for (i = 0; i < module->type_count; i++) {
  6718. if (module->rtt_types[i])
  6719. wasm_runtime_free(module->rtt_types[i]);
  6720. }
  6721. wasm_runtime_free(module->rtt_types);
  6722. }
  6723. #if WASM_ENABLE_STRINGREF != 0
  6724. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6725. if (module->stringref_rtts[i])
  6726. wasm_runtime_free(module->stringref_rtts[i]);
  6727. }
  6728. #endif
  6729. #endif
  6730. #if WASM_ENABLE_BRANCH_HINTS != 0
  6731. for (i = 0; i < module->function_count; i++) {
  6732. // be carefull when adding more hints. This only works as long as
  6733. // the hint structs have been allocated all at once as an array.
  6734. // With only branch-hints at the moment, this is the case.
  6735. if (module->function_hints != NULL && module->function_hints[i] != NULL)
  6736. wasm_runtime_free(module->function_hints[i]);
  6737. }
  6738. if (module->function_hints != NULL)
  6739. wasm_runtime_free(module->function_hints);
  6740. #endif
  6741. wasm_runtime_free(module);
  6742. }
  6743. bool
  6744. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6745. const uint8 *start_addr, const uint8 *code_end_addr,
  6746. uint8 label_type, uint8 **p_else_addr,
  6747. uint8 **p_end_addr)
  6748. {
  6749. const uint8 *p = start_addr, *p_end = code_end_addr;
  6750. uint8 *else_addr = NULL;
  6751. char error_buf[128];
  6752. uint32 block_nested_depth = 1, count, i, j, t;
  6753. uint32 error_buf_size = sizeof(error_buf);
  6754. uint8 opcode, u8;
  6755. BlockAddr block_stack[16] = { { 0 } }, *block;
  6756. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6757. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6758. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6759. if (block[j].start_addr == start_addr) {
  6760. /* Cache hit */
  6761. *p_else_addr = block[j].else_addr;
  6762. *p_end_addr = block[j].end_addr;
  6763. return true;
  6764. }
  6765. }
  6766. /* Cache unhit */
  6767. block_stack[0].start_addr = start_addr;
  6768. while (p < code_end_addr) {
  6769. opcode = *p++;
  6770. #if WASM_ENABLE_DEBUG_INTERP != 0
  6771. op_break_retry:
  6772. #endif
  6773. switch (opcode) {
  6774. case WASM_OP_UNREACHABLE:
  6775. case WASM_OP_NOP:
  6776. break;
  6777. #if WASM_ENABLE_EXCE_HANDLING != 0
  6778. case WASM_OP_TRY:
  6779. u8 = read_uint8(p);
  6780. if (block_nested_depth
  6781. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6782. block_stack[block_nested_depth].start_addr = p;
  6783. block_stack[block_nested_depth].else_addr = NULL;
  6784. }
  6785. block_nested_depth++;
  6786. break;
  6787. case EXT_OP_TRY:
  6788. skip_leb_uint32(p, p_end);
  6789. if (block_nested_depth
  6790. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6791. block_stack[block_nested_depth].start_addr = p;
  6792. block_stack[block_nested_depth].else_addr = NULL;
  6793. }
  6794. block_nested_depth++;
  6795. break;
  6796. case WASM_OP_CATCH:
  6797. if (block_nested_depth == 1) {
  6798. *p_end_addr = (uint8 *)(p - 1);
  6799. /* stop search and return the address of the catch block */
  6800. return true;
  6801. }
  6802. break;
  6803. case WASM_OP_CATCH_ALL:
  6804. if (block_nested_depth == 1) {
  6805. *p_end_addr = (uint8 *)(p - 1);
  6806. /* stop search and return the address of the catch_all block
  6807. */
  6808. return true;
  6809. }
  6810. break;
  6811. case WASM_OP_THROW:
  6812. /* skip tag_index */
  6813. skip_leb(p);
  6814. break;
  6815. case WASM_OP_RETHROW:
  6816. /* skip depth */
  6817. skip_leb(p);
  6818. break;
  6819. case WASM_OP_DELEGATE:
  6820. if (block_nested_depth == 1) {
  6821. *p_end_addr = (uint8 *)(p - 1);
  6822. return true;
  6823. }
  6824. else {
  6825. skip_leb(p);
  6826. /* the DELEGATE opcode ends the tryblock, */
  6827. block_nested_depth--;
  6828. if (block_nested_depth
  6829. < sizeof(block_stack) / sizeof(BlockAddr))
  6830. block_stack[block_nested_depth].end_addr =
  6831. (uint8 *)(p - 1);
  6832. }
  6833. break;
  6834. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6835. case WASM_OP_BLOCK:
  6836. case WASM_OP_LOOP:
  6837. case WASM_OP_IF:
  6838. {
  6839. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6840. u8 = read_uint8(p);
  6841. if (is_byte_a_type(u8)) {
  6842. #if WASM_ENABLE_GC != 0
  6843. if (wasm_is_type_multi_byte_type(u8)) {
  6844. /* the possible extra bytes of GC ref type have been
  6845. modified to OP_NOP, no need to resolve them again */
  6846. }
  6847. #endif
  6848. }
  6849. else {
  6850. p--;
  6851. /* block type */
  6852. skip_leb_int32(p, p_end);
  6853. }
  6854. if (block_nested_depth
  6855. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6856. block_stack[block_nested_depth].start_addr = p;
  6857. block_stack[block_nested_depth].else_addr = NULL;
  6858. }
  6859. block_nested_depth++;
  6860. break;
  6861. }
  6862. case EXT_OP_BLOCK:
  6863. case EXT_OP_LOOP:
  6864. case EXT_OP_IF:
  6865. /* block type */
  6866. skip_leb_int32(p, p_end);
  6867. if (block_nested_depth
  6868. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6869. block_stack[block_nested_depth].start_addr = p;
  6870. block_stack[block_nested_depth].else_addr = NULL;
  6871. }
  6872. block_nested_depth++;
  6873. break;
  6874. case WASM_OP_ELSE:
  6875. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6876. else_addr = (uint8 *)(p - 1);
  6877. if (block_nested_depth - 1
  6878. < sizeof(block_stack) / sizeof(BlockAddr))
  6879. block_stack[block_nested_depth - 1].else_addr =
  6880. (uint8 *)(p - 1);
  6881. break;
  6882. case WASM_OP_END:
  6883. if (block_nested_depth == 1) {
  6884. if (label_type == LABEL_TYPE_IF)
  6885. *p_else_addr = else_addr;
  6886. *p_end_addr = (uint8 *)(p - 1);
  6887. block_stack[0].end_addr = (uint8 *)(p - 1);
  6888. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6889. t++) {
  6890. start_addr = block_stack[t].start_addr;
  6891. if (start_addr) {
  6892. i = ((uintptr_t)start_addr)
  6893. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6894. block =
  6895. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6896. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6897. if (!block[j].start_addr)
  6898. break;
  6899. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6900. memmove(block + 1, block,
  6901. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6902. * sizeof(BlockAddr));
  6903. j = 0;
  6904. }
  6905. block[j].start_addr = block_stack[t].start_addr;
  6906. block[j].else_addr = block_stack[t].else_addr;
  6907. block[j].end_addr = block_stack[t].end_addr;
  6908. }
  6909. else
  6910. break;
  6911. }
  6912. return true;
  6913. }
  6914. else {
  6915. block_nested_depth--;
  6916. if (block_nested_depth
  6917. < sizeof(block_stack) / sizeof(BlockAddr))
  6918. block_stack[block_nested_depth].end_addr =
  6919. (uint8 *)(p - 1);
  6920. }
  6921. break;
  6922. case WASM_OP_BR:
  6923. case WASM_OP_BR_IF:
  6924. skip_leb_uint32(p, p_end); /* labelidx */
  6925. break;
  6926. case WASM_OP_BR_TABLE:
  6927. read_leb_uint32(p, p_end, count); /* label num */
  6928. #if WASM_ENABLE_FAST_INTERP != 0
  6929. for (i = 0; i <= count; i++) /* labelidxs */
  6930. skip_leb_uint32(p, p_end);
  6931. #else
  6932. p += count + 1;
  6933. while (*p == WASM_OP_NOP)
  6934. p++;
  6935. #endif
  6936. break;
  6937. #if WASM_ENABLE_FAST_INTERP == 0
  6938. case EXT_OP_BR_TABLE_CACHE:
  6939. read_leb_uint32(p, p_end, count); /* label num */
  6940. while (*p == WASM_OP_NOP)
  6941. p++;
  6942. break;
  6943. #endif
  6944. case WASM_OP_RETURN:
  6945. break;
  6946. case WASM_OP_CALL:
  6947. #if WASM_ENABLE_TAIL_CALL != 0
  6948. case WASM_OP_RETURN_CALL:
  6949. #endif
  6950. skip_leb_uint32(p, p_end); /* funcidx */
  6951. break;
  6952. case WASM_OP_CALL_INDIRECT:
  6953. #if WASM_ENABLE_TAIL_CALL != 0
  6954. case WASM_OP_RETURN_CALL_INDIRECT:
  6955. #endif
  6956. skip_leb_uint32(p, p_end); /* typeidx */
  6957. #if WASM_ENABLE_CALL_INDIRECT_OVERLONG != 0 || WASM_ENABLE_GC != 0
  6958. skip_leb_uint32(p, p_end); /* tableidx */
  6959. #else
  6960. u8 = read_uint8(p); /* 0x00 */
  6961. #endif
  6962. break;
  6963. #if WASM_ENABLE_GC != 0
  6964. case WASM_OP_CALL_REF:
  6965. case WASM_OP_RETURN_CALL_REF:
  6966. skip_leb_uint32(p, p_end); /* typeidx */
  6967. break;
  6968. #endif
  6969. case WASM_OP_DROP:
  6970. case WASM_OP_SELECT:
  6971. case WASM_OP_DROP_64:
  6972. case WASM_OP_SELECT_64:
  6973. #if WASM_ENABLE_SIMDE != 0
  6974. case WASM_OP_SELECT_128:
  6975. #endif
  6976. break;
  6977. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6978. case WASM_OP_SELECT_T:
  6979. {
  6980. skip_leb_uint32(p, p_end); /* vec length */
  6981. u8 = read_uint8(p); /* typeidx */
  6982. /* the possible extra bytes of GC ref type have been
  6983. modified to OP_NOP, no need to resolve them again */
  6984. break;
  6985. }
  6986. case WASM_OP_TABLE_GET:
  6987. case WASM_OP_TABLE_SET:
  6988. skip_leb_uint32(p, p_end); /* table index */
  6989. break;
  6990. case WASM_OP_REF_NULL:
  6991. {
  6992. u8 = read_uint8(p); /* type */
  6993. if (is_byte_a_type(u8)) {
  6994. #if WASM_ENABLE_GC != 0
  6995. if (wasm_is_type_multi_byte_type(u8)) {
  6996. /* the possible extra bytes of GC ref type have been
  6997. modified to OP_NOP, no need to resolve them again */
  6998. }
  6999. #endif
  7000. }
  7001. else {
  7002. p--;
  7003. skip_leb_uint32(p, p_end);
  7004. }
  7005. break;
  7006. }
  7007. case WASM_OP_REF_IS_NULL:
  7008. break;
  7009. case WASM_OP_REF_FUNC:
  7010. skip_leb_uint32(p, p_end); /* func index */
  7011. break;
  7012. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  7013. #if WASM_ENABLE_GC != 0
  7014. case WASM_OP_REF_AS_NON_NULL:
  7015. case WASM_OP_REF_EQ:
  7016. break;
  7017. case WASM_OP_BR_ON_NULL:
  7018. case WASM_OP_BR_ON_NON_NULL:
  7019. skip_leb_uint32(p, p_end); /* label index */
  7020. break;
  7021. #endif /* end of WASM_ENABLE_GC != 0 */
  7022. case WASM_OP_GET_LOCAL:
  7023. case WASM_OP_SET_LOCAL:
  7024. case WASM_OP_TEE_LOCAL:
  7025. case WASM_OP_GET_GLOBAL:
  7026. case WASM_OP_SET_GLOBAL:
  7027. case WASM_OP_GET_GLOBAL_64:
  7028. case WASM_OP_SET_GLOBAL_64:
  7029. #if WASM_ENABLE_SIMDE != 0
  7030. case WASM_OP_GET_GLOBAL_V128:
  7031. case WASM_OP_SET_GLOBAL_V128:
  7032. #endif
  7033. case WASM_OP_SET_GLOBAL_AUX_STACK:
  7034. skip_leb_uint32(p, p_end); /* local index */
  7035. break;
  7036. case EXT_OP_GET_LOCAL_FAST:
  7037. case EXT_OP_SET_LOCAL_FAST:
  7038. case EXT_OP_TEE_LOCAL_FAST:
  7039. CHECK_BUF(p, p_end, 1);
  7040. p++;
  7041. break;
  7042. case WASM_OP_I32_LOAD:
  7043. case WASM_OP_I64_LOAD:
  7044. case WASM_OP_F32_LOAD:
  7045. case WASM_OP_F64_LOAD:
  7046. case WASM_OP_I32_LOAD8_S:
  7047. case WASM_OP_I32_LOAD8_U:
  7048. case WASM_OP_I32_LOAD16_S:
  7049. case WASM_OP_I32_LOAD16_U:
  7050. case WASM_OP_I64_LOAD8_S:
  7051. case WASM_OP_I64_LOAD8_U:
  7052. case WASM_OP_I64_LOAD16_S:
  7053. case WASM_OP_I64_LOAD16_U:
  7054. case WASM_OP_I64_LOAD32_S:
  7055. case WASM_OP_I64_LOAD32_U:
  7056. case WASM_OP_I32_STORE:
  7057. case WASM_OP_I64_STORE:
  7058. case WASM_OP_F32_STORE:
  7059. case WASM_OP_F64_STORE:
  7060. case WASM_OP_I32_STORE8:
  7061. case WASM_OP_I32_STORE16:
  7062. case WASM_OP_I64_STORE8:
  7063. case WASM_OP_I64_STORE16:
  7064. case WASM_OP_I64_STORE32:
  7065. skip_leb_align(p, p_end); /* align */
  7066. skip_leb_mem_offset(p, p_end); /* offset */
  7067. break;
  7068. case WASM_OP_MEMORY_SIZE:
  7069. case WASM_OP_MEMORY_GROW:
  7070. skip_leb_memidx(p, p_end); /* memidx */
  7071. break;
  7072. case WASM_OP_I32_CONST:
  7073. skip_leb_int32(p, p_end);
  7074. break;
  7075. case WASM_OP_I64_CONST:
  7076. skip_leb_int64(p, p_end);
  7077. break;
  7078. case WASM_OP_F32_CONST:
  7079. p += sizeof(float32);
  7080. break;
  7081. case WASM_OP_F64_CONST:
  7082. p += sizeof(float64);
  7083. break;
  7084. case WASM_OP_I32_EQZ:
  7085. case WASM_OP_I32_EQ:
  7086. case WASM_OP_I32_NE:
  7087. case WASM_OP_I32_LT_S:
  7088. case WASM_OP_I32_LT_U:
  7089. case WASM_OP_I32_GT_S:
  7090. case WASM_OP_I32_GT_U:
  7091. case WASM_OP_I32_LE_S:
  7092. case WASM_OP_I32_LE_U:
  7093. case WASM_OP_I32_GE_S:
  7094. case WASM_OP_I32_GE_U:
  7095. case WASM_OP_I64_EQZ:
  7096. case WASM_OP_I64_EQ:
  7097. case WASM_OP_I64_NE:
  7098. case WASM_OP_I64_LT_S:
  7099. case WASM_OP_I64_LT_U:
  7100. case WASM_OP_I64_GT_S:
  7101. case WASM_OP_I64_GT_U:
  7102. case WASM_OP_I64_LE_S:
  7103. case WASM_OP_I64_LE_U:
  7104. case WASM_OP_I64_GE_S:
  7105. case WASM_OP_I64_GE_U:
  7106. case WASM_OP_F32_EQ:
  7107. case WASM_OP_F32_NE:
  7108. case WASM_OP_F32_LT:
  7109. case WASM_OP_F32_GT:
  7110. case WASM_OP_F32_LE:
  7111. case WASM_OP_F32_GE:
  7112. case WASM_OP_F64_EQ:
  7113. case WASM_OP_F64_NE:
  7114. case WASM_OP_F64_LT:
  7115. case WASM_OP_F64_GT:
  7116. case WASM_OP_F64_LE:
  7117. case WASM_OP_F64_GE:
  7118. case WASM_OP_I32_CLZ:
  7119. case WASM_OP_I32_CTZ:
  7120. case WASM_OP_I32_POPCNT:
  7121. case WASM_OP_I32_ADD:
  7122. case WASM_OP_I32_SUB:
  7123. case WASM_OP_I32_MUL:
  7124. case WASM_OP_I32_DIV_S:
  7125. case WASM_OP_I32_DIV_U:
  7126. case WASM_OP_I32_REM_S:
  7127. case WASM_OP_I32_REM_U:
  7128. case WASM_OP_I32_AND:
  7129. case WASM_OP_I32_OR:
  7130. case WASM_OP_I32_XOR:
  7131. case WASM_OP_I32_SHL:
  7132. case WASM_OP_I32_SHR_S:
  7133. case WASM_OP_I32_SHR_U:
  7134. case WASM_OP_I32_ROTL:
  7135. case WASM_OP_I32_ROTR:
  7136. case WASM_OP_I64_CLZ:
  7137. case WASM_OP_I64_CTZ:
  7138. case WASM_OP_I64_POPCNT:
  7139. case WASM_OP_I64_ADD:
  7140. case WASM_OP_I64_SUB:
  7141. case WASM_OP_I64_MUL:
  7142. case WASM_OP_I64_DIV_S:
  7143. case WASM_OP_I64_DIV_U:
  7144. case WASM_OP_I64_REM_S:
  7145. case WASM_OP_I64_REM_U:
  7146. case WASM_OP_I64_AND:
  7147. case WASM_OP_I64_OR:
  7148. case WASM_OP_I64_XOR:
  7149. case WASM_OP_I64_SHL:
  7150. case WASM_OP_I64_SHR_S:
  7151. case WASM_OP_I64_SHR_U:
  7152. case WASM_OP_I64_ROTL:
  7153. case WASM_OP_I64_ROTR:
  7154. case WASM_OP_F32_ABS:
  7155. case WASM_OP_F32_NEG:
  7156. case WASM_OP_F32_CEIL:
  7157. case WASM_OP_F32_FLOOR:
  7158. case WASM_OP_F32_TRUNC:
  7159. case WASM_OP_F32_NEAREST:
  7160. case WASM_OP_F32_SQRT:
  7161. case WASM_OP_F32_ADD:
  7162. case WASM_OP_F32_SUB:
  7163. case WASM_OP_F32_MUL:
  7164. case WASM_OP_F32_DIV:
  7165. case WASM_OP_F32_MIN:
  7166. case WASM_OP_F32_MAX:
  7167. case WASM_OP_F32_COPYSIGN:
  7168. case WASM_OP_F64_ABS:
  7169. case WASM_OP_F64_NEG:
  7170. case WASM_OP_F64_CEIL:
  7171. case WASM_OP_F64_FLOOR:
  7172. case WASM_OP_F64_TRUNC:
  7173. case WASM_OP_F64_NEAREST:
  7174. case WASM_OP_F64_SQRT:
  7175. case WASM_OP_F64_ADD:
  7176. case WASM_OP_F64_SUB:
  7177. case WASM_OP_F64_MUL:
  7178. case WASM_OP_F64_DIV:
  7179. case WASM_OP_F64_MIN:
  7180. case WASM_OP_F64_MAX:
  7181. case WASM_OP_F64_COPYSIGN:
  7182. case WASM_OP_I32_WRAP_I64:
  7183. case WASM_OP_I32_TRUNC_S_F32:
  7184. case WASM_OP_I32_TRUNC_U_F32:
  7185. case WASM_OP_I32_TRUNC_S_F64:
  7186. case WASM_OP_I32_TRUNC_U_F64:
  7187. case WASM_OP_I64_EXTEND_S_I32:
  7188. case WASM_OP_I64_EXTEND_U_I32:
  7189. case WASM_OP_I64_TRUNC_S_F32:
  7190. case WASM_OP_I64_TRUNC_U_F32:
  7191. case WASM_OP_I64_TRUNC_S_F64:
  7192. case WASM_OP_I64_TRUNC_U_F64:
  7193. case WASM_OP_F32_CONVERT_S_I32:
  7194. case WASM_OP_F32_CONVERT_U_I32:
  7195. case WASM_OP_F32_CONVERT_S_I64:
  7196. case WASM_OP_F32_CONVERT_U_I64:
  7197. case WASM_OP_F32_DEMOTE_F64:
  7198. case WASM_OP_F64_CONVERT_S_I32:
  7199. case WASM_OP_F64_CONVERT_U_I32:
  7200. case WASM_OP_F64_CONVERT_S_I64:
  7201. case WASM_OP_F64_CONVERT_U_I64:
  7202. case WASM_OP_F64_PROMOTE_F32:
  7203. case WASM_OP_I32_REINTERPRET_F32:
  7204. case WASM_OP_I64_REINTERPRET_F64:
  7205. case WASM_OP_F32_REINTERPRET_I32:
  7206. case WASM_OP_F64_REINTERPRET_I64:
  7207. case WASM_OP_I32_EXTEND8_S:
  7208. case WASM_OP_I32_EXTEND16_S:
  7209. case WASM_OP_I64_EXTEND8_S:
  7210. case WASM_OP_I64_EXTEND16_S:
  7211. case WASM_OP_I64_EXTEND32_S:
  7212. break;
  7213. #if WASM_ENABLE_GC != 0
  7214. case WASM_OP_GC_PREFIX:
  7215. {
  7216. uint32 opcode1;
  7217. read_leb_uint32(p, p_end, opcode1);
  7218. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7219. is no larger than UINT8_MAX */
  7220. opcode = (uint8)opcode1;
  7221. switch (opcode) {
  7222. case WASM_OP_STRUCT_NEW:
  7223. case WASM_OP_STRUCT_NEW_DEFAULT:
  7224. skip_leb_uint32(p, p_end); /* typeidx */
  7225. break;
  7226. case WASM_OP_STRUCT_GET:
  7227. case WASM_OP_STRUCT_GET_S:
  7228. case WASM_OP_STRUCT_GET_U:
  7229. case WASM_OP_STRUCT_SET:
  7230. skip_leb_uint32(p, p_end); /* typeidx */
  7231. skip_leb_uint32(p, p_end); /* fieldidx */
  7232. break;
  7233. case WASM_OP_ARRAY_NEW:
  7234. case WASM_OP_ARRAY_NEW_DEFAULT:
  7235. case WASM_OP_ARRAY_GET:
  7236. case WASM_OP_ARRAY_GET_S:
  7237. case WASM_OP_ARRAY_GET_U:
  7238. case WASM_OP_ARRAY_SET:
  7239. case WASM_OP_ARRAY_FILL:
  7240. skip_leb_uint32(p, p_end); /* typeidx */
  7241. break;
  7242. case WASM_OP_ARRAY_COPY:
  7243. skip_leb_uint32(p, p_end); /* typeidx1 */
  7244. skip_leb_uint32(p, p_end); /* typeidx2 */
  7245. break;
  7246. case WASM_OP_ARRAY_LEN:
  7247. break;
  7248. case WASM_OP_ARRAY_NEW_FIXED:
  7249. case WASM_OP_ARRAY_NEW_DATA:
  7250. case WASM_OP_ARRAY_NEW_ELEM:
  7251. skip_leb_uint32(p, p_end); /* typeidx */
  7252. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  7253. break;
  7254. case WASM_OP_REF_I31:
  7255. case WASM_OP_I31_GET_S:
  7256. case WASM_OP_I31_GET_U:
  7257. break;
  7258. case WASM_OP_REF_TEST:
  7259. case WASM_OP_REF_CAST:
  7260. case WASM_OP_REF_TEST_NULLABLE:
  7261. case WASM_OP_REF_CAST_NULLABLE:
  7262. skip_leb_int32(p, p_end); /* heaptype */
  7263. break;
  7264. case WASM_OP_BR_ON_CAST:
  7265. case WASM_OP_BR_ON_CAST_FAIL:
  7266. p += sizeof(uint8); /* castflag */
  7267. skip_leb_uint32(p, p_end); /* labelidx */
  7268. skip_leb_int32(p, p_end); /* heaptype */
  7269. skip_leb_int32(p, p_end); /* heaptype2 */
  7270. break;
  7271. case WASM_OP_ANY_CONVERT_EXTERN:
  7272. case WASM_OP_EXTERN_CONVERT_ANY:
  7273. break;
  7274. #if WASM_ENABLE_STRINGREF != 0
  7275. case WASM_OP_STRING_NEW_UTF8:
  7276. case WASM_OP_STRING_NEW_WTF16:
  7277. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  7278. case WASM_OP_STRING_NEW_WTF8:
  7279. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7280. break;
  7281. case WASM_OP_STRING_CONST:
  7282. skip_leb_int32(p, p_end); /* contents */
  7283. break;
  7284. case WASM_OP_STRING_MEASURE_UTF8:
  7285. case WASM_OP_STRING_MEASURE_WTF8:
  7286. case WASM_OP_STRING_MEASURE_WTF16:
  7287. break;
  7288. case WASM_OP_STRING_ENCODE_UTF8:
  7289. case WASM_OP_STRING_ENCODE_WTF16:
  7290. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  7291. case WASM_OP_STRING_ENCODE_WTF8:
  7292. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7293. break;
  7294. case WASM_OP_STRING_CONCAT:
  7295. case WASM_OP_STRING_EQ:
  7296. case WASM_OP_STRING_IS_USV_SEQUENCE:
  7297. case WASM_OP_STRING_AS_WTF8:
  7298. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  7299. break;
  7300. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  7301. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  7302. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  7303. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7304. break;
  7305. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  7306. case WASM_OP_STRING_AS_WTF16:
  7307. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  7308. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  7309. break;
  7310. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  7311. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  7312. break;
  7313. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  7314. case WASM_OP_STRING_AS_ITER:
  7315. case WASM_OP_STRINGVIEW_ITER_NEXT:
  7316. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  7317. case WASM_OP_STRINGVIEW_ITER_REWIND:
  7318. case WASM_OP_STRINGVIEW_ITER_SLICE:
  7319. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  7320. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  7321. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  7322. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  7323. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  7324. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  7325. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  7326. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  7327. break;
  7328. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  7329. default:
  7330. return false;
  7331. }
  7332. break;
  7333. }
  7334. #endif /* end of WASM_ENABLE_GC != 0 */
  7335. case WASM_OP_MISC_PREFIX:
  7336. {
  7337. uint32 opcode1;
  7338. read_leb_uint32(p, p_end, opcode1);
  7339. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7340. is no larger than UINT8_MAX */
  7341. opcode = (uint8)opcode1;
  7342. switch (opcode) {
  7343. case WASM_OP_I32_TRUNC_SAT_S_F32:
  7344. case WASM_OP_I32_TRUNC_SAT_U_F32:
  7345. case WASM_OP_I32_TRUNC_SAT_S_F64:
  7346. case WASM_OP_I32_TRUNC_SAT_U_F64:
  7347. case WASM_OP_I64_TRUNC_SAT_S_F32:
  7348. case WASM_OP_I64_TRUNC_SAT_U_F32:
  7349. case WASM_OP_I64_TRUNC_SAT_S_F64:
  7350. case WASM_OP_I64_TRUNC_SAT_U_F64:
  7351. break;
  7352. #if WASM_ENABLE_BULK_MEMORY != 0
  7353. case WASM_OP_MEMORY_INIT:
  7354. skip_leb_uint32(p, p_end);
  7355. skip_leb_memidx(p, p_end);
  7356. break;
  7357. case WASM_OP_DATA_DROP:
  7358. skip_leb_uint32(p, p_end);
  7359. break;
  7360. #endif /* WASM_ENABLE_BULK_MEMORY */
  7361. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  7362. case WASM_OP_MEMORY_COPY:
  7363. skip_leb_memidx(p, p_end);
  7364. skip_leb_memidx(p, p_end);
  7365. break;
  7366. case WASM_OP_MEMORY_FILL:
  7367. skip_leb_memidx(p, p_end);
  7368. break;
  7369. #endif /* WASM_ENABLE_BULK_MEMORY_OPT */
  7370. #if WASM_ENABLE_REF_TYPES != 0
  7371. case WASM_OP_TABLE_INIT:
  7372. case WASM_OP_TABLE_COPY:
  7373. /* tableidx */
  7374. skip_leb_uint32(p, p_end);
  7375. /* elemidx */
  7376. skip_leb_uint32(p, p_end);
  7377. break;
  7378. case WASM_OP_ELEM_DROP:
  7379. /* elemidx */
  7380. skip_leb_uint32(p, p_end);
  7381. break;
  7382. case WASM_OP_TABLE_SIZE:
  7383. case WASM_OP_TABLE_GROW:
  7384. case WASM_OP_TABLE_FILL:
  7385. skip_leb_uint32(p, p_end); /* table idx */
  7386. break;
  7387. #endif /* WASM_ENABLE_REF_TYPES */
  7388. default:
  7389. return false;
  7390. }
  7391. break;
  7392. }
  7393. #if WASM_ENABLE_SIMD != 0
  7394. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  7395. || (WASM_ENABLE_FAST_INTERP != 0)
  7396. case WASM_OP_SIMD_PREFIX:
  7397. {
  7398. uint32 opcode1;
  7399. read_leb_uint32(p, p_end, opcode1);
  7400. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7401. is no larger than UINT8_MAX */
  7402. opcode = (uint8)opcode1;
  7403. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  7404. */
  7405. switch (opcode) {
  7406. case SIMD_v128_load:
  7407. case SIMD_v128_load8x8_s:
  7408. case SIMD_v128_load8x8_u:
  7409. case SIMD_v128_load16x4_s:
  7410. case SIMD_v128_load16x4_u:
  7411. case SIMD_v128_load32x2_s:
  7412. case SIMD_v128_load32x2_u:
  7413. case SIMD_v128_load8_splat:
  7414. case SIMD_v128_load16_splat:
  7415. case SIMD_v128_load32_splat:
  7416. case SIMD_v128_load64_splat:
  7417. case SIMD_v128_store:
  7418. /* memarg align */
  7419. skip_leb_uint32(p, p_end);
  7420. /* memarg offset */
  7421. skip_leb_mem_offset(p, p_end);
  7422. break;
  7423. case SIMD_v128_const:
  7424. case SIMD_v8x16_shuffle:
  7425. /* immByte[16] immLaneId[16] */
  7426. CHECK_BUF1(p, p_end, 16);
  7427. p += 16;
  7428. break;
  7429. case SIMD_i8x16_extract_lane_s:
  7430. case SIMD_i8x16_extract_lane_u:
  7431. case SIMD_i8x16_replace_lane:
  7432. case SIMD_i16x8_extract_lane_s:
  7433. case SIMD_i16x8_extract_lane_u:
  7434. case SIMD_i16x8_replace_lane:
  7435. case SIMD_i32x4_extract_lane:
  7436. case SIMD_i32x4_replace_lane:
  7437. case SIMD_i64x2_extract_lane:
  7438. case SIMD_i64x2_replace_lane:
  7439. case SIMD_f32x4_extract_lane:
  7440. case SIMD_f32x4_replace_lane:
  7441. case SIMD_f64x2_extract_lane:
  7442. case SIMD_f64x2_replace_lane:
  7443. /* ImmLaneId */
  7444. CHECK_BUF(p, p_end, 1);
  7445. p++;
  7446. break;
  7447. case SIMD_v128_load8_lane:
  7448. case SIMD_v128_load16_lane:
  7449. case SIMD_v128_load32_lane:
  7450. case SIMD_v128_load64_lane:
  7451. case SIMD_v128_store8_lane:
  7452. case SIMD_v128_store16_lane:
  7453. case SIMD_v128_store32_lane:
  7454. case SIMD_v128_store64_lane:
  7455. /* memarg align */
  7456. skip_leb_uint32(p, p_end);
  7457. /* memarg offset */
  7458. skip_leb_mem_offset(p, p_end);
  7459. /* ImmLaneId */
  7460. CHECK_BUF(p, p_end, 1);
  7461. p++;
  7462. break;
  7463. case SIMD_v128_load32_zero:
  7464. case SIMD_v128_load64_zero:
  7465. /* memarg align */
  7466. skip_leb_uint32(p, p_end);
  7467. /* memarg offset */
  7468. skip_leb_mem_offset(p, p_end);
  7469. break;
  7470. default:
  7471. /*
  7472. * since latest SIMD specific used almost every value
  7473. * from 0x00 to 0xff, the default branch will present
  7474. * all opcodes without imm
  7475. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7476. */
  7477. break;
  7478. }
  7479. break;
  7480. }
  7481. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7482. (WASM_ENABLE_FAST_INTERP != 0) */
  7483. #endif /* end of WASM_ENABLE_SIMD */
  7484. #if WASM_ENABLE_SHARED_MEMORY != 0
  7485. case WASM_OP_ATOMIC_PREFIX:
  7486. {
  7487. uint32 opcode1;
  7488. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7489. read_leb_uint32(p, p_end, opcode1);
  7490. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7491. is no larger than UINT8_MAX */
  7492. opcode = (uint8)opcode1;
  7493. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7494. skip_leb_uint32(p, p_end); /* align */
  7495. skip_leb_mem_offset(p, p_end); /* offset */
  7496. }
  7497. else {
  7498. /* atomic.fence doesn't have memarg */
  7499. p++;
  7500. }
  7501. break;
  7502. }
  7503. #endif
  7504. #if WASM_ENABLE_DEBUG_INTERP != 0
  7505. case DEBUG_OP_BREAK:
  7506. {
  7507. WASMDebugInstance *debug_instance =
  7508. wasm_exec_env_get_instance(exec_env);
  7509. char original_opcode[1];
  7510. uint64 size = 1;
  7511. WASMModuleInstance *module_inst =
  7512. (WASMModuleInstance *)exec_env->module_inst;
  7513. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7514. ? (p - 1) - module_inst->module->load_addr
  7515. : ~0;
  7516. if (debug_instance) {
  7517. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7518. original_opcode, &size)
  7519. && size == 1) {
  7520. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7521. "with %02x: ",
  7522. original_opcode[0]);
  7523. opcode = original_opcode[0];
  7524. goto op_break_retry;
  7525. }
  7526. }
  7527. break;
  7528. }
  7529. #endif
  7530. default:
  7531. return false;
  7532. }
  7533. }
  7534. (void)u8;
  7535. (void)exec_env;
  7536. return false;
  7537. fail:
  7538. return false;
  7539. }
  7540. #if WASM_ENABLE_FAST_INTERP != 0
  7541. #if WASM_DEBUG_PREPROCESSOR != 0
  7542. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7543. #else
  7544. #define LOG_OP(...) (void)0
  7545. #endif
  7546. #define PATCH_ELSE 0
  7547. #define PATCH_END 1
  7548. typedef struct BranchBlockPatch {
  7549. struct BranchBlockPatch *next;
  7550. uint8 patch_type;
  7551. uint8 *code_compiled;
  7552. } BranchBlockPatch;
  7553. #endif
  7554. typedef struct BranchBlock {
  7555. uint8 label_type;
  7556. BlockType block_type;
  7557. uint8 *start_addr;
  7558. uint8 *else_addr;
  7559. uint8 *end_addr;
  7560. uint32 stack_cell_num;
  7561. #if WASM_ENABLE_GC != 0
  7562. uint32 reftype_map_num;
  7563. /* Indicate which local is used inside current block, used to validate
  7564. * local.get with non-nullable ref types */
  7565. uint8 *local_use_mask;
  7566. uint32 local_use_mask_size;
  7567. #endif
  7568. #if WASM_ENABLE_FAST_INTERP != 0
  7569. uint16 dynamic_offset;
  7570. uint8 *code_compiled;
  7571. BranchBlockPatch *patch_list;
  7572. /* This is used to save params frame_offset of of if block */
  7573. int16 *param_frame_offsets;
  7574. /* This is used to recover the dynamic offset for else branch,
  7575. * and also to remember the start offset of dynamic space which
  7576. * stores the block arguments for loop block, so we can use it
  7577. * to copy the stack operands to the loop block's arguments in
  7578. * wasm_loader_emit_br_info for opcode br. */
  7579. uint16 start_dynamic_offset;
  7580. #endif
  7581. /* Indicate the operand stack is in polymorphic state.
  7582. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7583. * to polymorphic state until the block's 'end' opcode is processed.
  7584. * If stack is in polymorphic state and stack is empty, instruction can
  7585. * pop any type of value directly without decreasing stack top pointer
  7586. * and stack cell num. */
  7587. bool is_stack_polymorphic;
  7588. } BranchBlock;
  7589. typedef struct WASMLoaderContext {
  7590. /* frame ref stack */
  7591. uint8 *frame_ref;
  7592. uint8 *frame_ref_bottom;
  7593. uint8 *frame_ref_boundary;
  7594. uint32 frame_ref_size;
  7595. uint32 stack_cell_num;
  7596. uint32 max_stack_cell_num;
  7597. #if WASM_ENABLE_GC != 0
  7598. /* frame reftype map stack */
  7599. WASMRefTypeMap *frame_reftype_map;
  7600. WASMRefTypeMap *frame_reftype_map_bottom;
  7601. WASMRefTypeMap *frame_reftype_map_boundary;
  7602. uint32 frame_reftype_map_size;
  7603. uint32 reftype_map_num;
  7604. uint32 max_reftype_map_num;
  7605. /* Current module */
  7606. WASMModule *module;
  7607. /* Current module's ref_type_set */
  7608. HashMap *ref_type_set;
  7609. /* Always point to local variable ref_type of
  7610. wasm_loader_prepare_bytecode */
  7611. WASMRefType *ref_type_tmp;
  7612. #endif
  7613. /* frame csp stack */
  7614. BranchBlock *frame_csp;
  7615. BranchBlock *frame_csp_bottom;
  7616. BranchBlock *frame_csp_boundary;
  7617. uint32 frame_csp_size;
  7618. uint32 csp_num;
  7619. uint32 max_csp_num;
  7620. #if WASM_ENABLE_FAST_INTERP != 0
  7621. /* frame offset stack */
  7622. int16 *frame_offset;
  7623. int16 *frame_offset_bottom;
  7624. int16 *frame_offset_boundary;
  7625. uint32 frame_offset_size;
  7626. int16 dynamic_offset;
  7627. int16 start_dynamic_offset;
  7628. int16 max_dynamic_offset;
  7629. /* preserved local offset */
  7630. int16 preserved_local_offset;
  7631. /* const buffer for i64 and f64 consts, note that the raw bytes
  7632. * of i64 and f64 are the same, so we read an i64 value from an
  7633. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7634. int64 *i64_consts;
  7635. uint32 i64_const_max_num;
  7636. uint32 i64_const_num;
  7637. /* const buffer for i32 and f32 consts */
  7638. int32 *i32_consts;
  7639. uint32 i32_const_max_num;
  7640. uint32 i32_const_num;
  7641. /* const buffer for V128 */
  7642. V128 *v128_consts;
  7643. uint32 v128_const_max_num;
  7644. uint32 v128_const_num;
  7645. /* processed code */
  7646. uint8 *p_code_compiled;
  7647. uint8 *p_code_compiled_end;
  7648. uint32 code_compiled_size;
  7649. /* If the last opcode will be dropped, the peak memory usage will be larger
  7650. * than the final code_compiled_size, we record the peak size to ensure
  7651. * there will not be invalid memory access during second traverse */
  7652. uint32 code_compiled_peak_size;
  7653. #endif
  7654. } WASMLoaderContext;
  7655. #define CHECK_CSP_PUSH() \
  7656. do { \
  7657. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7658. MEM_REALLOC( \
  7659. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7660. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7661. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7662. ctx->frame_csp_boundary = \
  7663. ctx->frame_csp_bottom \
  7664. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7665. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7666. } \
  7667. } while (0)
  7668. #define CHECK_CSP_POP() \
  7669. do { \
  7670. if (ctx->csp_num < 1) { \
  7671. set_error_buf(error_buf, error_buf_size, \
  7672. "type mismatch: " \
  7673. "expect data but block stack was empty"); \
  7674. goto fail; \
  7675. } \
  7676. } while (0)
  7677. #if WASM_ENABLE_FAST_INTERP != 0
  7678. static bool
  7679. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7680. uint32 error_buf_size)
  7681. {
  7682. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7683. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7684. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7685. ctx->frame_offset_size + 16);
  7686. ctx->frame_offset_size += 16;
  7687. ctx->frame_offset_boundary =
  7688. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7689. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7690. }
  7691. return true;
  7692. fail:
  7693. return false;
  7694. }
  7695. static bool
  7696. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7697. {
  7698. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7699. return false;
  7700. return true;
  7701. }
  7702. static bool
  7703. check_dynamic_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7704. {
  7705. if (ctx->dynamic_offset < 0 || (uint32)ctx->dynamic_offset < cells)
  7706. return false;
  7707. return true;
  7708. }
  7709. static void
  7710. free_label_patch_list(BranchBlock *frame_csp)
  7711. {
  7712. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7713. BranchBlockPatch *next;
  7714. while (label_patch != NULL) {
  7715. next = label_patch->next;
  7716. wasm_runtime_free(label_patch);
  7717. label_patch = next;
  7718. }
  7719. frame_csp->patch_list = NULL;
  7720. }
  7721. static void
  7722. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7723. {
  7724. BranchBlock *tmp_csp = frame_csp;
  7725. uint32 i;
  7726. for (i = 0; i < csp_num; i++) {
  7727. free_label_patch_list(tmp_csp);
  7728. tmp_csp++;
  7729. }
  7730. }
  7731. static void
  7732. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7733. {
  7734. BranchBlock *tmp_csp = frame_csp;
  7735. uint32 i;
  7736. for (i = 0; i < csp_num; i++) {
  7737. if (tmp_csp->param_frame_offsets)
  7738. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7739. tmp_csp++;
  7740. }
  7741. }
  7742. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7743. #if WASM_ENABLE_GC != 0
  7744. static bool
  7745. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7746. char *error_buf, uint32 error_buf_size)
  7747. {
  7748. BranchBlock *current_csp = ctx->frame_csp - 1;
  7749. uint32 local_mask_size;
  7750. if (local_count == 0) {
  7751. current_csp->local_use_mask_size = 0;
  7752. return true;
  7753. }
  7754. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7755. * else branch, we don't need to allocate memory again */
  7756. if (!current_csp->local_use_mask) {
  7757. local_mask_size = (local_count + 7) / sizeof(uint8);
  7758. if (!(current_csp->local_use_mask =
  7759. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7760. return false;
  7761. }
  7762. current_csp->local_use_mask_size = local_mask_size;
  7763. }
  7764. else {
  7765. local_mask_size = current_csp->local_use_mask_size;
  7766. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7767. }
  7768. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7769. /* For non-function blocks, inherit the use status from parent block */
  7770. BranchBlock *parent_csp = current_csp - 1;
  7771. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7772. bh_assert(parent_csp->local_use_mask);
  7773. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7774. parent_csp->local_use_mask, local_mask_size);
  7775. }
  7776. return true;
  7777. }
  7778. static void
  7779. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7780. {
  7781. BranchBlock *current_csp = ctx->frame_csp - 1;
  7782. bh_assert(current_csp->local_use_mask
  7783. || current_csp->local_use_mask_size == 0);
  7784. if (current_csp->local_use_mask) {
  7785. wasm_runtime_free(current_csp->local_use_mask);
  7786. }
  7787. current_csp->local_use_mask = NULL;
  7788. current_csp->local_use_mask_size = 0;
  7789. }
  7790. static void
  7791. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7792. {
  7793. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7794. uint32 i;
  7795. for (i = 0; i < ctx->csp_num; i++) {
  7796. if (tmp_csp->local_use_mask) {
  7797. wasm_runtime_free(tmp_csp->local_use_mask);
  7798. tmp_csp->local_use_mask = NULL;
  7799. tmp_csp->local_use_mask_size = 0;
  7800. }
  7801. tmp_csp++;
  7802. }
  7803. }
  7804. static void
  7805. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7806. {
  7807. BranchBlock *current_csp = ctx->frame_csp - 1;
  7808. uint32 byte_offset = index / sizeof(uint8);
  7809. uint32 bit_offset = index % sizeof(uint8);
  7810. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7811. bh_assert(current_csp->local_use_mask);
  7812. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7813. }
  7814. static bool
  7815. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7816. {
  7817. BranchBlock *current_csp = ctx->frame_csp - 1;
  7818. uint32 byte_offset = index / sizeof(uint8);
  7819. uint32 bit_offset = index % sizeof(uint8);
  7820. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7821. bh_assert(current_csp->local_use_mask);
  7822. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7823. ? true
  7824. : false;
  7825. }
  7826. #endif /* end of WASM_ENABLE_GC != 0 */
  7827. static void
  7828. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7829. {
  7830. if (ctx) {
  7831. if (ctx->frame_ref_bottom)
  7832. wasm_runtime_free(ctx->frame_ref_bottom);
  7833. #if WASM_ENABLE_GC != 0
  7834. if (ctx->frame_reftype_map_bottom)
  7835. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7836. #endif
  7837. if (ctx->frame_csp_bottom) {
  7838. #if WASM_ENABLE_FAST_INTERP != 0
  7839. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7840. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7841. ctx->csp_num);
  7842. #endif
  7843. #if WASM_ENABLE_GC != 0
  7844. wasm_loader_clean_all_local_use_masks(ctx);
  7845. #endif
  7846. wasm_runtime_free(ctx->frame_csp_bottom);
  7847. }
  7848. #if WASM_ENABLE_FAST_INTERP != 0
  7849. if (ctx->frame_offset_bottom)
  7850. wasm_runtime_free(ctx->frame_offset_bottom);
  7851. if (ctx->i64_consts)
  7852. wasm_runtime_free(ctx->i64_consts);
  7853. if (ctx->i32_consts)
  7854. wasm_runtime_free(ctx->i32_consts);
  7855. if (ctx->v128_consts)
  7856. wasm_runtime_free(ctx->v128_consts);
  7857. #endif
  7858. wasm_runtime_free(ctx);
  7859. }
  7860. }
  7861. static WASMLoaderContext *
  7862. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7863. {
  7864. WASMLoaderContext *loader_ctx =
  7865. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7866. if (!loader_ctx)
  7867. return NULL;
  7868. loader_ctx->frame_ref_size = 32;
  7869. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7870. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7871. goto fail;
  7872. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7873. #if WASM_ENABLE_GC != 0
  7874. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7875. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7876. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7877. error_buf_size)))
  7878. goto fail;
  7879. loader_ctx->frame_reftype_map_boundary =
  7880. loader_ctx->frame_reftype_map_bottom + 16;
  7881. #endif
  7882. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7883. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7884. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7885. goto fail;
  7886. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7887. #if WASM_ENABLE_EXCE_HANDLING != 0
  7888. func->exception_handler_count = 0;
  7889. #endif
  7890. #if WASM_ENABLE_FAST_INTERP != 0
  7891. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7892. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7893. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7894. error_buf_size)))
  7895. goto fail;
  7896. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7897. loader_ctx->i64_const_max_num = 8;
  7898. if (!(loader_ctx->i64_consts =
  7899. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7900. error_buf, error_buf_size)))
  7901. goto fail;
  7902. loader_ctx->i32_const_max_num = 8;
  7903. if (!(loader_ctx->i32_consts =
  7904. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7905. error_buf, error_buf_size)))
  7906. goto fail;
  7907. loader_ctx->v128_const_max_num = 8;
  7908. if (!(loader_ctx->v128_consts =
  7909. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7910. error_buf, error_buf_size)))
  7911. goto fail;
  7912. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7913. set_error_buf(error_buf, error_buf_size,
  7914. "fast interpreter offset overflow");
  7915. goto fail;
  7916. }
  7917. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7918. loader_ctx->max_dynamic_offset =
  7919. func->param_cell_num + func->local_cell_num;
  7920. #endif
  7921. return loader_ctx;
  7922. fail:
  7923. wasm_loader_ctx_destroy(loader_ctx);
  7924. return NULL;
  7925. }
  7926. static bool
  7927. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7928. uint32 error_buf_size)
  7929. {
  7930. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7931. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7932. /* Increase the frame ref stack */
  7933. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7934. ctx->frame_ref_size + 16);
  7935. ctx->frame_ref_size += 16;
  7936. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7937. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7938. }
  7939. #if WASM_ENABLE_GC != 0
  7940. if (wasm_is_type_multi_byte_type(type)
  7941. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7942. /* Increase the frame reftype map stack */
  7943. bh_assert(
  7944. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7945. * sizeof(WASMRefTypeMap))
  7946. == ctx->frame_reftype_map_size);
  7947. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7948. ctx->frame_reftype_map_size
  7949. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7950. ctx->frame_reftype_map =
  7951. ctx->frame_reftype_map_bottom
  7952. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7953. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7954. ctx->frame_reftype_map_boundary =
  7955. ctx->frame_reftype_map_bottom
  7956. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7957. }
  7958. #endif
  7959. return true;
  7960. fail:
  7961. return false;
  7962. }
  7963. static bool
  7964. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7965. uint32 error_buf_size)
  7966. {
  7967. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7968. uint32 i;
  7969. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7970. return false;
  7971. #if WASM_ENABLE_GC != 0
  7972. if (wasm_is_type_multi_byte_type(type)) {
  7973. WASMRefType *ref_type;
  7974. if (!(ref_type =
  7975. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7976. error_buf, error_buf_size))) {
  7977. return false;
  7978. }
  7979. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7980. /* Increase the frame reftype map stack */
  7981. bh_assert((uint32)((ctx->frame_reftype_map
  7982. - ctx->frame_reftype_map_bottom)
  7983. * sizeof(WASMRefTypeMap))
  7984. == ctx->frame_reftype_map_size);
  7985. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7986. ctx->frame_reftype_map_size,
  7987. ctx->frame_reftype_map_size
  7988. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7989. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7990. + ctx->frame_reftype_map_size
  7991. / ((uint32)sizeof(WASMRefTypeMap));
  7992. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7993. ctx->frame_reftype_map_boundary =
  7994. ctx->frame_reftype_map_bottom
  7995. + ctx->frame_reftype_map_size
  7996. / ((uint32)sizeof(WASMRefTypeMap));
  7997. }
  7998. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7999. ctx->frame_reftype_map->ref_type = ref_type;
  8000. ctx->frame_reftype_map++;
  8001. ctx->reftype_map_num++;
  8002. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  8003. ctx->max_reftype_map_num = ctx->reftype_map_num;
  8004. }
  8005. #endif
  8006. for (i = 0; i < type_cell_num; i++)
  8007. *ctx->frame_ref++ = type;
  8008. ctx->stack_cell_num += type_cell_num;
  8009. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  8010. ctx->max_stack_cell_num = ctx->stack_cell_num;
  8011. if (ctx->max_stack_cell_num > UINT16_MAX) {
  8012. set_error_buf(error_buf, error_buf_size,
  8013. "operand stack depth limit exceeded");
  8014. return false;
  8015. }
  8016. }
  8017. return true;
  8018. #if WASM_ENABLE_GC != 0
  8019. fail:
  8020. return false;
  8021. #endif
  8022. }
  8023. static bool
  8024. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  8025. int32 stack_cell_num,
  8026. #if WASM_ENABLE_GC != 0
  8027. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  8028. #endif
  8029. uint8 type,
  8030. #if WASM_ENABLE_GC != 0
  8031. WASMRefType *ref_type,
  8032. #endif
  8033. char *error_buf, uint32 error_buf_size)
  8034. {
  8035. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  8036. #if WASM_ENABLE_GC != 0
  8037. WASMRefType *frame_reftype = NULL;
  8038. #endif
  8039. if (stack_cell_num < type_cell_num) {
  8040. set_error_buf(error_buf, error_buf_size,
  8041. "type mismatch: expect data but stack was empty");
  8042. return false;
  8043. }
  8044. #if WASM_ENABLE_GC == 0
  8045. for (i = 0; i < type_cell_num; i++) {
  8046. if (*(frame_ref - 1 - i) != type) {
  8047. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8048. "type mismatch: expect ", type2str(type),
  8049. " but got other");
  8050. return false;
  8051. }
  8052. }
  8053. #else
  8054. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  8055. bh_assert(reftype_map_num > 0);
  8056. frame_reftype = (frame_reftype_map - 1)->ref_type;
  8057. }
  8058. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  8059. ref_type, ctx->module->types,
  8060. ctx->module->type_count)) {
  8061. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8062. "type mismatch: expect ", type2str(type),
  8063. " but got other");
  8064. return false;
  8065. }
  8066. for (i = 0; i < type_cell_num - 1; i++) {
  8067. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  8068. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  8069. "type mismatch: expect ", type2str(type),
  8070. " but got other");
  8071. return false;
  8072. }
  8073. }
  8074. #endif
  8075. return true;
  8076. }
  8077. static bool
  8078. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  8079. uint32 error_buf_size)
  8080. {
  8081. int32 block_stack_cell_num =
  8082. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  8083. #if WASM_ENABLE_GC != 0
  8084. int32 reftype_map_num =
  8085. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  8086. #endif
  8087. if (block_stack_cell_num > 0) {
  8088. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8089. /* the stack top is a value of any type, return success */
  8090. return true;
  8091. }
  8092. #if WASM_ENABLE_GC != 0
  8093. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  8094. uint8 stack_top_type = *(ctx->frame_ref - 1);
  8095. WASMRefType *stack_top_ref_type = NULL;
  8096. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  8097. bh_assert(reftype_map_num > 0);
  8098. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  8099. }
  8100. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  8101. ctx->ref_type_tmp, ctx->module->types,
  8102. ctx->module->type_count)) {
  8103. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  8104. uint32 ref_type_struct_size =
  8105. wasm_reftype_struct_size(stack_top_ref_type);
  8106. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  8107. stack_top_ref_type, ref_type_struct_size);
  8108. }
  8109. return true;
  8110. }
  8111. }
  8112. #endif
  8113. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  8114. #if WASM_ENABLE_GC != 0
  8115. ctx->frame_reftype_map, reftype_map_num,
  8116. #endif
  8117. type,
  8118. #if WASM_ENABLE_GC != 0
  8119. ctx->ref_type_tmp,
  8120. #endif
  8121. error_buf, error_buf_size)) {
  8122. return false;
  8123. }
  8124. return true;
  8125. }
  8126. static bool
  8127. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  8128. uint32 error_buf_size)
  8129. {
  8130. BranchBlock *cur_block = ctx->frame_csp - 1;
  8131. int32 available_stack_cell =
  8132. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8133. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  8134. /* Directly return success if current block is in stack
  8135. polymorphic state while stack is empty. */
  8136. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8137. return true;
  8138. if (type == VALUE_TYPE_VOID)
  8139. return true;
  8140. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  8141. return false;
  8142. bh_assert(available_stack_cell > 0);
  8143. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  8144. type = VALUE_TYPE_ANY;
  8145. cell_num_to_pop = 1;
  8146. }
  8147. ctx->frame_ref -= cell_num_to_pop;
  8148. ctx->stack_cell_num -= cell_num_to_pop;
  8149. #if WASM_ENABLE_GC != 0
  8150. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  8151. ctx->frame_reftype_map--;
  8152. ctx->reftype_map_num--;
  8153. }
  8154. #endif
  8155. return true;
  8156. }
  8157. #if WASM_ENABLE_GC != 0
  8158. /* Get the stack top element of current block */
  8159. static bool
  8160. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  8161. WASMRefType **p_ref_type, char *error_buf,
  8162. uint32 error_buf_size)
  8163. {
  8164. BranchBlock *cur_block = ctx->frame_csp - 1;
  8165. int32 available_stack_cell =
  8166. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8167. if (available_stack_cell <= 0) {
  8168. /* Directly return success if current block is in stack
  8169. polymorphic state while stack is empty. */
  8170. if (cur_block->is_stack_polymorphic) {
  8171. *p_type = VALUE_TYPE_ANY;
  8172. return true;
  8173. }
  8174. else {
  8175. set_error_buf(
  8176. error_buf, error_buf_size,
  8177. "type mismatch: expect data but block stack was empty");
  8178. return false;
  8179. }
  8180. }
  8181. *p_type = *(ctx->frame_ref - 1);
  8182. if (wasm_is_type_multi_byte_type(*p_type)) {
  8183. int32 available_reftype_map =
  8184. (int32)(ctx->reftype_map_num
  8185. - (ctx->frame_csp - 1)->reftype_map_num);
  8186. bh_assert(available_reftype_map > 0);
  8187. (void)available_reftype_map;
  8188. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  8189. }
  8190. return true;
  8191. }
  8192. #if WASM_ENABLE_FAST_INTERP != 0
  8193. static bool
  8194. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8195. char *error_buf, uint32 error_buf_size);
  8196. #endif
  8197. /* Check whether the stack top elem is a heap object, and if yes,
  8198. pop and return it */
  8199. static bool
  8200. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  8201. WASMRefType *ref_ht_ret, char *error_buf,
  8202. uint32 error_buf_size)
  8203. {
  8204. uint8 type = 0;
  8205. WASMRefType *ref_type = NULL;
  8206. /* Get stack top element */
  8207. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8208. error_buf_size)) {
  8209. return false;
  8210. }
  8211. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  8212. /* stack top isn't a ref type */
  8213. && !wasm_is_type_reftype(type)) {
  8214. set_error_buf(error_buf, error_buf_size,
  8215. "type mismatch: expect heap object but got others");
  8216. return false;
  8217. }
  8218. /* POP stack top */
  8219. if (wasm_is_type_multi_byte_type(type)) {
  8220. bh_assert(ref_type);
  8221. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8222. wasm_reftype_struct_size(ref_type));
  8223. }
  8224. #if WASM_ENABLE_FAST_INTERP != 0
  8225. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8226. error_buf_size)) {
  8227. return false;
  8228. }
  8229. #else
  8230. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8231. return false;
  8232. }
  8233. #endif
  8234. if (p_type)
  8235. *p_type = type;
  8236. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  8237. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  8238. wasm_reftype_struct_size(ref_type));
  8239. }
  8240. return true;
  8241. }
  8242. /* Check whether the stack top elem is subtype of (ref null ht),
  8243. and if yes, pop it and return the converted (ref ht) */
  8244. static bool
  8245. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  8246. WASMRefType *ref_ht_ret, char *error_buf,
  8247. uint32 error_buf_size)
  8248. {
  8249. uint8 type = 0;
  8250. WASMRefType ref_type = { 0 };
  8251. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  8252. error_buf_size)) {
  8253. return false;
  8254. }
  8255. /* Convert to related (ref ht) and return */
  8256. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  8257. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  8258. */
  8259. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  8260. HEAP_TYPE_ARRAY
  8261. + (type - REF_TYPE_ARRAYREF));
  8262. type = ref_ht_ret->ref_type;
  8263. }
  8264. else if (wasm_is_reftype_htref_nullable(type)
  8265. || wasm_is_reftype_htref_non_nullable(type)) {
  8266. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  8267. wasm_reftype_struct_size(&ref_type));
  8268. /* Convert to (ref ht) */
  8269. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  8270. ref_ht_ret->ref_ht_common.nullable = false;
  8271. type = ref_ht_ret->ref_type;
  8272. }
  8273. *p_type = type;
  8274. return true;
  8275. }
  8276. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  8277. and if yes, pop it and return the type_idx */
  8278. static bool
  8279. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  8280. uint32 *p_type_idx, char *error_buf,
  8281. uint32 error_buf_size)
  8282. {
  8283. uint8 type = 0;
  8284. int32 type_idx = -1;
  8285. WASMRefType *ref_type = NULL;
  8286. /* Get stack top element */
  8287. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  8288. error_buf_size)) {
  8289. return false;
  8290. }
  8291. if (type != VALUE_TYPE_ANY) {
  8292. /* stack top isn't (ref null $t) */
  8293. if (!((wasm_is_reftype_htref_nullable(type)
  8294. || wasm_is_reftype_htref_non_nullable(type))
  8295. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  8296. set_error_buf(error_buf, error_buf_size,
  8297. "type mismatch: expect (ref null $t) but got others");
  8298. return false;
  8299. }
  8300. type_idx = ref_type->ref_ht_typeidx.type_idx;
  8301. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  8302. wasm_reftype_struct_size(ref_type));
  8303. }
  8304. /* POP stack top */
  8305. #if WASM_ENABLE_FAST_INTERP != 0
  8306. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  8307. error_buf_size)) {
  8308. return false;
  8309. }
  8310. #else
  8311. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  8312. return false;
  8313. }
  8314. #endif
  8315. /* Convert to type_idx and return */
  8316. *p_type = type;
  8317. if (type != VALUE_TYPE_ANY)
  8318. *p_type_idx = (uint32)type_idx;
  8319. return true;
  8320. }
  8321. #endif /* WASM_ENABLE_GC != 0 */
  8322. #if WASM_ENABLE_FAST_INTERP == 0
  8323. static bool
  8324. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  8325. uint8 type_push, uint8 type_pop, char *error_buf,
  8326. uint32 error_buf_size)
  8327. {
  8328. for (int i = 0; i < pop_cnt; i++) {
  8329. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8330. error_buf_size))
  8331. return false;
  8332. }
  8333. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8334. return false;
  8335. return true;
  8336. }
  8337. #endif
  8338. static bool
  8339. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  8340. BlockType block_type, uint8 *start_addr,
  8341. char *error_buf, uint32 error_buf_size)
  8342. {
  8343. CHECK_CSP_PUSH();
  8344. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  8345. ctx->frame_csp->label_type = label_type;
  8346. ctx->frame_csp->block_type = block_type;
  8347. ctx->frame_csp->start_addr = start_addr;
  8348. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  8349. #if WASM_ENABLE_GC != 0
  8350. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  8351. #endif
  8352. #if WASM_ENABLE_FAST_INTERP != 0
  8353. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  8354. ctx->frame_csp->patch_list = NULL;
  8355. #endif
  8356. ctx->frame_csp++;
  8357. ctx->csp_num++;
  8358. if (ctx->csp_num > ctx->max_csp_num) {
  8359. ctx->max_csp_num = ctx->csp_num;
  8360. if (ctx->max_csp_num > UINT16_MAX) {
  8361. set_error_buf(error_buf, error_buf_size,
  8362. "label stack depth limit exceeded");
  8363. return false;
  8364. }
  8365. }
  8366. return true;
  8367. fail:
  8368. return false;
  8369. }
  8370. static bool
  8371. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  8372. uint32 error_buf_size)
  8373. {
  8374. CHECK_CSP_POP();
  8375. #if WASM_ENABLE_FAST_INTERP != 0
  8376. if ((ctx->frame_csp - 1)->param_frame_offsets)
  8377. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  8378. #endif
  8379. ctx->frame_csp--;
  8380. ctx->csp_num--;
  8381. return true;
  8382. fail:
  8383. return false;
  8384. }
  8385. #if WASM_ENABLE_FAST_INTERP != 0
  8386. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  8387. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  8388. #define emit_label(opcode) \
  8389. do { \
  8390. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  8391. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8392. } while (0)
  8393. #define skip_label() \
  8394. do { \
  8395. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  8396. LOG_OP("\ndelete last op\n"); \
  8397. } while (0)
  8398. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8399. #if UINTPTR_MAX == UINT64_MAX
  8400. #define emit_label(opcode) \
  8401. do { \
  8402. int32 offset = \
  8403. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  8404. /* emit int32 relative offset in 64-bit target */ \
  8405. wasm_loader_emit_uint32(loader_ctx, offset); \
  8406. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8407. } while (0)
  8408. #else
  8409. #define emit_label(opcode) \
  8410. do { \
  8411. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  8412. /* emit uint32 label address in 32-bit target */ \
  8413. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  8414. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8415. } while (0)
  8416. #endif
  8417. #define skip_label() \
  8418. do { \
  8419. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  8420. LOG_OP("\ndelete last op\n"); \
  8421. } while (0)
  8422. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  8423. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  8424. #define emit_label(opcode) \
  8425. do { \
  8426. wasm_loader_emit_uint8(loader_ctx, opcode); \
  8427. LOG_OP("\nemit_op [%02x]\t", opcode); \
  8428. } while (0)
  8429. #define skip_label() \
  8430. do { \
  8431. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  8432. LOG_OP("\ndelete last op\n"); \
  8433. } while (0)
  8434. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  8435. #define emit_empty_label_addr_and_frame_ip(type) \
  8436. do { \
  8437. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  8438. loader_ctx->p_code_compiled, error_buf, \
  8439. error_buf_size)) \
  8440. goto fail; \
  8441. /* label address, to be patched */ \
  8442. wasm_loader_emit_ptr(loader_ctx, NULL); \
  8443. } while (0)
  8444. #define emit_br_info(frame_csp, is_br) \
  8445. do { \
  8446. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  8447. error_buf_size)) \
  8448. goto fail; \
  8449. } while (0)
  8450. #define LAST_OP_OUTPUT_I32() \
  8451. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  8452. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  8453. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  8454. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  8455. || (last_op >= WASM_OP_I32_WRAP_I64 \
  8456. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  8457. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  8458. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  8459. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  8460. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  8461. || (last_op == EXT_OP_COPY_STACK_TOP)
  8462. #define LAST_OP_OUTPUT_I64() \
  8463. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  8464. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  8465. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  8466. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  8467. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  8468. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  8469. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8470. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8471. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8472. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8473. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8474. #define GET_CONST_OFFSET(type, val) \
  8475. do { \
  8476. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8477. &operand_offset, error_buf, \
  8478. error_buf_size))) \
  8479. goto fail; \
  8480. } while (0)
  8481. #define GET_CONST_F32_OFFSET(type, fval) \
  8482. do { \
  8483. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8484. &operand_offset, error_buf, \
  8485. error_buf_size))) \
  8486. goto fail; \
  8487. } while (0)
  8488. #define GET_CONST_F64_OFFSET(type, fval) \
  8489. do { \
  8490. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8491. &operand_offset, error_buf, \
  8492. error_buf_size))) \
  8493. goto fail; \
  8494. } while (0)
  8495. #define emit_operand(ctx, offset) \
  8496. do { \
  8497. wasm_loader_emit_int16(ctx, offset); \
  8498. LOG_OP("%d\t", offset); \
  8499. } while (0)
  8500. #define emit_byte(ctx, byte) \
  8501. do { \
  8502. wasm_loader_emit_uint8(ctx, byte); \
  8503. LOG_OP("%d\t", byte); \
  8504. } while (0)
  8505. #define emit_uint32(ctx, value) \
  8506. do { \
  8507. wasm_loader_emit_uint32(ctx, value); \
  8508. LOG_OP("%d\t", value); \
  8509. } while (0)
  8510. #define emit_uint64(ctx, value) \
  8511. do { \
  8512. wasm_loader_emit_const(ctx, &value, false); \
  8513. LOG_OP("%lld\t", value); \
  8514. } while (0)
  8515. #define emit_float32(ctx, value) \
  8516. do { \
  8517. wasm_loader_emit_const(ctx, &value, true); \
  8518. LOG_OP("%f\t", value); \
  8519. } while (0)
  8520. #define emit_float64(ctx, value) \
  8521. do { \
  8522. wasm_loader_emit_const(ctx, &value, false); \
  8523. LOG_OP("%f\t", value); \
  8524. } while (0)
  8525. static bool
  8526. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8527. {
  8528. if (!(ctx->p_code_compiled =
  8529. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8530. return false;
  8531. ctx->p_code_compiled_end =
  8532. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8533. /* clean up frame ref */
  8534. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8535. ctx->frame_ref = ctx->frame_ref_bottom;
  8536. ctx->stack_cell_num = 0;
  8537. #if WASM_ENABLE_GC != 0
  8538. /* clean up reftype map */
  8539. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8540. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8541. ctx->reftype_map_num = 0;
  8542. #endif
  8543. /* clean up frame csp */
  8544. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8545. ctx->frame_csp = ctx->frame_csp_bottom;
  8546. ctx->csp_num = 0;
  8547. ctx->max_csp_num = 0;
  8548. /* clean up frame offset */
  8549. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8550. ctx->frame_offset = ctx->frame_offset_bottom;
  8551. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8552. /* init preserved local offsets */
  8553. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8554. /* const buf is reserved */
  8555. return true;
  8556. }
  8557. static void
  8558. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8559. {
  8560. ctx->code_compiled_size += size;
  8561. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8562. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8563. }
  8564. }
  8565. static void
  8566. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8567. {
  8568. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8569. if (ctx->p_code_compiled) {
  8570. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8571. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8572. #endif
  8573. bh_memcpy_s(ctx->p_code_compiled,
  8574. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8575. value, size);
  8576. ctx->p_code_compiled += size;
  8577. }
  8578. else {
  8579. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8580. bh_assert((ctx->code_compiled_size & 1) == 0);
  8581. #endif
  8582. increase_compiled_code_space(ctx, size);
  8583. }
  8584. }
  8585. static void
  8586. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8587. {
  8588. if (ctx->p_code_compiled) {
  8589. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8590. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8591. #endif
  8592. STORE_U32(ctx->p_code_compiled, value);
  8593. ctx->p_code_compiled += sizeof(uint32);
  8594. }
  8595. else {
  8596. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8597. bh_assert((ctx->code_compiled_size & 1) == 0);
  8598. #endif
  8599. increase_compiled_code_space(ctx, sizeof(uint32));
  8600. }
  8601. }
  8602. static void
  8603. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8604. {
  8605. if (ctx->p_code_compiled) {
  8606. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8607. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8608. #endif
  8609. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8610. ctx->p_code_compiled += sizeof(int16);
  8611. }
  8612. else {
  8613. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8614. bh_assert((ctx->code_compiled_size & 1) == 0);
  8615. #endif
  8616. increase_compiled_code_space(ctx, sizeof(uint16));
  8617. }
  8618. }
  8619. static void
  8620. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8621. {
  8622. if (ctx->p_code_compiled) {
  8623. *(ctx->p_code_compiled) = value;
  8624. ctx->p_code_compiled += sizeof(uint8);
  8625. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8626. ctx->p_code_compiled++;
  8627. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8628. #endif
  8629. }
  8630. else {
  8631. increase_compiled_code_space(ctx, sizeof(uint8));
  8632. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8633. increase_compiled_code_space(ctx, sizeof(uint8));
  8634. bh_assert((ctx->code_compiled_size & 1) == 0);
  8635. #endif
  8636. }
  8637. }
  8638. static void
  8639. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8640. {
  8641. if (ctx->p_code_compiled) {
  8642. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8643. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8644. #endif
  8645. STORE_PTR(ctx->p_code_compiled, value);
  8646. ctx->p_code_compiled += sizeof(void *);
  8647. }
  8648. else {
  8649. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8650. bh_assert((ctx->code_compiled_size & 1) == 0);
  8651. #endif
  8652. increase_compiled_code_space(ctx, sizeof(void *));
  8653. }
  8654. }
  8655. static void
  8656. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8657. {
  8658. if (ctx->p_code_compiled) {
  8659. ctx->p_code_compiled -= size;
  8660. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8661. if (size == sizeof(uint8)) {
  8662. ctx->p_code_compiled--;
  8663. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8664. }
  8665. #endif
  8666. }
  8667. else {
  8668. ctx->code_compiled_size -= size;
  8669. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8670. if (size == sizeof(uint8)) {
  8671. ctx->code_compiled_size--;
  8672. bh_assert((ctx->code_compiled_size & 1) == 0);
  8673. }
  8674. #endif
  8675. }
  8676. }
  8677. static bool
  8678. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8679. uint32 local_index, uint32 local_type,
  8680. bool *preserved, char *error_buf,
  8681. uint32 error_buf_size)
  8682. {
  8683. uint32 i = 0;
  8684. int16 preserved_offset = (int16)local_index;
  8685. *preserved = false;
  8686. while (i < loader_ctx->stack_cell_num) {
  8687. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8688. /* move previous local into dynamic space before a set/tee_local opcode
  8689. */
  8690. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8691. if (!(*preserved)) {
  8692. *preserved = true;
  8693. skip_label();
  8694. preserved_offset = loader_ctx->preserved_local_offset;
  8695. if (loader_ctx->p_code_compiled) {
  8696. bh_assert(preserved_offset != (int16)local_index);
  8697. }
  8698. if (is_32bit_type(local_type)) {
  8699. /* Only increase preserve offset in the second traversal */
  8700. if (loader_ctx->p_code_compiled)
  8701. loader_ctx->preserved_local_offset++;
  8702. emit_label(EXT_OP_COPY_STACK_TOP);
  8703. }
  8704. #if WASM_ENABLE_SIMDE != 0
  8705. else if (local_type == VALUE_TYPE_V128) {
  8706. if (loader_ctx->p_code_compiled)
  8707. loader_ctx->preserved_local_offset += 4;
  8708. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8709. }
  8710. #endif
  8711. else {
  8712. if (loader_ctx->p_code_compiled)
  8713. loader_ctx->preserved_local_offset += 2;
  8714. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8715. }
  8716. /* overflow */
  8717. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8718. set_error_buf_v(error_buf, error_buf_size,
  8719. "too much local cells 0x%x",
  8720. loader_ctx->preserved_local_offset);
  8721. return false;
  8722. }
  8723. emit_operand(loader_ctx, local_index);
  8724. emit_operand(loader_ctx, preserved_offset);
  8725. emit_label(opcode);
  8726. }
  8727. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8728. }
  8729. if (cur_type == VALUE_TYPE_V128) {
  8730. i += 4;
  8731. }
  8732. else if (is_32bit_type(cur_type)) {
  8733. i++;
  8734. }
  8735. else {
  8736. i += 2;
  8737. }
  8738. }
  8739. (void)error_buf;
  8740. (void)error_buf_size;
  8741. return true;
  8742. }
  8743. static bool
  8744. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8745. char *error_buf, uint32 error_buf_size)
  8746. {
  8747. uint32 i = 0;
  8748. bool preserve_local;
  8749. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8750. blocks will not influence the value of these locals */
  8751. uint32 frame_offset_cell =
  8752. (uint32)(loader_ctx->frame_offset - loader_ctx->frame_offset_bottom);
  8753. uint32 frame_ref_cell =
  8754. (uint32)(loader_ctx->frame_ref - loader_ctx->frame_ref_bottom);
  8755. if (frame_offset_cell < loader_ctx->stack_cell_num
  8756. || frame_ref_cell < loader_ctx->stack_cell_num) {
  8757. set_error_buf(error_buf, error_buf_size, "stack cell num error");
  8758. return false;
  8759. }
  8760. while (i < loader_ctx->stack_cell_num) {
  8761. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8762. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8763. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8764. && (cur_offset >= 0)) {
  8765. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8766. cur_type, &preserve_local,
  8767. error_buf, error_buf_size)))
  8768. return false;
  8769. }
  8770. if (cur_type == VALUE_TYPE_V128) {
  8771. i += 4;
  8772. }
  8773. else if (is_32bit_type(cur_type)) {
  8774. i++;
  8775. }
  8776. else {
  8777. i += 2;
  8778. }
  8779. }
  8780. return true;
  8781. }
  8782. static bool
  8783. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8784. uint8 *p_code_compiled, char *error_buf,
  8785. uint32 error_buf_size)
  8786. {
  8787. BranchBlockPatch *patch =
  8788. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8789. if (!patch) {
  8790. return false;
  8791. }
  8792. patch->patch_type = patch_type;
  8793. patch->code_compiled = p_code_compiled;
  8794. if (!frame_csp->patch_list) {
  8795. frame_csp->patch_list = patch;
  8796. patch->next = NULL;
  8797. }
  8798. else {
  8799. patch->next = frame_csp->patch_list;
  8800. frame_csp->patch_list = patch;
  8801. }
  8802. return true;
  8803. }
  8804. static void
  8805. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8806. {
  8807. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8808. BranchBlockPatch *node = frame_csp->patch_list;
  8809. BranchBlockPatch *node_prev = NULL, *node_next;
  8810. if (!ctx->p_code_compiled)
  8811. return;
  8812. while (node) {
  8813. node_next = node->next;
  8814. if (node->patch_type == patch_type) {
  8815. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8816. if (node_prev == NULL) {
  8817. frame_csp->patch_list = node_next;
  8818. }
  8819. else {
  8820. node_prev->next = node_next;
  8821. }
  8822. wasm_runtime_free(node);
  8823. }
  8824. else {
  8825. node_prev = node;
  8826. }
  8827. node = node_next;
  8828. }
  8829. }
  8830. static bool
  8831. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8832. bool is_br, char *error_buf, uint32 error_buf_size)
  8833. {
  8834. /* br info layout:
  8835. * a) arity of target block
  8836. * b) total cell num of arity values
  8837. * c) each arity value's cell num
  8838. * d) each arity value's src frame offset
  8839. * e) each arity values's dst dynamic offset
  8840. * f) branch target address
  8841. *
  8842. * Note: b-e are omitted when arity is 0 so that
  8843. * interpreter can recover the br info quickly.
  8844. */
  8845. BlockType *block_type = &frame_csp->block_type;
  8846. uint8 *types = NULL, cell;
  8847. #if WASM_ENABLE_GC != 0
  8848. WASMRefTypeMap *reftype_maps;
  8849. uint32 reftype_map_count;
  8850. #endif
  8851. uint32 arity = 0;
  8852. int32 i;
  8853. int16 *frame_offset = ctx->frame_offset;
  8854. uint16 dynamic_offset;
  8855. /* Note: loop's arity is different from if and block. loop's arity is
  8856. * its parameter count while if and block arity is result count.
  8857. */
  8858. #if WASM_ENABLE_GC == 0
  8859. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8860. arity = block_type_get_param_types(block_type, &types);
  8861. else
  8862. arity = block_type_get_result_types(block_type, &types);
  8863. #else
  8864. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8865. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8866. &reftype_map_count);
  8867. else
  8868. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8869. &reftype_map_count);
  8870. #endif
  8871. /* Part a */
  8872. emit_uint32(ctx, arity);
  8873. if (arity) {
  8874. /* Part b */
  8875. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8876. /* Part c */
  8877. for (i = (int32)arity - 1; i >= 0; i--) {
  8878. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8879. emit_byte(ctx, cell);
  8880. }
  8881. /* Part d */
  8882. for (i = (int32)arity - 1; i >= 0; i--) {
  8883. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8884. frame_offset -= cell;
  8885. emit_operand(ctx, *(int16 *)(frame_offset));
  8886. }
  8887. /* Part e */
  8888. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8889. /* Use start_dynamic_offset which was set in
  8890. copy_params_to_dynamic_space */
  8891. dynamic_offset = frame_csp->start_dynamic_offset
  8892. + wasm_get_cell_num(types, arity);
  8893. else
  8894. dynamic_offset =
  8895. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8896. if (is_br)
  8897. ctx->dynamic_offset = dynamic_offset;
  8898. for (i = (int32)arity - 1; i >= 0; i--) {
  8899. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8900. dynamic_offset -= cell;
  8901. emit_operand(ctx, dynamic_offset);
  8902. }
  8903. }
  8904. /* Part f */
  8905. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8906. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8907. }
  8908. else {
  8909. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8910. error_buf, error_buf_size))
  8911. return false;
  8912. /* label address, to be patched */
  8913. wasm_loader_emit_ptr(ctx, NULL);
  8914. }
  8915. return true;
  8916. }
  8917. static bool
  8918. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8919. bool disable_emit, int16 operand_offset,
  8920. char *error_buf, uint32 error_buf_size)
  8921. {
  8922. uint32 cell_num_to_push, i;
  8923. if (type == VALUE_TYPE_VOID)
  8924. return true;
  8925. /* only check memory overflow in first traverse */
  8926. if (ctx->p_code_compiled == NULL) {
  8927. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8928. return false;
  8929. }
  8930. if (disable_emit)
  8931. *(ctx->frame_offset)++ = operand_offset;
  8932. else {
  8933. emit_operand(ctx, ctx->dynamic_offset);
  8934. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8935. ctx->dynamic_offset++;
  8936. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8937. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8938. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8939. goto fail;
  8940. }
  8941. }
  8942. }
  8943. if (is_32bit_type(type))
  8944. return true;
  8945. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8946. for (i = 0; i < cell_num_to_push; i++) {
  8947. if (ctx->p_code_compiled == NULL) {
  8948. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8949. return false;
  8950. }
  8951. ctx->frame_offset++;
  8952. if (!disable_emit) {
  8953. ctx->dynamic_offset++;
  8954. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8955. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8956. if (ctx->max_dynamic_offset >= INT16_MAX)
  8957. goto fail;
  8958. }
  8959. }
  8960. }
  8961. return true;
  8962. fail:
  8963. set_error_buf(error_buf, error_buf_size,
  8964. "fast interpreter offset overflow");
  8965. return false;
  8966. }
  8967. /* This function should be in front of wasm_loader_pop_frame_ref
  8968. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8969. will be modified by wasm_loader_pop_frame_ref */
  8970. static bool
  8971. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8972. char *error_buf, uint32 error_buf_size)
  8973. {
  8974. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8975. then current block is the function block */
  8976. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8977. BranchBlock *cur_block = ctx->frame_csp - depth;
  8978. int32 available_stack_cell =
  8979. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8980. uint32 cell_num_to_pop;
  8981. /* Directly return success if current block is in stack
  8982. polymorphic state while stack is empty. */
  8983. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8984. return true;
  8985. if (type == VALUE_TYPE_VOID)
  8986. return true;
  8987. /* Change type to ANY when the stack top is ANY, so as to avoid
  8988. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8989. two offsets */
  8990. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8991. type = VALUE_TYPE_ANY;
  8992. cell_num_to_pop = wasm_value_type_cell_num(type);
  8993. /* Check the offset stack bottom to ensure the frame offset
  8994. stack will not go underflow. But we don't thrown error
  8995. and return true here, because the error msg should be
  8996. given in wasm_loader_pop_frame_ref */
  8997. if (!check_offset_pop(ctx, cell_num_to_pop))
  8998. return true;
  8999. ctx->frame_offset -= cell_num_to_pop;
  9000. if (check_dynamic_offset_pop(ctx, cell_num_to_pop)
  9001. && (*(ctx->frame_offset) > ctx->start_dynamic_offset)
  9002. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  9003. ctx->dynamic_offset -= cell_num_to_pop;
  9004. emit_operand(ctx, *(ctx->frame_offset));
  9005. (void)error_buf;
  9006. (void)error_buf_size;
  9007. return true;
  9008. }
  9009. static bool
  9010. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  9011. bool disable_emit, int16 operand_offset,
  9012. char *error_buf, uint32 error_buf_size)
  9013. {
  9014. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  9015. error_buf, error_buf_size)))
  9016. return false;
  9017. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  9018. return false;
  9019. return true;
  9020. }
  9021. static bool
  9022. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  9023. char *error_buf, uint32 error_buf_size)
  9024. {
  9025. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  9026. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  9027. return false;
  9028. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  9029. return false;
  9030. return true;
  9031. }
  9032. static bool
  9033. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  9034. uint8 type_push, uint8 type_pop,
  9035. bool disable_emit, int16 operand_offset,
  9036. char *error_buf, uint32 error_buf_size)
  9037. {
  9038. uint8 i;
  9039. for (i = 0; i < pop_cnt; i++) {
  9040. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  9041. error_buf_size))
  9042. return false;
  9043. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  9044. error_buf_size))
  9045. return false;
  9046. }
  9047. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  9048. operand_offset, error_buf,
  9049. error_buf_size))
  9050. return false;
  9051. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  9052. return false;
  9053. return true;
  9054. }
  9055. static int
  9056. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  9057. {
  9058. int64 i64_const1 = *(int64 *)p_i64_const1;
  9059. int64 i64_const2 = *(int64 *)p_i64_const2;
  9060. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  9061. }
  9062. static int
  9063. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  9064. {
  9065. int32 i32_const1 = *(int32 *)p_i32_const1;
  9066. int32 i32_const2 = *(int32 *)p_i32_const2;
  9067. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  9068. }
  9069. static int
  9070. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  9071. {
  9072. V128 v128_const1 = *(V128 *)p_v128_const1;
  9073. V128 v128_const2 = *(V128 *)p_v128_const2;
  9074. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  9075. }
  9076. static bool
  9077. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  9078. int16 *offset, char *error_buf,
  9079. uint32 error_buf_size)
  9080. {
  9081. if (!ctx->p_code_compiled) {
  9082. /* Treat i64 and f64 as the same by reading i64 value from
  9083. the raw bytes */
  9084. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  9085. /* No slot left, emit const instead */
  9086. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  9087. *offset = 0;
  9088. return true;
  9089. }
  9090. /* Traverse the list if the const num is small */
  9091. if (ctx->i64_const_num < 10) {
  9092. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  9093. if (ctx->i64_consts[i] == *(int64 *)value) {
  9094. *offset = -1;
  9095. return true;
  9096. }
  9097. }
  9098. }
  9099. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  9100. MEM_REALLOC(ctx->i64_consts,
  9101. sizeof(int64) * ctx->i64_const_max_num,
  9102. sizeof(int64) * (ctx->i64_const_max_num * 2));
  9103. ctx->i64_const_max_num *= 2;
  9104. }
  9105. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  9106. }
  9107. else if (type == VALUE_TYPE_V128) {
  9108. /* No slot left, emit const instead */
  9109. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  9110. *offset = 0;
  9111. return true;
  9112. }
  9113. /* Traverse the list if the const num is small */
  9114. if (ctx->v128_const_num < 10) {
  9115. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  9116. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  9117. == 0) {
  9118. *offset = -1;
  9119. return true;
  9120. }
  9121. }
  9122. }
  9123. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  9124. MEM_REALLOC(ctx->v128_consts,
  9125. sizeof(V128) * ctx->v128_const_max_num,
  9126. sizeof(V128) * (ctx->v128_const_max_num * 2));
  9127. ctx->v128_const_max_num *= 2;
  9128. }
  9129. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  9130. }
  9131. else {
  9132. /* Treat i32 and f32 as the same by reading i32 value from
  9133. the raw bytes */
  9134. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  9135. /* No slot left, emit const instead */
  9136. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  9137. *offset = 0;
  9138. return true;
  9139. }
  9140. /* Traverse the list if the const num is small */
  9141. if (ctx->i32_const_num < 10) {
  9142. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  9143. if (ctx->i32_consts[i] == *(int32 *)value) {
  9144. *offset = -1;
  9145. return true;
  9146. }
  9147. }
  9148. }
  9149. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  9150. MEM_REALLOC(ctx->i32_consts,
  9151. sizeof(int32) * ctx->i32_const_max_num,
  9152. sizeof(int32) * (ctx->i32_const_max_num * 2));
  9153. ctx->i32_const_max_num *= 2;
  9154. }
  9155. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  9156. }
  9157. *offset = -1;
  9158. return true;
  9159. }
  9160. else {
  9161. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  9162. int64 key = *(int64 *)value, *i64_const;
  9163. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  9164. sizeof(int64), cmp_i64_const);
  9165. if (!i64_const) { /* not found, emit const instead */
  9166. *offset = 0;
  9167. return true;
  9168. }
  9169. /* constant index is encoded as negative value */
  9170. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  9171. + (int32)(i64_const - ctx->i64_consts) * 2;
  9172. }
  9173. else if (type == VALUE_TYPE_V128) {
  9174. V128 key = *(V128 *)value, *v128_const;
  9175. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  9176. sizeof(V128), cmp_v128_const);
  9177. if (!v128_const) { /* not found, emit const instead */
  9178. *offset = 0;
  9179. return true;
  9180. }
  9181. /* constant index is encoded as negative value */
  9182. *offset = -(int32)(ctx->v128_const_num)
  9183. + (int32)(v128_const - ctx->v128_consts);
  9184. }
  9185. else {
  9186. int32 key = *(int32 *)value, *i32_const;
  9187. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  9188. sizeof(int32), cmp_i32_const);
  9189. if (!i32_const) { /* not found, emit const instead */
  9190. *offset = 0;
  9191. return true;
  9192. }
  9193. /* constant index is encoded as negative value */
  9194. *offset = -(int32)(ctx->i32_const_num)
  9195. + (int32)(i32_const - ctx->i32_consts);
  9196. }
  9197. return true;
  9198. }
  9199. fail:
  9200. return false;
  9201. }
  9202. /*
  9203. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  9204. -- Mostly used for the binary / compare operation
  9205. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  9206. -- Mostly used in block / control instructions
  9207. The POP will always emit the offset on the top of the frame_offset stack
  9208. PUSH can be used in two ways:
  9209. 1. directly PUSH:
  9210. PUSH_XXX();
  9211. will allocate a dynamic space and emit
  9212. 2. silent PUSH:
  9213. operand_offset = xxx; disable_emit = true;
  9214. PUSH_XXX();
  9215. only push the frame_offset stack, no emit
  9216. */
  9217. #define TEMPLATE_PUSH(Type) \
  9218. do { \
  9219. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9220. disable_emit, operand_offset, \
  9221. error_buf, error_buf_size)) \
  9222. goto fail; \
  9223. } while (0)
  9224. #define TEMPLATE_PUSH_REF(Type) \
  9225. do { \
  9226. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  9227. operand_offset, error_buf, \
  9228. error_buf_size)) \
  9229. goto fail; \
  9230. } while (0)
  9231. #define TEMPLATE_POP(Type) \
  9232. do { \
  9233. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  9234. error_buf, error_buf_size)) \
  9235. goto fail; \
  9236. } while (0)
  9237. #define TEMPLATE_POP_REF(Type) \
  9238. do { \
  9239. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  9240. error_buf_size)) \
  9241. goto fail; \
  9242. } while (0)
  9243. #define PUSH_OFFSET_TYPE(type) \
  9244. do { \
  9245. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  9246. operand_offset, error_buf, \
  9247. error_buf_size))) \
  9248. goto fail; \
  9249. } while (0)
  9250. #define POP_OFFSET_TYPE(type) \
  9251. do { \
  9252. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  9253. error_buf_size))) \
  9254. goto fail; \
  9255. } while (0)
  9256. #define POP_AND_PUSH(type_pop, type_push) \
  9257. do { \
  9258. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9259. loader_ctx, 1, type_push, type_pop, disable_emit, \
  9260. operand_offset, error_buf, error_buf_size))) \
  9261. goto fail; \
  9262. } while (0)
  9263. /* type of POPs should be the same */
  9264. #define POP2_AND_PUSH(type_pop, type_push) \
  9265. do { \
  9266. if (!(wasm_loader_push_pop_frame_ref_offset( \
  9267. loader_ctx, 2, type_push, type_pop, disable_emit, \
  9268. operand_offset, error_buf, error_buf_size))) \
  9269. goto fail; \
  9270. } while (0)
  9271. #else /* WASM_ENABLE_FAST_INTERP */
  9272. #define TEMPLATE_PUSH(Type) \
  9273. do { \
  9274. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9275. error_buf, error_buf_size))) \
  9276. goto fail; \
  9277. } while (0)
  9278. #define TEMPLATE_PUSH_REF(Type) \
  9279. do { \
  9280. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  9281. error_buf_size))) \
  9282. goto fail; \
  9283. } while (0)
  9284. #define TEMPLATE_POP(Type) \
  9285. do { \
  9286. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  9287. error_buf, error_buf_size))) \
  9288. goto fail; \
  9289. } while (0)
  9290. #define TEMPLATE_POP_REF(Type) \
  9291. do { \
  9292. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  9293. error_buf_size))) \
  9294. goto fail; \
  9295. } while (0)
  9296. #define POP_AND_PUSH(type_pop, type_push) \
  9297. do { \
  9298. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  9299. type_pop, error_buf, \
  9300. error_buf_size))) \
  9301. goto fail; \
  9302. } while (0)
  9303. /* type of POPs should be the same */
  9304. #define POP2_AND_PUSH(type_pop, type_push) \
  9305. do { \
  9306. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  9307. type_pop, error_buf, \
  9308. error_buf_size))) \
  9309. goto fail; \
  9310. } while (0)
  9311. #endif /* WASM_ENABLE_FAST_INTERP */
  9312. #define PUSH_I32() TEMPLATE_PUSH(I32)
  9313. #define PUSH_F32() TEMPLATE_PUSH(F32)
  9314. #define PUSH_I64() TEMPLATE_PUSH(I64)
  9315. #define PUSH_F64() TEMPLATE_PUSH(F64)
  9316. #define PUSH_V128() TEMPLATE_PUSH(V128)
  9317. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  9318. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  9319. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  9320. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  9321. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  9322. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  9323. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  9324. #define POP_I32() TEMPLATE_POP(I32)
  9325. #define POP_F32() TEMPLATE_POP(F32)
  9326. #define POP_I64() TEMPLATE_POP(I64)
  9327. #define POP_F64() TEMPLATE_POP(F64)
  9328. #define POP_V128() TEMPLATE_POP(V128)
  9329. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  9330. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  9331. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  9332. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  9333. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  9334. #if WASM_ENABLE_FAST_INTERP != 0
  9335. static bool
  9336. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  9337. bool disable_emit, char *error_buf, uint32 error_buf_size)
  9338. {
  9339. int16 operand_offset = 0;
  9340. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  9341. : loader_ctx->frame_csp;
  9342. BlockType *block_type = &block->block_type;
  9343. uint8 *return_types = NULL;
  9344. #if WASM_ENABLE_GC != 0
  9345. WASMRefTypeMap *reftype_maps = NULL;
  9346. uint32 reftype_map_count;
  9347. #endif
  9348. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  9349. int32 i = 0;
  9350. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  9351. *frame_offset_org = NULL;
  9352. #if WASM_ENABLE_GC == 0
  9353. return_count = block_type_get_result_types(block_type, &return_types);
  9354. #else
  9355. return_count = block_type_get_result_types(
  9356. block_type, &return_types, &reftype_maps, &reftype_map_count);
  9357. #endif
  9358. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  9359. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  9360. if (return_count == 1) {
  9361. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  9362. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  9363. /* insert op_copy before else opcode */
  9364. if (opcode == WASM_OP_ELSE)
  9365. skip_label();
  9366. #if WASM_ENABLE_SIMDE != 0
  9367. if (cell == 4) {
  9368. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  9369. }
  9370. #endif
  9371. if (cell <= 2) {
  9372. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  9373. : EXT_OP_COPY_STACK_TOP_I64);
  9374. }
  9375. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  9376. emit_operand(loader_ctx, block->dynamic_offset);
  9377. if (opcode == WASM_OP_ELSE) {
  9378. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  9379. }
  9380. else {
  9381. loader_ctx->frame_offset -= cell;
  9382. loader_ctx->dynamic_offset = block->dynamic_offset;
  9383. PUSH_OFFSET_TYPE(return_types[0]);
  9384. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9385. }
  9386. if (opcode == WASM_OP_ELSE)
  9387. emit_label(opcode);
  9388. }
  9389. return true;
  9390. }
  9391. /* Copy stack top values to block's results which are in dynamic space.
  9392. * The instruction format:
  9393. * Part a: values count
  9394. * Part b: all values total cell num
  9395. * Part c: each value's cell_num, src offset and dst offset
  9396. * Part d: each value's src offset and dst offset
  9397. * Part e: each value's dst offset
  9398. */
  9399. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  9400. dynamic_offset = dynamic_offset_org =
  9401. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  9402. /* First traversal to get the count of values needed to be copied. */
  9403. for (i = (int32)return_count - 1; i >= 0; i--) {
  9404. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  9405. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  9406. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  9407. goto fail;
  9408. }
  9409. if (cells == 4) {
  9410. bool needs_copy = false;
  9411. int16 v128_dynamic = dynamic_offset - cells;
  9412. for (int j = 0; j < 4; j++) {
  9413. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  9414. needs_copy = true;
  9415. break;
  9416. }
  9417. }
  9418. if (needs_copy) {
  9419. value_count++;
  9420. total_cel_num += cells;
  9421. }
  9422. frame_offset -= cells;
  9423. dynamic_offset = v128_dynamic;
  9424. }
  9425. else {
  9426. frame_offset -= cells;
  9427. dynamic_offset -= cells;
  9428. if (dynamic_offset != *frame_offset) {
  9429. value_count++;
  9430. total_cel_num += cells;
  9431. }
  9432. }
  9433. }
  9434. if (value_count) {
  9435. uint32 j = 0;
  9436. uint8 *emit_data = NULL, *cells = NULL;
  9437. int16 *src_offsets = NULL;
  9438. uint16 *dst_offsets = NULL;
  9439. uint64 size =
  9440. (uint64)value_count
  9441. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  9442. /* Allocate memory for the emit data */
  9443. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9444. return false;
  9445. cells = emit_data;
  9446. src_offsets = (int16 *)(cells + value_count);
  9447. dst_offsets = (uint16 *)(src_offsets + value_count);
  9448. /* insert op_copy before else opcode */
  9449. if (opcode == WASM_OP_ELSE)
  9450. skip_label();
  9451. emit_label(EXT_OP_COPY_STACK_VALUES);
  9452. /* Part a) */
  9453. emit_uint32(loader_ctx, value_count);
  9454. /* Part b) */
  9455. emit_uint32(loader_ctx, total_cel_num);
  9456. /* Second traversal to get each value's cell num, src offset and dst
  9457. * offset. */
  9458. frame_offset = frame_offset_org;
  9459. dynamic_offset = dynamic_offset_org;
  9460. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  9461. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  9462. if (cell == 4) {
  9463. bool needs_copy = false;
  9464. int16 v128_dynamic = dynamic_offset - cell;
  9465. for (int k = 0; k < 4; k++) {
  9466. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  9467. needs_copy = true;
  9468. break;
  9469. }
  9470. }
  9471. if (needs_copy) {
  9472. cells[j] = cell;
  9473. src_offsets[j] = *(frame_offset - cell);
  9474. dst_offsets[j] = v128_dynamic;
  9475. j++;
  9476. }
  9477. frame_offset -= cell;
  9478. dynamic_offset = v128_dynamic;
  9479. }
  9480. else {
  9481. frame_offset -= cell;
  9482. dynamic_offset -= cell;
  9483. if (dynamic_offset != *frame_offset) {
  9484. cells[j] = cell;
  9485. /* src offset */
  9486. src_offsets[j] = *frame_offset;
  9487. /* dst offset */
  9488. dst_offsets[j] = dynamic_offset;
  9489. j++;
  9490. }
  9491. }
  9492. if (opcode == WASM_OP_ELSE) {
  9493. if (cell == 4) {
  9494. for (int k = 0; k < cell; k++) {
  9495. *(frame_offset + k) = dynamic_offset + k;
  9496. }
  9497. }
  9498. else {
  9499. *frame_offset = dynamic_offset;
  9500. }
  9501. }
  9502. else {
  9503. loader_ctx->frame_offset = frame_offset;
  9504. loader_ctx->dynamic_offset = dynamic_offset;
  9505. if (!(wasm_loader_push_frame_offset(
  9506. loader_ctx, return_types[i], disable_emit,
  9507. operand_offset, error_buf, error_buf_size))) {
  9508. wasm_runtime_free(emit_data);
  9509. goto fail;
  9510. }
  9511. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9512. loader_ctx->frame_offset = frame_offset_org;
  9513. loader_ctx->dynamic_offset = dynamic_offset_org;
  9514. }
  9515. }
  9516. bh_assert(j == value_count);
  9517. /* Emit the cells, src_offsets and dst_offsets */
  9518. for (j = 0; j < value_count; j++)
  9519. emit_byte(loader_ctx, cells[j]);
  9520. for (j = 0; j < value_count; j++)
  9521. emit_operand(loader_ctx, src_offsets[j]);
  9522. for (j = 0; j < value_count; j++)
  9523. emit_operand(loader_ctx, dst_offsets[j]);
  9524. if (opcode == WASM_OP_ELSE)
  9525. emit_label(opcode);
  9526. wasm_runtime_free(emit_data);
  9527. }
  9528. return true;
  9529. fail:
  9530. return false;
  9531. }
  9532. #endif /* WASM_ENABLE_FAST_INTERP */
  9533. #define PUSH_TYPE(type) \
  9534. do { \
  9535. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9536. error_buf_size))) \
  9537. goto fail; \
  9538. } while (0)
  9539. #define POP_TYPE(type) \
  9540. do { \
  9541. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9542. error_buf_size))) \
  9543. goto fail; \
  9544. } while (0)
  9545. #if WASM_ENABLE_GC == 0
  9546. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9547. do { \
  9548. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9549. _start_addr, error_buf, \
  9550. error_buf_size)) \
  9551. goto fail; \
  9552. } while (0)
  9553. #define POP_CSP() \
  9554. do { \
  9555. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9556. goto fail; \
  9557. } while (0)
  9558. #else
  9559. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9560. do { \
  9561. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9562. _start_addr, error_buf, \
  9563. error_buf_size)) \
  9564. goto fail; \
  9565. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9566. error_buf, error_buf_size)) { \
  9567. goto fail; \
  9568. } \
  9569. } while (0)
  9570. #define POP_CSP() \
  9571. do { \
  9572. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9573. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9574. goto fail; \
  9575. } while (0)
  9576. #endif /* end of WASM_ENABLE_GC == 0 */
  9577. #if WASM_ENABLE_GC == 0
  9578. #define GET_LOCAL_REFTYPE() (void)0
  9579. #else
  9580. #define GET_LOCAL_REFTYPE() \
  9581. do { \
  9582. if (wasm_is_type_multi_byte_type(local_type)) { \
  9583. WASMRefType *_ref_type; \
  9584. if (local_idx < param_count) \
  9585. _ref_type = wasm_reftype_map_find( \
  9586. param_reftype_maps, param_reftype_map_count, local_idx); \
  9587. else \
  9588. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9589. local_reftype_map_count, \
  9590. local_idx - param_count); \
  9591. bh_assert(_ref_type); \
  9592. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9593. wasm_reftype_struct_size(_ref_type)); \
  9594. } \
  9595. } while (0)
  9596. #endif /* end of WASM_ENABLE_GC == 0 */
  9597. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9598. do { \
  9599. read_leb_uint32(p, p_end, local_idx); \
  9600. if (local_idx >= param_count + local_count) { \
  9601. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9602. goto fail; \
  9603. } \
  9604. local_type = local_idx < param_count \
  9605. ? param_types[local_idx] \
  9606. : local_types[local_idx - param_count]; \
  9607. local_offset = local_offsets[local_idx]; \
  9608. GET_LOCAL_REFTYPE(); \
  9609. } while (0)
  9610. static bool
  9611. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9612. {
  9613. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9614. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9615. return false;
  9616. }
  9617. return true;
  9618. }
  9619. #define CHECK_MEMORY() \
  9620. do { \
  9621. if (!check_memory(module, error_buf, error_buf_size)) \
  9622. goto fail; \
  9623. } while (0)
  9624. static bool
  9625. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9626. uint32 error_buf_size)
  9627. {
  9628. uint8 mem_access_aligns[] = {
  9629. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9630. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9631. };
  9632. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9633. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9634. set_error_buf(error_buf, error_buf_size,
  9635. "invalid memop flags: alignment must not be larger "
  9636. "than natural");
  9637. return false;
  9638. }
  9639. return true;
  9640. }
  9641. #if WASM_ENABLE_SIMD != 0
  9642. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9643. || (WASM_ENABLE_FAST_INTERP != 0)
  9644. static bool
  9645. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9646. uint32 error_buf_size)
  9647. {
  9648. uint8 mem_access_aligns[] = {
  9649. 4, /* load */
  9650. 3, 3, 3, 3, 3, 3, /* load and extend */
  9651. 0, 1, 2, 3, /* load and splat */
  9652. 4, /* store */
  9653. };
  9654. uint8 mem_access_aligns_load_lane[] = {
  9655. 0, 1, 2, 3, /* load lane */
  9656. 0, 1, 2, 3, /* store lane */
  9657. 2, 3 /* store zero */
  9658. };
  9659. if (!((opcode <= SIMD_v128_store)
  9660. || (SIMD_v128_load8_lane <= opcode
  9661. && opcode <= SIMD_v128_load64_zero))) {
  9662. set_error_buf(error_buf, error_buf_size,
  9663. "the opcode doesn't include memarg");
  9664. return false;
  9665. }
  9666. if ((opcode <= SIMD_v128_store
  9667. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9668. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9669. && align > mem_access_aligns_load_lane[opcode
  9670. - SIMD_v128_load8_lane])) {
  9671. set_error_buf(error_buf, error_buf_size,
  9672. "invalid memop flags: alignment must not be larger "
  9673. "than natural");
  9674. return false;
  9675. }
  9676. return true;
  9677. }
  9678. static bool
  9679. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9680. uint32 error_buf_size)
  9681. {
  9682. switch (opcode) {
  9683. case SIMD_i8x16_extract_lane_s:
  9684. case SIMD_i8x16_extract_lane_u:
  9685. case SIMD_i8x16_replace_lane:
  9686. if (lane >= 16) {
  9687. goto fail;
  9688. }
  9689. break;
  9690. case SIMD_i16x8_extract_lane_s:
  9691. case SIMD_i16x8_extract_lane_u:
  9692. case SIMD_i16x8_replace_lane:
  9693. if (lane >= 8) {
  9694. goto fail;
  9695. }
  9696. break;
  9697. case SIMD_i32x4_extract_lane:
  9698. case SIMD_i32x4_replace_lane:
  9699. case SIMD_f32x4_extract_lane:
  9700. case SIMD_f32x4_replace_lane:
  9701. if (lane >= 4) {
  9702. goto fail;
  9703. }
  9704. break;
  9705. case SIMD_i64x2_extract_lane:
  9706. case SIMD_i64x2_replace_lane:
  9707. case SIMD_f64x2_extract_lane:
  9708. case SIMD_f64x2_replace_lane:
  9709. if (lane >= 2) {
  9710. goto fail;
  9711. }
  9712. break;
  9713. case SIMD_v128_load8_lane:
  9714. case SIMD_v128_load16_lane:
  9715. case SIMD_v128_load32_lane:
  9716. case SIMD_v128_load64_lane:
  9717. case SIMD_v128_store8_lane:
  9718. case SIMD_v128_store16_lane:
  9719. case SIMD_v128_store32_lane:
  9720. case SIMD_v128_store64_lane:
  9721. case SIMD_v128_load32_zero:
  9722. case SIMD_v128_load64_zero:
  9723. {
  9724. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9725. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9726. goto fail;
  9727. }
  9728. break;
  9729. }
  9730. default:
  9731. goto fail;
  9732. }
  9733. return true;
  9734. fail:
  9735. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9736. return false;
  9737. }
  9738. static bool
  9739. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9740. {
  9741. uint8 i;
  9742. for (i = 0; i != 16; ++i) {
  9743. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9744. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9745. return false;
  9746. }
  9747. }
  9748. return true;
  9749. }
  9750. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9751. #endif /* end of WASM_ENABLE_SIMD */
  9752. #if WASM_ENABLE_SHARED_MEMORY != 0
  9753. static bool
  9754. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9755. uint32 error_buf_size)
  9756. {
  9757. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9758. uint8 mem_access_aligns[] = {
  9759. 2, 3, 0, 1, 0, 1, 2,
  9760. };
  9761. uint8 expect;
  9762. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9763. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9764. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9765. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9766. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9767. }
  9768. else {
  9769. /* 7 opcodes in every group */
  9770. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9771. }
  9772. if (align != expect) {
  9773. set_error_buf(error_buf, error_buf_size,
  9774. "alignment isn't equal to natural");
  9775. return false;
  9776. }
  9777. return true;
  9778. }
  9779. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9780. static bool
  9781. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9782. char *error_buf, uint32 error_buf_size)
  9783. {
  9784. BranchBlock *target_block, *cur_block;
  9785. BlockType *target_block_type;
  9786. uint8 type, *types = NULL, *frame_ref;
  9787. uint32 arity = 0;
  9788. int32 i, available_stack_cell;
  9789. uint16 cell_num;
  9790. #if WASM_ENABLE_GC != 0
  9791. WASMRefTypeMap *frame_reftype_map;
  9792. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9793. WASMRefType *ref_type;
  9794. uint32 reftype_map_count = 0;
  9795. int32 available_reftype_map;
  9796. bool is_type_multi_byte;
  9797. #endif
  9798. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9799. uint8 *frame_ref_after_popped = NULL;
  9800. uint8 frame_ref_tmp[4] = { 0 };
  9801. uint8 *frame_ref_buf = frame_ref_tmp;
  9802. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9803. #if WASM_ENABLE_GC != 0
  9804. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9805. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9806. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9807. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9808. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9809. #endif
  9810. #if WASM_ENABLE_FAST_INTERP != 0
  9811. int16 *frame_offset_old = loader_ctx->frame_offset;
  9812. int16 *frame_offset_after_popped = NULL;
  9813. int16 frame_offset_tmp[4] = { 0 };
  9814. int16 *frame_offset_buf = frame_offset_tmp;
  9815. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9816. #endif
  9817. bool ret = false;
  9818. bh_assert(loader_ctx->csp_num > 0);
  9819. if (loader_ctx->csp_num - 1 < depth) {
  9820. set_error_buf(error_buf, error_buf_size,
  9821. "unknown label, "
  9822. "unexpected end of section or function");
  9823. return false;
  9824. }
  9825. cur_block = loader_ctx->frame_csp - 1;
  9826. target_block = loader_ctx->frame_csp - (depth + 1);
  9827. target_block_type = &target_block->block_type;
  9828. frame_ref = loader_ctx->frame_ref;
  9829. #if WASM_ENABLE_GC != 0
  9830. frame_reftype_map = loader_ctx->frame_reftype_map;
  9831. #endif
  9832. /* Note: loop's arity is different from if and block. loop's arity is
  9833. * its parameter count while if and block arity is result count.
  9834. */
  9835. #if WASM_ENABLE_GC == 0
  9836. if (target_block->label_type == LABEL_TYPE_LOOP)
  9837. arity = block_type_get_param_types(target_block_type, &types);
  9838. else
  9839. arity = block_type_get_result_types(target_block_type, &types);
  9840. #else
  9841. if (target_block->label_type == LABEL_TYPE_LOOP)
  9842. arity = block_type_get_param_types(target_block_type, &types,
  9843. &reftype_maps, &reftype_map_count);
  9844. else
  9845. arity = block_type_get_result_types(target_block_type, &types,
  9846. &reftype_maps, &reftype_map_count);
  9847. #endif
  9848. /* If the stack is in polymorphic state, just clear the stack
  9849. * and then re-push the values to make the stack top values
  9850. * match block type. */
  9851. if (cur_block->is_stack_polymorphic) {
  9852. #if WASM_ENABLE_GC != 0
  9853. int32 j = (int32)reftype_map_count - 1;
  9854. #endif
  9855. for (i = (int32)arity - 1; i >= 0; i--) {
  9856. #if WASM_ENABLE_GC != 0
  9857. if (wasm_is_type_multi_byte_type(types[i])) {
  9858. bh_assert(reftype_maps[j].index == i);
  9859. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9860. reftype_maps[j].ref_type,
  9861. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9862. j--;
  9863. }
  9864. #endif
  9865. #if WASM_ENABLE_FAST_INTERP != 0
  9866. POP_OFFSET_TYPE(types[i]);
  9867. #endif
  9868. POP_TYPE(types[i]);
  9869. }
  9870. /* Backup stack data since it may be changed in the below
  9871. push operations, and the stack data may be used when
  9872. checking other target blocks of opcode br_table */
  9873. if (opcode == WASM_OP_BR_TABLE) {
  9874. uint64 total_size;
  9875. frame_ref_after_popped = loader_ctx->frame_ref;
  9876. total_size = (uint64)sizeof(uint8)
  9877. * (frame_ref_old - frame_ref_after_popped);
  9878. if (total_size > sizeof(frame_ref_tmp)
  9879. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9880. error_buf_size))) {
  9881. goto fail;
  9882. }
  9883. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9884. frame_ref_after_popped, (uint32)total_size);
  9885. #if WASM_ENABLE_GC != 0
  9886. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9887. total_size =
  9888. (uint64)sizeof(WASMRefTypeMap)
  9889. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9890. if (total_size > sizeof(frame_reftype_map_tmp)
  9891. && !(frame_reftype_map_buf = loader_malloc(
  9892. total_size, error_buf, error_buf_size))) {
  9893. goto fail;
  9894. }
  9895. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9896. frame_reftype_map_after_popped, (uint32)total_size);
  9897. #endif
  9898. #if WASM_ENABLE_FAST_INTERP != 0
  9899. frame_offset_after_popped = loader_ctx->frame_offset;
  9900. total_size = (uint64)sizeof(int16)
  9901. * (frame_offset_old - frame_offset_after_popped);
  9902. if (total_size > sizeof(frame_offset_tmp)
  9903. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9904. error_buf_size))) {
  9905. goto fail;
  9906. }
  9907. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9908. frame_offset_after_popped, (uint32)total_size);
  9909. #endif
  9910. }
  9911. #if WASM_ENABLE_GC != 0
  9912. j = 0;
  9913. #endif
  9914. for (i = 0; i < (int32)arity; i++) {
  9915. #if WASM_ENABLE_GC != 0
  9916. if (wasm_is_type_multi_byte_type(types[i])) {
  9917. bh_assert(reftype_maps[j].index == i);
  9918. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9919. reftype_maps[j].ref_type,
  9920. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9921. j++;
  9922. }
  9923. #endif
  9924. #if WASM_ENABLE_FAST_INTERP != 0
  9925. bool disable_emit = true;
  9926. int16 operand_offset = 0;
  9927. PUSH_OFFSET_TYPE(types[i]);
  9928. #endif
  9929. PUSH_TYPE(types[i]);
  9930. }
  9931. #if WASM_ENABLE_FAST_INTERP != 0
  9932. emit_br_info(target_block, opcode == WASM_OP_BR);
  9933. #endif
  9934. /* Restore the stack data, note that frame_ref_bottom,
  9935. frame_reftype_map_bottom, frame_offset_bottom may be
  9936. re-allocated in the above push operations */
  9937. if (opcode == WASM_OP_BR_TABLE) {
  9938. uint32 total_size;
  9939. /* The stack operand num should not be smaller than before
  9940. after pop and push operations */
  9941. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9942. loader_ctx->stack_cell_num = stack_cell_num_old;
  9943. loader_ctx->frame_ref =
  9944. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9945. total_size = (uint32)(sizeof(uint8)
  9946. * (frame_ref_old - frame_ref_after_popped));
  9947. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9948. frame_ref_buf, total_size);
  9949. #if WASM_ENABLE_GC != 0
  9950. /* The stack operand num should not be smaller than before
  9951. after pop and push operations */
  9952. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9953. loader_ctx->reftype_map_num = reftype_map_num_old;
  9954. loader_ctx->frame_reftype_map =
  9955. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9956. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9957. * (frame_reftype_map_old
  9958. - frame_reftype_map_after_popped));
  9959. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9960. total_size, frame_reftype_map_buf, total_size);
  9961. #endif
  9962. #if WASM_ENABLE_FAST_INTERP != 0
  9963. loader_ctx->frame_offset =
  9964. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9965. total_size =
  9966. (uint32)(sizeof(int16)
  9967. * (frame_offset_old - frame_offset_after_popped));
  9968. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9969. total_size, frame_offset_buf, total_size);
  9970. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9971. #endif
  9972. }
  9973. ret = true;
  9974. goto cleanup_and_return;
  9975. }
  9976. available_stack_cell =
  9977. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9978. #if WASM_ENABLE_GC != 0
  9979. available_reftype_map =
  9980. (int32)(loader_ctx->reftype_map_num
  9981. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9982. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9983. #endif
  9984. /* Check stack top values match target block type */
  9985. for (i = (int32)arity - 1; i >= 0; i--) {
  9986. type = types[i];
  9987. #if WASM_ENABLE_GC != 0
  9988. ref_type = NULL;
  9989. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9990. if (is_type_multi_byte) {
  9991. bh_assert(reftype_map);
  9992. ref_type = reftype_map->ref_type;
  9993. }
  9994. #endif
  9995. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9996. break;
  9997. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9998. #if WASM_ENABLE_GC != 0
  9999. frame_reftype_map, available_reftype_map,
  10000. #endif
  10001. type,
  10002. #if WASM_ENABLE_GC != 0
  10003. ref_type,
  10004. #endif
  10005. error_buf, error_buf_size)) {
  10006. goto fail;
  10007. }
  10008. cell_num = wasm_value_type_cell_num(types[i]);
  10009. frame_ref -= cell_num;
  10010. available_stack_cell -= cell_num;
  10011. #if WASM_ENABLE_GC != 0
  10012. if (is_type_multi_byte) {
  10013. frame_reftype_map--;
  10014. available_reftype_map--;
  10015. reftype_map--;
  10016. }
  10017. #endif
  10018. }
  10019. #if WASM_ENABLE_FAST_INTERP != 0
  10020. emit_br_info(target_block, opcode == WASM_OP_BR);
  10021. #endif
  10022. ret = true;
  10023. cleanup_and_return:
  10024. fail:
  10025. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  10026. wasm_runtime_free(frame_ref_buf);
  10027. #if WASM_ENABLE_GC != 0
  10028. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  10029. wasm_runtime_free(frame_reftype_map_buf);
  10030. #endif
  10031. #if WASM_ENABLE_FAST_INTERP != 0
  10032. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  10033. wasm_runtime_free(frame_offset_buf);
  10034. #endif
  10035. return ret;
  10036. }
  10037. static BranchBlock *
  10038. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  10039. uint8 opcode, char *error_buf, uint32 error_buf_size)
  10040. {
  10041. uint8 *p = *p_buf, *p_end = buf_end;
  10042. BranchBlock *frame_csp_tmp;
  10043. uint32 depth;
  10044. read_leb_uint32(p, p_end, depth);
  10045. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  10046. error_buf_size)) {
  10047. goto fail;
  10048. }
  10049. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  10050. *p_buf = p;
  10051. return frame_csp_tmp;
  10052. fail:
  10053. return NULL;
  10054. }
  10055. #if WASM_ENABLE_EXCE_HANDLING != 0
  10056. static BranchBlock *
  10057. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  10058. uint8 *buf_end, char *error_buf,
  10059. uint32 error_buf_size)
  10060. {
  10061. uint8 *p = *p_buf, *p_end = buf_end;
  10062. BranchBlock *frame_csp_tmp;
  10063. uint32 depth;
  10064. read_leb_uint32(p, p_end, depth);
  10065. /*
  10066. * Note: "delegate 0" means the surrounding block, not the
  10067. * try-delegate block itself.
  10068. *
  10069. * Note: the caller hasn't popped the try-delegate frame yet.
  10070. */
  10071. bh_assert(loader_ctx->csp_num > 0);
  10072. if (loader_ctx->csp_num - 1 <= depth) {
  10073. #if WASM_ENABLE_SPEC_TEST == 0
  10074. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  10075. #else
  10076. set_error_buf(error_buf, error_buf_size, "unknown label");
  10077. #endif
  10078. goto fail;
  10079. }
  10080. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  10081. #if WASM_ENABLE_FAST_INTERP != 0
  10082. emit_br_info(frame_csp_tmp, false);
  10083. #endif
  10084. *p_buf = p;
  10085. return frame_csp_tmp;
  10086. fail:
  10087. return NULL;
  10088. }
  10089. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10090. static bool
  10091. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  10092. char *error_buf, uint32 error_buf_size)
  10093. {
  10094. BlockType *block_type = &block->block_type;
  10095. uint8 *return_types = NULL;
  10096. uint32 return_count = 0;
  10097. int32 available_stack_cell, return_cell_num, i;
  10098. uint8 *frame_ref = NULL;
  10099. #if WASM_ENABLE_GC != 0
  10100. WASMRefTypeMap *frame_reftype_map;
  10101. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  10102. WASMRefType *ref_type;
  10103. uint32 param_count, return_reftype_map_count = 0;
  10104. int32 available_reftype_map =
  10105. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  10106. #endif
  10107. available_stack_cell =
  10108. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  10109. #if WASM_ENABLE_GC == 0
  10110. return_count = block_type_get_result_types(block_type, &return_types);
  10111. #else
  10112. return_count = block_type_get_result_types(block_type, &return_types,
  10113. &return_reftype_maps,
  10114. &return_reftype_map_count);
  10115. param_count =
  10116. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  10117. (void)param_count;
  10118. #endif
  10119. return_cell_num =
  10120. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  10121. /* If the stack is in polymorphic state, just clear the stack
  10122. * and then re-push the values to make the stack top values
  10123. * match block type. */
  10124. if (block->is_stack_polymorphic) {
  10125. #if WASM_ENABLE_GC != 0
  10126. int32 j = (int32)return_reftype_map_count - 1;
  10127. #endif
  10128. for (i = (int32)return_count - 1; i >= 0; i--) {
  10129. #if WASM_ENABLE_GC != 0
  10130. if (wasm_is_type_multi_byte_type(return_types[i])) {
  10131. bh_assert(return_reftype_maps[j].index == i + param_count);
  10132. bh_memcpy_s(
  10133. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  10134. return_reftype_maps[j].ref_type,
  10135. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  10136. j--;
  10137. }
  10138. #endif
  10139. #if WASM_ENABLE_FAST_INTERP != 0
  10140. POP_OFFSET_TYPE(return_types[i]);
  10141. #endif
  10142. POP_TYPE(return_types[i]);
  10143. }
  10144. /* Check stack is empty */
  10145. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  10146. set_error_buf(
  10147. error_buf, error_buf_size,
  10148. "type mismatch: stack size does not match block type");
  10149. goto fail;
  10150. }
  10151. #if WASM_ENABLE_GC != 0
  10152. j = 0;
  10153. #endif
  10154. for (i = 0; i < (int32)return_count; i++) {
  10155. #if WASM_ENABLE_GC != 0
  10156. if (wasm_is_type_multi_byte_type(return_types[i])) {
  10157. bh_assert(return_reftype_maps[j].index == i + param_count);
  10158. bh_memcpy_s(
  10159. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  10160. return_reftype_maps[j].ref_type,
  10161. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  10162. j++;
  10163. }
  10164. #endif
  10165. #if WASM_ENABLE_FAST_INTERP != 0
  10166. bool disable_emit = true;
  10167. int16 operand_offset = 0;
  10168. PUSH_OFFSET_TYPE(return_types[i]);
  10169. #endif
  10170. PUSH_TYPE(return_types[i]);
  10171. }
  10172. return true;
  10173. }
  10174. if (available_stack_cell != return_cell_num) {
  10175. #if WASM_ENABLE_EXCE_HANDLING != 0
  10176. /* testspec: this error message format is expected by try_catch.wast */
  10177. snprintf(
  10178. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  10179. block->label_type == LABEL_TYPE_TRY
  10180. || (block->label_type == LABEL_TYPE_CATCH
  10181. && return_cell_num > 0)
  10182. ? "instruction"
  10183. : "block",
  10184. return_cell_num > 0 ? type2str(return_types[0]) : "",
  10185. " but stack has ",
  10186. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  10187. : "");
  10188. goto fail;
  10189. #else
  10190. set_error_buf(error_buf, error_buf_size,
  10191. "type mismatch: stack size does not match block type");
  10192. goto fail;
  10193. #endif
  10194. }
  10195. /* Check stack values match return types */
  10196. frame_ref = loader_ctx->frame_ref;
  10197. #if WASM_ENABLE_GC != 0
  10198. frame_reftype_map = loader_ctx->frame_reftype_map;
  10199. return_reftype_map =
  10200. return_reftype_map_count
  10201. ? return_reftype_maps + return_reftype_map_count - 1
  10202. : NULL;
  10203. #endif
  10204. for (i = (int32)return_count - 1; i >= 0; i--) {
  10205. uint8 type = return_types[i];
  10206. #if WASM_ENABLE_GC != 0
  10207. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  10208. ref_type = NULL;
  10209. if (is_type_multi_byte) {
  10210. bh_assert(return_reftype_map);
  10211. ref_type = return_reftype_map->ref_type;
  10212. }
  10213. #endif
  10214. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  10215. #if WASM_ENABLE_GC != 0
  10216. frame_reftype_map, available_reftype_map,
  10217. #endif
  10218. type,
  10219. #if WASM_ENABLE_GC != 0
  10220. ref_type,
  10221. #endif
  10222. error_buf, error_buf_size))
  10223. return false;
  10224. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  10225. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  10226. #if WASM_ENABLE_GC != 0
  10227. if (is_type_multi_byte) {
  10228. frame_reftype_map--;
  10229. available_reftype_map--;
  10230. return_reftype_map--;
  10231. }
  10232. #endif
  10233. }
  10234. return true;
  10235. fail:
  10236. return false;
  10237. }
  10238. #if WASM_ENABLE_FAST_INTERP != 0
  10239. /* Copy parameters to dynamic space.
  10240. * 1) POP original parameter out;
  10241. * 2) Push and copy original values to dynamic space.
  10242. * The copy instruction format:
  10243. * Part a: param count
  10244. * Part b: all param total cell num
  10245. * Part c: each param's cell_num, src offset and dst offset
  10246. * Part d: each param's src offset
  10247. * Part e: each param's dst offset
  10248. */
  10249. static bool
  10250. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  10251. uint32 error_buf_size)
  10252. {
  10253. bool ret = false;
  10254. int16 *frame_offset = NULL;
  10255. uint8 *cells = NULL, cell;
  10256. int16 *src_offsets = NULL;
  10257. uint8 *emit_data = NULL;
  10258. uint32 i;
  10259. BranchBlock *block = loader_ctx->frame_csp - 1;
  10260. BlockType *block_type = &block->block_type;
  10261. WASMFuncType *wasm_type = block_type->u.type;
  10262. uint32 param_count = block_type->u.type->param_count;
  10263. int16 condition_offset = 0;
  10264. bool disable_emit = false;
  10265. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  10266. int16 operand_offset = 0;
  10267. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  10268. bh_assert(size > 0);
  10269. /* For if block, we also need copy the condition operand offset. */
  10270. if (is_if_block)
  10271. size += sizeof(*cells) + sizeof(*src_offsets);
  10272. /* Allocate memory for the emit data */
  10273. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  10274. return false;
  10275. cells = emit_data;
  10276. src_offsets = (int16 *)(cells + param_count);
  10277. if (is_if_block)
  10278. condition_offset = *loader_ctx->frame_offset;
  10279. /* POP original parameter out */
  10280. for (i = 0; i < param_count; i++) {
  10281. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  10282. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  10283. }
  10284. frame_offset = loader_ctx->frame_offset;
  10285. /* Get each param's cell num and src offset */
  10286. for (i = 0; i < param_count; i++) {
  10287. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  10288. cells[i] = cell;
  10289. src_offsets[i] = *frame_offset;
  10290. frame_offset += cell;
  10291. }
  10292. /* emit copy instruction */
  10293. emit_label(EXT_OP_COPY_STACK_VALUES);
  10294. /* Part a) */
  10295. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  10296. /* Part b) */
  10297. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  10298. : wasm_type->param_cell_num);
  10299. /* Part c) */
  10300. for (i = 0; i < param_count; i++)
  10301. emit_byte(loader_ctx, cells[i]);
  10302. if (is_if_block)
  10303. emit_byte(loader_ctx, 1);
  10304. /* Part d) */
  10305. for (i = 0; i < param_count; i++)
  10306. emit_operand(loader_ctx, src_offsets[i]);
  10307. if (is_if_block)
  10308. emit_operand(loader_ctx, condition_offset);
  10309. /* Since the start offset to save the block's params and
  10310. * the start offset to save the block's results may be
  10311. * different, we remember the dynamic offset for loop block
  10312. * so that we can use it to copy the stack operands to the
  10313. * loop block's params in wasm_loader_emit_br_info. */
  10314. if (block->label_type == LABEL_TYPE_LOOP)
  10315. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10316. /* Part e) */
  10317. /* Push to dynamic space. The push will emit the dst offset. */
  10318. for (i = 0; i < param_count; i++)
  10319. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  10320. if (is_if_block)
  10321. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  10322. ret = true;
  10323. fail:
  10324. /* Free the emit data */
  10325. wasm_runtime_free(emit_data);
  10326. return ret;
  10327. }
  10328. #endif
  10329. #if WASM_ENABLE_GC == 0
  10330. #define RESET_REFTYPE_MAP_STACK() (void)0
  10331. #else
  10332. #define RESET_REFTYPE_MAP_STACK() \
  10333. do { \
  10334. loader_ctx->reftype_map_num = \
  10335. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  10336. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  10337. + loader_ctx->reftype_map_num; \
  10338. } while (0)
  10339. #endif
  10340. /* reset the stack to the state of before entering the last block */
  10341. #if WASM_ENABLE_FAST_INTERP != 0
  10342. #define RESET_STACK() \
  10343. do { \
  10344. loader_ctx->stack_cell_num = \
  10345. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10346. loader_ctx->frame_ref = \
  10347. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10348. loader_ctx->frame_offset = \
  10349. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  10350. RESET_REFTYPE_MAP_STACK(); \
  10351. } while (0)
  10352. #else
  10353. #define RESET_STACK() \
  10354. do { \
  10355. loader_ctx->stack_cell_num = \
  10356. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  10357. loader_ctx->frame_ref = \
  10358. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  10359. RESET_REFTYPE_MAP_STACK(); \
  10360. } while (0)
  10361. #endif
  10362. /* set current block's stack polymorphic state */
  10363. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  10364. do { \
  10365. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  10366. _cur_block->is_stack_polymorphic = flag; \
  10367. } while (0)
  10368. #define BLOCK_HAS_PARAM(block_type) \
  10369. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  10370. #define PRESERVE_LOCAL_FOR_BLOCK() \
  10371. do { \
  10372. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  10373. error_buf_size))) { \
  10374. goto fail; \
  10375. } \
  10376. } while (0)
  10377. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10378. static bool
  10379. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  10380. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  10381. uint32 error_buf_size)
  10382. {
  10383. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  10384. return false;
  10385. }
  10386. if (table_idx < module->import_table_count) {
  10387. if (p_elem_type)
  10388. *p_elem_type =
  10389. module->import_tables[table_idx].u.table.table_type.elem_type;
  10390. #if WASM_ENABLE_GC != 0
  10391. if (p_ref_type)
  10392. *((WASMRefType **)p_ref_type) =
  10393. module->import_tables[table_idx]
  10394. .u.table.table_type.elem_ref_type;
  10395. #endif
  10396. }
  10397. else {
  10398. if (p_elem_type)
  10399. *p_elem_type =
  10400. module->tables[table_idx - module->import_table_count]
  10401. .table_type.elem_type;
  10402. #if WASM_ENABLE_GC != 0
  10403. if (p_ref_type)
  10404. *((WASMRefType **)p_ref_type) =
  10405. module->tables[table_idx - module->import_table_count]
  10406. .table_type.elem_ref_type;
  10407. #endif
  10408. }
  10409. return true;
  10410. }
  10411. static bool
  10412. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  10413. uint8 *p_elem_type, void **p_elem_ref_type,
  10414. char *error_buf, uint32 error_buf_size)
  10415. {
  10416. if (table_seg_idx >= module->table_seg_count) {
  10417. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  10418. table_seg_idx);
  10419. return false;
  10420. }
  10421. if (p_elem_type) {
  10422. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  10423. }
  10424. #if WASM_ENABLE_GC != 0
  10425. if (p_elem_ref_type)
  10426. *((WASMRefType **)p_elem_ref_type) =
  10427. module->table_segments[table_seg_idx].elem_ref_type;
  10428. #endif
  10429. return true;
  10430. }
  10431. #endif
  10432. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  10433. const uint8 *
  10434. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  10435. uint32 *len)
  10436. {
  10437. WASMCustomSection *section = module->custom_section_list;
  10438. while (section) {
  10439. if ((section->name_len == strlen(name))
  10440. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  10441. if (len) {
  10442. *len = section->content_len;
  10443. }
  10444. return section->content_addr;
  10445. }
  10446. section = section->next;
  10447. }
  10448. return NULL;
  10449. }
  10450. #endif
  10451. #if 0
  10452. #define HANDLE_OPCODE(opcode) #opcode
  10453. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  10454. #undef HANDLE_OPCODE
  10455. #endif
  10456. #if WASM_ENABLE_FAST_INTERP == 0
  10457. #define pb_read_leb_uint32 read_leb_uint32
  10458. #define pb_read_leb_int32 read_leb_int32
  10459. #define pb_read_leb_int64 read_leb_int64
  10460. #define pb_read_leb_memarg read_leb_memarg
  10461. #define pb_read_leb_mem_offset read_leb_mem_offset
  10462. #else
  10463. /* Read leb without malformed format check */
  10464. static uint64
  10465. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  10466. {
  10467. uint8 *buf = *p_buf;
  10468. uint64 result = 0, byte = 0;
  10469. uint32 shift = 0;
  10470. do {
  10471. byte = *buf++;
  10472. result |= ((byte & 0x7f) << shift);
  10473. shift += 7;
  10474. } while (byte & 0x80);
  10475. if (sign && (shift < maxbits) && (byte & 0x40)) {
  10476. /* Sign extend */
  10477. result |= (~((uint64)0)) << shift;
  10478. }
  10479. *p_buf = buf;
  10480. return result;
  10481. }
  10482. #define pb_read_leb_uint32(p, p_end, res) \
  10483. do { \
  10484. if (!loader_ctx->p_code_compiled) \
  10485. /* Enable format check in the first scan */ \
  10486. read_leb_uint32(p, p_end, res); \
  10487. else \
  10488. /* Disable format check in the second scan */ \
  10489. res = (uint32)read_leb_quick(&p, 32, false); \
  10490. } while (0)
  10491. #define pb_read_leb_int32(p, p_end, res) \
  10492. do { \
  10493. if (!loader_ctx->p_code_compiled) \
  10494. /* Enable format check in the first scan */ \
  10495. read_leb_int32(p, p_end, res); \
  10496. else \
  10497. /* Disable format check in the second scan */ \
  10498. res = (int32)read_leb_quick(&p, 32, true); \
  10499. } while (0)
  10500. #define pb_read_leb_int64(p, p_end, res) \
  10501. do { \
  10502. if (!loader_ctx->p_code_compiled) \
  10503. /* Enable format check in the first scan */ \
  10504. read_leb_int64(p, p_end, res); \
  10505. else \
  10506. /* Disable format check in the second scan */ \
  10507. res = (int64)read_leb_quick(&p, 64, true); \
  10508. } while (0)
  10509. #if WASM_ENABLE_MULTI_MEMORY != 0
  10510. #define pb_read_leb_memarg read_leb_memarg
  10511. #else
  10512. #define pb_read_leb_memarg pb_read_leb_uint32
  10513. #endif
  10514. #if WASM_ENABLE_MEMORY64 != 0
  10515. #define pb_read_leb_mem_offset read_leb_mem_offset
  10516. #else
  10517. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10518. #endif
  10519. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10520. static bool
  10521. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10522. uint32 cur_func_idx, char *error_buf,
  10523. uint32 error_buf_size)
  10524. {
  10525. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10526. uint32 param_count, local_count, global_count;
  10527. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10528. table_elem_idx_type;
  10529. BlockType func_block_type;
  10530. uint16 *local_offsets, local_offset;
  10531. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10532. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10533. mem_offset_t mem_offset;
  10534. int32 i32_const = 0;
  10535. int64 i64_const;
  10536. uint8 opcode;
  10537. bool return_value = false;
  10538. WASMLoaderContext *loader_ctx;
  10539. BranchBlock *frame_csp_tmp;
  10540. #if WASM_ENABLE_GC != 0
  10541. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10542. uint32 param_reftype_map_count, local_reftype_map_count;
  10543. int32 heap_type;
  10544. WASMRefType wasm_ref_type = { 0 };
  10545. bool need_ref_type_map;
  10546. #endif
  10547. #if WASM_ENABLE_FAST_INTERP != 0
  10548. int16 operand_offset = 0;
  10549. uint8 last_op = 0;
  10550. bool disable_emit, preserve_local = false, if_condition_available = true;
  10551. float32 f32_const;
  10552. float64 f64_const;
  10553. /*
  10554. * It means that the fast interpreter detected an exception while preparing,
  10555. * typically near the block opcode, but it did not immediately trigger
  10556. * the exception. The loader should be capable of identifying it near
  10557. * the end opcode and then raising the exception.
  10558. */
  10559. bool pending_exception = false;
  10560. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10561. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10562. #endif
  10563. #if WASM_ENABLE_MEMORY64 != 0
  10564. bool is_memory64 = has_module_memory64(module);
  10565. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10566. #else
  10567. mem_offset_type = VALUE_TYPE_I32;
  10568. table_elem_idx_type = VALUE_TYPE_I32;
  10569. #endif
  10570. uint32 memidx;
  10571. global_count = module->import_global_count + module->global_count;
  10572. param_count = func->func_type->param_count;
  10573. param_types = func->func_type->types;
  10574. func_block_type.is_value_type = false;
  10575. func_block_type.u.type = func->func_type;
  10576. local_count = func->local_count;
  10577. local_types = func->local_types;
  10578. local_offsets = func->local_offsets;
  10579. #if WASM_ENABLE_GC != 0
  10580. param_reftype_maps = func->func_type->ref_type_maps;
  10581. param_reftype_map_count = func->func_type->ref_type_map_count;
  10582. local_reftype_maps = func->local_ref_type_maps;
  10583. local_reftype_map_count = func->local_ref_type_map_count;
  10584. #endif
  10585. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10586. goto fail;
  10587. }
  10588. #if WASM_ENABLE_GC != 0
  10589. loader_ctx->module = module;
  10590. loader_ctx->ref_type_set = module->ref_type_set;
  10591. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10592. #endif
  10593. #if WASM_ENABLE_FAST_INTERP != 0
  10594. /* For the first traverse, the initial value of preserved_local_offset has
  10595. * not been determined, we use the INT16_MAX to represent that a slot has
  10596. * been copied to preserve space. For second traverse, this field will be
  10597. * set to the appropriate value in wasm_loader_ctx_reinit.
  10598. * This is for Issue #1230,
  10599. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10600. * drop opcodes need to know which slots are preserved, so those slots will
  10601. * not be treated as dynamically allocated slots */
  10602. loader_ctx->preserved_local_offset = INT16_MAX;
  10603. re_scan:
  10604. if (loader_ctx->code_compiled_size > 0) {
  10605. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10606. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10607. goto fail;
  10608. }
  10609. p = func->code;
  10610. func->code_compiled = loader_ctx->p_code_compiled;
  10611. func->code_compiled_size = loader_ctx->code_compiled_size;
  10612. if (loader_ctx->i64_const_num > 0) {
  10613. int64 *i64_consts_old = loader_ctx->i64_consts;
  10614. /* Sort the i64 consts */
  10615. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10616. cmp_i64_const);
  10617. /* Remove the duplicated i64 consts */
  10618. uint32 k = 1;
  10619. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10620. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10621. i64_consts_old[k++] = i64_consts_old[i];
  10622. }
  10623. }
  10624. if (k < loader_ctx->i64_const_num) {
  10625. int64 *i64_consts_new;
  10626. /* Try to reallocate memory with a smaller size */
  10627. if ((i64_consts_new =
  10628. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10629. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10630. i64_consts_old, (uint32)sizeof(int64) * k);
  10631. /* Free the old memory */
  10632. wasm_runtime_free(i64_consts_old);
  10633. loader_ctx->i64_consts = i64_consts_new;
  10634. loader_ctx->i64_const_max_num = k;
  10635. }
  10636. loader_ctx->i64_const_num = k;
  10637. }
  10638. }
  10639. if (loader_ctx->v128_const_num > 0) {
  10640. V128 *v128_consts_old = loader_ctx->v128_consts;
  10641. /* Sort the v128 consts */
  10642. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10643. cmp_v128_const);
  10644. /* Remove the duplicated v128 consts */
  10645. uint32 k = 1;
  10646. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10647. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10648. sizeof(V128))
  10649. == 0)) {
  10650. v128_consts_old[k++] = v128_consts_old[i];
  10651. }
  10652. }
  10653. if (k < loader_ctx->v128_const_num) {
  10654. V128 *v128_consts_new;
  10655. /* Try to reallocate memory with a smaller size */
  10656. if ((v128_consts_new =
  10657. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10658. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10659. v128_consts_old, (uint32)sizeof(V128) * k);
  10660. /* Free the old memory */
  10661. wasm_runtime_free(v128_consts_old);
  10662. loader_ctx->v128_consts = v128_consts_new;
  10663. loader_ctx->v128_const_max_num = k;
  10664. }
  10665. loader_ctx->v128_const_num = k;
  10666. }
  10667. }
  10668. if (loader_ctx->i32_const_num > 0) {
  10669. int32 *i32_consts_old = loader_ctx->i32_consts;
  10670. /* Sort the i32 consts */
  10671. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10672. cmp_i32_const);
  10673. /* Remove the duplicated i32 consts */
  10674. uint32 k = 1;
  10675. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10676. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10677. i32_consts_old[k++] = i32_consts_old[i];
  10678. }
  10679. }
  10680. if (k < loader_ctx->i32_const_num) {
  10681. int32 *i32_consts_new;
  10682. /* Try to reallocate memory with a smaller size */
  10683. if ((i32_consts_new =
  10684. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10685. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10686. i32_consts_old, (uint32)sizeof(int32) * k);
  10687. /* Free the old memory */
  10688. wasm_runtime_free(i32_consts_old);
  10689. loader_ctx->i32_consts = i32_consts_new;
  10690. loader_ctx->i32_const_max_num = k;
  10691. }
  10692. loader_ctx->i32_const_num = k;
  10693. }
  10694. }
  10695. }
  10696. #endif
  10697. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10698. while (p < p_end) {
  10699. opcode = *p++;
  10700. #if WASM_ENABLE_FAST_INTERP != 0
  10701. p_org = p;
  10702. disable_emit = false;
  10703. emit_label(opcode);
  10704. #endif
  10705. switch (opcode) {
  10706. case WASM_OP_UNREACHABLE:
  10707. RESET_STACK();
  10708. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10709. break;
  10710. case WASM_OP_NOP:
  10711. #if WASM_ENABLE_FAST_INTERP != 0
  10712. skip_label();
  10713. #endif
  10714. break;
  10715. case WASM_OP_IF:
  10716. {
  10717. #if WASM_ENABLE_FAST_INTERP != 0
  10718. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10719. int32 available_stack_cell =
  10720. (int32)(loader_ctx->stack_cell_num
  10721. - parent_block->stack_cell_num);
  10722. if (available_stack_cell <= 0
  10723. && parent_block->is_stack_polymorphic)
  10724. if_condition_available = false;
  10725. else
  10726. if_condition_available = true;
  10727. PRESERVE_LOCAL_FOR_BLOCK();
  10728. #endif
  10729. #if WASM_ENABLE_GC == 0
  10730. POP_I32();
  10731. #endif
  10732. goto handle_op_block_and_loop;
  10733. }
  10734. case WASM_OP_BLOCK:
  10735. case WASM_OP_LOOP:
  10736. #if WASM_ENABLE_EXCE_HANDLING != 0
  10737. case WASM_OP_TRY:
  10738. if (opcode == WASM_OP_TRY) {
  10739. /*
  10740. * keep track of exception handlers to account for
  10741. * memory allocation
  10742. */
  10743. func->exception_handler_count++;
  10744. /*
  10745. * try is a block
  10746. * do nothing special, but execution continues to
  10747. * to handle_op_block_and_loop,
  10748. * and that be pushes the csp
  10749. */
  10750. }
  10751. #endif
  10752. #if WASM_ENABLE_FAST_INTERP != 0
  10753. PRESERVE_LOCAL_FOR_BLOCK();
  10754. #endif
  10755. handle_op_block_and_loop:
  10756. {
  10757. uint8 value_type;
  10758. BlockType block_type;
  10759. #if WASM_ENABLE_FAST_INTERP != 0
  10760. uint32 available_params = 0;
  10761. #endif
  10762. CHECK_BUF(p, p_end, 1);
  10763. value_type = read_uint8(p);
  10764. if (is_byte_a_type(value_type)) {
  10765. /* If the first byte is one of these special values:
  10766. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10767. * the single return value. */
  10768. block_type.is_value_type = true;
  10769. block_type.u.value_type.type = value_type;
  10770. #if WASM_ENABLE_WAMR_COMPILER != 0
  10771. if (value_type == VALUE_TYPE_V128)
  10772. module->is_simd_used = true;
  10773. else if (value_type == VALUE_TYPE_FUNCREF
  10774. || value_type == VALUE_TYPE_EXTERNREF)
  10775. module->is_ref_types_used = true;
  10776. #endif
  10777. #if WASM_ENABLE_GC != 0
  10778. if (value_type != VALUE_TYPE_VOID) {
  10779. p_org = p;
  10780. p--;
  10781. if (!resolve_value_type((const uint8 **)&p, p_end,
  10782. module, module->type_count,
  10783. &need_ref_type_map,
  10784. &wasm_ref_type, false,
  10785. error_buf, error_buf_size)) {
  10786. goto fail;
  10787. }
  10788. if (need_ref_type_map) {
  10789. block_type.u.value_type.ref_type_map.index = 0;
  10790. if (!(block_type.u.value_type.ref_type_map
  10791. .ref_type = reftype_set_insert(
  10792. module->ref_type_set, &wasm_ref_type,
  10793. error_buf, error_buf_size))) {
  10794. goto fail;
  10795. }
  10796. }
  10797. /* Set again as the type might be changed, e.g.
  10798. (ref null any) to anyref */
  10799. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10800. #if WASM_ENABLE_FAST_INTERP == 0
  10801. while (p_org < p) {
  10802. #if WASM_ENABLE_DEBUG_INTERP != 0
  10803. if (!record_fast_op(module, p_org, *p_org,
  10804. error_buf, error_buf_size)) {
  10805. goto fail;
  10806. }
  10807. #endif
  10808. /* Ignore extra bytes for interpreter */
  10809. *p_org++ = WASM_OP_NOP;
  10810. }
  10811. #endif
  10812. }
  10813. #endif /* end of WASM_ENABLE_GC != 0 */
  10814. }
  10815. else {
  10816. int32 type_index;
  10817. /* Resolve the leb128 encoded type index as block type */
  10818. p--;
  10819. p_org = p - 1;
  10820. pb_read_leb_int32(p, p_end, type_index);
  10821. if (!check_function_type(module, type_index, error_buf,
  10822. error_buf_size)) {
  10823. goto fail;
  10824. }
  10825. block_type.is_value_type = false;
  10826. block_type.u.type =
  10827. (WASMFuncType *)module->types[type_index];
  10828. #if WASM_ENABLE_FAST_INTERP == 0
  10829. /* If block use type index as block type, change the opcode
  10830. * to new extended opcode so that interpreter can resolve
  10831. * the block quickly.
  10832. */
  10833. #if WASM_ENABLE_DEBUG_INTERP != 0
  10834. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10835. error_buf_size)) {
  10836. goto fail;
  10837. }
  10838. #endif
  10839. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10840. #endif
  10841. }
  10842. #if WASM_ENABLE_GC != 0
  10843. if (opcode == WASM_OP_IF) {
  10844. POP_I32();
  10845. }
  10846. #endif
  10847. /* Pop block parameters from stack */
  10848. if (BLOCK_HAS_PARAM(block_type)) {
  10849. WASMFuncType *wasm_type = block_type.u.type;
  10850. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10851. #if WASM_ENABLE_GC != 0
  10852. WASMRefType *ref_type;
  10853. uint32 j = 0;
  10854. #endif
  10855. #if WASM_ENABLE_FAST_INTERP != 0
  10856. uint32 cell_num;
  10857. available_params = block_type.u.type->param_count;
  10858. #endif
  10859. #if WASM_ENABLE_GC != 0
  10860. /* find the index of the last param
  10861. * in wasm_type->ref_type_maps as j */
  10862. for (i = 0; i < block_type.u.type->param_count; i++) {
  10863. if (wasm_is_type_multi_byte_type(wasm_type->types[i])) {
  10864. j += 1;
  10865. }
  10866. }
  10867. if (j > 0) {
  10868. j -= 1;
  10869. }
  10870. #endif
  10871. for (i = 0; i < block_type.u.type->param_count; i++) {
  10872. int32 available_stack_cell =
  10873. (int32)(loader_ctx->stack_cell_num
  10874. - cur_block->stack_cell_num);
  10875. if (available_stack_cell <= 0
  10876. && cur_block->is_stack_polymorphic) {
  10877. #if WASM_ENABLE_FAST_INTERP != 0
  10878. available_params = i;
  10879. #endif
  10880. break;
  10881. }
  10882. #if WASM_ENABLE_GC != 0
  10883. if (wasm_is_type_multi_byte_type(
  10884. wasm_type
  10885. ->types[wasm_type->param_count - i - 1])) {
  10886. bh_assert(wasm_type->ref_type_maps[j].index
  10887. == wasm_type->param_count - i - 1);
  10888. ref_type = wasm_type->ref_type_maps[j].ref_type;
  10889. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10890. ref_type,
  10891. wasm_reftype_struct_size(ref_type));
  10892. j--;
  10893. }
  10894. #endif
  10895. uint8 *frame_ref_before_pop = loader_ctx->frame_ref;
  10896. POP_TYPE(
  10897. wasm_type->types[wasm_type->param_count - i - 1]);
  10898. #if WASM_ENABLE_FAST_INTERP != 0
  10899. /* decrease the frame_offset pointer accordingly to keep
  10900. * consistent with frame_ref stack. Use the actual
  10901. * popped cell count instead of
  10902. * wasm_value_type_cell_num() because when the stack top
  10903. * is VALUE_TYPE_ANY, wasm_loader_pop_frame_ref always
  10904. * pops exactly 1 cell regardless of the expected type
  10905. */
  10906. cell_num = (uint32)(frame_ref_before_pop
  10907. - loader_ctx->frame_ref);
  10908. loader_ctx->frame_offset -= cell_num;
  10909. if (loader_ctx->frame_offset
  10910. < loader_ctx->frame_offset_bottom) {
  10911. LOG_DEBUG(
  10912. "frame_offset underflow, roll back and "
  10913. "let following stack checker report it\n");
  10914. loader_ctx->frame_offset += cell_num;
  10915. pending_exception = true;
  10916. break;
  10917. }
  10918. #endif
  10919. }
  10920. }
  10921. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10922. block_type, p);
  10923. /* Pass parameters to block */
  10924. if (BLOCK_HAS_PARAM(block_type)) {
  10925. WASMFuncType *func_type = block_type.u.type;
  10926. #if WASM_ENABLE_GC != 0
  10927. WASMRefType *ref_type;
  10928. uint32 j = 0;
  10929. #endif
  10930. for (i = 0; i < func_type->param_count; i++) {
  10931. #if WASM_ENABLE_FAST_INTERP != 0
  10932. uint32 cell_num =
  10933. wasm_value_type_cell_num(func_type->types[i]);
  10934. if (i >= available_params) {
  10935. /* make sure enough space */
  10936. if (loader_ctx->p_code_compiled == NULL) {
  10937. loader_ctx->frame_offset += cell_num;
  10938. if (!check_offset_push(loader_ctx, error_buf,
  10939. error_buf_size))
  10940. goto fail;
  10941. /* for following dummy value assignment */
  10942. loader_ctx->frame_offset -= cell_num;
  10943. }
  10944. /* If there isn't enough data on stack, push a dummy
  10945. * offset to keep the stack consistent with
  10946. * frame_ref.
  10947. * Since the stack is already in polymorphic state,
  10948. * the opcode will not be executed, so the dummy
  10949. * offset won't cause any error */
  10950. for (uint32 n = 0; n < cell_num; n++) {
  10951. *loader_ctx->frame_offset++ = 0;
  10952. }
  10953. }
  10954. else {
  10955. loader_ctx->frame_offset += cell_num;
  10956. }
  10957. #endif
  10958. #if WASM_ENABLE_GC != 0
  10959. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10960. bh_assert(func_type->ref_type_maps[j].index == i);
  10961. ref_type = func_type->ref_type_maps[j].ref_type;
  10962. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10963. ref_type,
  10964. wasm_reftype_struct_size(ref_type));
  10965. j++;
  10966. }
  10967. #endif
  10968. PUSH_TYPE(func_type->types[i]);
  10969. }
  10970. }
  10971. #if WASM_ENABLE_FAST_INTERP != 0
  10972. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10973. skip_label();
  10974. if (BLOCK_HAS_PARAM(block_type)) {
  10975. /* Make sure params are in dynamic space */
  10976. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10977. error_buf_size))
  10978. goto fail;
  10979. }
  10980. if (opcode == WASM_OP_LOOP) {
  10981. (loader_ctx->frame_csp - 1)->code_compiled =
  10982. loader_ctx->p_code_compiled;
  10983. }
  10984. }
  10985. #if WASM_ENABLE_EXCE_HANDLING != 0
  10986. else if (opcode == WASM_OP_TRY) {
  10987. skip_label();
  10988. }
  10989. #endif
  10990. else if (opcode == WASM_OP_IF) {
  10991. BranchBlock *block = loader_ctx->frame_csp - 1;
  10992. /* If block has parameters, we should make sure they are in
  10993. * dynamic space. Otherwise, when else branch is missing,
  10994. * the later opcode may consume incorrect operand offset.
  10995. * Spec case:
  10996. * (func (export "params-id") (param i32) (result i32)
  10997. * (i32.const 1)
  10998. * (i32.const 2)
  10999. * (if (param i32 i32) (result i32 i32) (local.get 0)
  11000. * (then)) (i32.add)
  11001. * )
  11002. *
  11003. * So we should emit a copy instruction before the if.
  11004. *
  11005. * And we also need to save the parameter offsets and
  11006. * recover them before entering else branch.
  11007. *
  11008. */
  11009. if (BLOCK_HAS_PARAM(block_type)) {
  11010. uint64 size;
  11011. /* In polymorphic state, there may be no if condition on
  11012. * the stack, so the offset may not emitted */
  11013. if (if_condition_available) {
  11014. /* skip the if condition operand offset */
  11015. wasm_loader_emit_backspace(loader_ctx,
  11016. sizeof(int16));
  11017. }
  11018. /* skip the if label */
  11019. skip_label();
  11020. /* Emit a copy instruction */
  11021. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  11022. error_buf_size))
  11023. goto fail;
  11024. /* Emit the if instruction */
  11025. emit_label(opcode);
  11026. /* Emit the new condition operand offset */
  11027. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  11028. /* Save top param_count values of frame_offset stack, so
  11029. * that we can recover it before executing else branch
  11030. */
  11031. size = sizeof(int16)
  11032. * (uint64)block_type.u.type->param_cell_num;
  11033. if (!(block->param_frame_offsets = loader_malloc(
  11034. size, error_buf, error_buf_size)))
  11035. goto fail;
  11036. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  11037. loader_ctx->frame_offset
  11038. - size / sizeof(int16),
  11039. (uint32)size);
  11040. }
  11041. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  11042. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  11043. emit_empty_label_addr_and_frame_ip(PATCH_END);
  11044. }
  11045. #endif
  11046. break;
  11047. }
  11048. #if WASM_ENABLE_EXCE_HANDLING != 0
  11049. case WASM_OP_THROW:
  11050. {
  11051. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11052. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11053. uint8 label_type = cur_block->label_type;
  11054. uint32 tag_index = 0;
  11055. pb_read_leb_int32(p, p_end, tag_index);
  11056. /* check validity of tag_index against module->tag_count */
  11057. /* check tag index is within the tag index space */
  11058. if (tag_index >= module->import_tag_count + module->tag_count) {
  11059. snprintf(error_buf, error_buf_size, "unknown tag %d",
  11060. tag_index);
  11061. goto fail;
  11062. }
  11063. /* the tag_type is stored in either the WASMTag (section tags)
  11064. * or WASMTagImport (import tag) */
  11065. WASMFuncType *tag_type = NULL;
  11066. if (tag_index < module->import_tag_count) {
  11067. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  11068. }
  11069. else {
  11070. tag_type =
  11071. module->tags[tag_index - module->import_tag_count]
  11072. ->tag_type;
  11073. }
  11074. if (tag_type->result_count != 0) {
  11075. set_error_buf(error_buf, error_buf_size,
  11076. "tag type signature does not return void");
  11077. goto fail;
  11078. }
  11079. int32 available_stack_cell =
  11080. (int32)(loader_ctx->stack_cell_num
  11081. - cur_block->stack_cell_num);
  11082. int32 tti;
  11083. /* Check stack values match return types by comparing tag param
  11084. * types with stack cells */
  11085. uint8 *frame_ref = loader_ctx->frame_ref;
  11086. #if WASM_ENABLE_GC != 0
  11087. WASMRefTypeMap *frame_reftype_map =
  11088. loader_ctx->frame_reftype_map;
  11089. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  11090. /* Temporarily set these values since they may be used in
  11091. GET_LOCAL_REFTYPE(), remember they must be restored later */
  11092. param_reftype_maps = tag_type->ref_type_maps;
  11093. /* For tag_type function, it shouldn't have result_count = 0 */
  11094. param_reftype_map_count = tag_type->ref_type_map_count;
  11095. param_count = tag_type->param_count;
  11096. #endif
  11097. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  11098. #if WASM_ENABLE_GC != 0
  11099. local_type = tag_type->types[tti];
  11100. local_idx = tti;
  11101. /* Get the wasm_ref_type if the local_type is multibyte
  11102. type */
  11103. GET_LOCAL_REFTYPE();
  11104. #endif
  11105. if (!check_stack_top_values(
  11106. loader_ctx, frame_ref, available_stack_cell,
  11107. #if WASM_ENABLE_GC != 0
  11108. frame_reftype_map, frame_reftype_map_num,
  11109. #endif
  11110. tag_type->types[tti],
  11111. #if WASM_ENABLE_GC != 0
  11112. &wasm_ref_type,
  11113. #endif
  11114. error_buf, error_buf_size)) {
  11115. snprintf(error_buf, error_buf_size,
  11116. "type mismatch: instruction requires [%s] but "
  11117. "stack has [%s]",
  11118. tag_type->param_count > 0
  11119. ? type2str(tag_type->types[tti])
  11120. : "",
  11121. available_stack_cell > 0
  11122. ? type2str(*(loader_ctx->frame_ref - 1))
  11123. : "");
  11124. goto fail;
  11125. }
  11126. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  11127. available_stack_cell -=
  11128. wasm_value_type_cell_num(tag_type->types[tti]);
  11129. }
  11130. #if WASM_ENABLE_GC != 0
  11131. /* Restore the values */
  11132. param_reftype_maps = func->func_type->ref_type_maps;
  11133. param_reftype_map_count = func->func_type->ref_type_map_count;
  11134. param_count = func->func_type->param_count;
  11135. #endif
  11136. /* throw is stack polymorphic */
  11137. (void)label_type;
  11138. RESET_STACK();
  11139. break;
  11140. }
  11141. case WASM_OP_RETHROW:
  11142. {
  11143. /* must be done before checking branch block */
  11144. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11145. /* check the target catching block: LABEL_TYPE_CATCH */
  11146. if (!(frame_csp_tmp =
  11147. check_branch_block(loader_ctx, &p, p_end, opcode,
  11148. error_buf, error_buf_size)))
  11149. goto fail;
  11150. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  11151. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  11152. /* trap according to spectest (rethrow.wast) */
  11153. set_error_buf(error_buf, error_buf_size,
  11154. "invalid rethrow label");
  11155. goto fail;
  11156. }
  11157. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11158. uint8 label_type = cur_block->label_type;
  11159. (void)label_type;
  11160. /* rethrow is stack polymorphic */
  11161. RESET_STACK();
  11162. break;
  11163. }
  11164. case WASM_OP_DELEGATE:
  11165. {
  11166. /* check target block is valid */
  11167. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  11168. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  11169. goto fail;
  11170. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11171. uint8 label_type = cur_block->label_type;
  11172. (void)label_type;
  11173. /* DELEGATE ends the block */
  11174. POP_CSP();
  11175. break;
  11176. }
  11177. case WASM_OP_CATCH:
  11178. {
  11179. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11180. uint8 label_type = cur_block->label_type;
  11181. uint32 tag_index = 0;
  11182. pb_read_leb_int32(p, p_end, tag_index);
  11183. /* check validity of tag_index against module->tag_count */
  11184. /* check tag index is within the tag index space */
  11185. if (tag_index >= module->import_tag_count + module->tag_count) {
  11186. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  11187. __FUNCTION__);
  11188. set_error_buf(error_buf, error_buf_size, "unknown tag");
  11189. goto fail;
  11190. }
  11191. /* the tag_type is stored in either the WASMTag (section tags)
  11192. * or WASMTagImport (import tag) */
  11193. WASMFuncType *func_type = NULL;
  11194. if (tag_index < module->import_tag_count) {
  11195. func_type = module->import_tags[tag_index].u.tag.tag_type;
  11196. }
  11197. else {
  11198. func_type =
  11199. module->tags[tag_index - module->import_tag_count]
  11200. ->tag_type;
  11201. }
  11202. if (func_type->result_count != 0) {
  11203. set_error_buf(error_buf, error_buf_size,
  11204. "tag type signature does not return void");
  11205. goto fail;
  11206. }
  11207. /* check validity of current label (expect LABEL_TYPE_TRY or
  11208. * LABEL_TYPE_CATCH) */
  11209. if ((LABEL_TYPE_CATCH != label_type)
  11210. && (LABEL_TYPE_TRY != label_type)) {
  11211. set_error_buf(error_buf, error_buf_size,
  11212. "Unexpected block sequence encountered.");
  11213. goto fail;
  11214. }
  11215. /*
  11216. * replace frame_csp by LABEL_TYPE_CATCH
  11217. */
  11218. cur_block->label_type = LABEL_TYPE_CATCH;
  11219. /* RESET_STACK removes the values pushed in TRY or previous
  11220. * CATCH Blocks */
  11221. RESET_STACK();
  11222. #if WASM_ENABLE_GC != 0
  11223. WASMRefType *ref_type;
  11224. uint32 j = 0;
  11225. #endif
  11226. /* push types on the stack according to caught type */
  11227. for (i = 0; i < func_type->param_count; i++) {
  11228. #if WASM_ENABLE_GC != 0
  11229. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  11230. bh_assert(func_type->ref_type_maps[j].index == i);
  11231. ref_type = func_type->ref_type_maps[j].ref_type;
  11232. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11233. ref_type,
  11234. wasm_reftype_struct_size(ref_type));
  11235. j++;
  11236. }
  11237. #endif
  11238. PUSH_TYPE(func_type->types[i]);
  11239. }
  11240. break;
  11241. }
  11242. case WASM_OP_CATCH_ALL:
  11243. {
  11244. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11245. /* expecting a TRY or CATCH, anything else will be considered an
  11246. * error */
  11247. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  11248. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  11249. set_error_buf(error_buf, error_buf_size,
  11250. "Unexpected block sequence encountered.");
  11251. goto fail;
  11252. }
  11253. /* no immediates */
  11254. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  11255. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  11256. /* RESET_STACK removes the values pushed in TRY or previous
  11257. * CATCH Blocks */
  11258. RESET_STACK();
  11259. /* catch_all has no tagtype and therefore no parameters */
  11260. break;
  11261. }
  11262. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  11263. case WASM_OP_ELSE:
  11264. handle_op_else:
  11265. {
  11266. BranchBlock *block = NULL;
  11267. BlockType block_type;
  11268. if (loader_ctx->csp_num < 2
  11269. /* the matched if isn't found */
  11270. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  11271. /* duplicated else is found */
  11272. || (loader_ctx->frame_csp - 1)->else_addr) {
  11273. set_error_buf(
  11274. error_buf, error_buf_size,
  11275. "opcode else found without matched opcode if");
  11276. goto fail;
  11277. }
  11278. block = loader_ctx->frame_csp - 1;
  11279. /* check whether if branch's stack matches its result type */
  11280. if (!check_block_stack(loader_ctx, block, error_buf,
  11281. error_buf_size))
  11282. goto fail;
  11283. block->else_addr = p - 1;
  11284. block_type = block->block_type;
  11285. #if WASM_ENABLE_GC != 0
  11286. if (!wasm_loader_init_local_use_masks(
  11287. loader_ctx, local_count, error_buf, error_buf_size)) {
  11288. goto fail;
  11289. }
  11290. #endif
  11291. #if WASM_ENABLE_FAST_INTERP != 0
  11292. /* if the result of if branch is in local or const area, add a
  11293. * copy op */
  11294. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11295. error_buf, error_buf_size)) {
  11296. goto fail;
  11297. }
  11298. emit_empty_label_addr_and_frame_ip(PATCH_END);
  11299. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  11300. #endif
  11301. RESET_STACK();
  11302. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  11303. /* Pass parameters to if-false branch */
  11304. if (BLOCK_HAS_PARAM(block_type)) {
  11305. for (i = 0; i < block_type.u.type->param_count; i++)
  11306. PUSH_TYPE(block_type.u.type->types[i]);
  11307. }
  11308. #if WASM_ENABLE_FAST_INTERP != 0
  11309. /* Recover top param_count values of frame_offset stack */
  11310. if (BLOCK_HAS_PARAM((block_type))) {
  11311. uint32 size;
  11312. size = sizeof(int16) * block_type.u.type->param_cell_num;
  11313. bh_memcpy_s(loader_ctx->frame_offset, size,
  11314. block->param_frame_offsets, size);
  11315. loader_ctx->frame_offset += (size / sizeof(int16));
  11316. }
  11317. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  11318. #endif
  11319. break;
  11320. }
  11321. case WASM_OP_END:
  11322. {
  11323. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11324. /* check whether block stack matches its result type */
  11325. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  11326. error_buf_size))
  11327. goto fail;
  11328. /* if there is no else branch, make a virtual else opcode for
  11329. easier integrity check and to copy the correct results to
  11330. the block return address for fast-interp mode:
  11331. change if block from `if ... end` to `if ... else end` */
  11332. if (cur_block->label_type == LABEL_TYPE_IF
  11333. && !cur_block->else_addr) {
  11334. opcode = WASM_OP_ELSE;
  11335. p--;
  11336. #if WASM_ENABLE_FAST_INTERP != 0
  11337. p_org = p;
  11338. skip_label();
  11339. disable_emit = false;
  11340. emit_label(opcode);
  11341. #endif
  11342. goto handle_op_else;
  11343. }
  11344. POP_CSP();
  11345. #if WASM_ENABLE_FAST_INTERP != 0
  11346. skip_label();
  11347. /* copy the result to the block return address */
  11348. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  11349. error_buf, error_buf_size)) {
  11350. /* it could be tmp frame_csp allocated from opcode like
  11351. * OP_BR and not counted in loader_ctx->csp_num, it won't
  11352. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  11353. * to free the loader_ctx->frame_csp if fails */
  11354. free_label_patch_list(loader_ctx->frame_csp);
  11355. goto fail;
  11356. }
  11357. apply_label_patch(loader_ctx, 0, PATCH_END);
  11358. free_label_patch_list(loader_ctx->frame_csp);
  11359. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  11360. int32 idx;
  11361. uint8 ret_type;
  11362. emit_label(WASM_OP_RETURN);
  11363. for (idx = (int32)func->func_type->result_count - 1;
  11364. idx >= 0; idx--) {
  11365. ret_type = *(func->func_type->types
  11366. + func->func_type->param_count + idx);
  11367. POP_OFFSET_TYPE(ret_type);
  11368. }
  11369. }
  11370. #endif
  11371. if (loader_ctx->csp_num > 0) {
  11372. loader_ctx->frame_csp->end_addr = p - 1;
  11373. }
  11374. else {
  11375. /* end of function block, function will return */
  11376. if (p < p_end) {
  11377. set_error_buf(error_buf, error_buf_size,
  11378. "section size mismatch");
  11379. goto fail;
  11380. }
  11381. }
  11382. #if WASM_ENABLE_FAST_INTERP != 0
  11383. if (pending_exception) {
  11384. set_error_buf(
  11385. error_buf, error_buf_size,
  11386. "There is a pending exception needs to be handled");
  11387. goto fail;
  11388. }
  11389. #endif
  11390. break;
  11391. }
  11392. case WASM_OP_BR:
  11393. {
  11394. if (!(frame_csp_tmp =
  11395. check_branch_block(loader_ctx, &p, p_end, opcode,
  11396. error_buf, error_buf_size)))
  11397. goto fail;
  11398. RESET_STACK();
  11399. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11400. break;
  11401. }
  11402. case WASM_OP_BR_IF:
  11403. {
  11404. POP_I32();
  11405. if (!(frame_csp_tmp =
  11406. check_branch_block(loader_ctx, &p, p_end, opcode,
  11407. error_buf, error_buf_size)))
  11408. goto fail;
  11409. break;
  11410. }
  11411. case WASM_OP_BR_TABLE:
  11412. {
  11413. uint32 depth = 0, default_arity, arity = 0;
  11414. BranchBlock *target_block;
  11415. BlockType *target_block_type;
  11416. #if WASM_ENABLE_FAST_INTERP == 0
  11417. BrTableCache *br_table_cache = NULL;
  11418. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  11419. uint32 j;
  11420. #endif
  11421. pb_read_leb_uint32(p, p_end, count);
  11422. #if WASM_ENABLE_FAST_INTERP != 0
  11423. emit_uint32(loader_ctx, count);
  11424. #endif
  11425. POP_I32();
  11426. /* Get each depth and check it */
  11427. p_org = p;
  11428. for (i = 0; i <= count; i++) {
  11429. pb_read_leb_uint32(p, p_end, depth);
  11430. bh_assert(loader_ctx->csp_num > 0);
  11431. if (loader_ctx->csp_num - 1 < depth) {
  11432. set_error_buf(error_buf, error_buf_size,
  11433. "unknown label, "
  11434. "unexpected end of section or function");
  11435. goto fail;
  11436. }
  11437. }
  11438. p = p_org;
  11439. /* Get the default block's arity */
  11440. target_block = loader_ctx->frame_csp - (depth + 1);
  11441. target_block_type = &target_block->block_type;
  11442. default_arity = block_type_get_arity(target_block_type,
  11443. target_block->label_type);
  11444. #if WASM_ENABLE_FAST_INTERP == 0
  11445. p_depth_begin = p_depth = p;
  11446. #endif
  11447. for (i = 0; i <= count; i++) {
  11448. p_org = p;
  11449. pb_read_leb_uint32(p, p_end, depth);
  11450. p = p_org;
  11451. /* Get the target block's arity and check it */
  11452. target_block = loader_ctx->frame_csp - (depth + 1);
  11453. target_block_type = &target_block->block_type;
  11454. arity = block_type_get_arity(target_block_type,
  11455. target_block->label_type);
  11456. if (arity != default_arity) {
  11457. set_error_buf(error_buf, error_buf_size,
  11458. "type mismatch: br_table targets must "
  11459. "all use same result type");
  11460. goto fail;
  11461. }
  11462. if (!(frame_csp_tmp =
  11463. check_branch_block(loader_ctx, &p, p_end, opcode,
  11464. error_buf, error_buf_size))) {
  11465. goto fail;
  11466. }
  11467. #if WASM_ENABLE_FAST_INTERP == 0
  11468. if (br_table_cache) {
  11469. br_table_cache->br_depths[i] = depth;
  11470. }
  11471. else {
  11472. if (depth > 255) {
  11473. /* The depth cannot be stored in one byte,
  11474. create br_table cache to store each depth */
  11475. #if WASM_ENABLE_DEBUG_INTERP != 0
  11476. if (!record_fast_op(module, p_opcode, *p_opcode,
  11477. error_buf, error_buf_size)) {
  11478. goto fail;
  11479. }
  11480. #endif
  11481. if (!(br_table_cache = loader_malloc(
  11482. offsetof(BrTableCache, br_depths)
  11483. + sizeof(uint32)
  11484. * (uint64)(count + 1),
  11485. error_buf, error_buf_size))) {
  11486. goto fail;
  11487. }
  11488. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  11489. br_table_cache->br_table_op_addr = p_opcode;
  11490. br_table_cache->br_count = count;
  11491. /* Copy previous depths which are one byte */
  11492. for (j = 0; j < i; j++) {
  11493. br_table_cache->br_depths[j] = p_depth_begin[j];
  11494. }
  11495. br_table_cache->br_depths[i] = depth;
  11496. bh_list_insert(module->br_table_cache_list,
  11497. br_table_cache);
  11498. }
  11499. else {
  11500. /* The depth can be stored in one byte, use the
  11501. byte of the leb to store it */
  11502. *p_depth++ = (uint8)depth;
  11503. }
  11504. }
  11505. #endif
  11506. }
  11507. #if WASM_ENABLE_FAST_INTERP == 0
  11508. /* Set the tailing bytes to nop */
  11509. if (br_table_cache)
  11510. p_depth = p_depth_begin;
  11511. while (p_depth < p)
  11512. *p_depth++ = WASM_OP_NOP;
  11513. #endif
  11514. RESET_STACK();
  11515. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11516. break;
  11517. }
  11518. case WASM_OP_RETURN:
  11519. {
  11520. WASMFuncType *func_type = func->func_type;
  11521. int32 idx;
  11522. uint8 ret_type;
  11523. #if WASM_ENABLE_GC != 0
  11524. uint32 j = func_type->ref_type_map_count - 1;
  11525. #endif
  11526. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11527. idx--) {
  11528. ret_type =
  11529. *(func_type->types + func_type->param_count + idx);
  11530. #if WASM_ENABLE_GC != 0
  11531. if (wasm_is_type_multi_byte_type(ret_type)) {
  11532. WASMRefType *ref_type =
  11533. func_type->ref_type_maps[j].ref_type;
  11534. bh_assert(func_type->ref_type_maps[j].index
  11535. == func_type->param_count + idx);
  11536. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11537. ref_type,
  11538. wasm_reftype_struct_size(ref_type));
  11539. j--;
  11540. }
  11541. #endif
  11542. #if WASM_ENABLE_FAST_INTERP != 0
  11543. /* emit the offset after return opcode */
  11544. POP_OFFSET_TYPE(ret_type);
  11545. #endif
  11546. POP_TYPE(ret_type);
  11547. }
  11548. RESET_STACK();
  11549. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11550. break;
  11551. }
  11552. case WASM_OP_CALL:
  11553. #if WASM_ENABLE_TAIL_CALL != 0
  11554. case WASM_OP_RETURN_CALL:
  11555. #endif
  11556. #if WASM_ENABLE_GC != 0
  11557. case WASM_OP_CALL_REF:
  11558. case WASM_OP_RETURN_CALL_REF:
  11559. #endif
  11560. {
  11561. WASMFuncType *func_type;
  11562. uint8 type;
  11563. int32 idx;
  11564. #if WASM_ENABLE_GC != 0
  11565. WASMRefType *ref_type;
  11566. uint32 type_idx1;
  11567. int32 j;
  11568. #endif
  11569. #if WASM_ENABLE_GC != 0
  11570. if (opcode == WASM_OP_CALL_REF
  11571. || opcode == WASM_OP_RETURN_CALL_REF) {
  11572. pb_read_leb_uint32(p, p_end, type_idx1);
  11573. if (!check_type_index(module, module->type_count, type_idx1,
  11574. error_buf, error_buf_size)) {
  11575. goto fail;
  11576. }
  11577. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11578. set_error_buf(error_buf, error_buf_size,
  11579. "unknown function type");
  11580. goto fail;
  11581. }
  11582. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11583. &type_idx, error_buf,
  11584. error_buf_size)) {
  11585. goto fail;
  11586. }
  11587. if (type == VALUE_TYPE_ANY) {
  11588. type_idx = type_idx1;
  11589. }
  11590. if (!check_type_index(module, module->type_count, type_idx,
  11591. error_buf, error_buf_size)) {
  11592. goto fail;
  11593. }
  11594. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11595. set_error_buf(error_buf, error_buf_size,
  11596. "unknown function type");
  11597. goto fail;
  11598. }
  11599. if (!wasm_func_type_is_super_of(
  11600. (WASMFuncType *)module->types[type_idx1],
  11601. (WASMFuncType *)module->types[type_idx])) {
  11602. set_error_buf(error_buf, error_buf_size,
  11603. "function type mismatch");
  11604. goto fail;
  11605. }
  11606. func_type = (WASMFuncType *)module->types[type_idx];
  11607. }
  11608. else
  11609. #endif
  11610. {
  11611. pb_read_leb_uint32(p, p_end, func_idx);
  11612. #if WASM_ENABLE_FAST_INTERP != 0
  11613. /* we need to emit func_idx before arguments */
  11614. emit_uint32(loader_ctx, func_idx);
  11615. #endif
  11616. if (!check_function_index(module, func_idx, error_buf,
  11617. error_buf_size)) {
  11618. goto fail;
  11619. }
  11620. if (func_idx < module->import_function_count)
  11621. func_type = module->import_functions[func_idx]
  11622. .u.function.func_type;
  11623. else
  11624. func_type =
  11625. module
  11626. ->functions[func_idx
  11627. - module->import_function_count]
  11628. ->func_type;
  11629. }
  11630. if (func_type->param_count > 0) {
  11631. #if WASM_ENABLE_GC != 0
  11632. j = (int32)(func_type->result_ref_type_maps
  11633. - func_type->ref_type_maps - 1);
  11634. #endif
  11635. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11636. idx--) {
  11637. #if WASM_ENABLE_GC != 0
  11638. if (wasm_is_type_multi_byte_type(
  11639. func_type->types[idx])) {
  11640. ref_type = func_type->ref_type_maps[j].ref_type;
  11641. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11642. ref_type,
  11643. wasm_reftype_struct_size(ref_type));
  11644. j--;
  11645. }
  11646. #endif
  11647. #if WASM_ENABLE_FAST_INTERP != 0
  11648. POP_OFFSET_TYPE(func_type->types[idx]);
  11649. #endif
  11650. POP_TYPE(func_type->types[idx]);
  11651. }
  11652. }
  11653. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11654. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11655. #endif
  11656. #if WASM_ENABLE_GC != 0
  11657. j = (int32)(func_type->result_ref_type_maps
  11658. - func_type->ref_type_maps);
  11659. #endif
  11660. for (i = 0; i < func_type->result_count; i++) {
  11661. #if WASM_ENABLE_GC != 0
  11662. if (wasm_is_type_multi_byte_type(
  11663. func_type->types[func_type->param_count + i])) {
  11664. ref_type = func_type->ref_type_maps[j].ref_type;
  11665. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11666. ref_type,
  11667. wasm_reftype_struct_size(ref_type));
  11668. j++;
  11669. }
  11670. #endif
  11671. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11672. #if WASM_ENABLE_FAST_INTERP != 0
  11673. /* Here we emit each return value's dynamic_offset. But
  11674. * in fact these offsets are continuous, so interpreter
  11675. * only need to get the first return value's offset.
  11676. */
  11677. PUSH_OFFSET_TYPE(
  11678. func_type->types[func_type->param_count + i]);
  11679. #endif
  11680. }
  11681. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11682. }
  11683. else {
  11684. #if WASM_ENABLE_GC == 0
  11685. if (func_type->result_count
  11686. != func->func_type->result_count) {
  11687. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11688. "type mismatch: expect ",
  11689. func->func_type->result_count,
  11690. " return values but got other");
  11691. goto fail;
  11692. }
  11693. for (i = 0; i < func_type->result_count; i++) {
  11694. type = func->func_type
  11695. ->types[func->func_type->param_count + i];
  11696. if (func_type->types[func_type->param_count + i]
  11697. != type) {
  11698. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11699. "type mismatch: expect ",
  11700. type2str(type), " but got other");
  11701. goto fail;
  11702. }
  11703. }
  11704. #else
  11705. if (!wasm_func_type_result_is_subtype_of(
  11706. func_type, func->func_type, module->types,
  11707. module->type_count)) {
  11708. set_error_buf(
  11709. error_buf, error_buf_size,
  11710. "type mismatch: invalid func result types");
  11711. goto fail;
  11712. }
  11713. #endif
  11714. RESET_STACK();
  11715. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11716. }
  11717. #endif
  11718. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11719. || WASM_ENABLE_WAMR_COMPILER != 0
  11720. func->has_op_func_call = true;
  11721. #endif
  11722. (void)type;
  11723. break;
  11724. }
  11725. /*
  11726. * if disable reference type: call_indirect typeidx, 0x00
  11727. * if enable reference type: call_indirect typeidx, tableidx
  11728. */
  11729. case WASM_OP_CALL_INDIRECT:
  11730. #if WASM_ENABLE_TAIL_CALL != 0
  11731. case WASM_OP_RETURN_CALL_INDIRECT:
  11732. #endif
  11733. {
  11734. int32 idx;
  11735. WASMFuncType *func_type;
  11736. uint32 tbl_elem_type;
  11737. #if WASM_ENABLE_GC != 0
  11738. WASMRefType *elem_ref_type = NULL;
  11739. #endif
  11740. pb_read_leb_uint32(p, p_end, type_idx);
  11741. #if WASM_ENABLE_CALL_INDIRECT_OVERLONG != 0 || WASM_ENABLE_GC != 0
  11742. #if WASM_ENABLE_WAMR_COMPILER != 0
  11743. if (p + 1 < p_end && *p != 0x00) {
  11744. /*
  11745. * Any non-0x00 byte requires the ref types proposal.
  11746. * This is different from checking the table_idx value
  11747. * since `0x80 0x00` etc. are all valid encodings of zero.
  11748. */
  11749. module->is_ref_types_used = true;
  11750. }
  11751. #endif
  11752. pb_read_leb_uint32(p, p_end, table_idx);
  11753. #else
  11754. CHECK_BUF(p, p_end, 1);
  11755. table_idx = read_uint8(p);
  11756. #endif
  11757. if (!check_table_index(module, table_idx, error_buf,
  11758. error_buf_size)) {
  11759. goto fail;
  11760. }
  11761. tbl_elem_type =
  11762. table_idx < module->import_table_count
  11763. ? module->import_tables[table_idx]
  11764. .u.table.table_type.elem_type
  11765. : module->tables[table_idx - module->import_table_count]
  11766. .table_type.elem_type;
  11767. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11768. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11769. set_error_buf_v(error_buf, error_buf_size,
  11770. "type mismatch: instruction requires table "
  11771. "of functions but table %u has externref",
  11772. table_idx);
  11773. goto fail;
  11774. }
  11775. #elif WASM_ENABLE_GC != 0
  11776. /* Table element must match type ref null func */
  11777. elem_ref_type =
  11778. table_idx < module->import_table_count
  11779. ? module->import_tables[table_idx]
  11780. .u.table.table_type.elem_ref_type
  11781. : module->tables[table_idx - module->import_table_count]
  11782. .table_type.elem_ref_type;
  11783. if (!wasm_reftype_is_subtype_of(
  11784. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11785. module->types, module->type_count)) {
  11786. set_error_buf_v(error_buf, error_buf_size,
  11787. "type mismatch: instruction requires "
  11788. "reference type t match type ref null func"
  11789. "in table %u",
  11790. table_idx);
  11791. goto fail;
  11792. }
  11793. #else
  11794. (void)tbl_elem_type;
  11795. #endif
  11796. #if WASM_ENABLE_FAST_INTERP != 0
  11797. /* we need to emit before arguments */
  11798. #if WASM_ENABLE_TAIL_CALL != 0
  11799. emit_byte(loader_ctx, opcode);
  11800. #endif
  11801. emit_uint32(loader_ctx, type_idx);
  11802. emit_uint32(loader_ctx, table_idx);
  11803. #endif
  11804. #if WASM_ENABLE_MEMORY64 != 0
  11805. table_elem_idx_type = is_table_64bit(module, table_idx)
  11806. ? VALUE_TYPE_I64
  11807. : VALUE_TYPE_I32;
  11808. #endif
  11809. /* skip elem idx */
  11810. POP_TBL_ELEM_IDX();
  11811. if (!check_function_type(module, type_idx, error_buf,
  11812. error_buf_size)) {
  11813. goto fail;
  11814. }
  11815. func_type = (WASMFuncType *)module->types[type_idx];
  11816. if (func_type->param_count > 0) {
  11817. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11818. idx--) {
  11819. #if WASM_ENABLE_FAST_INTERP != 0
  11820. POP_OFFSET_TYPE(func_type->types[idx]);
  11821. #endif
  11822. POP_TYPE(func_type->types[idx]);
  11823. }
  11824. }
  11825. #if WASM_ENABLE_TAIL_CALL != 0
  11826. if (opcode == WASM_OP_CALL_INDIRECT) {
  11827. #endif
  11828. for (i = 0; i < func_type->result_count; i++) {
  11829. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11830. #if WASM_ENABLE_FAST_INTERP != 0
  11831. PUSH_OFFSET_TYPE(
  11832. func_type->types[func_type->param_count + i]);
  11833. #endif
  11834. }
  11835. #if WASM_ENABLE_TAIL_CALL != 0
  11836. }
  11837. else {
  11838. uint8 type;
  11839. if (func_type->result_count
  11840. != func->func_type->result_count) {
  11841. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11842. "type mismatch: expect ",
  11843. func->func_type->result_count,
  11844. " return values but got other");
  11845. goto fail;
  11846. }
  11847. for (i = 0; i < func_type->result_count; i++) {
  11848. type = func->func_type
  11849. ->types[func->func_type->param_count + i];
  11850. if (func_type->types[func_type->param_count + i]
  11851. != type) {
  11852. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11853. "type mismatch: expect ",
  11854. type2str(type), " but got other");
  11855. goto fail;
  11856. }
  11857. }
  11858. RESET_STACK();
  11859. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11860. }
  11861. #endif
  11862. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11863. || WASM_ENABLE_WAMR_COMPILER != 0
  11864. func->has_op_func_call = true;
  11865. #endif
  11866. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11867. func->has_op_call_indirect = true;
  11868. #endif
  11869. break;
  11870. }
  11871. case WASM_OP_DROP:
  11872. {
  11873. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11874. int32 available_stack_cell =
  11875. (int32)(loader_ctx->stack_cell_num
  11876. - cur_block->stack_cell_num);
  11877. if (available_stack_cell <= 0
  11878. && !cur_block->is_stack_polymorphic) {
  11879. set_error_buf(error_buf, error_buf_size,
  11880. "type mismatch, opcode drop was found "
  11881. "but stack was empty");
  11882. goto fail;
  11883. }
  11884. if (available_stack_cell > 0) {
  11885. #if WASM_ENABLE_GC != 0
  11886. if (wasm_is_type_multi_byte_type(
  11887. *(loader_ctx->frame_ref - 1))) {
  11888. bh_assert((int32)(loader_ctx->reftype_map_num
  11889. - cur_block->reftype_map_num)
  11890. > 0);
  11891. loader_ctx->frame_reftype_map--;
  11892. loader_ctx->reftype_map_num--;
  11893. }
  11894. #endif
  11895. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11896. loader_ctx->frame_ref--;
  11897. loader_ctx->stack_cell_num--;
  11898. #if WASM_ENABLE_FAST_INTERP != 0
  11899. skip_label();
  11900. loader_ctx->frame_offset--;
  11901. if ((*(loader_ctx->frame_offset)
  11902. > loader_ctx->start_dynamic_offset)
  11903. && (*(loader_ctx->frame_offset)
  11904. < loader_ctx->max_dynamic_offset))
  11905. loader_ctx->dynamic_offset--;
  11906. #endif
  11907. }
  11908. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11909. loader_ctx->frame_ref -= 2;
  11910. loader_ctx->stack_cell_num -= 2;
  11911. #if WASM_ENABLE_FAST_INTERP == 0
  11912. *(p - 1) = WASM_OP_DROP_64;
  11913. #endif
  11914. #if WASM_ENABLE_FAST_INTERP != 0
  11915. skip_label();
  11916. loader_ctx->frame_offset -= 2;
  11917. if ((*(loader_ctx->frame_offset)
  11918. > loader_ctx->start_dynamic_offset)
  11919. && (*(loader_ctx->frame_offset)
  11920. < loader_ctx->max_dynamic_offset))
  11921. loader_ctx->dynamic_offset -= 2;
  11922. #endif
  11923. }
  11924. #if WASM_ENABLE_SIMD != 0
  11925. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11926. || (WASM_ENABLE_FAST_INTERP != 0)
  11927. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11928. loader_ctx->frame_ref -= 4;
  11929. loader_ctx->stack_cell_num -= 4;
  11930. #if WASM_ENABLE_FAST_INTERP != 0
  11931. skip_label();
  11932. loader_ctx->frame_offset -= 4;
  11933. if ((*(loader_ctx->frame_offset)
  11934. > loader_ctx->start_dynamic_offset)
  11935. && (*(loader_ctx->frame_offset)
  11936. < loader_ctx->max_dynamic_offset))
  11937. loader_ctx->dynamic_offset -= 4;
  11938. #endif
  11939. }
  11940. #endif
  11941. #endif
  11942. else {
  11943. set_error_buf(error_buf, error_buf_size,
  11944. "type mismatch");
  11945. goto fail;
  11946. }
  11947. }
  11948. else {
  11949. #if WASM_ENABLE_FAST_INTERP != 0
  11950. skip_label();
  11951. #endif
  11952. }
  11953. break;
  11954. }
  11955. case WASM_OP_SELECT:
  11956. {
  11957. uint8 ref_type;
  11958. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11959. int32 available_stack_cell;
  11960. #if WASM_ENABLE_FAST_INTERP != 0
  11961. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11962. #endif
  11963. POP_I32();
  11964. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11965. - cur_block->stack_cell_num);
  11966. if (available_stack_cell <= 0
  11967. && !cur_block->is_stack_polymorphic) {
  11968. set_error_buf(error_buf, error_buf_size,
  11969. "type mismatch or invalid result arity, "
  11970. "opcode select was found "
  11971. "but stack was empty");
  11972. goto fail;
  11973. }
  11974. if (available_stack_cell > 0) {
  11975. switch (*(loader_ctx->frame_ref - 1)) {
  11976. case VALUE_TYPE_I32:
  11977. case VALUE_TYPE_F32:
  11978. case VALUE_TYPE_ANY:
  11979. break;
  11980. case VALUE_TYPE_I64:
  11981. case VALUE_TYPE_F64:
  11982. #if WASM_ENABLE_FAST_INTERP == 0
  11983. *(p - 1) = WASM_OP_SELECT_64;
  11984. #else
  11985. if (loader_ctx->p_code_compiled) {
  11986. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11987. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11988. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11989. *(void **)(p_code_compiled_tmp
  11990. - sizeof(void *)) =
  11991. handle_table[opcode_tmp];
  11992. #elif UINTPTR_MAX == UINT64_MAX
  11993. /* emit int32 relative offset in 64-bit target
  11994. */
  11995. int32 offset =
  11996. (int32)((uint8 *)handle_table[opcode_tmp]
  11997. - (uint8 *)handle_table[0]);
  11998. *(int32 *)(p_code_compiled_tmp
  11999. - sizeof(int32)) = offset;
  12000. #else
  12001. /* emit uint32 label address in 32-bit target */
  12002. *(uint32 *)(p_code_compiled_tmp
  12003. - sizeof(uint32)) =
  12004. (uint32)(uintptr_t)handle_table[opcode_tmp];
  12005. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12006. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  12007. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  12008. *(p_code_compiled_tmp - 1) = opcode_tmp;
  12009. #else
  12010. *(p_code_compiled_tmp - 2) = opcode_tmp;
  12011. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12012. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  12013. }
  12014. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12015. break;
  12016. #if WASM_ENABLE_SIMD != 0
  12017. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  12018. || (WASM_ENABLE_FAST_INTERP != 0)
  12019. case VALUE_TYPE_V128:
  12020. #if WASM_ENABLE_SIMDE != 0
  12021. if (loader_ctx->p_code_compiled) {
  12022. uint8 opcode_tmp = WASM_OP_SELECT_128;
  12023. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  12024. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  12025. *(void **)(p_code_compiled_tmp
  12026. - sizeof(void *)) =
  12027. handle_table[opcode_tmp];
  12028. #elif UINTPTR_MAX == UINT64_MAX
  12029. /* emit int32 relative offset in 64-bit target
  12030. */
  12031. int32 offset =
  12032. (int32)((uint8 *)handle_table[opcode_tmp]
  12033. - (uint8 *)handle_table[0]);
  12034. *(int32 *)(p_code_compiled_tmp
  12035. - sizeof(int32)) = offset;
  12036. #else
  12037. /* emit uint32 label address in 32-bit target */
  12038. *(uint32 *)(p_code_compiled_tmp
  12039. - sizeof(uint32)) =
  12040. (uint32)(uintptr_t)handle_table[opcode_tmp];
  12041. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12042. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  12043. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  12044. *(p_code_compiled_tmp - 1) = opcode_tmp;
  12045. #else
  12046. *(p_code_compiled_tmp - 2) = opcode_tmp;
  12047. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12048. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  12049. }
  12050. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12051. break;
  12052. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  12053. (WASM_ENABLE_FAST_INTERP != 0) */
  12054. #endif /* WASM_ENABLE_SIMD != 0 */
  12055. default:
  12056. {
  12057. set_error_buf(error_buf, error_buf_size,
  12058. "type mismatch");
  12059. goto fail;
  12060. }
  12061. }
  12062. ref_type = *(loader_ctx->frame_ref - 1);
  12063. #if WASM_ENABLE_FAST_INTERP != 0
  12064. POP_OFFSET_TYPE(ref_type);
  12065. POP_TYPE(ref_type);
  12066. POP_OFFSET_TYPE(ref_type);
  12067. POP_TYPE(ref_type);
  12068. PUSH_OFFSET_TYPE(ref_type);
  12069. PUSH_TYPE(ref_type);
  12070. #else
  12071. POP2_AND_PUSH(ref_type, ref_type);
  12072. #endif
  12073. }
  12074. else {
  12075. #if WASM_ENABLE_FAST_INTERP != 0
  12076. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  12077. #endif
  12078. PUSH_TYPE(VALUE_TYPE_ANY);
  12079. }
  12080. break;
  12081. }
  12082. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  12083. case WASM_OP_SELECT_T:
  12084. {
  12085. uint8 vec_len, type;
  12086. #if WASM_ENABLE_GC != 0
  12087. WASMRefType *ref_type = NULL;
  12088. #endif
  12089. #if WASM_ENABLE_FAST_INTERP != 0
  12090. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  12091. #endif
  12092. pb_read_leb_uint32(p, p_end, vec_len);
  12093. if (vec_len != 1) {
  12094. /* typed select must have exactly one result */
  12095. set_error_buf(error_buf, error_buf_size,
  12096. "invalid result arity");
  12097. goto fail;
  12098. }
  12099. #if WASM_ENABLE_GC == 0
  12100. CHECK_BUF(p, p_end, 1);
  12101. type = read_uint8(p);
  12102. if (!is_valid_value_type_for_interpreter(type)) {
  12103. set_error_buf(error_buf, error_buf_size,
  12104. "unknown value type");
  12105. goto fail;
  12106. }
  12107. #else
  12108. p_org = p + 1;
  12109. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  12110. module->type_count, &need_ref_type_map,
  12111. &wasm_ref_type, false, error_buf,
  12112. error_buf_size)) {
  12113. goto fail;
  12114. }
  12115. type = wasm_ref_type.ref_type;
  12116. if (need_ref_type_map) {
  12117. if (!(ref_type = reftype_set_insert(
  12118. module->ref_type_set, &wasm_ref_type, error_buf,
  12119. error_buf_size))) {
  12120. goto fail;
  12121. }
  12122. }
  12123. #if WASM_ENABLE_FAST_INTERP == 0
  12124. while (p_org < p) {
  12125. #if WASM_ENABLE_DEBUG_INTERP != 0
  12126. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12127. error_buf_size)) {
  12128. goto fail;
  12129. }
  12130. #endif
  12131. /* Ignore extra bytes for interpreter */
  12132. *p_org++ = WASM_OP_NOP;
  12133. }
  12134. #endif
  12135. #endif /* end of WASM_ENABLE_GC == 0 */
  12136. POP_I32();
  12137. #if WASM_ENABLE_FAST_INTERP != 0
  12138. if (loader_ctx->p_code_compiled) {
  12139. uint8 opcode_tmp = WASM_OP_SELECT;
  12140. if (type == VALUE_TYPE_V128) {
  12141. #if WASM_ENABLE_SIMDE != 0
  12142. opcode_tmp = WASM_OP_SELECT_128;
  12143. #else
  12144. set_error_buf(error_buf, error_buf_size,
  12145. "v128 value type requires simd feature");
  12146. #endif
  12147. }
  12148. else {
  12149. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  12150. opcode_tmp = WASM_OP_SELECT_64;
  12151. #if WASM_ENABLE_GC != 0
  12152. if (wasm_is_type_reftype(type))
  12153. opcode_tmp = WASM_OP_SELECT_T;
  12154. #endif
  12155. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  12156. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  12157. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  12158. handle_table[opcode_tmp];
  12159. #else
  12160. #if UINTPTR_MAX == UINT64_MAX
  12161. /* emit int32 relative offset in 64-bit target */
  12162. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  12163. - (uint8 *)handle_table[0]);
  12164. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  12165. offset;
  12166. #else
  12167. /* emit uint32 label address in 32-bit target */
  12168. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  12169. (uint32)(uintptr_t)handle_table[opcode_tmp];
  12170. #endif
  12171. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12172. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  12173. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  12174. *(p_code_compiled_tmp - 1) = opcode_tmp;
  12175. #else
  12176. *(p_code_compiled_tmp - 2) = opcode_tmp;
  12177. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  12178. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  12179. }
  12180. }
  12181. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  12182. POP_REF(type);
  12183. #if WASM_ENABLE_GC != 0
  12184. if (need_ref_type_map) {
  12185. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12186. wasm_reftype_struct_size(ref_type));
  12187. }
  12188. #endif
  12189. POP_REF(type);
  12190. #if WASM_ENABLE_GC != 0
  12191. if (need_ref_type_map) {
  12192. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12193. wasm_reftype_struct_size(ref_type));
  12194. }
  12195. #endif
  12196. PUSH_REF(type);
  12197. #if WASM_ENABLE_WAMR_COMPILER != 0
  12198. module->is_ref_types_used = true;
  12199. #endif
  12200. (void)vec_len;
  12201. break;
  12202. }
  12203. /* table.get x. tables[x]. [it] -> [t] */
  12204. /* table.set x. tables[x]. [it t] -> [] */
  12205. case WASM_OP_TABLE_GET:
  12206. case WASM_OP_TABLE_SET:
  12207. {
  12208. uint8 decl_ref_type;
  12209. #if WASM_ENABLE_GC != 0
  12210. WASMRefType *ref_type;
  12211. #endif
  12212. pb_read_leb_uint32(p, p_end, table_idx);
  12213. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  12214. #if WASM_ENABLE_GC != 0
  12215. (void **)&ref_type,
  12216. #else
  12217. NULL,
  12218. #endif
  12219. error_buf, error_buf_size))
  12220. goto fail;
  12221. #if WASM_ENABLE_GC != 0
  12222. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  12223. bh_assert(ref_type);
  12224. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  12225. ref_type, wasm_reftype_struct_size(ref_type));
  12226. }
  12227. #endif
  12228. #if WASM_ENABLE_FAST_INTERP != 0
  12229. emit_uint32(loader_ctx, table_idx);
  12230. #endif
  12231. #if WASM_ENABLE_MEMORY64 != 0
  12232. table_elem_idx_type = is_table_64bit(module, table_idx)
  12233. ? VALUE_TYPE_I64
  12234. : VALUE_TYPE_I32;
  12235. #endif
  12236. if (opcode == WASM_OP_TABLE_GET) {
  12237. POP_TBL_ELEM_IDX();
  12238. #if WASM_ENABLE_FAST_INTERP != 0
  12239. PUSH_OFFSET_TYPE(decl_ref_type);
  12240. #endif
  12241. PUSH_TYPE(decl_ref_type);
  12242. }
  12243. else {
  12244. #if WASM_ENABLE_FAST_INTERP != 0
  12245. POP_OFFSET_TYPE(decl_ref_type);
  12246. #endif
  12247. POP_TYPE(decl_ref_type);
  12248. POP_TBL_ELEM_IDX();
  12249. }
  12250. #if WASM_ENABLE_WAMR_COMPILER != 0
  12251. module->is_ref_types_used = true;
  12252. #endif
  12253. break;
  12254. }
  12255. case WASM_OP_REF_NULL:
  12256. {
  12257. uint8 ref_type;
  12258. #if WASM_ENABLE_GC == 0
  12259. CHECK_BUF(p, p_end, 1);
  12260. ref_type = read_uint8(p);
  12261. if (ref_type != VALUE_TYPE_FUNCREF
  12262. && ref_type != VALUE_TYPE_EXTERNREF) {
  12263. set_error_buf(error_buf, error_buf_size, "type mismatch");
  12264. goto fail;
  12265. }
  12266. #else
  12267. pb_read_leb_int32(p, p_end, heap_type);
  12268. if (heap_type >= 0) {
  12269. if (!check_type_index(module, module->type_count, heap_type,
  12270. error_buf, error_buf_size)) {
  12271. goto fail;
  12272. }
  12273. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12274. true, heap_type);
  12275. ref_type = wasm_ref_type.ref_type;
  12276. }
  12277. else {
  12278. if (!wasm_is_valid_heap_type(heap_type)) {
  12279. set_error_buf(error_buf, error_buf_size,
  12280. "unknown type");
  12281. goto fail;
  12282. }
  12283. ref_type = (uint8)((int32)0x80 + heap_type);
  12284. }
  12285. #endif /* end of WASM_ENABLE_GC == 0 */
  12286. #if WASM_ENABLE_FAST_INTERP != 0
  12287. PUSH_OFFSET_TYPE(ref_type);
  12288. #endif
  12289. PUSH_TYPE(ref_type);
  12290. #if WASM_ENABLE_WAMR_COMPILER != 0
  12291. module->is_ref_types_used = true;
  12292. #endif
  12293. break;
  12294. }
  12295. case WASM_OP_REF_IS_NULL:
  12296. {
  12297. #if WASM_ENABLE_GC == 0
  12298. #if WASM_ENABLE_FAST_INTERP != 0
  12299. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12300. int32 block_stack_cell_num =
  12301. (int32)(loader_ctx->stack_cell_num
  12302. - cur_block->stack_cell_num);
  12303. if (block_stack_cell_num <= 0) {
  12304. if (!cur_block->is_stack_polymorphic) {
  12305. set_error_buf(
  12306. error_buf, error_buf_size,
  12307. "type mismatch: expect data but stack was empty");
  12308. goto fail;
  12309. }
  12310. }
  12311. else {
  12312. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  12313. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  12314. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  12315. if (!wasm_loader_pop_frame_ref_offset(
  12316. loader_ctx, *(loader_ctx->frame_ref - 1),
  12317. error_buf, error_buf_size)) {
  12318. goto fail;
  12319. }
  12320. }
  12321. else {
  12322. set_error_buf(error_buf, error_buf_size,
  12323. "type mismatch");
  12324. goto fail;
  12325. }
  12326. }
  12327. #else
  12328. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  12329. error_buf, error_buf_size)
  12330. && !wasm_loader_pop_frame_ref(loader_ctx,
  12331. VALUE_TYPE_EXTERNREF,
  12332. error_buf, error_buf_size)) {
  12333. goto fail;
  12334. }
  12335. #endif
  12336. #else /* else of WASM_ENABLE_GC == 0 */
  12337. uint8 type;
  12338. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  12339. error_buf, error_buf_size)) {
  12340. goto fail;
  12341. }
  12342. #endif
  12343. PUSH_I32();
  12344. #if WASM_ENABLE_WAMR_COMPILER != 0
  12345. module->is_ref_types_used = true;
  12346. #endif
  12347. break;
  12348. }
  12349. case WASM_OP_REF_FUNC:
  12350. {
  12351. pb_read_leb_uint32(p, p_end, func_idx);
  12352. if (!check_function_index(module, func_idx, error_buf,
  12353. error_buf_size)) {
  12354. goto fail;
  12355. }
  12356. /* Refer to a forward-declared function:
  12357. the function must be an import, exported, or present in
  12358. a table elem segment or global initializer to be used as
  12359. the operand to ref.func */
  12360. if (func_idx >= module->import_function_count) {
  12361. WASMTableSeg *table_seg = module->table_segments;
  12362. bool func_declared = false;
  12363. uint32 j;
  12364. for (i = 0; i < module->global_count; i++) {
  12365. if (module->globals[i].type.val_type
  12366. == VALUE_TYPE_FUNCREF
  12367. && module->globals[i].init_expr.init_expr_type
  12368. == INIT_EXPR_TYPE_FUNCREF_CONST
  12369. && module->globals[i].init_expr.u.unary.v.u32
  12370. == func_idx) {
  12371. func_declared = true;
  12372. break;
  12373. }
  12374. }
  12375. if (!func_declared) {
  12376. /* Check whether the function is declared in table segs,
  12377. note that it doesn't matter whether the table seg's
  12378. mode is passive, active or declarative. */
  12379. for (i = 0; i < module->table_seg_count;
  12380. i++, table_seg++) {
  12381. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  12382. #if WASM_ENABLE_GC != 0
  12383. /* elem type is (ref null? func) or
  12384. (ref null? $t) */
  12385. || ((table_seg->elem_type
  12386. == REF_TYPE_HT_NON_NULLABLE
  12387. || table_seg->elem_type
  12388. == REF_TYPE_HT_NULLABLE)
  12389. && (table_seg->elem_ref_type->ref_ht_common
  12390. .heap_type
  12391. == HEAP_TYPE_FUNC
  12392. || table_seg->elem_ref_type
  12393. ->ref_ht_common.heap_type
  12394. > 0))
  12395. #endif
  12396. ) {
  12397. for (j = 0; j < table_seg->value_count; j++) {
  12398. if (table_seg->init_values[j]
  12399. .u.unary.v.ref_index
  12400. == func_idx) {
  12401. func_declared = true;
  12402. break;
  12403. }
  12404. }
  12405. }
  12406. }
  12407. }
  12408. if (!func_declared) {
  12409. /* Check whether the function is exported */
  12410. for (i = 0; i < module->export_count; i++) {
  12411. if (module->exports[i].kind == EXPORT_KIND_FUNC
  12412. && module->exports[i].index == func_idx) {
  12413. func_declared = true;
  12414. break;
  12415. }
  12416. }
  12417. }
  12418. if (!func_declared) {
  12419. set_error_buf(error_buf, error_buf_size,
  12420. "undeclared function reference");
  12421. goto fail;
  12422. }
  12423. }
  12424. #if WASM_ENABLE_FAST_INTERP != 0
  12425. emit_uint32(loader_ctx, func_idx);
  12426. #endif
  12427. #if WASM_ENABLE_GC == 0
  12428. PUSH_FUNCREF();
  12429. #else
  12430. if (func_idx < module->import_function_count)
  12431. type_idx =
  12432. module->import_functions[func_idx].u.function.type_idx;
  12433. else
  12434. type_idx = module
  12435. ->functions[func_idx
  12436. - module->import_function_count]
  12437. ->type_idx;
  12438. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  12439. false, type_idx);
  12440. PUSH_REF(wasm_ref_type.ref_type);
  12441. #endif
  12442. #if WASM_ENABLE_WAMR_COMPILER != 0
  12443. module->is_ref_types_used = true;
  12444. #endif
  12445. break;
  12446. }
  12447. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  12448. #if WASM_ENABLE_GC != 0
  12449. case WASM_OP_REF_AS_NON_NULL:
  12450. case WASM_OP_BR_ON_NULL:
  12451. {
  12452. uint8 type;
  12453. WASMRefType ref_type;
  12454. /* POP (ref null ht) and get the converted (ref ht) */
  12455. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12456. error_buf, error_buf_size)) {
  12457. goto fail;
  12458. }
  12459. if (opcode == WASM_OP_BR_ON_NULL) {
  12460. if (!(frame_csp_tmp =
  12461. check_branch_block(loader_ctx, &p, p_end, opcode,
  12462. error_buf, error_buf_size))) {
  12463. goto fail;
  12464. }
  12465. #if WASM_ENABLE_FAST_INTERP != 0
  12466. disable_emit = true;
  12467. #endif
  12468. }
  12469. /* PUSH the converted (ref ht) */
  12470. if (type != VALUE_TYPE_ANY) {
  12471. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  12472. sizeof(WASMRefType));
  12473. }
  12474. PUSH_REF(type);
  12475. break;
  12476. }
  12477. case WASM_OP_BR_ON_NON_NULL:
  12478. {
  12479. uint8 type;
  12480. WASMRefType ref_type;
  12481. uint32 available_stack_cell =
  12482. loader_ctx->stack_cell_num
  12483. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  12484. /* POP (ref null ht) and get the converted (ref ht) */
  12485. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  12486. error_buf, error_buf_size)) {
  12487. goto fail;
  12488. }
  12489. #if WASM_ENABLE_FAST_INTERP != 0
  12490. disable_emit = true;
  12491. #endif
  12492. /* Temporarily PUSH back (ref ht), check brach block and
  12493. then POP it */
  12494. if (available_stack_cell
  12495. > 0) { /* stack isn't in polymorphic state */
  12496. if (type != VALUE_TYPE_ANY) {
  12497. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12498. &ref_type, sizeof(WASMRefType));
  12499. }
  12500. PUSH_REF(type);
  12501. }
  12502. if (!(frame_csp_tmp =
  12503. check_branch_block(loader_ctx, &p, p_end, opcode,
  12504. error_buf, error_buf_size))) {
  12505. goto fail;
  12506. }
  12507. if (available_stack_cell
  12508. > 0) { /* stack isn't in polymorphic state */
  12509. POP_REF(type);
  12510. #if WASM_ENABLE_FAST_INTERP != 0
  12511. /* Erase the opnd offset emitted by POP_REF() */
  12512. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12513. #endif
  12514. }
  12515. break;
  12516. }
  12517. case WASM_OP_REF_EQ:
  12518. POP_REF(REF_TYPE_EQREF);
  12519. POP_REF(REF_TYPE_EQREF);
  12520. PUSH_I32();
  12521. break;
  12522. #endif /* end of WASM_ENABLE_GC != 0 */
  12523. case WASM_OP_GET_LOCAL:
  12524. {
  12525. p_org = p - 1;
  12526. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12527. PUSH_TYPE(local_type);
  12528. #if WASM_ENABLE_GC != 0
  12529. /* Cannot get a non-nullable and unset local */
  12530. if (local_idx >= param_count
  12531. && wasm_is_reftype_htref_non_nullable(local_type)
  12532. && !wasm_loader_get_local_status(loader_ctx,
  12533. local_idx - param_count)) {
  12534. set_error_buf(error_buf, error_buf_size,
  12535. "uninitialized local");
  12536. goto fail;
  12537. }
  12538. #endif
  12539. #if WASM_ENABLE_FAST_INTERP != 0
  12540. /* Get Local is optimized out */
  12541. skip_label();
  12542. disable_emit = true;
  12543. operand_offset = local_offset;
  12544. PUSH_OFFSET_TYPE(local_type);
  12545. #else
  12546. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12547. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12548. if (local_offset < 0x80
  12549. #if WASM_ENABLE_GC != 0
  12550. && !wasm_is_type_reftype(local_type)
  12551. #endif
  12552. ) {
  12553. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12554. if (is_32bit_type(local_type)) {
  12555. *p_org++ = (uint8)local_offset;
  12556. }
  12557. else {
  12558. *p_org++ = (uint8)(local_offset | 0x80);
  12559. }
  12560. while (p_org < p) {
  12561. *p_org++ = WASM_OP_NOP;
  12562. }
  12563. }
  12564. #endif
  12565. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12566. break;
  12567. }
  12568. case WASM_OP_SET_LOCAL:
  12569. {
  12570. p_org = p - 1;
  12571. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12572. #if WASM_ENABLE_FAST_INTERP != 0
  12573. if (!(preserve_referenced_local(
  12574. loader_ctx, opcode, local_offset, local_type,
  12575. &preserve_local, error_buf, error_buf_size)))
  12576. goto fail;
  12577. if (local_offset < 256
  12578. #if WASM_ENABLE_GC != 0
  12579. && !wasm_is_type_reftype(local_type)
  12580. #endif
  12581. ) {
  12582. skip_label();
  12583. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12584. if (loader_ctx->p_code_compiled)
  12585. STORE_U16(loader_ctx->p_code_compiled - 2,
  12586. local_offset);
  12587. loader_ctx->frame_offset--;
  12588. loader_ctx->dynamic_offset--;
  12589. }
  12590. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12591. if (loader_ctx->p_code_compiled)
  12592. STORE_U16(loader_ctx->p_code_compiled - 2,
  12593. local_offset);
  12594. loader_ctx->frame_offset -= 2;
  12595. loader_ctx->dynamic_offset -= 2;
  12596. }
  12597. else {
  12598. if (is_32bit_type(local_type)) {
  12599. emit_label(EXT_OP_SET_LOCAL_FAST);
  12600. emit_byte(loader_ctx, (uint8)local_offset);
  12601. }
  12602. else if (is_64bit_type(local_type)) {
  12603. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12604. emit_byte(loader_ctx, (uint8)local_offset);
  12605. }
  12606. #if WASM_ENABLE_SIMDE != 0
  12607. else if (local_type == VALUE_TYPE_V128) {
  12608. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12609. emit_byte(loader_ctx, (uint8)local_offset);
  12610. }
  12611. #endif
  12612. else {
  12613. set_error_buf(error_buf, error_buf_size,
  12614. "unknown local type");
  12615. goto fail;
  12616. }
  12617. POP_OFFSET_TYPE(local_type);
  12618. }
  12619. }
  12620. else { /* local index larger than 255, reserve leb */
  12621. emit_uint32(loader_ctx, local_idx);
  12622. POP_OFFSET_TYPE(local_type);
  12623. }
  12624. #else
  12625. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12626. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12627. if (local_offset < 0x80
  12628. #if WASM_ENABLE_GC != 0
  12629. && !wasm_is_type_reftype(local_type)
  12630. #endif
  12631. ) {
  12632. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12633. if (is_32bit_type(local_type)) {
  12634. *p_org++ = (uint8)local_offset;
  12635. }
  12636. else {
  12637. *p_org++ = (uint8)(local_offset | 0x80);
  12638. }
  12639. while (p_org < p) {
  12640. *p_org++ = WASM_OP_NOP;
  12641. }
  12642. }
  12643. #endif
  12644. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12645. #if WASM_ENABLE_GC != 0
  12646. if (local_idx >= param_count) {
  12647. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12648. }
  12649. #endif
  12650. POP_TYPE(local_type);
  12651. break;
  12652. }
  12653. case WASM_OP_TEE_LOCAL:
  12654. {
  12655. p_org = p - 1;
  12656. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12657. #if WASM_ENABLE_FAST_INTERP != 0
  12658. /* If the stack is in polymorphic state, do fake pop and push on
  12659. offset stack to keep the depth of offset stack to be the
  12660. same with ref stack */
  12661. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12662. if (cur_block->is_stack_polymorphic) {
  12663. POP_OFFSET_TYPE(local_type);
  12664. PUSH_OFFSET_TYPE(local_type);
  12665. }
  12666. #endif
  12667. POP_TYPE(local_type);
  12668. PUSH_TYPE(local_type);
  12669. #if WASM_ENABLE_FAST_INTERP != 0
  12670. if (!(preserve_referenced_local(
  12671. loader_ctx, opcode, local_offset, local_type,
  12672. &preserve_local, error_buf, error_buf_size)))
  12673. goto fail;
  12674. if (local_offset < 256
  12675. #if WASM_ENABLE_GC != 0
  12676. && !wasm_is_type_reftype(local_type)
  12677. #endif
  12678. ) {
  12679. skip_label();
  12680. if (is_32bit_type(local_type)) {
  12681. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12682. emit_byte(loader_ctx, (uint8)local_offset);
  12683. }
  12684. #if WASM_ENABLE_SIMDE != 0
  12685. else if (local_type == VALUE_TYPE_V128) {
  12686. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12687. emit_byte(loader_ctx, (uint8)local_offset);
  12688. }
  12689. #endif
  12690. else {
  12691. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12692. emit_byte(loader_ctx, (uint8)local_offset);
  12693. }
  12694. }
  12695. else { /* local index larger than 255, reserve leb */
  12696. emit_uint32(loader_ctx, local_idx);
  12697. }
  12698. emit_operand(loader_ctx,
  12699. *(loader_ctx->frame_offset
  12700. - wasm_value_type_cell_num(local_type)));
  12701. #else
  12702. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12703. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12704. if (local_offset < 0x80
  12705. #if WASM_ENABLE_GC != 0
  12706. && !wasm_is_type_reftype(local_type)
  12707. #endif
  12708. ) {
  12709. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12710. if (is_32bit_type(local_type)) {
  12711. *p_org++ = (uint8)local_offset;
  12712. }
  12713. else {
  12714. *p_org++ = (uint8)(local_offset | 0x80);
  12715. }
  12716. while (p_org < p) {
  12717. *p_org++ = WASM_OP_NOP;
  12718. }
  12719. }
  12720. #endif
  12721. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12722. #if WASM_ENABLE_GC != 0
  12723. if (local_idx >= param_count) {
  12724. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12725. }
  12726. #endif
  12727. break;
  12728. }
  12729. case WASM_OP_GET_GLOBAL:
  12730. {
  12731. #if WASM_ENABLE_GC != 0
  12732. WASMRefType *ref_type;
  12733. #endif
  12734. p_org = p - 1;
  12735. pb_read_leb_uint32(p, p_end, global_idx);
  12736. if (global_idx >= global_count) {
  12737. set_error_buf(error_buf, error_buf_size, "unknown global");
  12738. goto fail;
  12739. }
  12740. global_type = global_idx < module->import_global_count
  12741. ? module->import_globals[global_idx]
  12742. .u.global.type.val_type
  12743. : module
  12744. ->globals[global_idx
  12745. - module->import_global_count]
  12746. .type.val_type;
  12747. #if WASM_ENABLE_GC != 0
  12748. ref_type =
  12749. global_idx < module->import_global_count
  12750. ? module->import_globals[global_idx].u.global.ref_type
  12751. : module
  12752. ->globals[global_idx
  12753. - module->import_global_count]
  12754. .ref_type;
  12755. if (wasm_is_type_multi_byte_type(global_type)) {
  12756. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12757. wasm_reftype_struct_size(ref_type));
  12758. }
  12759. #endif
  12760. PUSH_TYPE(global_type);
  12761. #if WASM_ENABLE_FAST_INTERP == 0
  12762. if (global_type == VALUE_TYPE_I64
  12763. || global_type == VALUE_TYPE_F64) {
  12764. #if WASM_ENABLE_DEBUG_INTERP != 0
  12765. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12766. error_buf_size)) {
  12767. goto fail;
  12768. }
  12769. #endif
  12770. *p_org = WASM_OP_GET_GLOBAL_64;
  12771. }
  12772. #else /* else of WASM_ENABLE_FAST_INTERP */
  12773. if (global_type == VALUE_TYPE_I64
  12774. || global_type == VALUE_TYPE_F64) {
  12775. skip_label();
  12776. emit_label(WASM_OP_GET_GLOBAL_64);
  12777. }
  12778. #if WASM_ENABLE_SIMDE != 0
  12779. if (global_type == VALUE_TYPE_V128) {
  12780. skip_label();
  12781. emit_label(WASM_OP_GET_GLOBAL_V128);
  12782. }
  12783. #endif /* end of WASM_ENABLE_SIMDE */
  12784. emit_uint32(loader_ctx, global_idx);
  12785. PUSH_OFFSET_TYPE(global_type);
  12786. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12787. break;
  12788. }
  12789. case WASM_OP_SET_GLOBAL:
  12790. {
  12791. bool is_mutable = false;
  12792. #if WASM_ENABLE_GC != 0
  12793. WASMRefType *ref_type;
  12794. #endif
  12795. p_org = p - 1;
  12796. pb_read_leb_uint32(p, p_end, global_idx);
  12797. if (global_idx >= global_count) {
  12798. set_error_buf(error_buf, error_buf_size, "unknown global");
  12799. goto fail;
  12800. }
  12801. is_mutable = global_idx < module->import_global_count
  12802. ? module->import_globals[global_idx]
  12803. .u.global.type.is_mutable
  12804. : module
  12805. ->globals[global_idx
  12806. - module->import_global_count]
  12807. .type.is_mutable;
  12808. if (!is_mutable) {
  12809. #if WASM_ENABLE_GC == 0
  12810. set_error_buf(error_buf, error_buf_size,
  12811. "global is immutable");
  12812. #else
  12813. set_error_buf(error_buf, error_buf_size,
  12814. "immutable global");
  12815. #endif
  12816. goto fail;
  12817. }
  12818. global_type = global_idx < module->import_global_count
  12819. ? module->import_globals[global_idx]
  12820. .u.global.type.val_type
  12821. : module
  12822. ->globals[global_idx
  12823. - module->import_global_count]
  12824. .type.val_type;
  12825. #if WASM_ENABLE_GC != 0
  12826. ref_type =
  12827. global_idx < module->import_global_count
  12828. ? module->import_globals[global_idx].u.global.ref_type
  12829. : module
  12830. ->globals[global_idx
  12831. - module->import_global_count]
  12832. .ref_type;
  12833. if (wasm_is_type_multi_byte_type(global_type)) {
  12834. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12835. wasm_reftype_struct_size(ref_type));
  12836. }
  12837. #endif
  12838. #if WASM_ENABLE_FAST_INTERP == 0
  12839. if (global_type == VALUE_TYPE_I64
  12840. || global_type == VALUE_TYPE_F64) {
  12841. #if WASM_ENABLE_DEBUG_INTERP != 0
  12842. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12843. error_buf_size)) {
  12844. goto fail;
  12845. }
  12846. #endif
  12847. *p_org = WASM_OP_SET_GLOBAL_64;
  12848. }
  12849. else if (module->aux_stack_size > 0
  12850. && global_idx == module->aux_stack_top_global_index) {
  12851. #if WASM_ENABLE_DEBUG_INTERP != 0
  12852. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12853. error_buf_size)) {
  12854. goto fail;
  12855. }
  12856. #endif
  12857. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12858. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12859. func->has_op_set_global_aux_stack = true;
  12860. #endif
  12861. }
  12862. #else /* else of WASM_ENABLE_FAST_INTERP */
  12863. if (global_type == VALUE_TYPE_I64
  12864. || global_type == VALUE_TYPE_F64) {
  12865. skip_label();
  12866. emit_label(WASM_OP_SET_GLOBAL_64);
  12867. }
  12868. else if (module->aux_stack_size > 0
  12869. && global_idx == module->aux_stack_top_global_index) {
  12870. skip_label();
  12871. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12872. }
  12873. #if WASM_ENABLE_SIMDE != 0
  12874. else if (global_type == VALUE_TYPE_V128) {
  12875. skip_label();
  12876. emit_label(WASM_OP_SET_GLOBAL_V128);
  12877. }
  12878. #endif /* end of WASM_ENABLE_SIMDE */
  12879. emit_uint32(loader_ctx, global_idx);
  12880. POP_OFFSET_TYPE(global_type);
  12881. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12882. POP_TYPE(global_type);
  12883. break;
  12884. }
  12885. /* load */
  12886. case WASM_OP_I32_LOAD:
  12887. case WASM_OP_I32_LOAD8_S:
  12888. case WASM_OP_I32_LOAD8_U:
  12889. case WASM_OP_I32_LOAD16_S:
  12890. case WASM_OP_I32_LOAD16_U:
  12891. case WASM_OP_I64_LOAD:
  12892. case WASM_OP_I64_LOAD8_S:
  12893. case WASM_OP_I64_LOAD8_U:
  12894. case WASM_OP_I64_LOAD16_S:
  12895. case WASM_OP_I64_LOAD16_U:
  12896. case WASM_OP_I64_LOAD32_S:
  12897. case WASM_OP_I64_LOAD32_U:
  12898. case WASM_OP_F32_LOAD:
  12899. case WASM_OP_F64_LOAD:
  12900. /* store */
  12901. case WASM_OP_I32_STORE:
  12902. case WASM_OP_I32_STORE8:
  12903. case WASM_OP_I32_STORE16:
  12904. case WASM_OP_I64_STORE:
  12905. case WASM_OP_I64_STORE8:
  12906. case WASM_OP_I64_STORE16:
  12907. case WASM_OP_I64_STORE32:
  12908. case WASM_OP_F32_STORE:
  12909. case WASM_OP_F64_STORE:
  12910. {
  12911. #if WASM_ENABLE_FAST_INTERP != 0
  12912. /* change F32/F64 into I32/I64 */
  12913. if (opcode == WASM_OP_F32_LOAD) {
  12914. skip_label();
  12915. emit_label(WASM_OP_I32_LOAD);
  12916. }
  12917. else if (opcode == WASM_OP_F64_LOAD) {
  12918. skip_label();
  12919. emit_label(WASM_OP_I64_LOAD);
  12920. }
  12921. else if (opcode == WASM_OP_F32_STORE) {
  12922. skip_label();
  12923. emit_label(WASM_OP_I32_STORE);
  12924. }
  12925. else if (opcode == WASM_OP_F64_STORE) {
  12926. skip_label();
  12927. emit_label(WASM_OP_I64_STORE);
  12928. }
  12929. #endif
  12930. CHECK_MEMORY();
  12931. pb_read_leb_memarg(p, p_end, align); /* align */
  12932. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12933. if (!check_memory_access_align(opcode, align, error_buf,
  12934. error_buf_size)) {
  12935. goto fail;
  12936. }
  12937. #if WASM_ENABLE_FAST_INTERP != 0
  12938. emit_uint32(loader_ctx, mem_offset);
  12939. #endif
  12940. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12941. func->has_memory_operations = true;
  12942. #endif
  12943. switch (opcode) {
  12944. /* load */
  12945. case WASM_OP_I32_LOAD:
  12946. case WASM_OP_I32_LOAD8_S:
  12947. case WASM_OP_I32_LOAD8_U:
  12948. case WASM_OP_I32_LOAD16_S:
  12949. case WASM_OP_I32_LOAD16_U:
  12950. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12951. break;
  12952. case WASM_OP_I64_LOAD:
  12953. case WASM_OP_I64_LOAD8_S:
  12954. case WASM_OP_I64_LOAD8_U:
  12955. case WASM_OP_I64_LOAD16_S:
  12956. case WASM_OP_I64_LOAD16_U:
  12957. case WASM_OP_I64_LOAD32_S:
  12958. case WASM_OP_I64_LOAD32_U:
  12959. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12960. break;
  12961. case WASM_OP_F32_LOAD:
  12962. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12963. break;
  12964. case WASM_OP_F64_LOAD:
  12965. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12966. break;
  12967. /* store */
  12968. case WASM_OP_I32_STORE:
  12969. case WASM_OP_I32_STORE8:
  12970. case WASM_OP_I32_STORE16:
  12971. POP_I32();
  12972. POP_MEM_OFFSET();
  12973. break;
  12974. case WASM_OP_I64_STORE:
  12975. case WASM_OP_I64_STORE8:
  12976. case WASM_OP_I64_STORE16:
  12977. case WASM_OP_I64_STORE32:
  12978. POP_I64();
  12979. POP_MEM_OFFSET();
  12980. break;
  12981. case WASM_OP_F32_STORE:
  12982. POP_F32();
  12983. POP_MEM_OFFSET();
  12984. break;
  12985. case WASM_OP_F64_STORE:
  12986. POP_F64();
  12987. POP_MEM_OFFSET();
  12988. break;
  12989. default:
  12990. break;
  12991. }
  12992. break;
  12993. }
  12994. case WASM_OP_MEMORY_SIZE:
  12995. CHECK_MEMORY();
  12996. pb_read_leb_uint32(p, p_end, memidx);
  12997. check_memidx(module, memidx);
  12998. PUSH_PAGE_COUNT();
  12999. module->possible_memory_grow = true;
  13000. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13001. func->has_memory_operations = true;
  13002. #endif
  13003. break;
  13004. case WASM_OP_MEMORY_GROW:
  13005. CHECK_MEMORY();
  13006. pb_read_leb_uint32(p, p_end, memidx);
  13007. check_memidx(module, memidx);
  13008. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  13009. module->possible_memory_grow = true;
  13010. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  13011. || WASM_ENABLE_WAMR_COMPILER != 0
  13012. func->has_op_memory_grow = true;
  13013. #endif
  13014. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13015. func->has_memory_operations = true;
  13016. #endif
  13017. break;
  13018. case WASM_OP_I32_CONST:
  13019. pb_read_leb_int32(p, p_end, i32_const);
  13020. #if WASM_ENABLE_FAST_INTERP != 0
  13021. skip_label();
  13022. disable_emit = true;
  13023. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  13024. if (operand_offset == 0) {
  13025. disable_emit = false;
  13026. emit_label(WASM_OP_I32_CONST);
  13027. emit_uint32(loader_ctx, i32_const);
  13028. }
  13029. #else
  13030. (void)i32_const;
  13031. #endif
  13032. PUSH_I32();
  13033. break;
  13034. case WASM_OP_I64_CONST:
  13035. pb_read_leb_int64(p, p_end, i64_const);
  13036. #if WASM_ENABLE_FAST_INTERP != 0
  13037. skip_label();
  13038. disable_emit = true;
  13039. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  13040. if (operand_offset == 0) {
  13041. disable_emit = false;
  13042. emit_label(WASM_OP_I64_CONST);
  13043. emit_uint64(loader_ctx, i64_const);
  13044. }
  13045. #endif
  13046. PUSH_I64();
  13047. break;
  13048. case WASM_OP_F32_CONST:
  13049. CHECK_BUF(p, p_end, sizeof(float32));
  13050. p += sizeof(float32);
  13051. #if WASM_ENABLE_FAST_INTERP != 0
  13052. skip_label();
  13053. disable_emit = true;
  13054. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  13055. sizeof(float32));
  13056. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  13057. if (operand_offset == 0) {
  13058. disable_emit = false;
  13059. emit_label(WASM_OP_F32_CONST);
  13060. emit_float32(loader_ctx, f32_const);
  13061. }
  13062. #endif
  13063. PUSH_F32();
  13064. break;
  13065. case WASM_OP_F64_CONST:
  13066. CHECK_BUF(p, p_end, sizeof(float64));
  13067. p += sizeof(float64);
  13068. #if WASM_ENABLE_FAST_INTERP != 0
  13069. skip_label();
  13070. disable_emit = true;
  13071. /* Some MCU may require 8-byte align */
  13072. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  13073. sizeof(float64));
  13074. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  13075. if (operand_offset == 0) {
  13076. disable_emit = false;
  13077. emit_label(WASM_OP_F64_CONST);
  13078. emit_float64(loader_ctx, f64_const);
  13079. }
  13080. #endif
  13081. PUSH_F64();
  13082. break;
  13083. case WASM_OP_I32_EQZ:
  13084. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13085. break;
  13086. case WASM_OP_I32_EQ:
  13087. case WASM_OP_I32_NE:
  13088. case WASM_OP_I32_LT_S:
  13089. case WASM_OP_I32_LT_U:
  13090. case WASM_OP_I32_GT_S:
  13091. case WASM_OP_I32_GT_U:
  13092. case WASM_OP_I32_LE_S:
  13093. case WASM_OP_I32_LE_U:
  13094. case WASM_OP_I32_GE_S:
  13095. case WASM_OP_I32_GE_U:
  13096. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13097. break;
  13098. case WASM_OP_I64_EQZ:
  13099. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13100. break;
  13101. case WASM_OP_I64_EQ:
  13102. case WASM_OP_I64_NE:
  13103. case WASM_OP_I64_LT_S:
  13104. case WASM_OP_I64_LT_U:
  13105. case WASM_OP_I64_GT_S:
  13106. case WASM_OP_I64_GT_U:
  13107. case WASM_OP_I64_LE_S:
  13108. case WASM_OP_I64_LE_U:
  13109. case WASM_OP_I64_GE_S:
  13110. case WASM_OP_I64_GE_U:
  13111. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13112. break;
  13113. case WASM_OP_F32_EQ:
  13114. case WASM_OP_F32_NE:
  13115. case WASM_OP_F32_LT:
  13116. case WASM_OP_F32_GT:
  13117. case WASM_OP_F32_LE:
  13118. case WASM_OP_F32_GE:
  13119. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13120. break;
  13121. case WASM_OP_F64_EQ:
  13122. case WASM_OP_F64_NE:
  13123. case WASM_OP_F64_LT:
  13124. case WASM_OP_F64_GT:
  13125. case WASM_OP_F64_LE:
  13126. case WASM_OP_F64_GE:
  13127. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13128. break;
  13129. case WASM_OP_I32_CLZ:
  13130. case WASM_OP_I32_CTZ:
  13131. case WASM_OP_I32_POPCNT:
  13132. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13133. break;
  13134. case WASM_OP_I32_ADD:
  13135. case WASM_OP_I32_SUB:
  13136. case WASM_OP_I32_MUL:
  13137. case WASM_OP_I32_DIV_S:
  13138. case WASM_OP_I32_DIV_U:
  13139. case WASM_OP_I32_REM_S:
  13140. case WASM_OP_I32_REM_U:
  13141. case WASM_OP_I32_AND:
  13142. case WASM_OP_I32_OR:
  13143. case WASM_OP_I32_XOR:
  13144. case WASM_OP_I32_SHL:
  13145. case WASM_OP_I32_SHR_S:
  13146. case WASM_OP_I32_SHR_U:
  13147. case WASM_OP_I32_ROTL:
  13148. case WASM_OP_I32_ROTR:
  13149. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13150. break;
  13151. case WASM_OP_I64_CLZ:
  13152. case WASM_OP_I64_CTZ:
  13153. case WASM_OP_I64_POPCNT:
  13154. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13155. break;
  13156. case WASM_OP_I64_ADD:
  13157. case WASM_OP_I64_SUB:
  13158. case WASM_OP_I64_MUL:
  13159. case WASM_OP_I64_DIV_S:
  13160. case WASM_OP_I64_DIV_U:
  13161. case WASM_OP_I64_REM_S:
  13162. case WASM_OP_I64_REM_U:
  13163. case WASM_OP_I64_AND:
  13164. case WASM_OP_I64_OR:
  13165. case WASM_OP_I64_XOR:
  13166. case WASM_OP_I64_SHL:
  13167. case WASM_OP_I64_SHR_S:
  13168. case WASM_OP_I64_SHR_U:
  13169. case WASM_OP_I64_ROTL:
  13170. case WASM_OP_I64_ROTR:
  13171. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13172. break;
  13173. case WASM_OP_F32_ABS:
  13174. case WASM_OP_F32_NEG:
  13175. case WASM_OP_F32_CEIL:
  13176. case WASM_OP_F32_FLOOR:
  13177. case WASM_OP_F32_TRUNC:
  13178. case WASM_OP_F32_NEAREST:
  13179. case WASM_OP_F32_SQRT:
  13180. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  13181. break;
  13182. case WASM_OP_F32_ADD:
  13183. case WASM_OP_F32_SUB:
  13184. case WASM_OP_F32_MUL:
  13185. case WASM_OP_F32_DIV:
  13186. case WASM_OP_F32_MIN:
  13187. case WASM_OP_F32_MAX:
  13188. case WASM_OP_F32_COPYSIGN:
  13189. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  13190. break;
  13191. case WASM_OP_F64_ABS:
  13192. case WASM_OP_F64_NEG:
  13193. case WASM_OP_F64_CEIL:
  13194. case WASM_OP_F64_FLOOR:
  13195. case WASM_OP_F64_TRUNC:
  13196. case WASM_OP_F64_NEAREST:
  13197. case WASM_OP_F64_SQRT:
  13198. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13199. break;
  13200. case WASM_OP_F64_ADD:
  13201. case WASM_OP_F64_SUB:
  13202. case WASM_OP_F64_MUL:
  13203. case WASM_OP_F64_DIV:
  13204. case WASM_OP_F64_MIN:
  13205. case WASM_OP_F64_MAX:
  13206. case WASM_OP_F64_COPYSIGN:
  13207. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  13208. break;
  13209. case WASM_OP_I32_WRAP_I64:
  13210. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  13211. break;
  13212. case WASM_OP_I32_TRUNC_S_F32:
  13213. case WASM_OP_I32_TRUNC_U_F32:
  13214. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13215. break;
  13216. case WASM_OP_I32_TRUNC_S_F64:
  13217. case WASM_OP_I32_TRUNC_U_F64:
  13218. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13219. break;
  13220. case WASM_OP_I64_EXTEND_S_I32:
  13221. case WASM_OP_I64_EXTEND_U_I32:
  13222. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  13223. break;
  13224. case WASM_OP_I64_TRUNC_S_F32:
  13225. case WASM_OP_I64_TRUNC_U_F32:
  13226. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13227. break;
  13228. case WASM_OP_I64_TRUNC_S_F64:
  13229. case WASM_OP_I64_TRUNC_U_F64:
  13230. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13231. break;
  13232. case WASM_OP_F32_CONVERT_S_I32:
  13233. case WASM_OP_F32_CONVERT_U_I32:
  13234. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13235. break;
  13236. case WASM_OP_F32_CONVERT_S_I64:
  13237. case WASM_OP_F32_CONVERT_U_I64:
  13238. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  13239. break;
  13240. case WASM_OP_F32_DEMOTE_F64:
  13241. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  13242. break;
  13243. case WASM_OP_F64_CONVERT_S_I32:
  13244. case WASM_OP_F64_CONVERT_U_I32:
  13245. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  13246. break;
  13247. case WASM_OP_F64_CONVERT_S_I64:
  13248. case WASM_OP_F64_CONVERT_U_I64:
  13249. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13250. break;
  13251. case WASM_OP_F64_PROMOTE_F32:
  13252. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  13253. break;
  13254. case WASM_OP_I32_REINTERPRET_F32:
  13255. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13256. break;
  13257. case WASM_OP_I64_REINTERPRET_F64:
  13258. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13259. break;
  13260. case WASM_OP_F32_REINTERPRET_I32:
  13261. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  13262. break;
  13263. case WASM_OP_F64_REINTERPRET_I64:
  13264. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  13265. break;
  13266. case WASM_OP_I32_EXTEND8_S:
  13267. case WASM_OP_I32_EXTEND16_S:
  13268. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  13269. break;
  13270. case WASM_OP_I64_EXTEND8_S:
  13271. case WASM_OP_I64_EXTEND16_S:
  13272. case WASM_OP_I64_EXTEND32_S:
  13273. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  13274. break;
  13275. #if WASM_ENABLE_GC != 0
  13276. case WASM_OP_GC_PREFIX:
  13277. {
  13278. uint32 opcode1;
  13279. pb_read_leb_uint32(p, p_end, opcode1);
  13280. #if WASM_ENABLE_FAST_INTERP != 0
  13281. emit_byte(loader_ctx, ((uint8)opcode1));
  13282. #endif
  13283. switch (opcode1) {
  13284. case WASM_OP_STRUCT_NEW:
  13285. case WASM_OP_STRUCT_NEW_DEFAULT:
  13286. {
  13287. pb_read_leb_uint32(p, p_end, type_idx);
  13288. #if WASM_ENABLE_FAST_INTERP != 0
  13289. emit_uint32(loader_ctx, type_idx);
  13290. #endif
  13291. if (!check_type_index(module, module->type_count,
  13292. type_idx, error_buf,
  13293. error_buf_size)) {
  13294. goto fail;
  13295. }
  13296. if (module->types[type_idx]->type_flag
  13297. != WASM_TYPE_STRUCT) {
  13298. set_error_buf(error_buf, error_buf_size,
  13299. "unknown struct type");
  13300. goto fail;
  13301. }
  13302. if (opcode1 == WASM_OP_STRUCT_NEW) {
  13303. int32 j, k;
  13304. uint8 value_type;
  13305. uint32 ref_type_struct_size;
  13306. WASMStructType *struct_type =
  13307. (WASMStructType *)module->types[type_idx];
  13308. k = struct_type->ref_type_map_count - 1;
  13309. for (j = struct_type->field_count - 1; j >= 0;
  13310. j--) {
  13311. value_type = struct_type->fields[j].field_type;
  13312. if (wasm_is_type_reftype(value_type)) {
  13313. if (wasm_is_type_multi_byte_type(
  13314. value_type)) {
  13315. ref_type_struct_size =
  13316. wasm_reftype_struct_size(
  13317. struct_type->ref_type_maps[k]
  13318. .ref_type);
  13319. bh_memcpy_s(
  13320. &wasm_ref_type,
  13321. (uint32)sizeof(WASMRefType),
  13322. struct_type->ref_type_maps[k]
  13323. .ref_type,
  13324. ref_type_struct_size);
  13325. k--;
  13326. }
  13327. POP_REF(value_type);
  13328. }
  13329. else {
  13330. switch (value_type) {
  13331. case VALUE_TYPE_I32:
  13332. case PACKED_TYPE_I8:
  13333. case PACKED_TYPE_I16:
  13334. POP_I32();
  13335. break;
  13336. case VALUE_TYPE_I64:
  13337. POP_I64();
  13338. break;
  13339. case VALUE_TYPE_F32:
  13340. POP_F32();
  13341. break;
  13342. case VALUE_TYPE_F64:
  13343. POP_F64();
  13344. break;
  13345. default:
  13346. set_error_buf(error_buf,
  13347. error_buf_size,
  13348. "unknown type");
  13349. goto fail;
  13350. }
  13351. }
  13352. }
  13353. }
  13354. /* PUSH struct obj, (ref $t) */
  13355. wasm_set_refheaptype_typeidx(
  13356. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13357. PUSH_REF(wasm_ref_type.ref_type);
  13358. break;
  13359. }
  13360. case WASM_OP_STRUCT_GET:
  13361. case WASM_OP_STRUCT_GET_S:
  13362. case WASM_OP_STRUCT_GET_U:
  13363. case WASM_OP_STRUCT_SET:
  13364. {
  13365. WASMStructType *struct_type;
  13366. WASMRefType *ref_type = NULL;
  13367. uint32 field_idx;
  13368. uint8 field_type;
  13369. pb_read_leb_uint32(p, p_end, type_idx);
  13370. #if WASM_ENABLE_FAST_INTERP != 0
  13371. emit_uint32(loader_ctx, type_idx);
  13372. #endif
  13373. if (!check_type_index(module, module->type_count,
  13374. type_idx, error_buf,
  13375. error_buf_size)) {
  13376. goto fail;
  13377. }
  13378. if (module->types[type_idx]->type_flag
  13379. != WASM_TYPE_STRUCT) {
  13380. set_error_buf(error_buf, error_buf_size,
  13381. "unknown struct type");
  13382. goto fail;
  13383. }
  13384. struct_type = (WASMStructType *)module->types[type_idx];
  13385. pb_read_leb_uint32(p, p_end, field_idx);
  13386. #if WASM_ENABLE_FAST_INTERP != 0
  13387. emit_uint32(loader_ctx, field_idx);
  13388. #endif
  13389. if (field_idx >= struct_type->field_count) {
  13390. set_error_buf(error_buf, error_buf_size,
  13391. "unknown struct field");
  13392. goto fail;
  13393. }
  13394. if (opcode1 == WASM_OP_STRUCT_SET
  13395. && !(struct_type->fields[field_idx].field_flags
  13396. & 1)) {
  13397. set_error_buf(error_buf, error_buf_size,
  13398. "field is immutable");
  13399. goto fail;
  13400. }
  13401. field_type = struct_type->fields[field_idx].field_type;
  13402. if (is_packed_type(field_type)) {
  13403. if (opcode1 == WASM_OP_STRUCT_GET) {
  13404. set_error_buf(error_buf, error_buf_size,
  13405. "type mismatch");
  13406. goto fail;
  13407. }
  13408. else {
  13409. field_type = VALUE_TYPE_I32;
  13410. }
  13411. }
  13412. if (wasm_is_type_multi_byte_type(field_type)) {
  13413. ref_type = wasm_reftype_map_find(
  13414. struct_type->ref_type_maps,
  13415. struct_type->ref_type_map_count, field_idx);
  13416. bh_assert(ref_type);
  13417. }
  13418. if (opcode1 == WASM_OP_STRUCT_SET) {
  13419. /* POP field */
  13420. if (wasm_is_type_multi_byte_type(field_type)) {
  13421. bh_memcpy_s(&wasm_ref_type,
  13422. (uint32)sizeof(WASMRefType),
  13423. ref_type,
  13424. wasm_reftype_struct_size(ref_type));
  13425. }
  13426. POP_REF(field_type);
  13427. /* POP struct obj, (ref null $t) */
  13428. wasm_set_refheaptype_typeidx(
  13429. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13430. POP_REF(wasm_ref_type.ref_type);
  13431. }
  13432. else {
  13433. /* POP struct obj, (ref null $t) */
  13434. wasm_set_refheaptype_typeidx(
  13435. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13436. POP_REF(wasm_ref_type.ref_type);
  13437. /* PUSH field */
  13438. if (wasm_is_type_multi_byte_type(field_type)) {
  13439. bh_memcpy_s(&wasm_ref_type,
  13440. (uint32)sizeof(WASMRefType),
  13441. ref_type,
  13442. wasm_reftype_struct_size(ref_type));
  13443. }
  13444. PUSH_REF(field_type);
  13445. }
  13446. break;
  13447. }
  13448. case WASM_OP_ARRAY_NEW:
  13449. case WASM_OP_ARRAY_NEW_DEFAULT:
  13450. case WASM_OP_ARRAY_NEW_FIXED:
  13451. case WASM_OP_ARRAY_NEW_DATA:
  13452. case WASM_OP_ARRAY_NEW_ELEM:
  13453. {
  13454. WASMArrayType *array_type;
  13455. uint8 elem_type;
  13456. uint32 u32 = 0;
  13457. pb_read_leb_uint32(p, p_end, type_idx);
  13458. #if WASM_ENABLE_FAST_INTERP != 0
  13459. emit_uint32(loader_ctx, type_idx);
  13460. #endif
  13461. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  13462. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  13463. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13464. pb_read_leb_uint32(p, p_end, u32);
  13465. #if WASM_ENABLE_FAST_INTERP != 0
  13466. emit_uint32(loader_ctx, u32);
  13467. #endif
  13468. }
  13469. if (!check_array_type(module, type_idx, error_buf,
  13470. error_buf_size)) {
  13471. goto fail;
  13472. }
  13473. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  13474. /* length */
  13475. POP_I32();
  13476. }
  13477. array_type = (WASMArrayType *)module->types[type_idx];
  13478. elem_type = array_type->elem_type;
  13479. if (opcode1 == WASM_OP_ARRAY_NEW
  13480. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13481. if (wasm_is_type_multi_byte_type(elem_type)) {
  13482. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13483. array_type->elem_ref_type,
  13484. wasm_reftype_struct_size(
  13485. array_type->elem_ref_type));
  13486. }
  13487. if (is_packed_type(elem_type)) {
  13488. elem_type = VALUE_TYPE_I32;
  13489. }
  13490. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  13491. uint32 N = u32;
  13492. for (i = 0; i < N; i++) {
  13493. if (wasm_is_type_multi_byte_type(
  13494. elem_type)) {
  13495. bh_memcpy_s(
  13496. &wasm_ref_type, sizeof(WASMRefType),
  13497. array_type->elem_ref_type,
  13498. wasm_reftype_struct_size(
  13499. array_type->elem_ref_type));
  13500. }
  13501. POP_REF(elem_type);
  13502. }
  13503. }
  13504. else
  13505. POP_REF(elem_type);
  13506. }
  13507. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  13508. /* offset of data segment */
  13509. POP_I32();
  13510. if (u32 >= module->data_seg_count) {
  13511. set_error_buf(error_buf, error_buf_size,
  13512. "unknown data segment");
  13513. goto fail;
  13514. }
  13515. if (wasm_is_type_reftype(elem_type)) {
  13516. set_error_buf(error_buf, error_buf_size,
  13517. "array elem type mismatch");
  13518. goto fail;
  13519. }
  13520. }
  13521. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  13522. WASMTableSeg *table_seg =
  13523. module->table_segments + u32;
  13524. /* offset of element segment */
  13525. POP_I32();
  13526. if (u32 >= module->table_seg_count) {
  13527. set_error_buf(error_buf, error_buf_size,
  13528. "unknown element segment");
  13529. goto fail;
  13530. }
  13531. if (!wasm_reftype_is_subtype_of(
  13532. table_seg->elem_type,
  13533. table_seg->elem_ref_type, elem_type,
  13534. array_type->elem_ref_type, module->types,
  13535. module->type_count)) {
  13536. set_error_buf(error_buf, error_buf_size,
  13537. "array elem type mismatch");
  13538. goto fail;
  13539. }
  13540. }
  13541. /* PUSH array obj, (ref $t) */
  13542. wasm_set_refheaptype_typeidx(
  13543. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13544. PUSH_REF(wasm_ref_type.ref_type);
  13545. break;
  13546. }
  13547. case WASM_OP_ARRAY_GET:
  13548. case WASM_OP_ARRAY_GET_S:
  13549. case WASM_OP_ARRAY_GET_U:
  13550. case WASM_OP_ARRAY_SET:
  13551. {
  13552. uint8 elem_type;
  13553. WASMArrayType *array_type;
  13554. WASMRefType *ref_type = NULL;
  13555. pb_read_leb_uint32(p, p_end, type_idx);
  13556. #if WASM_ENABLE_FAST_INTERP != 0
  13557. emit_uint32(loader_ctx, type_idx);
  13558. #endif
  13559. if (!check_array_type(module, type_idx, error_buf,
  13560. error_buf_size)) {
  13561. goto fail;
  13562. }
  13563. array_type = (WASMArrayType *)module->types[type_idx];
  13564. if (opcode1 == WASM_OP_ARRAY_SET
  13565. && !(array_type->elem_flags & 1)) {
  13566. set_error_buf(error_buf, error_buf_size,
  13567. "array is immutable");
  13568. goto fail;
  13569. }
  13570. elem_type = array_type->elem_type;
  13571. if (is_packed_type(elem_type)) {
  13572. if (opcode1 != WASM_OP_ARRAY_GET_S
  13573. && opcode1 != WASM_OP_ARRAY_GET_U
  13574. && opcode1 != WASM_OP_ARRAY_SET) {
  13575. set_error_buf(error_buf, error_buf_size,
  13576. "type mismatch");
  13577. goto fail;
  13578. }
  13579. else {
  13580. elem_type = VALUE_TYPE_I32;
  13581. }
  13582. }
  13583. ref_type = array_type->elem_ref_type;
  13584. if (opcode1 == WASM_OP_ARRAY_SET) {
  13585. /* POP elem to set */
  13586. if (wasm_is_type_multi_byte_type(elem_type)) {
  13587. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13588. ref_type,
  13589. wasm_reftype_struct_size(ref_type));
  13590. }
  13591. POP_REF(elem_type);
  13592. }
  13593. /* elem idx */
  13594. POP_I32();
  13595. /* POP array obj, (ref null $t) */
  13596. wasm_set_refheaptype_typeidx(
  13597. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13598. POP_REF(wasm_ref_type.ref_type);
  13599. if (opcode1 != WASM_OP_ARRAY_SET) {
  13600. /* PUSH elem */
  13601. if (wasm_is_type_multi_byte_type(elem_type)) {
  13602. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13603. ref_type,
  13604. wasm_reftype_struct_size(ref_type));
  13605. }
  13606. PUSH_REF(elem_type);
  13607. }
  13608. break;
  13609. }
  13610. case WASM_OP_ARRAY_LEN:
  13611. {
  13612. POP_REF(REF_TYPE_ARRAYREF);
  13613. /* length */
  13614. PUSH_I32();
  13615. break;
  13616. }
  13617. case WASM_OP_ARRAY_FILL:
  13618. {
  13619. WASMArrayType *array_type;
  13620. uint8 elem_type;
  13621. /* typeidx */
  13622. pb_read_leb_uint32(p, p_end, type_idx);
  13623. #if WASM_ENABLE_FAST_INTERP != 0
  13624. emit_uint32(loader_ctx, type_idx);
  13625. #endif
  13626. if (!check_array_type(module, type_idx, error_buf,
  13627. error_buf_size)) {
  13628. goto fail;
  13629. }
  13630. array_type = (WASMArrayType *)module->types[type_idx];
  13631. if (!(array_type->elem_flags & 1)) {
  13632. set_error_buf(error_buf, error_buf_size,
  13633. "array is immutable");
  13634. goto fail;
  13635. }
  13636. elem_type = array_type->elem_type;
  13637. if (is_packed_type(elem_type)) {
  13638. elem_type = VALUE_TYPE_I32;
  13639. }
  13640. POP_I32(); /* length */
  13641. #if WASM_ENABLE_FAST_INTERP != 0
  13642. POP_OFFSET_TYPE(elem_type);
  13643. #endif
  13644. POP_TYPE(elem_type);
  13645. POP_I32(); /* start */
  13646. /* POP array obj, (ref null $t) */
  13647. wasm_set_refheaptype_typeidx(
  13648. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13649. POP_REF(wasm_ref_type.ref_type);
  13650. break;
  13651. }
  13652. case WASM_OP_ARRAY_COPY:
  13653. {
  13654. uint32 src_type_idx;
  13655. uint8 src_elem_type, dst_elem_type;
  13656. WASMRefType src_ref_type = { 0 },
  13657. *src_elem_ref_type = NULL;
  13658. WASMRefType dst_ref_type = { 0 },
  13659. *dst_elem_ref_type = NULL;
  13660. WASMArrayType *array_type;
  13661. /* typeidx1 */
  13662. pb_read_leb_uint32(p, p_end, type_idx);
  13663. #if WASM_ENABLE_FAST_INTERP != 0
  13664. emit_uint32(loader_ctx, type_idx);
  13665. #endif
  13666. /* typeidx2 */
  13667. pb_read_leb_uint32(p, p_end, src_type_idx);
  13668. #if WASM_ENABLE_FAST_INTERP != 0
  13669. emit_uint32(loader_ctx, src_type_idx);
  13670. #endif
  13671. if (!check_array_type(module, type_idx, error_buf,
  13672. error_buf_size)) {
  13673. goto fail;
  13674. }
  13675. if (!check_array_type(module, src_type_idx, error_buf,
  13676. error_buf_size)) {
  13677. goto fail;
  13678. }
  13679. POP_I32();
  13680. POP_I32();
  13681. /* POP array obj, (ref null $t) */
  13682. wasm_set_refheaptype_typeidx(
  13683. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13684. POP_REF(wasm_ref_type.ref_type);
  13685. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13686. &wasm_ref_type,
  13687. wasm_reftype_struct_size(&wasm_ref_type));
  13688. POP_I32();
  13689. /* POP array obj, (ref null $t) */
  13690. wasm_set_refheaptype_typeidx(
  13691. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13692. POP_REF(wasm_ref_type.ref_type);
  13693. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13694. &wasm_ref_type,
  13695. wasm_reftype_struct_size(&wasm_ref_type));
  13696. array_type = (WASMArrayType *)module->types[type_idx];
  13697. if (!(array_type->elem_flags & 1)) {
  13698. set_error_buf(error_buf, error_buf_size,
  13699. "destination array is immutable");
  13700. goto fail;
  13701. }
  13702. dst_elem_type = array_type->elem_type;
  13703. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13704. dst_elem_ref_type = array_type->elem_ref_type;
  13705. }
  13706. array_type =
  13707. (WASMArrayType *)module->types[src_type_idx];
  13708. src_elem_type = array_type->elem_type;
  13709. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13710. src_elem_ref_type = array_type->elem_ref_type;
  13711. }
  13712. if (!wasm_reftype_is_subtype_of(
  13713. src_elem_type, src_elem_ref_type, dst_elem_type,
  13714. dst_elem_ref_type, module->types,
  13715. module->type_count)) {
  13716. set_error_buf(error_buf, error_buf_size,
  13717. "array types do not match");
  13718. goto fail;
  13719. }
  13720. break;
  13721. }
  13722. case WASM_OP_REF_I31:
  13723. {
  13724. POP_I32();
  13725. wasm_set_refheaptype_common(
  13726. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13727. PUSH_REF(wasm_ref_type.ref_type);
  13728. break;
  13729. }
  13730. case WASM_OP_I31_GET_S:
  13731. case WASM_OP_I31_GET_U:
  13732. {
  13733. POP_REF(REF_TYPE_I31REF);
  13734. PUSH_I32();
  13735. break;
  13736. }
  13737. case WASM_OP_REF_TEST:
  13738. case WASM_OP_REF_CAST:
  13739. case WASM_OP_REF_TEST_NULLABLE:
  13740. case WASM_OP_REF_CAST_NULLABLE:
  13741. {
  13742. uint8 type;
  13743. pb_read_leb_int32(p, p_end, heap_type);
  13744. #if WASM_ENABLE_FAST_INTERP != 0
  13745. emit_uint32(loader_ctx, (uint32)heap_type);
  13746. #endif
  13747. if (heap_type >= 0) {
  13748. if (!check_type_index(module, module->type_count,
  13749. heap_type, error_buf,
  13750. error_buf_size)) {
  13751. goto fail;
  13752. }
  13753. }
  13754. else {
  13755. if (!wasm_is_valid_heap_type(heap_type)) {
  13756. set_error_buf(error_buf, error_buf_size,
  13757. "unknown type");
  13758. goto fail;
  13759. }
  13760. }
  13761. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13762. &wasm_ref_type, error_buf,
  13763. error_buf_size)) {
  13764. goto fail;
  13765. }
  13766. if (opcode1 == WASM_OP_REF_TEST
  13767. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13768. PUSH_I32();
  13769. else {
  13770. bool nullable =
  13771. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13772. : false;
  13773. if (heap_type >= 0 || !nullable) {
  13774. wasm_set_refheaptype_typeidx(
  13775. &wasm_ref_type.ref_ht_typeidx, nullable,
  13776. heap_type);
  13777. PUSH_REF(wasm_ref_type.ref_type);
  13778. }
  13779. else {
  13780. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13781. }
  13782. }
  13783. break;
  13784. }
  13785. case WASM_OP_BR_ON_CAST:
  13786. case WASM_OP_BR_ON_CAST_FAIL:
  13787. {
  13788. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13789. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13790. uint8 type_tmp, castflags;
  13791. uint32 depth;
  13792. int32 heap_type_dst;
  13793. bool src_nullable, dst_nullable;
  13794. CHECK_BUF(p, p_end, 1);
  13795. castflags = read_uint8(p);
  13796. #if WASM_ENABLE_FAST_INTERP != 0
  13797. /* Emit heap_type firstly */
  13798. emit_byte(loader_ctx, castflags);
  13799. #endif
  13800. p_org = p;
  13801. pb_read_leb_uint32(p, p_end, depth);
  13802. pb_read_leb_int32(p, p_end, heap_type);
  13803. #if WASM_ENABLE_FAST_INTERP != 0
  13804. /* Emit heap_type firstly */
  13805. emit_uint32(loader_ctx, (uint32)heap_type);
  13806. #endif
  13807. pb_read_leb_int32(p, p_end, heap_type_dst);
  13808. #if WASM_ENABLE_FAST_INTERP != 0
  13809. /* Emit heap_type firstly */
  13810. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13811. #endif
  13812. (void)depth;
  13813. /*
  13814. * castflags should be 0~3:
  13815. * 0: (non-null, non-null)
  13816. * 1: (null, non-null)
  13817. * 2: (non-null, null)
  13818. * 3: (null, null)
  13819. */
  13820. if (castflags > 3) {
  13821. set_error_buf(error_buf, error_buf_size,
  13822. "invalid castflags");
  13823. break;
  13824. }
  13825. src_nullable =
  13826. (castflags == 1) || (castflags == 3) ? true : false;
  13827. dst_nullable =
  13828. (castflags == 2) || (castflags == 3) ? true : false;
  13829. /* Pop and backup the stack top's ref type */
  13830. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13831. &ref_type_tmp, error_buf,
  13832. error_buf_size)) {
  13833. goto fail;
  13834. }
  13835. /* The reference type rt1 must be valid */
  13836. if (!init_ref_type(module, &ref_type1, src_nullable,
  13837. heap_type, error_buf,
  13838. error_buf_size)) {
  13839. goto fail;
  13840. }
  13841. /* The reference type rt2 must be valid. */
  13842. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13843. heap_type_dst, error_buf,
  13844. error_buf_size)) {
  13845. goto fail;
  13846. }
  13847. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13848. &ref_type2);
  13849. /* The reference type rt2 must match rt1. */
  13850. if (!wasm_reftype_is_subtype_of(
  13851. ref_type2.ref_type, &ref_type2,
  13852. ref_type1.ref_type, &ref_type1, module->types,
  13853. module->type_count)) {
  13854. set_error_buf(error_buf, error_buf_size,
  13855. "type mismatch");
  13856. goto fail;
  13857. }
  13858. p = p_org;
  13859. /* Push ref type casted for branch block check */
  13860. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13861. /* The reference type rt2 must match rt′. */
  13862. type_tmp = ref_type2.ref_type;
  13863. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13864. bh_memcpy_s(
  13865. &wasm_ref_type,
  13866. wasm_reftype_struct_size(&ref_type2),
  13867. &ref_type2,
  13868. wasm_reftype_struct_size(&ref_type2));
  13869. }
  13870. }
  13871. else {
  13872. /* The reference type rt′1 must match rt′. */
  13873. type_tmp = ref_type_diff.ref_type;
  13874. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13875. bh_memcpy_s(
  13876. &wasm_ref_type,
  13877. wasm_reftype_struct_size(&ref_type_diff),
  13878. &ref_type_diff,
  13879. wasm_reftype_struct_size(&ref_type_diff));
  13880. }
  13881. }
  13882. PUSH_REF(type_tmp);
  13883. if (!(frame_csp_tmp = check_branch_block(
  13884. loader_ctx, &p, p_end, opcode, error_buf,
  13885. error_buf_size))) {
  13886. goto fail;
  13887. }
  13888. /* Ignore heap_types */
  13889. skip_leb_uint32(p, p_end);
  13890. skip_leb_uint32(p, p_end);
  13891. /* Restore the original stack top's ref type */
  13892. POP_REF(type_tmp);
  13893. #if WASM_ENABLE_FAST_INTERP != 0
  13894. /* Erase the opnd offset emitted by POP_REF() */
  13895. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13896. #endif
  13897. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13898. type_tmp = ref_type_diff.ref_type;
  13899. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13900. bh_memcpy_s(
  13901. &wasm_ref_type,
  13902. wasm_reftype_struct_size(&ref_type_diff),
  13903. &ref_type_diff,
  13904. wasm_reftype_struct_size(&ref_type_diff));
  13905. }
  13906. }
  13907. else {
  13908. type_tmp = ref_type2.ref_type;
  13909. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13910. bh_memcpy_s(
  13911. &wasm_ref_type,
  13912. wasm_reftype_struct_size(&ref_type2),
  13913. &ref_type2,
  13914. wasm_reftype_struct_size(&ref_type2));
  13915. }
  13916. }
  13917. PUSH_REF(type_tmp);
  13918. #if WASM_ENABLE_FAST_INTERP != 0
  13919. /* Erase the opnd offset emitted by PUSH_REF() */
  13920. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13921. #endif
  13922. break;
  13923. }
  13924. case WASM_OP_ANY_CONVERT_EXTERN:
  13925. {
  13926. uint8 type;
  13927. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13928. &wasm_ref_type, error_buf,
  13929. error_buf_size)) {
  13930. goto fail;
  13931. }
  13932. if (!(type == REF_TYPE_EXTERNREF
  13933. || (type == REF_TYPE_HT_NON_NULLABLE
  13934. && wasm_ref_type.ref_ht_common.heap_type
  13935. == HEAP_TYPE_EXTERN)
  13936. || type == VALUE_TYPE_ANY)) {
  13937. set_error_buf(error_buf, error_buf_size,
  13938. "type mismatch");
  13939. goto fail;
  13940. }
  13941. if (type == REF_TYPE_EXTERNREF)
  13942. type = REF_TYPE_ANYREF;
  13943. else {
  13944. wasm_ref_type.ref_ht_common.heap_type =
  13945. HEAP_TYPE_ANY;
  13946. }
  13947. PUSH_REF(type);
  13948. break;
  13949. }
  13950. case WASM_OP_EXTERN_CONVERT_ANY:
  13951. {
  13952. uint8 type;
  13953. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13954. &wasm_ref_type, error_buf,
  13955. error_buf_size)) {
  13956. goto fail;
  13957. }
  13958. if (type == REF_TYPE_EXTERNREF
  13959. || ((type == REF_TYPE_HT_NULLABLE
  13960. || type == REF_TYPE_HT_NON_NULLABLE)
  13961. && wasm_ref_type.ref_ht_common.heap_type
  13962. == HEAP_TYPE_EXTERN)) {
  13963. set_error_buf(error_buf, error_buf_size,
  13964. "type mismatch");
  13965. goto fail;
  13966. }
  13967. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13968. /* push (ref null extern) */
  13969. type = REF_TYPE_EXTERNREF;
  13970. }
  13971. else {
  13972. /* push (ref extern) */
  13973. type = REF_TYPE_HT_NON_NULLABLE;
  13974. wasm_set_refheaptype_common(
  13975. &wasm_ref_type.ref_ht_common, false,
  13976. HEAP_TYPE_EXTERN);
  13977. }
  13978. PUSH_REF(type);
  13979. break;
  13980. }
  13981. #if WASM_ENABLE_STRINGREF != 0
  13982. case WASM_OP_STRING_NEW_UTF8:
  13983. case WASM_OP_STRING_NEW_WTF16:
  13984. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13985. case WASM_OP_STRING_NEW_WTF8:
  13986. {
  13987. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13988. func->has_memory_operations = true;
  13989. #endif
  13990. pb_read_leb_uint32(p, p_end, memidx);
  13991. #if WASM_ENABLE_FAST_INTERP != 0
  13992. emit_uint32(loader_ctx, (uint32)memidx);
  13993. #endif
  13994. POP_I32();
  13995. POP_I32();
  13996. PUSH_REF(REF_TYPE_STRINGREF);
  13997. break;
  13998. }
  13999. case WASM_OP_STRING_CONST:
  14000. {
  14001. uint32 contents;
  14002. pb_read_leb_uint32(p, p_end, contents);
  14003. #if WASM_ENABLE_FAST_INTERP != 0
  14004. emit_uint32(loader_ctx, (uint32)contents);
  14005. #endif
  14006. PUSH_REF(REF_TYPE_STRINGREF);
  14007. (void)contents;
  14008. break;
  14009. }
  14010. case WASM_OP_STRING_MEASURE_UTF8:
  14011. case WASM_OP_STRING_MEASURE_WTF8:
  14012. case WASM_OP_STRING_MEASURE_WTF16:
  14013. {
  14014. POP_STRINGREF();
  14015. PUSH_I32();
  14016. break;
  14017. }
  14018. case WASM_OP_STRING_ENCODE_UTF8:
  14019. case WASM_OP_STRING_ENCODE_WTF16:
  14020. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  14021. case WASM_OP_STRING_ENCODE_WTF8:
  14022. {
  14023. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14024. func->has_memory_operations = true;
  14025. #endif
  14026. pb_read_leb_uint32(p, p_end, memidx);
  14027. #if WASM_ENABLE_FAST_INTERP != 0
  14028. emit_uint32(loader_ctx, (uint32)memidx);
  14029. #endif
  14030. POP_I32();
  14031. POP_STRINGREF();
  14032. PUSH_I32();
  14033. break;
  14034. }
  14035. case WASM_OP_STRING_CONCAT:
  14036. {
  14037. POP_STRINGREF();
  14038. POP_STRINGREF();
  14039. PUSH_REF(REF_TYPE_STRINGREF);
  14040. break;
  14041. }
  14042. case WASM_OP_STRING_EQ:
  14043. {
  14044. POP_STRINGREF();
  14045. POP_STRINGREF();
  14046. PUSH_I32();
  14047. break;
  14048. }
  14049. case WASM_OP_STRING_IS_USV_SEQUENCE:
  14050. {
  14051. POP_STRINGREF();
  14052. PUSH_I32();
  14053. break;
  14054. }
  14055. case WASM_OP_STRING_AS_WTF8:
  14056. {
  14057. POP_STRINGREF();
  14058. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  14059. break;
  14060. }
  14061. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  14062. {
  14063. POP_I32();
  14064. POP_I32();
  14065. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  14066. PUSH_I32();
  14067. break;
  14068. }
  14069. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  14070. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  14071. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  14072. {
  14073. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14074. func->has_memory_operations = true;
  14075. #endif
  14076. pb_read_leb_uint32(p, p_end, memidx);
  14077. #if WASM_ENABLE_FAST_INTERP != 0
  14078. emit_uint32(loader_ctx, (uint32)memidx);
  14079. #endif
  14080. POP_I32();
  14081. POP_I32();
  14082. POP_I32();
  14083. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  14084. PUSH_I32();
  14085. PUSH_I32();
  14086. break;
  14087. }
  14088. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  14089. {
  14090. POP_I32();
  14091. POP_I32();
  14092. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  14093. PUSH_REF(REF_TYPE_STRINGREF);
  14094. break;
  14095. }
  14096. case WASM_OP_STRING_AS_WTF16:
  14097. {
  14098. POP_STRINGREF();
  14099. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  14100. break;
  14101. }
  14102. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  14103. {
  14104. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14105. PUSH_I32();
  14106. break;
  14107. }
  14108. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  14109. {
  14110. POP_I32();
  14111. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14112. PUSH_I32();
  14113. break;
  14114. }
  14115. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  14116. {
  14117. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14118. func->has_memory_operations = true;
  14119. #endif
  14120. pb_read_leb_uint32(p, p_end, memidx);
  14121. #if WASM_ENABLE_FAST_INTERP != 0
  14122. emit_uint32(loader_ctx, (uint32)memidx);
  14123. #endif
  14124. POP_I32();
  14125. POP_I32();
  14126. POP_I32();
  14127. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14128. PUSH_I32();
  14129. break;
  14130. }
  14131. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  14132. {
  14133. POP_I32();
  14134. POP_I32();
  14135. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  14136. PUSH_REF(REF_TYPE_STRINGREF);
  14137. break;
  14138. }
  14139. case WASM_OP_STRING_AS_ITER:
  14140. {
  14141. POP_STRINGREF();
  14142. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  14143. break;
  14144. }
  14145. case WASM_OP_STRINGVIEW_ITER_NEXT:
  14146. {
  14147. POP_REF(REF_TYPE_STRINGVIEWITER);
  14148. PUSH_I32();
  14149. break;
  14150. }
  14151. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  14152. case WASM_OP_STRINGVIEW_ITER_REWIND:
  14153. {
  14154. POP_I32();
  14155. POP_REF(REF_TYPE_STRINGVIEWITER);
  14156. PUSH_I32();
  14157. break;
  14158. }
  14159. case WASM_OP_STRINGVIEW_ITER_SLICE:
  14160. {
  14161. POP_I32();
  14162. POP_REF(REF_TYPE_STRINGVIEWITER);
  14163. PUSH_REF(REF_TYPE_STRINGREF);
  14164. break;
  14165. }
  14166. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  14167. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  14168. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  14169. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  14170. {
  14171. POP_I32();
  14172. POP_I32();
  14173. POP_REF(REF_TYPE_ARRAYREF);
  14174. PUSH_REF(REF_TYPE_STRINGREF);
  14175. break;
  14176. }
  14177. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  14178. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  14179. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  14180. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  14181. {
  14182. POP_I32();
  14183. POP_REF(REF_TYPE_ARRAYREF);
  14184. POP_STRINGREF();
  14185. PUSH_I32();
  14186. break;
  14187. }
  14188. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  14189. default:
  14190. set_error_buf_v(error_buf, error_buf_size,
  14191. "%s %02x %02x", "unsupported opcode",
  14192. 0xfb, opcode1);
  14193. goto fail;
  14194. }
  14195. break;
  14196. }
  14197. #endif /* end of WASM_ENABLE_GC != 0 */
  14198. case WASM_OP_MISC_PREFIX:
  14199. {
  14200. uint32 opcode1;
  14201. pb_read_leb_uint32(p, p_end, opcode1);
  14202. #if WASM_ENABLE_FAST_INTERP != 0
  14203. emit_byte(loader_ctx, ((uint8)opcode1));
  14204. #endif
  14205. switch (opcode1) {
  14206. case WASM_OP_I32_TRUNC_SAT_S_F32:
  14207. case WASM_OP_I32_TRUNC_SAT_U_F32:
  14208. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  14209. break;
  14210. case WASM_OP_I32_TRUNC_SAT_S_F64:
  14211. case WASM_OP_I32_TRUNC_SAT_U_F64:
  14212. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  14213. break;
  14214. case WASM_OP_I64_TRUNC_SAT_S_F32:
  14215. case WASM_OP_I64_TRUNC_SAT_U_F32:
  14216. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  14217. break;
  14218. case WASM_OP_I64_TRUNC_SAT_S_F64:
  14219. case WASM_OP_I64_TRUNC_SAT_U_F64:
  14220. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  14221. break;
  14222. #if WASM_ENABLE_BULK_MEMORY != 0
  14223. case WASM_OP_MEMORY_INIT:
  14224. {
  14225. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14226. #if WASM_ENABLE_FAST_INTERP != 0
  14227. emit_uint32(loader_ctx, data_seg_idx);
  14228. #endif
  14229. if (module->import_memory_count == 0
  14230. && module->memory_count == 0) {
  14231. set_error_buf(error_buf, error_buf_size,
  14232. "unknown memory 0");
  14233. goto fail;
  14234. }
  14235. pb_read_leb_uint32(p, p_end, memidx);
  14236. check_memidx(module, memidx);
  14237. if (data_seg_idx >= module->data_seg_count) {
  14238. set_error_buf_v(error_buf, error_buf_size,
  14239. "unknown data segment %d",
  14240. data_seg_idx);
  14241. goto fail;
  14242. }
  14243. if (module->data_seg_count1 == 0)
  14244. goto fail_data_cnt_sec_require;
  14245. POP_I32();
  14246. POP_I32();
  14247. POP_MEM_OFFSET();
  14248. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14249. func->has_memory_operations = true;
  14250. #endif
  14251. #if WASM_ENABLE_WAMR_COMPILER != 0
  14252. module->is_bulk_memory_used = true;
  14253. #endif
  14254. break;
  14255. }
  14256. case WASM_OP_DATA_DROP:
  14257. {
  14258. pb_read_leb_uint32(p, p_end, data_seg_idx);
  14259. #if WASM_ENABLE_FAST_INTERP != 0
  14260. emit_uint32(loader_ctx, data_seg_idx);
  14261. #endif
  14262. if (data_seg_idx >= module->data_seg_count) {
  14263. set_error_buf(error_buf, error_buf_size,
  14264. "unknown data segment");
  14265. goto fail;
  14266. }
  14267. if (module->data_seg_count1 == 0)
  14268. goto fail_data_cnt_sec_require;
  14269. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14270. func->has_memory_operations = true;
  14271. #endif
  14272. #if WASM_ENABLE_WAMR_COMPILER != 0
  14273. module->is_bulk_memory_used = true;
  14274. #endif
  14275. break;
  14276. }
  14277. fail_data_cnt_sec_require:
  14278. set_error_buf(error_buf, error_buf_size,
  14279. "data count section required");
  14280. goto fail;
  14281. #endif /* WASM_ENABLE_BULK_MEMORY */
  14282. #if WASM_ENABLE_BULK_MEMORY_OPT != 0
  14283. case WASM_OP_MEMORY_COPY:
  14284. {
  14285. CHECK_BUF(p, p_end, sizeof(int16));
  14286. /* check both src and dst memory index */
  14287. pb_read_leb_uint32(p, p_end, memidx);
  14288. check_memidx(module, memidx);
  14289. pb_read_leb_uint32(p, p_end, memidx);
  14290. check_memidx(module, memidx);
  14291. if (module->import_memory_count == 0
  14292. && module->memory_count == 0) {
  14293. set_error_buf(error_buf, error_buf_size,
  14294. "unknown memory 0");
  14295. goto fail;
  14296. }
  14297. POP_MEM_OFFSET();
  14298. POP_MEM_OFFSET();
  14299. POP_MEM_OFFSET();
  14300. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14301. func->has_memory_operations = true;
  14302. #endif
  14303. #if WASM_ENABLE_WAMR_COMPILER != 0
  14304. module->is_bulk_memory_used = true;
  14305. #endif
  14306. break;
  14307. }
  14308. case WASM_OP_MEMORY_FILL:
  14309. {
  14310. pb_read_leb_uint32(p, p_end, memidx);
  14311. check_memidx(module, memidx);
  14312. if (module->import_memory_count == 0
  14313. && module->memory_count == 0) {
  14314. set_error_buf(error_buf, error_buf_size,
  14315. "unknown memory 0");
  14316. goto fail;
  14317. }
  14318. POP_MEM_OFFSET();
  14319. POP_I32();
  14320. POP_MEM_OFFSET();
  14321. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14322. func->has_memory_operations = true;
  14323. #endif
  14324. #if WASM_ENABLE_WAMR_COMPILER != 0
  14325. module->is_bulk_memory_used = true;
  14326. #endif
  14327. break;
  14328. }
  14329. #endif /* WASM_ENABLE_BULK_MEMORY_OPT */
  14330. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  14331. case WASM_OP_TABLE_INIT:
  14332. {
  14333. uint8 seg_type = 0, tbl_type = 0;
  14334. #if WASM_ENABLE_GC != 0
  14335. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  14336. #endif
  14337. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14338. pb_read_leb_uint32(p, p_end, table_idx);
  14339. if (!get_table_elem_type(module, table_idx, &tbl_type,
  14340. #if WASM_ENABLE_GC != 0
  14341. (void **)&tbl_ref_type,
  14342. #else
  14343. NULL,
  14344. #endif
  14345. error_buf, error_buf_size))
  14346. goto fail;
  14347. if (!get_table_seg_elem_type(module, table_seg_idx,
  14348. &seg_type,
  14349. #if WASM_ENABLE_GC != 0
  14350. (void **)&seg_ref_type,
  14351. #else
  14352. NULL,
  14353. #endif
  14354. error_buf, error_buf_size))
  14355. goto fail;
  14356. #if WASM_ENABLE_GC == 0
  14357. if (seg_type != tbl_type) {
  14358. set_error_buf(error_buf, error_buf_size,
  14359. "type mismatch");
  14360. goto fail;
  14361. }
  14362. #else
  14363. if (!wasm_reftype_is_subtype_of(
  14364. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  14365. module->types, module->type_count)) {
  14366. set_error_buf(error_buf, error_buf_size,
  14367. "type mismatch");
  14368. goto fail;
  14369. }
  14370. #endif
  14371. #if WASM_ENABLE_FAST_INTERP != 0
  14372. emit_uint32(loader_ctx, table_seg_idx);
  14373. emit_uint32(loader_ctx, table_idx);
  14374. #endif
  14375. POP_I32();
  14376. POP_I32();
  14377. #if WASM_ENABLE_MEMORY64 != 0
  14378. table_elem_idx_type = is_table_64bit(module, table_idx)
  14379. ? VALUE_TYPE_I64
  14380. : VALUE_TYPE_I32;
  14381. #endif
  14382. POP_TBL_ELEM_IDX();
  14383. #if WASM_ENABLE_WAMR_COMPILER != 0
  14384. module->is_ref_types_used = true;
  14385. #endif
  14386. break;
  14387. }
  14388. case WASM_OP_ELEM_DROP:
  14389. {
  14390. pb_read_leb_uint32(p, p_end, table_seg_idx);
  14391. if (!get_table_seg_elem_type(module, table_seg_idx,
  14392. NULL, NULL, error_buf,
  14393. error_buf_size))
  14394. goto fail;
  14395. #if WASM_ENABLE_FAST_INTERP != 0
  14396. emit_uint32(loader_ctx, table_seg_idx);
  14397. #endif
  14398. #if WASM_ENABLE_WAMR_COMPILER != 0
  14399. module->is_ref_types_used = true;
  14400. #endif
  14401. break;
  14402. }
  14403. case WASM_OP_TABLE_COPY:
  14404. {
  14405. uint8 src_type, dst_type, src_tbl_idx_type,
  14406. dst_tbl_idx_type, min_tbl_idx_type;
  14407. #if WASM_ENABLE_GC != 0
  14408. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  14409. #endif
  14410. uint32 src_tbl_idx, dst_tbl_idx;
  14411. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  14412. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  14413. #if WASM_ENABLE_GC != 0
  14414. (void **)&dst_ref_type,
  14415. #else
  14416. NULL,
  14417. #endif
  14418. error_buf, error_buf_size))
  14419. goto fail;
  14420. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  14421. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  14422. #if WASM_ENABLE_GC != 0
  14423. (void **)&src_ref_type,
  14424. #else
  14425. NULL,
  14426. #endif
  14427. error_buf, error_buf_size))
  14428. goto fail;
  14429. #if WASM_ENABLE_GC == 0
  14430. if (src_type != dst_type) {
  14431. set_error_buf(error_buf, error_buf_size,
  14432. "type mismatch");
  14433. goto fail;
  14434. }
  14435. #else
  14436. if (!wasm_reftype_is_subtype_of(
  14437. src_type, src_ref_type, dst_type, dst_ref_type,
  14438. module->types, module->type_count)) {
  14439. set_error_buf(error_buf, error_buf_size,
  14440. "type mismatch");
  14441. goto fail;
  14442. }
  14443. #endif
  14444. #if WASM_ENABLE_FAST_INTERP != 0
  14445. emit_uint32(loader_ctx, dst_tbl_idx);
  14446. emit_uint32(loader_ctx, src_tbl_idx);
  14447. #endif
  14448. #if WASM_ENABLE_MEMORY64 != 0
  14449. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  14450. ? VALUE_TYPE_I64
  14451. : VALUE_TYPE_I32;
  14452. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  14453. ? VALUE_TYPE_I64
  14454. : VALUE_TYPE_I32;
  14455. min_tbl_idx_type =
  14456. (src_tbl_idx_type == VALUE_TYPE_I32
  14457. || dst_tbl_idx_type == VALUE_TYPE_I32)
  14458. ? VALUE_TYPE_I32
  14459. : VALUE_TYPE_I64;
  14460. #else
  14461. src_tbl_idx_type = VALUE_TYPE_I32;
  14462. dst_tbl_idx_type = VALUE_TYPE_I32;
  14463. min_tbl_idx_type = VALUE_TYPE_I32;
  14464. #endif
  14465. table_elem_idx_type = min_tbl_idx_type;
  14466. POP_TBL_ELEM_IDX();
  14467. table_elem_idx_type = src_tbl_idx_type;
  14468. POP_TBL_ELEM_IDX();
  14469. table_elem_idx_type = dst_tbl_idx_type;
  14470. POP_TBL_ELEM_IDX();
  14471. #if WASM_ENABLE_WAMR_COMPILER != 0
  14472. module->is_ref_types_used = true;
  14473. #endif
  14474. break;
  14475. }
  14476. case WASM_OP_TABLE_SIZE:
  14477. {
  14478. pb_read_leb_uint32(p, p_end, table_idx);
  14479. /* TODO: shall we create a new function to check
  14480. table idx instead of using below function? */
  14481. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  14482. error_buf, error_buf_size))
  14483. goto fail;
  14484. #if WASM_ENABLE_FAST_INTERP != 0
  14485. emit_uint32(loader_ctx, table_idx);
  14486. #endif
  14487. #if WASM_ENABLE_MEMORY64 != 0
  14488. table_elem_idx_type = is_table_64bit(module, table_idx)
  14489. ? VALUE_TYPE_I64
  14490. : VALUE_TYPE_I32;
  14491. #endif
  14492. PUSH_TBL_ELEM_IDX();
  14493. #if WASM_ENABLE_WAMR_COMPILER != 0
  14494. module->is_ref_types_used = true;
  14495. #endif
  14496. break;
  14497. }
  14498. case WASM_OP_TABLE_GROW:
  14499. case WASM_OP_TABLE_FILL:
  14500. {
  14501. uint8 decl_type;
  14502. #if WASM_ENABLE_GC != 0
  14503. WASMRefType *ref_type = NULL;
  14504. #endif
  14505. pb_read_leb_uint32(p, p_end, table_idx);
  14506. if (!get_table_elem_type(module, table_idx, &decl_type,
  14507. #if WASM_ENABLE_GC != 0
  14508. (void **)&ref_type,
  14509. #else
  14510. NULL,
  14511. #endif
  14512. error_buf, error_buf_size))
  14513. goto fail;
  14514. #if WASM_ENABLE_GC != 0
  14515. if (wasm_is_type_multi_byte_type(decl_type)) {
  14516. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  14517. ref_type,
  14518. wasm_reftype_struct_size(ref_type));
  14519. }
  14520. #endif
  14521. if (opcode1 == WASM_OP_TABLE_GROW) {
  14522. if (table_idx < module->import_table_count) {
  14523. module->import_tables[table_idx]
  14524. .u.table.table_type.possible_grow = true;
  14525. }
  14526. else {
  14527. module
  14528. ->tables[table_idx
  14529. - module->import_table_count]
  14530. .table_type.possible_grow = true;
  14531. }
  14532. }
  14533. #if WASM_ENABLE_FAST_INTERP != 0
  14534. emit_uint32(loader_ctx, table_idx);
  14535. #endif
  14536. #if WASM_ENABLE_MEMORY64 != 0
  14537. table_elem_idx_type = is_table_64bit(module, table_idx)
  14538. ? VALUE_TYPE_I64
  14539. : VALUE_TYPE_I32;
  14540. #endif
  14541. POP_TBL_ELEM_IDX();
  14542. #if WASM_ENABLE_FAST_INTERP != 0
  14543. POP_OFFSET_TYPE(decl_type);
  14544. #endif
  14545. POP_TYPE(decl_type);
  14546. if (opcode1 == WASM_OP_TABLE_GROW)
  14547. PUSH_TBL_ELEM_IDX();
  14548. else
  14549. POP_TBL_ELEM_IDX();
  14550. #if WASM_ENABLE_WAMR_COMPILER != 0
  14551. module->is_ref_types_used = true;
  14552. #endif
  14553. break;
  14554. }
  14555. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14556. default:
  14557. set_error_buf_v(error_buf, error_buf_size,
  14558. "%s %02x %02x", "unsupported opcode",
  14559. 0xfc, opcode1);
  14560. goto fail;
  14561. }
  14562. break;
  14563. }
  14564. #if WASM_ENABLE_SIMD != 0
  14565. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14566. || (WASM_ENABLE_FAST_INTERP != 0)
  14567. case WASM_OP_SIMD_PREFIX:
  14568. {
  14569. uint32 opcode1;
  14570. #if WASM_ENABLE_WAMR_COMPILER != 0
  14571. /* Mark the SIMD instruction is used in this module */
  14572. module->is_simd_used = true;
  14573. #endif
  14574. pb_read_leb_uint32(p, p_end, opcode1);
  14575. #if WASM_ENABLE_FAST_INTERP != 0
  14576. emit_byte(loader_ctx, opcode1);
  14577. #endif
  14578. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14579. */
  14580. switch (opcode1) {
  14581. /* memory instruction */
  14582. case SIMD_v128_load:
  14583. case SIMD_v128_load8x8_s:
  14584. case SIMD_v128_load8x8_u:
  14585. case SIMD_v128_load16x4_s:
  14586. case SIMD_v128_load16x4_u:
  14587. case SIMD_v128_load32x2_s:
  14588. case SIMD_v128_load32x2_u:
  14589. case SIMD_v128_load8_splat:
  14590. case SIMD_v128_load16_splat:
  14591. case SIMD_v128_load32_splat:
  14592. case SIMD_v128_load64_splat:
  14593. {
  14594. CHECK_MEMORY();
  14595. pb_read_leb_uint32(p, p_end, align); /* align */
  14596. if (!check_simd_memory_access_align(
  14597. opcode1, align, error_buf, error_buf_size)) {
  14598. goto fail;
  14599. }
  14600. pb_read_leb_mem_offset(p, p_end,
  14601. mem_offset); /* offset */
  14602. #if WASM_ENABLE_FAST_INTERP != 0
  14603. emit_uint32(loader_ctx, mem_offset);
  14604. #endif
  14605. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14606. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14607. func->has_memory_operations = true;
  14608. #endif
  14609. break;
  14610. }
  14611. case SIMD_v128_store:
  14612. {
  14613. CHECK_MEMORY();
  14614. pb_read_leb_uint32(p, p_end, align); /* align */
  14615. if (!check_simd_memory_access_align(
  14616. opcode1, align, error_buf, error_buf_size)) {
  14617. goto fail;
  14618. }
  14619. pb_read_leb_mem_offset(p, p_end,
  14620. mem_offset); /* offset */
  14621. #if WASM_ENABLE_FAST_INTERP != 0
  14622. emit_uint32(loader_ctx, mem_offset);
  14623. #endif
  14624. POP_V128();
  14625. POP_MEM_OFFSET();
  14626. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14627. func->has_memory_operations = true;
  14628. #endif
  14629. break;
  14630. }
  14631. /* basic operation */
  14632. case SIMD_v128_const:
  14633. {
  14634. #if WASM_ENABLE_FAST_INTERP != 0
  14635. uint64 high, low;
  14636. #endif
  14637. CHECK_BUF1(p, p_end, 16);
  14638. #if WASM_ENABLE_FAST_INTERP != 0
  14639. wasm_runtime_read_v128(p, &high, &low);
  14640. emit_uint64(loader_ctx, high);
  14641. emit_uint64(loader_ctx, low);
  14642. #endif
  14643. p += 16;
  14644. PUSH_V128();
  14645. break;
  14646. }
  14647. case SIMD_v8x16_shuffle:
  14648. {
  14649. V128 mask;
  14650. CHECK_BUF1(p, p_end, 16);
  14651. mask = read_i8x16(p, error_buf, error_buf_size);
  14652. if (!check_simd_shuffle_mask(mask, error_buf,
  14653. error_buf_size)) {
  14654. goto fail;
  14655. }
  14656. #if WASM_ENABLE_FAST_INTERP != 0
  14657. uint64 high, low;
  14658. wasm_runtime_read_v128(p, &high, &low);
  14659. emit_uint64(loader_ctx, high);
  14660. emit_uint64(loader_ctx, low);
  14661. #endif
  14662. p += 16;
  14663. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14664. break;
  14665. }
  14666. case SIMD_v8x16_swizzle:
  14667. {
  14668. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14669. break;
  14670. }
  14671. /* splat operation */
  14672. case SIMD_i8x16_splat:
  14673. case SIMD_i16x8_splat:
  14674. case SIMD_i32x4_splat:
  14675. case SIMD_i64x2_splat:
  14676. case SIMD_f32x4_splat:
  14677. case SIMD_f64x2_splat:
  14678. {
  14679. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14680. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14681. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14682. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14683. VALUE_TYPE_V128);
  14684. break;
  14685. }
  14686. /* lane operation */
  14687. case SIMD_i8x16_extract_lane_s:
  14688. case SIMD_i8x16_extract_lane_u:
  14689. case SIMD_i8x16_replace_lane:
  14690. case SIMD_i16x8_extract_lane_s:
  14691. case SIMD_i16x8_extract_lane_u:
  14692. case SIMD_i16x8_replace_lane:
  14693. case SIMD_i32x4_extract_lane:
  14694. case SIMD_i32x4_replace_lane:
  14695. case SIMD_i64x2_extract_lane:
  14696. case SIMD_i64x2_replace_lane:
  14697. case SIMD_f32x4_extract_lane:
  14698. case SIMD_f32x4_replace_lane:
  14699. case SIMD_f64x2_extract_lane:
  14700. case SIMD_f64x2_replace_lane:
  14701. {
  14702. uint8 lane;
  14703. /* clang-format off */
  14704. uint8 replace[] = {
  14705. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14706. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14707. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14708. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14709. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14710. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14711. };
  14712. uint8 push_type[] = {
  14713. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14714. VALUE_TYPE_V128,
  14715. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14716. VALUE_TYPE_V128,
  14717. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14718. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14719. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14720. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14721. };
  14722. /* clang-format on */
  14723. CHECK_BUF(p, p_end, 1);
  14724. lane = read_uint8(p);
  14725. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14726. error_buf_size)) {
  14727. goto fail;
  14728. }
  14729. #if WASM_ENABLE_FAST_INTERP != 0
  14730. emit_byte(loader_ctx, lane);
  14731. #endif
  14732. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14733. #if WASM_ENABLE_FAST_INTERP != 0
  14734. if (!(wasm_loader_pop_frame_ref_offset(
  14735. loader_ctx,
  14736. replace[opcode1
  14737. - SIMD_i8x16_extract_lane_s],
  14738. error_buf, error_buf_size)))
  14739. goto fail;
  14740. #else
  14741. if (!(wasm_loader_pop_frame_ref(
  14742. loader_ctx,
  14743. replace[opcode1
  14744. - SIMD_i8x16_extract_lane_s],
  14745. error_buf, error_buf_size)))
  14746. goto fail;
  14747. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14748. }
  14749. POP_AND_PUSH(
  14750. VALUE_TYPE_V128,
  14751. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14752. break;
  14753. }
  14754. /* i8x16 compare operation */
  14755. case SIMD_i8x16_eq:
  14756. case SIMD_i8x16_ne:
  14757. case SIMD_i8x16_lt_s:
  14758. case SIMD_i8x16_lt_u:
  14759. case SIMD_i8x16_gt_s:
  14760. case SIMD_i8x16_gt_u:
  14761. case SIMD_i8x16_le_s:
  14762. case SIMD_i8x16_le_u:
  14763. case SIMD_i8x16_ge_s:
  14764. case SIMD_i8x16_ge_u:
  14765. /* i16x8 compare operation */
  14766. case SIMD_i16x8_eq:
  14767. case SIMD_i16x8_ne:
  14768. case SIMD_i16x8_lt_s:
  14769. case SIMD_i16x8_lt_u:
  14770. case SIMD_i16x8_gt_s:
  14771. case SIMD_i16x8_gt_u:
  14772. case SIMD_i16x8_le_s:
  14773. case SIMD_i16x8_le_u:
  14774. case SIMD_i16x8_ge_s:
  14775. case SIMD_i16x8_ge_u:
  14776. /* i32x4 compare operation */
  14777. case SIMD_i32x4_eq:
  14778. case SIMD_i32x4_ne:
  14779. case SIMD_i32x4_lt_s:
  14780. case SIMD_i32x4_lt_u:
  14781. case SIMD_i32x4_gt_s:
  14782. case SIMD_i32x4_gt_u:
  14783. case SIMD_i32x4_le_s:
  14784. case SIMD_i32x4_le_u:
  14785. case SIMD_i32x4_ge_s:
  14786. case SIMD_i32x4_ge_u:
  14787. /* f32x4 compare operation */
  14788. case SIMD_f32x4_eq:
  14789. case SIMD_f32x4_ne:
  14790. case SIMD_f32x4_lt:
  14791. case SIMD_f32x4_gt:
  14792. case SIMD_f32x4_le:
  14793. case SIMD_f32x4_ge:
  14794. /* f64x2 compare operation */
  14795. case SIMD_f64x2_eq:
  14796. case SIMD_f64x2_ne:
  14797. case SIMD_f64x2_lt:
  14798. case SIMD_f64x2_gt:
  14799. case SIMD_f64x2_le:
  14800. case SIMD_f64x2_ge:
  14801. {
  14802. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14803. break;
  14804. }
  14805. /* v128 operation */
  14806. case SIMD_v128_not:
  14807. {
  14808. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14809. break;
  14810. }
  14811. case SIMD_v128_and:
  14812. case SIMD_v128_andnot:
  14813. case SIMD_v128_or:
  14814. case SIMD_v128_xor:
  14815. {
  14816. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14817. break;
  14818. }
  14819. case SIMD_v128_bitselect:
  14820. {
  14821. POP_V128();
  14822. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14823. break;
  14824. }
  14825. case SIMD_v128_any_true:
  14826. {
  14827. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14828. break;
  14829. }
  14830. /* Load Lane Operation */
  14831. case SIMD_v128_load8_lane:
  14832. case SIMD_v128_load16_lane:
  14833. case SIMD_v128_load32_lane:
  14834. case SIMD_v128_load64_lane:
  14835. case SIMD_v128_store8_lane:
  14836. case SIMD_v128_store16_lane:
  14837. case SIMD_v128_store32_lane:
  14838. case SIMD_v128_store64_lane:
  14839. {
  14840. uint8 lane;
  14841. CHECK_MEMORY();
  14842. pb_read_leb_uint32(p, p_end, align); /* align */
  14843. if (!check_simd_memory_access_align(
  14844. opcode1, align, error_buf, error_buf_size)) {
  14845. goto fail;
  14846. }
  14847. pb_read_leb_mem_offset(p, p_end,
  14848. mem_offset); /* offset */
  14849. CHECK_BUF(p, p_end, 1);
  14850. lane = read_uint8(p);
  14851. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14852. error_buf_size)) {
  14853. goto fail;
  14854. }
  14855. #if WASM_ENABLE_FAST_INTERP != 0
  14856. emit_uint32(loader_ctx, mem_offset);
  14857. #endif
  14858. POP_V128();
  14859. POP_MEM_OFFSET();
  14860. #if WASM_ENABLE_FAST_INTERP != 0
  14861. emit_byte(loader_ctx, lane);
  14862. #endif
  14863. if (opcode1 < SIMD_v128_store8_lane) {
  14864. PUSH_V128();
  14865. }
  14866. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14867. func->has_memory_operations = true;
  14868. #endif
  14869. break;
  14870. }
  14871. case SIMD_v128_load32_zero:
  14872. case SIMD_v128_load64_zero:
  14873. {
  14874. CHECK_MEMORY();
  14875. pb_read_leb_uint32(p, p_end, align); /* align */
  14876. if (!check_simd_memory_access_align(
  14877. opcode1, align, error_buf, error_buf_size)) {
  14878. goto fail;
  14879. }
  14880. pb_read_leb_mem_offset(p, p_end,
  14881. mem_offset); /* offset */
  14882. #if WASM_ENABLE_FAST_INTERP != 0
  14883. emit_uint32(loader_ctx, mem_offset);
  14884. #endif
  14885. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14886. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14887. func->has_memory_operations = true;
  14888. #endif
  14889. break;
  14890. }
  14891. /* Float conversion */
  14892. case SIMD_f32x4_demote_f64x2_zero:
  14893. case SIMD_f64x2_promote_low_f32x4_zero:
  14894. {
  14895. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14896. break;
  14897. }
  14898. /* i8x16 Operation */
  14899. case SIMD_i8x16_abs:
  14900. case SIMD_i8x16_neg:
  14901. case SIMD_i8x16_popcnt:
  14902. {
  14903. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14904. break;
  14905. }
  14906. case SIMD_i8x16_all_true:
  14907. case SIMD_i8x16_bitmask:
  14908. {
  14909. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14910. break;
  14911. }
  14912. case SIMD_i8x16_narrow_i16x8_s:
  14913. case SIMD_i8x16_narrow_i16x8_u:
  14914. {
  14915. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14916. break;
  14917. }
  14918. case SIMD_f32x4_ceil:
  14919. case SIMD_f32x4_floor:
  14920. case SIMD_f32x4_trunc:
  14921. case SIMD_f32x4_nearest:
  14922. {
  14923. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14924. break;
  14925. }
  14926. case SIMD_i8x16_shl:
  14927. case SIMD_i8x16_shr_s:
  14928. case SIMD_i8x16_shr_u:
  14929. {
  14930. POP_I32();
  14931. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14932. break;
  14933. }
  14934. case SIMD_i8x16_add:
  14935. case SIMD_i8x16_add_sat_s:
  14936. case SIMD_i8x16_add_sat_u:
  14937. case SIMD_i8x16_sub:
  14938. case SIMD_i8x16_sub_sat_s:
  14939. case SIMD_i8x16_sub_sat_u:
  14940. {
  14941. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14942. break;
  14943. }
  14944. case SIMD_f64x2_ceil:
  14945. case SIMD_f64x2_floor:
  14946. {
  14947. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14948. break;
  14949. }
  14950. case SIMD_i8x16_min_s:
  14951. case SIMD_i8x16_min_u:
  14952. case SIMD_i8x16_max_s:
  14953. case SIMD_i8x16_max_u:
  14954. {
  14955. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14956. break;
  14957. }
  14958. case SIMD_f64x2_trunc:
  14959. {
  14960. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14961. break;
  14962. }
  14963. case SIMD_i8x16_avgr_u:
  14964. {
  14965. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14966. break;
  14967. }
  14968. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14969. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14970. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14971. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14972. /* i16x8 operation */
  14973. case SIMD_i16x8_abs:
  14974. case SIMD_i16x8_neg:
  14975. {
  14976. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14977. break;
  14978. }
  14979. case SIMD_i16x8_q15mulr_sat_s:
  14980. {
  14981. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14982. break;
  14983. }
  14984. case SIMD_i16x8_all_true:
  14985. case SIMD_i16x8_bitmask:
  14986. {
  14987. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14988. break;
  14989. }
  14990. case SIMD_i16x8_narrow_i32x4_s:
  14991. case SIMD_i16x8_narrow_i32x4_u:
  14992. {
  14993. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14994. break;
  14995. }
  14996. case SIMD_i16x8_extend_low_i8x16_s:
  14997. case SIMD_i16x8_extend_high_i8x16_s:
  14998. case SIMD_i16x8_extend_low_i8x16_u:
  14999. case SIMD_i16x8_extend_high_i8x16_u:
  15000. {
  15001. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15002. break;
  15003. }
  15004. case SIMD_i16x8_shl:
  15005. case SIMD_i16x8_shr_s:
  15006. case SIMD_i16x8_shr_u:
  15007. {
  15008. POP_I32();
  15009. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15010. break;
  15011. }
  15012. case SIMD_i16x8_add:
  15013. case SIMD_i16x8_add_sat_s:
  15014. case SIMD_i16x8_add_sat_u:
  15015. case SIMD_i16x8_sub:
  15016. case SIMD_i16x8_sub_sat_s:
  15017. case SIMD_i16x8_sub_sat_u:
  15018. {
  15019. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15020. break;
  15021. }
  15022. case SIMD_f64x2_nearest:
  15023. {
  15024. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15025. break;
  15026. }
  15027. case SIMD_i16x8_mul:
  15028. case SIMD_i16x8_min_s:
  15029. case SIMD_i16x8_min_u:
  15030. case SIMD_i16x8_max_s:
  15031. case SIMD_i16x8_max_u:
  15032. case SIMD_i16x8_avgr_u:
  15033. case SIMD_i16x8_extmul_low_i8x16_s:
  15034. case SIMD_i16x8_extmul_high_i8x16_s:
  15035. case SIMD_i16x8_extmul_low_i8x16_u:
  15036. case SIMD_i16x8_extmul_high_i8x16_u:
  15037. {
  15038. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15039. break;
  15040. }
  15041. /* i32x4 operation */
  15042. case SIMD_i32x4_abs:
  15043. case SIMD_i32x4_neg:
  15044. {
  15045. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15046. break;
  15047. }
  15048. case SIMD_i32x4_all_true:
  15049. case SIMD_i32x4_bitmask:
  15050. {
  15051. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  15052. break;
  15053. }
  15054. case SIMD_i32x4_extend_low_i16x8_s:
  15055. case SIMD_i32x4_extend_high_i16x8_s:
  15056. case SIMD_i32x4_extend_low_i16x8_u:
  15057. case SIMD_i32x4_extend_high_i16x8_u:
  15058. {
  15059. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15060. break;
  15061. }
  15062. case SIMD_i32x4_shl:
  15063. case SIMD_i32x4_shr_s:
  15064. case SIMD_i32x4_shr_u:
  15065. {
  15066. POP_I32();
  15067. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15068. break;
  15069. }
  15070. case SIMD_i32x4_add:
  15071. case SIMD_i32x4_sub:
  15072. case SIMD_i32x4_mul:
  15073. case SIMD_i32x4_min_s:
  15074. case SIMD_i32x4_min_u:
  15075. case SIMD_i32x4_max_s:
  15076. case SIMD_i32x4_max_u:
  15077. case SIMD_i32x4_dot_i16x8_s:
  15078. case SIMD_i32x4_extmul_low_i16x8_s:
  15079. case SIMD_i32x4_extmul_high_i16x8_s:
  15080. case SIMD_i32x4_extmul_low_i16x8_u:
  15081. case SIMD_i32x4_extmul_high_i16x8_u:
  15082. {
  15083. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15084. break;
  15085. }
  15086. /* i64x2 operation */
  15087. case SIMD_i64x2_abs:
  15088. case SIMD_i64x2_neg:
  15089. {
  15090. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15091. break;
  15092. }
  15093. case SIMD_i64x2_all_true:
  15094. case SIMD_i64x2_bitmask:
  15095. {
  15096. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  15097. break;
  15098. }
  15099. case SIMD_i64x2_extend_low_i32x4_s:
  15100. case SIMD_i64x2_extend_high_i32x4_s:
  15101. case SIMD_i64x2_extend_low_i32x4_u:
  15102. case SIMD_i64x2_extend_high_i32x4_u:
  15103. {
  15104. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15105. break;
  15106. }
  15107. case SIMD_i64x2_shl:
  15108. case SIMD_i64x2_shr_s:
  15109. case SIMD_i64x2_shr_u:
  15110. {
  15111. POP_I32();
  15112. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15113. break;
  15114. }
  15115. case SIMD_i64x2_add:
  15116. case SIMD_i64x2_sub:
  15117. case SIMD_i64x2_mul:
  15118. case SIMD_i64x2_eq:
  15119. case SIMD_i64x2_ne:
  15120. case SIMD_i64x2_lt_s:
  15121. case SIMD_i64x2_gt_s:
  15122. case SIMD_i64x2_le_s:
  15123. case SIMD_i64x2_ge_s:
  15124. case SIMD_i64x2_extmul_low_i32x4_s:
  15125. case SIMD_i64x2_extmul_high_i32x4_s:
  15126. case SIMD_i64x2_extmul_low_i32x4_u:
  15127. case SIMD_i64x2_extmul_high_i32x4_u:
  15128. {
  15129. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15130. break;
  15131. }
  15132. /* f32x4 operation */
  15133. case SIMD_f32x4_abs:
  15134. case SIMD_f32x4_neg:
  15135. case SIMD_f32x4_sqrt:
  15136. {
  15137. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15138. break;
  15139. }
  15140. case SIMD_f32x4_add:
  15141. case SIMD_f32x4_sub:
  15142. case SIMD_f32x4_mul:
  15143. case SIMD_f32x4_div:
  15144. case SIMD_f32x4_min:
  15145. case SIMD_f32x4_max:
  15146. case SIMD_f32x4_pmin:
  15147. case SIMD_f32x4_pmax:
  15148. {
  15149. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15150. break;
  15151. }
  15152. /* f64x2 operation */
  15153. case SIMD_f64x2_abs:
  15154. case SIMD_f64x2_neg:
  15155. case SIMD_f64x2_sqrt:
  15156. {
  15157. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15158. break;
  15159. }
  15160. case SIMD_f64x2_add:
  15161. case SIMD_f64x2_sub:
  15162. case SIMD_f64x2_mul:
  15163. case SIMD_f64x2_div:
  15164. case SIMD_f64x2_min:
  15165. case SIMD_f64x2_max:
  15166. case SIMD_f64x2_pmin:
  15167. case SIMD_f64x2_pmax:
  15168. {
  15169. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15170. break;
  15171. }
  15172. case SIMD_i32x4_trunc_sat_f32x4_s:
  15173. case SIMD_i32x4_trunc_sat_f32x4_u:
  15174. case SIMD_f32x4_convert_i32x4_s:
  15175. case SIMD_f32x4_convert_i32x4_u:
  15176. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  15177. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  15178. case SIMD_f64x2_convert_low_i32x4_s:
  15179. case SIMD_f64x2_convert_low_i32x4_u:
  15180. {
  15181. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  15182. break;
  15183. }
  15184. default:
  15185. {
  15186. if (error_buf != NULL) {
  15187. snprintf(error_buf, error_buf_size,
  15188. "WASM module load failed: "
  15189. "invalid opcode 0xfd %02x.",
  15190. opcode1);
  15191. }
  15192. goto fail;
  15193. }
  15194. }
  15195. break;
  15196. }
  15197. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  15198. (WASM_ENABLE_FAST_INTERP != 0) */
  15199. #endif /* end of WASM_ENABLE_SIMD */
  15200. #if WASM_ENABLE_SHARED_MEMORY != 0
  15201. case WASM_OP_ATOMIC_PREFIX:
  15202. {
  15203. uint32 opcode1;
  15204. pb_read_leb_uint32(p, p_end, opcode1);
  15205. #if WASM_ENABLE_FAST_INTERP != 0
  15206. emit_byte(loader_ctx, opcode1);
  15207. #endif
  15208. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  15209. CHECK_MEMORY();
  15210. pb_read_leb_uint32(p, p_end, align); /* align */
  15211. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  15212. if (!check_memory_align_equal(opcode1, align, error_buf,
  15213. error_buf_size)) {
  15214. goto fail;
  15215. }
  15216. #if WASM_ENABLE_FAST_INTERP != 0
  15217. emit_uint32(loader_ctx, mem_offset);
  15218. #endif
  15219. }
  15220. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  15221. func->has_memory_operations = true;
  15222. #endif
  15223. switch (opcode1) {
  15224. case WASM_OP_ATOMIC_NOTIFY:
  15225. POP_I32();
  15226. POP_MEM_OFFSET();
  15227. PUSH_I32();
  15228. break;
  15229. case WASM_OP_ATOMIC_WAIT32:
  15230. POP_I64();
  15231. POP_I32();
  15232. POP_MEM_OFFSET();
  15233. PUSH_I32();
  15234. break;
  15235. case WASM_OP_ATOMIC_WAIT64:
  15236. POP_I64();
  15237. POP_I64();
  15238. POP_MEM_OFFSET();
  15239. PUSH_I32();
  15240. break;
  15241. case WASM_OP_ATOMIC_FENCE:
  15242. /* reserved byte 0x00 */
  15243. if (*p++ != 0x00) {
  15244. set_error_buf(error_buf, error_buf_size,
  15245. "zero byte expected");
  15246. goto fail;
  15247. }
  15248. break;
  15249. case WASM_OP_ATOMIC_I32_LOAD:
  15250. case WASM_OP_ATOMIC_I32_LOAD8_U:
  15251. case WASM_OP_ATOMIC_I32_LOAD16_U:
  15252. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  15253. break;
  15254. case WASM_OP_ATOMIC_I32_STORE:
  15255. case WASM_OP_ATOMIC_I32_STORE8:
  15256. case WASM_OP_ATOMIC_I32_STORE16:
  15257. POP_I32();
  15258. POP_MEM_OFFSET();
  15259. break;
  15260. case WASM_OP_ATOMIC_I64_LOAD:
  15261. case WASM_OP_ATOMIC_I64_LOAD8_U:
  15262. case WASM_OP_ATOMIC_I64_LOAD16_U:
  15263. case WASM_OP_ATOMIC_I64_LOAD32_U:
  15264. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  15265. break;
  15266. case WASM_OP_ATOMIC_I64_STORE:
  15267. case WASM_OP_ATOMIC_I64_STORE8:
  15268. case WASM_OP_ATOMIC_I64_STORE16:
  15269. case WASM_OP_ATOMIC_I64_STORE32:
  15270. POP_I64();
  15271. POP_MEM_OFFSET();
  15272. break;
  15273. case WASM_OP_ATOMIC_RMW_I32_ADD:
  15274. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  15275. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  15276. case WASM_OP_ATOMIC_RMW_I32_SUB:
  15277. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  15278. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  15279. case WASM_OP_ATOMIC_RMW_I32_AND:
  15280. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  15281. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  15282. case WASM_OP_ATOMIC_RMW_I32_OR:
  15283. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  15284. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  15285. case WASM_OP_ATOMIC_RMW_I32_XOR:
  15286. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  15287. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  15288. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  15289. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  15290. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  15291. POP_I32();
  15292. POP_MEM_OFFSET();
  15293. PUSH_I32();
  15294. break;
  15295. case WASM_OP_ATOMIC_RMW_I64_ADD:
  15296. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  15297. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  15298. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  15299. case WASM_OP_ATOMIC_RMW_I64_SUB:
  15300. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  15301. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  15302. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  15303. case WASM_OP_ATOMIC_RMW_I64_AND:
  15304. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  15305. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  15306. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  15307. case WASM_OP_ATOMIC_RMW_I64_OR:
  15308. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  15309. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  15310. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  15311. case WASM_OP_ATOMIC_RMW_I64_XOR:
  15312. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  15313. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  15314. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  15315. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  15316. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  15317. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  15318. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  15319. POP_I64();
  15320. POP_MEM_OFFSET();
  15321. PUSH_I64();
  15322. break;
  15323. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  15324. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  15325. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  15326. POP_I32();
  15327. POP_I32();
  15328. POP_MEM_OFFSET();
  15329. PUSH_I32();
  15330. break;
  15331. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  15332. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  15333. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  15334. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  15335. POP_I64();
  15336. POP_I64();
  15337. POP_MEM_OFFSET();
  15338. PUSH_I64();
  15339. break;
  15340. default:
  15341. set_error_buf_v(error_buf, error_buf_size,
  15342. "%s %02x %02x", "unsupported opcode",
  15343. 0xfe, opcode1);
  15344. goto fail;
  15345. }
  15346. break;
  15347. }
  15348. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  15349. default:
  15350. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  15351. "unsupported opcode", opcode);
  15352. goto fail;
  15353. }
  15354. #if WASM_ENABLE_FAST_INTERP != 0
  15355. last_op = opcode;
  15356. #endif
  15357. }
  15358. if (loader_ctx->csp_num > 0) {
  15359. /* unmatched end opcodes result from unbalanced control flow structures,
  15360. * for example, br_table with inconsistent target count (1 declared, 2
  15361. * given), or simply superfluous end opcodes */
  15362. set_error_buf(
  15363. error_buf, error_buf_size,
  15364. "unexpected end opcodes from unbalanced control flow structures");
  15365. goto fail;
  15366. }
  15367. #if WASM_ENABLE_FAST_INTERP != 0
  15368. if (loader_ctx->p_code_compiled == NULL)
  15369. goto re_scan;
  15370. func->const_cell_num = loader_ctx->i64_const_num * 2
  15371. + loader_ctx->v128_const_num * 4
  15372. + loader_ctx->i32_const_num;
  15373. if (func->const_cell_num > 0) {
  15374. if (!(func->consts =
  15375. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  15376. error_buf, error_buf_size)))
  15377. goto fail;
  15378. if (loader_ctx->i64_const_num > 0) {
  15379. bh_memcpy_s(func->consts,
  15380. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  15381. loader_ctx->i64_consts,
  15382. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  15383. }
  15384. if (loader_ctx->i32_const_num > 0) {
  15385. bh_memcpy_s(func->consts
  15386. + sizeof(int64) * loader_ctx->i64_const_num,
  15387. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  15388. loader_ctx->i32_consts,
  15389. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  15390. }
  15391. if (loader_ctx->v128_const_num > 0) {
  15392. bh_memcpy_s(func->consts,
  15393. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  15394. loader_ctx->v128_consts,
  15395. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  15396. }
  15397. }
  15398. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  15399. - loader_ctx->start_dynamic_offset + 1;
  15400. #else
  15401. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  15402. #endif
  15403. func->max_block_num = loader_ctx->max_csp_num;
  15404. return_value = true;
  15405. fail:
  15406. wasm_loader_ctx_destroy(loader_ctx);
  15407. (void)table_idx;
  15408. (void)table_seg_idx;
  15409. (void)data_seg_idx;
  15410. (void)i64_const;
  15411. (void)local_offset;
  15412. (void)p_org;
  15413. (void)mem_offset;
  15414. (void)align;
  15415. return return_value;
  15416. }