wasm_loader.c 610 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485
  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. #if WASM_ENABLE_GC != 0
  344. static bool
  345. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  346. char *error_buf, uint32 error_buf_size)
  347. {
  348. if (type_index >= type_count) {
  349. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  350. type_index);
  351. return false;
  352. }
  353. return true;
  354. }
  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. static bool
  371. check_function_index(const WASMModule *module, uint32 function_index,
  372. char *error_buf, uint32 error_buf_size)
  373. {
  374. if (function_index
  375. >= module->import_function_count + module->function_count) {
  376. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  377. function_index);
  378. return false;
  379. }
  380. return true;
  381. }
  382. typedef struct InitValue {
  383. uint8 type;
  384. uint8 flag;
  385. #if WASM_ENABLE_GC != 0
  386. uint8 gc_opcode;
  387. WASMRefType ref_type;
  388. #endif
  389. WASMValue value;
  390. } InitValue;
  391. typedef struct ConstExprContext {
  392. uint32 sp;
  393. uint32 size;
  394. WASMModule *module;
  395. InitValue *stack;
  396. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  397. } ConstExprContext;
  398. static void
  399. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  400. {
  401. ctx->sp = 0;
  402. ctx->module = module;
  403. ctx->stack = ctx->data;
  404. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  405. }
  406. static bool
  407. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  408. #if WASM_ENABLE_GC != 0
  409. WASMRefType *ref_type, uint8 gc_opcode,
  410. #endif
  411. WASMValue *value, char *error_buf, uint32 error_buf_size)
  412. {
  413. InitValue *cur_value;
  414. if (ctx->sp >= ctx->size) {
  415. if (ctx->stack != ctx->data) {
  416. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  417. (ctx->size + 4) * sizeof(InitValue));
  418. }
  419. else {
  420. if (!(ctx->stack =
  421. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  422. error_buf, error_buf_size))) {
  423. goto fail;
  424. }
  425. bh_memcpy_s(ctx->stack, (ctx->size + 4) * (uint32)sizeof(InitValue),
  426. ctx->data, ctx->size * (uint32)sizeof(InitValue));
  427. }
  428. ctx->size += 4;
  429. }
  430. cur_value = &ctx->stack[ctx->sp++];
  431. cur_value->type = type;
  432. cur_value->flag = flag;
  433. cur_value->value = *value;
  434. #if WASM_ENABLE_GC != 0
  435. cur_value->gc_opcode = gc_opcode;
  436. if (wasm_is_type_multi_byte_type(type)) {
  437. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  438. ref_type, wasm_reftype_struct_size(ref_type));
  439. }
  440. #endif
  441. return true;
  442. fail:
  443. return false;
  444. }
  445. #if WASM_ENABLE_GC != 0
  446. static void
  447. destroy_init_expr_data_recursive(WASMModule *module, void *data)
  448. {
  449. WASMStructNewInitValues *struct_init_values =
  450. (WASMStructNewInitValues *)data;
  451. WASMArrayNewInitValues *array_init_values = (WASMArrayNewInitValues *)data;
  452. WASMType *wasm_type;
  453. uint32 i;
  454. if (!data)
  455. return;
  456. wasm_type = module->types[struct_init_values->type_idx];
  457. /* The data can only be type of `WASMStructNewInitValues *`
  458. or `WASMArrayNewInitValues *` */
  459. bh_assert(wasm_type->type_flag == WASM_TYPE_STRUCT
  460. || wasm_type->type_flag == WASM_TYPE_ARRAY);
  461. if (wasm_type->type_flag == WASM_TYPE_STRUCT) {
  462. WASMStructType *struct_type = (WASMStructType *)wasm_type;
  463. WASMRefType *ref_type;
  464. uint8 field_type;
  465. uint16 ref_type_map_index = 0;
  466. for (i = 0; i < struct_init_values->count; i++) {
  467. field_type = struct_type->fields[i].field_type;
  468. if (wasm_is_type_multi_byte_type(field_type))
  469. ref_type =
  470. struct_type->ref_type_maps[ref_type_map_index++].ref_type;
  471. else
  472. ref_type = NULL;
  473. if (wasm_reftype_is_subtype_of(field_type, ref_type,
  474. REF_TYPE_STRUCTREF, NULL,
  475. module->types, module->type_count)
  476. || wasm_reftype_is_subtype_of(
  477. field_type, ref_type, REF_TYPE_ARRAYREF, NULL,
  478. module->types, module->type_count)) {
  479. destroy_init_expr_data_recursive(
  480. module, struct_init_values->fields[i].data);
  481. }
  482. }
  483. }
  484. else if (wasm_type->type_flag == WASM_TYPE_ARRAY) {
  485. WASMArrayType *array_type = (WASMArrayType *)wasm_type;
  486. WASMRefType *elem_ref_type = array_type->elem_ref_type;
  487. uint8 elem_type = array_type->elem_type;
  488. for (i = 0; i < array_init_values->length; i++) {
  489. if (wasm_reftype_is_subtype_of(elem_type, elem_ref_type,
  490. REF_TYPE_STRUCTREF, NULL,
  491. module->types, module->type_count)
  492. || wasm_reftype_is_subtype_of(
  493. elem_type, elem_ref_type, REF_TYPE_ARRAYREF, NULL,
  494. module->types, module->type_count)) {
  495. destroy_init_expr_data_recursive(
  496. module, array_init_values->elem_data[i].data);
  497. }
  498. }
  499. }
  500. wasm_runtime_free(data);
  501. }
  502. #endif
  503. static bool
  504. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  505. #if WASM_ENABLE_GC != 0
  506. WASMRefType *ref_type, uint8 *p_gc_opcode,
  507. #endif
  508. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  509. {
  510. InitValue *cur_value;
  511. if (ctx->sp == 0) {
  512. set_error_buf(error_buf, error_buf_size,
  513. "type mismatch: const expr stack underflow");
  514. return false;
  515. }
  516. cur_value = &ctx->stack[--ctx->sp];
  517. #if WASM_ENABLE_GC == 0
  518. if (cur_value->type != type) {
  519. set_error_buf(error_buf, error_buf_size, "type mismatch");
  520. return false;
  521. }
  522. #else
  523. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  524. ref_type, ctx->module->types,
  525. ctx->module->type_count)) {
  526. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  527. "type mismatch: expect ", type2str(type),
  528. " but got other");
  529. goto fail;
  530. }
  531. #endif
  532. if (p_flag)
  533. *p_flag = cur_value->flag;
  534. if (p_value)
  535. *p_value = cur_value->value;
  536. #if WASM_ENABLE_GC != 0
  537. if (p_gc_opcode)
  538. *p_gc_opcode = cur_value->gc_opcode;
  539. #endif
  540. return true;
  541. #if WASM_ENABLE_GC != 0
  542. fail:
  543. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  544. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  545. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  546. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  547. destroy_init_expr_data_recursive(ctx->module, cur_value->value.data);
  548. }
  549. return false;
  550. #endif
  551. }
  552. static void
  553. destroy_const_expr_stack(ConstExprContext *ctx)
  554. {
  555. #if WASM_ENABLE_GC != 0
  556. uint32 i;
  557. for (i = 0; i < ctx->sp; i++) {
  558. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  559. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  560. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  561. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  562. destroy_init_expr_data_recursive(ctx->module,
  563. ctx->stack[i].value.data);
  564. }
  565. }
  566. #endif
  567. if (ctx->stack != ctx->data) {
  568. wasm_runtime_free(ctx->stack);
  569. }
  570. }
  571. #if WASM_ENABLE_GC != 0
  572. static void
  573. destroy_init_expr(WASMModule *module, InitializerExpression *expr)
  574. {
  575. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  576. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  577. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  578. destroy_init_expr_data_recursive(module, expr->u.data);
  579. }
  580. }
  581. #endif /* end of WASM_ENABLE_GC != 0 */
  582. static bool
  583. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  584. InitializerExpression *init_expr, uint8 type, void *ref_type,
  585. char *error_buf, uint32 error_buf_size)
  586. {
  587. const uint8 *p = *p_buf, *p_end = buf_end;
  588. uint8 flag, *p_float;
  589. uint32 i;
  590. ConstExprContext const_expr_ctx = { 0 };
  591. WASMValue cur_value;
  592. #if WASM_ENABLE_GC != 0
  593. uint32 opcode1, type_idx;
  594. uint8 opcode;
  595. WASMRefType cur_ref_type = { 0 };
  596. #endif
  597. init_const_expr_stack(&const_expr_ctx, module);
  598. CHECK_BUF(p, p_end, 1);
  599. flag = read_uint8(p);
  600. while (flag != WASM_OP_END) {
  601. switch (flag) {
  602. /* i32.const */
  603. case INIT_EXPR_TYPE_I32_CONST:
  604. read_leb_int32(p, p_end, cur_value.i32);
  605. if (!push_const_expr_stack(
  606. &const_expr_ctx, flag, VALUE_TYPE_I32,
  607. #if WASM_ENABLE_GC != 0
  608. NULL, 0,
  609. #endif
  610. &cur_value, error_buf, error_buf_size))
  611. goto fail;
  612. break;
  613. /* i64.const */
  614. case INIT_EXPR_TYPE_I64_CONST:
  615. read_leb_int64(p, p_end, cur_value.i64);
  616. if (!push_const_expr_stack(
  617. &const_expr_ctx, flag, VALUE_TYPE_I64,
  618. #if WASM_ENABLE_GC != 0
  619. NULL, 0,
  620. #endif
  621. &cur_value, error_buf, error_buf_size))
  622. goto fail;
  623. break;
  624. /* f32.const */
  625. case INIT_EXPR_TYPE_F32_CONST:
  626. CHECK_BUF(p, p_end, 4);
  627. p_float = (uint8 *)&cur_value.f32;
  628. for (i = 0; i < sizeof(float32); i++)
  629. *p_float++ = *p++;
  630. if (!push_const_expr_stack(
  631. &const_expr_ctx, flag, VALUE_TYPE_F32,
  632. #if WASM_ENABLE_GC != 0
  633. NULL, 0,
  634. #endif
  635. &cur_value, error_buf, error_buf_size))
  636. goto fail;
  637. break;
  638. /* f64.const */
  639. case INIT_EXPR_TYPE_F64_CONST:
  640. CHECK_BUF(p, p_end, 8);
  641. p_float = (uint8 *)&cur_value.f64;
  642. for (i = 0; i < sizeof(float64); i++)
  643. *p_float++ = *p++;
  644. if (!push_const_expr_stack(
  645. &const_expr_ctx, flag, VALUE_TYPE_F64,
  646. #if WASM_ENABLE_GC != 0
  647. NULL, 0,
  648. #endif
  649. &cur_value, error_buf, error_buf_size))
  650. goto fail;
  651. break;
  652. #if WASM_ENABLE_SIMD != 0
  653. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  654. || (WASM_ENABLE_FAST_INTERP != 0)
  655. /* v128.const */
  656. case INIT_EXPR_TYPE_V128_CONST:
  657. {
  658. uint64 high, low;
  659. CHECK_BUF(p, p_end, 1);
  660. (void)read_uint8(p);
  661. CHECK_BUF(p, p_end, 16);
  662. wasm_runtime_read_v128(p, &high, &low);
  663. p += 16;
  664. cur_value.v128.i64x2[0] = high;
  665. cur_value.v128.i64x2[1] = low;
  666. if (!push_const_expr_stack(
  667. &const_expr_ctx, flag, VALUE_TYPE_V128,
  668. #if WASM_ENABLE_GC != 0
  669. NULL, 0,
  670. #endif
  671. &cur_value, error_buf, error_buf_size))
  672. goto fail;
  673. #if WASM_ENABLE_WAMR_COMPILER != 0
  674. /* If any init_expr is v128.const, mark SIMD used */
  675. module->is_simd_used = true;
  676. #endif
  677. break;
  678. }
  679. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  680. (WASM_ENABLE_FAST_INTERP != 0) */
  681. #endif /* end of WASM_ENABLE_SIMD */
  682. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  683. /* ref.func */
  684. case INIT_EXPR_TYPE_FUNCREF_CONST:
  685. {
  686. uint32 func_idx;
  687. read_leb_uint32(p, p_end, func_idx);
  688. cur_value.ref_index = func_idx;
  689. if (!check_function_index(module, func_idx, error_buf,
  690. error_buf_size)) {
  691. goto fail;
  692. }
  693. #if WASM_ENABLE_GC == 0
  694. if (!push_const_expr_stack(&const_expr_ctx, flag,
  695. VALUE_TYPE_FUNCREF, &cur_value,
  696. error_buf, error_buf_size))
  697. goto fail;
  698. #else
  699. if (func_idx < module->import_function_count) {
  700. type_idx =
  701. module->import_functions[func_idx].u.function.type_idx;
  702. }
  703. else {
  704. type_idx = module
  705. ->functions[func_idx
  706. - module->import_function_count]
  707. ->type_idx;
  708. }
  709. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  710. false, type_idx);
  711. if (!push_const_expr_stack(&const_expr_ctx, flag,
  712. cur_ref_type.ref_type, &cur_ref_type,
  713. 0, &cur_value, error_buf,
  714. error_buf_size))
  715. goto fail;
  716. #endif
  717. #if WASM_ENABLE_WAMR_COMPILER != 0
  718. module->is_ref_types_used = true;
  719. #endif
  720. break;
  721. }
  722. /* ref.null */
  723. case INIT_EXPR_TYPE_REFNULL_CONST:
  724. {
  725. uint8 type1;
  726. CHECK_BUF(p, p_end, 1);
  727. type1 = read_uint8(p);
  728. #if WASM_ENABLE_GC == 0
  729. cur_value.ref_index = NULL_REF;
  730. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  731. &cur_value, error_buf,
  732. error_buf_size))
  733. goto fail;
  734. #else
  735. cur_value.gc_obj = NULL_REF;
  736. if (!is_byte_a_type(type1)) {
  737. p--;
  738. read_leb_uint32(p, p_end, type_idx);
  739. if (!check_type_index(module, module->type_count, type_idx,
  740. error_buf, error_buf_size))
  741. goto fail;
  742. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  743. true, type_idx);
  744. if (!push_const_expr_stack(&const_expr_ctx, flag,
  745. cur_ref_type.ref_type,
  746. &cur_ref_type, 0, &cur_value,
  747. error_buf, error_buf_size))
  748. goto fail;
  749. }
  750. else {
  751. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  752. NULL, 0, &cur_value, error_buf,
  753. error_buf_size))
  754. goto fail;
  755. }
  756. #endif
  757. #if WASM_ENABLE_WAMR_COMPILER != 0
  758. module->is_ref_types_used = true;
  759. #endif
  760. break;
  761. }
  762. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  763. /* get_global */
  764. case INIT_EXPR_TYPE_GET_GLOBAL:
  765. {
  766. uint32 global_idx;
  767. uint8 global_type;
  768. read_leb_uint32(p, p_end, cur_value.global_index);
  769. global_idx = cur_value.global_index;
  770. /*
  771. * Currently, constant expressions occurring as initializers
  772. * of globals are further constrained in that contained
  773. * global.get instructions are
  774. * only allowed to refer to imported globals.
  775. *
  776. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  777. */
  778. if (global_idx >= module->import_global_count
  779. /* make spec test happy */
  780. #if WASM_ENABLE_GC != 0
  781. + module->global_count
  782. #endif
  783. ) {
  784. set_error_buf_v(error_buf, error_buf_size,
  785. "unknown global %u", global_idx);
  786. goto fail;
  787. }
  788. if (
  789. /* make spec test happy */
  790. #if WASM_ENABLE_GC != 0
  791. global_idx < module->import_global_count &&
  792. #endif
  793. module->import_globals[global_idx]
  794. .u.global.type.is_mutable) {
  795. set_error_buf_v(error_buf, error_buf_size,
  796. "constant expression required");
  797. goto fail;
  798. }
  799. if (global_idx < module->import_global_count) {
  800. global_type = module->import_globals[global_idx]
  801. .u.global.type.val_type;
  802. #if WASM_ENABLE_GC != 0
  803. if (wasm_is_type_multi_byte_type(global_type)) {
  804. WASMRefType *global_ref_type =
  805. module->import_globals[global_idx]
  806. .u.global.ref_type;
  807. bh_memcpy_s(&cur_ref_type,
  808. wasm_reftype_struct_size(global_ref_type),
  809. global_ref_type,
  810. wasm_reftype_struct_size(global_ref_type));
  811. }
  812. #endif
  813. }
  814. else {
  815. global_type =
  816. module
  817. ->globals[global_idx - module->import_global_count]
  818. .type.val_type;
  819. #if WASM_ENABLE_GC != 0
  820. if (wasm_is_type_multi_byte_type(global_type)) {
  821. WASMRefType *global_ref_type =
  822. module
  823. ->globals[global_idx
  824. - module->import_global_count]
  825. .ref_type;
  826. bh_memcpy_s(&cur_ref_type,
  827. wasm_reftype_struct_size(global_ref_type),
  828. global_ref_type,
  829. wasm_reftype_struct_size(global_ref_type));
  830. }
  831. #endif
  832. }
  833. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  834. #if WASM_ENABLE_GC != 0
  835. &cur_ref_type, 0,
  836. #endif
  837. &cur_value, error_buf,
  838. error_buf_size))
  839. goto fail;
  840. break;
  841. }
  842. #if WASM_ENABLE_GC != 0
  843. /* struct.new and array.new */
  844. case WASM_OP_GC_PREFIX:
  845. {
  846. read_leb_uint32(p, p_end, opcode1);
  847. switch (opcode1) {
  848. case WASM_OP_STRUCT_NEW:
  849. {
  850. WASMStructType *struct_type;
  851. WASMStructNewInitValues *struct_init_values = NULL;
  852. uint32 field_count;
  853. read_leb_uint32(p, p_end, type_idx);
  854. if (!check_type_index(module, module->type_count,
  855. type_idx, error_buf,
  856. error_buf_size)) {
  857. goto fail;
  858. }
  859. struct_type = (WASMStructType *)module->types[type_idx];
  860. if (struct_type->base_type.type_flag
  861. != WASM_TYPE_STRUCT) {
  862. set_error_buf(error_buf, error_buf_size,
  863. "unknown struct type");
  864. goto fail;
  865. }
  866. field_count = struct_type->field_count;
  867. if (!(struct_init_values = loader_malloc(
  868. offsetof(WASMStructNewInitValues, fields)
  869. + (uint64)field_count * sizeof(WASMValue),
  870. error_buf, error_buf_size))) {
  871. goto fail;
  872. }
  873. struct_init_values->type_idx = type_idx;
  874. struct_init_values->count = field_count;
  875. for (i = field_count; i > 0; i--) {
  876. WASMRefType *field_ref_type = NULL;
  877. uint32 field_idx = i - 1;
  878. uint8 field_type =
  879. struct_type->fields[field_idx].field_type;
  880. if (wasm_is_type_multi_byte_type(field_type)) {
  881. field_ref_type = wasm_reftype_map_find(
  882. struct_type->ref_type_maps,
  883. struct_type->ref_type_map_count, field_idx);
  884. }
  885. if (is_packed_type(field_type)) {
  886. field_type = VALUE_TYPE_I32;
  887. }
  888. if (!pop_const_expr_stack(
  889. &const_expr_ctx, NULL, field_type,
  890. field_ref_type, NULL,
  891. &struct_init_values->fields[field_idx],
  892. error_buf, error_buf_size)) {
  893. destroy_init_expr_data_recursive(
  894. module, struct_init_values);
  895. goto fail;
  896. }
  897. }
  898. cur_value.data = struct_init_values;
  899. wasm_set_refheaptype_typeidx(
  900. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  901. if (!push_const_expr_stack(
  902. &const_expr_ctx, flag, cur_ref_type.ref_type,
  903. &cur_ref_type, (uint8)opcode1, &cur_value,
  904. error_buf, error_buf_size)) {
  905. destroy_init_expr_data_recursive(
  906. module, struct_init_values);
  907. goto fail;
  908. }
  909. break;
  910. }
  911. case WASM_OP_STRUCT_NEW_DEFAULT:
  912. {
  913. read_leb_uint32(p, p_end, cur_value.type_index);
  914. type_idx = cur_value.type_index;
  915. if (!check_type_index(module, module->type_count,
  916. type_idx, error_buf,
  917. error_buf_size)) {
  918. goto fail;
  919. }
  920. if (module->types[type_idx]->type_flag
  921. != WASM_TYPE_STRUCT) {
  922. set_error_buf(error_buf, error_buf_size,
  923. "unknown struct type");
  924. goto fail;
  925. }
  926. cur_value.type_index = type_idx;
  927. cur_value.data = NULL;
  928. wasm_set_refheaptype_typeidx(
  929. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  930. if (!push_const_expr_stack(
  931. &const_expr_ctx, flag, cur_ref_type.ref_type,
  932. &cur_ref_type, (uint8)opcode1, &cur_value,
  933. error_buf, error_buf_size)) {
  934. goto fail;
  935. }
  936. break;
  937. }
  938. case WASM_OP_ARRAY_NEW:
  939. case WASM_OP_ARRAY_NEW_DEFAULT:
  940. case WASM_OP_ARRAY_NEW_FIXED:
  941. {
  942. WASMArrayNewInitValues *array_init_values = NULL;
  943. WASMArrayType *array_type = NULL;
  944. WASMRefType *elem_ref_type = NULL;
  945. uint64 total_size;
  946. uint8 elem_type;
  947. read_leb_uint32(p, p_end, cur_value.type_index);
  948. type_idx = cur_value.type_index;
  949. if (!check_type_index(module, module->type_count,
  950. type_idx, error_buf,
  951. error_buf_size)) {
  952. goto fail;
  953. }
  954. array_type = (WASMArrayType *)module->types[type_idx];
  955. if (array_type->base_type.type_flag
  956. != WASM_TYPE_ARRAY) {
  957. set_error_buf(error_buf, error_buf_size,
  958. "unknown array type");
  959. goto fail;
  960. }
  961. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  962. elem_type = array_type->elem_type;
  963. if (wasm_is_type_multi_byte_type(elem_type)) {
  964. elem_ref_type = array_type->elem_ref_type;
  965. }
  966. if (is_packed_type(elem_type)) {
  967. elem_type = VALUE_TYPE_I32;
  968. }
  969. if (opcode1 == WASM_OP_ARRAY_NEW) {
  970. WASMValue len_val = { 0 };
  971. uint64 size = 0;
  972. if (!pop_const_expr_stack(
  973. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  974. NULL, NULL, &len_val, error_buf,
  975. error_buf_size)) {
  976. goto fail;
  977. }
  978. size =
  979. sizeof(WASMArrayNewInitValues)
  980. + sizeof(WASMValue) * (uint64)len_val.i32;
  981. if (!(array_init_values = loader_malloc(
  982. size, error_buf, error_buf_size))) {
  983. goto fail;
  984. }
  985. array_init_values->type_idx = type_idx;
  986. array_init_values->length = len_val.i32;
  987. if (!pop_const_expr_stack(
  988. &const_expr_ctx, NULL, elem_type,
  989. elem_ref_type, NULL,
  990. &array_init_values->elem_data[0],
  991. error_buf, error_buf_size)) {
  992. destroy_init_expr_data_recursive(
  993. module, array_init_values);
  994. goto fail;
  995. }
  996. cur_value.data = array_init_values;
  997. }
  998. else {
  999. /* WASM_OP_ARRAY_NEW_FIXED */
  1000. uint32 len;
  1001. read_leb_uint32(p, p_end, len);
  1002. total_size =
  1003. (uint64)offsetof(WASMArrayNewInitValues,
  1004. elem_data)
  1005. + (uint64)sizeof(WASMValue) * len;
  1006. if (!(array_init_values =
  1007. loader_malloc(total_size, error_buf,
  1008. error_buf_size))) {
  1009. goto fail;
  1010. }
  1011. array_init_values->type_idx = type_idx;
  1012. array_init_values->length = len;
  1013. for (i = len; i > 0; i--) {
  1014. if (!pop_const_expr_stack(
  1015. &const_expr_ctx, NULL, elem_type,
  1016. elem_ref_type, NULL,
  1017. &array_init_values
  1018. ->elem_data[i - 1],
  1019. error_buf, error_buf_size)) {
  1020. destroy_init_expr_data_recursive(
  1021. module, array_init_values);
  1022. goto fail;
  1023. }
  1024. }
  1025. cur_value.data = array_init_values;
  1026. }
  1027. }
  1028. else {
  1029. /* WASM_OP_ARRAY_NEW_DEFAULT */
  1030. WASMValue len_val;
  1031. uint32 len;
  1032. /* POP(i32) */
  1033. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1034. VALUE_TYPE_I32, NULL,
  1035. NULL, &len_val, error_buf,
  1036. error_buf_size)) {
  1037. goto fail;
  1038. }
  1039. len = len_val.i32;
  1040. cur_value.array_new_default.type_index = type_idx;
  1041. cur_value.array_new_default.length = len;
  1042. }
  1043. wasm_set_refheaptype_typeidx(
  1044. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1045. if (!push_const_expr_stack(
  1046. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1047. &cur_ref_type, (uint8)opcode1, &cur_value,
  1048. error_buf, error_buf_size)) {
  1049. if (array_init_values) {
  1050. destroy_init_expr_data_recursive(
  1051. module, array_init_values);
  1052. }
  1053. goto fail;
  1054. }
  1055. break;
  1056. }
  1057. case WASM_OP_ANY_CONVERT_EXTERN:
  1058. {
  1059. set_error_buf(error_buf, error_buf_size,
  1060. "unsupported constant expression of "
  1061. "extern.internalize");
  1062. goto fail;
  1063. }
  1064. case WASM_OP_EXTERN_CONVERT_ANY:
  1065. {
  1066. set_error_buf(error_buf, error_buf_size,
  1067. "unsupported constant expression of "
  1068. "extern.externalize");
  1069. goto fail;
  1070. }
  1071. case WASM_OP_REF_I31:
  1072. {
  1073. /* POP(i32) */
  1074. if (!pop_const_expr_stack(
  1075. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1076. NULL, &cur_value, error_buf, error_buf_size)) {
  1077. goto fail;
  1078. }
  1079. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1080. false, HEAP_TYPE_I31);
  1081. if (!push_const_expr_stack(
  1082. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1083. &cur_ref_type, (uint8)opcode1, &cur_value,
  1084. error_buf, error_buf_size)) {
  1085. goto fail;
  1086. }
  1087. break;
  1088. }
  1089. default:
  1090. set_error_buf(
  1091. error_buf, error_buf_size,
  1092. "type mismatch or constant expression required");
  1093. goto fail;
  1094. }
  1095. break;
  1096. }
  1097. #endif /* end of WASM_ENABLE_GC != 0 */
  1098. default:
  1099. {
  1100. set_error_buf(error_buf, error_buf_size,
  1101. "illegal opcode "
  1102. "or constant expression required "
  1103. "or type mismatch");
  1104. goto fail;
  1105. }
  1106. }
  1107. CHECK_BUF(p, p_end, 1);
  1108. flag = read_uint8(p);
  1109. }
  1110. /* There should be only one value left on the init value stack */
  1111. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1112. #if WASM_ENABLE_GC != 0
  1113. ref_type, &opcode,
  1114. #endif
  1115. &cur_value, error_buf, error_buf_size)) {
  1116. goto fail;
  1117. }
  1118. if (const_expr_ctx.sp != 0) {
  1119. set_error_buf(error_buf, error_buf_size,
  1120. "type mismatch: illegal constant opcode sequence");
  1121. goto fail;
  1122. }
  1123. init_expr->init_expr_type = flag;
  1124. init_expr->u = cur_value;
  1125. #if WASM_ENABLE_GC != 0
  1126. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1127. switch (opcode) {
  1128. case WASM_OP_STRUCT_NEW:
  1129. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1130. break;
  1131. case WASM_OP_STRUCT_NEW_DEFAULT:
  1132. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1133. break;
  1134. case WASM_OP_ARRAY_NEW:
  1135. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1136. break;
  1137. case WASM_OP_ARRAY_NEW_DEFAULT:
  1138. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1139. break;
  1140. case WASM_OP_ARRAY_NEW_FIXED:
  1141. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1142. break;
  1143. case WASM_OP_REF_I31:
  1144. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1145. break;
  1146. default:
  1147. bh_assert(0);
  1148. break;
  1149. }
  1150. }
  1151. #endif /* end of WASM_ENABLE_GC != 0 */
  1152. *p_buf = p;
  1153. destroy_const_expr_stack(&const_expr_ctx);
  1154. return true;
  1155. fail:
  1156. destroy_const_expr_stack(&const_expr_ctx);
  1157. return false;
  1158. }
  1159. static bool
  1160. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1161. {
  1162. if (mutable >= 2) {
  1163. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1164. return false;
  1165. }
  1166. return true;
  1167. }
  1168. #if WASM_ENABLE_GC != 0
  1169. static void
  1170. destroy_func_type(WASMFuncType *type)
  1171. {
  1172. /* Destroy the reference type hash set */
  1173. if (type->ref_type_maps)
  1174. wasm_runtime_free(type->ref_type_maps);
  1175. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1176. && WASM_ENABLE_LAZY_JIT != 0
  1177. if (type->call_to_llvm_jit_from_fast_jit)
  1178. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1179. #endif
  1180. /* Free the type */
  1181. wasm_runtime_free(type);
  1182. }
  1183. static void
  1184. destroy_struct_type(WASMStructType *type)
  1185. {
  1186. if (type->ref_type_maps)
  1187. wasm_runtime_free(type->ref_type_maps);
  1188. wasm_runtime_free(type);
  1189. }
  1190. static void
  1191. destroy_array_type(WASMArrayType *type)
  1192. {
  1193. wasm_runtime_free(type);
  1194. }
  1195. static void
  1196. destroy_wasm_type(WASMType *type)
  1197. {
  1198. if (type->ref_count > 1) {
  1199. /* The type is referenced by other types
  1200. of current wasm module */
  1201. type->ref_count--;
  1202. return;
  1203. }
  1204. if (type->type_flag == WASM_TYPE_FUNC)
  1205. destroy_func_type((WASMFuncType *)type);
  1206. else if (type->type_flag == WASM_TYPE_STRUCT)
  1207. destroy_struct_type((WASMStructType *)type);
  1208. else if (type->type_flag == WASM_TYPE_ARRAY)
  1209. destroy_array_type((WASMArrayType *)type);
  1210. else {
  1211. bh_assert(0);
  1212. }
  1213. }
  1214. /* Resolve (ref null ht) or (ref ht) */
  1215. static bool
  1216. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1217. WASMModule *module, uint32 type_count, bool nullable,
  1218. WASMRefType *ref_type, char *error_buf,
  1219. uint32 error_buf_size)
  1220. {
  1221. const uint8 *p = *p_buf, *p_end = buf_end;
  1222. ref_type->ref_type =
  1223. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1224. ref_type->ref_ht_common.nullable = nullable;
  1225. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1226. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1227. /* heap type is (type i), i : typeidx, >= 0 */
  1228. if (!check_type_index(module, type_count,
  1229. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1230. error_buf_size)) {
  1231. return false;
  1232. }
  1233. }
  1234. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1235. /* heap type is func, extern, any, eq, i31 or data */
  1236. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1237. return false;
  1238. }
  1239. *p_buf = p;
  1240. return true;
  1241. fail:
  1242. return false;
  1243. }
  1244. static bool
  1245. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1246. WASMModule *module, uint32 type_count,
  1247. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1248. bool allow_packed_type, char *error_buf,
  1249. uint32 error_buf_size)
  1250. {
  1251. const uint8 *p = *p_buf, *p_end = buf_end;
  1252. uint8 type;
  1253. memset(ref_type, 0, sizeof(WASMRefType));
  1254. CHECK_BUF(p, p_end, 1);
  1255. type = read_uint8(p);
  1256. if (wasm_is_reftype_htref_nullable(type)) {
  1257. /* (ref null ht) */
  1258. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1259. ref_type, error_buf, error_buf_size))
  1260. return false;
  1261. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1262. *p_need_ref_type_map = true;
  1263. else {
  1264. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1265. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1266. multi-byte type to one-byte type to reduce the footprint and
  1267. the complexity of type equal/subtype checking */
  1268. ref_type->ref_type =
  1269. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1270. *p_need_ref_type_map = false;
  1271. }
  1272. }
  1273. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1274. /* (ref ht) */
  1275. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1276. ref_type, error_buf, error_buf_size))
  1277. return false;
  1278. *p_need_ref_type_map = true;
  1279. #if WASM_ENABLE_STRINGREF != 0
  1280. /* covert (ref string) to stringref */
  1281. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1282. ref_type->ref_type =
  1283. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1284. *p_need_ref_type_map = false;
  1285. }
  1286. #endif
  1287. }
  1288. else {
  1289. /* type which can be represented by one byte */
  1290. if (!is_valid_value_type_for_interpreter(type)
  1291. && !(allow_packed_type && is_packed_type(type))) {
  1292. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1293. return false;
  1294. }
  1295. ref_type->ref_type = type;
  1296. *p_need_ref_type_map = false;
  1297. #if WASM_ENABLE_WAMR_COMPILER != 0
  1298. /* If any value's type is v128, mark the module as SIMD used */
  1299. if (type == VALUE_TYPE_V128)
  1300. module->is_simd_used = true;
  1301. #endif
  1302. }
  1303. *p_buf = p;
  1304. return true;
  1305. fail:
  1306. return false;
  1307. }
  1308. static WASMRefType *
  1309. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1310. char *error_buf, uint32 error_buf_size)
  1311. {
  1312. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1313. if (!ret) {
  1314. set_error_buf(error_buf, error_buf_size,
  1315. "insert ref type to hash set failed");
  1316. }
  1317. return ret;
  1318. }
  1319. static bool
  1320. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1321. uint32 type_count, uint32 type_idx, char *error_buf,
  1322. uint32 error_buf_size)
  1323. {
  1324. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1325. uint32 param_count, result_count, i, j = 0;
  1326. uint32 param_cell_num, ret_cell_num;
  1327. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1328. uint64 total_size;
  1329. bool need_ref_type_map;
  1330. WASMRefType ref_type;
  1331. WASMFuncType *type = NULL;
  1332. /* Parse first time to resolve param count, result count and
  1333. ref type map count */
  1334. read_leb_uint32(p, p_end, param_count);
  1335. p_org = p;
  1336. for (i = 0; i < param_count; i++) {
  1337. if (!resolve_value_type(&p, p_end, module, type_count,
  1338. &need_ref_type_map, &ref_type, false, error_buf,
  1339. error_buf_size)) {
  1340. return false;
  1341. }
  1342. if (need_ref_type_map)
  1343. ref_type_map_count++;
  1344. }
  1345. read_leb_uint32(p, p_end, result_count);
  1346. for (i = 0; i < result_count; i++) {
  1347. if (!resolve_value_type(&p, p_end, module, type_count,
  1348. &need_ref_type_map, &ref_type, false, error_buf,
  1349. error_buf_size)) {
  1350. return false;
  1351. }
  1352. if (need_ref_type_map) {
  1353. ref_type_map_count++;
  1354. result_ref_type_map_count++;
  1355. }
  1356. }
  1357. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1358. "ref type map count: %d",
  1359. type_idx, param_count, result_count, ref_type_map_count);
  1360. /* Parse second time to resolve param types, result types and
  1361. ref type map info */
  1362. p = p_org;
  1363. total_size = offsetof(WASMFuncType, types)
  1364. + sizeof(uint8) * (uint64)(param_count + result_count);
  1365. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1366. return false;
  1367. }
  1368. if (ref_type_map_count > 0) {
  1369. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1370. if (!(type->ref_type_maps =
  1371. loader_malloc(total_size, error_buf, error_buf_size))) {
  1372. goto fail;
  1373. }
  1374. }
  1375. type->base_type.type_flag = WASM_TYPE_FUNC;
  1376. type->param_count = param_count;
  1377. type->result_count = result_count;
  1378. type->ref_type_map_count = ref_type_map_count;
  1379. if (ref_type_map_count > 0) {
  1380. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1381. - result_ref_type_map_count;
  1382. }
  1383. for (i = 0; i < param_count; i++) {
  1384. if (!resolve_value_type(&p, p_end, module, type_count,
  1385. &need_ref_type_map, &ref_type, false, error_buf,
  1386. error_buf_size)) {
  1387. goto fail;
  1388. }
  1389. type->types[i] = ref_type.ref_type;
  1390. if (need_ref_type_map) {
  1391. type->ref_type_maps[j].index = i;
  1392. if (!(type->ref_type_maps[j++].ref_type =
  1393. reftype_set_insert(module->ref_type_set, &ref_type,
  1394. error_buf, error_buf_size))) {
  1395. goto fail;
  1396. }
  1397. }
  1398. }
  1399. read_leb_uint32(p, p_end, result_count);
  1400. for (i = 0; i < result_count; i++) {
  1401. if (!resolve_value_type(&p, p_end, module, type_count,
  1402. &need_ref_type_map, &ref_type, false, error_buf,
  1403. error_buf_size)) {
  1404. goto fail;
  1405. }
  1406. type->types[param_count + i] = ref_type.ref_type;
  1407. if (need_ref_type_map) {
  1408. type->ref_type_maps[j].index = param_count + i;
  1409. if (!(type->ref_type_maps[j++].ref_type =
  1410. reftype_set_insert(module->ref_type_set, &ref_type,
  1411. error_buf, error_buf_size))) {
  1412. goto fail;
  1413. }
  1414. }
  1415. }
  1416. bh_assert(j == type->ref_type_map_count);
  1417. #if TRACE_WASM_LOADER != 0
  1418. os_printf("type %d = ", type_idx);
  1419. wasm_dump_func_type(type);
  1420. #endif
  1421. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1422. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1423. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1424. set_error_buf(error_buf, error_buf_size,
  1425. "param count or result count too large");
  1426. goto fail;
  1427. }
  1428. type->param_cell_num = (uint16)param_cell_num;
  1429. type->ret_cell_num = (uint16)ret_cell_num;
  1430. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1431. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1432. #endif
  1433. #if WASM_ENABLE_WAMR_COMPILER != 0
  1434. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1435. if (type->types[i] == VALUE_TYPE_V128)
  1436. module->is_simd_used = true;
  1437. }
  1438. #endif
  1439. *p_buf = p;
  1440. module->types[type_idx] = (WASMType *)type;
  1441. return true;
  1442. fail:
  1443. if (type)
  1444. destroy_func_type(type);
  1445. return false;
  1446. }
  1447. static bool
  1448. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1449. WASMModule *module, uint32 type_count, uint32 type_idx,
  1450. char *error_buf, uint32 error_buf_size)
  1451. {
  1452. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1453. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1454. uint32 i, j = 0, offset;
  1455. uint16 *reference_table;
  1456. uint64 total_size;
  1457. uint8 mutable;
  1458. bool need_ref_type_map;
  1459. WASMRefType ref_type;
  1460. WASMStructType *type = NULL;
  1461. /* Parse first time to resolve field count and ref type map count */
  1462. read_leb_uint32(p, p_end, field_count);
  1463. p_org = p;
  1464. for (i = 0; i < field_count; i++) {
  1465. if (!resolve_value_type(&p, p_end, module, type_count,
  1466. &need_ref_type_map, &ref_type, true, error_buf,
  1467. error_buf_size)) {
  1468. return false;
  1469. }
  1470. if (need_ref_type_map)
  1471. ref_type_map_count++;
  1472. if (wasm_is_type_reftype(ref_type.ref_type))
  1473. ref_field_count++;
  1474. CHECK_BUF(p, p_end, 1);
  1475. mutable = read_uint8(p);
  1476. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1477. return false;
  1478. }
  1479. }
  1480. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1481. type_idx, field_count, ref_type_map_count);
  1482. /* Parse second time to resolve field types and ref type map info */
  1483. p = p_org;
  1484. total_size = offsetof(WASMStructType, fields)
  1485. + sizeof(WASMStructFieldType) * (uint64)field_count
  1486. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1487. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1488. return false;
  1489. }
  1490. if (ref_type_map_count > 0) {
  1491. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1492. if (!(type->ref_type_maps =
  1493. loader_malloc(total_size, error_buf, error_buf_size))) {
  1494. goto fail;
  1495. }
  1496. }
  1497. type->reference_table = reference_table =
  1498. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1499. + sizeof(WASMStructFieldType) * field_count);
  1500. *reference_table++ = ref_field_count;
  1501. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1502. type->field_count = field_count;
  1503. type->ref_type_map_count = ref_type_map_count;
  1504. offset = (uint32)offsetof(WASMStructObject, field_data);
  1505. for (i = 0; i < field_count; i++) {
  1506. if (!resolve_value_type(&p, p_end, module, type_count,
  1507. &need_ref_type_map, &ref_type, true, error_buf,
  1508. error_buf_size)) {
  1509. goto fail;
  1510. }
  1511. type->fields[i].field_type = ref_type.ref_type;
  1512. if (need_ref_type_map) {
  1513. type->ref_type_maps[j].index = i;
  1514. if (!(type->ref_type_maps[j++].ref_type =
  1515. reftype_set_insert(module->ref_type_set, &ref_type,
  1516. error_buf, error_buf_size))) {
  1517. goto fail;
  1518. }
  1519. }
  1520. CHECK_BUF(p, p_end, 1);
  1521. type->fields[i].field_flags = read_uint8(p);
  1522. type->fields[i].field_size =
  1523. (uint8)wasm_reftype_size(ref_type.ref_type);
  1524. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1525. || defined(BUILD_TARGET_X86_32))
  1526. if (type->fields[i].field_size == 2)
  1527. offset = align_uint(offset, 2);
  1528. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1529. offset = align_uint(offset, 4);
  1530. #endif
  1531. type->fields[i].field_offset = offset;
  1532. if (wasm_is_type_reftype(ref_type.ref_type))
  1533. *reference_table++ = offset;
  1534. offset += type->fields[i].field_size;
  1535. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1536. type->fields[i].field_flags, type->fields[i].field_type);
  1537. }
  1538. type->total_size = offset;
  1539. bh_assert(j == type->ref_type_map_count);
  1540. #if TRACE_WASM_LOADER != 0
  1541. os_printf("type %d = ", type_idx);
  1542. wasm_dump_struct_type(type);
  1543. #endif
  1544. *p_buf = p;
  1545. module->types[type_idx] = (WASMType *)type;
  1546. return true;
  1547. fail:
  1548. if (type)
  1549. destroy_struct_type(type);
  1550. return false;
  1551. }
  1552. static bool
  1553. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1554. WASMModule *module, uint32 type_count, uint32 type_idx,
  1555. char *error_buf, uint32 error_buf_size)
  1556. {
  1557. const uint8 *p = *p_buf, *p_end = buf_end;
  1558. uint8 mutable;
  1559. bool need_ref_type_map;
  1560. WASMRefType ref_type;
  1561. WASMArrayType *type = NULL;
  1562. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1563. &ref_type, true, error_buf, error_buf_size)) {
  1564. return false;
  1565. }
  1566. CHECK_BUF(p, p_end, 1);
  1567. mutable = read_uint8(p);
  1568. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1569. return false;
  1570. }
  1571. LOG_VERBOSE("type %u: array", type_idx);
  1572. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1573. error_buf_size))) {
  1574. return false;
  1575. }
  1576. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1577. type->elem_flags = mutable;
  1578. type->elem_type = ref_type.ref_type;
  1579. if (need_ref_type_map) {
  1580. if (!(type->elem_ref_type =
  1581. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1582. error_buf_size))) {
  1583. goto fail;
  1584. }
  1585. }
  1586. #if TRACE_WASM_LOADER != 0
  1587. os_printf("type %d = ", type_idx);
  1588. wasm_dump_array_type(type);
  1589. #endif
  1590. *p_buf = p;
  1591. module->types[type_idx] = (WASMType *)type;
  1592. return true;
  1593. fail:
  1594. if (type)
  1595. destroy_array_type(type);
  1596. return false;
  1597. }
  1598. static bool
  1599. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1600. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1601. {
  1602. if (heap_type >= 0) {
  1603. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1604. error_buf_size)) {
  1605. return false;
  1606. }
  1607. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1608. heap_type);
  1609. }
  1610. else {
  1611. if (!wasm_is_valid_heap_type(heap_type)) {
  1612. set_error_buf(error_buf, error_buf_size, "unknown type");
  1613. return false;
  1614. }
  1615. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1616. heap_type);
  1617. if (nullable) {
  1618. /* For (ref null func/extern/any/eq/i31/data),
  1619. they are same as
  1620. funcref/externref/anyref/eqref/i31ref/dataref,
  1621. we convert the multi-byte type to one-byte
  1622. type to reduce the footprint and the
  1623. complexity of type equal/subtype checking */
  1624. ref_type->ref_type =
  1625. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1626. }
  1627. }
  1628. return true;
  1629. }
  1630. static void
  1631. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1632. WASMRefType *ref_type2)
  1633. {
  1634. /**
  1635. * The difference rt1 ∖ rt2 between two reference types is defined as
  1636. * follows:
  1637. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1638. * (ref ht2) = (ref null?1 ht1)
  1639. */
  1640. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1641. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1642. ref_type1, wasm_reftype_struct_size(ref_type1));
  1643. }
  1644. else {
  1645. ref_type_diff->ref_type = ref_type1->ref_type;
  1646. }
  1647. if (ref_type2->ref_ht_common.nullable) {
  1648. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1649. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1650. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1651. (int32)ref_type_diff->ref_type - 0x80);
  1652. }
  1653. else {
  1654. ref_type_diff->ref_ht_typeidx.nullable = false;
  1655. }
  1656. }
  1657. }
  1658. #else /* else of WASM_ENABLE_GC != 0 */
  1659. static void
  1660. destroy_wasm_type(WASMType *type)
  1661. {
  1662. if (type->ref_count > 1) {
  1663. /* The type is referenced by other types
  1664. of current wasm module */
  1665. type->ref_count--;
  1666. return;
  1667. }
  1668. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1669. && WASM_ENABLE_LAZY_JIT != 0
  1670. if (type->call_to_llvm_jit_from_fast_jit)
  1671. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1672. #endif
  1673. wasm_runtime_free(type);
  1674. }
  1675. #endif /* end of WASM_ENABLE_GC != 0 */
  1676. static bool
  1677. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1678. char *error_buf, uint32 error_buf_size)
  1679. {
  1680. const uint8 *p = buf, *p_end = buf_end;
  1681. uint32 type_count, i;
  1682. uint64 total_size;
  1683. uint8 flag;
  1684. #if WASM_ENABLE_GC != 0
  1685. uint32 processed_type_count = 0;
  1686. #endif
  1687. read_leb_uint32(p, p_end, type_count);
  1688. if (type_count) {
  1689. module->type_count = type_count;
  1690. total_size = sizeof(WASMType *) * (uint64)type_count;
  1691. if (!(module->types =
  1692. loader_malloc(total_size, error_buf, error_buf_size))) {
  1693. return false;
  1694. }
  1695. #if WASM_ENABLE_GC == 0
  1696. for (i = 0; i < type_count; i++) {
  1697. WASMFuncType *type;
  1698. const uint8 *p_org;
  1699. uint32 param_count, result_count, j;
  1700. uint32 param_cell_num, ret_cell_num;
  1701. CHECK_BUF(p, p_end, 1);
  1702. flag = read_uint8(p);
  1703. if (flag != 0x60) {
  1704. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1705. return false;
  1706. }
  1707. read_leb_uint32(p, p_end, param_count);
  1708. /* Resolve param count and result count firstly */
  1709. p_org = p;
  1710. CHECK_BUF(p, p_end, param_count);
  1711. p += param_count;
  1712. read_leb_uint32(p, p_end, result_count);
  1713. CHECK_BUF(p, p_end, result_count);
  1714. p = p_org;
  1715. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1716. set_error_buf(error_buf, error_buf_size,
  1717. "param count or result count too large");
  1718. return false;
  1719. }
  1720. total_size = offsetof(WASMFuncType, types)
  1721. + sizeof(uint8) * (uint64)(param_count + result_count);
  1722. if (!(type = module->types[i] =
  1723. loader_malloc(total_size, error_buf, error_buf_size))) {
  1724. return false;
  1725. }
  1726. /* Resolve param types and result types */
  1727. type->ref_count = 1;
  1728. type->param_count = (uint16)param_count;
  1729. type->result_count = (uint16)result_count;
  1730. for (j = 0; j < param_count; j++) {
  1731. CHECK_BUF(p, p_end, 1);
  1732. type->types[j] = read_uint8(p);
  1733. }
  1734. read_leb_uint32(p, p_end, result_count);
  1735. for (j = 0; j < result_count; j++) {
  1736. CHECK_BUF(p, p_end, 1);
  1737. type->types[param_count + j] = read_uint8(p);
  1738. }
  1739. for (j = 0; j < param_count + result_count; j++) {
  1740. if (!is_valid_value_type_for_interpreter(type->types[j])) {
  1741. set_error_buf(error_buf, error_buf_size,
  1742. "unknown value type");
  1743. return false;
  1744. }
  1745. }
  1746. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1747. ret_cell_num =
  1748. wasm_get_cell_num(type->types + param_count, result_count);
  1749. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1750. set_error_buf(error_buf, error_buf_size,
  1751. "param count or result count too large");
  1752. return false;
  1753. }
  1754. type->param_cell_num = (uint16)param_cell_num;
  1755. type->ret_cell_num = (uint16)ret_cell_num;
  1756. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1757. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1758. #endif
  1759. #if WASM_ENABLE_WAMR_COMPILER != 0
  1760. for (j = 0; j < type->param_count + type->result_count; j++) {
  1761. if (type->types[j] == VALUE_TYPE_V128)
  1762. module->is_simd_used = true;
  1763. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1764. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1765. module->is_ref_types_used = true;
  1766. }
  1767. #endif
  1768. /* If there is already a same type created, use it instead */
  1769. for (j = 0; j < i; j++) {
  1770. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1771. if (module->types[j]->ref_count == UINT16_MAX) {
  1772. set_error_buf(error_buf, error_buf_size,
  1773. "wasm type's ref count too large");
  1774. return false;
  1775. }
  1776. destroy_wasm_type(type);
  1777. module->types[i] = module->types[j];
  1778. module->types[j]->ref_count++;
  1779. break;
  1780. }
  1781. }
  1782. }
  1783. #else /* else of WASM_ENABLE_GC == 0 */
  1784. for (i = 0; i < type_count; i++) {
  1785. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1786. uint32 rec_count = 1, j;
  1787. bool is_sub_final = true;
  1788. CHECK_BUF(p, p_end, 1);
  1789. flag = read_uint8(p);
  1790. if (flag == DEFINED_TYPE_REC) {
  1791. read_leb_uint32(p, p_end, rec_count);
  1792. if (rec_count > 1) {
  1793. uint64 new_total_size;
  1794. /* integer overflow */
  1795. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1796. set_error_buf(error_buf, error_buf_size,
  1797. "recursive type count too large");
  1798. return false;
  1799. }
  1800. module->type_count += rec_count - 1;
  1801. new_total_size =
  1802. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1803. if (new_total_size > UINT32_MAX) {
  1804. set_error_buf(error_buf, error_buf_size,
  1805. "allocate memory failed");
  1806. return false;
  1807. }
  1808. MEM_REALLOC(module->types, (uint32)total_size,
  1809. (uint32)new_total_size);
  1810. total_size = new_total_size;
  1811. }
  1812. LOG_VERBOSE("Processing rec group [%d-%d]",
  1813. processed_type_count,
  1814. processed_type_count + rec_count - 1);
  1815. }
  1816. else {
  1817. p--;
  1818. }
  1819. for (j = 0; j < rec_count; j++) {
  1820. WASMType *cur_type = NULL;
  1821. CHECK_BUF(p, p_end, 1);
  1822. flag = read_uint8(p);
  1823. parent_type_idx = -1;
  1824. if (flag == DEFINED_TYPE_SUB
  1825. || flag == DEFINED_TYPE_SUB_FINAL) {
  1826. read_leb_uint32(p, p_end, super_type_count);
  1827. if (super_type_count > 1) {
  1828. set_error_buf(error_buf, error_buf_size,
  1829. "super type count too large");
  1830. return false;
  1831. }
  1832. if (super_type_count > 0) {
  1833. read_leb_uint32(p, p_end, parent_type_idx);
  1834. if (parent_type_idx >= processed_type_count + j) {
  1835. set_error_buf_v(error_buf, error_buf_size,
  1836. "unknown type %d", parent_type_idx);
  1837. return false;
  1838. }
  1839. if (module->types[parent_type_idx]->is_sub_final) {
  1840. set_error_buf(error_buf, error_buf_size,
  1841. "sub type can not inherit from "
  1842. "a final super type");
  1843. return false;
  1844. }
  1845. }
  1846. if (flag == DEFINED_TYPE_SUB)
  1847. is_sub_final = false;
  1848. CHECK_BUF(p, p_end, 1);
  1849. flag = read_uint8(p);
  1850. }
  1851. if (flag == DEFINED_TYPE_FUNC) {
  1852. if (!resolve_func_type(&p, buf_end, module,
  1853. processed_type_count + rec_count,
  1854. processed_type_count + j, error_buf,
  1855. error_buf_size)) {
  1856. return false;
  1857. }
  1858. }
  1859. else if (flag == DEFINED_TYPE_STRUCT) {
  1860. if (!resolve_struct_type(&p, buf_end, module,
  1861. processed_type_count + rec_count,
  1862. processed_type_count + j,
  1863. error_buf, error_buf_size)) {
  1864. return false;
  1865. }
  1866. }
  1867. else if (flag == DEFINED_TYPE_ARRAY) {
  1868. if (!resolve_array_type(&p, buf_end, module,
  1869. processed_type_count + rec_count,
  1870. processed_type_count + j, error_buf,
  1871. error_buf_size)) {
  1872. return false;
  1873. }
  1874. }
  1875. else {
  1876. set_error_buf(error_buf, error_buf_size,
  1877. "invalid type flag");
  1878. return false;
  1879. }
  1880. cur_type = module->types[processed_type_count + j];
  1881. cur_type->ref_count = 1;
  1882. cur_type->parent_type_idx = parent_type_idx;
  1883. cur_type->is_sub_final = is_sub_final;
  1884. cur_type->rec_count = rec_count;
  1885. cur_type->rec_idx = j;
  1886. cur_type->rec_begin_type_idx = processed_type_count;
  1887. }
  1888. /* resolve subtyping relationship in current rec group */
  1889. for (j = 0; j < rec_count; j++) {
  1890. WASMType *cur_type = module->types[processed_type_count + j];
  1891. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1892. WASMType *parent_type =
  1893. module->types[cur_type->parent_type_idx];
  1894. cur_type->parent_type = parent_type;
  1895. cur_type->root_type = parent_type->root_type;
  1896. if (parent_type->inherit_depth == UINT16_MAX) {
  1897. set_error_buf(error_buf, error_buf_size,
  1898. "parent type's inherit depth too large");
  1899. return false;
  1900. }
  1901. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1902. }
  1903. else {
  1904. cur_type->parent_type = NULL;
  1905. cur_type->root_type = cur_type;
  1906. cur_type->inherit_depth = 0;
  1907. }
  1908. }
  1909. for (j = 0; j < rec_count; j++) {
  1910. WASMType *cur_type = module->types[processed_type_count + j];
  1911. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1912. WASMType *parent_type =
  1913. module->types[cur_type->parent_type_idx];
  1914. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1915. module->types,
  1916. module->type_count)) {
  1917. set_error_buf_v(error_buf, error_buf_size,
  1918. "sub type %u does not match super type",
  1919. processed_type_count + j);
  1920. return false;
  1921. }
  1922. }
  1923. }
  1924. /* If there is already an equivalence type or a group of equivalence
  1925. recursive types created, use it or them instead */
  1926. for (j = 0; j < processed_type_count;) {
  1927. WASMType *src_type = module->types[j];
  1928. WASMType *cur_type = module->types[processed_type_count];
  1929. uint32 k, src_rec_count;
  1930. src_rec_count = src_type->rec_count;
  1931. if (src_rec_count != rec_count) {
  1932. /* no type equivalence */
  1933. j += src_rec_count;
  1934. continue;
  1935. }
  1936. for (k = 0; k < rec_count; k++) {
  1937. src_type = module->types[j + k];
  1938. cur_type = module->types[processed_type_count + k];
  1939. if (!wasm_type_equal(src_type, cur_type, module->types,
  1940. module->type_count)) {
  1941. break;
  1942. }
  1943. }
  1944. if (k < rec_count) {
  1945. /* no type equivalence */
  1946. j += src_rec_count;
  1947. continue;
  1948. }
  1949. /* type equivalence */
  1950. for (k = 0; k < rec_count; k++) {
  1951. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1952. set_error_buf(error_buf, error_buf_size,
  1953. "wasm type's ref count too large");
  1954. return false;
  1955. }
  1956. destroy_wasm_type(module->types[processed_type_count + k]);
  1957. module->types[processed_type_count + k] =
  1958. module->types[j + k];
  1959. module->types[j + k]->ref_count++;
  1960. }
  1961. break;
  1962. }
  1963. if (rec_count > 1) {
  1964. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1965. processed_type_count,
  1966. processed_type_count + rec_count - 1);
  1967. }
  1968. processed_type_count += rec_count;
  1969. }
  1970. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1971. * module->type_count,
  1972. error_buf, error_buf_size))) {
  1973. return false;
  1974. }
  1975. #endif /* end of WASM_ENABLE_GC == 0 */
  1976. }
  1977. if (p != p_end) {
  1978. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1979. return false;
  1980. }
  1981. LOG_VERBOSE("Load type section success.\n");
  1982. return true;
  1983. fail:
  1984. return false;
  1985. }
  1986. static void
  1987. adjust_table_max_size(bool is_table64, uint32 init_size, uint32 max_size_flag,
  1988. uint32 *max_size)
  1989. {
  1990. uint32 default_max_size;
  1991. /* TODO: current still use UINT32_MAX as upper limit for table size to keep
  1992. * ABI unchanged */
  1993. (void)is_table64;
  1994. if (UINT32_MAX / 2 > init_size)
  1995. default_max_size = init_size * 2;
  1996. else
  1997. default_max_size = UINT32_MAX;
  1998. if (default_max_size < WASM_TABLE_MAX_SIZE)
  1999. default_max_size = WASM_TABLE_MAX_SIZE;
  2000. if (max_size_flag) {
  2001. /* module defines the table limitation */
  2002. bh_assert(init_size <= *max_size);
  2003. if (init_size < *max_size) {
  2004. *max_size =
  2005. *max_size < default_max_size ? *max_size : default_max_size;
  2006. }
  2007. }
  2008. else {
  2009. /* partial defined table limitation, gives a default value */
  2010. *max_size = default_max_size;
  2011. }
  2012. }
  2013. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  2014. /**
  2015. * Find export item of a module with export info:
  2016. * module name, field name and export kind
  2017. */
  2018. static WASMExport *
  2019. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  2020. const char *field_name, uint8 export_kind,
  2021. char *error_buf, uint32 error_buf_size)
  2022. {
  2023. WASMExport *export =
  2024. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  2025. export_kind, error_buf, error_buf_size);
  2026. return export;
  2027. }
  2028. #endif
  2029. #if WASM_ENABLE_MULTI_MODULE != 0
  2030. static WASMTable *
  2031. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2032. uint32 init_size, uint32 max_size, char *error_buf,
  2033. uint32 error_buf_size)
  2034. {
  2035. WASMModuleCommon *module_reg;
  2036. WASMTable *table = NULL;
  2037. WASMExport *export = NULL;
  2038. WASMModule *module = NULL;
  2039. module_reg = wasm_runtime_find_module_registered(module_name);
  2040. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2041. LOG_DEBUG("can not find a module named %s for table", module_name);
  2042. set_error_buf(error_buf, error_buf_size, "unknown import");
  2043. return NULL;
  2044. }
  2045. module = (WASMModule *)module_reg;
  2046. export =
  2047. wasm_loader_find_export(module, module_name, table_name,
  2048. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2049. if (!export) {
  2050. return NULL;
  2051. }
  2052. /* resolve table and check the init/max size */
  2053. if (export->index < module->import_table_count) {
  2054. table =
  2055. module->import_tables[export->index].u.table.import_table_linked;
  2056. }
  2057. else {
  2058. table = &(module->tables[export->index - module->import_table_count]);
  2059. }
  2060. if (table->table_type.init_size < init_size
  2061. || table->table_type.max_size > max_size) {
  2062. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2063. module_name, table_name, table->table_type.init_size,
  2064. table->table_type.max_size, init_size, max_size);
  2065. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2066. return NULL;
  2067. }
  2068. return table;
  2069. }
  2070. static WASMMemory *
  2071. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2072. uint32 init_page_count, uint32 max_page_count,
  2073. char *error_buf, uint32 error_buf_size)
  2074. {
  2075. WASMModuleCommon *module_reg;
  2076. WASMMemory *memory = NULL;
  2077. WASMExport *export = NULL;
  2078. WASMModule *module = NULL;
  2079. module_reg = wasm_runtime_find_module_registered(module_name);
  2080. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2081. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2082. set_error_buf(error_buf, error_buf_size, "unknown import");
  2083. return NULL;
  2084. }
  2085. module = (WASMModule *)module_reg;
  2086. export =
  2087. wasm_loader_find_export(module, module_name, memory_name,
  2088. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2089. if (!export) {
  2090. return NULL;
  2091. }
  2092. /* resolve memory and check the init/max page count */
  2093. if (export->index < module->import_memory_count) {
  2094. memory = module->import_memories[export->index]
  2095. .u.memory.import_memory_linked;
  2096. }
  2097. else {
  2098. memory =
  2099. &(module->memories[export->index - module->import_memory_count]);
  2100. }
  2101. if (memory->init_page_count < init_page_count
  2102. || memory->max_page_count > max_page_count) {
  2103. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2104. module_name, memory_name, memory->init_page_count,
  2105. memory->max_page_count, init_page_count, max_page_count);
  2106. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2107. return NULL;
  2108. }
  2109. return memory;
  2110. }
  2111. static WASMGlobal *
  2112. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2113. uint8 type, bool is_mutable, char *error_buf,
  2114. uint32 error_buf_size)
  2115. {
  2116. WASMModuleCommon *module_reg;
  2117. WASMGlobal *global = NULL;
  2118. WASMExport *export = NULL;
  2119. WASMModule *module = NULL;
  2120. module_reg = wasm_runtime_find_module_registered(module_name);
  2121. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2122. LOG_DEBUG("can not find a module named %s for global", module_name);
  2123. set_error_buf(error_buf, error_buf_size, "unknown import");
  2124. return NULL;
  2125. }
  2126. module = (WASMModule *)module_reg;
  2127. export =
  2128. wasm_loader_find_export(module, module_name, global_name,
  2129. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2130. if (!export) {
  2131. return NULL;
  2132. }
  2133. /* resolve and check the global */
  2134. if (export->index < module->import_global_count) {
  2135. global =
  2136. module->import_globals[export->index].u.global.import_global_linked;
  2137. }
  2138. else {
  2139. global =
  2140. &(module->globals[export->index - module->import_global_count]);
  2141. }
  2142. if (global->type.val_type != type
  2143. || global->type.is_mutable != is_mutable) {
  2144. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2145. module_name, global_name, global->type.val_type,
  2146. global->type.is_mutable, type, is_mutable);
  2147. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2148. return NULL;
  2149. }
  2150. return global;
  2151. }
  2152. #if WASM_ENABLE_TAGS != 0
  2153. static WASMTag *
  2154. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2155. const WASMType *expected_tag_type,
  2156. uint32 *linked_tag_index, char *error_buf,
  2157. uint32 error_buf_size)
  2158. {
  2159. WASMModuleCommon *module_reg;
  2160. WASMTag *tag = NULL;
  2161. WASMExport *export = NULL;
  2162. WASMModule *module = NULL;
  2163. module_reg = wasm_runtime_find_module_registered(module_name);
  2164. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2165. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2166. tag_name);
  2167. set_error_buf(error_buf, error_buf_size, "unknown import");
  2168. return NULL;
  2169. }
  2170. module = (WASMModule *)module_reg;
  2171. export =
  2172. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2173. error_buf, error_buf_size);
  2174. if (!export) {
  2175. return NULL;
  2176. }
  2177. /* resolve tag type and tag */
  2178. if (export->index < module->import_tag_count) {
  2179. /* importing an imported tag from the submodule */
  2180. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2181. }
  2182. else {
  2183. /* importing an section tag from the submodule */
  2184. tag = module->tags[export->index - module->import_tag_count];
  2185. }
  2186. /* check function type */
  2187. if (!wasm_type_equal(expected_tag_type, tag->tag_type, module->types,
  2188. module->type_count)) {
  2189. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2190. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2191. return NULL;
  2192. }
  2193. if (linked_tag_index != NULL) {
  2194. *linked_tag_index = export->index;
  2195. }
  2196. return tag;
  2197. }
  2198. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2199. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2200. static bool
  2201. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2202. const WASMModule *parent_module,
  2203. const char *sub_module_name, const char *function_name,
  2204. WASMFunctionImport *function, bool no_resolve,
  2205. char *error_buf, uint32 error_buf_size)
  2206. {
  2207. const uint8 *p = *p_buf, *p_end = buf_end;
  2208. uint32 declare_type_index = 0;
  2209. read_leb_uint32(p, p_end, declare_type_index);
  2210. *p_buf = p;
  2211. if (declare_type_index >= parent_module->type_count) {
  2212. set_error_buf(error_buf, error_buf_size, "unknown type");
  2213. return false;
  2214. }
  2215. #if WASM_ENABLE_GC != 0
  2216. function->type_idx = declare_type_index;
  2217. #endif
  2218. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2219. declare_type_index = wasm_get_smallest_type_idx(
  2220. parent_module->types, parent_module->type_count, declare_type_index);
  2221. #endif
  2222. function->func_type =
  2223. (WASMFuncType *)parent_module->types[declare_type_index];
  2224. function->module_name = (char *)sub_module_name;
  2225. function->field_name = (char *)function_name;
  2226. function->attachment = NULL;
  2227. function->signature = NULL;
  2228. function->call_conv_raw = false;
  2229. /* lookup registered native symbols first */
  2230. if (!no_resolve) {
  2231. wasm_resolve_import_func(parent_module, function);
  2232. }
  2233. return true;
  2234. fail:
  2235. return false;
  2236. }
  2237. static bool
  2238. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2239. uint32 error_buf_size)
  2240. {
  2241. if (max_size < init_size) {
  2242. set_error_buf(error_buf, error_buf_size,
  2243. "size minimum must not be greater than maximum");
  2244. return false;
  2245. }
  2246. return true;
  2247. }
  2248. static bool
  2249. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2250. WASMModule *parent_module, const char *sub_module_name,
  2251. const char *table_name, WASMTableImport *table,
  2252. char *error_buf, uint32 error_buf_size)
  2253. {
  2254. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2255. uint32 declare_elem_type = 0, table_flag = 0, declare_init_size = 0,
  2256. declare_max_size = 0;
  2257. #if WASM_ENABLE_MULTI_MODULE != 0
  2258. WASMModule *sub_module = NULL;
  2259. WASMTable *linked_table = NULL;
  2260. #endif
  2261. #if WASM_ENABLE_GC != 0
  2262. WASMRefType ref_type;
  2263. bool need_ref_type_map;
  2264. #endif
  2265. bool is_table64 = false;
  2266. #if WASM_ENABLE_GC == 0
  2267. CHECK_BUF(p, p_end, 1);
  2268. /* 0x70 or 0x6F */
  2269. declare_elem_type = read_uint8(p);
  2270. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2271. #if WASM_ENABLE_REF_TYPES != 0
  2272. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2273. #endif
  2274. ) {
  2275. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2276. return false;
  2277. }
  2278. #else /* else of WASM_ENABLE_GC == 0 */
  2279. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2280. &need_ref_type_map, &ref_type, false, error_buf,
  2281. error_buf_size)) {
  2282. return false;
  2283. }
  2284. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2285. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2286. return false;
  2287. }
  2288. declare_elem_type = ref_type.ref_type;
  2289. if (need_ref_type_map) {
  2290. if (!(table->table_type.elem_ref_type =
  2291. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2292. error_buf, error_buf_size))) {
  2293. return false;
  2294. }
  2295. }
  2296. #if TRACE_WASM_LOADER != 0
  2297. os_printf("import table type: ");
  2298. wasm_dump_value_type(declare_elem_type, table->table_type.elem_ref_type);
  2299. os_printf("\n");
  2300. #endif
  2301. #endif /* end of WASM_ENABLE_GC == 0 */
  2302. p_org = p;
  2303. read_leb_uint32(p, p_end, table_flag);
  2304. is_table64 = table_flag & TABLE64_FLAG;
  2305. if (p - p_org > 1) {
  2306. LOG_VERBOSE("integer representation too long(import table)");
  2307. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2308. return false;
  2309. }
  2310. if (!wasm_table_check_flags(table_flag, error_buf, error_buf_size, false)) {
  2311. return false;
  2312. }
  2313. read_leb_uint32(p, p_end, declare_init_size);
  2314. if (table_flag & MAX_TABLE_SIZE_FLAG) {
  2315. read_leb_uint32(p, p_end, declare_max_size);
  2316. if (!check_table_max_size(declare_init_size, declare_max_size,
  2317. error_buf, error_buf_size))
  2318. return false;
  2319. }
  2320. adjust_table_max_size(is_table64, declare_init_size,
  2321. table_flag & MAX_TABLE_SIZE_FLAG, &declare_max_size);
  2322. *p_buf = p;
  2323. #if WASM_ENABLE_MULTI_MODULE != 0
  2324. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2325. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2326. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2327. error_buf_size);
  2328. if (sub_module) {
  2329. linked_table = wasm_loader_resolve_table(
  2330. sub_module_name, table_name, declare_init_size,
  2331. declare_max_size, error_buf, error_buf_size);
  2332. if (linked_table) {
  2333. /* reset with linked table limit */
  2334. declare_elem_type = linked_table->table_type.elem_type;
  2335. declare_init_size = linked_table->table_type.init_size;
  2336. declare_max_size = linked_table->table_type.max_size;
  2337. table_flag = linked_table->table_type.flags;
  2338. table->import_table_linked = linked_table;
  2339. table->import_module = sub_module;
  2340. }
  2341. }
  2342. }
  2343. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2344. /* (table (export "table") 10 20 funcref) */
  2345. /* (table (export "table64") 10 20 funcref) */
  2346. /* we need this section working in wamrc */
  2347. if (!strcmp("spectest", sub_module_name)) {
  2348. const uint32 spectest_table_init_size = 10;
  2349. const uint32 spectest_table_max_size = 20;
  2350. if (strcmp("table", table_name)
  2351. #if WASM_ENABLE_MEMORY64 != 0
  2352. && strcmp("table64", table_name)
  2353. #endif
  2354. ) {
  2355. set_error_buf(error_buf, error_buf_size,
  2356. "incompatible import type or unknown import");
  2357. return false;
  2358. }
  2359. if (declare_init_size > spectest_table_init_size
  2360. || declare_max_size < spectest_table_max_size) {
  2361. set_error_buf(error_buf, error_buf_size,
  2362. "incompatible import type");
  2363. return false;
  2364. }
  2365. declare_init_size = spectest_table_init_size;
  2366. declare_max_size = spectest_table_max_size;
  2367. }
  2368. /* now we believe all declaration are ok */
  2369. table->table_type.elem_type = declare_elem_type;
  2370. table->table_type.init_size = declare_init_size;
  2371. table->table_type.flags = table_flag;
  2372. table->table_type.max_size = declare_max_size;
  2373. #if WASM_ENABLE_WAMR_COMPILER != 0
  2374. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2375. parent_module->is_ref_types_used = true;
  2376. #endif
  2377. (void)parent_module;
  2378. return true;
  2379. fail:
  2380. return false;
  2381. }
  2382. static bool
  2383. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2384. uint32 error_buf_size)
  2385. {
  2386. uint32 default_max_size =
  2387. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2388. if (!is_memory64 && init_size > default_max_size) {
  2389. set_error_buf(error_buf, error_buf_size,
  2390. "memory size must be at most 65536 pages (4GiB)");
  2391. return false;
  2392. }
  2393. #if WASM_ENABLE_MEMORY64 != 0
  2394. else if (is_memory64 && init_size > default_max_size) {
  2395. set_error_buf(
  2396. error_buf, error_buf_size,
  2397. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2398. return false;
  2399. }
  2400. #endif
  2401. return true;
  2402. }
  2403. static bool
  2404. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2405. char *error_buf, uint32 error_buf_size)
  2406. {
  2407. uint32 default_max_size =
  2408. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2409. if (max_size < init_size) {
  2410. set_error_buf(error_buf, error_buf_size,
  2411. "size minimum must not be greater than maximum");
  2412. return false;
  2413. }
  2414. if (!is_memory64 && max_size > default_max_size) {
  2415. set_error_buf(error_buf, error_buf_size,
  2416. "memory size must be at most 65536 pages (4GiB)");
  2417. return false;
  2418. }
  2419. #if WASM_ENABLE_MEMORY64 != 0
  2420. else if (is_memory64 && max_size > default_max_size) {
  2421. set_error_buf(
  2422. error_buf, error_buf_size,
  2423. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2424. return false;
  2425. }
  2426. #endif
  2427. return true;
  2428. }
  2429. static bool
  2430. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2431. WASMModule *parent_module, const char *sub_module_name,
  2432. const char *memory_name, WASMMemoryImport *memory,
  2433. char *error_buf, uint32 error_buf_size)
  2434. {
  2435. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2436. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2437. uint32 pool_size = wasm_runtime_memory_pool_size();
  2438. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2439. / DEFAULT_NUM_BYTES_PER_PAGE;
  2440. #else
  2441. uint32 max_page_count;
  2442. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2443. uint32 mem_flag = 0;
  2444. bool is_memory64 = false;
  2445. uint32 declare_init_page_count = 0;
  2446. uint32 declare_max_page_count = 0;
  2447. #if WASM_ENABLE_MULTI_MODULE != 0
  2448. WASMModule *sub_module = NULL;
  2449. WASMMemory *linked_memory = NULL;
  2450. #endif
  2451. p_org = p;
  2452. read_leb_uint32(p, p_end, mem_flag);
  2453. is_memory64 = mem_flag & MEMORY64_FLAG;
  2454. if (p - p_org > 1) {
  2455. LOG_VERBOSE("integer representation too long(import memory)");
  2456. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2457. return false;
  2458. }
  2459. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2460. return false;
  2461. }
  2462. read_leb_uint32(p, p_end, declare_init_page_count);
  2463. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2464. error_buf_size)) {
  2465. return false;
  2466. }
  2467. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2468. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2469. #endif
  2470. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2471. read_leb_uint32(p, p_end, declare_max_page_count);
  2472. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2473. declare_max_page_count, error_buf,
  2474. error_buf_size)) {
  2475. return false;
  2476. }
  2477. if (declare_max_page_count > max_page_count) {
  2478. declare_max_page_count = max_page_count;
  2479. }
  2480. }
  2481. else {
  2482. /* Limit the maximum memory size to max_page_count */
  2483. declare_max_page_count = max_page_count;
  2484. }
  2485. #if WASM_ENABLE_MULTI_MODULE != 0
  2486. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2487. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2488. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2489. error_buf_size);
  2490. if (sub_module) {
  2491. linked_memory = wasm_loader_resolve_memory(
  2492. sub_module_name, memory_name, declare_init_page_count,
  2493. declare_max_page_count, error_buf, error_buf_size);
  2494. if (linked_memory) {
  2495. /**
  2496. * reset with linked memory limit
  2497. */
  2498. memory->import_module = sub_module;
  2499. memory->import_memory_linked = linked_memory;
  2500. declare_init_page_count = linked_memory->init_page_count;
  2501. declare_max_page_count = linked_memory->max_page_count;
  2502. }
  2503. }
  2504. }
  2505. #endif
  2506. /* (memory (export "memory") 1 2) */
  2507. if (!strcmp("spectest", sub_module_name)) {
  2508. uint32 spectest_memory_init_page = 1;
  2509. uint32 spectest_memory_max_page = 2;
  2510. if (strcmp("memory", memory_name)) {
  2511. set_error_buf(error_buf, error_buf_size,
  2512. "incompatible import type or unknown import");
  2513. return false;
  2514. }
  2515. if (declare_init_page_count > spectest_memory_init_page
  2516. || declare_max_page_count < spectest_memory_max_page) {
  2517. set_error_buf(error_buf, error_buf_size,
  2518. "incompatible import type");
  2519. return false;
  2520. }
  2521. declare_init_page_count = spectest_memory_init_page;
  2522. declare_max_page_count = spectest_memory_max_page;
  2523. }
  2524. #if WASM_ENABLE_WASI_TEST != 0
  2525. /* a case in wasi-testsuite which imports ("foo" "bar") */
  2526. else if (!strcmp("foo", sub_module_name)) {
  2527. uint32 spectest_memory_init_page = 1;
  2528. uint32 spectest_memory_max_page = 1;
  2529. if (strcmp("bar", memory_name)) {
  2530. set_error_buf(error_buf, error_buf_size,
  2531. "incompatible import type or unknown import");
  2532. return false;
  2533. }
  2534. if (declare_init_page_count > spectest_memory_init_page
  2535. || declare_max_page_count < spectest_memory_max_page) {
  2536. set_error_buf(error_buf, error_buf_size,
  2537. "incompatible import type");
  2538. return false;
  2539. }
  2540. declare_init_page_count = spectest_memory_init_page;
  2541. declare_max_page_count = spectest_memory_max_page;
  2542. }
  2543. #endif
  2544. /* now we believe all declaration are ok */
  2545. memory->mem_type.flags = mem_flag;
  2546. memory->mem_type.init_page_count = declare_init_page_count;
  2547. memory->mem_type.max_page_count = declare_max_page_count;
  2548. memory->mem_type.num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2549. *p_buf = p;
  2550. (void)parent_module;
  2551. return true;
  2552. fail:
  2553. return false;
  2554. }
  2555. #if WASM_ENABLE_TAGS != 0
  2556. static bool
  2557. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2558. const WASMModule *parent_module, /* this module ! */
  2559. const char *sub_module_name, const char *tag_name,
  2560. WASMTagImport *tag, /* structure to fill */
  2561. char *error_buf, uint32 error_buf_size)
  2562. {
  2563. /* attribute and type of the import statement */
  2564. uint8 declare_tag_attribute;
  2565. uint32 declare_type_index;
  2566. const uint8 *p = *p_buf, *p_end = buf_end;
  2567. #if WASM_ENABLE_MULTI_MODULE != 0
  2568. WASMModule *sub_module = NULL;
  2569. #endif
  2570. /* get the one byte attribute */
  2571. CHECK_BUF(p, p_end, 1);
  2572. declare_tag_attribute = read_uint8(p);
  2573. if (declare_tag_attribute != 0) {
  2574. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2575. goto fail;
  2576. }
  2577. /* get type */
  2578. read_leb_uint32(p, p_end, declare_type_index);
  2579. /* compare against module->types */
  2580. if (declare_type_index >= parent_module->type_count) {
  2581. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2582. goto fail;
  2583. }
  2584. WASMFuncType *declare_tag_type =
  2585. (WASMFuncType *)parent_module->types[declare_type_index];
  2586. /* check, that the type of the declared tag returns void */
  2587. if (declare_tag_type->result_count != 0) {
  2588. set_error_buf(error_buf, error_buf_size,
  2589. "tag type signature does not return void");
  2590. goto fail;
  2591. }
  2592. #if WASM_ENABLE_MULTI_MODULE != 0
  2593. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2594. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2595. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2596. error_buf_size);
  2597. if (sub_module) {
  2598. /* wasm_loader_resolve_tag checks, that the imported tag
  2599. * and the declared tag have the same type
  2600. */
  2601. uint32 linked_tag_index = 0;
  2602. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2603. sub_module_name, tag_name, declare_tag_type,
  2604. &linked_tag_index /* out */, error_buf, error_buf_size);
  2605. if (linked_tag) {
  2606. tag->import_module = sub_module;
  2607. tag->import_tag_linked = linked_tag;
  2608. tag->import_tag_index_linked = linked_tag_index;
  2609. }
  2610. }
  2611. }
  2612. #endif
  2613. /* store to module tag declarations */
  2614. tag->attribute = declare_tag_attribute;
  2615. tag->type = declare_type_index;
  2616. tag->module_name = (char *)sub_module_name;
  2617. tag->field_name = (char *)tag_name;
  2618. tag->tag_type = declare_tag_type;
  2619. *p_buf = p;
  2620. (void)parent_module;
  2621. LOG_VERBOSE("Load tag import success\n");
  2622. return true;
  2623. fail:
  2624. return false;
  2625. }
  2626. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2627. static bool
  2628. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2629. WASMModule *parent_module, char *sub_module_name,
  2630. char *global_name, WASMGlobalImport *global, char *error_buf,
  2631. uint32 error_buf_size)
  2632. {
  2633. const uint8 *p = *p_buf, *p_end = buf_end;
  2634. uint8 declare_type = 0;
  2635. uint8 declare_mutable = 0;
  2636. #if WASM_ENABLE_MULTI_MODULE != 0
  2637. WASMModule *sub_module = NULL;
  2638. WASMGlobal *linked_global = NULL;
  2639. #endif
  2640. #if WASM_ENABLE_GC != 0
  2641. WASMRefType ref_type;
  2642. bool need_ref_type_map;
  2643. #endif
  2644. bool ret = false;
  2645. #if WASM_ENABLE_GC == 0
  2646. CHECK_BUF(p, p_end, 2);
  2647. /* global type */
  2648. declare_type = read_uint8(p);
  2649. if (!is_valid_value_type_for_interpreter(declare_type)) {
  2650. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2651. return false;
  2652. }
  2653. declare_mutable = read_uint8(p);
  2654. #else
  2655. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2656. &need_ref_type_map, &ref_type, false, error_buf,
  2657. error_buf_size)) {
  2658. return false;
  2659. }
  2660. declare_type = ref_type.ref_type;
  2661. if (need_ref_type_map) {
  2662. if (!(global->ref_type =
  2663. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2664. error_buf, error_buf_size))) {
  2665. return false;
  2666. }
  2667. }
  2668. #if TRACE_WASM_LOADER != 0
  2669. os_printf("import global type: ");
  2670. wasm_dump_value_type(declare_type, global->ref_type);
  2671. os_printf("\n");
  2672. #endif
  2673. CHECK_BUF(p, p_end, 1);
  2674. declare_mutable = read_uint8(p);
  2675. #endif /* end of WASM_ENABLE_GC == 0 */
  2676. *p_buf = p;
  2677. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2678. return false;
  2679. }
  2680. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2681. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2682. global);
  2683. if (ret) {
  2684. if (global->type.val_type != declare_type
  2685. || global->type.is_mutable != declare_mutable) {
  2686. set_error_buf(error_buf, error_buf_size,
  2687. "incompatible import type");
  2688. return false;
  2689. }
  2690. global->is_linked = true;
  2691. }
  2692. #endif
  2693. #if WASM_ENABLE_MULTI_MODULE != 0
  2694. if (!global->is_linked
  2695. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2696. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2697. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2698. error_buf_size);
  2699. if (sub_module) {
  2700. /* check sub modules */
  2701. linked_global = wasm_loader_resolve_global(
  2702. sub_module_name, global_name, declare_type, declare_mutable,
  2703. error_buf, error_buf_size);
  2704. if (linked_global) {
  2705. global->import_module = sub_module;
  2706. global->import_global_linked = linked_global;
  2707. global->is_linked = true;
  2708. }
  2709. }
  2710. }
  2711. #endif
  2712. global->module_name = sub_module_name;
  2713. global->field_name = global_name;
  2714. global->type.val_type = declare_type;
  2715. global->type.is_mutable = (declare_mutable == 1);
  2716. #if WASM_ENABLE_WAMR_COMPILER != 0
  2717. if (global->type.val_type == VALUE_TYPE_V128)
  2718. parent_module->is_simd_used = true;
  2719. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2720. parent_module->is_ref_types_used = true;
  2721. #endif
  2722. (void)parent_module;
  2723. (void)ret;
  2724. return true;
  2725. fail:
  2726. return false;
  2727. }
  2728. static bool
  2729. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2730. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2731. {
  2732. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2733. #if WASM_ENABLE_GC != 0
  2734. WASMRefType ref_type;
  2735. bool need_ref_type_map;
  2736. #endif
  2737. bool is_table64 = false;
  2738. #if WASM_ENABLE_GC == 0
  2739. CHECK_BUF(p, p_end, 1);
  2740. /* 0x70 or 0x6F */
  2741. table->table_type.elem_type = read_uint8(p);
  2742. if (VALUE_TYPE_FUNCREF != table->table_type.elem_type
  2743. #if WASM_ENABLE_REF_TYPES != 0
  2744. && VALUE_TYPE_EXTERNREF != table->table_type.elem_type
  2745. #endif
  2746. ) {
  2747. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2748. return false;
  2749. }
  2750. #else /* else of WASM_ENABLE_GC == 0 */
  2751. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2752. &need_ref_type_map, &ref_type, false, error_buf,
  2753. error_buf_size)) {
  2754. return false;
  2755. }
  2756. table->table_type.elem_type = ref_type.ref_type;
  2757. if (need_ref_type_map) {
  2758. if (!(table->table_type.elem_ref_type =
  2759. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2760. error_buf_size))) {
  2761. return false;
  2762. }
  2763. }
  2764. #if TRACE_WASM_LOADER != 0
  2765. os_printf("table type: ");
  2766. wasm_dump_value_type(table->table_type.elem_type,
  2767. table->table_type.elem_ref_type);
  2768. os_printf("\n");
  2769. #endif
  2770. #endif /* end of WASM_ENABLE_GC == 0 */
  2771. p_org = p;
  2772. read_leb_uint32(p, p_end, table->table_type.flags);
  2773. is_table64 = table->table_type.flags & TABLE64_FLAG;
  2774. if (p - p_org > 1) {
  2775. LOG_VERBOSE("integer representation too long(table)");
  2776. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2777. return false;
  2778. }
  2779. if (!wasm_table_check_flags(table->table_type.flags, error_buf,
  2780. error_buf_size, false)) {
  2781. return false;
  2782. }
  2783. read_leb_uint32(p, p_end, table->table_type.init_size);
  2784. if (table->table_type.flags & MAX_TABLE_SIZE_FLAG) {
  2785. read_leb_uint32(p, p_end, table->table_type.max_size);
  2786. if (!check_table_max_size(table->table_type.init_size,
  2787. table->table_type.max_size, error_buf,
  2788. error_buf_size))
  2789. return false;
  2790. }
  2791. adjust_table_max_size(is_table64, table->table_type.init_size,
  2792. table->table_type.flags & MAX_TABLE_SIZE_FLAG,
  2793. &table->table_type.max_size);
  2794. #if WASM_ENABLE_WAMR_COMPILER != 0
  2795. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  2796. module->is_ref_types_used = true;
  2797. #endif
  2798. *p_buf = p;
  2799. return true;
  2800. fail:
  2801. return false;
  2802. }
  2803. static bool
  2804. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2805. char *error_buf, uint32 error_buf_size)
  2806. {
  2807. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2808. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2809. uint32 pool_size = wasm_runtime_memory_pool_size();
  2810. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2811. / DEFAULT_NUM_BYTES_PER_PAGE;
  2812. #else
  2813. uint32 max_page_count;
  2814. #endif
  2815. bool is_memory64 = false;
  2816. p_org = p;
  2817. read_leb_uint32(p, p_end, memory->flags);
  2818. is_memory64 = memory->flags & MEMORY64_FLAG;
  2819. if (p - p_org > 1) {
  2820. LOG_VERBOSE("integer representation too long(memory)");
  2821. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2822. return false;
  2823. }
  2824. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2825. false)) {
  2826. return false;
  2827. }
  2828. read_leb_uint32(p, p_end, memory->init_page_count);
  2829. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2830. error_buf_size))
  2831. return false;
  2832. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2833. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2834. #endif
  2835. if (memory->flags & 1) {
  2836. read_leb_uint32(p, p_end, memory->max_page_count);
  2837. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2838. memory->max_page_count, error_buf,
  2839. error_buf_size))
  2840. return false;
  2841. if (memory->max_page_count > max_page_count)
  2842. memory->max_page_count = max_page_count;
  2843. }
  2844. else {
  2845. /* Limit the maximum memory size to max_page_count */
  2846. memory->max_page_count = max_page_count;
  2847. }
  2848. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2849. *p_buf = p;
  2850. return true;
  2851. fail:
  2852. return false;
  2853. }
  2854. static int
  2855. cmp_export_name(const void *a, const void *b)
  2856. {
  2857. return strcmp(*(char **)a, *(char **)b);
  2858. }
  2859. static bool
  2860. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2861. bool is_load_from_file_buf, bool no_resolve,
  2862. char *error_buf, uint32 error_buf_size)
  2863. {
  2864. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2865. uint32 import_count, name_len, type_index, i, u32, flags;
  2866. uint64 total_size;
  2867. WASMImport *import;
  2868. WASMImport *import_functions = NULL, *import_tables = NULL;
  2869. WASMImport *import_memories = NULL, *import_globals = NULL;
  2870. #if WASM_ENABLE_TAGS != 0
  2871. WASMImport *import_tags = NULL;
  2872. #endif
  2873. char *sub_module_name, *field_name;
  2874. uint8 u8, kind, global_type;
  2875. read_leb_uint32(p, p_end, import_count);
  2876. if (import_count) {
  2877. module->import_count = import_count;
  2878. total_size = sizeof(WASMImport) * (uint64)import_count;
  2879. if (!(module->imports =
  2880. loader_malloc(total_size, error_buf, error_buf_size))) {
  2881. return false;
  2882. }
  2883. p_old = p;
  2884. /* Scan firstly to get import count of each type */
  2885. for (i = 0; i < import_count; i++) {
  2886. /* module name */
  2887. read_leb_uint32(p, p_end, name_len);
  2888. CHECK_BUF(p, p_end, name_len);
  2889. p += name_len;
  2890. /* field name */
  2891. read_leb_uint32(p, p_end, name_len);
  2892. CHECK_BUF(p, p_end, name_len);
  2893. p += name_len;
  2894. CHECK_BUF(p, p_end, 1);
  2895. /* 0x00/0x01/0x02/0x03/0x04 */
  2896. kind = read_uint8(p);
  2897. switch (kind) {
  2898. case IMPORT_KIND_FUNC: /* import function */
  2899. read_leb_uint32(p, p_end, type_index);
  2900. module->import_function_count++;
  2901. break;
  2902. case IMPORT_KIND_TABLE: /* import table */
  2903. CHECK_BUF(p, p_end, 1);
  2904. /* 0x70 */
  2905. u8 = read_uint8(p);
  2906. read_leb_uint32(p, p_end, flags);
  2907. read_leb_uint32(p, p_end, u32);
  2908. if (flags & 1)
  2909. read_leb_uint32(p, p_end, u32);
  2910. module->import_table_count++;
  2911. if (module->import_table_count > 1) {
  2912. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2913. set_error_buf(error_buf, error_buf_size,
  2914. "multiple tables");
  2915. return false;
  2916. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2917. module->is_ref_types_used = true;
  2918. #endif
  2919. }
  2920. break;
  2921. case IMPORT_KIND_MEMORY: /* import memory */
  2922. read_leb_uint32(p, p_end, flags);
  2923. read_leb_uint32(p, p_end, u32);
  2924. if (flags & 1)
  2925. read_leb_uint32(p, p_end, u32);
  2926. module->import_memory_count++;
  2927. #if WASM_ENABLE_MULTI_MEMORY == 0
  2928. if (module->import_memory_count > 1) {
  2929. set_error_buf(error_buf, error_buf_size,
  2930. "multiple memories");
  2931. return false;
  2932. }
  2933. #endif
  2934. break;
  2935. #if WASM_ENABLE_TAGS != 0
  2936. case IMPORT_KIND_TAG: /* import tags */
  2937. /* it only counts the number of tags to import */
  2938. module->import_tag_count++;
  2939. CHECK_BUF(p, p_end, 1);
  2940. u8 = read_uint8(p);
  2941. read_leb_uint32(p, p_end, type_index);
  2942. break;
  2943. #endif
  2944. case IMPORT_KIND_GLOBAL: /* import global */
  2945. #if WASM_ENABLE_GC != 0
  2946. /* valtype */
  2947. CHECK_BUF(p, p_end, 1);
  2948. global_type = read_uint8(p);
  2949. if (wasm_is_type_multi_byte_type(global_type)) {
  2950. int32 heap_type;
  2951. read_leb_int32(p, p_end, heap_type);
  2952. (void)heap_type;
  2953. }
  2954. /* mutability */
  2955. CHECK_BUF(p, p_end, 1);
  2956. p += 1;
  2957. #else
  2958. CHECK_BUF(p, p_end, 2);
  2959. p += 2;
  2960. #endif
  2961. (void)global_type;
  2962. module->import_global_count++;
  2963. break;
  2964. default:
  2965. set_error_buf(error_buf, error_buf_size,
  2966. "invalid import kind");
  2967. return false;
  2968. }
  2969. }
  2970. if (module->import_function_count)
  2971. import_functions = module->import_functions = module->imports;
  2972. if (module->import_table_count)
  2973. import_tables = module->import_tables =
  2974. module->imports + module->import_function_count;
  2975. if (module->import_memory_count)
  2976. import_memories = module->import_memories =
  2977. module->imports + module->import_function_count
  2978. + module->import_table_count;
  2979. #if WASM_ENABLE_TAGS != 0
  2980. if (module->import_tag_count)
  2981. import_tags = module->import_tags =
  2982. module->imports + module->import_function_count
  2983. + module->import_table_count + module->import_memory_count;
  2984. if (module->import_global_count)
  2985. import_globals = module->import_globals =
  2986. module->imports + module->import_function_count
  2987. + module->import_table_count + module->import_memory_count
  2988. + module->import_tag_count;
  2989. #else
  2990. if (module->import_global_count)
  2991. import_globals = module->import_globals =
  2992. module->imports + module->import_function_count
  2993. + module->import_table_count + module->import_memory_count;
  2994. #endif
  2995. p = p_old;
  2996. /* Scan again to resolve the data */
  2997. for (i = 0; i < import_count; i++) {
  2998. /* load module name */
  2999. read_leb_uint32(p, p_end, name_len);
  3000. CHECK_BUF(p, p_end, name_len);
  3001. if (!(sub_module_name = wasm_const_str_list_insert(
  3002. p, name_len, module, is_load_from_file_buf, error_buf,
  3003. error_buf_size))) {
  3004. return false;
  3005. }
  3006. p += name_len;
  3007. /* load field name */
  3008. read_leb_uint32(p, p_end, name_len);
  3009. CHECK_BUF(p, p_end, name_len);
  3010. if (!(field_name = wasm_const_str_list_insert(
  3011. p, name_len, module, is_load_from_file_buf, error_buf,
  3012. error_buf_size))) {
  3013. return false;
  3014. }
  3015. p += name_len;
  3016. CHECK_BUF(p, p_end, 1);
  3017. /* 0x00/0x01/0x02/0x03/0x4 */
  3018. kind = read_uint8(p);
  3019. switch (kind) {
  3020. case IMPORT_KIND_FUNC: /* import function */
  3021. bh_assert(import_functions);
  3022. import = import_functions++;
  3023. if (!load_function_import(&p, p_end, module,
  3024. sub_module_name, field_name,
  3025. &import->u.function, no_resolve,
  3026. error_buf, error_buf_size)) {
  3027. return false;
  3028. }
  3029. break;
  3030. case IMPORT_KIND_TABLE: /* import table */
  3031. bh_assert(import_tables);
  3032. import = import_tables++;
  3033. if (!load_table_import(&p, p_end, module, sub_module_name,
  3034. field_name, &import->u.table,
  3035. error_buf, error_buf_size)) {
  3036. LOG_DEBUG("can not import such a table (%s,%s)",
  3037. sub_module_name, field_name);
  3038. return false;
  3039. }
  3040. break;
  3041. case IMPORT_KIND_MEMORY: /* import memory */
  3042. bh_assert(import_memories);
  3043. import = import_memories++;
  3044. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3045. field_name, &import->u.memory,
  3046. error_buf, error_buf_size)) {
  3047. return false;
  3048. }
  3049. break;
  3050. #if WASM_ENABLE_TAGS != 0
  3051. case IMPORT_KIND_TAG:
  3052. bh_assert(import_tags);
  3053. import = import_tags++;
  3054. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3055. field_name, &import->u.tag, error_buf,
  3056. error_buf_size)) {
  3057. return false;
  3058. }
  3059. break;
  3060. #endif
  3061. case IMPORT_KIND_GLOBAL: /* import global */
  3062. bh_assert(import_globals);
  3063. import = import_globals++;
  3064. if (!load_global_import(&p, p_end, module, sub_module_name,
  3065. field_name, &import->u.global,
  3066. error_buf, error_buf_size)) {
  3067. return false;
  3068. }
  3069. break;
  3070. default:
  3071. set_error_buf(error_buf, error_buf_size,
  3072. "invalid import kind");
  3073. return false;
  3074. }
  3075. import->kind = kind;
  3076. import->u.names.module_name = sub_module_name;
  3077. import->u.names.field_name = field_name;
  3078. }
  3079. #if WASM_ENABLE_LIBC_WASI != 0
  3080. import = module->import_functions;
  3081. for (i = 0; i < module->import_function_count; i++, import++) {
  3082. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3083. || !strcmp(import->u.names.module_name,
  3084. "wasi_snapshot_preview1")) {
  3085. module->import_wasi_api = true;
  3086. break;
  3087. }
  3088. }
  3089. #endif
  3090. }
  3091. if (p != p_end) {
  3092. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3093. return false;
  3094. }
  3095. LOG_VERBOSE("Load import section success.\n");
  3096. (void)u8;
  3097. (void)u32;
  3098. (void)type_index;
  3099. return true;
  3100. fail:
  3101. return false;
  3102. }
  3103. static bool
  3104. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3105. uint32 error_buf_size)
  3106. {
  3107. WASMFuncType *param_type = func->func_type;
  3108. uint32 param_count = param_type->param_count;
  3109. uint8 *param_types = param_type->types;
  3110. uint32 local_count = func->local_count;
  3111. uint8 *local_types = func->local_types;
  3112. uint32 i, local_offset = 0;
  3113. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3114. /*
  3115. * Only allocate memory when total_size is not 0,
  3116. * or the return value of malloc(0) might be NULL on some platforms,
  3117. * which causes wasm loader return false.
  3118. */
  3119. if (total_size > 0
  3120. && !(func->local_offsets =
  3121. loader_malloc(total_size, error_buf, error_buf_size))) {
  3122. return false;
  3123. }
  3124. for (i = 0; i < param_count; i++) {
  3125. func->local_offsets[i] = (uint16)local_offset;
  3126. local_offset += wasm_value_type_cell_num(param_types[i]);
  3127. }
  3128. for (i = 0; i < local_count; i++) {
  3129. func->local_offsets[param_count + i] = (uint16)local_offset;
  3130. local_offset += wasm_value_type_cell_num(local_types[i]);
  3131. }
  3132. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3133. return true;
  3134. }
  3135. static bool
  3136. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3137. const uint8 *buf_code, const uint8 *buf_code_end,
  3138. WASMModule *module, char *error_buf,
  3139. uint32 error_buf_size)
  3140. {
  3141. const uint8 *p = buf, *p_end = buf_end;
  3142. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3143. uint32 func_count;
  3144. uint64 total_size;
  3145. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3146. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3147. uint8 type;
  3148. WASMFunction *func;
  3149. #if WASM_ENABLE_GC != 0
  3150. bool need_ref_type_map;
  3151. WASMRefType ref_type;
  3152. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3153. #endif
  3154. read_leb_uint32(p, p_end, func_count);
  3155. if (buf_code)
  3156. read_leb_uint32(p_code, buf_code_end, code_count);
  3157. if (func_count != code_count) {
  3158. set_error_buf(error_buf, error_buf_size,
  3159. "function and code section have inconsistent lengths or "
  3160. "unexpected end");
  3161. return false;
  3162. }
  3163. if (is_indices_overflow(module->import_function_count, func_count,
  3164. error_buf, error_buf_size))
  3165. return false;
  3166. if (func_count) {
  3167. module->function_count = func_count;
  3168. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3169. if (!(module->functions =
  3170. loader_malloc(total_size, error_buf, error_buf_size))) {
  3171. return false;
  3172. }
  3173. for (i = 0; i < func_count; i++) {
  3174. /* Resolve function type */
  3175. read_leb_uint32(p, p_end, type_index);
  3176. if (type_index >= module->type_count) {
  3177. set_error_buf(error_buf, error_buf_size, "unknown type");
  3178. return false;
  3179. }
  3180. #if WASM_ENABLE_GC != 0
  3181. type_index_org = type_index;
  3182. #endif
  3183. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3184. && WASM_ENABLE_GC == 0
  3185. type_index = wasm_get_smallest_type_idx(
  3186. module->types, module->type_count, type_index);
  3187. #endif
  3188. read_leb_uint32(p_code, buf_code_end, code_size);
  3189. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3190. set_error_buf(error_buf, error_buf_size,
  3191. "invalid function code size");
  3192. return false;
  3193. }
  3194. /* Resolve local set count */
  3195. p_code_end = p_code + code_size;
  3196. local_count = 0;
  3197. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3198. p_code_save = p_code;
  3199. #if WASM_ENABLE_GC != 0
  3200. ref_type_map_count = 0;
  3201. #endif
  3202. /* Calculate total local count */
  3203. for (j = 0; j < local_set_count; j++) {
  3204. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3205. if (sub_local_count > UINT32_MAX - local_count) {
  3206. set_error_buf(error_buf, error_buf_size, "too many locals");
  3207. return false;
  3208. }
  3209. #if WASM_ENABLE_GC == 0
  3210. CHECK_BUF(p_code, buf_code_end, 1);
  3211. /* 0x7F/0x7E/0x7D/0x7C */
  3212. type = read_uint8(p_code);
  3213. local_count += sub_local_count;
  3214. #if WASM_ENABLE_WAMR_COMPILER != 0
  3215. /* If any value's type is v128, mark the module as SIMD used */
  3216. if (type == VALUE_TYPE_V128)
  3217. module->is_simd_used = true;
  3218. #endif
  3219. #else
  3220. if (!resolve_value_type(&p_code, buf_code_end, module,
  3221. module->type_count, &need_ref_type_map,
  3222. &ref_type, false, error_buf,
  3223. error_buf_size)) {
  3224. return false;
  3225. }
  3226. local_count += sub_local_count;
  3227. if (need_ref_type_map)
  3228. ref_type_map_count += sub_local_count;
  3229. #endif
  3230. }
  3231. /* Code size in code entry can't be smaller than size of vec(locals)
  3232. * + expr(at least 1 for opcode end). And expressions are encoded by
  3233. * their instruction sequence terminated with an explicit 0x0B
  3234. * opcode for end. */
  3235. if (p_code_end <= p_code || *(p_code_end - 1) != WASM_OP_END) {
  3236. set_error_buf(
  3237. error_buf, error_buf_size,
  3238. "section size mismatch: function body END opcode expected");
  3239. return false;
  3240. }
  3241. /* Alloc memory, layout: function structure + local types */
  3242. code_size = (uint32)(p_code_end - p_code);
  3243. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3244. if (!(func = module->functions[i] =
  3245. loader_malloc(total_size, error_buf, error_buf_size))) {
  3246. return false;
  3247. }
  3248. #if WASM_ENABLE_GC != 0
  3249. if (ref_type_map_count > 0) {
  3250. total_size =
  3251. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3252. if (!(func->local_ref_type_maps = loader_malloc(
  3253. total_size, error_buf, error_buf_size))) {
  3254. return false;
  3255. }
  3256. func->local_ref_type_map_count = ref_type_map_count;
  3257. }
  3258. #endif
  3259. /* Set function type, local count, code size and code body */
  3260. func->func_type = (WASMFuncType *)module->types[type_index];
  3261. func->local_count = local_count;
  3262. if (local_count > 0)
  3263. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3264. func->code_size = code_size;
  3265. /*
  3266. * we shall make a copy of code body [p_code, p_code + code_size]
  3267. * when we are worrying about inappropriate releasing behaviour.
  3268. * all code bodies are actually in a buffer which user allocates in
  3269. * his embedding environment and we don't have power on them.
  3270. * it will be like:
  3271. * code_body_cp = malloc(code_size);
  3272. * memcpy(code_body_cp, p_code, code_size);
  3273. * func->code = code_body_cp;
  3274. */
  3275. func->code = (uint8 *)p_code;
  3276. #if WASM_ENABLE_GC != 0
  3277. func->type_idx = type_index_org;
  3278. #endif
  3279. #if WASM_ENABLE_GC != 0
  3280. t = 0;
  3281. #endif
  3282. /* Load each local type */
  3283. p_code = p_code_save;
  3284. local_type_index = 0;
  3285. for (j = 0; j < local_set_count; j++) {
  3286. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3287. /* Note: sub_local_count is allowed to be 0 */
  3288. if (local_type_index > UINT32_MAX - sub_local_count
  3289. || local_type_index + sub_local_count > local_count) {
  3290. set_error_buf(error_buf, error_buf_size,
  3291. "invalid local count");
  3292. return false;
  3293. }
  3294. #if WASM_ENABLE_GC == 0
  3295. CHECK_BUF(p_code, buf_code_end, 1);
  3296. /* 0x7F/0x7E/0x7D/0x7C */
  3297. type = read_uint8(p_code);
  3298. if (!is_valid_value_type_for_interpreter(type)) {
  3299. if (type == VALUE_TYPE_V128)
  3300. set_error_buf(error_buf, error_buf_size,
  3301. "v128 value type requires simd feature");
  3302. else if (type == VALUE_TYPE_FUNCREF
  3303. || type == VALUE_TYPE_EXTERNREF)
  3304. set_error_buf(error_buf, error_buf_size,
  3305. "ref value type requires "
  3306. "reference types feature");
  3307. else
  3308. set_error_buf_v(error_buf, error_buf_size,
  3309. "invalid local type 0x%02X", type);
  3310. return false;
  3311. }
  3312. #else
  3313. if (!resolve_value_type(&p_code, buf_code_end, module,
  3314. module->type_count, &need_ref_type_map,
  3315. &ref_type, false, error_buf,
  3316. error_buf_size)) {
  3317. return false;
  3318. }
  3319. if (need_ref_type_map) {
  3320. WASMRefType *ref_type_tmp;
  3321. if (!(ref_type_tmp = reftype_set_insert(
  3322. module->ref_type_set, &ref_type, error_buf,
  3323. error_buf_size))) {
  3324. return false;
  3325. }
  3326. for (k = 0; k < sub_local_count; k++) {
  3327. func->local_ref_type_maps[t + k].ref_type =
  3328. ref_type_tmp;
  3329. func->local_ref_type_maps[t + k].index =
  3330. local_type_index + k;
  3331. }
  3332. t += sub_local_count;
  3333. }
  3334. type = ref_type.ref_type;
  3335. #endif
  3336. for (k = 0; k < sub_local_count; k++) {
  3337. func->local_types[local_type_index++] = type;
  3338. }
  3339. #if WASM_ENABLE_WAMR_COMPILER != 0
  3340. if (type == VALUE_TYPE_V128)
  3341. module->is_simd_used = true;
  3342. else if (type == VALUE_TYPE_FUNCREF
  3343. || type == VALUE_TYPE_EXTERNREF)
  3344. module->is_ref_types_used = true;
  3345. #endif
  3346. }
  3347. bh_assert(local_type_index == func->local_count);
  3348. #if WASM_ENABLE_GC != 0
  3349. bh_assert(t == func->local_ref_type_map_count);
  3350. #if TRACE_WASM_LOADER != 0
  3351. os_printf("func %u, local types: [", i);
  3352. k = 0;
  3353. for (j = 0; j < func->local_count; j++) {
  3354. WASMRefType *ref_type_tmp = NULL;
  3355. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3356. bh_assert(j == func->local_ref_type_maps[k].index);
  3357. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3358. }
  3359. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3360. if (j < func->local_count - 1)
  3361. os_printf(" ");
  3362. }
  3363. os_printf("]\n");
  3364. #endif
  3365. #endif
  3366. func->param_cell_num = func->func_type->param_cell_num;
  3367. func->ret_cell_num = func->func_type->ret_cell_num;
  3368. local_cell_num =
  3369. wasm_get_cell_num(func->local_types, func->local_count);
  3370. if (local_cell_num > UINT16_MAX) {
  3371. set_error_buf(error_buf, error_buf_size,
  3372. "local count too large");
  3373. return false;
  3374. }
  3375. func->local_cell_num = (uint16)local_cell_num;
  3376. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3377. return false;
  3378. p_code = p_code_end;
  3379. }
  3380. }
  3381. if (p != p_end) {
  3382. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3383. return false;
  3384. }
  3385. LOG_VERBOSE("Load function section success.\n");
  3386. return true;
  3387. fail:
  3388. return false;
  3389. }
  3390. static bool
  3391. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3392. char *error_buf, uint32 error_buf_size)
  3393. {
  3394. const uint8 *p = buf, *p_end = buf_end;
  3395. uint32 table_count, i;
  3396. uint64 total_size;
  3397. WASMTable *table;
  3398. read_leb_uint32(p, p_end, table_count);
  3399. if (module->import_table_count + table_count > 1) {
  3400. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3401. /* a total of one table is allowed */
  3402. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3403. return false;
  3404. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3405. module->is_ref_types_used = true;
  3406. #endif
  3407. }
  3408. if (table_count) {
  3409. module->table_count = table_count;
  3410. total_size = sizeof(WASMTable) * (uint64)table_count;
  3411. if (!(module->tables =
  3412. loader_malloc(total_size, error_buf, error_buf_size))) {
  3413. return false;
  3414. }
  3415. /* load each table */
  3416. table = module->tables;
  3417. for (i = 0; i < table_count; i++, table++) {
  3418. #if WASM_ENABLE_GC != 0
  3419. uint8 flag;
  3420. bool has_init = false;
  3421. CHECK_BUF(buf, buf_end, 1);
  3422. flag = read_uint8(p);
  3423. if (flag == TABLE_INIT_EXPR_FLAG) {
  3424. CHECK_BUF(buf, buf_end, 1);
  3425. flag = read_uint8(p);
  3426. if (flag != 0x00) {
  3427. set_error_buf(error_buf, error_buf_size,
  3428. "invalid leading bytes for table");
  3429. return false;
  3430. }
  3431. has_init = true;
  3432. }
  3433. else {
  3434. p--;
  3435. }
  3436. #endif /* end of WASM_ENABLE_GC != 0 */
  3437. if (!load_table(&p, p_end, module, table, error_buf,
  3438. error_buf_size))
  3439. return false;
  3440. #if WASM_ENABLE_GC != 0
  3441. if (has_init) {
  3442. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3443. table->table_type.elem_type,
  3444. table->table_type.elem_ref_type, error_buf,
  3445. error_buf_size))
  3446. return false;
  3447. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3448. && table->init_expr.init_expr_type
  3449. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3450. set_error_buf(
  3451. error_buf, error_buf_size,
  3452. "unsupported initializer expression for table");
  3453. return false;
  3454. }
  3455. }
  3456. else {
  3457. if (wasm_is_reftype_htref_non_nullable(
  3458. table->table_type.elem_type)) {
  3459. set_error_buf(
  3460. error_buf, error_buf_size,
  3461. "type mismatch: non-nullable table without init expr");
  3462. return false;
  3463. }
  3464. }
  3465. #endif /* end of WASM_ENABLE_GC != 0 */
  3466. #if WASM_ENABLE_WAMR_COMPILER != 0
  3467. if (table->table_type.elem_type == VALUE_TYPE_EXTERNREF)
  3468. module->is_ref_types_used = true;
  3469. #endif
  3470. }
  3471. }
  3472. if (p != p_end) {
  3473. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3474. return false;
  3475. }
  3476. LOG_VERBOSE("Load table section success.\n");
  3477. return true;
  3478. fail:
  3479. return false;
  3480. }
  3481. static bool
  3482. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3483. char *error_buf, uint32 error_buf_size)
  3484. {
  3485. const uint8 *p = buf, *p_end = buf_end;
  3486. uint32 memory_count, i;
  3487. uint64 total_size;
  3488. WASMMemory *memory;
  3489. read_leb_uint32(p, p_end, memory_count);
  3490. #if WASM_ENABLE_MULTI_MEMORY == 0
  3491. /* a total of one memory is allowed */
  3492. if (module->import_memory_count + memory_count > 1) {
  3493. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3494. return false;
  3495. }
  3496. #endif
  3497. if (memory_count) {
  3498. module->memory_count = memory_count;
  3499. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3500. if (!(module->memories =
  3501. loader_malloc(total_size, error_buf, error_buf_size))) {
  3502. return false;
  3503. }
  3504. /* load each memory */
  3505. memory = module->memories;
  3506. for (i = 0; i < memory_count; i++, memory++)
  3507. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3508. return false;
  3509. }
  3510. if (p != p_end) {
  3511. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3512. return false;
  3513. }
  3514. LOG_VERBOSE("Load memory section success.\n");
  3515. return true;
  3516. fail:
  3517. return false;
  3518. }
  3519. static bool
  3520. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3521. char *error_buf, uint32 error_buf_size)
  3522. {
  3523. const uint8 *p = buf, *p_end = buf_end;
  3524. uint32 global_count, i;
  3525. uint64 total_size;
  3526. WASMGlobal *global;
  3527. uint8 mutable;
  3528. #if WASM_ENABLE_GC != 0
  3529. bool need_ref_type_map;
  3530. WASMRefType ref_type;
  3531. #endif
  3532. read_leb_uint32(p, p_end, global_count);
  3533. if (is_indices_overflow(module->import_global_count, global_count,
  3534. error_buf, error_buf_size))
  3535. return false;
  3536. module->global_count = 0;
  3537. if (global_count) {
  3538. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3539. if (!(module->globals =
  3540. loader_malloc(total_size, error_buf, error_buf_size))) {
  3541. return false;
  3542. }
  3543. global = module->globals;
  3544. for (i = 0; i < global_count; i++, global++) {
  3545. #if WASM_ENABLE_GC == 0
  3546. CHECK_BUF(p, p_end, 2);
  3547. /* global type */
  3548. global->type.val_type = read_uint8(p);
  3549. if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
  3550. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3551. return false;
  3552. }
  3553. mutable = read_uint8(p);
  3554. #else
  3555. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3556. &need_ref_type_map, &ref_type, false,
  3557. error_buf, error_buf_size)) {
  3558. return false;
  3559. }
  3560. global->type.val_type = ref_type.ref_type;
  3561. CHECK_BUF(p, p_end, 1);
  3562. mutable = read_uint8(p);
  3563. #endif /* end of WASM_ENABLE_GC */
  3564. #if WASM_ENABLE_WAMR_COMPILER != 0
  3565. if (global->type.val_type == VALUE_TYPE_V128)
  3566. module->is_simd_used = true;
  3567. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3568. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3569. module->is_ref_types_used = true;
  3570. #endif
  3571. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3572. return false;
  3573. }
  3574. global->type.is_mutable = mutable ? true : false;
  3575. /* initialize expression */
  3576. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3577. global->type.val_type,
  3578. #if WASM_ENABLE_GC == 0
  3579. NULL,
  3580. #else
  3581. &ref_type,
  3582. #endif
  3583. error_buf, error_buf_size))
  3584. return false;
  3585. #if WASM_ENABLE_GC != 0
  3586. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3587. uint8 global_type;
  3588. WASMRefType *global_ref_type;
  3589. uint32 global_idx = global->init_expr.u.global_index;
  3590. if (global->init_expr.u.global_index
  3591. >= module->import_global_count + i) {
  3592. set_error_buf(error_buf, error_buf_size, "unknown global");
  3593. return false;
  3594. }
  3595. if (global_idx < module->import_global_count) {
  3596. global_type = module->import_globals[global_idx]
  3597. .u.global.type.val_type;
  3598. global_ref_type =
  3599. module->import_globals[global_idx].u.global.ref_type;
  3600. }
  3601. else {
  3602. global_type =
  3603. module
  3604. ->globals[global_idx - module->import_global_count]
  3605. .type.val_type;
  3606. global_ref_type =
  3607. module
  3608. ->globals[global_idx - module->import_global_count]
  3609. .ref_type;
  3610. }
  3611. if (!wasm_reftype_is_subtype_of(
  3612. global_type, global_ref_type, global->type.val_type,
  3613. global->ref_type, module->types, module->type_count)) {
  3614. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3615. return false;
  3616. }
  3617. }
  3618. if (need_ref_type_map) {
  3619. if (!(global->ref_type =
  3620. reftype_set_insert(module->ref_type_set, &ref_type,
  3621. error_buf, error_buf_size))) {
  3622. return false;
  3623. }
  3624. }
  3625. #if TRACE_WASM_LOADER != 0
  3626. os_printf("global type: ");
  3627. wasm_dump_value_type(global->type, global->ref_type);
  3628. os_printf("\n");
  3629. #endif
  3630. #endif
  3631. module->global_count++;
  3632. }
  3633. bh_assert(module->global_count == global_count);
  3634. }
  3635. if (p != p_end) {
  3636. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3637. return false;
  3638. }
  3639. LOG_VERBOSE("Load global section success.\n");
  3640. return true;
  3641. fail:
  3642. return false;
  3643. }
  3644. static bool
  3645. check_duplicate_exports(WASMModule *module, char *error_buf,
  3646. uint32 error_buf_size)
  3647. {
  3648. uint32 i;
  3649. bool result = false;
  3650. char *names_buf[32], **names = names_buf;
  3651. if (module->export_count > 32) {
  3652. names = loader_malloc(module->export_count * sizeof(char *), error_buf,
  3653. error_buf_size);
  3654. if (!names) {
  3655. return result;
  3656. }
  3657. }
  3658. for (i = 0; i < module->export_count; i++) {
  3659. names[i] = module->exports[i].name;
  3660. }
  3661. qsort(names, module->export_count, sizeof(char *), cmp_export_name);
  3662. for (i = 1; i < module->export_count; i++) {
  3663. if (!strcmp(names[i], names[i - 1])) {
  3664. set_error_buf(error_buf, error_buf_size, "duplicate export name");
  3665. goto cleanup;
  3666. }
  3667. }
  3668. result = true;
  3669. cleanup:
  3670. if (module->export_count > 32) {
  3671. wasm_runtime_free(names);
  3672. }
  3673. return result;
  3674. }
  3675. static bool
  3676. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3677. bool is_load_from_file_buf, char *error_buf,
  3678. uint32 error_buf_size)
  3679. {
  3680. const uint8 *p = buf, *p_end = buf_end;
  3681. uint32 export_count, i, index;
  3682. uint64 total_size;
  3683. uint32 str_len;
  3684. WASMExport *export;
  3685. read_leb_uint32(p, p_end, export_count);
  3686. if (export_count) {
  3687. module->export_count = export_count;
  3688. total_size = sizeof(WASMExport) * (uint64)export_count;
  3689. if (!(module->exports =
  3690. loader_malloc(total_size, error_buf, error_buf_size))) {
  3691. return false;
  3692. }
  3693. export = module->exports;
  3694. for (i = 0; i < export_count; i++, export ++) {
  3695. #if WASM_ENABLE_THREAD_MGR == 0
  3696. if (p == p_end) {
  3697. /* export section with inconsistent count:
  3698. n export declared, but less than n given */
  3699. set_error_buf(error_buf, error_buf_size,
  3700. "length out of bounds");
  3701. return false;
  3702. }
  3703. #endif
  3704. read_leb_uint32(p, p_end, str_len);
  3705. CHECK_BUF(p, p_end, str_len);
  3706. if (!(export->name = wasm_const_str_list_insert(
  3707. p, str_len, module, is_load_from_file_buf, error_buf,
  3708. error_buf_size))) {
  3709. return false;
  3710. }
  3711. p += str_len;
  3712. CHECK_BUF(p, p_end, 1);
  3713. export->kind = read_uint8(p);
  3714. read_leb_uint32(p, p_end, index);
  3715. export->index = index;
  3716. switch (export->kind) {
  3717. /* function index */
  3718. case EXPORT_KIND_FUNC:
  3719. if (index >= module->function_count
  3720. + module->import_function_count) {
  3721. set_error_buf(error_buf, error_buf_size,
  3722. "unknown function");
  3723. return false;
  3724. }
  3725. #if WASM_ENABLE_SIMD != 0
  3726. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  3727. || (WASM_ENABLE_FAST_INTERP != 0)
  3728. /* TODO: check func type, if it has v128 param or result,
  3729. report error */
  3730. #endif
  3731. #endif
  3732. break;
  3733. /* table index */
  3734. case EXPORT_KIND_TABLE:
  3735. if (index
  3736. >= module->table_count + module->import_table_count) {
  3737. set_error_buf(error_buf, error_buf_size,
  3738. "unknown table");
  3739. return false;
  3740. }
  3741. break;
  3742. /* memory index */
  3743. case EXPORT_KIND_MEMORY:
  3744. if (index
  3745. >= module->memory_count + module->import_memory_count) {
  3746. set_error_buf(error_buf, error_buf_size,
  3747. "unknown memory");
  3748. return false;
  3749. }
  3750. break;
  3751. #if WASM_ENABLE_TAGS != 0
  3752. /* export tag */
  3753. case EXPORT_KIND_TAG:
  3754. if (index >= module->tag_count + module->import_tag_count) {
  3755. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3756. return false;
  3757. }
  3758. break;
  3759. #endif
  3760. /* global index */
  3761. case EXPORT_KIND_GLOBAL:
  3762. if (index
  3763. >= module->global_count + module->import_global_count) {
  3764. set_error_buf(error_buf, error_buf_size,
  3765. "unknown global");
  3766. return false;
  3767. }
  3768. break;
  3769. default:
  3770. set_error_buf(error_buf, error_buf_size,
  3771. "invalid export kind");
  3772. return false;
  3773. }
  3774. }
  3775. if (!check_duplicate_exports(module, error_buf, error_buf_size)) {
  3776. return false;
  3777. }
  3778. }
  3779. if (p != p_end) {
  3780. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3781. return false;
  3782. }
  3783. LOG_VERBOSE("Load export section success.\n");
  3784. return true;
  3785. fail:
  3786. return false;
  3787. }
  3788. static bool
  3789. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3790. uint32 error_buf_size)
  3791. {
  3792. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3793. if (table_index != 0) {
  3794. set_error_buf(
  3795. error_buf, error_buf_size,
  3796. "zero byte expected. The module uses reference types feature "
  3797. "which is disabled in the runtime.");
  3798. return false;
  3799. }
  3800. #endif
  3801. if (table_index >= module->import_table_count + module->table_count) {
  3802. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3803. table_index);
  3804. return false;
  3805. }
  3806. return true;
  3807. }
  3808. static bool
  3809. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3810. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3811. {
  3812. const uint8 *p = *p_buf, *p_end = buf_end;
  3813. uint32 table_index;
  3814. read_leb_uint32(p, p_end, table_index);
  3815. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3816. return false;
  3817. }
  3818. *p_table_index = table_index;
  3819. *p_buf = p;
  3820. return true;
  3821. fail:
  3822. return false;
  3823. }
  3824. /* Element segments must match element type of table */
  3825. static bool
  3826. check_table_elem_type(WASMModule *module, uint32 table_index,
  3827. uint32 type_from_elem_seg, char *error_buf,
  3828. uint32 error_buf_size)
  3829. {
  3830. uint32 table_declared_elem_type;
  3831. if (table_index < module->import_table_count)
  3832. table_declared_elem_type =
  3833. module->import_tables[table_index].u.table.table_type.elem_type;
  3834. else
  3835. table_declared_elem_type =
  3836. module->tables[table_index - module->import_table_count]
  3837. .table_type.elem_type;
  3838. if (table_declared_elem_type == type_from_elem_seg)
  3839. return true;
  3840. #if WASM_ENABLE_GC != 0
  3841. /*
  3842. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3843. */
  3844. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3845. return true;
  3846. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3847. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3848. return true;
  3849. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3850. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3851. return true;
  3852. #endif
  3853. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3854. return false;
  3855. }
  3856. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3857. static bool
  3858. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3859. uint32 *p_elem_type,
  3860. #if WASM_ENABLE_GC != 0
  3861. WASMRefType **p_elem_ref_type,
  3862. #endif
  3863. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3864. {
  3865. const uint8 *p = *p_buf, *p_end = buf_end;
  3866. uint8 elem_type;
  3867. #if WASM_ENABLE_GC != 0
  3868. WASMRefType elem_ref_type;
  3869. bool need_ref_type_map;
  3870. #endif
  3871. CHECK_BUF(p, p_end, 1);
  3872. elem_type = read_uint8(p);
  3873. if (elemkind_zero) {
  3874. if (elem_type != 0) {
  3875. set_error_buf(error_buf, error_buf_size,
  3876. "invalid reference type or unknown type");
  3877. return false;
  3878. }
  3879. else {
  3880. *p_elem_type = VALUE_TYPE_FUNCREF;
  3881. *p_buf = p;
  3882. return true;
  3883. }
  3884. }
  3885. #if WASM_ENABLE_GC == 0
  3886. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3887. set_error_buf(error_buf, error_buf_size,
  3888. "invalid reference type or unknown type");
  3889. return false;
  3890. }
  3891. *p_elem_type = elem_type;
  3892. #else
  3893. p--;
  3894. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3895. module->type_count, &need_ref_type_map,
  3896. &elem_ref_type, false, error_buf, error_buf_size)) {
  3897. return false;
  3898. }
  3899. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3900. set_error_buf(error_buf, error_buf_size,
  3901. "invalid reference type or unknown type");
  3902. return false;
  3903. }
  3904. *p_elem_type = elem_ref_type.ref_type;
  3905. if (need_ref_type_map) {
  3906. if (!(*p_elem_ref_type =
  3907. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3908. error_buf, error_buf_size))) {
  3909. return false;
  3910. }
  3911. }
  3912. #endif
  3913. *p_buf = p;
  3914. return true;
  3915. fail:
  3916. return false;
  3917. }
  3918. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3919. static bool
  3920. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3921. WASMModule *module, WASMTableSeg *table_segment,
  3922. char *error_buf, uint32 error_buf_size)
  3923. {
  3924. const uint8 *p = *p_buf, *p_end = buf_end;
  3925. uint32 function_count, function_index = 0, i;
  3926. uint64 total_size;
  3927. read_leb_uint32(p, p_end, function_count);
  3928. table_segment->value_count = function_count;
  3929. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3930. if (total_size > 0
  3931. && !(table_segment->init_values =
  3932. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3933. error_buf_size))) {
  3934. return false;
  3935. }
  3936. for (i = 0; i < function_count; i++) {
  3937. InitializerExpression *init_expr = &table_segment->init_values[i];
  3938. read_leb_uint32(p, p_end, function_index);
  3939. if (!check_function_index(module, function_index, error_buf,
  3940. error_buf_size)) {
  3941. return false;
  3942. }
  3943. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3944. init_expr->u.ref_index = function_index;
  3945. }
  3946. *p_buf = p;
  3947. return true;
  3948. fail:
  3949. return false;
  3950. }
  3951. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3952. static bool
  3953. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3954. WASMModule *module, WASMTableSeg *table_segment,
  3955. char *error_buf, uint32 error_buf_size)
  3956. {
  3957. const uint8 *p = *p_buf, *p_end = buf_end;
  3958. uint32 ref_count, i;
  3959. uint64 total_size;
  3960. read_leb_uint32(p, p_end, ref_count);
  3961. table_segment->value_count = ref_count;
  3962. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3963. if (total_size > 0
  3964. && !(table_segment->init_values =
  3965. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3966. error_buf_size))) {
  3967. return false;
  3968. }
  3969. for (i = 0; i < ref_count; i++) {
  3970. InitializerExpression *init_expr = &table_segment->init_values[i];
  3971. if (!load_init_expr(module, &p, p_end, init_expr,
  3972. table_segment->elem_type,
  3973. #if WASM_ENABLE_GC == 0
  3974. NULL,
  3975. #else
  3976. table_segment->elem_ref_type,
  3977. #endif
  3978. error_buf, error_buf_size))
  3979. return false;
  3980. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3981. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3982. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3983. && init_expr->init_expr_type
  3984. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3985. }
  3986. *p_buf = p;
  3987. return true;
  3988. fail:
  3989. return false;
  3990. }
  3991. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3992. static bool
  3993. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3994. WASMModule *module, char *error_buf,
  3995. uint32 error_buf_size)
  3996. {
  3997. const uint8 *p = buf, *p_end = buf_end;
  3998. uint8 table_elem_idx_type;
  3999. uint32 table_segment_count, i;
  4000. uint64 total_size;
  4001. WASMTableSeg *table_segment;
  4002. read_leb_uint32(p, p_end, table_segment_count);
  4003. if (table_segment_count) {
  4004. module->table_seg_count = table_segment_count;
  4005. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  4006. if (!(module->table_segments =
  4007. loader_malloc(total_size, error_buf, error_buf_size))) {
  4008. return false;
  4009. }
  4010. table_segment = module->table_segments;
  4011. for (i = 0; i < table_segment_count; i++, table_segment++) {
  4012. if (p >= p_end) {
  4013. set_error_buf(error_buf, error_buf_size,
  4014. "invalid value type or "
  4015. "invalid elements segment kind");
  4016. return false;
  4017. }
  4018. table_elem_idx_type = VALUE_TYPE_I32;
  4019. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  4020. read_leb_uint32(p, p_end, table_segment->mode);
  4021. /* last three bits */
  4022. table_segment->mode = table_segment->mode & 0x07;
  4023. switch (table_segment->mode) {
  4024. /* elemkind/elemtype + active */
  4025. case 0:
  4026. case 4:
  4027. {
  4028. #if WASM_ENABLE_GC != 0
  4029. if (table_segment->mode == 0) {
  4030. /* vec(funcidx), set elem type to (ref func) */
  4031. WASMRefType elem_ref_type = { 0 };
  4032. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  4033. wasm_set_refheaptype_common(
  4034. &elem_ref_type.ref_ht_common, false,
  4035. HEAP_TYPE_FUNC);
  4036. if (!(table_segment->elem_ref_type = reftype_set_insert(
  4037. module->ref_type_set, &elem_ref_type,
  4038. error_buf, error_buf_size)))
  4039. return false;
  4040. }
  4041. else {
  4042. /* vec(expr), set elem type to funcref */
  4043. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4044. }
  4045. #else
  4046. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4047. #endif
  4048. table_segment->table_index = 0;
  4049. if (!check_table_index(module, table_segment->table_index,
  4050. error_buf, error_buf_size))
  4051. return false;
  4052. #if WASM_ENABLE_MEMORY64 != 0
  4053. table_elem_idx_type =
  4054. is_table_64bit(module, table_segment->table_index)
  4055. ? VALUE_TYPE_I64
  4056. : VALUE_TYPE_I32;
  4057. #endif
  4058. if (!load_init_expr(module, &p, p_end,
  4059. &table_segment->base_offset,
  4060. table_elem_idx_type, NULL, error_buf,
  4061. error_buf_size))
  4062. return false;
  4063. if (table_segment->mode == 0) {
  4064. /* vec(funcidx) */
  4065. if (!load_func_index_vec(&p, p_end, module,
  4066. table_segment, error_buf,
  4067. error_buf_size))
  4068. return false;
  4069. }
  4070. else {
  4071. /* vec(expr) */
  4072. if (!load_init_expr_vec(&p, p_end, module,
  4073. table_segment, error_buf,
  4074. error_buf_size))
  4075. return false;
  4076. }
  4077. if (!check_table_elem_type(module,
  4078. table_segment->table_index,
  4079. table_segment->elem_type,
  4080. error_buf, error_buf_size))
  4081. return false;
  4082. break;
  4083. }
  4084. /* elemkind + passive/declarative */
  4085. case 1:
  4086. case 3:
  4087. if (!load_elem_type(module, &p, p_end,
  4088. &table_segment->elem_type,
  4089. #if WASM_ENABLE_GC != 0
  4090. &table_segment->elem_ref_type,
  4091. #endif
  4092. true, error_buf, error_buf_size))
  4093. return false;
  4094. /* vec(funcidx) */
  4095. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4096. error_buf, error_buf_size))
  4097. return false;
  4098. break;
  4099. /* elemkind/elemtype + table_idx + active */
  4100. case 2:
  4101. case 6:
  4102. if (!load_table_index(&p, p_end, module,
  4103. &table_segment->table_index,
  4104. error_buf, error_buf_size))
  4105. return false;
  4106. #if WASM_ENABLE_MEMORY64 != 0
  4107. table_elem_idx_type =
  4108. is_table_64bit(module, table_segment->table_index)
  4109. ? VALUE_TYPE_I64
  4110. : VALUE_TYPE_I32;
  4111. #endif
  4112. if (!load_init_expr(module, &p, p_end,
  4113. &table_segment->base_offset,
  4114. table_elem_idx_type, NULL, error_buf,
  4115. error_buf_size))
  4116. return false;
  4117. if (!load_elem_type(module, &p, p_end,
  4118. &table_segment->elem_type,
  4119. #if WASM_ENABLE_GC != 0
  4120. &table_segment->elem_ref_type,
  4121. #endif
  4122. table_segment->mode == 2 ? true : false,
  4123. error_buf, error_buf_size))
  4124. return false;
  4125. if (table_segment->mode == 2) {
  4126. /* vec(funcidx) */
  4127. if (!load_func_index_vec(&p, p_end, module,
  4128. table_segment, error_buf,
  4129. error_buf_size))
  4130. return false;
  4131. }
  4132. else {
  4133. /* vec(expr) */
  4134. if (!load_init_expr_vec(&p, p_end, module,
  4135. table_segment, error_buf,
  4136. error_buf_size))
  4137. return false;
  4138. }
  4139. if (!check_table_elem_type(module,
  4140. table_segment->table_index,
  4141. table_segment->elem_type,
  4142. error_buf, error_buf_size))
  4143. return false;
  4144. break;
  4145. case 5:
  4146. case 7:
  4147. if (!load_elem_type(module, &p, p_end,
  4148. &table_segment->elem_type,
  4149. #if WASM_ENABLE_GC != 0
  4150. &table_segment->elem_ref_type,
  4151. #endif
  4152. false, error_buf, error_buf_size))
  4153. return false;
  4154. /* vec(expr) */
  4155. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4156. error_buf, error_buf_size))
  4157. return false;
  4158. break;
  4159. default:
  4160. set_error_buf(error_buf, error_buf_size,
  4161. "unknown element segment kind");
  4162. return false;
  4163. }
  4164. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4165. /*
  4166. * like: 00 41 05 0b 04 00 01 00 01
  4167. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4168. */
  4169. if (!load_table_index(&p, p_end, module,
  4170. &table_segment->table_index, error_buf,
  4171. error_buf_size))
  4172. return false;
  4173. #if WASM_ENABLE_MEMORY64 != 0
  4174. table_elem_idx_type =
  4175. is_table_64bit(module, table_segment->table_index)
  4176. ? VALUE_TYPE_I64
  4177. : VALUE_TYPE_I32;
  4178. #endif
  4179. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4180. table_elem_idx_type, NULL, error_buf,
  4181. error_buf_size))
  4182. return false;
  4183. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4184. error_buf, error_buf_size))
  4185. return false;
  4186. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4187. if (!check_table_elem_type(module, table_segment->table_index,
  4188. table_segment->elem_type, error_buf,
  4189. error_buf_size))
  4190. return false;
  4191. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4192. #if WASM_ENABLE_MEMORY64 != 0
  4193. if (table_elem_idx_type == VALUE_TYPE_I64
  4194. && table_segment->base_offset.u.u64 > UINT32_MAX) {
  4195. set_error_buf(error_buf, error_buf_size,
  4196. "In table64, table base offset can't be "
  4197. "larger than UINT32_MAX");
  4198. return false;
  4199. }
  4200. #endif
  4201. #if WASM_ENABLE_WAMR_COMPILER != 0
  4202. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4203. module->is_ref_types_used = true;
  4204. #endif
  4205. }
  4206. }
  4207. if (p != p_end) {
  4208. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4209. return false;
  4210. }
  4211. LOG_VERBOSE("Load table segment section success.\n");
  4212. return true;
  4213. fail:
  4214. return false;
  4215. }
  4216. #if WASM_ENABLE_BULK_MEMORY != 0
  4217. static bool
  4218. check_data_count_consistency(bool has_datacount_section, int datacount_len,
  4219. int data_seg_len, char *error_buf,
  4220. uint32 error_buf_size)
  4221. {
  4222. if (has_datacount_section && datacount_len != data_seg_len) {
  4223. set_error_buf(error_buf, error_buf_size,
  4224. "data count and data section have inconsistent lengths");
  4225. return false;
  4226. }
  4227. return true;
  4228. }
  4229. #endif
  4230. static bool
  4231. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4232. WASMModule *module,
  4233. #if WASM_ENABLE_BULK_MEMORY != 0
  4234. bool has_datacount_section,
  4235. #endif
  4236. bool clone_data_seg, char *error_buf,
  4237. uint32 error_buf_size)
  4238. {
  4239. const uint8 *p = buf, *p_end = buf_end;
  4240. uint32 data_seg_count, i, mem_index, data_seg_len;
  4241. uint64 total_size;
  4242. WASMDataSeg *dataseg;
  4243. InitializerExpression init_expr;
  4244. #if WASM_ENABLE_BULK_MEMORY != 0
  4245. bool is_passive = false;
  4246. uint32 mem_flag;
  4247. #endif
  4248. uint8 mem_offset_type = VALUE_TYPE_I32;
  4249. read_leb_uint32(p, p_end, data_seg_count);
  4250. #if WASM_ENABLE_BULK_MEMORY != 0
  4251. if (!check_data_count_consistency(has_datacount_section,
  4252. module->data_seg_count1, data_seg_count,
  4253. error_buf, error_buf_size)) {
  4254. return false;
  4255. }
  4256. #endif
  4257. if (data_seg_count) {
  4258. module->data_seg_count = data_seg_count;
  4259. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4260. if (!(module->data_segments =
  4261. loader_malloc(total_size, error_buf, error_buf_size))) {
  4262. return false;
  4263. }
  4264. for (i = 0; i < data_seg_count; i++) {
  4265. read_leb_uint32(p, p_end, mem_index);
  4266. #if WASM_ENABLE_BULK_MEMORY != 0
  4267. is_passive = false;
  4268. mem_flag = mem_index & 0x03;
  4269. switch (mem_flag) {
  4270. case 0x01:
  4271. is_passive = true;
  4272. #if WASM_ENABLE_WAMR_COMPILER != 0
  4273. module->is_bulk_memory_used = true;
  4274. #endif
  4275. break;
  4276. case 0x00:
  4277. /* no memory index, treat index as 0 */
  4278. mem_index = 0;
  4279. goto check_mem_index;
  4280. case 0x02:
  4281. /* read following memory index */
  4282. read_leb_uint32(p, p_end, mem_index);
  4283. #if WASM_ENABLE_WAMR_COMPILER != 0
  4284. module->is_bulk_memory_used = true;
  4285. #endif
  4286. check_mem_index:
  4287. if (mem_index
  4288. >= module->import_memory_count + module->memory_count) {
  4289. set_error_buf_v(error_buf, error_buf_size,
  4290. "unknown memory %d", mem_index);
  4291. return false;
  4292. }
  4293. break;
  4294. case 0x03:
  4295. default:
  4296. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4297. return false;
  4298. break;
  4299. }
  4300. #else
  4301. if (mem_index
  4302. >= module->import_memory_count + module->memory_count) {
  4303. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4304. mem_index);
  4305. return false;
  4306. }
  4307. #endif /* WASM_ENABLE_BULK_MEMORY */
  4308. #if WASM_ENABLE_BULK_MEMORY != 0
  4309. if (!is_passive)
  4310. #endif
  4311. {
  4312. #if WASM_ENABLE_MEMORY64 != 0
  4313. /* This memory_flag is from memory instead of data segment */
  4314. uint8 memory_flag;
  4315. if (module->import_memory_count > 0) {
  4316. memory_flag = module->import_memories[mem_index]
  4317. .u.memory.mem_type.flags;
  4318. }
  4319. else {
  4320. memory_flag =
  4321. module
  4322. ->memories[mem_index - module->import_memory_count]
  4323. .flags;
  4324. }
  4325. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4326. : VALUE_TYPE_I32;
  4327. #else
  4328. mem_offset_type = VALUE_TYPE_I32;
  4329. #endif
  4330. }
  4331. #if WASM_ENABLE_BULK_MEMORY != 0
  4332. if (!is_passive)
  4333. #endif
  4334. if (!load_init_expr(module, &p, p_end, &init_expr,
  4335. mem_offset_type, NULL, error_buf,
  4336. error_buf_size))
  4337. return false;
  4338. read_leb_uint32(p, p_end, data_seg_len);
  4339. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4340. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4341. return false;
  4342. }
  4343. #if WASM_ENABLE_BULK_MEMORY != 0
  4344. dataseg->is_passive = is_passive;
  4345. if (!is_passive)
  4346. #endif
  4347. {
  4348. bh_memcpy_s(&dataseg->base_offset,
  4349. sizeof(InitializerExpression), &init_expr,
  4350. sizeof(InitializerExpression));
  4351. dataseg->memory_index = mem_index;
  4352. }
  4353. dataseg->data_length = data_seg_len;
  4354. CHECK_BUF(p, p_end, data_seg_len);
  4355. if (clone_data_seg) {
  4356. if (!(dataseg->data = loader_malloc(
  4357. dataseg->data_length, error_buf, error_buf_size))) {
  4358. return false;
  4359. }
  4360. bh_memcpy_s(dataseg->data, dataseg->data_length, p,
  4361. data_seg_len);
  4362. }
  4363. else {
  4364. dataseg->data = (uint8 *)p;
  4365. }
  4366. dataseg->is_data_cloned = clone_data_seg;
  4367. p += data_seg_len;
  4368. }
  4369. }
  4370. if (p != p_end) {
  4371. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4372. return false;
  4373. }
  4374. LOG_VERBOSE("Load data segment section success.\n");
  4375. return true;
  4376. fail:
  4377. return false;
  4378. }
  4379. #if WASM_ENABLE_BULK_MEMORY != 0
  4380. static bool
  4381. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4382. WASMModule *module, char *error_buf,
  4383. uint32 error_buf_size)
  4384. {
  4385. const uint8 *p = buf, *p_end = buf_end;
  4386. uint32 data_seg_count1 = 0;
  4387. read_leb_uint32(p, p_end, data_seg_count1);
  4388. module->data_seg_count1 = data_seg_count1;
  4389. if (p != p_end) {
  4390. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4391. return false;
  4392. }
  4393. #if WASM_ENABLE_WAMR_COMPILER != 0
  4394. module->is_bulk_memory_used = true;
  4395. #endif
  4396. LOG_VERBOSE("Load datacount section success.\n");
  4397. return true;
  4398. fail:
  4399. return false;
  4400. }
  4401. #endif
  4402. #if WASM_ENABLE_TAGS != 0
  4403. static bool
  4404. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4405. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4406. uint32 error_buf_size)
  4407. {
  4408. (void)buf_code;
  4409. (void)buf_code_end;
  4410. const uint8 *p = buf, *p_end = buf_end;
  4411. size_t total_size = 0;
  4412. /* number of tags defined in the section */
  4413. uint32 section_tag_count = 0;
  4414. uint8 tag_attribute;
  4415. uint32 tag_type;
  4416. WASMTag *tag = NULL;
  4417. /* get tag count */
  4418. read_leb_uint32(p, p_end, section_tag_count);
  4419. if (is_indices_overflow(module->import_tag_count, section_tag_count,
  4420. error_buf, error_buf_size))
  4421. return false;
  4422. module->tag_count = section_tag_count;
  4423. if (section_tag_count) {
  4424. total_size = sizeof(WASMTag *) * module->tag_count;
  4425. if (!(module->tags =
  4426. loader_malloc(total_size, error_buf, error_buf_size))) {
  4427. return false;
  4428. }
  4429. /* load each tag, imported tags precede the tags */
  4430. uint32 tag_index;
  4431. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4432. /* get the one byte attribute */
  4433. CHECK_BUF(p, p_end, 1);
  4434. tag_attribute = read_uint8(p);
  4435. /* get type */
  4436. read_leb_uint32(p, p_end, tag_type);
  4437. /* compare against module->types */
  4438. if (tag_type >= module->type_count) {
  4439. set_error_buf(error_buf, error_buf_size, "unknown type");
  4440. return false;
  4441. }
  4442. /* get return type (must be 0) */
  4443. /* check, that the type of the referred tag returns void */
  4444. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4445. if (func_type->result_count != 0) {
  4446. set_error_buf(error_buf, error_buf_size,
  4447. "non-empty tag result type");
  4448. goto fail;
  4449. }
  4450. if (!(tag = module->tags[tag_index] = loader_malloc(
  4451. sizeof(WASMTag), error_buf, error_buf_size))) {
  4452. return false;
  4453. }
  4454. /* store to module tag declarations */
  4455. tag->attribute = tag_attribute;
  4456. tag->type = tag_type;
  4457. tag->tag_type = func_type;
  4458. }
  4459. }
  4460. if (p != p_end) {
  4461. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4462. return false;
  4463. }
  4464. LOG_VERBOSE("Load tag section success.\n");
  4465. return true;
  4466. fail:
  4467. return false;
  4468. }
  4469. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4470. static bool
  4471. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4472. const uint8 *buf_func_end, WASMModule *module,
  4473. char *error_buf, uint32 error_buf_size)
  4474. {
  4475. const uint8 *p = buf, *p_end = buf_end;
  4476. const uint8 *p_func = buf_func;
  4477. uint32 func_count = 0, code_count;
  4478. /* code has been loaded in function section, so pass it here, just check
  4479. * whether function and code section have inconsistent lengths */
  4480. read_leb_uint32(p, p_end, code_count);
  4481. if (buf_func)
  4482. read_leb_uint32(p_func, buf_func_end, func_count);
  4483. if (func_count != code_count) {
  4484. set_error_buf(error_buf, error_buf_size,
  4485. "function and code section have inconsistent lengths");
  4486. return false;
  4487. }
  4488. LOG_VERBOSE("Load code segment section success.\n");
  4489. (void)module;
  4490. return true;
  4491. fail:
  4492. return false;
  4493. }
  4494. static bool
  4495. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4496. char *error_buf, uint32 error_buf_size)
  4497. {
  4498. const uint8 *p = buf, *p_end = buf_end;
  4499. WASMFuncType *type;
  4500. uint32 start_function;
  4501. read_leb_uint32(p, p_end, start_function);
  4502. if (start_function
  4503. >= module->function_count + module->import_function_count) {
  4504. set_error_buf(error_buf, error_buf_size, "unknown function");
  4505. return false;
  4506. }
  4507. if (start_function < module->import_function_count)
  4508. type = module->import_functions[start_function].u.function.func_type;
  4509. else
  4510. type = module->functions[start_function - module->import_function_count]
  4511. ->func_type;
  4512. if (type->param_count != 0 || type->result_count != 0) {
  4513. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4514. return false;
  4515. }
  4516. module->start_function = start_function;
  4517. if (p != p_end) {
  4518. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4519. return false;
  4520. }
  4521. LOG_VERBOSE("Load start section success.\n");
  4522. return true;
  4523. fail:
  4524. return false;
  4525. }
  4526. #if WASM_ENABLE_STRINGREF != 0
  4527. static bool
  4528. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4529. WASMModule *module, bool is_load_from_file_buf,
  4530. char *error_buf, uint32 error_buf_size)
  4531. {
  4532. const uint8 *p = buf, *p_end = buf_end;
  4533. int32 deferred_count, immediate_count, string_length, i;
  4534. uint64 total_size;
  4535. read_leb_uint32(p, p_end, deferred_count);
  4536. read_leb_uint32(p, p_end, immediate_count);
  4537. /* proposal set deferred_count for future extension */
  4538. if (deferred_count != 0) {
  4539. goto fail;
  4540. }
  4541. if (immediate_count > 0) {
  4542. total_size = sizeof(char *) * (uint64)immediate_count;
  4543. if (!(module->string_literal_ptrs =
  4544. loader_malloc(total_size, error_buf, error_buf_size))) {
  4545. goto fail;
  4546. }
  4547. module->string_literal_count = immediate_count;
  4548. total_size = sizeof(uint32) * (uint64)immediate_count;
  4549. if (!(module->string_literal_lengths =
  4550. loader_malloc(total_size, error_buf, error_buf_size))) {
  4551. goto fail;
  4552. }
  4553. for (i = 0; i < immediate_count; i++) {
  4554. read_leb_uint32(p, p_end, string_length);
  4555. CHECK_BUF(p, p_end, string_length);
  4556. module->string_literal_ptrs[i] = p;
  4557. module->string_literal_lengths[i] = string_length;
  4558. p += string_length;
  4559. }
  4560. }
  4561. if (p != p_end) {
  4562. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4563. goto fail;
  4564. }
  4565. LOG_VERBOSE("Load stringref section success.\n");
  4566. return true;
  4567. fail:
  4568. return false;
  4569. }
  4570. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4571. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4572. static bool
  4573. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4574. bool is_load_from_file_buf, char *error_buf,
  4575. uint32 error_buf_size)
  4576. {
  4577. const uint8 *p = buf, *p_end = buf_end;
  4578. uint32 name_type, subsection_size;
  4579. uint32 previous_name_type = 0;
  4580. uint32 num_func_name;
  4581. uint32 func_index;
  4582. uint32 previous_func_index = ~0U;
  4583. uint32 func_name_len;
  4584. uint32 name_index;
  4585. int i = 0;
  4586. if (p >= p_end) {
  4587. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4588. return false;
  4589. }
  4590. while (p < p_end) {
  4591. read_leb_uint32(p, p_end, name_type);
  4592. if (i != 0) {
  4593. if (name_type == previous_name_type) {
  4594. set_error_buf(error_buf, error_buf_size,
  4595. "duplicate sub-section");
  4596. return false;
  4597. }
  4598. if (name_type < previous_name_type) {
  4599. set_error_buf(error_buf, error_buf_size,
  4600. "out-of-order sub-section");
  4601. return false;
  4602. }
  4603. }
  4604. previous_name_type = name_type;
  4605. read_leb_uint32(p, p_end, subsection_size);
  4606. CHECK_BUF(p, p_end, subsection_size);
  4607. switch (name_type) {
  4608. case SUB_SECTION_TYPE_FUNC:
  4609. if (subsection_size) {
  4610. read_leb_uint32(p, p_end, num_func_name);
  4611. for (name_index = 0; name_index < num_func_name;
  4612. name_index++) {
  4613. read_leb_uint32(p, p_end, func_index);
  4614. if (func_index == previous_func_index) {
  4615. set_error_buf(error_buf, error_buf_size,
  4616. "duplicate function name");
  4617. return false;
  4618. }
  4619. if (func_index < previous_func_index
  4620. && previous_func_index != ~0U) {
  4621. set_error_buf(error_buf, error_buf_size,
  4622. "out-of-order function index ");
  4623. return false;
  4624. }
  4625. previous_func_index = func_index;
  4626. read_leb_uint32(p, p_end, func_name_len);
  4627. CHECK_BUF(p, p_end, func_name_len);
  4628. /* Skip the import functions */
  4629. if (func_index >= module->import_function_count) {
  4630. func_index -= module->import_function_count;
  4631. if (func_index >= module->function_count) {
  4632. set_error_buf(error_buf, error_buf_size,
  4633. "out-of-range function index");
  4634. return false;
  4635. }
  4636. if (!(module->functions[func_index]->field_name =
  4637. wasm_const_str_list_insert(
  4638. p, func_name_len, module,
  4639. #if WASM_ENABLE_WAMR_COMPILER != 0
  4640. false,
  4641. #else
  4642. is_load_from_file_buf,
  4643. #endif
  4644. error_buf, error_buf_size))) {
  4645. return false;
  4646. }
  4647. }
  4648. p += func_name_len;
  4649. }
  4650. }
  4651. break;
  4652. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4653. */
  4654. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4655. default:
  4656. p = p + subsection_size;
  4657. break;
  4658. }
  4659. i++;
  4660. }
  4661. return true;
  4662. fail:
  4663. return false;
  4664. }
  4665. #endif
  4666. static bool
  4667. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4668. bool is_load_from_file_buf, char *error_buf,
  4669. uint32 error_buf_size)
  4670. {
  4671. const uint8 *p = buf, *p_end = buf_end;
  4672. char section_name[32];
  4673. uint32 name_len, buffer_len;
  4674. if (p >= p_end) {
  4675. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4676. return false;
  4677. }
  4678. read_leb_uint32(p, p_end, name_len);
  4679. if (p + name_len > p_end) {
  4680. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4681. return false;
  4682. }
  4683. if (!wasm_check_utf8_str(p, name_len)) {
  4684. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4685. return false;
  4686. }
  4687. buffer_len = sizeof(section_name);
  4688. memset(section_name, 0, buffer_len);
  4689. if (name_len < buffer_len) {
  4690. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4691. }
  4692. else {
  4693. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4694. memset(section_name + buffer_len - 4, '.', 3);
  4695. }
  4696. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4697. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4698. module->name_section_buf = buf;
  4699. module->name_section_buf_end = buf_end;
  4700. p += name_len;
  4701. if (!handle_name_section(p, p_end, module, is_load_from_file_buf,
  4702. error_buf, error_buf_size)) {
  4703. return false;
  4704. }
  4705. LOG_VERBOSE("Load custom name section success.");
  4706. }
  4707. #endif
  4708. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4709. {
  4710. WASMCustomSection *section =
  4711. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4712. if (!section) {
  4713. return false;
  4714. }
  4715. section->name_addr = (char *)p;
  4716. section->name_len = name_len;
  4717. section->content_addr = (uint8 *)(p + name_len);
  4718. section->content_len = (uint32)(p_end - p - name_len);
  4719. section->next = module->custom_section_list;
  4720. module->custom_section_list = section;
  4721. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4722. return true;
  4723. }
  4724. #endif
  4725. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4726. (void)is_load_from_file_buf;
  4727. (void)module;
  4728. return true;
  4729. fail:
  4730. return false;
  4731. }
  4732. static void
  4733. calculate_global_data_offset(WASMModule *module)
  4734. {
  4735. uint32 i, data_offset;
  4736. data_offset = 0;
  4737. for (i = 0; i < module->import_global_count; i++) {
  4738. WASMGlobalImport *import_global =
  4739. &((module->import_globals + i)->u.global);
  4740. #if WASM_ENABLE_FAST_JIT != 0
  4741. import_global->data_offset = data_offset;
  4742. #endif
  4743. data_offset += wasm_value_type_size(import_global->type.val_type);
  4744. }
  4745. for (i = 0; i < module->global_count; i++) {
  4746. WASMGlobal *global = module->globals + i;
  4747. #if WASM_ENABLE_FAST_JIT != 0
  4748. global->data_offset = data_offset;
  4749. #endif
  4750. data_offset += wasm_value_type_size(global->type.val_type);
  4751. }
  4752. module->global_data_size = data_offset;
  4753. }
  4754. #if WASM_ENABLE_FAST_JIT != 0
  4755. static bool
  4756. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4757. uint32 error_buf_size)
  4758. {
  4759. #if WASM_ENABLE_LAZY_JIT != 0
  4760. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4761. #endif
  4762. uint32 i;
  4763. if (!module->function_count)
  4764. return true;
  4765. if (!(module->fast_jit_func_ptrs =
  4766. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4767. error_buf_size))) {
  4768. return false;
  4769. }
  4770. #if WASM_ENABLE_LAZY_JIT != 0
  4771. for (i = 0; i < module->function_count; i++) {
  4772. module->fast_jit_func_ptrs[i] =
  4773. jit_globals->compile_fast_jit_and_then_call;
  4774. }
  4775. #endif
  4776. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4777. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4778. set_error_buf(error_buf, error_buf_size,
  4779. "init fast jit thread lock failed");
  4780. return false;
  4781. }
  4782. module->fast_jit_thread_locks_inited[i] = true;
  4783. }
  4784. return true;
  4785. }
  4786. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4787. #if WASM_ENABLE_JIT != 0
  4788. static bool
  4789. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4790. uint32 error_buf_size)
  4791. {
  4792. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4793. AOTCompOption option = { 0 };
  4794. char *aot_last_error;
  4795. uint64 size;
  4796. #if WASM_ENABLE_GC != 0
  4797. bool gc_enabled = true;
  4798. #else
  4799. bool gc_enabled = false;
  4800. #endif
  4801. if (module->function_count == 0)
  4802. return true;
  4803. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4804. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4805. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4806. return false;
  4807. }
  4808. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4809. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4810. os_mutex_destroy(&module->tierup_wait_lock);
  4811. return false;
  4812. }
  4813. module->tierup_wait_lock_inited = true;
  4814. #endif
  4815. size = sizeof(void *) * (uint64)module->function_count
  4816. + sizeof(bool) * (uint64)module->function_count;
  4817. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4818. return false;
  4819. }
  4820. module->func_ptrs_compiled =
  4821. (bool *)((uint8 *)module->func_ptrs
  4822. + sizeof(void *) * module->function_count);
  4823. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4824. if (!module->comp_data) {
  4825. aot_last_error = aot_get_last_error();
  4826. bh_assert(aot_last_error != NULL);
  4827. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4828. return false;
  4829. }
  4830. option.is_jit_mode = true;
  4831. option.opt_level = llvm_jit_options->opt_level;
  4832. option.size_level = llvm_jit_options->size_level;
  4833. option.segue_flags = llvm_jit_options->segue_flags;
  4834. option.quick_invoke_c_api_import =
  4835. llvm_jit_options->quick_invoke_c_api_import;
  4836. #if WASM_ENABLE_BULK_MEMORY != 0
  4837. option.enable_bulk_memory = true;
  4838. #endif
  4839. #if WASM_ENABLE_THREAD_MGR != 0
  4840. option.enable_thread_mgr = true;
  4841. #endif
  4842. #if WASM_ENABLE_TAIL_CALL != 0
  4843. option.enable_tail_call = true;
  4844. #endif
  4845. #if WASM_ENABLE_SIMD != 0
  4846. option.enable_simd = true;
  4847. #endif
  4848. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4849. option.enable_ref_types = true;
  4850. #elif WASM_ENABLE_GC != 0
  4851. option.enable_gc = true;
  4852. #endif
  4853. option.enable_aux_stack_check = true;
  4854. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4855. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4856. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  4857. aot_call_stack_features_init_default(&option.call_stack_features);
  4858. #endif
  4859. #if WASM_ENABLE_PERF_PROFILING != 0
  4860. option.enable_perf_profiling = true;
  4861. #endif
  4862. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4863. option.enable_memory_profiling = true;
  4864. option.enable_stack_estimation = true;
  4865. #endif
  4866. #if WASM_ENABLE_SHARED_HEAP != 0
  4867. option.enable_shared_heap = true;
  4868. #endif
  4869. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4870. if (!module->comp_ctx) {
  4871. aot_last_error = aot_get_last_error();
  4872. bh_assert(aot_last_error != NULL);
  4873. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4874. return false;
  4875. }
  4876. return true;
  4877. }
  4878. static bool
  4879. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4880. uint32 error_buf_size)
  4881. {
  4882. char *aot_last_error;
  4883. uint32 i;
  4884. if (module->function_count == 0)
  4885. return true;
  4886. if (!aot_compile_wasm(module->comp_ctx)) {
  4887. aot_last_error = aot_get_last_error();
  4888. bh_assert(aot_last_error != NULL);
  4889. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4890. return false;
  4891. }
  4892. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4893. if (module->orcjit_stop_compiling)
  4894. return false;
  4895. #endif
  4896. bh_print_time("Begin to lookup llvm jit functions");
  4897. for (i = 0; i < module->function_count; i++) {
  4898. LLVMOrcJITTargetAddress func_addr = 0;
  4899. LLVMErrorRef error;
  4900. char func_name[48];
  4901. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4902. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4903. func_name);
  4904. if (error != LLVMErrorSuccess) {
  4905. char *err_msg = LLVMGetErrorMessage(error);
  4906. set_error_buf_v(error_buf, error_buf_size,
  4907. "failed to compile llvm jit function: %s", err_msg);
  4908. LLVMDisposeErrorMessage(err_msg);
  4909. return false;
  4910. }
  4911. /**
  4912. * No need to lock the func_ptr[func_idx] here as it is basic
  4913. * data type, the load/store for it can be finished by one cpu
  4914. * instruction, and there can be only one cpu instruction
  4915. * loading/storing at the same time.
  4916. */
  4917. module->func_ptrs[i] = (void *)func_addr;
  4918. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4919. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4920. if (module->orcjit_stop_compiling)
  4921. return false;
  4922. #endif
  4923. }
  4924. bh_print_time("End lookup llvm jit functions");
  4925. return true;
  4926. }
  4927. #endif /* end of WASM_ENABLE_JIT != 0 */
  4928. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4929. && WASM_ENABLE_LAZY_JIT != 0
  4930. static void *
  4931. init_llvm_jit_functions_stage2_callback(void *arg)
  4932. {
  4933. WASMModule *module = (WASMModule *)arg;
  4934. char error_buf[128];
  4935. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4936. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4937. module->orcjit_stop_compiling = true;
  4938. return NULL;
  4939. }
  4940. os_mutex_lock(&module->tierup_wait_lock);
  4941. module->llvm_jit_inited = true;
  4942. os_cond_broadcast(&module->tierup_wait_cond);
  4943. os_mutex_unlock(&module->tierup_wait_lock);
  4944. return NULL;
  4945. }
  4946. #endif
  4947. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4948. /* The callback function to compile jit functions */
  4949. static void *
  4950. orcjit_thread_callback(void *arg)
  4951. {
  4952. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4953. #if WASM_ENABLE_JIT != 0
  4954. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4955. #endif
  4956. WASMModule *module = thread_arg->module;
  4957. uint32 group_idx = thread_arg->group_idx;
  4958. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4959. uint32 func_count = module->function_count;
  4960. uint32 i;
  4961. #if WASM_ENABLE_FAST_JIT != 0
  4962. /* Compile fast jit functions of this group */
  4963. for (i = group_idx; i < func_count; i += group_stride) {
  4964. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4965. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4966. break;
  4967. }
  4968. if (module->orcjit_stop_compiling) {
  4969. return NULL;
  4970. }
  4971. }
  4972. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4973. os_mutex_lock(&module->tierup_wait_lock);
  4974. module->fast_jit_ready_groups++;
  4975. os_mutex_unlock(&module->tierup_wait_lock);
  4976. #endif
  4977. #endif
  4978. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4979. && WASM_ENABLE_LAZY_JIT != 0
  4980. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4981. for (i = group_idx; i < func_count;
  4982. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4983. uint32 j;
  4984. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4985. if (i + j * group_stride < func_count) {
  4986. if (!jit_compiler_set_call_to_fast_jit(
  4987. module,
  4988. i + j * group_stride + module->import_function_count)) {
  4989. LOG_ERROR(
  4990. "failed to compile call_to_fast_jit for func %u\n",
  4991. i + j * group_stride + module->import_function_count);
  4992. module->orcjit_stop_compiling = true;
  4993. return NULL;
  4994. }
  4995. }
  4996. if (module->orcjit_stop_compiling) {
  4997. return NULL;
  4998. }
  4999. }
  5000. }
  5001. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  5002. fast jit functions are compiled */
  5003. os_mutex_lock(&module->tierup_wait_lock);
  5004. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  5005. && module->fast_jit_ready_groups >= group_stride)) {
  5006. os_cond_reltimedwait(&module->tierup_wait_cond,
  5007. &module->tierup_wait_lock, 10000);
  5008. if (module->orcjit_stop_compiling) {
  5009. /* init_llvm_jit_functions_stage2 failed */
  5010. os_mutex_unlock(&module->tierup_wait_lock);
  5011. return NULL;
  5012. }
  5013. }
  5014. os_mutex_unlock(&module->tierup_wait_lock);
  5015. #endif
  5016. #if WASM_ENABLE_JIT != 0
  5017. /* Compile llvm jit functions of this group */
  5018. for (i = group_idx; i < func_count;
  5019. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  5020. LLVMOrcJITTargetAddress func_addr = 0;
  5021. LLVMErrorRef error;
  5022. char func_name[48];
  5023. typedef void (*F)(void);
  5024. union {
  5025. F f;
  5026. void *v;
  5027. } u;
  5028. uint32 j;
  5029. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  5030. "_wrapper");
  5031. LOG_DEBUG("compile llvm jit func %s", func_name);
  5032. error =
  5033. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  5034. if (error != LLVMErrorSuccess) {
  5035. char *err_msg = LLVMGetErrorMessage(error);
  5036. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  5037. LLVMDisposeErrorMessage(err_msg);
  5038. break;
  5039. }
  5040. /* Call the jit wrapper function to trigger its compilation, so as
  5041. to compile the actual jit functions, since we add the latter to
  5042. function list in the PartitionFunction callback */
  5043. u.v = (void *)func_addr;
  5044. u.f();
  5045. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  5046. if (i + j * group_stride < func_count) {
  5047. module->func_ptrs_compiled[i + j * group_stride] = true;
  5048. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5049. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  5050. i + j * group_stride);
  5051. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  5052. func_name);
  5053. if (error != LLVMErrorSuccess) {
  5054. char *err_msg = LLVMGetErrorMessage(error);
  5055. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  5056. err_msg);
  5057. LLVMDisposeErrorMessage(err_msg);
  5058. /* Ignore current llvm jit func, as its func ptr is
  5059. previous set to call_to_fast_jit, which also works */
  5060. continue;
  5061. }
  5062. jit_compiler_set_llvm_jit_func_ptr(
  5063. module,
  5064. i + j * group_stride + module->import_function_count,
  5065. (void *)func_addr);
  5066. /* Try to switch to call this llvm jit function instead of
  5067. fast jit function from fast jit jitted code */
  5068. jit_compiler_set_call_to_llvm_jit(
  5069. module,
  5070. i + j * group_stride + module->import_function_count);
  5071. #endif
  5072. }
  5073. }
  5074. if (module->orcjit_stop_compiling) {
  5075. break;
  5076. }
  5077. }
  5078. #endif
  5079. return NULL;
  5080. }
  5081. static void
  5082. orcjit_stop_compile_threads(WASMModule *module)
  5083. {
  5084. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  5085. / sizeof(OrcJitThreadArg));
  5086. module->orcjit_stop_compiling = true;
  5087. for (i = 0; i < thread_num; i++) {
  5088. if (module->orcjit_threads[i])
  5089. os_thread_join(module->orcjit_threads[i], NULL);
  5090. }
  5091. }
  5092. static bool
  5093. compile_jit_functions(WASMModule *module, char *error_buf,
  5094. uint32 error_buf_size)
  5095. {
  5096. uint32 thread_num =
  5097. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  5098. uint32 i, j;
  5099. bh_print_time("Begin to compile jit functions");
  5100. /* Create threads to compile the jit functions */
  5101. for (i = 0; i < thread_num && i < module->function_count; i++) {
  5102. #if WASM_ENABLE_JIT != 0
  5103. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  5104. #endif
  5105. module->orcjit_thread_args[i].module = module;
  5106. module->orcjit_thread_args[i].group_idx = i;
  5107. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5108. (void *)&module->orcjit_thread_args[i],
  5109. APP_THREAD_STACK_SIZE_DEFAULT)
  5110. != 0) {
  5111. set_error_buf(error_buf, error_buf_size,
  5112. "create orcjit compile thread failed");
  5113. /* Terminate the threads created */
  5114. module->orcjit_stop_compiling = true;
  5115. for (j = 0; j < i; j++) {
  5116. os_thread_join(module->orcjit_threads[j], NULL);
  5117. }
  5118. return false;
  5119. }
  5120. }
  5121. #if WASM_ENABLE_LAZY_JIT == 0
  5122. /* Wait until all jit functions are compiled for eager mode */
  5123. for (i = 0; i < thread_num; i++) {
  5124. if (module->orcjit_threads[i])
  5125. os_thread_join(module->orcjit_threads[i], NULL);
  5126. }
  5127. #if WASM_ENABLE_FAST_JIT != 0
  5128. /* Ensure all the fast-jit functions are compiled */
  5129. for (i = 0; i < module->function_count; i++) {
  5130. if (!jit_compiler_is_compiled(module,
  5131. i + module->import_function_count)) {
  5132. set_error_buf(error_buf, error_buf_size,
  5133. "failed to compile fast jit function");
  5134. return false;
  5135. }
  5136. }
  5137. #endif
  5138. #if WASM_ENABLE_JIT != 0
  5139. /* Ensure all the llvm-jit functions are compiled */
  5140. for (i = 0; i < module->function_count; i++) {
  5141. if (!module->func_ptrs_compiled[i]) {
  5142. set_error_buf(error_buf, error_buf_size,
  5143. "failed to compile llvm jit function");
  5144. return false;
  5145. }
  5146. }
  5147. #endif
  5148. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5149. bh_print_time("End compile jit functions");
  5150. return true;
  5151. }
  5152. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5153. static bool
  5154. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5155. uint32 cur_func_idx, char *error_buf,
  5156. uint32 error_buf_size);
  5157. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5158. void **
  5159. wasm_interp_get_handle_table(void);
  5160. static void **handle_table;
  5161. #endif
  5162. static bool
  5163. load_from_sections(WASMModule *module, WASMSection *sections,
  5164. bool is_load_from_file_buf, bool wasm_binary_freeable,
  5165. bool no_resolve, char *error_buf, uint32 error_buf_size)
  5166. {
  5167. WASMExport *export;
  5168. WASMSection *section = sections;
  5169. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5170. *buf_func = NULL, *buf_func_end = NULL;
  5171. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5172. WASMGlobal *aux_stack_top_global = NULL, *global;
  5173. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5174. aux_stack_top = (uint64)-1LL;
  5175. uint32 global_index, func_index, i;
  5176. uint32 aux_data_end_global_index = (uint32)-1;
  5177. uint32 aux_heap_base_global_index = (uint32)-1;
  5178. WASMFuncType *func_type;
  5179. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5180. bool reuse_const_strings = is_load_from_file_buf && !wasm_binary_freeable;
  5181. bool clone_data_seg = is_load_from_file_buf && wasm_binary_freeable;
  5182. #if WASM_ENABLE_BULK_MEMORY != 0
  5183. bool has_datacount_section = false;
  5184. #endif
  5185. /* Find code and function sections if have */
  5186. while (section) {
  5187. if (section->section_type == SECTION_TYPE_CODE) {
  5188. buf_code = section->section_body;
  5189. buf_code_end = buf_code + section->section_body_size;
  5190. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5191. module->buf_code = (uint8 *)buf_code;
  5192. module->buf_code_size = section->section_body_size;
  5193. #endif
  5194. }
  5195. else if (section->section_type == SECTION_TYPE_FUNC) {
  5196. buf_func = section->section_body;
  5197. buf_func_end = buf_func + section->section_body_size;
  5198. }
  5199. section = section->next;
  5200. }
  5201. section = sections;
  5202. while (section) {
  5203. buf = section->section_body;
  5204. buf_end = buf + section->section_body_size;
  5205. switch (section->section_type) {
  5206. case SECTION_TYPE_USER:
  5207. /* unsupported user section, ignore it. */
  5208. if (!load_user_section(buf, buf_end, module,
  5209. reuse_const_strings, error_buf,
  5210. error_buf_size))
  5211. return false;
  5212. break;
  5213. case SECTION_TYPE_TYPE:
  5214. if (!load_type_section(buf, buf_end, module, error_buf,
  5215. error_buf_size))
  5216. return false;
  5217. break;
  5218. case SECTION_TYPE_IMPORT:
  5219. if (!load_import_section(buf, buf_end, module,
  5220. reuse_const_strings, no_resolve,
  5221. error_buf, error_buf_size))
  5222. return false;
  5223. break;
  5224. case SECTION_TYPE_FUNC:
  5225. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5226. module, error_buf, error_buf_size))
  5227. return false;
  5228. break;
  5229. case SECTION_TYPE_TABLE:
  5230. if (!load_table_section(buf, buf_end, module, error_buf,
  5231. error_buf_size))
  5232. return false;
  5233. break;
  5234. case SECTION_TYPE_MEMORY:
  5235. if (!load_memory_section(buf, buf_end, module, error_buf,
  5236. error_buf_size))
  5237. return false;
  5238. break;
  5239. #if WASM_ENABLE_TAGS != 0
  5240. case SECTION_TYPE_TAG:
  5241. /* load tag declaration section */
  5242. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5243. module, error_buf, error_buf_size))
  5244. return false;
  5245. break;
  5246. #endif
  5247. case SECTION_TYPE_GLOBAL:
  5248. if (!load_global_section(buf, buf_end, module, error_buf,
  5249. error_buf_size))
  5250. return false;
  5251. break;
  5252. case SECTION_TYPE_EXPORT:
  5253. if (!load_export_section(buf, buf_end, module,
  5254. reuse_const_strings, error_buf,
  5255. error_buf_size))
  5256. return false;
  5257. break;
  5258. case SECTION_TYPE_START:
  5259. if (!load_start_section(buf, buf_end, module, error_buf,
  5260. error_buf_size))
  5261. return false;
  5262. break;
  5263. case SECTION_TYPE_ELEM:
  5264. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5265. error_buf_size))
  5266. return false;
  5267. break;
  5268. case SECTION_TYPE_CODE:
  5269. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5270. module, error_buf, error_buf_size))
  5271. return false;
  5272. break;
  5273. case SECTION_TYPE_DATA:
  5274. if (!load_data_segment_section(buf, buf_end, module,
  5275. #if WASM_ENABLE_BULK_MEMORY != 0
  5276. has_datacount_section,
  5277. #endif
  5278. clone_data_seg, error_buf,
  5279. error_buf_size))
  5280. return false;
  5281. break;
  5282. #if WASM_ENABLE_BULK_MEMORY != 0
  5283. case SECTION_TYPE_DATACOUNT:
  5284. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5285. error_buf_size))
  5286. return false;
  5287. has_datacount_section = true;
  5288. break;
  5289. #endif
  5290. #if WASM_ENABLE_STRINGREF != 0
  5291. case SECTION_TYPE_STRINGREF:
  5292. if (!load_stringref_section(buf, buf_end, module,
  5293. reuse_const_strings, error_buf,
  5294. error_buf_size))
  5295. return false;
  5296. break;
  5297. #endif
  5298. default:
  5299. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5300. return false;
  5301. }
  5302. section = section->next;
  5303. }
  5304. #if WASM_ENABLE_BULK_MEMORY != 0
  5305. if (!check_data_count_consistency(
  5306. has_datacount_section, module->data_seg_count1,
  5307. module->data_seg_count, error_buf, error_buf_size)) {
  5308. return false;
  5309. }
  5310. #endif
  5311. module->aux_data_end_global_index = (uint32)-1;
  5312. module->aux_heap_base_global_index = (uint32)-1;
  5313. module->aux_stack_top_global_index = (uint32)-1;
  5314. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5315. export = module->exports;
  5316. for (i = 0; i < module->export_count; i++, export ++) {
  5317. if (export->kind == EXPORT_KIND_GLOBAL) {
  5318. if (!strcmp(export->name, "__heap_base")) {
  5319. if (export->index < module->import_global_count) {
  5320. LOG_DEBUG("Skip the process if __heap_base is imported "
  5321. "instead of being a local global");
  5322. continue;
  5323. }
  5324. /* only process linker-generated symbols */
  5325. global_index = export->index - module->import_global_count;
  5326. global = module->globals + global_index;
  5327. if (global->type.val_type == VALUE_TYPE_I32
  5328. && !global->type.is_mutable
  5329. && global->init_expr.init_expr_type
  5330. == INIT_EXPR_TYPE_I32_CONST) {
  5331. aux_heap_base_global = global;
  5332. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5333. aux_heap_base_global_index = export->index;
  5334. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5335. aux_heap_base);
  5336. }
  5337. }
  5338. else if (!strcmp(export->name, "__data_end")) {
  5339. if (export->index < module->import_global_count) {
  5340. LOG_DEBUG("Skip the process if __data_end is imported "
  5341. "instead of being a local global");
  5342. continue;
  5343. }
  5344. /* only process linker-generated symbols */
  5345. global_index = export->index - module->import_global_count;
  5346. global = module->globals + global_index;
  5347. if (global->type.val_type == VALUE_TYPE_I32
  5348. && !global->type.is_mutable
  5349. && global->init_expr.init_expr_type
  5350. == INIT_EXPR_TYPE_I32_CONST) {
  5351. aux_data_end_global = global;
  5352. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5353. aux_data_end_global_index = export->index;
  5354. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5355. aux_data_end);
  5356. aux_data_end = align_uint64(aux_data_end, 16);
  5357. }
  5358. }
  5359. /* For module compiled with -pthread option, the global is:
  5360. [0] stack_top <-- 0
  5361. [1] tls_pointer
  5362. [2] tls_size
  5363. [3] data_end <-- 3
  5364. [4] global_base
  5365. [5] heap_base <-- 5
  5366. [6] dso_handle
  5367. For module compiled without -pthread option:
  5368. [0] stack_top <-- 0
  5369. [1] data_end <-- 1
  5370. [2] global_base
  5371. [3] heap_base <-- 3
  5372. [4] dso_handle
  5373. */
  5374. if (aux_data_end_global && aux_heap_base_global
  5375. && aux_data_end <= aux_heap_base) {
  5376. module->aux_data_end_global_index = aux_data_end_global_index;
  5377. module->aux_data_end = aux_data_end;
  5378. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5379. module->aux_heap_base = aux_heap_base;
  5380. /* Resolve aux stack top global */
  5381. for (global_index = 0; global_index < module->global_count;
  5382. global_index++) {
  5383. global = module->globals + global_index;
  5384. if (global->type.is_mutable /* heap_base and data_end is
  5385. not mutable */
  5386. && global->type.val_type == VALUE_TYPE_I32
  5387. && global->init_expr.init_expr_type
  5388. == INIT_EXPR_TYPE_I32_CONST
  5389. && (uint64)(uint32)global->init_expr.u.i32
  5390. <= aux_heap_base) {
  5391. aux_stack_top_global = global;
  5392. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5393. module->aux_stack_top_global_index =
  5394. module->import_global_count + global_index;
  5395. module->aux_stack_bottom = aux_stack_top;
  5396. module->aux_stack_size =
  5397. aux_stack_top > aux_data_end
  5398. ? (uint32)(aux_stack_top - aux_data_end)
  5399. : (uint32)aux_stack_top;
  5400. LOG_VERBOSE(
  5401. "Found aux stack top global, value: %" PRIu64 ", "
  5402. "global index: %d, stack size: %d",
  5403. aux_stack_top, global_index,
  5404. module->aux_stack_size);
  5405. break;
  5406. }
  5407. }
  5408. if (!aux_stack_top_global) {
  5409. /* Auxiliary stack global isn't found, it must be unused
  5410. in the wasm app, as if it is used, the global must be
  5411. defined. Here we set it to __heap_base global and set
  5412. its size to 0. */
  5413. aux_stack_top_global = aux_heap_base_global;
  5414. aux_stack_top = aux_heap_base;
  5415. module->aux_stack_top_global_index =
  5416. module->aux_heap_base_global_index;
  5417. module->aux_stack_bottom = aux_stack_top;
  5418. module->aux_stack_size = 0;
  5419. }
  5420. break;
  5421. }
  5422. }
  5423. }
  5424. module->malloc_function = (uint32)-1;
  5425. module->free_function = (uint32)-1;
  5426. module->retain_function = (uint32)-1;
  5427. /* Resolve malloc/free function exported by wasm module */
  5428. #if WASM_ENABLE_MEMORY64 != 0
  5429. if (has_module_memory64(module))
  5430. malloc_free_io_type = VALUE_TYPE_I64;
  5431. #endif
  5432. export = module->exports;
  5433. for (i = 0; i < module->export_count; i++, export ++) {
  5434. if (export->kind == EXPORT_KIND_FUNC) {
  5435. if (!strcmp(export->name, "malloc")
  5436. && export->index >= module->import_function_count) {
  5437. func_index = export->index - module->import_function_count;
  5438. func_type = module->functions[func_index]->func_type;
  5439. if (func_type->param_count == 1 && func_type->result_count == 1
  5440. && func_type->types[0] == malloc_free_io_type
  5441. && func_type->types[1] == malloc_free_io_type) {
  5442. bh_assert(module->malloc_function == (uint32)-1);
  5443. module->malloc_function = export->index;
  5444. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5445. export->name, export->index);
  5446. }
  5447. }
  5448. else if (!strcmp(export->name, "__new")
  5449. && export->index >= module->import_function_count) {
  5450. /* __new && __pin for AssemblyScript */
  5451. func_index = export->index - module->import_function_count;
  5452. func_type = module->functions[func_index]->func_type;
  5453. if (func_type->param_count == 2 && func_type->result_count == 1
  5454. && func_type->types[0] == malloc_free_io_type
  5455. && func_type->types[1] == VALUE_TYPE_I32
  5456. && func_type->types[2] == malloc_free_io_type) {
  5457. uint32 j;
  5458. WASMExport *export_tmp;
  5459. bh_assert(module->malloc_function == (uint32)-1);
  5460. module->malloc_function = export->index;
  5461. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5462. export->name, export->index);
  5463. /* resolve retain function.
  5464. If not found, reset malloc function index */
  5465. export_tmp = module->exports;
  5466. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5467. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5468. && (!strcmp(export_tmp->name, "__retain")
  5469. || (!strcmp(export_tmp->name, "__pin")))
  5470. && (export_tmp->index
  5471. >= module->import_function_count)) {
  5472. func_index = export_tmp->index
  5473. - module->import_function_count;
  5474. func_type =
  5475. module->functions[func_index]->func_type;
  5476. if (func_type->param_count == 1
  5477. && func_type->result_count == 1
  5478. && func_type->types[0] == malloc_free_io_type
  5479. && func_type->types[1] == malloc_free_io_type) {
  5480. bh_assert(module->retain_function
  5481. == (uint32)-1);
  5482. module->retain_function = export_tmp->index;
  5483. LOG_VERBOSE("Found retain function, name: %s, "
  5484. "index: %u",
  5485. export_tmp->name,
  5486. export_tmp->index);
  5487. break;
  5488. }
  5489. }
  5490. }
  5491. if (j == module->export_count) {
  5492. module->malloc_function = (uint32)-1;
  5493. LOG_VERBOSE("Can't find retain function,"
  5494. "reset malloc function index to -1");
  5495. }
  5496. }
  5497. }
  5498. else if (((!strcmp(export->name, "free"))
  5499. || (!strcmp(export->name, "__release"))
  5500. || (!strcmp(export->name, "__unpin")))
  5501. && export->index >= module->import_function_count) {
  5502. func_index = export->index - module->import_function_count;
  5503. func_type = module->functions[func_index]->func_type;
  5504. if (func_type->param_count == 1 && func_type->result_count == 0
  5505. && func_type->types[0] == malloc_free_io_type) {
  5506. bh_assert(module->free_function == (uint32)-1);
  5507. module->free_function = export->index;
  5508. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5509. export->name, export->index);
  5510. }
  5511. }
  5512. }
  5513. }
  5514. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5515. handle_table = wasm_interp_get_handle_table();
  5516. #endif
  5517. for (i = 0; i < module->function_count; i++) {
  5518. WASMFunction *func = module->functions[i];
  5519. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5520. error_buf_size)) {
  5521. return false;
  5522. }
  5523. if (i == module->function_count - 1
  5524. && func->code + func->code_size != buf_code_end) {
  5525. set_error_buf(error_buf, error_buf_size,
  5526. "code section size mismatch");
  5527. return false;
  5528. }
  5529. }
  5530. if (!module->possible_memory_grow) {
  5531. #if WASM_ENABLE_SHRUNK_MEMORY != 0
  5532. if (aux_data_end_global && aux_heap_base_global
  5533. && aux_stack_top_global) {
  5534. uint64 init_memory_size;
  5535. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5536. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5537. * valid range of uint32 */
  5538. if (shrunk_memory_size <= UINT32_MAX) {
  5539. if (module->import_memory_count) {
  5540. WASMMemoryImport *memory_import =
  5541. &module->import_memories[0].u.memory;
  5542. init_memory_size =
  5543. (uint64)memory_import->mem_type.num_bytes_per_page
  5544. * memory_import->mem_type.init_page_count;
  5545. if (shrunk_memory_size <= init_memory_size) {
  5546. /* Reset memory info to decrease memory usage */
  5547. memory_import->mem_type.num_bytes_per_page =
  5548. (uint32)shrunk_memory_size;
  5549. memory_import->mem_type.init_page_count = 1;
  5550. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5551. shrunk_memory_size);
  5552. }
  5553. }
  5554. if (module->memory_count) {
  5555. WASMMemory *memory = &module->memories[0];
  5556. init_memory_size = (uint64)memory->num_bytes_per_page
  5557. * memory->init_page_count;
  5558. if (shrunk_memory_size <= init_memory_size) {
  5559. /* Reset memory info to decrease memory usage */
  5560. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5561. memory->init_page_count = 1;
  5562. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5563. shrunk_memory_size);
  5564. }
  5565. }
  5566. }
  5567. }
  5568. #endif /* WASM_ENABLE_SHRUNK_MEMORY != 0 */
  5569. #if WASM_ENABLE_MULTI_MODULE == 0
  5570. if (module->import_memory_count) {
  5571. WASMMemoryImport *memory_import =
  5572. &module->import_memories[0].u.memory;
  5573. /* Only resize the memory to one big page if num_bytes_per_page is
  5574. * in valid range of uint32 */
  5575. if (memory_import->mem_type.init_page_count < DEFAULT_MAX_PAGES) {
  5576. memory_import->mem_type.num_bytes_per_page *=
  5577. memory_import->mem_type.init_page_count;
  5578. if (memory_import->mem_type.init_page_count > 0)
  5579. memory_import->mem_type.init_page_count =
  5580. memory_import->mem_type.max_page_count = 1;
  5581. else
  5582. memory_import->mem_type.init_page_count =
  5583. memory_import->mem_type.max_page_count = 0;
  5584. }
  5585. }
  5586. if (module->memory_count) {
  5587. WASMMemory *memory = &module->memories[0];
  5588. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5589. * valid range of uint32 */
  5590. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5591. memory->num_bytes_per_page *= memory->init_page_count;
  5592. if (memory->init_page_count > 0)
  5593. memory->init_page_count = memory->max_page_count = 1;
  5594. else
  5595. memory->init_page_count = memory->max_page_count = 0;
  5596. }
  5597. }
  5598. #endif
  5599. }
  5600. #if WASM_ENABLE_MEMORY64 != 0
  5601. if (!check_memory64_flags_consistency(module, error_buf, error_buf_size,
  5602. false))
  5603. return false;
  5604. #endif
  5605. calculate_global_data_offset(module);
  5606. #if WASM_ENABLE_FAST_JIT != 0
  5607. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5608. return false;
  5609. }
  5610. #endif
  5611. #if WASM_ENABLE_JIT != 0
  5612. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5613. return false;
  5614. }
  5615. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5616. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5617. return false;
  5618. }
  5619. #else
  5620. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5621. thread fast jit execution, since applying llvm optimizations in
  5622. aot_compile_wasm may cost a lot of time.
  5623. Create thread with enough native stack to apply llvm optimizations */
  5624. if (os_thread_create(&module->llvm_jit_init_thread,
  5625. init_llvm_jit_functions_stage2_callback,
  5626. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5627. != 0) {
  5628. set_error_buf(error_buf, error_buf_size,
  5629. "create orcjit compile thread failed");
  5630. return false;
  5631. }
  5632. #endif
  5633. #endif
  5634. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5635. /* Create threads to compile the jit functions */
  5636. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5637. return false;
  5638. }
  5639. #endif
  5640. #if WASM_ENABLE_MEMORY_TRACING != 0
  5641. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5642. #endif
  5643. return true;
  5644. }
  5645. static WASMModule *
  5646. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5647. {
  5648. WASMModule *module =
  5649. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5650. bh_list_status ret;
  5651. if (!module) {
  5652. return NULL;
  5653. }
  5654. module->module_type = Wasm_Module_Bytecode;
  5655. /* Set start_function to -1, means no start function */
  5656. module->start_function = (uint32)-1;
  5657. module->name = name;
  5658. module->is_binary_freeable = false;
  5659. #if WASM_ENABLE_FAST_INTERP == 0
  5660. module->br_table_cache_list = &module->br_table_cache_list_head;
  5661. ret = bh_list_init(module->br_table_cache_list);
  5662. bh_assert(ret == BH_LIST_SUCCESS);
  5663. #endif
  5664. #if WASM_ENABLE_MULTI_MODULE != 0
  5665. module->import_module_list = &module->import_module_list_head;
  5666. ret = bh_list_init(module->import_module_list);
  5667. bh_assert(ret == BH_LIST_SUCCESS);
  5668. #endif
  5669. #if WASM_ENABLE_DEBUG_INTERP != 0
  5670. ret = bh_list_init(&module->fast_opcode_list);
  5671. bh_assert(ret == BH_LIST_SUCCESS);
  5672. #endif
  5673. #if WASM_ENABLE_GC != 0
  5674. if (!(module->ref_type_set =
  5675. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5676. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5677. goto fail1;
  5678. }
  5679. if (os_mutex_init(&module->rtt_type_lock)) {
  5680. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5681. goto fail2;
  5682. }
  5683. #endif
  5684. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5685. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5686. && WASM_ENABLE_LAZY_JIT != 0)
  5687. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5688. set_error_buf(error_buf, error_buf_size,
  5689. "init instance list lock failed");
  5690. goto fail3;
  5691. }
  5692. #endif
  5693. (void)ret;
  5694. return module;
  5695. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5696. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5697. && WASM_ENABLE_LAZY_JIT != 0)
  5698. fail3:
  5699. #endif
  5700. #if WASM_ENABLE_GC != 0
  5701. os_mutex_destroy(&module->rtt_type_lock);
  5702. fail2:
  5703. bh_hash_map_destroy(module->ref_type_set);
  5704. fail1:
  5705. #endif
  5706. wasm_runtime_free(module);
  5707. return NULL;
  5708. }
  5709. #if WASM_ENABLE_DEBUG_INTERP != 0
  5710. static bool
  5711. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5712. uint32 error_buf_size)
  5713. {
  5714. WASMFastOPCodeNode *fast_op =
  5715. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5716. if (fast_op) {
  5717. fast_op->offset = pos - module->load_addr;
  5718. fast_op->orig_op = orig_op;
  5719. bh_list_insert(&module->fast_opcode_list, fast_op);
  5720. }
  5721. return fast_op ? true : false;
  5722. }
  5723. #endif
  5724. WASMModule *
  5725. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5726. uint32 error_buf_size)
  5727. {
  5728. WASMModule *module = create_module("", error_buf, error_buf_size);
  5729. if (!module)
  5730. return NULL;
  5731. if (!load_from_sections(module, section_list, false, true, false, error_buf,
  5732. error_buf_size)) {
  5733. wasm_loader_unload(module);
  5734. return NULL;
  5735. }
  5736. LOG_VERBOSE("Load module from sections success.\n");
  5737. return module;
  5738. }
  5739. static void
  5740. destroy_sections(WASMSection *section_list)
  5741. {
  5742. WASMSection *section = section_list, *next;
  5743. while (section) {
  5744. next = section->next;
  5745. wasm_runtime_free(section);
  5746. section = next;
  5747. }
  5748. }
  5749. /* clang-format off */
  5750. static uint8 section_ids[] = {
  5751. SECTION_TYPE_USER,
  5752. SECTION_TYPE_TYPE,
  5753. SECTION_TYPE_IMPORT,
  5754. SECTION_TYPE_FUNC,
  5755. SECTION_TYPE_TABLE,
  5756. SECTION_TYPE_MEMORY,
  5757. #if WASM_ENABLE_TAGS != 0
  5758. SECTION_TYPE_TAG,
  5759. #endif
  5760. #if WASM_ENABLE_STRINGREF != 0
  5761. /* must immediately precede the global section,
  5762. or where the global section would be */
  5763. SECTION_TYPE_STRINGREF,
  5764. #endif
  5765. SECTION_TYPE_GLOBAL,
  5766. SECTION_TYPE_EXPORT,
  5767. SECTION_TYPE_START,
  5768. SECTION_TYPE_ELEM,
  5769. #if WASM_ENABLE_BULK_MEMORY != 0
  5770. SECTION_TYPE_DATACOUNT,
  5771. #endif
  5772. SECTION_TYPE_CODE,
  5773. SECTION_TYPE_DATA
  5774. };
  5775. /* clang-format on */
  5776. static uint8
  5777. get_section_index(uint8 section_type)
  5778. {
  5779. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5780. for (uint8 i = 0; i < max_id; i++) {
  5781. if (section_type == section_ids[i])
  5782. return i;
  5783. }
  5784. return (uint8)-1;
  5785. }
  5786. static bool
  5787. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5788. char *error_buf, uint32 error_buf_size)
  5789. {
  5790. WASMSection *section_list_end = NULL, *section;
  5791. const uint8 *p = buf, *p_end = buf + size;
  5792. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5793. uint32 section_size;
  5794. bh_assert(!*p_section_list);
  5795. p += 8;
  5796. while (p < p_end) {
  5797. CHECK_BUF(p, p_end, 1);
  5798. section_type = read_uint8(p);
  5799. section_index = get_section_index(section_type);
  5800. if (section_index != (uint8)-1) {
  5801. if (section_type != SECTION_TYPE_USER) {
  5802. /* Custom sections may be inserted at any place,
  5803. while other sections must occur at most once
  5804. and in prescribed order. */
  5805. if (last_section_index != (uint8)-1
  5806. && (section_index <= last_section_index)) {
  5807. set_error_buf(error_buf, error_buf_size,
  5808. "unexpected content after last section or "
  5809. "junk after last section");
  5810. return false;
  5811. }
  5812. last_section_index = section_index;
  5813. }
  5814. read_leb_uint32(p, p_end, section_size);
  5815. CHECK_BUF1(p, p_end, section_size);
  5816. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5817. error_buf_size))) {
  5818. return false;
  5819. }
  5820. section->section_type = section_type;
  5821. section->section_body = (uint8 *)p;
  5822. section->section_body_size = section_size;
  5823. if (!section_list_end)
  5824. *p_section_list = section_list_end = section;
  5825. else {
  5826. section_list_end->next = section;
  5827. section_list_end = section;
  5828. }
  5829. p += section_size;
  5830. }
  5831. else {
  5832. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5833. return false;
  5834. }
  5835. }
  5836. return true;
  5837. fail:
  5838. return false;
  5839. }
  5840. static void
  5841. exchange32(uint8 *p_data)
  5842. {
  5843. uint8 value = *p_data;
  5844. *p_data = *(p_data + 3);
  5845. *(p_data + 3) = value;
  5846. value = *(p_data + 1);
  5847. *(p_data + 1) = *(p_data + 2);
  5848. *(p_data + 2) = value;
  5849. }
  5850. static union {
  5851. int a;
  5852. char b;
  5853. } __ue = { .a = 1 };
  5854. #define is_little_endian() (__ue.b == 1)
  5855. static bool
  5856. load(const uint8 *buf, uint32 size, WASMModule *module,
  5857. bool wasm_binary_freeable, bool no_resolve, char *error_buf,
  5858. uint32 error_buf_size)
  5859. {
  5860. const uint8 *buf_end = buf + size;
  5861. const uint8 *p = buf, *p_end = buf_end;
  5862. uint32 magic_number, version;
  5863. WASMSection *section_list = NULL;
  5864. CHECK_BUF1(p, p_end, sizeof(uint32));
  5865. magic_number = read_uint32(p);
  5866. if (!is_little_endian())
  5867. exchange32((uint8 *)&magic_number);
  5868. if (magic_number != WASM_MAGIC_NUMBER) {
  5869. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5870. return false;
  5871. }
  5872. CHECK_BUF1(p, p_end, sizeof(uint32));
  5873. version = read_uint32(p);
  5874. if (!is_little_endian())
  5875. exchange32((uint8 *)&version);
  5876. if (version != WASM_CURRENT_VERSION) {
  5877. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5878. return false;
  5879. }
  5880. module->package_version = version;
  5881. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5882. || !load_from_sections(module, section_list, true, wasm_binary_freeable,
  5883. no_resolve, error_buf, error_buf_size)) {
  5884. destroy_sections(section_list);
  5885. return false;
  5886. }
  5887. destroy_sections(section_list);
  5888. return true;
  5889. fail:
  5890. return false;
  5891. }
  5892. #if WASM_ENABLE_LIBC_WASI != 0
  5893. /**
  5894. * refer to
  5895. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5896. */
  5897. static bool
  5898. check_wasi_abi_compatibility(const WASMModule *module,
  5899. #if WASM_ENABLE_MULTI_MODULE != 0
  5900. bool main_module,
  5901. #endif
  5902. char *error_buf, uint32 error_buf_size)
  5903. {
  5904. /**
  5905. * be careful with:
  5906. * wasi compatible modules(command/reactor) which don't import any wasi
  5907. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5908. * reactor can depend on nothing. At the same time, each has its own entry
  5909. * point.
  5910. *
  5911. * observations:
  5912. * - clang always injects `_start` into a command
  5913. * - clang always injects `_initialize` into a reactor
  5914. * - `iwasm -f` allows to run a function in the reactor
  5915. *
  5916. * strong assumptions:
  5917. * - no one will define either `_start` or `_initialize` on purpose
  5918. * - `_start` should always be `void _start(void)`
  5919. * - `_initialize` should always be `void _initialize(void)`
  5920. *
  5921. */
  5922. /* clang-format off */
  5923. /**
  5924. *
  5925. * | | import_wasi_api True | | import_wasi_api False | |
  5926. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5927. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5928. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5929. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5930. */
  5931. /* clang-format on */
  5932. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5933. uint32 import_function_count = module->import_function_count;
  5934. WASMFuncType *func_type;
  5935. /* (func (export "_start") (...) */
  5936. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5937. error_buf, error_buf_size);
  5938. if (start) {
  5939. if (start->index < import_function_count) {
  5940. set_error_buf(
  5941. error_buf, error_buf_size,
  5942. "the builtin _start function can not be an import function");
  5943. return false;
  5944. }
  5945. func_type =
  5946. module->functions[start->index - import_function_count]->func_type;
  5947. if (func_type->param_count || func_type->result_count) {
  5948. set_error_buf(error_buf, error_buf_size,
  5949. "the signature of builtin _start function is wrong");
  5950. return false;
  5951. }
  5952. }
  5953. else {
  5954. /* (func (export "_initialize") (...) */
  5955. initialize =
  5956. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5957. error_buf, error_buf_size);
  5958. if (initialize) {
  5959. if (initialize->index < import_function_count) {
  5960. set_error_buf(error_buf, error_buf_size,
  5961. "the builtin _initialize function can not be an "
  5962. "import function");
  5963. return false;
  5964. }
  5965. func_type =
  5966. module->functions[initialize->index - import_function_count]
  5967. ->func_type;
  5968. if (func_type->param_count || func_type->result_count) {
  5969. set_error_buf(
  5970. error_buf, error_buf_size,
  5971. "the signature of builtin _initialize function is wrong");
  5972. return false;
  5973. }
  5974. }
  5975. }
  5976. /* filter out non-wasi compatible modules */
  5977. if (!module->import_wasi_api && !start && !initialize) {
  5978. return true;
  5979. }
  5980. /* should have one at least */
  5981. if (module->import_wasi_api && !start && !initialize) {
  5982. LOG_WARNING("warning: a module with WASI apis should be either "
  5983. "a command or a reactor");
  5984. }
  5985. /*
  5986. * there is at least one of `_start` and `_initialize` in below cases.
  5987. * according to the assumption, they should be all wasi compatible
  5988. */
  5989. #if WASM_ENABLE_MULTI_MODULE != 0
  5990. /* filter out commands (with `_start`) cases */
  5991. if (start && !main_module) {
  5992. set_error_buf(
  5993. error_buf, error_buf_size,
  5994. "a command (with _start function) can not be a sub-module");
  5995. return false;
  5996. }
  5997. #endif
  5998. /*
  5999. * it is ok a reactor acts as a main module,
  6000. * so skip the check about (with `_initialize`)
  6001. */
  6002. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  6003. error_buf, error_buf_size);
  6004. if (!memory
  6005. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  6006. /*
  6007. * with wasi-threads, it's still an open question if a memory
  6008. * should be exported.
  6009. *
  6010. * https://github.com/WebAssembly/wasi-threads/issues/22
  6011. * https://github.com/WebAssembly/WASI/issues/502
  6012. *
  6013. * Note: this code assumes the number of memories is at most 1.
  6014. */
  6015. && module->import_memory_count == 0
  6016. #endif
  6017. ) {
  6018. set_error_buf(error_buf, error_buf_size,
  6019. "a module with WASI apis must export memory by default");
  6020. return false;
  6021. }
  6022. return true;
  6023. }
  6024. #endif
  6025. WASMModule *
  6026. wasm_loader_load(uint8 *buf, uint32 size,
  6027. #if WASM_ENABLE_MULTI_MODULE != 0
  6028. bool main_module,
  6029. #endif
  6030. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  6031. {
  6032. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  6033. if (!module) {
  6034. return NULL;
  6035. }
  6036. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  6037. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  6038. module->load_addr = (uint8 *)buf;
  6039. module->load_size = size;
  6040. #endif
  6041. if (!load(buf, size, module, args->wasm_binary_freeable, args->no_resolve,
  6042. error_buf, error_buf_size)) {
  6043. goto fail;
  6044. }
  6045. #if WASM_ENABLE_LIBC_WASI != 0
  6046. /* Check the WASI application ABI */
  6047. if (!check_wasi_abi_compatibility(module,
  6048. #if WASM_ENABLE_MULTI_MODULE != 0
  6049. main_module,
  6050. #endif
  6051. error_buf, error_buf_size)) {
  6052. goto fail;
  6053. }
  6054. #endif
  6055. LOG_VERBOSE("Load module success.\n");
  6056. return module;
  6057. fail:
  6058. wasm_loader_unload(module);
  6059. return NULL;
  6060. }
  6061. void
  6062. wasm_loader_unload(WASMModule *module)
  6063. {
  6064. uint32 i;
  6065. if (!module)
  6066. return;
  6067. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6068. && WASM_ENABLE_LAZY_JIT != 0
  6069. module->orcjit_stop_compiling = true;
  6070. if (module->llvm_jit_init_thread)
  6071. os_thread_join(module->llvm_jit_init_thread, NULL);
  6072. #endif
  6073. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  6074. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  6075. module internal data after they were freed */
  6076. orcjit_stop_compile_threads(module);
  6077. #endif
  6078. #if WASM_ENABLE_JIT != 0
  6079. if (module->func_ptrs)
  6080. wasm_runtime_free(module->func_ptrs);
  6081. if (module->comp_ctx)
  6082. aot_destroy_comp_context(module->comp_ctx);
  6083. if (module->comp_data)
  6084. aot_destroy_comp_data(module->comp_data);
  6085. #endif
  6086. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6087. && WASM_ENABLE_LAZY_JIT != 0
  6088. if (module->tierup_wait_lock_inited) {
  6089. os_mutex_destroy(&module->tierup_wait_lock);
  6090. os_cond_destroy(&module->tierup_wait_cond);
  6091. }
  6092. #endif
  6093. if (module->imports)
  6094. wasm_runtime_free(module->imports);
  6095. if (module->functions) {
  6096. for (i = 0; i < module->function_count; i++) {
  6097. if (module->functions[i]) {
  6098. if (module->functions[i]->local_offsets)
  6099. wasm_runtime_free(module->functions[i]->local_offsets);
  6100. #if WASM_ENABLE_FAST_INTERP != 0
  6101. if (module->functions[i]->code_compiled)
  6102. wasm_runtime_free(module->functions[i]->code_compiled);
  6103. if (module->functions[i]->consts)
  6104. wasm_runtime_free(module->functions[i]->consts);
  6105. #endif
  6106. #if WASM_ENABLE_FAST_JIT != 0
  6107. if (module->functions[i]->fast_jit_jitted_code) {
  6108. jit_code_cache_free(
  6109. module->functions[i]->fast_jit_jitted_code);
  6110. }
  6111. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  6112. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  6113. jit_code_cache_free(
  6114. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  6115. }
  6116. #endif
  6117. #endif
  6118. #if WASM_ENABLE_GC != 0
  6119. if (module->functions[i]->local_ref_type_maps) {
  6120. wasm_runtime_free(
  6121. module->functions[i]->local_ref_type_maps);
  6122. }
  6123. #endif
  6124. wasm_runtime_free(module->functions[i]);
  6125. }
  6126. }
  6127. wasm_runtime_free(module->functions);
  6128. }
  6129. if (module->tables) {
  6130. #if WASM_ENABLE_GC != 0
  6131. for (i = 0; i < module->table_count; i++) {
  6132. destroy_init_expr(module, &module->tables[i].init_expr);
  6133. }
  6134. #endif
  6135. wasm_runtime_free(module->tables);
  6136. }
  6137. if (module->memories)
  6138. wasm_runtime_free(module->memories);
  6139. if (module->globals) {
  6140. #if WASM_ENABLE_GC != 0
  6141. for (i = 0; i < module->global_count; i++) {
  6142. destroy_init_expr(module, &module->globals[i].init_expr);
  6143. }
  6144. #endif
  6145. wasm_runtime_free(module->globals);
  6146. }
  6147. #if WASM_ENABLE_TAGS != 0
  6148. if (module->tags) {
  6149. for (i = 0; i < module->tag_count; i++) {
  6150. if (module->tags[i])
  6151. wasm_runtime_free(module->tags[i]);
  6152. }
  6153. wasm_runtime_free(module->tags);
  6154. }
  6155. #endif
  6156. if (module->exports)
  6157. wasm_runtime_free(module->exports);
  6158. if (module->table_segments) {
  6159. for (i = 0; i < module->table_seg_count; i++) {
  6160. if (module->table_segments[i].init_values) {
  6161. #if WASM_ENABLE_GC != 0
  6162. uint32 j;
  6163. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6164. destroy_init_expr(
  6165. module, &module->table_segments[i].init_values[j]);
  6166. }
  6167. #endif
  6168. wasm_runtime_free(module->table_segments[i].init_values);
  6169. }
  6170. }
  6171. wasm_runtime_free(module->table_segments);
  6172. }
  6173. if (module->data_segments) {
  6174. for (i = 0; i < module->data_seg_count; i++) {
  6175. if (module->data_segments[i]) {
  6176. if (module->data_segments[i]->is_data_cloned)
  6177. wasm_runtime_free(module->data_segments[i]->data);
  6178. wasm_runtime_free(module->data_segments[i]);
  6179. }
  6180. }
  6181. wasm_runtime_free(module->data_segments);
  6182. }
  6183. if (module->types) {
  6184. for (i = 0; i < module->type_count; i++) {
  6185. if (module->types[i])
  6186. destroy_wasm_type(module->types[i]);
  6187. }
  6188. wasm_runtime_free(module->types);
  6189. }
  6190. if (module->const_str_list) {
  6191. StringNode *node = module->const_str_list, *node_next;
  6192. while (node) {
  6193. node_next = node->next;
  6194. wasm_runtime_free(node);
  6195. node = node_next;
  6196. }
  6197. }
  6198. #if WASM_ENABLE_STRINGREF != 0
  6199. if (module->string_literal_ptrs) {
  6200. wasm_runtime_free((void *)module->string_literal_ptrs);
  6201. }
  6202. if (module->string_literal_lengths) {
  6203. wasm_runtime_free(module->string_literal_lengths);
  6204. }
  6205. #endif
  6206. #if WASM_ENABLE_FAST_INTERP == 0
  6207. if (module->br_table_cache_list) {
  6208. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6209. BrTableCache *node_next;
  6210. while (node) {
  6211. node_next = bh_list_elem_next(node);
  6212. wasm_runtime_free(node);
  6213. node = node_next;
  6214. }
  6215. }
  6216. #endif
  6217. #if WASM_ENABLE_MULTI_MODULE != 0
  6218. /* just release the sub module list */
  6219. if (module->import_module_list) {
  6220. WASMRegisteredModule *node =
  6221. bh_list_first_elem(module->import_module_list);
  6222. while (node) {
  6223. WASMRegisteredModule *next = bh_list_elem_next(node);
  6224. bh_list_remove(module->import_module_list, node);
  6225. /*
  6226. * unload(sub_module) will be triggered during runtime_destroy().
  6227. * every module in the global module list will be unloaded one by
  6228. * one. so don't worry.
  6229. */
  6230. wasm_runtime_free(node);
  6231. /*
  6232. * the module file reading buffer will be released
  6233. * in runtime_destroy()
  6234. */
  6235. node = next;
  6236. }
  6237. }
  6238. #endif
  6239. #if WASM_ENABLE_DEBUG_INTERP != 0
  6240. WASMFastOPCodeNode *fast_opcode =
  6241. bh_list_first_elem(&module->fast_opcode_list);
  6242. while (fast_opcode) {
  6243. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6244. wasm_runtime_free(fast_opcode);
  6245. fast_opcode = next;
  6246. }
  6247. #endif
  6248. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6249. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6250. && WASM_ENABLE_LAZY_JIT != 0)
  6251. os_mutex_destroy(&module->instance_list_lock);
  6252. #endif
  6253. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6254. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6255. #endif
  6256. #if WASM_ENABLE_FAST_JIT != 0
  6257. if (module->fast_jit_func_ptrs) {
  6258. wasm_runtime_free(module->fast_jit_func_ptrs);
  6259. }
  6260. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6261. if (module->fast_jit_thread_locks_inited[i]) {
  6262. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6263. }
  6264. }
  6265. #endif
  6266. #if WASM_ENABLE_GC != 0
  6267. os_mutex_destroy(&module->rtt_type_lock);
  6268. bh_hash_map_destroy(module->ref_type_set);
  6269. if (module->rtt_types) {
  6270. for (i = 0; i < module->type_count; i++) {
  6271. if (module->rtt_types[i])
  6272. wasm_runtime_free(module->rtt_types[i]);
  6273. }
  6274. wasm_runtime_free(module->rtt_types);
  6275. }
  6276. #if WASM_ENABLE_STRINGREF != 0
  6277. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6278. if (module->stringref_rtts[i])
  6279. wasm_runtime_free(module->stringref_rtts[i]);
  6280. }
  6281. #endif
  6282. #endif
  6283. wasm_runtime_free(module);
  6284. }
  6285. bool
  6286. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6287. const uint8 *start_addr, const uint8 *code_end_addr,
  6288. uint8 label_type, uint8 **p_else_addr,
  6289. uint8 **p_end_addr)
  6290. {
  6291. const uint8 *p = start_addr, *p_end = code_end_addr;
  6292. uint8 *else_addr = NULL;
  6293. char error_buf[128];
  6294. uint32 block_nested_depth = 1, count, i, j, t;
  6295. uint32 error_buf_size = sizeof(error_buf);
  6296. uint8 opcode, u8;
  6297. BlockAddr block_stack[16] = { { 0 } }, *block;
  6298. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6299. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6300. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6301. if (block[j].start_addr == start_addr) {
  6302. /* Cache hit */
  6303. *p_else_addr = block[j].else_addr;
  6304. *p_end_addr = block[j].end_addr;
  6305. return true;
  6306. }
  6307. }
  6308. /* Cache unhit */
  6309. block_stack[0].start_addr = start_addr;
  6310. while (p < code_end_addr) {
  6311. opcode = *p++;
  6312. #if WASM_ENABLE_DEBUG_INTERP != 0
  6313. op_break_retry:
  6314. #endif
  6315. switch (opcode) {
  6316. case WASM_OP_UNREACHABLE:
  6317. case WASM_OP_NOP:
  6318. break;
  6319. #if WASM_ENABLE_EXCE_HANDLING != 0
  6320. case WASM_OP_TRY:
  6321. u8 = read_uint8(p);
  6322. if (block_nested_depth
  6323. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6324. block_stack[block_nested_depth].start_addr = p;
  6325. block_stack[block_nested_depth].else_addr = NULL;
  6326. }
  6327. block_nested_depth++;
  6328. break;
  6329. case EXT_OP_TRY:
  6330. skip_leb_uint32(p, p_end);
  6331. if (block_nested_depth
  6332. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6333. block_stack[block_nested_depth].start_addr = p;
  6334. block_stack[block_nested_depth].else_addr = NULL;
  6335. }
  6336. block_nested_depth++;
  6337. break;
  6338. case WASM_OP_CATCH:
  6339. if (block_nested_depth == 1) {
  6340. *p_end_addr = (uint8 *)(p - 1);
  6341. /* stop search and return the address of the catch block */
  6342. return true;
  6343. }
  6344. break;
  6345. case WASM_OP_CATCH_ALL:
  6346. if (block_nested_depth == 1) {
  6347. *p_end_addr = (uint8 *)(p - 1);
  6348. /* stop search and return the address of the catch_all block
  6349. */
  6350. return true;
  6351. }
  6352. break;
  6353. case WASM_OP_THROW:
  6354. /* skip tag_index */
  6355. skip_leb(p);
  6356. break;
  6357. case WASM_OP_RETHROW:
  6358. /* skip depth */
  6359. skip_leb(p);
  6360. break;
  6361. case WASM_OP_DELEGATE:
  6362. if (block_nested_depth == 1) {
  6363. *p_end_addr = (uint8 *)(p - 1);
  6364. return true;
  6365. }
  6366. else {
  6367. skip_leb(p);
  6368. /* the DELEGATE opcode ends the tryblock, */
  6369. block_nested_depth--;
  6370. if (block_nested_depth
  6371. < sizeof(block_stack) / sizeof(BlockAddr))
  6372. block_stack[block_nested_depth].end_addr =
  6373. (uint8 *)(p - 1);
  6374. }
  6375. break;
  6376. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6377. case WASM_OP_BLOCK:
  6378. case WASM_OP_LOOP:
  6379. case WASM_OP_IF:
  6380. {
  6381. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6382. u8 = read_uint8(p);
  6383. if (is_byte_a_type(u8)) {
  6384. #if WASM_ENABLE_GC != 0
  6385. if (wasm_is_type_multi_byte_type(u8)) {
  6386. /* the possible extra bytes of GC ref type have been
  6387. modified to OP_NOP, no need to resolve them again */
  6388. }
  6389. #endif
  6390. }
  6391. else {
  6392. p--;
  6393. /* block type */
  6394. skip_leb_int32(p, p_end);
  6395. }
  6396. if (block_nested_depth
  6397. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6398. block_stack[block_nested_depth].start_addr = p;
  6399. block_stack[block_nested_depth].else_addr = NULL;
  6400. }
  6401. block_nested_depth++;
  6402. break;
  6403. }
  6404. case EXT_OP_BLOCK:
  6405. case EXT_OP_LOOP:
  6406. case EXT_OP_IF:
  6407. /* block type */
  6408. skip_leb_int32(p, p_end);
  6409. if (block_nested_depth
  6410. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6411. block_stack[block_nested_depth].start_addr = p;
  6412. block_stack[block_nested_depth].else_addr = NULL;
  6413. }
  6414. block_nested_depth++;
  6415. break;
  6416. case WASM_OP_ELSE:
  6417. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6418. else_addr = (uint8 *)(p - 1);
  6419. if (block_nested_depth - 1
  6420. < sizeof(block_stack) / sizeof(BlockAddr))
  6421. block_stack[block_nested_depth - 1].else_addr =
  6422. (uint8 *)(p - 1);
  6423. break;
  6424. case WASM_OP_END:
  6425. if (block_nested_depth == 1) {
  6426. if (label_type == LABEL_TYPE_IF)
  6427. *p_else_addr = else_addr;
  6428. *p_end_addr = (uint8 *)(p - 1);
  6429. block_stack[0].end_addr = (uint8 *)(p - 1);
  6430. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6431. t++) {
  6432. start_addr = block_stack[t].start_addr;
  6433. if (start_addr) {
  6434. i = ((uintptr_t)start_addr)
  6435. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6436. block =
  6437. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6438. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6439. if (!block[j].start_addr)
  6440. break;
  6441. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6442. memmove(block + 1, block,
  6443. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6444. * sizeof(BlockAddr));
  6445. j = 0;
  6446. }
  6447. block[j].start_addr = block_stack[t].start_addr;
  6448. block[j].else_addr = block_stack[t].else_addr;
  6449. block[j].end_addr = block_stack[t].end_addr;
  6450. }
  6451. else
  6452. break;
  6453. }
  6454. return true;
  6455. }
  6456. else {
  6457. block_nested_depth--;
  6458. if (block_nested_depth
  6459. < sizeof(block_stack) / sizeof(BlockAddr))
  6460. block_stack[block_nested_depth].end_addr =
  6461. (uint8 *)(p - 1);
  6462. }
  6463. break;
  6464. case WASM_OP_BR:
  6465. case WASM_OP_BR_IF:
  6466. skip_leb_uint32(p, p_end); /* labelidx */
  6467. break;
  6468. case WASM_OP_BR_TABLE:
  6469. read_leb_uint32(p, p_end, count); /* label num */
  6470. #if WASM_ENABLE_FAST_INTERP != 0
  6471. for (i = 0; i <= count; i++) /* labelidxs */
  6472. skip_leb_uint32(p, p_end);
  6473. #else
  6474. p += count + 1;
  6475. while (*p == WASM_OP_NOP)
  6476. p++;
  6477. #endif
  6478. break;
  6479. #if WASM_ENABLE_FAST_INTERP == 0
  6480. case EXT_OP_BR_TABLE_CACHE:
  6481. read_leb_uint32(p, p_end, count); /* label num */
  6482. while (*p == WASM_OP_NOP)
  6483. p++;
  6484. break;
  6485. #endif
  6486. case WASM_OP_RETURN:
  6487. break;
  6488. case WASM_OP_CALL:
  6489. #if WASM_ENABLE_TAIL_CALL != 0
  6490. case WASM_OP_RETURN_CALL:
  6491. #endif
  6492. skip_leb_uint32(p, p_end); /* funcidx */
  6493. break;
  6494. case WASM_OP_CALL_INDIRECT:
  6495. #if WASM_ENABLE_TAIL_CALL != 0
  6496. case WASM_OP_RETURN_CALL_INDIRECT:
  6497. #endif
  6498. skip_leb_uint32(p, p_end); /* typeidx */
  6499. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6500. skip_leb_uint32(p, p_end); /* tableidx */
  6501. #else
  6502. u8 = read_uint8(p); /* 0x00 */
  6503. #endif
  6504. break;
  6505. #if WASM_ENABLE_GC != 0
  6506. case WASM_OP_CALL_REF:
  6507. case WASM_OP_RETURN_CALL_REF:
  6508. skip_leb_uint32(p, p_end); /* typeidx */
  6509. break;
  6510. #endif
  6511. case WASM_OP_DROP:
  6512. case WASM_OP_SELECT:
  6513. case WASM_OP_DROP_64:
  6514. case WASM_OP_SELECT_64:
  6515. break;
  6516. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6517. case WASM_OP_SELECT_T:
  6518. {
  6519. skip_leb_uint32(p, p_end); /* vec length */
  6520. u8 = read_uint8(p); /* typeidx */
  6521. /* the possible extra bytes of GC ref type have been
  6522. modified to OP_NOP, no need to resolve them again */
  6523. break;
  6524. }
  6525. case WASM_OP_TABLE_GET:
  6526. case WASM_OP_TABLE_SET:
  6527. skip_leb_uint32(p, p_end); /* table index */
  6528. break;
  6529. case WASM_OP_REF_NULL:
  6530. {
  6531. u8 = read_uint8(p); /* type */
  6532. if (is_byte_a_type(u8)) {
  6533. #if WASM_ENABLE_GC != 0
  6534. if (wasm_is_type_multi_byte_type(u8)) {
  6535. /* the possible extra bytes of GC ref type have been
  6536. modified to OP_NOP, no need to resolve them again */
  6537. }
  6538. #endif
  6539. }
  6540. else {
  6541. p--;
  6542. skip_leb_uint32(p, p_end);
  6543. }
  6544. break;
  6545. }
  6546. case WASM_OP_REF_IS_NULL:
  6547. break;
  6548. case WASM_OP_REF_FUNC:
  6549. skip_leb_uint32(p, p_end); /* func index */
  6550. break;
  6551. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6552. #if WASM_ENABLE_GC != 0
  6553. case WASM_OP_REF_AS_NON_NULL:
  6554. case WASM_OP_REF_EQ:
  6555. break;
  6556. case WASM_OP_BR_ON_NULL:
  6557. case WASM_OP_BR_ON_NON_NULL:
  6558. skip_leb_uint32(p, p_end); /* label index */
  6559. break;
  6560. #endif /* end of WASM_ENABLE_GC != 0 */
  6561. case WASM_OP_GET_LOCAL:
  6562. case WASM_OP_SET_LOCAL:
  6563. case WASM_OP_TEE_LOCAL:
  6564. case WASM_OP_GET_GLOBAL:
  6565. case WASM_OP_SET_GLOBAL:
  6566. case WASM_OP_GET_GLOBAL_64:
  6567. case WASM_OP_SET_GLOBAL_64:
  6568. #if WASM_ENABLE_SIMDE != 0
  6569. case WASM_OP_GET_GLOBAL_V128:
  6570. case WASM_OP_SET_GLOBAL_V128:
  6571. #endif
  6572. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6573. skip_leb_uint32(p, p_end); /* local index */
  6574. break;
  6575. case EXT_OP_GET_LOCAL_FAST:
  6576. case EXT_OP_SET_LOCAL_FAST:
  6577. case EXT_OP_TEE_LOCAL_FAST:
  6578. CHECK_BUF(p, p_end, 1);
  6579. p++;
  6580. break;
  6581. case WASM_OP_I32_LOAD:
  6582. case WASM_OP_I64_LOAD:
  6583. case WASM_OP_F32_LOAD:
  6584. case WASM_OP_F64_LOAD:
  6585. case WASM_OP_I32_LOAD8_S:
  6586. case WASM_OP_I32_LOAD8_U:
  6587. case WASM_OP_I32_LOAD16_S:
  6588. case WASM_OP_I32_LOAD16_U:
  6589. case WASM_OP_I64_LOAD8_S:
  6590. case WASM_OP_I64_LOAD8_U:
  6591. case WASM_OP_I64_LOAD16_S:
  6592. case WASM_OP_I64_LOAD16_U:
  6593. case WASM_OP_I64_LOAD32_S:
  6594. case WASM_OP_I64_LOAD32_U:
  6595. case WASM_OP_I32_STORE:
  6596. case WASM_OP_I64_STORE:
  6597. case WASM_OP_F32_STORE:
  6598. case WASM_OP_F64_STORE:
  6599. case WASM_OP_I32_STORE8:
  6600. case WASM_OP_I32_STORE16:
  6601. case WASM_OP_I64_STORE8:
  6602. case WASM_OP_I64_STORE16:
  6603. case WASM_OP_I64_STORE32:
  6604. skip_leb_align(p, p_end); /* align */
  6605. skip_leb_mem_offset(p, p_end); /* offset */
  6606. break;
  6607. case WASM_OP_MEMORY_SIZE:
  6608. case WASM_OP_MEMORY_GROW:
  6609. skip_leb_memidx(p, p_end); /* memidx */
  6610. break;
  6611. case WASM_OP_I32_CONST:
  6612. skip_leb_int32(p, p_end);
  6613. break;
  6614. case WASM_OP_I64_CONST:
  6615. skip_leb_int64(p, p_end);
  6616. break;
  6617. case WASM_OP_F32_CONST:
  6618. p += sizeof(float32);
  6619. break;
  6620. case WASM_OP_F64_CONST:
  6621. p += sizeof(float64);
  6622. break;
  6623. case WASM_OP_I32_EQZ:
  6624. case WASM_OP_I32_EQ:
  6625. case WASM_OP_I32_NE:
  6626. case WASM_OP_I32_LT_S:
  6627. case WASM_OP_I32_LT_U:
  6628. case WASM_OP_I32_GT_S:
  6629. case WASM_OP_I32_GT_U:
  6630. case WASM_OP_I32_LE_S:
  6631. case WASM_OP_I32_LE_U:
  6632. case WASM_OP_I32_GE_S:
  6633. case WASM_OP_I32_GE_U:
  6634. case WASM_OP_I64_EQZ:
  6635. case WASM_OP_I64_EQ:
  6636. case WASM_OP_I64_NE:
  6637. case WASM_OP_I64_LT_S:
  6638. case WASM_OP_I64_LT_U:
  6639. case WASM_OP_I64_GT_S:
  6640. case WASM_OP_I64_GT_U:
  6641. case WASM_OP_I64_LE_S:
  6642. case WASM_OP_I64_LE_U:
  6643. case WASM_OP_I64_GE_S:
  6644. case WASM_OP_I64_GE_U:
  6645. case WASM_OP_F32_EQ:
  6646. case WASM_OP_F32_NE:
  6647. case WASM_OP_F32_LT:
  6648. case WASM_OP_F32_GT:
  6649. case WASM_OP_F32_LE:
  6650. case WASM_OP_F32_GE:
  6651. case WASM_OP_F64_EQ:
  6652. case WASM_OP_F64_NE:
  6653. case WASM_OP_F64_LT:
  6654. case WASM_OP_F64_GT:
  6655. case WASM_OP_F64_LE:
  6656. case WASM_OP_F64_GE:
  6657. case WASM_OP_I32_CLZ:
  6658. case WASM_OP_I32_CTZ:
  6659. case WASM_OP_I32_POPCNT:
  6660. case WASM_OP_I32_ADD:
  6661. case WASM_OP_I32_SUB:
  6662. case WASM_OP_I32_MUL:
  6663. case WASM_OP_I32_DIV_S:
  6664. case WASM_OP_I32_DIV_U:
  6665. case WASM_OP_I32_REM_S:
  6666. case WASM_OP_I32_REM_U:
  6667. case WASM_OP_I32_AND:
  6668. case WASM_OP_I32_OR:
  6669. case WASM_OP_I32_XOR:
  6670. case WASM_OP_I32_SHL:
  6671. case WASM_OP_I32_SHR_S:
  6672. case WASM_OP_I32_SHR_U:
  6673. case WASM_OP_I32_ROTL:
  6674. case WASM_OP_I32_ROTR:
  6675. case WASM_OP_I64_CLZ:
  6676. case WASM_OP_I64_CTZ:
  6677. case WASM_OP_I64_POPCNT:
  6678. case WASM_OP_I64_ADD:
  6679. case WASM_OP_I64_SUB:
  6680. case WASM_OP_I64_MUL:
  6681. case WASM_OP_I64_DIV_S:
  6682. case WASM_OP_I64_DIV_U:
  6683. case WASM_OP_I64_REM_S:
  6684. case WASM_OP_I64_REM_U:
  6685. case WASM_OP_I64_AND:
  6686. case WASM_OP_I64_OR:
  6687. case WASM_OP_I64_XOR:
  6688. case WASM_OP_I64_SHL:
  6689. case WASM_OP_I64_SHR_S:
  6690. case WASM_OP_I64_SHR_U:
  6691. case WASM_OP_I64_ROTL:
  6692. case WASM_OP_I64_ROTR:
  6693. case WASM_OP_F32_ABS:
  6694. case WASM_OP_F32_NEG:
  6695. case WASM_OP_F32_CEIL:
  6696. case WASM_OP_F32_FLOOR:
  6697. case WASM_OP_F32_TRUNC:
  6698. case WASM_OP_F32_NEAREST:
  6699. case WASM_OP_F32_SQRT:
  6700. case WASM_OP_F32_ADD:
  6701. case WASM_OP_F32_SUB:
  6702. case WASM_OP_F32_MUL:
  6703. case WASM_OP_F32_DIV:
  6704. case WASM_OP_F32_MIN:
  6705. case WASM_OP_F32_MAX:
  6706. case WASM_OP_F32_COPYSIGN:
  6707. case WASM_OP_F64_ABS:
  6708. case WASM_OP_F64_NEG:
  6709. case WASM_OP_F64_CEIL:
  6710. case WASM_OP_F64_FLOOR:
  6711. case WASM_OP_F64_TRUNC:
  6712. case WASM_OP_F64_NEAREST:
  6713. case WASM_OP_F64_SQRT:
  6714. case WASM_OP_F64_ADD:
  6715. case WASM_OP_F64_SUB:
  6716. case WASM_OP_F64_MUL:
  6717. case WASM_OP_F64_DIV:
  6718. case WASM_OP_F64_MIN:
  6719. case WASM_OP_F64_MAX:
  6720. case WASM_OP_F64_COPYSIGN:
  6721. case WASM_OP_I32_WRAP_I64:
  6722. case WASM_OP_I32_TRUNC_S_F32:
  6723. case WASM_OP_I32_TRUNC_U_F32:
  6724. case WASM_OP_I32_TRUNC_S_F64:
  6725. case WASM_OP_I32_TRUNC_U_F64:
  6726. case WASM_OP_I64_EXTEND_S_I32:
  6727. case WASM_OP_I64_EXTEND_U_I32:
  6728. case WASM_OP_I64_TRUNC_S_F32:
  6729. case WASM_OP_I64_TRUNC_U_F32:
  6730. case WASM_OP_I64_TRUNC_S_F64:
  6731. case WASM_OP_I64_TRUNC_U_F64:
  6732. case WASM_OP_F32_CONVERT_S_I32:
  6733. case WASM_OP_F32_CONVERT_U_I32:
  6734. case WASM_OP_F32_CONVERT_S_I64:
  6735. case WASM_OP_F32_CONVERT_U_I64:
  6736. case WASM_OP_F32_DEMOTE_F64:
  6737. case WASM_OP_F64_CONVERT_S_I32:
  6738. case WASM_OP_F64_CONVERT_U_I32:
  6739. case WASM_OP_F64_CONVERT_S_I64:
  6740. case WASM_OP_F64_CONVERT_U_I64:
  6741. case WASM_OP_F64_PROMOTE_F32:
  6742. case WASM_OP_I32_REINTERPRET_F32:
  6743. case WASM_OP_I64_REINTERPRET_F64:
  6744. case WASM_OP_F32_REINTERPRET_I32:
  6745. case WASM_OP_F64_REINTERPRET_I64:
  6746. case WASM_OP_I32_EXTEND8_S:
  6747. case WASM_OP_I32_EXTEND16_S:
  6748. case WASM_OP_I64_EXTEND8_S:
  6749. case WASM_OP_I64_EXTEND16_S:
  6750. case WASM_OP_I64_EXTEND32_S:
  6751. break;
  6752. #if WASM_ENABLE_GC != 0
  6753. case WASM_OP_GC_PREFIX:
  6754. {
  6755. uint32 opcode1;
  6756. read_leb_uint32(p, p_end, opcode1);
  6757. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6758. is no larger than UINT8_MAX */
  6759. opcode = (uint8)opcode1;
  6760. switch (opcode) {
  6761. case WASM_OP_STRUCT_NEW:
  6762. case WASM_OP_STRUCT_NEW_DEFAULT:
  6763. skip_leb_uint32(p, p_end); /* typeidx */
  6764. break;
  6765. case WASM_OP_STRUCT_GET:
  6766. case WASM_OP_STRUCT_GET_S:
  6767. case WASM_OP_STRUCT_GET_U:
  6768. case WASM_OP_STRUCT_SET:
  6769. skip_leb_uint32(p, p_end); /* typeidx */
  6770. skip_leb_uint32(p, p_end); /* fieldidx */
  6771. break;
  6772. case WASM_OP_ARRAY_NEW:
  6773. case WASM_OP_ARRAY_NEW_DEFAULT:
  6774. case WASM_OP_ARRAY_GET:
  6775. case WASM_OP_ARRAY_GET_S:
  6776. case WASM_OP_ARRAY_GET_U:
  6777. case WASM_OP_ARRAY_SET:
  6778. case WASM_OP_ARRAY_FILL:
  6779. skip_leb_uint32(p, p_end); /* typeidx */
  6780. break;
  6781. case WASM_OP_ARRAY_COPY:
  6782. skip_leb_uint32(p, p_end); /* typeidx1 */
  6783. skip_leb_uint32(p, p_end); /* typeidx2 */
  6784. break;
  6785. case WASM_OP_ARRAY_LEN:
  6786. break;
  6787. case WASM_OP_ARRAY_NEW_FIXED:
  6788. case WASM_OP_ARRAY_NEW_DATA:
  6789. case WASM_OP_ARRAY_NEW_ELEM:
  6790. skip_leb_uint32(p, p_end); /* typeidx */
  6791. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6792. break;
  6793. case WASM_OP_REF_I31:
  6794. case WASM_OP_I31_GET_S:
  6795. case WASM_OP_I31_GET_U:
  6796. break;
  6797. case WASM_OP_REF_TEST:
  6798. case WASM_OP_REF_CAST:
  6799. case WASM_OP_REF_TEST_NULLABLE:
  6800. case WASM_OP_REF_CAST_NULLABLE:
  6801. skip_leb_int32(p, p_end); /* heaptype */
  6802. break;
  6803. case WASM_OP_BR_ON_CAST:
  6804. case WASM_OP_BR_ON_CAST_FAIL:
  6805. p += sizeof(uint8); /* castflag */
  6806. skip_leb_uint32(p, p_end); /* labelidx */
  6807. skip_leb_int32(p, p_end); /* heaptype */
  6808. skip_leb_int32(p, p_end); /* heaptype2 */
  6809. break;
  6810. case WASM_OP_ANY_CONVERT_EXTERN:
  6811. case WASM_OP_EXTERN_CONVERT_ANY:
  6812. break;
  6813. #if WASM_ENABLE_STRINGREF != 0
  6814. case WASM_OP_STRING_NEW_UTF8:
  6815. case WASM_OP_STRING_NEW_WTF16:
  6816. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6817. case WASM_OP_STRING_NEW_WTF8:
  6818. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6819. break;
  6820. case WASM_OP_STRING_CONST:
  6821. skip_leb_int32(p, p_end); /* contents */
  6822. break;
  6823. case WASM_OP_STRING_MEASURE_UTF8:
  6824. case WASM_OP_STRING_MEASURE_WTF8:
  6825. case WASM_OP_STRING_MEASURE_WTF16:
  6826. break;
  6827. case WASM_OP_STRING_ENCODE_UTF8:
  6828. case WASM_OP_STRING_ENCODE_WTF16:
  6829. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6830. case WASM_OP_STRING_ENCODE_WTF8:
  6831. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6832. break;
  6833. case WASM_OP_STRING_CONCAT:
  6834. case WASM_OP_STRING_EQ:
  6835. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6836. case WASM_OP_STRING_AS_WTF8:
  6837. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6838. break;
  6839. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6840. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6841. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6842. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6843. break;
  6844. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6845. case WASM_OP_STRING_AS_WTF16:
  6846. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6847. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6848. break;
  6849. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6850. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6851. break;
  6852. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6853. case WASM_OP_STRING_AS_ITER:
  6854. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6855. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6856. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6857. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6858. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6859. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6860. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6861. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6862. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6863. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6864. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6865. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6866. break;
  6867. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6868. default:
  6869. return false;
  6870. }
  6871. break;
  6872. }
  6873. #endif /* end of WASM_ENABLE_GC != 0 */
  6874. case WASM_OP_MISC_PREFIX:
  6875. {
  6876. uint32 opcode1;
  6877. read_leb_uint32(p, p_end, opcode1);
  6878. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6879. is no larger than UINT8_MAX */
  6880. opcode = (uint8)opcode1;
  6881. switch (opcode) {
  6882. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6883. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6884. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6885. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6886. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6887. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6888. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6889. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6890. break;
  6891. #if WASM_ENABLE_BULK_MEMORY != 0
  6892. case WASM_OP_MEMORY_INIT:
  6893. skip_leb_uint32(p, p_end);
  6894. skip_leb_memidx(p, p_end);
  6895. break;
  6896. case WASM_OP_DATA_DROP:
  6897. skip_leb_uint32(p, p_end);
  6898. break;
  6899. case WASM_OP_MEMORY_COPY:
  6900. skip_leb_memidx(p, p_end);
  6901. skip_leb_memidx(p, p_end);
  6902. break;
  6903. case WASM_OP_MEMORY_FILL:
  6904. skip_leb_memidx(p, p_end);
  6905. break;
  6906. #endif /* WASM_ENABLE_BULK_MEMORY */
  6907. #if WASM_ENABLE_REF_TYPES != 0
  6908. case WASM_OP_TABLE_INIT:
  6909. case WASM_OP_TABLE_COPY:
  6910. /* tableidx */
  6911. skip_leb_uint32(p, p_end);
  6912. /* elemidx */
  6913. skip_leb_uint32(p, p_end);
  6914. break;
  6915. case WASM_OP_ELEM_DROP:
  6916. /* elemidx */
  6917. skip_leb_uint32(p, p_end);
  6918. break;
  6919. case WASM_OP_TABLE_SIZE:
  6920. case WASM_OP_TABLE_GROW:
  6921. case WASM_OP_TABLE_FILL:
  6922. skip_leb_uint32(p, p_end); /* table idx */
  6923. break;
  6924. #endif /* WASM_ENABLE_REF_TYPES */
  6925. default:
  6926. return false;
  6927. }
  6928. break;
  6929. }
  6930. #if WASM_ENABLE_SIMD != 0
  6931. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  6932. || (WASM_ENABLE_FAST_INTERP != 0)
  6933. case WASM_OP_SIMD_PREFIX:
  6934. {
  6935. uint32 opcode1;
  6936. read_leb_uint32(p, p_end, opcode1);
  6937. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6938. is no larger than UINT8_MAX */
  6939. opcode = (uint8)opcode1;
  6940. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6941. */
  6942. switch (opcode) {
  6943. case SIMD_v128_load:
  6944. case SIMD_v128_load8x8_s:
  6945. case SIMD_v128_load8x8_u:
  6946. case SIMD_v128_load16x4_s:
  6947. case SIMD_v128_load16x4_u:
  6948. case SIMD_v128_load32x2_s:
  6949. case SIMD_v128_load32x2_u:
  6950. case SIMD_v128_load8_splat:
  6951. case SIMD_v128_load16_splat:
  6952. case SIMD_v128_load32_splat:
  6953. case SIMD_v128_load64_splat:
  6954. case SIMD_v128_store:
  6955. /* memarg align */
  6956. skip_leb_uint32(p, p_end);
  6957. /* memarg offset */
  6958. skip_leb_mem_offset(p, p_end);
  6959. break;
  6960. case SIMD_v128_const:
  6961. case SIMD_v8x16_shuffle:
  6962. /* immByte[16] immLaneId[16] */
  6963. CHECK_BUF1(p, p_end, 16);
  6964. p += 16;
  6965. break;
  6966. case SIMD_i8x16_extract_lane_s:
  6967. case SIMD_i8x16_extract_lane_u:
  6968. case SIMD_i8x16_replace_lane:
  6969. case SIMD_i16x8_extract_lane_s:
  6970. case SIMD_i16x8_extract_lane_u:
  6971. case SIMD_i16x8_replace_lane:
  6972. case SIMD_i32x4_extract_lane:
  6973. case SIMD_i32x4_replace_lane:
  6974. case SIMD_i64x2_extract_lane:
  6975. case SIMD_i64x2_replace_lane:
  6976. case SIMD_f32x4_extract_lane:
  6977. case SIMD_f32x4_replace_lane:
  6978. case SIMD_f64x2_extract_lane:
  6979. case SIMD_f64x2_replace_lane:
  6980. /* ImmLaneId */
  6981. CHECK_BUF(p, p_end, 1);
  6982. p++;
  6983. break;
  6984. case SIMD_v128_load8_lane:
  6985. case SIMD_v128_load16_lane:
  6986. case SIMD_v128_load32_lane:
  6987. case SIMD_v128_load64_lane:
  6988. case SIMD_v128_store8_lane:
  6989. case SIMD_v128_store16_lane:
  6990. case SIMD_v128_store32_lane:
  6991. case SIMD_v128_store64_lane:
  6992. /* memarg align */
  6993. skip_leb_uint32(p, p_end);
  6994. /* memarg offset */
  6995. skip_leb_mem_offset(p, p_end);
  6996. /* ImmLaneId */
  6997. CHECK_BUF(p, p_end, 1);
  6998. p++;
  6999. break;
  7000. case SIMD_v128_load32_zero:
  7001. case SIMD_v128_load64_zero:
  7002. /* memarg align */
  7003. skip_leb_uint32(p, p_end);
  7004. /* memarg offset */
  7005. skip_leb_mem_offset(p, p_end);
  7006. break;
  7007. default:
  7008. /*
  7009. * since latest SIMD specific used almost every value
  7010. * from 0x00 to 0xff, the default branch will present
  7011. * all opcodes without imm
  7012. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  7013. */
  7014. break;
  7015. }
  7016. break;
  7017. }
  7018. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  7019. (WASM_ENABLE_FAST_INTERP != 0) */
  7020. #endif /* end of WASM_ENABLE_SIMD */
  7021. #if WASM_ENABLE_SHARED_MEMORY != 0
  7022. case WASM_OP_ATOMIC_PREFIX:
  7023. {
  7024. uint32 opcode1;
  7025. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  7026. read_leb_uint32(p, p_end, opcode1);
  7027. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  7028. is no larger than UINT8_MAX */
  7029. opcode = (uint8)opcode1;
  7030. if (opcode != WASM_OP_ATOMIC_FENCE) {
  7031. skip_leb_uint32(p, p_end); /* align */
  7032. skip_leb_mem_offset(p, p_end); /* offset */
  7033. }
  7034. else {
  7035. /* atomic.fence doesn't have memarg */
  7036. p++;
  7037. }
  7038. break;
  7039. }
  7040. #endif
  7041. #if WASM_ENABLE_DEBUG_INTERP != 0
  7042. case DEBUG_OP_BREAK:
  7043. {
  7044. WASMDebugInstance *debug_instance =
  7045. wasm_exec_env_get_instance(exec_env);
  7046. char original_opcode[1];
  7047. uint64 size = 1;
  7048. WASMModuleInstance *module_inst =
  7049. (WASMModuleInstance *)exec_env->module_inst;
  7050. uint64 offset = (p - 1) >= module_inst->module->load_addr
  7051. ? (p - 1) - module_inst->module->load_addr
  7052. : ~0;
  7053. if (debug_instance) {
  7054. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  7055. original_opcode, &size)
  7056. && size == 1) {
  7057. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  7058. "with %02x: ",
  7059. original_opcode[0]);
  7060. opcode = original_opcode[0];
  7061. goto op_break_retry;
  7062. }
  7063. }
  7064. break;
  7065. }
  7066. #endif
  7067. default:
  7068. return false;
  7069. }
  7070. }
  7071. (void)u8;
  7072. (void)exec_env;
  7073. return false;
  7074. fail:
  7075. return false;
  7076. }
  7077. #if WASM_ENABLE_FAST_INTERP != 0
  7078. #if WASM_DEBUG_PREPROCESSOR != 0
  7079. #define LOG_OP(...) os_printf(__VA_ARGS__)
  7080. #else
  7081. #define LOG_OP(...) (void)0
  7082. #endif
  7083. #define PATCH_ELSE 0
  7084. #define PATCH_END 1
  7085. typedef struct BranchBlockPatch {
  7086. struct BranchBlockPatch *next;
  7087. uint8 patch_type;
  7088. uint8 *code_compiled;
  7089. } BranchBlockPatch;
  7090. #endif
  7091. typedef struct BranchBlock {
  7092. uint8 label_type;
  7093. BlockType block_type;
  7094. uint8 *start_addr;
  7095. uint8 *else_addr;
  7096. uint8 *end_addr;
  7097. uint32 stack_cell_num;
  7098. #if WASM_ENABLE_GC != 0
  7099. uint32 reftype_map_num;
  7100. /* Indicate which local is used inside current block, used to validate
  7101. * local.get with non-nullable ref types */
  7102. uint8 *local_use_mask;
  7103. uint32 local_use_mask_size;
  7104. #endif
  7105. #if WASM_ENABLE_FAST_INTERP != 0
  7106. uint16 dynamic_offset;
  7107. uint8 *code_compiled;
  7108. BranchBlockPatch *patch_list;
  7109. /* This is used to save params frame_offset of of if block */
  7110. int16 *param_frame_offsets;
  7111. /* This is used to recover the dynamic offset for else branch,
  7112. * and also to remember the start offset of dynamic space which
  7113. * stores the block arguments for loop block, so we can use it
  7114. * to copy the stack operands to the loop block's arguments in
  7115. * wasm_loader_emit_br_info for opcode br. */
  7116. uint16 start_dynamic_offset;
  7117. #endif
  7118. /* Indicate the operand stack is in polymorphic state.
  7119. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  7120. * to polymorphic state until the block's 'end' opcode is processed.
  7121. * If stack is in polymorphic state and stack is empty, instruction can
  7122. * pop any type of value directly without decreasing stack top pointer
  7123. * and stack cell num. */
  7124. bool is_stack_polymorphic;
  7125. } BranchBlock;
  7126. typedef struct WASMLoaderContext {
  7127. /* frame ref stack */
  7128. uint8 *frame_ref;
  7129. uint8 *frame_ref_bottom;
  7130. uint8 *frame_ref_boundary;
  7131. uint32 frame_ref_size;
  7132. uint32 stack_cell_num;
  7133. uint32 max_stack_cell_num;
  7134. #if WASM_ENABLE_GC != 0
  7135. /* frame reftype map stack */
  7136. WASMRefTypeMap *frame_reftype_map;
  7137. WASMRefTypeMap *frame_reftype_map_bottom;
  7138. WASMRefTypeMap *frame_reftype_map_boundary;
  7139. uint32 frame_reftype_map_size;
  7140. uint32 reftype_map_num;
  7141. uint32 max_reftype_map_num;
  7142. /* Current module */
  7143. WASMModule *module;
  7144. /* Current module's ref_type_set */
  7145. HashMap *ref_type_set;
  7146. /* Always point to local variable ref_type of
  7147. wasm_loader_prepare_bytecode */
  7148. WASMRefType *ref_type_tmp;
  7149. #endif
  7150. /* frame csp stack */
  7151. BranchBlock *frame_csp;
  7152. BranchBlock *frame_csp_bottom;
  7153. BranchBlock *frame_csp_boundary;
  7154. uint32 frame_csp_size;
  7155. uint32 csp_num;
  7156. uint32 max_csp_num;
  7157. #if WASM_ENABLE_FAST_INTERP != 0
  7158. /* frame offset stack */
  7159. int16 *frame_offset;
  7160. int16 *frame_offset_bottom;
  7161. int16 *frame_offset_boundary;
  7162. uint32 frame_offset_size;
  7163. int16 dynamic_offset;
  7164. int16 start_dynamic_offset;
  7165. int16 max_dynamic_offset;
  7166. /* preserved local offset */
  7167. int16 preserved_local_offset;
  7168. /* const buffer for i64 and f64 consts, note that the raw bytes
  7169. * of i64 and f64 are the same, so we read an i64 value from an
  7170. * f64 const with its raw bytes, something like `*(int64 *)&f64 */
  7171. int64 *i64_consts;
  7172. uint32 i64_const_max_num;
  7173. uint32 i64_const_num;
  7174. /* const buffer for i32 and f32 consts */
  7175. int32 *i32_consts;
  7176. uint32 i32_const_max_num;
  7177. uint32 i32_const_num;
  7178. /* const buffer for V128 */
  7179. V128 *v128_consts;
  7180. uint32 v128_const_max_num;
  7181. uint32 v128_const_num;
  7182. /* processed code */
  7183. uint8 *p_code_compiled;
  7184. uint8 *p_code_compiled_end;
  7185. uint32 code_compiled_size;
  7186. /* If the last opcode will be dropped, the peak memory usage will be larger
  7187. * than the final code_compiled_size, we record the peak size to ensure
  7188. * there will not be invalid memory access during second traverse */
  7189. uint32 code_compiled_peak_size;
  7190. #endif
  7191. } WASMLoaderContext;
  7192. #define CHECK_CSP_PUSH() \
  7193. do { \
  7194. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7195. MEM_REALLOC( \
  7196. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7197. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7198. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7199. ctx->frame_csp_boundary = \
  7200. ctx->frame_csp_bottom \
  7201. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7202. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7203. } \
  7204. } while (0)
  7205. #define CHECK_CSP_POP() \
  7206. do { \
  7207. if (ctx->csp_num < 1) { \
  7208. set_error_buf(error_buf, error_buf_size, \
  7209. "type mismatch: " \
  7210. "expect data but block stack was empty"); \
  7211. goto fail; \
  7212. } \
  7213. } while (0)
  7214. #if WASM_ENABLE_FAST_INTERP != 0
  7215. static bool
  7216. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7217. uint32 error_buf_size)
  7218. {
  7219. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7220. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7221. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7222. ctx->frame_offset_size + 16);
  7223. ctx->frame_offset_size += 16;
  7224. ctx->frame_offset_boundary =
  7225. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7226. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7227. }
  7228. return true;
  7229. fail:
  7230. return false;
  7231. }
  7232. static bool
  7233. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7234. {
  7235. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7236. return false;
  7237. return true;
  7238. }
  7239. static void
  7240. free_label_patch_list(BranchBlock *frame_csp)
  7241. {
  7242. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7243. BranchBlockPatch *next;
  7244. while (label_patch != NULL) {
  7245. next = label_patch->next;
  7246. wasm_runtime_free(label_patch);
  7247. label_patch = next;
  7248. }
  7249. frame_csp->patch_list = NULL;
  7250. }
  7251. static void
  7252. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7253. {
  7254. BranchBlock *tmp_csp = frame_csp;
  7255. uint32 i;
  7256. for (i = 0; i < csp_num; i++) {
  7257. free_label_patch_list(tmp_csp);
  7258. tmp_csp++;
  7259. }
  7260. }
  7261. static void
  7262. free_all_label_param_frame_offsets(BranchBlock *frame_csp, uint32 csp_num)
  7263. {
  7264. BranchBlock *tmp_csp = frame_csp;
  7265. uint32 i;
  7266. for (i = 0; i < csp_num; i++) {
  7267. if (tmp_csp->param_frame_offsets)
  7268. wasm_runtime_free(tmp_csp->param_frame_offsets);
  7269. tmp_csp++;
  7270. }
  7271. }
  7272. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7273. #if WASM_ENABLE_GC != 0
  7274. static bool
  7275. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7276. char *error_buf, uint32 error_buf_size)
  7277. {
  7278. BranchBlock *current_csp = ctx->frame_csp - 1;
  7279. uint32 local_mask_size;
  7280. if (local_count == 0) {
  7281. current_csp->local_use_mask_size = 0;
  7282. return true;
  7283. }
  7284. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7285. * else branch, we don't need to allocate memory again */
  7286. if (!current_csp->local_use_mask) {
  7287. local_mask_size = (local_count + 7) / sizeof(uint8);
  7288. if (!(current_csp->local_use_mask =
  7289. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7290. return false;
  7291. }
  7292. current_csp->local_use_mask_size = local_mask_size;
  7293. }
  7294. else {
  7295. local_mask_size = current_csp->local_use_mask_size;
  7296. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7297. }
  7298. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7299. /* For non-function blocks, inherit the use status from parent block */
  7300. BranchBlock *parent_csp = current_csp - 1;
  7301. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7302. bh_assert(parent_csp->local_use_mask);
  7303. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7304. parent_csp->local_use_mask, local_mask_size);
  7305. }
  7306. return true;
  7307. }
  7308. static void
  7309. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7310. {
  7311. BranchBlock *current_csp = ctx->frame_csp - 1;
  7312. bh_assert(current_csp->local_use_mask
  7313. || current_csp->local_use_mask_size == 0);
  7314. if (current_csp->local_use_mask) {
  7315. wasm_runtime_free(current_csp->local_use_mask);
  7316. }
  7317. current_csp->local_use_mask = NULL;
  7318. current_csp->local_use_mask_size = 0;
  7319. }
  7320. static void
  7321. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7322. {
  7323. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7324. uint32 i;
  7325. for (i = 0; i < ctx->csp_num; i++) {
  7326. if (tmp_csp->local_use_mask) {
  7327. wasm_runtime_free(tmp_csp->local_use_mask);
  7328. tmp_csp->local_use_mask = NULL;
  7329. tmp_csp->local_use_mask_size = 0;
  7330. }
  7331. tmp_csp++;
  7332. }
  7333. }
  7334. static void
  7335. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7336. {
  7337. BranchBlock *current_csp = ctx->frame_csp - 1;
  7338. uint32 byte_offset = index / sizeof(uint8);
  7339. uint32 bit_offset = index % sizeof(uint8);
  7340. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7341. bh_assert(current_csp->local_use_mask);
  7342. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7343. }
  7344. static bool
  7345. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7346. {
  7347. BranchBlock *current_csp = ctx->frame_csp - 1;
  7348. uint32 byte_offset = index / sizeof(uint8);
  7349. uint32 bit_offset = index % sizeof(uint8);
  7350. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7351. bh_assert(current_csp->local_use_mask);
  7352. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7353. ? true
  7354. : false;
  7355. }
  7356. #endif /* end of WASM_ENABLE_GC != 0 */
  7357. static void
  7358. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7359. {
  7360. if (ctx) {
  7361. if (ctx->frame_ref_bottom)
  7362. wasm_runtime_free(ctx->frame_ref_bottom);
  7363. #if WASM_ENABLE_GC != 0
  7364. if (ctx->frame_reftype_map_bottom)
  7365. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7366. #endif
  7367. if (ctx->frame_csp_bottom) {
  7368. #if WASM_ENABLE_FAST_INTERP != 0
  7369. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7370. free_all_label_param_frame_offsets(ctx->frame_csp_bottom,
  7371. ctx->csp_num);
  7372. #endif
  7373. #if WASM_ENABLE_GC != 0
  7374. wasm_loader_clean_all_local_use_masks(ctx);
  7375. #endif
  7376. wasm_runtime_free(ctx->frame_csp_bottom);
  7377. }
  7378. #if WASM_ENABLE_FAST_INTERP != 0
  7379. if (ctx->frame_offset_bottom)
  7380. wasm_runtime_free(ctx->frame_offset_bottom);
  7381. if (ctx->i64_consts)
  7382. wasm_runtime_free(ctx->i64_consts);
  7383. if (ctx->i32_consts)
  7384. wasm_runtime_free(ctx->i32_consts);
  7385. if (ctx->v128_consts)
  7386. wasm_runtime_free(ctx->v128_consts);
  7387. #endif
  7388. wasm_runtime_free(ctx);
  7389. }
  7390. }
  7391. static WASMLoaderContext *
  7392. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7393. {
  7394. WASMLoaderContext *loader_ctx =
  7395. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7396. if (!loader_ctx)
  7397. return NULL;
  7398. loader_ctx->frame_ref_size = 32;
  7399. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7400. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7401. goto fail;
  7402. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7403. #if WASM_ENABLE_GC != 0
  7404. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7405. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7406. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7407. error_buf_size)))
  7408. goto fail;
  7409. loader_ctx->frame_reftype_map_boundary =
  7410. loader_ctx->frame_reftype_map_bottom + 16;
  7411. #endif
  7412. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7413. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7414. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7415. goto fail;
  7416. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7417. #if WASM_ENABLE_EXCE_HANDLING != 0
  7418. func->exception_handler_count = 0;
  7419. #endif
  7420. #if WASM_ENABLE_FAST_INTERP != 0
  7421. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7422. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7423. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7424. error_buf_size)))
  7425. goto fail;
  7426. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7427. loader_ctx->i64_const_max_num = 8;
  7428. if (!(loader_ctx->i64_consts =
  7429. loader_malloc(sizeof(int64) * loader_ctx->i64_const_max_num,
  7430. error_buf, error_buf_size)))
  7431. goto fail;
  7432. loader_ctx->i32_const_max_num = 8;
  7433. if (!(loader_ctx->i32_consts =
  7434. loader_malloc(sizeof(int32) * loader_ctx->i32_const_max_num,
  7435. error_buf, error_buf_size)))
  7436. goto fail;
  7437. loader_ctx->v128_const_max_num = 8;
  7438. if (!(loader_ctx->v128_consts =
  7439. loader_malloc(sizeof(V128) * loader_ctx->v128_const_max_num,
  7440. error_buf, error_buf_size)))
  7441. goto fail;
  7442. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7443. set_error_buf(error_buf, error_buf_size,
  7444. "fast interpreter offset overflow");
  7445. goto fail;
  7446. }
  7447. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7448. loader_ctx->max_dynamic_offset =
  7449. func->param_cell_num + func->local_cell_num;
  7450. #endif
  7451. return loader_ctx;
  7452. fail:
  7453. wasm_loader_ctx_destroy(loader_ctx);
  7454. return NULL;
  7455. }
  7456. static bool
  7457. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7458. uint32 error_buf_size)
  7459. {
  7460. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7461. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7462. /* Increase the frame ref stack */
  7463. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7464. ctx->frame_ref_size + 16);
  7465. ctx->frame_ref_size += 16;
  7466. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7467. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7468. }
  7469. #if WASM_ENABLE_GC != 0
  7470. if (wasm_is_type_multi_byte_type(type)
  7471. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7472. /* Increase the frame reftype map stack */
  7473. bh_assert(
  7474. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7475. * sizeof(WASMRefTypeMap))
  7476. == ctx->frame_reftype_map_size);
  7477. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7478. ctx->frame_reftype_map_size
  7479. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7480. ctx->frame_reftype_map =
  7481. ctx->frame_reftype_map_bottom
  7482. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7483. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7484. ctx->frame_reftype_map_boundary =
  7485. ctx->frame_reftype_map_bottom
  7486. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7487. }
  7488. #endif
  7489. return true;
  7490. fail:
  7491. return false;
  7492. }
  7493. static bool
  7494. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7495. uint32 error_buf_size)
  7496. {
  7497. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7498. uint32 i;
  7499. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7500. return false;
  7501. #if WASM_ENABLE_GC != 0
  7502. if (wasm_is_type_multi_byte_type(type)) {
  7503. WASMRefType *ref_type;
  7504. if (!(ref_type =
  7505. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7506. error_buf, error_buf_size))) {
  7507. return false;
  7508. }
  7509. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7510. /* Increase the frame reftype map stack */
  7511. bh_assert((uint32)((ctx->frame_reftype_map
  7512. - ctx->frame_reftype_map_bottom)
  7513. * sizeof(WASMRefTypeMap))
  7514. == ctx->frame_reftype_map_size);
  7515. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7516. ctx->frame_reftype_map_size,
  7517. ctx->frame_reftype_map_size
  7518. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7519. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7520. + ctx->frame_reftype_map_size
  7521. / ((uint32)sizeof(WASMRefTypeMap));
  7522. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7523. ctx->frame_reftype_map_boundary =
  7524. ctx->frame_reftype_map_bottom
  7525. + ctx->frame_reftype_map_size
  7526. / ((uint32)sizeof(WASMRefTypeMap));
  7527. }
  7528. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7529. ctx->frame_reftype_map->ref_type = ref_type;
  7530. ctx->frame_reftype_map++;
  7531. ctx->reftype_map_num++;
  7532. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7533. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7534. }
  7535. #endif
  7536. for (i = 0; i < type_cell_num; i++)
  7537. *ctx->frame_ref++ = type;
  7538. ctx->stack_cell_num += type_cell_num;
  7539. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7540. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7541. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7542. set_error_buf(error_buf, error_buf_size,
  7543. "operand stack depth limit exceeded");
  7544. return false;
  7545. }
  7546. }
  7547. return true;
  7548. #if WASM_ENABLE_GC != 0
  7549. fail:
  7550. return false;
  7551. #endif
  7552. }
  7553. static bool
  7554. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7555. int32 stack_cell_num,
  7556. #if WASM_ENABLE_GC != 0
  7557. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7558. #endif
  7559. uint8 type,
  7560. #if WASM_ENABLE_GC != 0
  7561. WASMRefType *ref_type,
  7562. #endif
  7563. char *error_buf, uint32 error_buf_size)
  7564. {
  7565. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7566. #if WASM_ENABLE_GC != 0
  7567. WASMRefType *frame_reftype = NULL;
  7568. #endif
  7569. if (stack_cell_num < type_cell_num) {
  7570. set_error_buf(error_buf, error_buf_size,
  7571. "type mismatch: expect data but stack was empty");
  7572. return false;
  7573. }
  7574. #if WASM_ENABLE_GC == 0
  7575. for (i = 0; i < type_cell_num; i++) {
  7576. if (*(frame_ref - 1 - i) != type) {
  7577. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7578. "type mismatch: expect ", type2str(type),
  7579. " but got other");
  7580. return false;
  7581. }
  7582. }
  7583. #else
  7584. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7585. bh_assert(reftype_map_num > 0);
  7586. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7587. }
  7588. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7589. ref_type, ctx->module->types,
  7590. ctx->module->type_count)) {
  7591. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7592. "type mismatch: expect ", type2str(type),
  7593. " but got other");
  7594. return false;
  7595. }
  7596. for (i = 0; i < type_cell_num - 1; i++) {
  7597. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7598. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7599. "type mismatch: expect ", type2str(type),
  7600. " but got other");
  7601. return false;
  7602. }
  7603. }
  7604. #endif
  7605. return true;
  7606. }
  7607. static bool
  7608. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7609. uint32 error_buf_size)
  7610. {
  7611. int32 block_stack_cell_num =
  7612. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7613. #if WASM_ENABLE_GC != 0
  7614. int32 reftype_map_num =
  7615. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7616. #endif
  7617. if (block_stack_cell_num > 0) {
  7618. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7619. /* the stack top is a value of any type, return success */
  7620. return true;
  7621. }
  7622. #if WASM_ENABLE_GC != 0
  7623. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7624. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7625. WASMRefType *stack_top_ref_type = NULL;
  7626. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7627. bh_assert(reftype_map_num > 0);
  7628. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7629. }
  7630. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7631. ctx->ref_type_tmp, ctx->module->types,
  7632. ctx->module->type_count)) {
  7633. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7634. uint32 ref_type_struct_size =
  7635. wasm_reftype_struct_size(stack_top_ref_type);
  7636. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7637. stack_top_ref_type, ref_type_struct_size);
  7638. }
  7639. return true;
  7640. }
  7641. }
  7642. #endif
  7643. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7644. #if WASM_ENABLE_GC != 0
  7645. ctx->frame_reftype_map, reftype_map_num,
  7646. #endif
  7647. type,
  7648. #if WASM_ENABLE_GC != 0
  7649. ctx->ref_type_tmp,
  7650. #endif
  7651. error_buf, error_buf_size)) {
  7652. return false;
  7653. }
  7654. return true;
  7655. }
  7656. static bool
  7657. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7658. uint32 error_buf_size)
  7659. {
  7660. BranchBlock *cur_block = ctx->frame_csp - 1;
  7661. int32 available_stack_cell =
  7662. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7663. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7664. /* Directly return success if current block is in stack
  7665. polymorphic state while stack is empty. */
  7666. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7667. return true;
  7668. if (type == VALUE_TYPE_VOID)
  7669. return true;
  7670. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7671. return false;
  7672. bh_assert(available_stack_cell > 0);
  7673. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7674. type = VALUE_TYPE_ANY;
  7675. cell_num_to_pop = 1;
  7676. }
  7677. ctx->frame_ref -= cell_num_to_pop;
  7678. ctx->stack_cell_num -= cell_num_to_pop;
  7679. #if WASM_ENABLE_GC != 0
  7680. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7681. ctx->frame_reftype_map--;
  7682. ctx->reftype_map_num--;
  7683. }
  7684. #endif
  7685. return true;
  7686. }
  7687. #if WASM_ENABLE_GC != 0
  7688. /* Get the stack top element of current block */
  7689. static bool
  7690. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7691. WASMRefType **p_ref_type, char *error_buf,
  7692. uint32 error_buf_size)
  7693. {
  7694. BranchBlock *cur_block = ctx->frame_csp - 1;
  7695. int32 available_stack_cell =
  7696. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7697. if (available_stack_cell <= 0) {
  7698. /* Directly return success if current block is in stack
  7699. polymorphic state while stack is empty. */
  7700. if (cur_block->is_stack_polymorphic) {
  7701. *p_type = VALUE_TYPE_ANY;
  7702. return true;
  7703. }
  7704. else {
  7705. set_error_buf(
  7706. error_buf, error_buf_size,
  7707. "type mismatch: expect data but block stack was empty");
  7708. return false;
  7709. }
  7710. }
  7711. *p_type = *(ctx->frame_ref - 1);
  7712. if (wasm_is_type_multi_byte_type(*p_type)) {
  7713. int32 available_reftype_map =
  7714. (int32)(ctx->reftype_map_num
  7715. - (ctx->frame_csp - 1)->reftype_map_num);
  7716. bh_assert(available_reftype_map > 0);
  7717. (void)available_reftype_map;
  7718. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7719. }
  7720. return true;
  7721. }
  7722. #if WASM_ENABLE_FAST_INTERP != 0
  7723. static bool
  7724. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7725. char *error_buf, uint32 error_buf_size);
  7726. #endif
  7727. /* Check whether the stack top elem is a heap object, and if yes,
  7728. pop and return it */
  7729. static bool
  7730. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7731. WASMRefType *ref_ht_ret, char *error_buf,
  7732. uint32 error_buf_size)
  7733. {
  7734. uint8 type = 0;
  7735. WASMRefType *ref_type = NULL;
  7736. /* Get stack top element */
  7737. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7738. error_buf_size)) {
  7739. return false;
  7740. }
  7741. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7742. /* stack top isn't a ref type */
  7743. && !wasm_is_type_reftype(type)) {
  7744. set_error_buf(error_buf, error_buf_size,
  7745. "type mismatch: expect heap object but got others");
  7746. return false;
  7747. }
  7748. /* POP stack top */
  7749. if (wasm_is_type_multi_byte_type(type)) {
  7750. bh_assert(ref_type);
  7751. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7752. wasm_reftype_struct_size(ref_type));
  7753. }
  7754. #if WASM_ENABLE_FAST_INTERP != 0
  7755. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7756. error_buf_size)) {
  7757. return false;
  7758. }
  7759. #else
  7760. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7761. return false;
  7762. }
  7763. #endif
  7764. if (p_type)
  7765. *p_type = type;
  7766. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7767. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7768. wasm_reftype_struct_size(ref_type));
  7769. }
  7770. return true;
  7771. }
  7772. /* Check whether the stack top elem is subtype of (ref null ht),
  7773. and if yes, pop it and return the converted (ref ht) */
  7774. static bool
  7775. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7776. WASMRefType *ref_ht_ret, char *error_buf,
  7777. uint32 error_buf_size)
  7778. {
  7779. uint8 type = 0;
  7780. WASMRefType ref_type = { 0 };
  7781. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7782. error_buf_size)) {
  7783. return false;
  7784. }
  7785. /* Convert to related (ref ht) and return */
  7786. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7787. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7788. */
  7789. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7790. HEAP_TYPE_ARRAY
  7791. + (type - REF_TYPE_ARRAYREF));
  7792. type = ref_ht_ret->ref_type;
  7793. }
  7794. else if (wasm_is_reftype_htref_nullable(type)
  7795. || wasm_is_reftype_htref_non_nullable(type)) {
  7796. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7797. wasm_reftype_struct_size(&ref_type));
  7798. /* Convert to (ref ht) */
  7799. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7800. ref_ht_ret->ref_ht_common.nullable = false;
  7801. type = ref_ht_ret->ref_type;
  7802. }
  7803. *p_type = type;
  7804. return true;
  7805. }
  7806. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7807. and if yes, pop it and return the type_idx */
  7808. static bool
  7809. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7810. uint32 *p_type_idx, char *error_buf,
  7811. uint32 error_buf_size)
  7812. {
  7813. uint8 type = 0;
  7814. int32 type_idx = -1;
  7815. WASMRefType *ref_type = NULL;
  7816. /* Get stack top element */
  7817. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7818. error_buf_size)) {
  7819. return false;
  7820. }
  7821. if (type != VALUE_TYPE_ANY) {
  7822. /* stack top isn't (ref null $t) */
  7823. if (!((wasm_is_reftype_htref_nullable(type)
  7824. || wasm_is_reftype_htref_non_nullable(type))
  7825. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7826. set_error_buf(error_buf, error_buf_size,
  7827. "type mismatch: expect (ref null $t) but got others");
  7828. return false;
  7829. }
  7830. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7831. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7832. wasm_reftype_struct_size(ref_type));
  7833. }
  7834. /* POP stack top */
  7835. #if WASM_ENABLE_FAST_INTERP != 0
  7836. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7837. error_buf_size)) {
  7838. return false;
  7839. }
  7840. #else
  7841. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7842. return false;
  7843. }
  7844. #endif
  7845. /* Convert to type_idx and return */
  7846. *p_type = type;
  7847. if (type != VALUE_TYPE_ANY)
  7848. *p_type_idx = (uint32)type_idx;
  7849. return true;
  7850. }
  7851. #endif /* WASM_ENABLE_GC != 0 */
  7852. #if WASM_ENABLE_FAST_INTERP == 0
  7853. static bool
  7854. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7855. uint8 type_push, uint8 type_pop, char *error_buf,
  7856. uint32 error_buf_size)
  7857. {
  7858. for (int i = 0; i < pop_cnt; i++) {
  7859. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7860. error_buf_size))
  7861. return false;
  7862. }
  7863. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7864. return false;
  7865. return true;
  7866. }
  7867. #endif
  7868. static bool
  7869. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7870. BlockType block_type, uint8 *start_addr,
  7871. char *error_buf, uint32 error_buf_size)
  7872. {
  7873. CHECK_CSP_PUSH();
  7874. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7875. ctx->frame_csp->label_type = label_type;
  7876. ctx->frame_csp->block_type = block_type;
  7877. ctx->frame_csp->start_addr = start_addr;
  7878. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7879. #if WASM_ENABLE_GC != 0
  7880. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7881. #endif
  7882. #if WASM_ENABLE_FAST_INTERP != 0
  7883. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7884. ctx->frame_csp->patch_list = NULL;
  7885. #endif
  7886. ctx->frame_csp++;
  7887. ctx->csp_num++;
  7888. if (ctx->csp_num > ctx->max_csp_num) {
  7889. ctx->max_csp_num = ctx->csp_num;
  7890. if (ctx->max_csp_num > UINT16_MAX) {
  7891. set_error_buf(error_buf, error_buf_size,
  7892. "label stack depth limit exceeded");
  7893. return false;
  7894. }
  7895. }
  7896. return true;
  7897. fail:
  7898. return false;
  7899. }
  7900. static bool
  7901. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7902. uint32 error_buf_size)
  7903. {
  7904. CHECK_CSP_POP();
  7905. #if WASM_ENABLE_FAST_INTERP != 0
  7906. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7907. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7908. #endif
  7909. ctx->frame_csp--;
  7910. ctx->csp_num--;
  7911. return true;
  7912. fail:
  7913. return false;
  7914. }
  7915. #if WASM_ENABLE_FAST_INTERP != 0
  7916. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7917. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7918. #define emit_label(opcode) \
  7919. do { \
  7920. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7921. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7922. } while (0)
  7923. #define skip_label() \
  7924. do { \
  7925. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7926. LOG_OP("\ndelete last op\n"); \
  7927. } while (0)
  7928. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7929. #if UINTPTR_MAX == UINT64_MAX
  7930. #define emit_label(opcode) \
  7931. do { \
  7932. int32 offset = \
  7933. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7934. /* emit int32 relative offset in 64-bit target */ \
  7935. wasm_loader_emit_uint32(loader_ctx, offset); \
  7936. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7937. } while (0)
  7938. #else
  7939. #define emit_label(opcode) \
  7940. do { \
  7941. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7942. /* emit uint32 label address in 32-bit target */ \
  7943. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7944. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7945. } while (0)
  7946. #endif
  7947. #define skip_label() \
  7948. do { \
  7949. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7950. LOG_OP("\ndelete last op\n"); \
  7951. } while (0)
  7952. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7953. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7954. #define emit_label(opcode) \
  7955. do { \
  7956. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7957. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7958. } while (0)
  7959. #define skip_label() \
  7960. do { \
  7961. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7962. LOG_OP("\ndelete last op\n"); \
  7963. } while (0)
  7964. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7965. #define emit_empty_label_addr_and_frame_ip(type) \
  7966. do { \
  7967. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7968. loader_ctx->p_code_compiled, error_buf, \
  7969. error_buf_size)) \
  7970. goto fail; \
  7971. /* label address, to be patched */ \
  7972. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7973. } while (0)
  7974. #define emit_br_info(frame_csp, is_br) \
  7975. do { \
  7976. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7977. error_buf_size)) \
  7978. goto fail; \
  7979. } while (0)
  7980. #define LAST_OP_OUTPUT_I32() \
  7981. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7982. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7983. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7984. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7985. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7986. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7987. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7988. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7989. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7990. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7991. || (last_op == EXT_OP_COPY_STACK_TOP)
  7992. #define LAST_OP_OUTPUT_I64() \
  7993. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7994. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7995. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7996. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7997. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7998. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7999. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  8000. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  8001. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  8002. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  8003. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  8004. #define GET_CONST_OFFSET(type, val) \
  8005. do { \
  8006. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  8007. &operand_offset, error_buf, \
  8008. error_buf_size))) \
  8009. goto fail; \
  8010. } while (0)
  8011. #define GET_CONST_F32_OFFSET(type, fval) \
  8012. do { \
  8013. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8014. &operand_offset, error_buf, \
  8015. error_buf_size))) \
  8016. goto fail; \
  8017. } while (0)
  8018. #define GET_CONST_F64_OFFSET(type, fval) \
  8019. do { \
  8020. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  8021. &operand_offset, error_buf, \
  8022. error_buf_size))) \
  8023. goto fail; \
  8024. } while (0)
  8025. #define emit_operand(ctx, offset) \
  8026. do { \
  8027. wasm_loader_emit_int16(ctx, offset); \
  8028. LOG_OP("%d\t", offset); \
  8029. } while (0)
  8030. #define emit_byte(ctx, byte) \
  8031. do { \
  8032. wasm_loader_emit_uint8(ctx, byte); \
  8033. LOG_OP("%d\t", byte); \
  8034. } while (0)
  8035. #define emit_uint32(ctx, value) \
  8036. do { \
  8037. wasm_loader_emit_uint32(ctx, value); \
  8038. LOG_OP("%d\t", value); \
  8039. } while (0)
  8040. #define emit_uint64(ctx, value) \
  8041. do { \
  8042. wasm_loader_emit_const(ctx, &value, false); \
  8043. LOG_OP("%lld\t", value); \
  8044. } while (0)
  8045. #define emit_float32(ctx, value) \
  8046. do { \
  8047. wasm_loader_emit_const(ctx, &value, true); \
  8048. LOG_OP("%f\t", value); \
  8049. } while (0)
  8050. #define emit_float64(ctx, value) \
  8051. do { \
  8052. wasm_loader_emit_const(ctx, &value, false); \
  8053. LOG_OP("%f\t", value); \
  8054. } while (0)
  8055. static bool
  8056. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  8057. {
  8058. if (!(ctx->p_code_compiled =
  8059. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  8060. return false;
  8061. ctx->p_code_compiled_end =
  8062. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  8063. /* clean up frame ref */
  8064. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  8065. ctx->frame_ref = ctx->frame_ref_bottom;
  8066. ctx->stack_cell_num = 0;
  8067. #if WASM_ENABLE_GC != 0
  8068. /* clean up reftype map */
  8069. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  8070. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  8071. ctx->reftype_map_num = 0;
  8072. #endif
  8073. /* clean up frame csp */
  8074. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  8075. ctx->frame_csp = ctx->frame_csp_bottom;
  8076. ctx->csp_num = 0;
  8077. ctx->max_csp_num = 0;
  8078. /* clean up frame offset */
  8079. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  8080. ctx->frame_offset = ctx->frame_offset_bottom;
  8081. ctx->dynamic_offset = ctx->start_dynamic_offset;
  8082. /* init preserved local offsets */
  8083. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  8084. /* const buf is reserved */
  8085. return true;
  8086. }
  8087. static void
  8088. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  8089. {
  8090. ctx->code_compiled_size += size;
  8091. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  8092. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  8093. }
  8094. }
  8095. static void
  8096. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  8097. {
  8098. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  8099. if (ctx->p_code_compiled) {
  8100. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8101. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8102. #endif
  8103. bh_memcpy_s(ctx->p_code_compiled,
  8104. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  8105. value, size);
  8106. ctx->p_code_compiled += size;
  8107. }
  8108. else {
  8109. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8110. bh_assert((ctx->code_compiled_size & 1) == 0);
  8111. #endif
  8112. increase_compiled_code_space(ctx, size);
  8113. }
  8114. }
  8115. static void
  8116. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  8117. {
  8118. if (ctx->p_code_compiled) {
  8119. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8120. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8121. #endif
  8122. STORE_U32(ctx->p_code_compiled, value);
  8123. ctx->p_code_compiled += sizeof(uint32);
  8124. }
  8125. else {
  8126. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8127. bh_assert((ctx->code_compiled_size & 1) == 0);
  8128. #endif
  8129. increase_compiled_code_space(ctx, sizeof(uint32));
  8130. }
  8131. }
  8132. static void
  8133. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  8134. {
  8135. if (ctx->p_code_compiled) {
  8136. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8137. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8138. #endif
  8139. STORE_U16(ctx->p_code_compiled, (uint16)value);
  8140. ctx->p_code_compiled += sizeof(int16);
  8141. }
  8142. else {
  8143. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8144. bh_assert((ctx->code_compiled_size & 1) == 0);
  8145. #endif
  8146. increase_compiled_code_space(ctx, sizeof(uint16));
  8147. }
  8148. }
  8149. static void
  8150. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  8151. {
  8152. if (ctx->p_code_compiled) {
  8153. *(ctx->p_code_compiled) = value;
  8154. ctx->p_code_compiled += sizeof(uint8);
  8155. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8156. ctx->p_code_compiled++;
  8157. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8158. #endif
  8159. }
  8160. else {
  8161. increase_compiled_code_space(ctx, sizeof(uint8));
  8162. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8163. increase_compiled_code_space(ctx, sizeof(uint8));
  8164. bh_assert((ctx->code_compiled_size & 1) == 0);
  8165. #endif
  8166. }
  8167. }
  8168. static void
  8169. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  8170. {
  8171. if (ctx->p_code_compiled) {
  8172. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8173. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8174. #endif
  8175. STORE_PTR(ctx->p_code_compiled, value);
  8176. ctx->p_code_compiled += sizeof(void *);
  8177. }
  8178. else {
  8179. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8180. bh_assert((ctx->code_compiled_size & 1) == 0);
  8181. #endif
  8182. increase_compiled_code_space(ctx, sizeof(void *));
  8183. }
  8184. }
  8185. static void
  8186. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8187. {
  8188. if (ctx->p_code_compiled) {
  8189. ctx->p_code_compiled -= size;
  8190. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8191. if (size == sizeof(uint8)) {
  8192. ctx->p_code_compiled--;
  8193. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8194. }
  8195. #endif
  8196. }
  8197. else {
  8198. ctx->code_compiled_size -= size;
  8199. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8200. if (size == sizeof(uint8)) {
  8201. ctx->code_compiled_size--;
  8202. bh_assert((ctx->code_compiled_size & 1) == 0);
  8203. }
  8204. #endif
  8205. }
  8206. }
  8207. static bool
  8208. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8209. uint32 local_index, uint32 local_type,
  8210. bool *preserved, char *error_buf,
  8211. uint32 error_buf_size)
  8212. {
  8213. uint32 i = 0;
  8214. int16 preserved_offset = (int16)local_index;
  8215. *preserved = false;
  8216. while (i < loader_ctx->stack_cell_num) {
  8217. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8218. /* move previous local into dynamic space before a set/tee_local opcode
  8219. */
  8220. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8221. if (!(*preserved)) {
  8222. *preserved = true;
  8223. skip_label();
  8224. preserved_offset = loader_ctx->preserved_local_offset;
  8225. if (loader_ctx->p_code_compiled) {
  8226. bh_assert(preserved_offset != (int16)local_index);
  8227. }
  8228. if (is_32bit_type(local_type)) {
  8229. /* Only increase preserve offset in the second traversal */
  8230. if (loader_ctx->p_code_compiled)
  8231. loader_ctx->preserved_local_offset++;
  8232. emit_label(EXT_OP_COPY_STACK_TOP);
  8233. }
  8234. #if WASM_ENABLE_SIMDE != 0
  8235. else if (local_type == VALUE_TYPE_V128) {
  8236. if (loader_ctx->p_code_compiled)
  8237. loader_ctx->preserved_local_offset += 4;
  8238. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8239. }
  8240. #endif
  8241. else {
  8242. if (loader_ctx->p_code_compiled)
  8243. loader_ctx->preserved_local_offset += 2;
  8244. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8245. }
  8246. /* overflow */
  8247. if (preserved_offset > loader_ctx->preserved_local_offset) {
  8248. set_error_buf_v(error_buf, error_buf_size,
  8249. "too much local cells 0x%x",
  8250. loader_ctx->preserved_local_offset);
  8251. return false;
  8252. }
  8253. emit_operand(loader_ctx, local_index);
  8254. emit_operand(loader_ctx, preserved_offset);
  8255. emit_label(opcode);
  8256. }
  8257. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8258. }
  8259. if (cur_type == VALUE_TYPE_V128) {
  8260. i += 4;
  8261. }
  8262. else if (is_32bit_type(cur_type)) {
  8263. i++;
  8264. }
  8265. else {
  8266. i += 2;
  8267. }
  8268. }
  8269. (void)error_buf;
  8270. (void)error_buf_size;
  8271. return true;
  8272. }
  8273. static bool
  8274. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8275. char *error_buf, uint32 error_buf_size)
  8276. {
  8277. uint32 i = 0;
  8278. bool preserve_local;
  8279. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8280. blocks will not influence the value of these locals */
  8281. while (i < loader_ctx->stack_cell_num) {
  8282. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8283. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8284. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8285. && (cur_offset >= 0)) {
  8286. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8287. cur_type, &preserve_local,
  8288. error_buf, error_buf_size)))
  8289. return false;
  8290. }
  8291. if (cur_type == VALUE_TYPE_V128) {
  8292. i += 4;
  8293. }
  8294. else if (is_32bit_type(cur_type)) {
  8295. i++;
  8296. }
  8297. else {
  8298. i += 2;
  8299. }
  8300. }
  8301. return true;
  8302. }
  8303. static bool
  8304. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8305. uint8 *p_code_compiled, char *error_buf,
  8306. uint32 error_buf_size)
  8307. {
  8308. BranchBlockPatch *patch =
  8309. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8310. if (!patch) {
  8311. return false;
  8312. }
  8313. patch->patch_type = patch_type;
  8314. patch->code_compiled = p_code_compiled;
  8315. if (!frame_csp->patch_list) {
  8316. frame_csp->patch_list = patch;
  8317. patch->next = NULL;
  8318. }
  8319. else {
  8320. patch->next = frame_csp->patch_list;
  8321. frame_csp->patch_list = patch;
  8322. }
  8323. return true;
  8324. }
  8325. static void
  8326. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8327. {
  8328. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8329. BranchBlockPatch *node = frame_csp->patch_list;
  8330. BranchBlockPatch *node_prev = NULL, *node_next;
  8331. if (!ctx->p_code_compiled)
  8332. return;
  8333. while (node) {
  8334. node_next = node->next;
  8335. if (node->patch_type == patch_type) {
  8336. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8337. if (node_prev == NULL) {
  8338. frame_csp->patch_list = node_next;
  8339. }
  8340. else {
  8341. node_prev->next = node_next;
  8342. }
  8343. wasm_runtime_free(node);
  8344. }
  8345. else {
  8346. node_prev = node;
  8347. }
  8348. node = node_next;
  8349. }
  8350. }
  8351. static bool
  8352. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8353. bool is_br, char *error_buf, uint32 error_buf_size)
  8354. {
  8355. /* br info layout:
  8356. * a) arity of target block
  8357. * b) total cell num of arity values
  8358. * c) each arity value's cell num
  8359. * d) each arity value's src frame offset
  8360. * e) each arity values's dst dynamic offset
  8361. * f) branch target address
  8362. *
  8363. * Note: b-e are omitted when arity is 0 so that
  8364. * interpreter can recover the br info quickly.
  8365. */
  8366. BlockType *block_type = &frame_csp->block_type;
  8367. uint8 *types = NULL, cell;
  8368. #if WASM_ENABLE_GC != 0
  8369. WASMRefTypeMap *reftype_maps;
  8370. uint32 reftype_map_count;
  8371. #endif
  8372. uint32 arity = 0;
  8373. int32 i;
  8374. int16 *frame_offset = ctx->frame_offset;
  8375. uint16 dynamic_offset;
  8376. /* Note: loop's arity is different from if and block. loop's arity is
  8377. * its parameter count while if and block arity is result count.
  8378. */
  8379. #if WASM_ENABLE_GC == 0
  8380. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8381. arity = block_type_get_param_types(block_type, &types);
  8382. else
  8383. arity = block_type_get_result_types(block_type, &types);
  8384. #else
  8385. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8386. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8387. &reftype_map_count);
  8388. else
  8389. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8390. &reftype_map_count);
  8391. #endif
  8392. /* Part a */
  8393. emit_uint32(ctx, arity);
  8394. if (arity) {
  8395. /* Part b */
  8396. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8397. /* Part c */
  8398. for (i = (int32)arity - 1; i >= 0; i--) {
  8399. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8400. emit_byte(ctx, cell);
  8401. }
  8402. /* Part d */
  8403. for (i = (int32)arity - 1; i >= 0; i--) {
  8404. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8405. frame_offset -= cell;
  8406. emit_operand(ctx, *(int16 *)(frame_offset));
  8407. }
  8408. /* Part e */
  8409. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8410. /* Use start_dynamic_offset which was set in
  8411. copy_params_to_dynamic_space */
  8412. dynamic_offset = frame_csp->start_dynamic_offset
  8413. + wasm_get_cell_num(types, arity);
  8414. else
  8415. dynamic_offset =
  8416. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8417. if (is_br)
  8418. ctx->dynamic_offset = dynamic_offset;
  8419. for (i = (int32)arity - 1; i >= 0; i--) {
  8420. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8421. dynamic_offset -= cell;
  8422. emit_operand(ctx, dynamic_offset);
  8423. }
  8424. }
  8425. /* Part f */
  8426. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8427. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8428. }
  8429. else {
  8430. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8431. error_buf, error_buf_size))
  8432. return false;
  8433. /* label address, to be patched */
  8434. wasm_loader_emit_ptr(ctx, NULL);
  8435. }
  8436. return true;
  8437. }
  8438. static bool
  8439. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8440. bool disable_emit, int16 operand_offset,
  8441. char *error_buf, uint32 error_buf_size)
  8442. {
  8443. uint32 cell_num_to_push, i;
  8444. if (type == VALUE_TYPE_VOID)
  8445. return true;
  8446. /* only check memory overflow in first traverse */
  8447. if (ctx->p_code_compiled == NULL) {
  8448. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8449. return false;
  8450. }
  8451. if (disable_emit)
  8452. *(ctx->frame_offset)++ = operand_offset;
  8453. else {
  8454. emit_operand(ctx, ctx->dynamic_offset);
  8455. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8456. ctx->dynamic_offset++;
  8457. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8458. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8459. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8460. goto fail;
  8461. }
  8462. }
  8463. }
  8464. if (is_32bit_type(type))
  8465. return true;
  8466. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8467. for (i = 0; i < cell_num_to_push; i++) {
  8468. if (ctx->p_code_compiled == NULL) {
  8469. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8470. return false;
  8471. }
  8472. ctx->frame_offset++;
  8473. if (!disable_emit) {
  8474. ctx->dynamic_offset++;
  8475. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8476. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8477. if (ctx->max_dynamic_offset >= INT16_MAX)
  8478. goto fail;
  8479. }
  8480. }
  8481. }
  8482. return true;
  8483. fail:
  8484. set_error_buf(error_buf, error_buf_size,
  8485. "fast interpreter offset overflow");
  8486. return false;
  8487. }
  8488. /* This function should be in front of wasm_loader_pop_frame_ref
  8489. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8490. will be modified by wasm_loader_pop_frame_ref */
  8491. static bool
  8492. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8493. char *error_buf, uint32 error_buf_size)
  8494. {
  8495. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8496. then current block is the function block */
  8497. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8498. BranchBlock *cur_block = ctx->frame_csp - depth;
  8499. int32 available_stack_cell =
  8500. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8501. uint32 cell_num_to_pop;
  8502. /* Directly return success if current block is in stack
  8503. polymorphic state while stack is empty. */
  8504. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8505. return true;
  8506. if (type == VALUE_TYPE_VOID)
  8507. return true;
  8508. /* Change type to ANY when the stack top is ANY, so as to avoid
  8509. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8510. two offsets */
  8511. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8512. type = VALUE_TYPE_ANY;
  8513. cell_num_to_pop = wasm_value_type_cell_num(type);
  8514. /* Check the offset stack bottom to ensure the frame offset
  8515. stack will not go underflow. But we don't thrown error
  8516. and return true here, because the error msg should be
  8517. given in wasm_loader_pop_frame_ref */
  8518. if (!check_offset_pop(ctx, cell_num_to_pop))
  8519. return true;
  8520. ctx->frame_offset -= cell_num_to_pop;
  8521. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8522. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8523. ctx->dynamic_offset -= cell_num_to_pop;
  8524. emit_operand(ctx, *(ctx->frame_offset));
  8525. (void)error_buf;
  8526. (void)error_buf_size;
  8527. return true;
  8528. }
  8529. static bool
  8530. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8531. bool disable_emit, int16 operand_offset,
  8532. char *error_buf, uint32 error_buf_size)
  8533. {
  8534. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8535. error_buf, error_buf_size)))
  8536. return false;
  8537. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8538. return false;
  8539. return true;
  8540. }
  8541. static bool
  8542. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8543. char *error_buf, uint32 error_buf_size)
  8544. {
  8545. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8546. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8547. return false;
  8548. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8549. return false;
  8550. return true;
  8551. }
  8552. static bool
  8553. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8554. uint8 type_push, uint8 type_pop,
  8555. bool disable_emit, int16 operand_offset,
  8556. char *error_buf, uint32 error_buf_size)
  8557. {
  8558. uint8 i;
  8559. for (i = 0; i < pop_cnt; i++) {
  8560. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8561. error_buf_size))
  8562. return false;
  8563. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8564. error_buf_size))
  8565. return false;
  8566. }
  8567. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8568. operand_offset, error_buf,
  8569. error_buf_size))
  8570. return false;
  8571. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8572. return false;
  8573. return true;
  8574. }
  8575. static int
  8576. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8577. {
  8578. int64 i64_const1 = *(int64 *)p_i64_const1;
  8579. int64 i64_const2 = *(int64 *)p_i64_const2;
  8580. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8581. }
  8582. static int
  8583. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8584. {
  8585. int32 i32_const1 = *(int32 *)p_i32_const1;
  8586. int32 i32_const2 = *(int32 *)p_i32_const2;
  8587. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8588. }
  8589. static int
  8590. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  8591. {
  8592. V128 v128_const1 = *(V128 *)p_v128_const1;
  8593. V128 v128_const2 = *(V128 *)p_v128_const2;
  8594. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  8595. }
  8596. static bool
  8597. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8598. int16 *offset, char *error_buf,
  8599. uint32 error_buf_size)
  8600. {
  8601. if (!ctx->p_code_compiled) {
  8602. /* Treat i64 and f64 as the same by reading i64 value from
  8603. the raw bytes */
  8604. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8605. /* No slot left, emit const instead */
  8606. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8607. *offset = 0;
  8608. return true;
  8609. }
  8610. /* Traverse the list if the const num is small */
  8611. if (ctx->i64_const_num < 10) {
  8612. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8613. if (ctx->i64_consts[i] == *(int64 *)value) {
  8614. *offset = -1;
  8615. return true;
  8616. }
  8617. }
  8618. }
  8619. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8620. MEM_REALLOC(ctx->i64_consts,
  8621. sizeof(int64) * ctx->i64_const_max_num,
  8622. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8623. ctx->i64_const_max_num *= 2;
  8624. }
  8625. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8626. }
  8627. else if (type == VALUE_TYPE_V128) {
  8628. /* No slot left, emit const instead */
  8629. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  8630. *offset = 0;
  8631. return true;
  8632. }
  8633. /* Traverse the list if the const num is small */
  8634. if (ctx->v128_const_num < 10) {
  8635. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  8636. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  8637. == 0) {
  8638. *offset = -1;
  8639. return true;
  8640. }
  8641. }
  8642. }
  8643. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  8644. MEM_REALLOC(ctx->v128_consts,
  8645. sizeof(V128) * ctx->v128_const_max_num,
  8646. sizeof(V128) * (ctx->v128_const_max_num * 2));
  8647. ctx->v128_const_max_num *= 2;
  8648. }
  8649. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  8650. }
  8651. else {
  8652. /* Treat i32 and f32 as the same by reading i32 value from
  8653. the raw bytes */
  8654. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8655. /* No slot left, emit const instead */
  8656. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8657. *offset = 0;
  8658. return true;
  8659. }
  8660. /* Traverse the list if the const num is small */
  8661. if (ctx->i32_const_num < 10) {
  8662. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8663. if (ctx->i32_consts[i] == *(int32 *)value) {
  8664. *offset = -1;
  8665. return true;
  8666. }
  8667. }
  8668. }
  8669. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8670. MEM_REALLOC(ctx->i32_consts,
  8671. sizeof(int32) * ctx->i32_const_max_num,
  8672. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8673. ctx->i32_const_max_num *= 2;
  8674. }
  8675. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8676. }
  8677. *offset = -1;
  8678. return true;
  8679. }
  8680. else {
  8681. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8682. int64 key = *(int64 *)value, *i64_const;
  8683. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8684. sizeof(int64), cmp_i64_const);
  8685. if (!i64_const) { /* not found, emit const instead */
  8686. *offset = 0;
  8687. return true;
  8688. }
  8689. /* constant index is encoded as negative value */
  8690. *offset = -(int32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  8691. + (int32)(i64_const - ctx->i64_consts) * 2;
  8692. }
  8693. else if (type == VALUE_TYPE_V128) {
  8694. V128 key = *(V128 *)value, *v128_const;
  8695. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  8696. sizeof(V128), cmp_v128_const);
  8697. if (!v128_const) { /* not found, emit const instead */
  8698. *offset = 0;
  8699. return true;
  8700. }
  8701. /* constant index is encoded as negative value */
  8702. *offset = -(int32)(ctx->v128_const_num)
  8703. + (int32)(v128_const - ctx->v128_consts);
  8704. }
  8705. else {
  8706. int32 key = *(int32 *)value, *i32_const;
  8707. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  8708. sizeof(int32), cmp_i32_const);
  8709. if (!i32_const) { /* not found, emit const instead */
  8710. *offset = 0;
  8711. return true;
  8712. }
  8713. /* constant index is encoded as negative value */
  8714. *offset = -(int32)(ctx->i32_const_num)
  8715. + (int32)(i32_const - ctx->i32_consts);
  8716. }
  8717. return true;
  8718. }
  8719. fail:
  8720. return false;
  8721. }
  8722. /*
  8723. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8724. -- Mostly used for the binary / compare operation
  8725. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8726. -- Mostly used in block / control instructions
  8727. The POP will always emit the offset on the top of the frame_offset stack
  8728. PUSH can be used in two ways:
  8729. 1. directly PUSH:
  8730. PUSH_XXX();
  8731. will allocate a dynamic space and emit
  8732. 2. silent PUSH:
  8733. operand_offset = xxx; disable_emit = true;
  8734. PUSH_XXX();
  8735. only push the frame_offset stack, no emit
  8736. */
  8737. #define TEMPLATE_PUSH(Type) \
  8738. do { \
  8739. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8740. disable_emit, operand_offset, \
  8741. error_buf, error_buf_size)) \
  8742. goto fail; \
  8743. } while (0)
  8744. #define TEMPLATE_PUSH_REF(Type) \
  8745. do { \
  8746. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8747. operand_offset, error_buf, \
  8748. error_buf_size)) \
  8749. goto fail; \
  8750. } while (0)
  8751. #define TEMPLATE_POP(Type) \
  8752. do { \
  8753. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8754. error_buf, error_buf_size)) \
  8755. goto fail; \
  8756. } while (0)
  8757. #define TEMPLATE_POP_REF(Type) \
  8758. do { \
  8759. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8760. error_buf_size)) \
  8761. goto fail; \
  8762. } while (0)
  8763. #define PUSH_OFFSET_TYPE(type) \
  8764. do { \
  8765. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8766. operand_offset, error_buf, \
  8767. error_buf_size))) \
  8768. goto fail; \
  8769. } while (0)
  8770. #define POP_OFFSET_TYPE(type) \
  8771. do { \
  8772. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8773. error_buf_size))) \
  8774. goto fail; \
  8775. } while (0)
  8776. #define POP_AND_PUSH(type_pop, type_push) \
  8777. do { \
  8778. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8779. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8780. operand_offset, error_buf, error_buf_size))) \
  8781. goto fail; \
  8782. } while (0)
  8783. /* type of POPs should be the same */
  8784. #define POP2_AND_PUSH(type_pop, type_push) \
  8785. do { \
  8786. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8787. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8788. operand_offset, error_buf, error_buf_size))) \
  8789. goto fail; \
  8790. } while (0)
  8791. #else /* WASM_ENABLE_FAST_INTERP */
  8792. #define TEMPLATE_PUSH(Type) \
  8793. do { \
  8794. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8795. error_buf, error_buf_size))) \
  8796. goto fail; \
  8797. } while (0)
  8798. #define TEMPLATE_PUSH_REF(Type) \
  8799. do { \
  8800. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8801. error_buf_size))) \
  8802. goto fail; \
  8803. } while (0)
  8804. #define TEMPLATE_POP(Type) \
  8805. do { \
  8806. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8807. error_buf, error_buf_size))) \
  8808. goto fail; \
  8809. } while (0)
  8810. #define TEMPLATE_POP_REF(Type) \
  8811. do { \
  8812. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8813. error_buf_size))) \
  8814. goto fail; \
  8815. } while (0)
  8816. #define POP_AND_PUSH(type_pop, type_push) \
  8817. do { \
  8818. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8819. type_pop, error_buf, \
  8820. error_buf_size))) \
  8821. goto fail; \
  8822. } while (0)
  8823. /* type of POPs should be the same */
  8824. #define POP2_AND_PUSH(type_pop, type_push) \
  8825. do { \
  8826. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8827. type_pop, error_buf, \
  8828. error_buf_size))) \
  8829. goto fail; \
  8830. } while (0)
  8831. #endif /* WASM_ENABLE_FAST_INTERP */
  8832. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8833. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8834. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8835. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8836. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8837. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8838. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8839. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8840. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8841. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8842. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8843. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8844. #define POP_I32() TEMPLATE_POP(I32)
  8845. #define POP_F32() TEMPLATE_POP(F32)
  8846. #define POP_I64() TEMPLATE_POP(I64)
  8847. #define POP_F64() TEMPLATE_POP(F64)
  8848. #define POP_V128() TEMPLATE_POP(V128)
  8849. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8850. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8851. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8852. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8853. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8854. #if WASM_ENABLE_FAST_INTERP != 0
  8855. static bool
  8856. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8857. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8858. {
  8859. int16 operand_offset = 0;
  8860. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8861. : loader_ctx->frame_csp;
  8862. BlockType *block_type = &block->block_type;
  8863. uint8 *return_types = NULL;
  8864. #if WASM_ENABLE_GC != 0
  8865. WASMRefTypeMap *reftype_maps = NULL;
  8866. uint32 reftype_map_count;
  8867. #endif
  8868. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8869. int32 i = 0;
  8870. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8871. *frame_offset_org = NULL;
  8872. #if WASM_ENABLE_GC == 0
  8873. return_count = block_type_get_result_types(block_type, &return_types);
  8874. #else
  8875. return_count = block_type_get_result_types(
  8876. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8877. #endif
  8878. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  8879. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8880. if (return_count == 1) {
  8881. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8882. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8883. /* insert op_copy before else opcode */
  8884. if (opcode == WASM_OP_ELSE)
  8885. skip_label();
  8886. #if WASM_ENABLE_SIMDE != 0
  8887. if (cell == 4) {
  8888. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8889. }
  8890. #endif
  8891. if (cell <= 2) {
  8892. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8893. : EXT_OP_COPY_STACK_TOP_I64);
  8894. }
  8895. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8896. emit_operand(loader_ctx, block->dynamic_offset);
  8897. if (opcode == WASM_OP_ELSE) {
  8898. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8899. }
  8900. else {
  8901. loader_ctx->frame_offset -= cell;
  8902. loader_ctx->dynamic_offset = block->dynamic_offset;
  8903. PUSH_OFFSET_TYPE(return_types[0]);
  8904. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8905. }
  8906. if (opcode == WASM_OP_ELSE)
  8907. emit_label(opcode);
  8908. }
  8909. return true;
  8910. }
  8911. /* Copy stack top values to block's results which are in dynamic space.
  8912. * The instruction format:
  8913. * Part a: values count
  8914. * Part b: all values total cell num
  8915. * Part c: each value's cell_num, src offset and dst offset
  8916. * Part d: each value's src offset and dst offset
  8917. * Part e: each value's dst offset
  8918. */
  8919. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8920. dynamic_offset = dynamic_offset_org =
  8921. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8922. /* First traversal to get the count of values needed to be copied. */
  8923. for (i = (int32)return_count - 1; i >= 0; i--) {
  8924. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8925. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  8926. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  8927. goto fail;
  8928. }
  8929. if (cells == 4) {
  8930. bool needs_copy = false;
  8931. int16 v128_dynamic = dynamic_offset - cells;
  8932. for (int j = 0; j < 4; j++) {
  8933. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  8934. needs_copy = true;
  8935. break;
  8936. }
  8937. }
  8938. if (needs_copy) {
  8939. value_count++;
  8940. total_cel_num += cells;
  8941. }
  8942. frame_offset -= cells;
  8943. dynamic_offset = v128_dynamic;
  8944. }
  8945. else {
  8946. frame_offset -= cells;
  8947. dynamic_offset -= cells;
  8948. if (dynamic_offset != *frame_offset) {
  8949. value_count++;
  8950. total_cel_num += cells;
  8951. }
  8952. }
  8953. }
  8954. if (value_count) {
  8955. uint32 j = 0;
  8956. uint8 *emit_data = NULL, *cells = NULL;
  8957. int16 *src_offsets = NULL;
  8958. uint16 *dst_offsets = NULL;
  8959. uint64 size =
  8960. (uint64)value_count
  8961. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8962. /* Allocate memory for the emit data */
  8963. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8964. return false;
  8965. cells = emit_data;
  8966. src_offsets = (int16 *)(cells + value_count);
  8967. dst_offsets = (uint16 *)(src_offsets + value_count);
  8968. /* insert op_copy before else opcode */
  8969. if (opcode == WASM_OP_ELSE)
  8970. skip_label();
  8971. emit_label(EXT_OP_COPY_STACK_VALUES);
  8972. /* Part a) */
  8973. emit_uint32(loader_ctx, value_count);
  8974. /* Part b) */
  8975. emit_uint32(loader_ctx, total_cel_num);
  8976. /* Second traversal to get each value's cell num, src offset and dst
  8977. * offset. */
  8978. frame_offset = frame_offset_org;
  8979. dynamic_offset = dynamic_offset_org;
  8980. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8981. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8982. if (cell == 4) {
  8983. bool needs_copy = false;
  8984. int16 v128_dynamic = dynamic_offset - cell;
  8985. for (int k = 0; k < 4; k++) {
  8986. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  8987. needs_copy = true;
  8988. break;
  8989. }
  8990. }
  8991. if (needs_copy) {
  8992. cells[j] = cell;
  8993. src_offsets[j] = *(frame_offset - cell);
  8994. dst_offsets[j] = v128_dynamic;
  8995. j++;
  8996. }
  8997. frame_offset -= cell;
  8998. dynamic_offset = v128_dynamic;
  8999. }
  9000. else {
  9001. frame_offset -= cell;
  9002. dynamic_offset -= cell;
  9003. if (dynamic_offset != *frame_offset) {
  9004. cells[j] = cell;
  9005. /* src offset */
  9006. src_offsets[j] = *frame_offset;
  9007. /* dst offset */
  9008. dst_offsets[j] = dynamic_offset;
  9009. j++;
  9010. }
  9011. }
  9012. if (opcode == WASM_OP_ELSE) {
  9013. if (cell == 4) {
  9014. for (int k = 0; k < cell; k++) {
  9015. *(frame_offset + k) = dynamic_offset + k;
  9016. }
  9017. }
  9018. else {
  9019. *frame_offset = dynamic_offset;
  9020. }
  9021. }
  9022. else {
  9023. loader_ctx->frame_offset = frame_offset;
  9024. loader_ctx->dynamic_offset = dynamic_offset;
  9025. if (!(wasm_loader_push_frame_offset(
  9026. loader_ctx, return_types[i], disable_emit,
  9027. operand_offset, error_buf, error_buf_size))) {
  9028. wasm_runtime_free(emit_data);
  9029. goto fail;
  9030. }
  9031. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9032. loader_ctx->frame_offset = frame_offset_org;
  9033. loader_ctx->dynamic_offset = dynamic_offset_org;
  9034. }
  9035. }
  9036. bh_assert(j == value_count);
  9037. /* Emit the cells, src_offsets and dst_offsets */
  9038. for (j = 0; j < value_count; j++)
  9039. emit_byte(loader_ctx, cells[j]);
  9040. for (j = 0; j < value_count; j++)
  9041. emit_operand(loader_ctx, src_offsets[j]);
  9042. for (j = 0; j < value_count; j++)
  9043. emit_operand(loader_ctx, dst_offsets[j]);
  9044. if (opcode == WASM_OP_ELSE)
  9045. emit_label(opcode);
  9046. wasm_runtime_free(emit_data);
  9047. }
  9048. return true;
  9049. fail:
  9050. return false;
  9051. }
  9052. #endif /* WASM_ENABLE_FAST_INTERP */
  9053. #define PUSH_TYPE(type) \
  9054. do { \
  9055. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9056. error_buf_size))) \
  9057. goto fail; \
  9058. } while (0)
  9059. #define POP_TYPE(type) \
  9060. do { \
  9061. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9062. error_buf_size))) \
  9063. goto fail; \
  9064. } while (0)
  9065. #if WASM_ENABLE_GC == 0
  9066. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9067. do { \
  9068. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9069. _start_addr, error_buf, \
  9070. error_buf_size)) \
  9071. goto fail; \
  9072. } while (0)
  9073. #define POP_CSP() \
  9074. do { \
  9075. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9076. goto fail; \
  9077. } while (0)
  9078. #else
  9079. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9080. do { \
  9081. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9082. _start_addr, error_buf, \
  9083. error_buf_size)) \
  9084. goto fail; \
  9085. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9086. error_buf, error_buf_size)) { \
  9087. goto fail; \
  9088. } \
  9089. } while (0)
  9090. #define POP_CSP() \
  9091. do { \
  9092. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9093. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9094. goto fail; \
  9095. } while (0)
  9096. #endif /* end of WASM_ENABLE_GC == 0 */
  9097. #if WASM_ENABLE_GC == 0
  9098. #define GET_LOCAL_REFTYPE() (void)0
  9099. #else
  9100. #define GET_LOCAL_REFTYPE() \
  9101. do { \
  9102. if (wasm_is_type_multi_byte_type(local_type)) { \
  9103. WASMRefType *_ref_type; \
  9104. if (local_idx < param_count) \
  9105. _ref_type = wasm_reftype_map_find( \
  9106. param_reftype_maps, param_reftype_map_count, local_idx); \
  9107. else \
  9108. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9109. local_reftype_map_count, \
  9110. local_idx - param_count); \
  9111. bh_assert(_ref_type); \
  9112. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9113. wasm_reftype_struct_size(_ref_type)); \
  9114. } \
  9115. } while (0)
  9116. #endif /* end of WASM_ENABLE_GC == 0 */
  9117. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9118. do { \
  9119. read_leb_uint32(p, p_end, local_idx); \
  9120. if (local_idx >= param_count + local_count) { \
  9121. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9122. goto fail; \
  9123. } \
  9124. local_type = local_idx < param_count \
  9125. ? param_types[local_idx] \
  9126. : local_types[local_idx - param_count]; \
  9127. local_offset = local_offsets[local_idx]; \
  9128. GET_LOCAL_REFTYPE(); \
  9129. } while (0)
  9130. static bool
  9131. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9132. {
  9133. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9134. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9135. return false;
  9136. }
  9137. return true;
  9138. }
  9139. #define CHECK_MEMORY() \
  9140. do { \
  9141. if (!check_memory(module, error_buf, error_buf_size)) \
  9142. goto fail; \
  9143. } while (0)
  9144. static bool
  9145. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9146. uint32 error_buf_size)
  9147. {
  9148. uint8 mem_access_aligns[] = {
  9149. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9150. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9151. };
  9152. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9153. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9154. set_error_buf(error_buf, error_buf_size,
  9155. "invalid memop flags: alignment must not be larger "
  9156. "than natural");
  9157. return false;
  9158. }
  9159. return true;
  9160. }
  9161. #if WASM_ENABLE_SIMD != 0
  9162. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9163. || (WASM_ENABLE_FAST_INTERP != 0)
  9164. static bool
  9165. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9166. uint32 error_buf_size)
  9167. {
  9168. uint8 mem_access_aligns[] = {
  9169. 4, /* load */
  9170. 3, 3, 3, 3, 3, 3, /* load and extend */
  9171. 0, 1, 2, 3, /* load and splat */
  9172. 4, /* store */
  9173. };
  9174. uint8 mem_access_aligns_load_lane[] = {
  9175. 0, 1, 2, 3, /* load lane */
  9176. 0, 1, 2, 3, /* store lane */
  9177. 2, 3 /* store zero */
  9178. };
  9179. if (!((opcode <= SIMD_v128_store)
  9180. || (SIMD_v128_load8_lane <= opcode
  9181. && opcode <= SIMD_v128_load64_zero))) {
  9182. set_error_buf(error_buf, error_buf_size,
  9183. "the opcode doesn't include memarg");
  9184. return false;
  9185. }
  9186. if ((opcode <= SIMD_v128_store
  9187. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9188. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9189. && align > mem_access_aligns_load_lane[opcode
  9190. - SIMD_v128_load8_lane])) {
  9191. set_error_buf(error_buf, error_buf_size,
  9192. "invalid memop flags: alignment must not be larger "
  9193. "than natural");
  9194. return false;
  9195. }
  9196. return true;
  9197. }
  9198. static bool
  9199. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9200. uint32 error_buf_size)
  9201. {
  9202. switch (opcode) {
  9203. case SIMD_i8x16_extract_lane_s:
  9204. case SIMD_i8x16_extract_lane_u:
  9205. case SIMD_i8x16_replace_lane:
  9206. if (lane >= 16) {
  9207. goto fail;
  9208. }
  9209. break;
  9210. case SIMD_i16x8_extract_lane_s:
  9211. case SIMD_i16x8_extract_lane_u:
  9212. case SIMD_i16x8_replace_lane:
  9213. if (lane >= 8) {
  9214. goto fail;
  9215. }
  9216. break;
  9217. case SIMD_i32x4_extract_lane:
  9218. case SIMD_i32x4_replace_lane:
  9219. case SIMD_f32x4_extract_lane:
  9220. case SIMD_f32x4_replace_lane:
  9221. if (lane >= 4) {
  9222. goto fail;
  9223. }
  9224. break;
  9225. case SIMD_i64x2_extract_lane:
  9226. case SIMD_i64x2_replace_lane:
  9227. case SIMD_f64x2_extract_lane:
  9228. case SIMD_f64x2_replace_lane:
  9229. if (lane >= 2) {
  9230. goto fail;
  9231. }
  9232. break;
  9233. case SIMD_v128_load8_lane:
  9234. case SIMD_v128_load16_lane:
  9235. case SIMD_v128_load32_lane:
  9236. case SIMD_v128_load64_lane:
  9237. case SIMD_v128_store8_lane:
  9238. case SIMD_v128_store16_lane:
  9239. case SIMD_v128_store32_lane:
  9240. case SIMD_v128_store64_lane:
  9241. case SIMD_v128_load32_zero:
  9242. case SIMD_v128_load64_zero:
  9243. {
  9244. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9245. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9246. goto fail;
  9247. }
  9248. break;
  9249. }
  9250. default:
  9251. goto fail;
  9252. }
  9253. return true;
  9254. fail:
  9255. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9256. return false;
  9257. }
  9258. static bool
  9259. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9260. {
  9261. uint8 i;
  9262. for (i = 0; i != 16; ++i) {
  9263. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9264. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9265. return false;
  9266. }
  9267. }
  9268. return true;
  9269. }
  9270. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9271. #endif /* end of WASM_ENABLE_SIMD */
  9272. #if WASM_ENABLE_SHARED_MEMORY != 0
  9273. static bool
  9274. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9275. uint32 error_buf_size)
  9276. {
  9277. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9278. uint8 mem_access_aligns[] = {
  9279. 2, 3, 0, 1, 0, 1, 2,
  9280. };
  9281. uint8 expect;
  9282. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9283. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9284. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9285. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9286. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9287. }
  9288. else {
  9289. /* 7 opcodes in every group */
  9290. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9291. }
  9292. if (align != expect) {
  9293. set_error_buf(error_buf, error_buf_size,
  9294. "alignment isn't equal to natural");
  9295. return false;
  9296. }
  9297. return true;
  9298. }
  9299. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9300. static bool
  9301. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9302. char *error_buf, uint32 error_buf_size)
  9303. {
  9304. BranchBlock *target_block, *cur_block;
  9305. BlockType *target_block_type;
  9306. uint8 type, *types = NULL, *frame_ref;
  9307. uint32 arity = 0;
  9308. int32 i, available_stack_cell;
  9309. uint16 cell_num;
  9310. #if WASM_ENABLE_GC != 0
  9311. WASMRefTypeMap *frame_reftype_map;
  9312. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9313. WASMRefType *ref_type;
  9314. uint32 reftype_map_count = 0;
  9315. int32 available_reftype_map;
  9316. bool is_type_multi_byte;
  9317. #endif
  9318. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9319. uint8 *frame_ref_after_popped = NULL;
  9320. uint8 frame_ref_tmp[4] = { 0 };
  9321. uint8 *frame_ref_buf = frame_ref_tmp;
  9322. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9323. #if WASM_ENABLE_GC != 0
  9324. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9325. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9326. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9327. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9328. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9329. #endif
  9330. #if WASM_ENABLE_FAST_INTERP != 0
  9331. int16 *frame_offset_old = loader_ctx->frame_offset;
  9332. int16 *frame_offset_after_popped = NULL;
  9333. int16 frame_offset_tmp[4] = { 0 };
  9334. int16 *frame_offset_buf = frame_offset_tmp;
  9335. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9336. #endif
  9337. bool ret = false;
  9338. bh_assert(loader_ctx->csp_num > 0);
  9339. if (loader_ctx->csp_num - 1 < depth) {
  9340. set_error_buf(error_buf, error_buf_size,
  9341. "unknown label, "
  9342. "unexpected end of section or function");
  9343. return false;
  9344. }
  9345. cur_block = loader_ctx->frame_csp - 1;
  9346. target_block = loader_ctx->frame_csp - (depth + 1);
  9347. target_block_type = &target_block->block_type;
  9348. frame_ref = loader_ctx->frame_ref;
  9349. #if WASM_ENABLE_GC != 0
  9350. frame_reftype_map = loader_ctx->frame_reftype_map;
  9351. #endif
  9352. /* Note: loop's arity is different from if and block. loop's arity is
  9353. * its parameter count while if and block arity is result count.
  9354. */
  9355. #if WASM_ENABLE_GC == 0
  9356. if (target_block->label_type == LABEL_TYPE_LOOP)
  9357. arity = block_type_get_param_types(target_block_type, &types);
  9358. else
  9359. arity = block_type_get_result_types(target_block_type, &types);
  9360. #else
  9361. if (target_block->label_type == LABEL_TYPE_LOOP)
  9362. arity = block_type_get_param_types(target_block_type, &types,
  9363. &reftype_maps, &reftype_map_count);
  9364. else
  9365. arity = block_type_get_result_types(target_block_type, &types,
  9366. &reftype_maps, &reftype_map_count);
  9367. #endif
  9368. /* If the stack is in polymorphic state, just clear the stack
  9369. * and then re-push the values to make the stack top values
  9370. * match block type. */
  9371. if (cur_block->is_stack_polymorphic) {
  9372. #if WASM_ENABLE_GC != 0
  9373. int32 j = reftype_map_count - 1;
  9374. #endif
  9375. for (i = (int32)arity - 1; i >= 0; i--) {
  9376. #if WASM_ENABLE_GC != 0
  9377. if (wasm_is_type_multi_byte_type(types[i])) {
  9378. bh_assert(reftype_maps[j].index == i);
  9379. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9380. reftype_maps[j].ref_type,
  9381. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9382. j--;
  9383. }
  9384. #endif
  9385. #if WASM_ENABLE_FAST_INTERP != 0
  9386. POP_OFFSET_TYPE(types[i]);
  9387. #endif
  9388. POP_TYPE(types[i]);
  9389. }
  9390. /* Backup stack data since it may be changed in the below
  9391. push operations, and the stack data may be used when
  9392. checking other target blocks of opcode br_table */
  9393. if (opcode == WASM_OP_BR_TABLE) {
  9394. uint64 total_size;
  9395. frame_ref_after_popped = loader_ctx->frame_ref;
  9396. total_size = (uint64)sizeof(uint8)
  9397. * (frame_ref_old - frame_ref_after_popped);
  9398. if (total_size > sizeof(frame_ref_tmp)
  9399. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9400. error_buf_size))) {
  9401. goto fail;
  9402. }
  9403. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9404. frame_ref_after_popped, (uint32)total_size);
  9405. #if WASM_ENABLE_GC != 0
  9406. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9407. total_size =
  9408. (uint64)sizeof(WASMRefTypeMap)
  9409. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9410. if (total_size > sizeof(frame_reftype_map_tmp)
  9411. && !(frame_reftype_map_buf = loader_malloc(
  9412. total_size, error_buf, error_buf_size))) {
  9413. goto fail;
  9414. }
  9415. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9416. frame_reftype_map_after_popped, (uint32)total_size);
  9417. #endif
  9418. #if WASM_ENABLE_FAST_INTERP != 0
  9419. frame_offset_after_popped = loader_ctx->frame_offset;
  9420. total_size = (uint64)sizeof(int16)
  9421. * (frame_offset_old - frame_offset_after_popped);
  9422. if (total_size > sizeof(frame_offset_tmp)
  9423. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9424. error_buf_size))) {
  9425. goto fail;
  9426. }
  9427. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9428. frame_offset_after_popped, (uint32)total_size);
  9429. #endif
  9430. }
  9431. #if WASM_ENABLE_GC != 0
  9432. j = 0;
  9433. #endif
  9434. for (i = 0; i < (int32)arity; i++) {
  9435. #if WASM_ENABLE_GC != 0
  9436. if (wasm_is_type_multi_byte_type(types[i])) {
  9437. bh_assert(reftype_maps[j].index == i);
  9438. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9439. reftype_maps[j].ref_type,
  9440. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9441. j++;
  9442. }
  9443. #endif
  9444. #if WASM_ENABLE_FAST_INTERP != 0
  9445. bool disable_emit = true;
  9446. int16 operand_offset = 0;
  9447. PUSH_OFFSET_TYPE(types[i]);
  9448. #endif
  9449. PUSH_TYPE(types[i]);
  9450. }
  9451. #if WASM_ENABLE_FAST_INTERP != 0
  9452. emit_br_info(target_block, opcode == WASM_OP_BR);
  9453. #endif
  9454. /* Restore the stack data, note that frame_ref_bottom,
  9455. frame_reftype_map_bottom, frame_offset_bottom may be
  9456. re-allocated in the above push operations */
  9457. if (opcode == WASM_OP_BR_TABLE) {
  9458. uint32 total_size;
  9459. /* The stack operand num should not be smaller than before
  9460. after pop and push operations */
  9461. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9462. loader_ctx->stack_cell_num = stack_cell_num_old;
  9463. loader_ctx->frame_ref =
  9464. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9465. total_size = (uint32)(sizeof(uint8)
  9466. * (frame_ref_old - frame_ref_after_popped));
  9467. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9468. frame_ref_buf, total_size);
  9469. #if WASM_ENABLE_GC != 0
  9470. /* The stack operand num should not be smaller than before
  9471. after pop and push operations */
  9472. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9473. loader_ctx->reftype_map_num = reftype_map_num_old;
  9474. loader_ctx->frame_reftype_map =
  9475. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9476. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9477. * (frame_reftype_map_old
  9478. - frame_reftype_map_after_popped));
  9479. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9480. total_size, frame_reftype_map_buf, total_size);
  9481. #endif
  9482. #if WASM_ENABLE_FAST_INTERP != 0
  9483. loader_ctx->frame_offset =
  9484. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9485. total_size =
  9486. (uint32)(sizeof(int16)
  9487. * (frame_offset_old - frame_offset_after_popped));
  9488. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9489. total_size, frame_offset_buf, total_size);
  9490. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9491. #endif
  9492. }
  9493. ret = true;
  9494. goto cleanup_and_return;
  9495. }
  9496. available_stack_cell =
  9497. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9498. #if WASM_ENABLE_GC != 0
  9499. available_reftype_map =
  9500. (int32)(loader_ctx->reftype_map_num
  9501. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9502. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9503. #endif
  9504. /* Check stack top values match target block type */
  9505. for (i = (int32)arity - 1; i >= 0; i--) {
  9506. type = types[i];
  9507. #if WASM_ENABLE_GC != 0
  9508. ref_type = NULL;
  9509. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9510. if (is_type_multi_byte) {
  9511. bh_assert(reftype_map);
  9512. ref_type = reftype_map->ref_type;
  9513. }
  9514. #endif
  9515. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9516. break;
  9517. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9518. #if WASM_ENABLE_GC != 0
  9519. frame_reftype_map, available_reftype_map,
  9520. #endif
  9521. type,
  9522. #if WASM_ENABLE_GC != 0
  9523. ref_type,
  9524. #endif
  9525. error_buf, error_buf_size)) {
  9526. goto fail;
  9527. }
  9528. cell_num = wasm_value_type_cell_num(types[i]);
  9529. frame_ref -= cell_num;
  9530. available_stack_cell -= cell_num;
  9531. #if WASM_ENABLE_GC != 0
  9532. if (is_type_multi_byte) {
  9533. frame_reftype_map--;
  9534. available_reftype_map--;
  9535. reftype_map--;
  9536. }
  9537. #endif
  9538. }
  9539. #if WASM_ENABLE_FAST_INTERP != 0
  9540. emit_br_info(target_block, opcode == WASM_OP_BR);
  9541. #endif
  9542. ret = true;
  9543. cleanup_and_return:
  9544. fail:
  9545. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9546. wasm_runtime_free(frame_ref_buf);
  9547. #if WASM_ENABLE_GC != 0
  9548. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9549. wasm_runtime_free(frame_reftype_map_buf);
  9550. #endif
  9551. #if WASM_ENABLE_FAST_INTERP != 0
  9552. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9553. wasm_runtime_free(frame_offset_buf);
  9554. #endif
  9555. return ret;
  9556. }
  9557. static BranchBlock *
  9558. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9559. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9560. {
  9561. uint8 *p = *p_buf, *p_end = buf_end;
  9562. BranchBlock *frame_csp_tmp;
  9563. uint32 depth;
  9564. read_leb_uint32(p, p_end, depth);
  9565. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9566. error_buf_size)) {
  9567. goto fail;
  9568. }
  9569. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9570. *p_buf = p;
  9571. return frame_csp_tmp;
  9572. fail:
  9573. return NULL;
  9574. }
  9575. #if WASM_ENABLE_EXCE_HANDLING != 0
  9576. static BranchBlock *
  9577. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9578. uint8 *buf_end, char *error_buf,
  9579. uint32 error_buf_size)
  9580. {
  9581. uint8 *p = *p_buf, *p_end = buf_end;
  9582. BranchBlock *frame_csp_tmp;
  9583. uint32 depth;
  9584. read_leb_uint32(p, p_end, depth);
  9585. /*
  9586. * Note: "delegate 0" means the surrounding block, not the
  9587. * try-delegate block itself.
  9588. *
  9589. * Note: the caller hasn't popped the try-delegate frame yet.
  9590. */
  9591. bh_assert(loader_ctx->csp_num > 0);
  9592. if (loader_ctx->csp_num - 1 <= depth) {
  9593. #if WASM_ENABLE_SPEC_TEST == 0
  9594. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9595. #else
  9596. set_error_buf(error_buf, error_buf_size, "unknown label");
  9597. #endif
  9598. goto fail;
  9599. }
  9600. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9601. #if WASM_ENABLE_FAST_INTERP != 0
  9602. emit_br_info(frame_csp_tmp, false);
  9603. #endif
  9604. *p_buf = p;
  9605. return frame_csp_tmp;
  9606. fail:
  9607. return NULL;
  9608. }
  9609. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9610. static bool
  9611. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9612. char *error_buf, uint32 error_buf_size)
  9613. {
  9614. BlockType *block_type = &block->block_type;
  9615. uint8 *return_types = NULL;
  9616. uint32 return_count = 0;
  9617. int32 available_stack_cell, return_cell_num, i;
  9618. uint8 *frame_ref = NULL;
  9619. #if WASM_ENABLE_GC != 0
  9620. WASMRefTypeMap *frame_reftype_map;
  9621. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9622. WASMRefType *ref_type;
  9623. uint32 param_count, return_reftype_map_count = 0;
  9624. int32 available_reftype_map =
  9625. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9626. #endif
  9627. available_stack_cell =
  9628. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9629. #if WASM_ENABLE_GC == 0
  9630. return_count = block_type_get_result_types(block_type, &return_types);
  9631. #else
  9632. return_count = block_type_get_result_types(block_type, &return_types,
  9633. &return_reftype_maps,
  9634. &return_reftype_map_count);
  9635. param_count =
  9636. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9637. (void)param_count;
  9638. #endif
  9639. return_cell_num =
  9640. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9641. /* If the stack is in polymorphic state, just clear the stack
  9642. * and then re-push the values to make the stack top values
  9643. * match block type. */
  9644. if (block->is_stack_polymorphic) {
  9645. #if WASM_ENABLE_GC != 0
  9646. int32 j = return_reftype_map_count - 1;
  9647. #endif
  9648. for (i = (int32)return_count - 1; i >= 0; i--) {
  9649. #if WASM_ENABLE_GC != 0
  9650. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9651. bh_assert(return_reftype_maps[j].index == i + param_count);
  9652. bh_memcpy_s(
  9653. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9654. return_reftype_maps[j].ref_type,
  9655. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9656. j--;
  9657. }
  9658. #endif
  9659. #if WASM_ENABLE_FAST_INTERP != 0
  9660. POP_OFFSET_TYPE(return_types[i]);
  9661. #endif
  9662. POP_TYPE(return_types[i]);
  9663. }
  9664. /* Check stack is empty */
  9665. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9666. set_error_buf(
  9667. error_buf, error_buf_size,
  9668. "type mismatch: stack size does not match block type");
  9669. goto fail;
  9670. }
  9671. #if WASM_ENABLE_GC != 0
  9672. j = 0;
  9673. #endif
  9674. for (i = 0; i < (int32)return_count; i++) {
  9675. #if WASM_ENABLE_GC != 0
  9676. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9677. bh_assert(return_reftype_maps[j].index == i + param_count);
  9678. bh_memcpy_s(
  9679. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9680. return_reftype_maps[j].ref_type,
  9681. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9682. j++;
  9683. }
  9684. #endif
  9685. #if WASM_ENABLE_FAST_INTERP != 0
  9686. bool disable_emit = true;
  9687. int16 operand_offset = 0;
  9688. PUSH_OFFSET_TYPE(return_types[i]);
  9689. #endif
  9690. PUSH_TYPE(return_types[i]);
  9691. }
  9692. return true;
  9693. }
  9694. if (available_stack_cell != return_cell_num) {
  9695. #if WASM_ENABLE_EXCE_HANDLING != 0
  9696. /* testspec: this error message format is expected by try_catch.wast */
  9697. snprintf(
  9698. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9699. block->label_type == LABEL_TYPE_TRY
  9700. || (block->label_type == LABEL_TYPE_CATCH
  9701. && return_cell_num > 0)
  9702. ? "instruction"
  9703. : "block",
  9704. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9705. " but stack has ",
  9706. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9707. : "");
  9708. goto fail;
  9709. #else
  9710. set_error_buf(error_buf, error_buf_size,
  9711. "type mismatch: stack size does not match block type");
  9712. goto fail;
  9713. #endif
  9714. }
  9715. /* Check stack values match return types */
  9716. frame_ref = loader_ctx->frame_ref;
  9717. #if WASM_ENABLE_GC != 0
  9718. frame_reftype_map = loader_ctx->frame_reftype_map;
  9719. return_reftype_map =
  9720. return_reftype_map_count
  9721. ? return_reftype_maps + return_reftype_map_count - 1
  9722. : NULL;
  9723. #endif
  9724. for (i = (int32)return_count - 1; i >= 0; i--) {
  9725. uint8 type = return_types[i];
  9726. #if WASM_ENABLE_GC != 0
  9727. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9728. ref_type = NULL;
  9729. if (is_type_multi_byte) {
  9730. bh_assert(return_reftype_map);
  9731. ref_type = return_reftype_map->ref_type;
  9732. }
  9733. #endif
  9734. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9735. #if WASM_ENABLE_GC != 0
  9736. frame_reftype_map, available_reftype_map,
  9737. #endif
  9738. type,
  9739. #if WASM_ENABLE_GC != 0
  9740. ref_type,
  9741. #endif
  9742. error_buf, error_buf_size))
  9743. return false;
  9744. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9745. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9746. #if WASM_ENABLE_GC != 0
  9747. if (is_type_multi_byte) {
  9748. frame_reftype_map--;
  9749. available_reftype_map--;
  9750. return_reftype_map--;
  9751. }
  9752. #endif
  9753. }
  9754. return true;
  9755. fail:
  9756. return false;
  9757. }
  9758. #if WASM_ENABLE_FAST_INTERP != 0
  9759. /* Copy parameters to dynamic space.
  9760. * 1) POP original parameter out;
  9761. * 2) Push and copy original values to dynamic space.
  9762. * The copy instruction format:
  9763. * Part a: param count
  9764. * Part b: all param total cell num
  9765. * Part c: each param's cell_num, src offset and dst offset
  9766. * Part d: each param's src offset
  9767. * Part e: each param's dst offset
  9768. */
  9769. static bool
  9770. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9771. uint32 error_buf_size)
  9772. {
  9773. bool ret = false;
  9774. int16 *frame_offset = NULL;
  9775. uint8 *cells = NULL, cell;
  9776. int16 *src_offsets = NULL;
  9777. uint8 *emit_data = NULL;
  9778. uint32 i;
  9779. BranchBlock *block = loader_ctx->frame_csp - 1;
  9780. BlockType *block_type = &block->block_type;
  9781. WASMFuncType *wasm_type = block_type->u.type;
  9782. uint32 param_count = block_type->u.type->param_count;
  9783. int16 condition_offset = 0;
  9784. bool disable_emit = false;
  9785. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9786. int16 operand_offset = 0;
  9787. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9788. bh_assert(size > 0);
  9789. /* For if block, we also need copy the condition operand offset. */
  9790. if (is_if_block)
  9791. size += sizeof(*cells) + sizeof(*src_offsets);
  9792. /* Allocate memory for the emit data */
  9793. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9794. return false;
  9795. cells = emit_data;
  9796. src_offsets = (int16 *)(cells + param_count);
  9797. if (is_if_block)
  9798. condition_offset = *loader_ctx->frame_offset;
  9799. /* POP original parameter out */
  9800. for (i = 0; i < param_count; i++) {
  9801. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9802. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9803. }
  9804. frame_offset = loader_ctx->frame_offset;
  9805. /* Get each param's cell num and src offset */
  9806. for (i = 0; i < param_count; i++) {
  9807. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9808. cells[i] = cell;
  9809. src_offsets[i] = *frame_offset;
  9810. frame_offset += cell;
  9811. }
  9812. /* emit copy instruction */
  9813. emit_label(EXT_OP_COPY_STACK_VALUES);
  9814. /* Part a) */
  9815. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9816. /* Part b) */
  9817. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9818. : wasm_type->param_cell_num);
  9819. /* Part c) */
  9820. for (i = 0; i < param_count; i++)
  9821. emit_byte(loader_ctx, cells[i]);
  9822. if (is_if_block)
  9823. emit_byte(loader_ctx, 1);
  9824. /* Part d) */
  9825. for (i = 0; i < param_count; i++)
  9826. emit_operand(loader_ctx, src_offsets[i]);
  9827. if (is_if_block)
  9828. emit_operand(loader_ctx, condition_offset);
  9829. /* Since the start offset to save the block's params and
  9830. * the start offset to save the block's results may be
  9831. * different, we remember the dynamic offset for loop block
  9832. * so that we can use it to copy the stack operands to the
  9833. * loop block's params in wasm_loader_emit_br_info. */
  9834. if (block->label_type == LABEL_TYPE_LOOP)
  9835. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9836. /* Part e) */
  9837. /* Push to dynamic space. The push will emit the dst offset. */
  9838. for (i = 0; i < param_count; i++)
  9839. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9840. if (is_if_block)
  9841. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9842. ret = true;
  9843. fail:
  9844. /* Free the emit data */
  9845. wasm_runtime_free(emit_data);
  9846. return ret;
  9847. }
  9848. #endif
  9849. #if WASM_ENABLE_GC == 0
  9850. #define RESET_REFTYPE_MAP_STACK() (void)0
  9851. #else
  9852. #define RESET_REFTYPE_MAP_STACK() \
  9853. do { \
  9854. loader_ctx->reftype_map_num = \
  9855. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9856. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9857. + loader_ctx->reftype_map_num; \
  9858. } while (0)
  9859. #endif
  9860. /* reset the stack to the state of before entering the last block */
  9861. #if WASM_ENABLE_FAST_INTERP != 0
  9862. #define RESET_STACK() \
  9863. do { \
  9864. loader_ctx->stack_cell_num = \
  9865. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9866. loader_ctx->frame_ref = \
  9867. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9868. loader_ctx->frame_offset = \
  9869. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9870. RESET_REFTYPE_MAP_STACK(); \
  9871. } while (0)
  9872. #else
  9873. #define RESET_STACK() \
  9874. do { \
  9875. loader_ctx->stack_cell_num = \
  9876. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9877. loader_ctx->frame_ref = \
  9878. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9879. RESET_REFTYPE_MAP_STACK(); \
  9880. } while (0)
  9881. #endif
  9882. /* set current block's stack polymorphic state */
  9883. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9884. do { \
  9885. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9886. _cur_block->is_stack_polymorphic = flag; \
  9887. } while (0)
  9888. #define BLOCK_HAS_PARAM(block_type) \
  9889. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9890. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9891. do { \
  9892. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9893. error_buf_size))) { \
  9894. goto fail; \
  9895. } \
  9896. } while (0)
  9897. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9898. static bool
  9899. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9900. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9901. uint32 error_buf_size)
  9902. {
  9903. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9904. return false;
  9905. }
  9906. if (table_idx < module->import_table_count) {
  9907. if (p_elem_type)
  9908. *p_elem_type =
  9909. module->import_tables[table_idx].u.table.table_type.elem_type;
  9910. #if WASM_ENABLE_GC != 0
  9911. if (p_ref_type)
  9912. *((WASMRefType **)p_ref_type) =
  9913. module->import_tables[table_idx]
  9914. .u.table.table_type.elem_ref_type;
  9915. #endif
  9916. }
  9917. else {
  9918. if (p_elem_type)
  9919. *p_elem_type =
  9920. module->tables[table_idx - module->import_table_count]
  9921. .table_type.elem_type;
  9922. #if WASM_ENABLE_GC != 0
  9923. if (p_ref_type)
  9924. *((WASMRefType **)p_ref_type) =
  9925. module->tables[table_idx - module->import_table_count]
  9926. .table_type.elem_ref_type;
  9927. #endif
  9928. }
  9929. return true;
  9930. }
  9931. static bool
  9932. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9933. uint8 *p_elem_type, void **p_elem_ref_type,
  9934. char *error_buf, uint32 error_buf_size)
  9935. {
  9936. if (table_seg_idx >= module->table_seg_count) {
  9937. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9938. table_seg_idx);
  9939. return false;
  9940. }
  9941. if (p_elem_type) {
  9942. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9943. }
  9944. #if WASM_ENABLE_GC != 0
  9945. if (p_elem_ref_type)
  9946. *((WASMRefType **)p_elem_ref_type) =
  9947. module->table_segments[table_seg_idx].elem_ref_type;
  9948. #endif
  9949. return true;
  9950. }
  9951. #endif
  9952. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9953. const uint8 *
  9954. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9955. uint32 *len)
  9956. {
  9957. WASMCustomSection *section = module->custom_section_list;
  9958. while (section) {
  9959. if ((section->name_len == strlen(name))
  9960. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9961. if (len) {
  9962. *len = section->content_len;
  9963. }
  9964. return section->content_addr;
  9965. }
  9966. section = section->next;
  9967. }
  9968. return NULL;
  9969. }
  9970. #endif
  9971. #if 0
  9972. #define HANDLE_OPCODE(opcode) #opcode
  9973. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9974. #undef HANDLE_OPCODE
  9975. #endif
  9976. #if WASM_ENABLE_FAST_INTERP == 0
  9977. #define pb_read_leb_uint32 read_leb_uint32
  9978. #define pb_read_leb_int32 read_leb_int32
  9979. #define pb_read_leb_int64 read_leb_int64
  9980. #define pb_read_leb_memarg read_leb_memarg
  9981. #define pb_read_leb_mem_offset read_leb_mem_offset
  9982. #else
  9983. /* Read leb without malformed format check */
  9984. static uint64
  9985. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  9986. {
  9987. uint8 *buf = *p_buf;
  9988. uint64 result = 0, byte = 0;
  9989. uint32 shift = 0;
  9990. do {
  9991. byte = *buf++;
  9992. result |= ((byte & 0x7f) << shift);
  9993. shift += 7;
  9994. } while (byte & 0x80);
  9995. if (sign && (shift < maxbits) && (byte & 0x40)) {
  9996. /* Sign extend */
  9997. result |= (~((uint64)0)) << shift;
  9998. }
  9999. *p_buf = buf;
  10000. return result;
  10001. }
  10002. #define pb_read_leb_uint32(p, p_end, res) \
  10003. do { \
  10004. if (!loader_ctx->p_code_compiled) \
  10005. /* Enable format check in the first scan */ \
  10006. read_leb_uint32(p, p_end, res); \
  10007. else \
  10008. /* Disable format check in the second scan */ \
  10009. res = (uint32)read_leb_quick(&p, 32, false); \
  10010. } while (0)
  10011. #define pb_read_leb_int32(p, p_end, res) \
  10012. do { \
  10013. if (!loader_ctx->p_code_compiled) \
  10014. /* Enable format check in the first scan */ \
  10015. read_leb_int32(p, p_end, res); \
  10016. else \
  10017. /* Disable format check in the second scan */ \
  10018. res = (int32)read_leb_quick(&p, 32, true); \
  10019. } while (0)
  10020. #define pb_read_leb_int64(p, p_end, res) \
  10021. do { \
  10022. if (!loader_ctx->p_code_compiled) \
  10023. /* Enable format check in the first scan */ \
  10024. read_leb_int64(p, p_end, res); \
  10025. else \
  10026. /* Disable format check in the second scan */ \
  10027. res = (int64)read_leb_quick(&p, 64, true); \
  10028. } while (0)
  10029. #if WASM_ENABLE_MULTI_MEMORY != 0
  10030. #define pb_read_leb_memarg read_leb_memarg
  10031. #else
  10032. #define pb_read_leb_memarg pb_read_leb_uint32
  10033. #endif
  10034. #if WASM_ENABLE_MEMORY64 != 0
  10035. #define pb_read_leb_mem_offset read_leb_mem_offset
  10036. #else
  10037. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10038. #endif
  10039. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10040. static bool
  10041. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10042. uint32 cur_func_idx, char *error_buf,
  10043. uint32 error_buf_size)
  10044. {
  10045. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10046. uint32 param_count, local_count, global_count;
  10047. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10048. table_elem_idx_type;
  10049. BlockType func_block_type;
  10050. uint16 *local_offsets, local_offset;
  10051. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10052. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10053. mem_offset_t mem_offset;
  10054. int32 i32_const = 0;
  10055. int64 i64_const;
  10056. uint8 opcode;
  10057. bool return_value = false;
  10058. WASMLoaderContext *loader_ctx;
  10059. BranchBlock *frame_csp_tmp;
  10060. #if WASM_ENABLE_GC != 0
  10061. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10062. uint32 param_reftype_map_count, local_reftype_map_count;
  10063. int32 heap_type;
  10064. WASMRefType wasm_ref_type = { 0 };
  10065. bool need_ref_type_map;
  10066. #endif
  10067. #if WASM_ENABLE_FAST_INTERP != 0
  10068. int16 operand_offset = 0;
  10069. uint8 last_op = 0;
  10070. bool disable_emit, preserve_local = false, if_condition_available = true;
  10071. float32 f32_const;
  10072. float64 f64_const;
  10073. /*
  10074. * It means that the fast interpreter detected an exception while preparing,
  10075. * typically near the block opcode, but it did not immediately trigger
  10076. * the exception. The loader should be capable of identifying it near
  10077. * the end opcode and then raising the exception.
  10078. */
  10079. bool pending_exception = false;
  10080. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10081. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10082. #endif
  10083. #if WASM_ENABLE_MEMORY64 != 0
  10084. bool is_memory64 = has_module_memory64(module);
  10085. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10086. #else
  10087. mem_offset_type = VALUE_TYPE_I32;
  10088. table_elem_idx_type = VALUE_TYPE_I32;
  10089. #endif
  10090. uint32 memidx;
  10091. global_count = module->import_global_count + module->global_count;
  10092. param_count = func->func_type->param_count;
  10093. param_types = func->func_type->types;
  10094. func_block_type.is_value_type = false;
  10095. func_block_type.u.type = func->func_type;
  10096. local_count = func->local_count;
  10097. local_types = func->local_types;
  10098. local_offsets = func->local_offsets;
  10099. #if WASM_ENABLE_GC != 0
  10100. param_reftype_maps = func->func_type->ref_type_maps;
  10101. param_reftype_map_count = func->func_type->ref_type_map_count;
  10102. local_reftype_maps = func->local_ref_type_maps;
  10103. local_reftype_map_count = func->local_ref_type_map_count;
  10104. #endif
  10105. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10106. goto fail;
  10107. }
  10108. #if WASM_ENABLE_GC != 0
  10109. loader_ctx->module = module;
  10110. loader_ctx->ref_type_set = module->ref_type_set;
  10111. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10112. #endif
  10113. #if WASM_ENABLE_FAST_INTERP != 0
  10114. /* For the first traverse, the initial value of preserved_local_offset has
  10115. * not been determined, we use the INT16_MAX to represent that a slot has
  10116. * been copied to preserve space. For second traverse, this field will be
  10117. * set to the appropriate value in wasm_loader_ctx_reinit.
  10118. * This is for Issue #1230,
  10119. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10120. * drop opcodes need to know which slots are preserved, so those slots will
  10121. * not be treated as dynamically allocated slots */
  10122. loader_ctx->preserved_local_offset = INT16_MAX;
  10123. re_scan:
  10124. if (loader_ctx->code_compiled_size > 0) {
  10125. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10126. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10127. goto fail;
  10128. }
  10129. p = func->code;
  10130. func->code_compiled = loader_ctx->p_code_compiled;
  10131. func->code_compiled_size = loader_ctx->code_compiled_size;
  10132. if (loader_ctx->i64_const_num > 0) {
  10133. int64 *i64_consts_old = loader_ctx->i64_consts;
  10134. /* Sort the i64 consts */
  10135. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10136. cmp_i64_const);
  10137. /* Remove the duplicated i64 consts */
  10138. uint32 k = 1;
  10139. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10140. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10141. i64_consts_old[k++] = i64_consts_old[i];
  10142. }
  10143. }
  10144. if (k < loader_ctx->i64_const_num) {
  10145. int64 *i64_consts_new;
  10146. /* Try to reallocate memory with a smaller size */
  10147. if ((i64_consts_new =
  10148. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10149. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10150. i64_consts_old, (uint32)sizeof(int64) * k);
  10151. /* Free the old memory */
  10152. wasm_runtime_free(i64_consts_old);
  10153. loader_ctx->i64_consts = i64_consts_new;
  10154. loader_ctx->i64_const_max_num = k;
  10155. }
  10156. loader_ctx->i64_const_num = k;
  10157. }
  10158. }
  10159. if (loader_ctx->v128_const_num > 0) {
  10160. V128 *v128_consts_old = loader_ctx->v128_consts;
  10161. /* Sort the v128 consts */
  10162. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10163. cmp_v128_const);
  10164. /* Remove the duplicated v128 consts */
  10165. uint32 k = 1;
  10166. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10167. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10168. sizeof(V128))
  10169. == 0)) {
  10170. v128_consts_old[k++] = v128_consts_old[i];
  10171. }
  10172. }
  10173. if (k < loader_ctx->v128_const_num) {
  10174. V128 *v128_consts_new;
  10175. /* Try to reallocate memory with a smaller size */
  10176. if ((v128_consts_new =
  10177. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10178. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10179. v128_consts_old, (uint32)sizeof(V128) * k);
  10180. /* Free the old memory */
  10181. wasm_runtime_free(v128_consts_old);
  10182. loader_ctx->v128_consts = v128_consts_new;
  10183. loader_ctx->v128_const_max_num = k;
  10184. }
  10185. loader_ctx->v128_const_num = k;
  10186. }
  10187. }
  10188. if (loader_ctx->i32_const_num > 0) {
  10189. int32 *i32_consts_old = loader_ctx->i32_consts;
  10190. /* Sort the i32 consts */
  10191. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10192. cmp_i32_const);
  10193. /* Remove the duplicated i32 consts */
  10194. uint32 k = 1;
  10195. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10196. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10197. i32_consts_old[k++] = i32_consts_old[i];
  10198. }
  10199. }
  10200. if (k < loader_ctx->i32_const_num) {
  10201. int32 *i32_consts_new;
  10202. /* Try to reallocate memory with a smaller size */
  10203. if ((i32_consts_new =
  10204. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10205. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10206. i32_consts_old, (uint32)sizeof(int32) * k);
  10207. /* Free the old memory */
  10208. wasm_runtime_free(i32_consts_old);
  10209. loader_ctx->i32_consts = i32_consts_new;
  10210. loader_ctx->i32_const_max_num = k;
  10211. }
  10212. loader_ctx->i32_const_num = k;
  10213. }
  10214. }
  10215. }
  10216. #endif
  10217. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10218. while (p < p_end) {
  10219. opcode = *p++;
  10220. #if WASM_ENABLE_FAST_INTERP != 0
  10221. p_org = p;
  10222. disable_emit = false;
  10223. emit_label(opcode);
  10224. #endif
  10225. switch (opcode) {
  10226. case WASM_OP_UNREACHABLE:
  10227. RESET_STACK();
  10228. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10229. break;
  10230. case WASM_OP_NOP:
  10231. #if WASM_ENABLE_FAST_INTERP != 0
  10232. skip_label();
  10233. #endif
  10234. break;
  10235. case WASM_OP_IF:
  10236. {
  10237. #if WASM_ENABLE_FAST_INTERP != 0
  10238. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10239. int32 available_stack_cell =
  10240. (int32)(loader_ctx->stack_cell_num
  10241. - parent_block->stack_cell_num);
  10242. if (available_stack_cell <= 0
  10243. && parent_block->is_stack_polymorphic)
  10244. if_condition_available = false;
  10245. else
  10246. if_condition_available = true;
  10247. PRESERVE_LOCAL_FOR_BLOCK();
  10248. #endif
  10249. #if WASM_ENABLE_GC == 0
  10250. POP_I32();
  10251. #endif
  10252. goto handle_op_block_and_loop;
  10253. }
  10254. case WASM_OP_BLOCK:
  10255. case WASM_OP_LOOP:
  10256. #if WASM_ENABLE_EXCE_HANDLING != 0
  10257. case WASM_OP_TRY:
  10258. if (opcode == WASM_OP_TRY) {
  10259. /*
  10260. * keep track of exception handlers to account for
  10261. * memory allocation
  10262. */
  10263. func->exception_handler_count++;
  10264. /*
  10265. * try is a block
  10266. * do nothing special, but execution continues to
  10267. * to handle_op_block_and_loop,
  10268. * and that be pushes the csp
  10269. */
  10270. }
  10271. #endif
  10272. #if WASM_ENABLE_FAST_INTERP != 0
  10273. PRESERVE_LOCAL_FOR_BLOCK();
  10274. #endif
  10275. handle_op_block_and_loop:
  10276. {
  10277. uint8 value_type;
  10278. BlockType block_type;
  10279. #if WASM_ENABLE_FAST_INTERP != 0
  10280. uint32 available_params = 0;
  10281. #endif
  10282. CHECK_BUF(p, p_end, 1);
  10283. value_type = read_uint8(p);
  10284. if (is_byte_a_type(value_type)) {
  10285. /* If the first byte is one of these special values:
  10286. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10287. * the single return value. */
  10288. block_type.is_value_type = true;
  10289. block_type.u.value_type.type = value_type;
  10290. #if WASM_ENABLE_WAMR_COMPILER != 0
  10291. if (value_type == VALUE_TYPE_V128)
  10292. module->is_simd_used = true;
  10293. else if (value_type == VALUE_TYPE_FUNCREF
  10294. || value_type == VALUE_TYPE_EXTERNREF)
  10295. module->is_ref_types_used = true;
  10296. #endif
  10297. #if WASM_ENABLE_GC != 0
  10298. if (value_type != VALUE_TYPE_VOID) {
  10299. p_org = p;
  10300. p--;
  10301. if (!resolve_value_type((const uint8 **)&p, p_end,
  10302. module, module->type_count,
  10303. &need_ref_type_map,
  10304. &wasm_ref_type, false,
  10305. error_buf, error_buf_size)) {
  10306. goto fail;
  10307. }
  10308. if (need_ref_type_map) {
  10309. block_type.u.value_type.ref_type_map.index = 0;
  10310. if (!(block_type.u.value_type.ref_type_map
  10311. .ref_type = reftype_set_insert(
  10312. module->ref_type_set, &wasm_ref_type,
  10313. error_buf, error_buf_size))) {
  10314. goto fail;
  10315. }
  10316. }
  10317. /* Set again as the type might be changed, e.g.
  10318. (ref null any) to anyref */
  10319. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10320. #if WASM_ENABLE_FAST_INTERP == 0
  10321. while (p_org < p) {
  10322. #if WASM_ENABLE_DEBUG_INTERP != 0
  10323. if (!record_fast_op(module, p_org, *p_org,
  10324. error_buf, error_buf_size)) {
  10325. goto fail;
  10326. }
  10327. #endif
  10328. /* Ignore extra bytes for interpreter */
  10329. *p_org++ = WASM_OP_NOP;
  10330. }
  10331. #endif
  10332. }
  10333. #endif /* end of WASM_ENABLE_GC != 0 */
  10334. }
  10335. else {
  10336. int32 type_index;
  10337. /* Resolve the leb128 encoded type index as block type */
  10338. p--;
  10339. p_org = p - 1;
  10340. pb_read_leb_int32(p, p_end, type_index);
  10341. if ((uint32)type_index >= module->type_count) {
  10342. set_error_buf(error_buf, error_buf_size,
  10343. "unknown type");
  10344. goto fail;
  10345. }
  10346. block_type.is_value_type = false;
  10347. block_type.u.type =
  10348. (WASMFuncType *)module->types[type_index];
  10349. #if WASM_ENABLE_FAST_INTERP == 0
  10350. /* If block use type index as block type, change the opcode
  10351. * to new extended opcode so that interpreter can resolve
  10352. * the block quickly.
  10353. */
  10354. #if WASM_ENABLE_DEBUG_INTERP != 0
  10355. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10356. error_buf_size)) {
  10357. goto fail;
  10358. }
  10359. #endif
  10360. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10361. #endif
  10362. }
  10363. #if WASM_ENABLE_GC != 0
  10364. if (opcode == WASM_OP_IF) {
  10365. POP_I32();
  10366. }
  10367. #endif
  10368. /* Pop block parameters from stack */
  10369. if (BLOCK_HAS_PARAM(block_type)) {
  10370. WASMFuncType *wasm_type = block_type.u.type;
  10371. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10372. #if WASM_ENABLE_FAST_INTERP != 0
  10373. uint32 cell_num;
  10374. available_params = block_type.u.type->param_count;
  10375. #endif
  10376. for (i = 0; i < block_type.u.type->param_count; i++) {
  10377. int32 available_stack_cell =
  10378. (int32)(loader_ctx->stack_cell_num
  10379. - cur_block->stack_cell_num);
  10380. if (available_stack_cell <= 0
  10381. && cur_block->is_stack_polymorphic) {
  10382. #if WASM_ENABLE_FAST_INTERP != 0
  10383. available_params = i;
  10384. #endif
  10385. break;
  10386. }
  10387. POP_TYPE(
  10388. wasm_type->types[wasm_type->param_count - i - 1]);
  10389. #if WASM_ENABLE_FAST_INTERP != 0
  10390. /* decrease the frame_offset pointer accordingly to keep
  10391. * consistent with frame_ref stack */
  10392. cell_num = wasm_value_type_cell_num(
  10393. wasm_type->types[wasm_type->param_count - i - 1]);
  10394. loader_ctx->frame_offset -= cell_num;
  10395. if (loader_ctx->frame_offset
  10396. < loader_ctx->frame_offset_bottom) {
  10397. LOG_DEBUG(
  10398. "frame_offset underflow, roll back and "
  10399. "let following stack checker report it\n");
  10400. loader_ctx->frame_offset += cell_num;
  10401. pending_exception = true;
  10402. break;
  10403. }
  10404. #endif
  10405. }
  10406. }
  10407. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10408. block_type, p);
  10409. /* Pass parameters to block */
  10410. if (BLOCK_HAS_PARAM(block_type)) {
  10411. WASMFuncType *func_type = block_type.u.type;
  10412. #if WASM_ENABLE_GC != 0
  10413. WASMRefType *ref_type;
  10414. uint32 j = 0;
  10415. #endif
  10416. for (i = 0; i < func_type->param_count; i++) {
  10417. #if WASM_ENABLE_FAST_INTERP != 0
  10418. uint32 cell_num =
  10419. wasm_value_type_cell_num(func_type->types[i]);
  10420. if (i >= available_params) {
  10421. /* make sure enough space */
  10422. if (loader_ctx->p_code_compiled == NULL) {
  10423. loader_ctx->frame_offset += cell_num;
  10424. if (!check_offset_push(loader_ctx, error_buf,
  10425. error_buf_size))
  10426. goto fail;
  10427. /* for following dummy value assignment */
  10428. loader_ctx->frame_offset -= cell_num;
  10429. }
  10430. /* If there isn't enough data on stack, push a dummy
  10431. * offset to keep the stack consistent with
  10432. * frame_ref.
  10433. * Since the stack is already in polymorphic state,
  10434. * the opcode will not be executed, so the dummy
  10435. * offset won't cause any error */
  10436. for (uint32 n = 0; n < cell_num; n++) {
  10437. *loader_ctx->frame_offset++ = 0;
  10438. }
  10439. }
  10440. else {
  10441. loader_ctx->frame_offset += cell_num;
  10442. }
  10443. #endif
  10444. #if WASM_ENABLE_GC != 0
  10445. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10446. bh_assert(func_type->ref_type_maps[j].index == i);
  10447. ref_type = func_type->ref_type_maps[j].ref_type;
  10448. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10449. ref_type,
  10450. wasm_reftype_struct_size(ref_type));
  10451. j++;
  10452. }
  10453. #endif
  10454. PUSH_TYPE(func_type->types[i]);
  10455. }
  10456. }
  10457. #if WASM_ENABLE_FAST_INTERP != 0
  10458. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10459. skip_label();
  10460. if (BLOCK_HAS_PARAM(block_type)) {
  10461. /* Make sure params are in dynamic space */
  10462. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10463. error_buf_size))
  10464. goto fail;
  10465. }
  10466. if (opcode == WASM_OP_LOOP) {
  10467. (loader_ctx->frame_csp - 1)->code_compiled =
  10468. loader_ctx->p_code_compiled;
  10469. }
  10470. }
  10471. #if WASM_ENABLE_EXCE_HANDLING != 0
  10472. else if (opcode == WASM_OP_TRY) {
  10473. skip_label();
  10474. }
  10475. #endif
  10476. else if (opcode == WASM_OP_IF) {
  10477. BranchBlock *block = loader_ctx->frame_csp - 1;
  10478. /* If block has parameters, we should make sure they are in
  10479. * dynamic space. Otherwise, when else branch is missing,
  10480. * the later opcode may consume incorrect operand offset.
  10481. * Spec case:
  10482. * (func (export "params-id") (param i32) (result i32)
  10483. * (i32.const 1)
  10484. * (i32.const 2)
  10485. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10486. * (then)) (i32.add)
  10487. * )
  10488. *
  10489. * So we should emit a copy instruction before the if.
  10490. *
  10491. * And we also need to save the parameter offsets and
  10492. * recover them before entering else branch.
  10493. *
  10494. */
  10495. if (BLOCK_HAS_PARAM(block_type)) {
  10496. uint64 size;
  10497. /* In polymorphic state, there may be no if condition on
  10498. * the stack, so the offset may not emitted */
  10499. if (if_condition_available) {
  10500. /* skip the if condition operand offset */
  10501. wasm_loader_emit_backspace(loader_ctx,
  10502. sizeof(int16));
  10503. }
  10504. /* skip the if label */
  10505. skip_label();
  10506. /* Emit a copy instruction */
  10507. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10508. error_buf_size))
  10509. goto fail;
  10510. /* Emit the if instruction */
  10511. emit_label(opcode);
  10512. /* Emit the new condition operand offset */
  10513. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10514. /* Save top param_count values of frame_offset stack, so
  10515. * that we can recover it before executing else branch
  10516. */
  10517. size = sizeof(int16)
  10518. * (uint64)block_type.u.type->param_cell_num;
  10519. if (!(block->param_frame_offsets = loader_malloc(
  10520. size, error_buf, error_buf_size)))
  10521. goto fail;
  10522. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10523. loader_ctx->frame_offset
  10524. - size / sizeof(int16),
  10525. (uint32)size);
  10526. }
  10527. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10528. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10529. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10530. }
  10531. #endif
  10532. break;
  10533. }
  10534. #if WASM_ENABLE_EXCE_HANDLING != 0
  10535. case WASM_OP_THROW:
  10536. {
  10537. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10538. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10539. uint8 label_type = cur_block->label_type;
  10540. uint32 tag_index = 0;
  10541. pb_read_leb_int32(p, p_end, tag_index);
  10542. /* check validity of tag_index against module->tag_count */
  10543. /* check tag index is within the tag index space */
  10544. if (tag_index >= module->import_tag_count + module->tag_count) {
  10545. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10546. tag_index);
  10547. goto fail;
  10548. }
  10549. /* the tag_type is stored in either the WASMTag (section tags)
  10550. * or WASMTagImport (import tag) */
  10551. WASMFuncType *tag_type = NULL;
  10552. if (tag_index < module->import_tag_count) {
  10553. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10554. }
  10555. else {
  10556. tag_type =
  10557. module->tags[tag_index - module->import_tag_count]
  10558. ->tag_type;
  10559. }
  10560. if (tag_type->result_count != 0) {
  10561. set_error_buf(error_buf, error_buf_size,
  10562. "tag type signature does not return void");
  10563. goto fail;
  10564. }
  10565. int32 available_stack_cell =
  10566. (int32)(loader_ctx->stack_cell_num
  10567. - cur_block->stack_cell_num);
  10568. int32 tti;
  10569. /* Check stack values match return types by comparing tag param
  10570. * types with stack cells */
  10571. uint8 *frame_ref = loader_ctx->frame_ref;
  10572. #if WASM_ENABLE_GC != 0
  10573. WASMRefTypeMap *frame_reftype_map =
  10574. loader_ctx->frame_reftype_map;
  10575. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10576. /* Temporarily set these values since they may be used in
  10577. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10578. param_reftype_maps = tag_type->ref_type_maps;
  10579. /* For tag_type function, it shouldn't have result_count = 0 */
  10580. param_reftype_map_count = tag_type->ref_type_map_count;
  10581. param_count = tag_type->param_count;
  10582. #endif
  10583. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10584. #if WASM_ENABLE_GC != 0
  10585. local_type = tag_type->types[tti];
  10586. local_idx = tti;
  10587. /* Get the wasm_ref_type if the local_type is multibyte
  10588. type */
  10589. GET_LOCAL_REFTYPE();
  10590. #endif
  10591. if (!check_stack_top_values(
  10592. loader_ctx, frame_ref, available_stack_cell,
  10593. #if WASM_ENABLE_GC != 0
  10594. frame_reftype_map, frame_reftype_map_num,
  10595. #endif
  10596. tag_type->types[tti],
  10597. #if WASM_ENABLE_GC != 0
  10598. &wasm_ref_type,
  10599. #endif
  10600. error_buf, error_buf_size)) {
  10601. snprintf(error_buf, error_buf_size,
  10602. "type mismatch: instruction requires [%s] but "
  10603. "stack has [%s]",
  10604. tag_type->param_count > 0
  10605. ? type2str(tag_type->types[tti])
  10606. : "",
  10607. available_stack_cell > 0
  10608. ? type2str(*(loader_ctx->frame_ref - 1))
  10609. : "");
  10610. goto fail;
  10611. }
  10612. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10613. available_stack_cell -=
  10614. wasm_value_type_cell_num(tag_type->types[tti]);
  10615. }
  10616. #if WASM_ENABLE_GC != 0
  10617. /* Restore the values */
  10618. param_reftype_maps = func->func_type->ref_type_maps;
  10619. param_reftype_map_count = func->func_type->ref_type_map_count;
  10620. param_count = func->func_type->param_count;
  10621. #endif
  10622. /* throw is stack polymorphic */
  10623. (void)label_type;
  10624. RESET_STACK();
  10625. break;
  10626. }
  10627. case WASM_OP_RETHROW:
  10628. {
  10629. /* must be done before checking branch block */
  10630. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10631. /* check the target catching block: LABEL_TYPE_CATCH */
  10632. if (!(frame_csp_tmp =
  10633. check_branch_block(loader_ctx, &p, p_end, opcode,
  10634. error_buf, error_buf_size)))
  10635. goto fail;
  10636. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10637. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10638. /* trap according to spectest (rethrow.wast) */
  10639. set_error_buf(error_buf, error_buf_size,
  10640. "invalid rethrow label");
  10641. goto fail;
  10642. }
  10643. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10644. uint8 label_type = cur_block->label_type;
  10645. (void)label_type;
  10646. /* rethrow is stack polymorphic */
  10647. RESET_STACK();
  10648. break;
  10649. }
  10650. case WASM_OP_DELEGATE:
  10651. {
  10652. /* check target block is valid */
  10653. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10654. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10655. goto fail;
  10656. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10657. uint8 label_type = cur_block->label_type;
  10658. (void)label_type;
  10659. /* DELEGATE ends the block */
  10660. POP_CSP();
  10661. break;
  10662. }
  10663. case WASM_OP_CATCH:
  10664. {
  10665. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10666. uint8 label_type = cur_block->label_type;
  10667. uint32 tag_index = 0;
  10668. pb_read_leb_int32(p, p_end, tag_index);
  10669. /* check validity of tag_index against module->tag_count */
  10670. /* check tag index is within the tag index space */
  10671. if (tag_index >= module->import_tag_count + module->tag_count) {
  10672. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10673. __FUNCTION__);
  10674. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10675. goto fail;
  10676. }
  10677. /* the tag_type is stored in either the WASMTag (section tags)
  10678. * or WASMTagImport (import tag) */
  10679. WASMFuncType *func_type = NULL;
  10680. if (tag_index < module->import_tag_count) {
  10681. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10682. }
  10683. else {
  10684. func_type =
  10685. module->tags[tag_index - module->import_tag_count]
  10686. ->tag_type;
  10687. }
  10688. if (func_type->result_count != 0) {
  10689. set_error_buf(error_buf, error_buf_size,
  10690. "tag type signature does not return void");
  10691. goto fail;
  10692. }
  10693. /* check validity of current label (expect LABEL_TYPE_TRY or
  10694. * LABEL_TYPE_CATCH) */
  10695. if ((LABEL_TYPE_CATCH != label_type)
  10696. && (LABEL_TYPE_TRY != label_type)) {
  10697. set_error_buf(error_buf, error_buf_size,
  10698. "Unexpected block sequence encountered.");
  10699. goto fail;
  10700. }
  10701. /*
  10702. * replace frame_csp by LABEL_TYPE_CATCH
  10703. */
  10704. cur_block->label_type = LABEL_TYPE_CATCH;
  10705. /* RESET_STACK removes the values pushed in TRY or previous
  10706. * CATCH Blocks */
  10707. RESET_STACK();
  10708. #if WASM_ENABLE_GC != 0
  10709. WASMRefType *ref_type;
  10710. uint32 j = 0;
  10711. #endif
  10712. /* push types on the stack according to caught type */
  10713. for (i = 0; i < func_type->param_count; i++) {
  10714. #if WASM_ENABLE_GC != 0
  10715. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10716. bh_assert(func_type->ref_type_maps[j].index == i);
  10717. ref_type = func_type->ref_type_maps[j].ref_type;
  10718. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10719. ref_type,
  10720. wasm_reftype_struct_size(ref_type));
  10721. j++;
  10722. }
  10723. #endif
  10724. PUSH_TYPE(func_type->types[i]);
  10725. }
  10726. break;
  10727. }
  10728. case WASM_OP_CATCH_ALL:
  10729. {
  10730. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10731. /* expecting a TRY or CATCH, anything else will be considered an
  10732. * error */
  10733. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10734. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10735. set_error_buf(error_buf, error_buf_size,
  10736. "Unexpected block sequence encountered.");
  10737. goto fail;
  10738. }
  10739. /* no immediates */
  10740. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10741. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10742. /* RESET_STACK removes the values pushed in TRY or previous
  10743. * CATCH Blocks */
  10744. RESET_STACK();
  10745. /* catch_all has no tagtype and therefore no parameters */
  10746. break;
  10747. }
  10748. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10749. case WASM_OP_ELSE:
  10750. handle_op_else:
  10751. {
  10752. BranchBlock *block = NULL;
  10753. BlockType block_type;
  10754. if (loader_ctx->csp_num < 2
  10755. /* the matched if isn't found */
  10756. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10757. /* duplicated else is found */
  10758. || (loader_ctx->frame_csp - 1)->else_addr) {
  10759. set_error_buf(
  10760. error_buf, error_buf_size,
  10761. "opcode else found without matched opcode if");
  10762. goto fail;
  10763. }
  10764. block = loader_ctx->frame_csp - 1;
  10765. /* check whether if branch's stack matches its result type */
  10766. if (!check_block_stack(loader_ctx, block, error_buf,
  10767. error_buf_size))
  10768. goto fail;
  10769. block->else_addr = p - 1;
  10770. block_type = block->block_type;
  10771. #if WASM_ENABLE_GC != 0
  10772. if (!wasm_loader_init_local_use_masks(
  10773. loader_ctx, local_count, error_buf, error_buf_size)) {
  10774. goto fail;
  10775. }
  10776. #endif
  10777. #if WASM_ENABLE_FAST_INTERP != 0
  10778. /* if the result of if branch is in local or const area, add a
  10779. * copy op */
  10780. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10781. error_buf, error_buf_size)) {
  10782. goto fail;
  10783. }
  10784. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10785. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10786. #endif
  10787. RESET_STACK();
  10788. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10789. /* Pass parameters to if-false branch */
  10790. if (BLOCK_HAS_PARAM(block_type)) {
  10791. for (i = 0; i < block_type.u.type->param_count; i++)
  10792. PUSH_TYPE(block_type.u.type->types[i]);
  10793. }
  10794. #if WASM_ENABLE_FAST_INTERP != 0
  10795. /* Recover top param_count values of frame_offset stack */
  10796. if (BLOCK_HAS_PARAM((block_type))) {
  10797. uint32 size;
  10798. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10799. bh_memcpy_s(loader_ctx->frame_offset, size,
  10800. block->param_frame_offsets, size);
  10801. loader_ctx->frame_offset += (size / sizeof(int16));
  10802. }
  10803. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10804. #endif
  10805. break;
  10806. }
  10807. case WASM_OP_END:
  10808. {
  10809. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10810. /* check whether block stack matches its result type */
  10811. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10812. error_buf_size))
  10813. goto fail;
  10814. /* if there is no else branch, make a virtual else opcode for
  10815. easier integrity check and to copy the correct results to
  10816. the block return address for fast-interp mode:
  10817. change if block from `if ... end` to `if ... else end` */
  10818. if (cur_block->label_type == LABEL_TYPE_IF
  10819. && !cur_block->else_addr) {
  10820. opcode = WASM_OP_ELSE;
  10821. p--;
  10822. #if WASM_ENABLE_FAST_INTERP != 0
  10823. p_org = p;
  10824. skip_label();
  10825. disable_emit = false;
  10826. emit_label(opcode);
  10827. #endif
  10828. goto handle_op_else;
  10829. }
  10830. POP_CSP();
  10831. #if WASM_ENABLE_FAST_INTERP != 0
  10832. skip_label();
  10833. /* copy the result to the block return address */
  10834. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10835. error_buf, error_buf_size)) {
  10836. /* it could be tmp frame_csp allocated from opcode like
  10837. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10838. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10839. * to free the loader_ctx->frame_csp if fails */
  10840. free_label_patch_list(loader_ctx->frame_csp);
  10841. goto fail;
  10842. }
  10843. apply_label_patch(loader_ctx, 0, PATCH_END);
  10844. free_label_patch_list(loader_ctx->frame_csp);
  10845. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10846. int32 idx;
  10847. uint8 ret_type;
  10848. emit_label(WASM_OP_RETURN);
  10849. for (idx = (int32)func->func_type->result_count - 1;
  10850. idx >= 0; idx--) {
  10851. ret_type = *(func->func_type->types
  10852. + func->func_type->param_count + idx);
  10853. POP_OFFSET_TYPE(ret_type);
  10854. }
  10855. }
  10856. #endif
  10857. if (loader_ctx->csp_num > 0) {
  10858. loader_ctx->frame_csp->end_addr = p - 1;
  10859. }
  10860. else {
  10861. /* end of function block, function will return */
  10862. if (p < p_end) {
  10863. set_error_buf(error_buf, error_buf_size,
  10864. "section size mismatch");
  10865. goto fail;
  10866. }
  10867. }
  10868. #if WASM_ENABLE_FAST_INTERP != 0
  10869. if (pending_exception) {
  10870. set_error_buf(
  10871. error_buf, error_buf_size,
  10872. "There is a pending exception needs to be handled");
  10873. goto fail;
  10874. }
  10875. #endif
  10876. break;
  10877. }
  10878. case WASM_OP_BR:
  10879. {
  10880. if (!(frame_csp_tmp =
  10881. check_branch_block(loader_ctx, &p, p_end, opcode,
  10882. error_buf, error_buf_size)))
  10883. goto fail;
  10884. RESET_STACK();
  10885. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10886. break;
  10887. }
  10888. case WASM_OP_BR_IF:
  10889. {
  10890. POP_I32();
  10891. if (!(frame_csp_tmp =
  10892. check_branch_block(loader_ctx, &p, p_end, opcode,
  10893. error_buf, error_buf_size)))
  10894. goto fail;
  10895. break;
  10896. }
  10897. case WASM_OP_BR_TABLE:
  10898. {
  10899. uint32 depth = 0, default_arity, arity = 0;
  10900. BranchBlock *target_block;
  10901. BlockType *target_block_type;
  10902. #if WASM_ENABLE_FAST_INTERP == 0
  10903. BrTableCache *br_table_cache = NULL;
  10904. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10905. uint32 j;
  10906. #endif
  10907. pb_read_leb_uint32(p, p_end, count);
  10908. #if WASM_ENABLE_FAST_INTERP != 0
  10909. emit_uint32(loader_ctx, count);
  10910. #endif
  10911. POP_I32();
  10912. /* Get each depth and check it */
  10913. p_org = p;
  10914. for (i = 0; i <= count; i++) {
  10915. pb_read_leb_uint32(p, p_end, depth);
  10916. bh_assert(loader_ctx->csp_num > 0);
  10917. if (loader_ctx->csp_num - 1 < depth) {
  10918. set_error_buf(error_buf, error_buf_size,
  10919. "unknown label, "
  10920. "unexpected end of section or function");
  10921. goto fail;
  10922. }
  10923. }
  10924. p = p_org;
  10925. /* Get the default block's arity */
  10926. target_block = loader_ctx->frame_csp - (depth + 1);
  10927. target_block_type = &target_block->block_type;
  10928. default_arity = block_type_get_arity(target_block_type,
  10929. target_block->label_type);
  10930. #if WASM_ENABLE_FAST_INTERP == 0
  10931. p_depth_begin = p_depth = p;
  10932. #endif
  10933. for (i = 0; i <= count; i++) {
  10934. p_org = p;
  10935. pb_read_leb_uint32(p, p_end, depth);
  10936. p = p_org;
  10937. /* Get the target block's arity and check it */
  10938. target_block = loader_ctx->frame_csp - (depth + 1);
  10939. target_block_type = &target_block->block_type;
  10940. arity = block_type_get_arity(target_block_type,
  10941. target_block->label_type);
  10942. if (arity != default_arity) {
  10943. set_error_buf(error_buf, error_buf_size,
  10944. "type mismatch: br_table targets must "
  10945. "all use same result type");
  10946. goto fail;
  10947. }
  10948. if (!(frame_csp_tmp =
  10949. check_branch_block(loader_ctx, &p, p_end, opcode,
  10950. error_buf, error_buf_size))) {
  10951. goto fail;
  10952. }
  10953. #if WASM_ENABLE_FAST_INTERP == 0
  10954. if (br_table_cache) {
  10955. br_table_cache->br_depths[i] = depth;
  10956. }
  10957. else {
  10958. if (depth > 255) {
  10959. /* The depth cannot be stored in one byte,
  10960. create br_table cache to store each depth */
  10961. #if WASM_ENABLE_DEBUG_INTERP != 0
  10962. if (!record_fast_op(module, p_opcode, *p_opcode,
  10963. error_buf, error_buf_size)) {
  10964. goto fail;
  10965. }
  10966. #endif
  10967. if (!(br_table_cache = loader_malloc(
  10968. offsetof(BrTableCache, br_depths)
  10969. + sizeof(uint32)
  10970. * (uint64)(count + 1),
  10971. error_buf, error_buf_size))) {
  10972. goto fail;
  10973. }
  10974. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10975. br_table_cache->br_table_op_addr = p_opcode;
  10976. br_table_cache->br_count = count;
  10977. /* Copy previous depths which are one byte */
  10978. for (j = 0; j < i; j++) {
  10979. br_table_cache->br_depths[j] = p_depth_begin[j];
  10980. }
  10981. br_table_cache->br_depths[i] = depth;
  10982. bh_list_insert(module->br_table_cache_list,
  10983. br_table_cache);
  10984. }
  10985. else {
  10986. /* The depth can be stored in one byte, use the
  10987. byte of the leb to store it */
  10988. *p_depth++ = (uint8)depth;
  10989. }
  10990. }
  10991. #endif
  10992. }
  10993. #if WASM_ENABLE_FAST_INTERP == 0
  10994. /* Set the tailing bytes to nop */
  10995. if (br_table_cache)
  10996. p_depth = p_depth_begin;
  10997. while (p_depth < p)
  10998. *p_depth++ = WASM_OP_NOP;
  10999. #endif
  11000. RESET_STACK();
  11001. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11002. break;
  11003. }
  11004. case WASM_OP_RETURN:
  11005. {
  11006. WASMFuncType *func_type = func->func_type;
  11007. int32 idx;
  11008. uint8 ret_type;
  11009. #if WASM_ENABLE_GC != 0
  11010. uint32 j = func_type->ref_type_map_count - 1;
  11011. #endif
  11012. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  11013. idx--) {
  11014. ret_type =
  11015. *(func_type->types + func_type->param_count + idx);
  11016. #if WASM_ENABLE_GC != 0
  11017. if (wasm_is_type_multi_byte_type(ret_type)) {
  11018. WASMRefType *ref_type =
  11019. func_type->ref_type_maps[j].ref_type;
  11020. bh_assert(func_type->ref_type_maps[j].index
  11021. == func_type->param_count + idx);
  11022. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11023. ref_type,
  11024. wasm_reftype_struct_size(ref_type));
  11025. j--;
  11026. }
  11027. #endif
  11028. #if WASM_ENABLE_FAST_INTERP != 0
  11029. /* emit the offset after return opcode */
  11030. POP_OFFSET_TYPE(ret_type);
  11031. #endif
  11032. POP_TYPE(ret_type);
  11033. }
  11034. RESET_STACK();
  11035. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11036. break;
  11037. }
  11038. case WASM_OP_CALL:
  11039. #if WASM_ENABLE_TAIL_CALL != 0
  11040. case WASM_OP_RETURN_CALL:
  11041. #endif
  11042. #if WASM_ENABLE_GC != 0
  11043. case WASM_OP_CALL_REF:
  11044. case WASM_OP_RETURN_CALL_REF:
  11045. #endif
  11046. {
  11047. WASMFuncType *func_type;
  11048. uint8 type;
  11049. int32 idx;
  11050. #if WASM_ENABLE_GC != 0
  11051. WASMRefType *ref_type;
  11052. uint32 type_idx1;
  11053. int32 j;
  11054. #endif
  11055. #if WASM_ENABLE_GC != 0
  11056. if (opcode == WASM_OP_CALL_REF
  11057. || opcode == WASM_OP_RETURN_CALL_REF) {
  11058. pb_read_leb_uint32(p, p_end, type_idx1);
  11059. if (!check_type_index(module, module->type_count, type_idx1,
  11060. error_buf, error_buf_size)) {
  11061. goto fail;
  11062. }
  11063. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11064. set_error_buf(error_buf, error_buf_size,
  11065. "unknown function type");
  11066. goto fail;
  11067. }
  11068. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11069. &type_idx, error_buf,
  11070. error_buf_size)) {
  11071. goto fail;
  11072. }
  11073. if (type == VALUE_TYPE_ANY) {
  11074. type_idx = type_idx1;
  11075. }
  11076. if (!check_type_index(module, module->type_count, type_idx,
  11077. error_buf, error_buf_size)) {
  11078. goto fail;
  11079. }
  11080. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11081. set_error_buf(error_buf, error_buf_size,
  11082. "unknown function type");
  11083. goto fail;
  11084. }
  11085. if (!wasm_func_type_is_super_of(
  11086. (WASMFuncType *)module->types[type_idx1],
  11087. (WASMFuncType *)module->types[type_idx])) {
  11088. set_error_buf(error_buf, error_buf_size,
  11089. "function type mismatch");
  11090. goto fail;
  11091. }
  11092. func_type = (WASMFuncType *)module->types[type_idx];
  11093. }
  11094. else
  11095. #endif
  11096. {
  11097. pb_read_leb_uint32(p, p_end, func_idx);
  11098. #if WASM_ENABLE_FAST_INTERP != 0
  11099. /* we need to emit func_idx before arguments */
  11100. emit_uint32(loader_ctx, func_idx);
  11101. #endif
  11102. if (!check_function_index(module, func_idx, error_buf,
  11103. error_buf_size)) {
  11104. goto fail;
  11105. }
  11106. if (func_idx < module->import_function_count)
  11107. func_type = module->import_functions[func_idx]
  11108. .u.function.func_type;
  11109. else
  11110. func_type =
  11111. module
  11112. ->functions[func_idx
  11113. - module->import_function_count]
  11114. ->func_type;
  11115. }
  11116. if (func_type->param_count > 0) {
  11117. #if WASM_ENABLE_GC != 0
  11118. j = (int32)(func_type->result_ref_type_maps
  11119. - func_type->ref_type_maps - 1);
  11120. #endif
  11121. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11122. idx--) {
  11123. #if WASM_ENABLE_GC != 0
  11124. if (wasm_is_type_multi_byte_type(
  11125. func_type->types[idx])) {
  11126. ref_type = func_type->ref_type_maps[j].ref_type;
  11127. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11128. ref_type,
  11129. wasm_reftype_struct_size(ref_type));
  11130. j--;
  11131. }
  11132. #endif
  11133. #if WASM_ENABLE_FAST_INTERP != 0
  11134. POP_OFFSET_TYPE(func_type->types[idx]);
  11135. #endif
  11136. POP_TYPE(func_type->types[idx]);
  11137. }
  11138. }
  11139. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11140. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11141. #endif
  11142. #if WASM_ENABLE_GC != 0
  11143. j = (int32)(func_type->result_ref_type_maps
  11144. - func_type->ref_type_maps);
  11145. #endif
  11146. for (i = 0; i < func_type->result_count; i++) {
  11147. #if WASM_ENABLE_GC != 0
  11148. if (wasm_is_type_multi_byte_type(
  11149. func_type->types[func_type->param_count + i])) {
  11150. ref_type = func_type->ref_type_maps[j].ref_type;
  11151. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11152. ref_type,
  11153. wasm_reftype_struct_size(ref_type));
  11154. j++;
  11155. }
  11156. #endif
  11157. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11158. #if WASM_ENABLE_FAST_INTERP != 0
  11159. /* Here we emit each return value's dynamic_offset. But
  11160. * in fact these offsets are continuous, so interpreter
  11161. * only need to get the first return value's offset.
  11162. */
  11163. PUSH_OFFSET_TYPE(
  11164. func_type->types[func_type->param_count + i]);
  11165. #endif
  11166. }
  11167. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11168. }
  11169. else {
  11170. #if WASM_ENABLE_GC == 0
  11171. if (func_type->result_count
  11172. != func->func_type->result_count) {
  11173. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11174. "type mismatch: expect ",
  11175. func->func_type->result_count,
  11176. " return values but got other");
  11177. goto fail;
  11178. }
  11179. for (i = 0; i < func_type->result_count; i++) {
  11180. type = func->func_type
  11181. ->types[func->func_type->param_count + i];
  11182. if (func_type->types[func_type->param_count + i]
  11183. != type) {
  11184. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11185. "type mismatch: expect ",
  11186. type2str(type), " but got other");
  11187. goto fail;
  11188. }
  11189. }
  11190. #else
  11191. if (!wasm_func_type_result_is_subtype_of(
  11192. func_type, func->func_type, module->types,
  11193. module->type_count)) {
  11194. set_error_buf(
  11195. error_buf, error_buf_size,
  11196. "type mismatch: invalid func result types");
  11197. goto fail;
  11198. }
  11199. #endif
  11200. RESET_STACK();
  11201. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11202. }
  11203. #endif
  11204. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11205. || WASM_ENABLE_WAMR_COMPILER != 0
  11206. func->has_op_func_call = true;
  11207. #endif
  11208. (void)type;
  11209. break;
  11210. }
  11211. /*
  11212. * if disable reference type: call_indirect typeidx, 0x00
  11213. * if enable reference type: call_indirect typeidx, tableidx
  11214. */
  11215. case WASM_OP_CALL_INDIRECT:
  11216. #if WASM_ENABLE_TAIL_CALL != 0
  11217. case WASM_OP_RETURN_CALL_INDIRECT:
  11218. #endif
  11219. {
  11220. int32 idx;
  11221. WASMFuncType *func_type;
  11222. uint32 tbl_elem_type;
  11223. #if WASM_ENABLE_GC != 0
  11224. WASMRefType *elem_ref_type = NULL;
  11225. #endif
  11226. pb_read_leb_uint32(p, p_end, type_idx);
  11227. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11228. #if WASM_ENABLE_WAMR_COMPILER != 0
  11229. if (p + 1 < p_end && *p != 0x00) {
  11230. /*
  11231. * Any non-0x00 byte requires the ref types proposal.
  11232. * This is different from checking the table_idx value
  11233. * since `0x80 0x00` etc. are all valid encodings of zero.
  11234. */
  11235. module->is_ref_types_used = true;
  11236. }
  11237. #endif
  11238. pb_read_leb_uint32(p, p_end, table_idx);
  11239. #else
  11240. CHECK_BUF(p, p_end, 1);
  11241. table_idx = read_uint8(p);
  11242. #endif
  11243. if (!check_table_index(module, table_idx, error_buf,
  11244. error_buf_size)) {
  11245. goto fail;
  11246. }
  11247. tbl_elem_type =
  11248. table_idx < module->import_table_count
  11249. ? module->import_tables[table_idx]
  11250. .u.table.table_type.elem_type
  11251. : module->tables[table_idx - module->import_table_count]
  11252. .table_type.elem_type;
  11253. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11254. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11255. set_error_buf_v(error_buf, error_buf_size,
  11256. "type mismatch: instruction requires table "
  11257. "of functions but table %u has externref",
  11258. table_idx);
  11259. goto fail;
  11260. }
  11261. #elif WASM_ENABLE_GC != 0
  11262. /* Table element must match type ref null func */
  11263. elem_ref_type =
  11264. table_idx < module->import_table_count
  11265. ? module->import_tables[table_idx]
  11266. .u.table.table_type.elem_ref_type
  11267. : module->tables[table_idx - module->import_table_count]
  11268. .table_type.elem_ref_type;
  11269. if (!wasm_reftype_is_subtype_of(
  11270. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11271. module->types, module->type_count)) {
  11272. set_error_buf_v(error_buf, error_buf_size,
  11273. "type mismatch: instruction requires "
  11274. "reference type t match type ref null func"
  11275. "in table %u",
  11276. table_idx);
  11277. goto fail;
  11278. }
  11279. #else
  11280. (void)tbl_elem_type;
  11281. #endif
  11282. #if WASM_ENABLE_FAST_INTERP != 0
  11283. /* we need to emit before arguments */
  11284. #if WASM_ENABLE_TAIL_CALL != 0
  11285. emit_byte(loader_ctx, opcode);
  11286. #endif
  11287. emit_uint32(loader_ctx, type_idx);
  11288. emit_uint32(loader_ctx, table_idx);
  11289. #endif
  11290. #if WASM_ENABLE_MEMORY64 != 0
  11291. table_elem_idx_type = is_table_64bit(module, table_idx)
  11292. ? VALUE_TYPE_I64
  11293. : VALUE_TYPE_I32;
  11294. #endif
  11295. /* skip elem idx */
  11296. POP_TBL_ELEM_IDX();
  11297. if (type_idx >= module->type_count) {
  11298. set_error_buf(error_buf, error_buf_size, "unknown type");
  11299. goto fail;
  11300. }
  11301. func_type = (WASMFuncType *)module->types[type_idx];
  11302. if (func_type->param_count > 0) {
  11303. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11304. idx--) {
  11305. #if WASM_ENABLE_FAST_INTERP != 0
  11306. POP_OFFSET_TYPE(func_type->types[idx]);
  11307. #endif
  11308. POP_TYPE(func_type->types[idx]);
  11309. }
  11310. }
  11311. #if WASM_ENABLE_TAIL_CALL != 0
  11312. if (opcode == WASM_OP_CALL_INDIRECT) {
  11313. #endif
  11314. for (i = 0; i < func_type->result_count; i++) {
  11315. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11316. #if WASM_ENABLE_FAST_INTERP != 0
  11317. PUSH_OFFSET_TYPE(
  11318. func_type->types[func_type->param_count + i]);
  11319. #endif
  11320. }
  11321. #if WASM_ENABLE_TAIL_CALL != 0
  11322. }
  11323. else {
  11324. uint8 type;
  11325. if (func_type->result_count
  11326. != func->func_type->result_count) {
  11327. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11328. "type mismatch: expect ",
  11329. func->func_type->result_count,
  11330. " return values but got other");
  11331. goto fail;
  11332. }
  11333. for (i = 0; i < func_type->result_count; i++) {
  11334. type = func->func_type
  11335. ->types[func->func_type->param_count + i];
  11336. if (func_type->types[func_type->param_count + i]
  11337. != type) {
  11338. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11339. "type mismatch: expect ",
  11340. type2str(type), " but got other");
  11341. goto fail;
  11342. }
  11343. }
  11344. RESET_STACK();
  11345. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11346. }
  11347. #endif
  11348. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11349. || WASM_ENABLE_WAMR_COMPILER != 0
  11350. func->has_op_func_call = true;
  11351. #endif
  11352. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11353. func->has_op_call_indirect = true;
  11354. #endif
  11355. break;
  11356. }
  11357. case WASM_OP_DROP:
  11358. {
  11359. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11360. int32 available_stack_cell =
  11361. (int32)(loader_ctx->stack_cell_num
  11362. - cur_block->stack_cell_num);
  11363. if (available_stack_cell <= 0
  11364. && !cur_block->is_stack_polymorphic) {
  11365. set_error_buf(error_buf, error_buf_size,
  11366. "type mismatch, opcode drop was found "
  11367. "but stack was empty");
  11368. goto fail;
  11369. }
  11370. if (available_stack_cell > 0) {
  11371. #if WASM_ENABLE_GC != 0
  11372. if (wasm_is_type_multi_byte_type(
  11373. *(loader_ctx->frame_ref - 1))) {
  11374. bh_assert((int32)(loader_ctx->reftype_map_num
  11375. - cur_block->reftype_map_num)
  11376. > 0);
  11377. loader_ctx->frame_reftype_map--;
  11378. loader_ctx->reftype_map_num--;
  11379. }
  11380. #endif
  11381. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11382. loader_ctx->frame_ref--;
  11383. loader_ctx->stack_cell_num--;
  11384. #if WASM_ENABLE_FAST_INTERP != 0
  11385. skip_label();
  11386. loader_ctx->frame_offset--;
  11387. if ((*(loader_ctx->frame_offset)
  11388. > loader_ctx->start_dynamic_offset)
  11389. && (*(loader_ctx->frame_offset)
  11390. < loader_ctx->max_dynamic_offset))
  11391. loader_ctx->dynamic_offset--;
  11392. #endif
  11393. }
  11394. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11395. loader_ctx->frame_ref -= 2;
  11396. loader_ctx->stack_cell_num -= 2;
  11397. #if WASM_ENABLE_FAST_INTERP == 0
  11398. *(p - 1) = WASM_OP_DROP_64;
  11399. #endif
  11400. #if WASM_ENABLE_FAST_INTERP != 0
  11401. skip_label();
  11402. loader_ctx->frame_offset -= 2;
  11403. if ((*(loader_ctx->frame_offset)
  11404. > loader_ctx->start_dynamic_offset)
  11405. && (*(loader_ctx->frame_offset)
  11406. < loader_ctx->max_dynamic_offset))
  11407. loader_ctx->dynamic_offset -= 2;
  11408. #endif
  11409. }
  11410. #if WASM_ENABLE_SIMD != 0
  11411. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11412. || (WASM_ENABLE_FAST_INTERP != 0)
  11413. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11414. loader_ctx->frame_ref -= 4;
  11415. loader_ctx->stack_cell_num -= 4;
  11416. #if WASM_ENABLE_FAST_INTERP != 0
  11417. skip_label();
  11418. loader_ctx->frame_offset -= 4;
  11419. if ((*(loader_ctx->frame_offset)
  11420. > loader_ctx->start_dynamic_offset)
  11421. && (*(loader_ctx->frame_offset)
  11422. < loader_ctx->max_dynamic_offset))
  11423. loader_ctx->dynamic_offset -= 4;
  11424. #endif
  11425. }
  11426. #endif
  11427. #endif
  11428. else {
  11429. set_error_buf(error_buf, error_buf_size,
  11430. "type mismatch");
  11431. goto fail;
  11432. }
  11433. }
  11434. else {
  11435. #if WASM_ENABLE_FAST_INTERP != 0
  11436. skip_label();
  11437. #endif
  11438. }
  11439. break;
  11440. }
  11441. case WASM_OP_SELECT:
  11442. {
  11443. uint8 ref_type;
  11444. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11445. int32 available_stack_cell;
  11446. #if WASM_ENABLE_FAST_INTERP != 0
  11447. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11448. #endif
  11449. POP_I32();
  11450. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11451. - cur_block->stack_cell_num);
  11452. if (available_stack_cell <= 0
  11453. && !cur_block->is_stack_polymorphic) {
  11454. set_error_buf(error_buf, error_buf_size,
  11455. "type mismatch or invalid result arity, "
  11456. "opcode select was found "
  11457. "but stack was empty");
  11458. goto fail;
  11459. }
  11460. if (available_stack_cell > 0) {
  11461. switch (*(loader_ctx->frame_ref - 1)) {
  11462. case VALUE_TYPE_I32:
  11463. case VALUE_TYPE_F32:
  11464. case VALUE_TYPE_ANY:
  11465. break;
  11466. case VALUE_TYPE_I64:
  11467. case VALUE_TYPE_F64:
  11468. #if WASM_ENABLE_FAST_INTERP == 0
  11469. *(p - 1) = WASM_OP_SELECT_64;
  11470. #endif
  11471. #if WASM_ENABLE_FAST_INTERP != 0
  11472. if (loader_ctx->p_code_compiled) {
  11473. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11474. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11475. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11476. *(void **)(p_code_compiled_tmp
  11477. - sizeof(void *)) =
  11478. handle_table[opcode_tmp];
  11479. #else
  11480. #if UINTPTR_MAX == UINT64_MAX
  11481. /* emit int32 relative offset in 64-bit target
  11482. */
  11483. int32 offset =
  11484. (int32)((uint8 *)handle_table[opcode_tmp]
  11485. - (uint8 *)handle_table[0]);
  11486. *(int32 *)(p_code_compiled_tmp
  11487. - sizeof(int32)) = offset;
  11488. #else
  11489. /* emit uint32 label address in 32-bit target */
  11490. *(uint32 *)(p_code_compiled_tmp
  11491. - sizeof(uint32)) =
  11492. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11493. #endif
  11494. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11495. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11496. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11497. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11498. #else
  11499. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11500. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11501. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11502. }
  11503. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11504. break;
  11505. #if WASM_ENABLE_SIMD != 0
  11506. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11507. || (WASM_ENABLE_FAST_INTERP != 0)
  11508. case VALUE_TYPE_V128:
  11509. break;
  11510. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11511. (WASM_ENABLE_FAST_INTERP != 0) */
  11512. #endif /* WASM_ENABLE_SIMD != 0 */
  11513. default:
  11514. {
  11515. set_error_buf(error_buf, error_buf_size,
  11516. "type mismatch");
  11517. goto fail;
  11518. }
  11519. }
  11520. ref_type = *(loader_ctx->frame_ref - 1);
  11521. #if WASM_ENABLE_FAST_INTERP != 0
  11522. POP_OFFSET_TYPE(ref_type);
  11523. POP_TYPE(ref_type);
  11524. POP_OFFSET_TYPE(ref_type);
  11525. POP_TYPE(ref_type);
  11526. PUSH_OFFSET_TYPE(ref_type);
  11527. PUSH_TYPE(ref_type);
  11528. #else
  11529. POP2_AND_PUSH(ref_type, ref_type);
  11530. #endif
  11531. }
  11532. else {
  11533. #if WASM_ENABLE_FAST_INTERP != 0
  11534. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11535. #endif
  11536. PUSH_TYPE(VALUE_TYPE_ANY);
  11537. }
  11538. break;
  11539. }
  11540. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11541. case WASM_OP_SELECT_T:
  11542. {
  11543. uint8 vec_len, type;
  11544. #if WASM_ENABLE_GC != 0
  11545. WASMRefType *ref_type = NULL;
  11546. #endif
  11547. #if WASM_ENABLE_FAST_INTERP != 0
  11548. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11549. #endif
  11550. pb_read_leb_uint32(p, p_end, vec_len);
  11551. if (vec_len != 1) {
  11552. /* typed select must have exactly one result */
  11553. set_error_buf(error_buf, error_buf_size,
  11554. "invalid result arity");
  11555. goto fail;
  11556. }
  11557. #if WASM_ENABLE_GC == 0
  11558. CHECK_BUF(p, p_end, 1);
  11559. type = read_uint8(p);
  11560. if (!is_valid_value_type_for_interpreter(type)) {
  11561. set_error_buf(error_buf, error_buf_size,
  11562. "unknown value type");
  11563. goto fail;
  11564. }
  11565. #else
  11566. p_org = p + 1;
  11567. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11568. module->type_count, &need_ref_type_map,
  11569. &wasm_ref_type, false, error_buf,
  11570. error_buf_size)) {
  11571. goto fail;
  11572. }
  11573. type = wasm_ref_type.ref_type;
  11574. if (need_ref_type_map) {
  11575. if (!(ref_type = reftype_set_insert(
  11576. module->ref_type_set, &wasm_ref_type, error_buf,
  11577. error_buf_size))) {
  11578. goto fail;
  11579. }
  11580. }
  11581. #if WASM_ENABLE_FAST_INTERP == 0
  11582. while (p_org < p) {
  11583. #if WASM_ENABLE_DEBUG_INTERP != 0
  11584. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11585. error_buf_size)) {
  11586. goto fail;
  11587. }
  11588. #endif
  11589. /* Ignore extra bytes for interpreter */
  11590. *p_org++ = WASM_OP_NOP;
  11591. }
  11592. #endif
  11593. #endif /* end of WASM_ENABLE_GC == 0 */
  11594. POP_I32();
  11595. #if WASM_ENABLE_FAST_INTERP != 0
  11596. if (loader_ctx->p_code_compiled) {
  11597. uint8 opcode_tmp = WASM_OP_SELECT;
  11598. if (type == VALUE_TYPE_V128) {
  11599. #if (WASM_ENABLE_SIMD == 0) \
  11600. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11601. && (WASM_ENABLE_FAST_INTERP == 0))
  11602. set_error_buf(error_buf, error_buf_size,
  11603. "SIMD v128 type isn't supported");
  11604. goto fail;
  11605. #endif
  11606. }
  11607. else {
  11608. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11609. opcode_tmp = WASM_OP_SELECT_64;
  11610. #if WASM_ENABLE_GC != 0
  11611. if (wasm_is_type_reftype(type))
  11612. opcode_tmp = WASM_OP_SELECT_T;
  11613. #endif
  11614. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11615. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11616. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11617. handle_table[opcode_tmp];
  11618. #else
  11619. #if UINTPTR_MAX == UINT64_MAX
  11620. /* emit int32 relative offset in 64-bit target */
  11621. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11622. - (uint8 *)handle_table[0]);
  11623. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11624. offset;
  11625. #else
  11626. /* emit uint32 label address in 32-bit target */
  11627. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11628. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11629. #endif
  11630. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11631. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11632. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11633. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11634. #else
  11635. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11636. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11637. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11638. }
  11639. }
  11640. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11641. POP_REF(type);
  11642. #if WASM_ENABLE_GC != 0
  11643. if (need_ref_type_map) {
  11644. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11645. wasm_reftype_struct_size(ref_type));
  11646. }
  11647. #endif
  11648. POP_REF(type);
  11649. #if WASM_ENABLE_GC != 0
  11650. if (need_ref_type_map) {
  11651. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11652. wasm_reftype_struct_size(ref_type));
  11653. }
  11654. #endif
  11655. PUSH_REF(type);
  11656. #if WASM_ENABLE_WAMR_COMPILER != 0
  11657. module->is_ref_types_used = true;
  11658. #endif
  11659. (void)vec_len;
  11660. break;
  11661. }
  11662. /* table.get x. tables[x]. [it] -> [t] */
  11663. /* table.set x. tables[x]. [it t] -> [] */
  11664. case WASM_OP_TABLE_GET:
  11665. case WASM_OP_TABLE_SET:
  11666. {
  11667. uint8 decl_ref_type;
  11668. #if WASM_ENABLE_GC != 0
  11669. WASMRefType *ref_type;
  11670. #endif
  11671. pb_read_leb_uint32(p, p_end, table_idx);
  11672. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11673. #if WASM_ENABLE_GC != 0
  11674. (void **)&ref_type,
  11675. #else
  11676. NULL,
  11677. #endif
  11678. error_buf, error_buf_size))
  11679. goto fail;
  11680. #if WASM_ENABLE_GC != 0
  11681. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11682. bh_assert(ref_type);
  11683. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11684. ref_type, wasm_reftype_struct_size(ref_type));
  11685. }
  11686. #endif
  11687. #if WASM_ENABLE_FAST_INTERP != 0
  11688. emit_uint32(loader_ctx, table_idx);
  11689. #endif
  11690. #if WASM_ENABLE_MEMORY64 != 0
  11691. table_elem_idx_type = is_table_64bit(module, table_idx)
  11692. ? VALUE_TYPE_I64
  11693. : VALUE_TYPE_I32;
  11694. #endif
  11695. if (opcode == WASM_OP_TABLE_GET) {
  11696. POP_TBL_ELEM_IDX();
  11697. #if WASM_ENABLE_FAST_INTERP != 0
  11698. PUSH_OFFSET_TYPE(decl_ref_type);
  11699. #endif
  11700. PUSH_TYPE(decl_ref_type);
  11701. }
  11702. else {
  11703. #if WASM_ENABLE_FAST_INTERP != 0
  11704. POP_OFFSET_TYPE(decl_ref_type);
  11705. #endif
  11706. POP_TYPE(decl_ref_type);
  11707. POP_TBL_ELEM_IDX();
  11708. }
  11709. #if WASM_ENABLE_WAMR_COMPILER != 0
  11710. module->is_ref_types_used = true;
  11711. #endif
  11712. break;
  11713. }
  11714. case WASM_OP_REF_NULL:
  11715. {
  11716. uint8 ref_type;
  11717. #if WASM_ENABLE_GC == 0
  11718. CHECK_BUF(p, p_end, 1);
  11719. ref_type = read_uint8(p);
  11720. if (ref_type != VALUE_TYPE_FUNCREF
  11721. && ref_type != VALUE_TYPE_EXTERNREF) {
  11722. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11723. goto fail;
  11724. }
  11725. #else
  11726. pb_read_leb_int32(p, p_end, heap_type);
  11727. if (heap_type >= 0) {
  11728. if (!check_type_index(module, module->type_count, heap_type,
  11729. error_buf, error_buf_size)) {
  11730. goto fail;
  11731. }
  11732. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11733. true, heap_type);
  11734. ref_type = wasm_ref_type.ref_type;
  11735. }
  11736. else {
  11737. if (!wasm_is_valid_heap_type(heap_type)) {
  11738. set_error_buf(error_buf, error_buf_size,
  11739. "unknown type");
  11740. goto fail;
  11741. }
  11742. ref_type = (uint8)((int32)0x80 + heap_type);
  11743. }
  11744. #endif /* end of WASM_ENABLE_GC == 0 */
  11745. #if WASM_ENABLE_FAST_INTERP != 0
  11746. PUSH_OFFSET_TYPE(ref_type);
  11747. #endif
  11748. PUSH_TYPE(ref_type);
  11749. #if WASM_ENABLE_WAMR_COMPILER != 0
  11750. module->is_ref_types_used = true;
  11751. #endif
  11752. break;
  11753. }
  11754. case WASM_OP_REF_IS_NULL:
  11755. {
  11756. #if WASM_ENABLE_GC == 0
  11757. #if WASM_ENABLE_FAST_INTERP != 0
  11758. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11759. int32 block_stack_cell_num =
  11760. (int32)(loader_ctx->stack_cell_num
  11761. - cur_block->stack_cell_num);
  11762. if (block_stack_cell_num <= 0) {
  11763. if (!cur_block->is_stack_polymorphic) {
  11764. set_error_buf(
  11765. error_buf, error_buf_size,
  11766. "type mismatch: expect data but stack was empty");
  11767. goto fail;
  11768. }
  11769. }
  11770. else {
  11771. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11772. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11773. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11774. if (!wasm_loader_pop_frame_ref_offset(
  11775. loader_ctx, *(loader_ctx->frame_ref - 1),
  11776. error_buf, error_buf_size)) {
  11777. goto fail;
  11778. }
  11779. }
  11780. else {
  11781. set_error_buf(error_buf, error_buf_size,
  11782. "type mismatch");
  11783. goto fail;
  11784. }
  11785. }
  11786. #else
  11787. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11788. error_buf, error_buf_size)
  11789. && !wasm_loader_pop_frame_ref(loader_ctx,
  11790. VALUE_TYPE_EXTERNREF,
  11791. error_buf, error_buf_size)) {
  11792. goto fail;
  11793. }
  11794. #endif
  11795. #else /* else of WASM_ENABLE_GC == 0 */
  11796. uint8 type;
  11797. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11798. error_buf, error_buf_size)) {
  11799. goto fail;
  11800. }
  11801. #endif
  11802. PUSH_I32();
  11803. #if WASM_ENABLE_WAMR_COMPILER != 0
  11804. module->is_ref_types_used = true;
  11805. #endif
  11806. break;
  11807. }
  11808. case WASM_OP_REF_FUNC:
  11809. {
  11810. pb_read_leb_uint32(p, p_end, func_idx);
  11811. if (!check_function_index(module, func_idx, error_buf,
  11812. error_buf_size)) {
  11813. goto fail;
  11814. }
  11815. /* Refer to a forward-declared function:
  11816. the function must be an import, exported, or present in
  11817. a table elem segment or global initializer to be used as
  11818. the operand to ref.func */
  11819. if (func_idx >= module->import_function_count) {
  11820. WASMTableSeg *table_seg = module->table_segments;
  11821. bool func_declared = false;
  11822. uint32 j;
  11823. for (i = 0; i < module->global_count; i++) {
  11824. if (module->globals[i].type.val_type
  11825. == VALUE_TYPE_FUNCREF
  11826. && module->globals[i].init_expr.init_expr_type
  11827. == INIT_EXPR_TYPE_FUNCREF_CONST
  11828. && module->globals[i].init_expr.u.u32 == func_idx) {
  11829. func_declared = true;
  11830. break;
  11831. }
  11832. }
  11833. if (!func_declared) {
  11834. /* Check whether the function is declared in table segs,
  11835. note that it doesn't matter whether the table seg's
  11836. mode is passive, active or declarative. */
  11837. for (i = 0; i < module->table_seg_count;
  11838. i++, table_seg++) {
  11839. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11840. #if WASM_ENABLE_GC != 0
  11841. /* elem type is (ref null? func) or
  11842. (ref null? $t) */
  11843. || ((table_seg->elem_type
  11844. == REF_TYPE_HT_NON_NULLABLE
  11845. || table_seg->elem_type
  11846. == REF_TYPE_HT_NULLABLE)
  11847. && (table_seg->elem_ref_type->ref_ht_common
  11848. .heap_type
  11849. == HEAP_TYPE_FUNC
  11850. || table_seg->elem_ref_type
  11851. ->ref_ht_common.heap_type
  11852. > 0))
  11853. #endif
  11854. ) {
  11855. for (j = 0; j < table_seg->value_count; j++) {
  11856. if (table_seg->init_values[j].u.ref_index
  11857. == func_idx) {
  11858. func_declared = true;
  11859. break;
  11860. }
  11861. }
  11862. }
  11863. }
  11864. }
  11865. if (!func_declared) {
  11866. /* Check whether the function is exported */
  11867. for (i = 0; i < module->export_count; i++) {
  11868. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11869. && module->exports[i].index == func_idx) {
  11870. func_declared = true;
  11871. break;
  11872. }
  11873. }
  11874. }
  11875. if (!func_declared) {
  11876. set_error_buf(error_buf, error_buf_size,
  11877. "undeclared function reference");
  11878. goto fail;
  11879. }
  11880. }
  11881. #if WASM_ENABLE_FAST_INTERP != 0
  11882. emit_uint32(loader_ctx, func_idx);
  11883. #endif
  11884. #if WASM_ENABLE_GC == 0
  11885. PUSH_FUNCREF();
  11886. #else
  11887. if (func_idx < module->import_function_count)
  11888. type_idx =
  11889. module->import_functions[func_idx].u.function.type_idx;
  11890. else
  11891. type_idx = module
  11892. ->functions[func_idx
  11893. - module->import_function_count]
  11894. ->type_idx;
  11895. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11896. false, type_idx);
  11897. PUSH_REF(wasm_ref_type.ref_type);
  11898. #endif
  11899. #if WASM_ENABLE_WAMR_COMPILER != 0
  11900. module->is_ref_types_used = true;
  11901. #endif
  11902. break;
  11903. }
  11904. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11905. #if WASM_ENABLE_GC != 0
  11906. case WASM_OP_REF_AS_NON_NULL:
  11907. case WASM_OP_BR_ON_NULL:
  11908. {
  11909. uint8 type;
  11910. WASMRefType ref_type;
  11911. /* POP (ref null ht) and get the converted (ref ht) */
  11912. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11913. error_buf, error_buf_size)) {
  11914. goto fail;
  11915. }
  11916. if (opcode == WASM_OP_BR_ON_NULL) {
  11917. if (!(frame_csp_tmp =
  11918. check_branch_block(loader_ctx, &p, p_end, opcode,
  11919. error_buf, error_buf_size))) {
  11920. goto fail;
  11921. }
  11922. #if WASM_ENABLE_FAST_INTERP != 0
  11923. disable_emit = true;
  11924. #endif
  11925. }
  11926. /* PUSH the converted (ref ht) */
  11927. if (type != VALUE_TYPE_ANY) {
  11928. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11929. sizeof(WASMRefType));
  11930. }
  11931. PUSH_REF(type);
  11932. break;
  11933. }
  11934. case WASM_OP_BR_ON_NON_NULL:
  11935. {
  11936. uint8 type;
  11937. WASMRefType ref_type;
  11938. uint32 available_stack_cell =
  11939. loader_ctx->stack_cell_num
  11940. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11941. /* POP (ref null ht) and get the converted (ref ht) */
  11942. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11943. error_buf, error_buf_size)) {
  11944. goto fail;
  11945. }
  11946. #if WASM_ENABLE_FAST_INTERP != 0
  11947. disable_emit = true;
  11948. #endif
  11949. /* Temporarily PUSH back (ref ht), check brach block and
  11950. then POP it */
  11951. if (available_stack_cell
  11952. > 0) { /* stack isn't in polymorphic state */
  11953. if (type != VALUE_TYPE_ANY) {
  11954. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11955. &ref_type, sizeof(WASMRefType));
  11956. }
  11957. PUSH_REF(type);
  11958. }
  11959. if (!(frame_csp_tmp =
  11960. check_branch_block(loader_ctx, &p, p_end, opcode,
  11961. error_buf, error_buf_size))) {
  11962. goto fail;
  11963. }
  11964. if (available_stack_cell
  11965. > 0) { /* stack isn't in polymorphic state */
  11966. POP_REF(type);
  11967. #if WASM_ENABLE_FAST_INTERP != 0
  11968. /* Erase the opnd offset emitted by POP_REF() */
  11969. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11970. #endif
  11971. }
  11972. break;
  11973. }
  11974. case WASM_OP_REF_EQ:
  11975. POP_REF(REF_TYPE_EQREF);
  11976. POP_REF(REF_TYPE_EQREF);
  11977. PUSH_I32();
  11978. break;
  11979. #endif /* end of WASM_ENABLE_GC != 0 */
  11980. case WASM_OP_GET_LOCAL:
  11981. {
  11982. p_org = p - 1;
  11983. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11984. PUSH_TYPE(local_type);
  11985. #if WASM_ENABLE_GC != 0
  11986. /* Cannot get a non-nullable and unset local */
  11987. if (local_idx >= param_count
  11988. && wasm_is_reftype_htref_non_nullable(local_type)
  11989. && !wasm_loader_get_local_status(loader_ctx,
  11990. local_idx - param_count)) {
  11991. set_error_buf(error_buf, error_buf_size,
  11992. "uninitialized local");
  11993. goto fail;
  11994. }
  11995. #endif
  11996. #if WASM_ENABLE_FAST_INTERP != 0
  11997. /* Get Local is optimized out */
  11998. skip_label();
  11999. disable_emit = true;
  12000. operand_offset = local_offset;
  12001. PUSH_OFFSET_TYPE(local_type);
  12002. #else
  12003. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12004. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12005. if (local_offset < 0x80
  12006. #if WASM_ENABLE_GC != 0
  12007. && !wasm_is_type_reftype(local_type)
  12008. #endif
  12009. ) {
  12010. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  12011. if (is_32bit_type(local_type)) {
  12012. *p_org++ = (uint8)local_offset;
  12013. }
  12014. else {
  12015. *p_org++ = (uint8)(local_offset | 0x80);
  12016. }
  12017. while (p_org < p) {
  12018. *p_org++ = WASM_OP_NOP;
  12019. }
  12020. }
  12021. #endif
  12022. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12023. break;
  12024. }
  12025. case WASM_OP_SET_LOCAL:
  12026. {
  12027. p_org = p - 1;
  12028. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12029. #if WASM_ENABLE_FAST_INTERP != 0
  12030. if (!(preserve_referenced_local(
  12031. loader_ctx, opcode, local_offset, local_type,
  12032. &preserve_local, error_buf, error_buf_size)))
  12033. goto fail;
  12034. if (local_offset < 256
  12035. #if WASM_ENABLE_GC != 0
  12036. && !wasm_is_type_reftype(local_type)
  12037. #endif
  12038. ) {
  12039. skip_label();
  12040. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12041. if (loader_ctx->p_code_compiled)
  12042. STORE_U16(loader_ctx->p_code_compiled - 2,
  12043. local_offset);
  12044. loader_ctx->frame_offset--;
  12045. loader_ctx->dynamic_offset--;
  12046. }
  12047. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12048. if (loader_ctx->p_code_compiled)
  12049. STORE_U16(loader_ctx->p_code_compiled - 2,
  12050. local_offset);
  12051. loader_ctx->frame_offset -= 2;
  12052. loader_ctx->dynamic_offset -= 2;
  12053. }
  12054. else {
  12055. if (is_32bit_type(local_type)) {
  12056. emit_label(EXT_OP_SET_LOCAL_FAST);
  12057. emit_byte(loader_ctx, (uint8)local_offset);
  12058. }
  12059. else if (is_64bit_type(local_type)) {
  12060. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12061. emit_byte(loader_ctx, (uint8)local_offset);
  12062. }
  12063. #if WASM_ENABLE_SIMDE != 0
  12064. else if (local_type == VALUE_TYPE_V128) {
  12065. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12066. emit_byte(loader_ctx, (uint8)local_offset);
  12067. }
  12068. #endif
  12069. else {
  12070. set_error_buf(error_buf, error_buf_size,
  12071. "unknown local type");
  12072. goto fail;
  12073. }
  12074. POP_OFFSET_TYPE(local_type);
  12075. }
  12076. }
  12077. else { /* local index larger than 255, reserve leb */
  12078. emit_uint32(loader_ctx, local_idx);
  12079. POP_OFFSET_TYPE(local_type);
  12080. }
  12081. #else
  12082. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12083. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12084. if (local_offset < 0x80
  12085. #if WASM_ENABLE_GC != 0
  12086. && !wasm_is_type_reftype(local_type)
  12087. #endif
  12088. ) {
  12089. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12090. if (is_32bit_type(local_type)) {
  12091. *p_org++ = (uint8)local_offset;
  12092. }
  12093. else {
  12094. *p_org++ = (uint8)(local_offset | 0x80);
  12095. }
  12096. while (p_org < p) {
  12097. *p_org++ = WASM_OP_NOP;
  12098. }
  12099. }
  12100. #endif
  12101. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12102. #if WASM_ENABLE_GC != 0
  12103. if (local_idx >= param_count) {
  12104. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12105. }
  12106. #endif
  12107. POP_TYPE(local_type);
  12108. break;
  12109. }
  12110. case WASM_OP_TEE_LOCAL:
  12111. {
  12112. p_org = p - 1;
  12113. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12114. #if WASM_ENABLE_FAST_INTERP != 0
  12115. /* If the stack is in polymorphic state, do fake pop and push on
  12116. offset stack to keep the depth of offset stack to be the
  12117. same with ref stack */
  12118. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12119. if (cur_block->is_stack_polymorphic) {
  12120. POP_OFFSET_TYPE(local_type);
  12121. PUSH_OFFSET_TYPE(local_type);
  12122. }
  12123. #endif
  12124. POP_TYPE(local_type);
  12125. PUSH_TYPE(local_type);
  12126. #if WASM_ENABLE_FAST_INTERP != 0
  12127. if (!(preserve_referenced_local(
  12128. loader_ctx, opcode, local_offset, local_type,
  12129. &preserve_local, error_buf, error_buf_size)))
  12130. goto fail;
  12131. if (local_offset < 256
  12132. #if WASM_ENABLE_GC != 0
  12133. && !wasm_is_type_reftype(local_type)
  12134. #endif
  12135. ) {
  12136. skip_label();
  12137. if (is_32bit_type(local_type)) {
  12138. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12139. emit_byte(loader_ctx, (uint8)local_offset);
  12140. }
  12141. #if WASM_ENABLE_SIMDE != 0
  12142. else if (local_type == VALUE_TYPE_V128) {
  12143. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12144. emit_byte(loader_ctx, (uint8)local_offset);
  12145. }
  12146. #endif
  12147. else {
  12148. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12149. emit_byte(loader_ctx, (uint8)local_offset);
  12150. }
  12151. }
  12152. else { /* local index larger than 255, reserve leb */
  12153. emit_uint32(loader_ctx, local_idx);
  12154. }
  12155. emit_operand(loader_ctx,
  12156. *(loader_ctx->frame_offset
  12157. - wasm_value_type_cell_num(local_type)));
  12158. #else
  12159. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12160. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12161. if (local_offset < 0x80
  12162. #if WASM_ENABLE_GC != 0
  12163. && !wasm_is_type_reftype(local_type)
  12164. #endif
  12165. ) {
  12166. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12167. if (is_32bit_type(local_type)) {
  12168. *p_org++ = (uint8)local_offset;
  12169. }
  12170. else {
  12171. *p_org++ = (uint8)(local_offset | 0x80);
  12172. }
  12173. while (p_org < p) {
  12174. *p_org++ = WASM_OP_NOP;
  12175. }
  12176. }
  12177. #endif
  12178. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12179. #if WASM_ENABLE_GC != 0
  12180. if (local_idx >= param_count) {
  12181. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12182. }
  12183. #endif
  12184. break;
  12185. }
  12186. case WASM_OP_GET_GLOBAL:
  12187. {
  12188. #if WASM_ENABLE_GC != 0
  12189. WASMRefType *ref_type;
  12190. #endif
  12191. p_org = p - 1;
  12192. pb_read_leb_uint32(p, p_end, global_idx);
  12193. if (global_idx >= global_count) {
  12194. set_error_buf(error_buf, error_buf_size, "unknown global");
  12195. goto fail;
  12196. }
  12197. global_type = global_idx < module->import_global_count
  12198. ? module->import_globals[global_idx]
  12199. .u.global.type.val_type
  12200. : module
  12201. ->globals[global_idx
  12202. - module->import_global_count]
  12203. .type.val_type;
  12204. #if WASM_ENABLE_GC != 0
  12205. ref_type =
  12206. global_idx < module->import_global_count
  12207. ? module->import_globals[global_idx].u.global.ref_type
  12208. : module
  12209. ->globals[global_idx
  12210. - module->import_global_count]
  12211. .ref_type;
  12212. if (wasm_is_type_multi_byte_type(global_type)) {
  12213. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12214. wasm_reftype_struct_size(ref_type));
  12215. }
  12216. #endif
  12217. PUSH_TYPE(global_type);
  12218. #if WASM_ENABLE_FAST_INTERP == 0
  12219. if (global_type == VALUE_TYPE_I64
  12220. || global_type == VALUE_TYPE_F64) {
  12221. #if WASM_ENABLE_DEBUG_INTERP != 0
  12222. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12223. error_buf_size)) {
  12224. goto fail;
  12225. }
  12226. #endif
  12227. *p_org = WASM_OP_GET_GLOBAL_64;
  12228. }
  12229. #else /* else of WASM_ENABLE_FAST_INTERP */
  12230. if (global_type == VALUE_TYPE_I64
  12231. || global_type == VALUE_TYPE_F64) {
  12232. skip_label();
  12233. emit_label(WASM_OP_GET_GLOBAL_64);
  12234. }
  12235. #if WASM_ENABLE_SIMDE != 0
  12236. if (global_type == VALUE_TYPE_V128) {
  12237. skip_label();
  12238. emit_label(WASM_OP_GET_GLOBAL_V128);
  12239. }
  12240. #endif /* end of WASM_ENABLE_SIMDE */
  12241. emit_uint32(loader_ctx, global_idx);
  12242. PUSH_OFFSET_TYPE(global_type);
  12243. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12244. break;
  12245. }
  12246. case WASM_OP_SET_GLOBAL:
  12247. {
  12248. bool is_mutable = false;
  12249. #if WASM_ENABLE_GC != 0
  12250. WASMRefType *ref_type;
  12251. #endif
  12252. p_org = p - 1;
  12253. pb_read_leb_uint32(p, p_end, global_idx);
  12254. if (global_idx >= global_count) {
  12255. set_error_buf(error_buf, error_buf_size, "unknown global");
  12256. goto fail;
  12257. }
  12258. is_mutable = global_idx < module->import_global_count
  12259. ? module->import_globals[global_idx]
  12260. .u.global.type.is_mutable
  12261. : module
  12262. ->globals[global_idx
  12263. - module->import_global_count]
  12264. .type.is_mutable;
  12265. if (!is_mutable) {
  12266. #if WASM_ENABLE_GC == 0
  12267. set_error_buf(error_buf, error_buf_size,
  12268. "global is immutable");
  12269. #else
  12270. set_error_buf(error_buf, error_buf_size,
  12271. "immutable global");
  12272. #endif
  12273. goto fail;
  12274. }
  12275. global_type = global_idx < module->import_global_count
  12276. ? module->import_globals[global_idx]
  12277. .u.global.type.val_type
  12278. : module
  12279. ->globals[global_idx
  12280. - module->import_global_count]
  12281. .type.val_type;
  12282. #if WASM_ENABLE_GC != 0
  12283. ref_type =
  12284. global_idx < module->import_global_count
  12285. ? module->import_globals[global_idx].u.global.ref_type
  12286. : module
  12287. ->globals[global_idx
  12288. - module->import_global_count]
  12289. .ref_type;
  12290. if (wasm_is_type_multi_byte_type(global_type)) {
  12291. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12292. wasm_reftype_struct_size(ref_type));
  12293. }
  12294. #endif
  12295. #if WASM_ENABLE_FAST_INTERP == 0
  12296. if (global_type == VALUE_TYPE_I64
  12297. || global_type == VALUE_TYPE_F64) {
  12298. #if WASM_ENABLE_DEBUG_INTERP != 0
  12299. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12300. error_buf_size)) {
  12301. goto fail;
  12302. }
  12303. #endif
  12304. *p_org = WASM_OP_SET_GLOBAL_64;
  12305. }
  12306. else if (module->aux_stack_size > 0
  12307. && global_idx == module->aux_stack_top_global_index) {
  12308. #if WASM_ENABLE_DEBUG_INTERP != 0
  12309. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12310. error_buf_size)) {
  12311. goto fail;
  12312. }
  12313. #endif
  12314. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12315. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12316. func->has_op_set_global_aux_stack = true;
  12317. #endif
  12318. }
  12319. #else /* else of WASM_ENABLE_FAST_INTERP */
  12320. if (global_type == VALUE_TYPE_I64
  12321. || global_type == VALUE_TYPE_F64) {
  12322. skip_label();
  12323. emit_label(WASM_OP_SET_GLOBAL_64);
  12324. }
  12325. else if (module->aux_stack_size > 0
  12326. && global_idx == module->aux_stack_top_global_index) {
  12327. skip_label();
  12328. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12329. }
  12330. #if WASM_ENABLE_SIMDE != 0
  12331. else if (global_type == VALUE_TYPE_V128) {
  12332. skip_label();
  12333. emit_label(WASM_OP_SET_GLOBAL_V128);
  12334. }
  12335. #endif /* end of WASM_ENABLE_SIMDE */
  12336. emit_uint32(loader_ctx, global_idx);
  12337. POP_OFFSET_TYPE(global_type);
  12338. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12339. POP_TYPE(global_type);
  12340. break;
  12341. }
  12342. /* load */
  12343. case WASM_OP_I32_LOAD:
  12344. case WASM_OP_I32_LOAD8_S:
  12345. case WASM_OP_I32_LOAD8_U:
  12346. case WASM_OP_I32_LOAD16_S:
  12347. case WASM_OP_I32_LOAD16_U:
  12348. case WASM_OP_I64_LOAD:
  12349. case WASM_OP_I64_LOAD8_S:
  12350. case WASM_OP_I64_LOAD8_U:
  12351. case WASM_OP_I64_LOAD16_S:
  12352. case WASM_OP_I64_LOAD16_U:
  12353. case WASM_OP_I64_LOAD32_S:
  12354. case WASM_OP_I64_LOAD32_U:
  12355. case WASM_OP_F32_LOAD:
  12356. case WASM_OP_F64_LOAD:
  12357. /* store */
  12358. case WASM_OP_I32_STORE:
  12359. case WASM_OP_I32_STORE8:
  12360. case WASM_OP_I32_STORE16:
  12361. case WASM_OP_I64_STORE:
  12362. case WASM_OP_I64_STORE8:
  12363. case WASM_OP_I64_STORE16:
  12364. case WASM_OP_I64_STORE32:
  12365. case WASM_OP_F32_STORE:
  12366. case WASM_OP_F64_STORE:
  12367. {
  12368. #if WASM_ENABLE_FAST_INTERP != 0
  12369. /* change F32/F64 into I32/I64 */
  12370. if (opcode == WASM_OP_F32_LOAD) {
  12371. skip_label();
  12372. emit_label(WASM_OP_I32_LOAD);
  12373. }
  12374. else if (opcode == WASM_OP_F64_LOAD) {
  12375. skip_label();
  12376. emit_label(WASM_OP_I64_LOAD);
  12377. }
  12378. else if (opcode == WASM_OP_F32_STORE) {
  12379. skip_label();
  12380. emit_label(WASM_OP_I32_STORE);
  12381. }
  12382. else if (opcode == WASM_OP_F64_STORE) {
  12383. skip_label();
  12384. emit_label(WASM_OP_I64_STORE);
  12385. }
  12386. #endif
  12387. CHECK_MEMORY();
  12388. pb_read_leb_memarg(p, p_end, align); /* align */
  12389. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12390. if (!check_memory_access_align(opcode, align, error_buf,
  12391. error_buf_size)) {
  12392. goto fail;
  12393. }
  12394. #if WASM_ENABLE_FAST_INTERP != 0
  12395. emit_uint32(loader_ctx, mem_offset);
  12396. #endif
  12397. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12398. func->has_memory_operations = true;
  12399. #endif
  12400. switch (opcode) {
  12401. /* load */
  12402. case WASM_OP_I32_LOAD:
  12403. case WASM_OP_I32_LOAD8_S:
  12404. case WASM_OP_I32_LOAD8_U:
  12405. case WASM_OP_I32_LOAD16_S:
  12406. case WASM_OP_I32_LOAD16_U:
  12407. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12408. break;
  12409. case WASM_OP_I64_LOAD:
  12410. case WASM_OP_I64_LOAD8_S:
  12411. case WASM_OP_I64_LOAD8_U:
  12412. case WASM_OP_I64_LOAD16_S:
  12413. case WASM_OP_I64_LOAD16_U:
  12414. case WASM_OP_I64_LOAD32_S:
  12415. case WASM_OP_I64_LOAD32_U:
  12416. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12417. break;
  12418. case WASM_OP_F32_LOAD:
  12419. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12420. break;
  12421. case WASM_OP_F64_LOAD:
  12422. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12423. break;
  12424. /* store */
  12425. case WASM_OP_I32_STORE:
  12426. case WASM_OP_I32_STORE8:
  12427. case WASM_OP_I32_STORE16:
  12428. POP_I32();
  12429. POP_MEM_OFFSET();
  12430. break;
  12431. case WASM_OP_I64_STORE:
  12432. case WASM_OP_I64_STORE8:
  12433. case WASM_OP_I64_STORE16:
  12434. case WASM_OP_I64_STORE32:
  12435. POP_I64();
  12436. POP_MEM_OFFSET();
  12437. break;
  12438. case WASM_OP_F32_STORE:
  12439. POP_F32();
  12440. POP_MEM_OFFSET();
  12441. break;
  12442. case WASM_OP_F64_STORE:
  12443. POP_F64();
  12444. POP_MEM_OFFSET();
  12445. break;
  12446. default:
  12447. break;
  12448. }
  12449. break;
  12450. }
  12451. case WASM_OP_MEMORY_SIZE:
  12452. CHECK_MEMORY();
  12453. pb_read_leb_uint32(p, p_end, memidx);
  12454. check_memidx(module, memidx);
  12455. PUSH_PAGE_COUNT();
  12456. module->possible_memory_grow = true;
  12457. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12458. func->has_memory_operations = true;
  12459. #endif
  12460. break;
  12461. case WASM_OP_MEMORY_GROW:
  12462. CHECK_MEMORY();
  12463. pb_read_leb_uint32(p, p_end, memidx);
  12464. check_memidx(module, memidx);
  12465. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12466. module->possible_memory_grow = true;
  12467. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12468. || WASM_ENABLE_WAMR_COMPILER != 0
  12469. func->has_op_memory_grow = true;
  12470. #endif
  12471. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12472. func->has_memory_operations = true;
  12473. #endif
  12474. break;
  12475. case WASM_OP_I32_CONST:
  12476. pb_read_leb_int32(p, p_end, i32_const);
  12477. #if WASM_ENABLE_FAST_INTERP != 0
  12478. skip_label();
  12479. disable_emit = true;
  12480. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12481. if (operand_offset == 0) {
  12482. disable_emit = false;
  12483. emit_label(WASM_OP_I32_CONST);
  12484. emit_uint32(loader_ctx, i32_const);
  12485. }
  12486. #else
  12487. (void)i32_const;
  12488. #endif
  12489. PUSH_I32();
  12490. break;
  12491. case WASM_OP_I64_CONST:
  12492. pb_read_leb_int64(p, p_end, i64_const);
  12493. #if WASM_ENABLE_FAST_INTERP != 0
  12494. skip_label();
  12495. disable_emit = true;
  12496. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12497. if (operand_offset == 0) {
  12498. disable_emit = false;
  12499. emit_label(WASM_OP_I64_CONST);
  12500. emit_uint64(loader_ctx, i64_const);
  12501. }
  12502. #endif
  12503. PUSH_I64();
  12504. break;
  12505. case WASM_OP_F32_CONST:
  12506. CHECK_BUF(p, p_end, sizeof(float32));
  12507. p += sizeof(float32);
  12508. #if WASM_ENABLE_FAST_INTERP != 0
  12509. skip_label();
  12510. disable_emit = true;
  12511. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12512. sizeof(float32));
  12513. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12514. if (operand_offset == 0) {
  12515. disable_emit = false;
  12516. emit_label(WASM_OP_F32_CONST);
  12517. emit_float32(loader_ctx, f32_const);
  12518. }
  12519. #endif
  12520. PUSH_F32();
  12521. break;
  12522. case WASM_OP_F64_CONST:
  12523. CHECK_BUF(p, p_end, sizeof(float64));
  12524. p += sizeof(float64);
  12525. #if WASM_ENABLE_FAST_INTERP != 0
  12526. skip_label();
  12527. disable_emit = true;
  12528. /* Some MCU may require 8-byte align */
  12529. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12530. sizeof(float64));
  12531. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12532. if (operand_offset == 0) {
  12533. disable_emit = false;
  12534. emit_label(WASM_OP_F64_CONST);
  12535. emit_float64(loader_ctx, f64_const);
  12536. }
  12537. #endif
  12538. PUSH_F64();
  12539. break;
  12540. case WASM_OP_I32_EQZ:
  12541. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12542. break;
  12543. case WASM_OP_I32_EQ:
  12544. case WASM_OP_I32_NE:
  12545. case WASM_OP_I32_LT_S:
  12546. case WASM_OP_I32_LT_U:
  12547. case WASM_OP_I32_GT_S:
  12548. case WASM_OP_I32_GT_U:
  12549. case WASM_OP_I32_LE_S:
  12550. case WASM_OP_I32_LE_U:
  12551. case WASM_OP_I32_GE_S:
  12552. case WASM_OP_I32_GE_U:
  12553. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12554. break;
  12555. case WASM_OP_I64_EQZ:
  12556. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12557. break;
  12558. case WASM_OP_I64_EQ:
  12559. case WASM_OP_I64_NE:
  12560. case WASM_OP_I64_LT_S:
  12561. case WASM_OP_I64_LT_U:
  12562. case WASM_OP_I64_GT_S:
  12563. case WASM_OP_I64_GT_U:
  12564. case WASM_OP_I64_LE_S:
  12565. case WASM_OP_I64_LE_U:
  12566. case WASM_OP_I64_GE_S:
  12567. case WASM_OP_I64_GE_U:
  12568. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12569. break;
  12570. case WASM_OP_F32_EQ:
  12571. case WASM_OP_F32_NE:
  12572. case WASM_OP_F32_LT:
  12573. case WASM_OP_F32_GT:
  12574. case WASM_OP_F32_LE:
  12575. case WASM_OP_F32_GE:
  12576. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12577. break;
  12578. case WASM_OP_F64_EQ:
  12579. case WASM_OP_F64_NE:
  12580. case WASM_OP_F64_LT:
  12581. case WASM_OP_F64_GT:
  12582. case WASM_OP_F64_LE:
  12583. case WASM_OP_F64_GE:
  12584. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12585. break;
  12586. case WASM_OP_I32_CLZ:
  12587. case WASM_OP_I32_CTZ:
  12588. case WASM_OP_I32_POPCNT:
  12589. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12590. break;
  12591. case WASM_OP_I32_ADD:
  12592. case WASM_OP_I32_SUB:
  12593. case WASM_OP_I32_MUL:
  12594. case WASM_OP_I32_DIV_S:
  12595. case WASM_OP_I32_DIV_U:
  12596. case WASM_OP_I32_REM_S:
  12597. case WASM_OP_I32_REM_U:
  12598. case WASM_OP_I32_AND:
  12599. case WASM_OP_I32_OR:
  12600. case WASM_OP_I32_XOR:
  12601. case WASM_OP_I32_SHL:
  12602. case WASM_OP_I32_SHR_S:
  12603. case WASM_OP_I32_SHR_U:
  12604. case WASM_OP_I32_ROTL:
  12605. case WASM_OP_I32_ROTR:
  12606. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12607. break;
  12608. case WASM_OP_I64_CLZ:
  12609. case WASM_OP_I64_CTZ:
  12610. case WASM_OP_I64_POPCNT:
  12611. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12612. break;
  12613. case WASM_OP_I64_ADD:
  12614. case WASM_OP_I64_SUB:
  12615. case WASM_OP_I64_MUL:
  12616. case WASM_OP_I64_DIV_S:
  12617. case WASM_OP_I64_DIV_U:
  12618. case WASM_OP_I64_REM_S:
  12619. case WASM_OP_I64_REM_U:
  12620. case WASM_OP_I64_AND:
  12621. case WASM_OP_I64_OR:
  12622. case WASM_OP_I64_XOR:
  12623. case WASM_OP_I64_SHL:
  12624. case WASM_OP_I64_SHR_S:
  12625. case WASM_OP_I64_SHR_U:
  12626. case WASM_OP_I64_ROTL:
  12627. case WASM_OP_I64_ROTR:
  12628. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12629. break;
  12630. case WASM_OP_F32_ABS:
  12631. case WASM_OP_F32_NEG:
  12632. case WASM_OP_F32_CEIL:
  12633. case WASM_OP_F32_FLOOR:
  12634. case WASM_OP_F32_TRUNC:
  12635. case WASM_OP_F32_NEAREST:
  12636. case WASM_OP_F32_SQRT:
  12637. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12638. break;
  12639. case WASM_OP_F32_ADD:
  12640. case WASM_OP_F32_SUB:
  12641. case WASM_OP_F32_MUL:
  12642. case WASM_OP_F32_DIV:
  12643. case WASM_OP_F32_MIN:
  12644. case WASM_OP_F32_MAX:
  12645. case WASM_OP_F32_COPYSIGN:
  12646. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12647. break;
  12648. case WASM_OP_F64_ABS:
  12649. case WASM_OP_F64_NEG:
  12650. case WASM_OP_F64_CEIL:
  12651. case WASM_OP_F64_FLOOR:
  12652. case WASM_OP_F64_TRUNC:
  12653. case WASM_OP_F64_NEAREST:
  12654. case WASM_OP_F64_SQRT:
  12655. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12656. break;
  12657. case WASM_OP_F64_ADD:
  12658. case WASM_OP_F64_SUB:
  12659. case WASM_OP_F64_MUL:
  12660. case WASM_OP_F64_DIV:
  12661. case WASM_OP_F64_MIN:
  12662. case WASM_OP_F64_MAX:
  12663. case WASM_OP_F64_COPYSIGN:
  12664. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12665. break;
  12666. case WASM_OP_I32_WRAP_I64:
  12667. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12668. break;
  12669. case WASM_OP_I32_TRUNC_S_F32:
  12670. case WASM_OP_I32_TRUNC_U_F32:
  12671. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12672. break;
  12673. case WASM_OP_I32_TRUNC_S_F64:
  12674. case WASM_OP_I32_TRUNC_U_F64:
  12675. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12676. break;
  12677. case WASM_OP_I64_EXTEND_S_I32:
  12678. case WASM_OP_I64_EXTEND_U_I32:
  12679. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12680. break;
  12681. case WASM_OP_I64_TRUNC_S_F32:
  12682. case WASM_OP_I64_TRUNC_U_F32:
  12683. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12684. break;
  12685. case WASM_OP_I64_TRUNC_S_F64:
  12686. case WASM_OP_I64_TRUNC_U_F64:
  12687. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12688. break;
  12689. case WASM_OP_F32_CONVERT_S_I32:
  12690. case WASM_OP_F32_CONVERT_U_I32:
  12691. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12692. break;
  12693. case WASM_OP_F32_CONVERT_S_I64:
  12694. case WASM_OP_F32_CONVERT_U_I64:
  12695. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12696. break;
  12697. case WASM_OP_F32_DEMOTE_F64:
  12698. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12699. break;
  12700. case WASM_OP_F64_CONVERT_S_I32:
  12701. case WASM_OP_F64_CONVERT_U_I32:
  12702. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12703. break;
  12704. case WASM_OP_F64_CONVERT_S_I64:
  12705. case WASM_OP_F64_CONVERT_U_I64:
  12706. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12707. break;
  12708. case WASM_OP_F64_PROMOTE_F32:
  12709. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12710. break;
  12711. case WASM_OP_I32_REINTERPRET_F32:
  12712. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12713. break;
  12714. case WASM_OP_I64_REINTERPRET_F64:
  12715. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12716. break;
  12717. case WASM_OP_F32_REINTERPRET_I32:
  12718. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12719. break;
  12720. case WASM_OP_F64_REINTERPRET_I64:
  12721. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12722. break;
  12723. case WASM_OP_I32_EXTEND8_S:
  12724. case WASM_OP_I32_EXTEND16_S:
  12725. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12726. break;
  12727. case WASM_OP_I64_EXTEND8_S:
  12728. case WASM_OP_I64_EXTEND16_S:
  12729. case WASM_OP_I64_EXTEND32_S:
  12730. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12731. break;
  12732. #if WASM_ENABLE_GC != 0
  12733. case WASM_OP_GC_PREFIX:
  12734. {
  12735. uint32 opcode1;
  12736. pb_read_leb_uint32(p, p_end, opcode1);
  12737. #if WASM_ENABLE_FAST_INTERP != 0
  12738. emit_byte(loader_ctx, ((uint8)opcode1));
  12739. #endif
  12740. switch (opcode1) {
  12741. case WASM_OP_STRUCT_NEW:
  12742. case WASM_OP_STRUCT_NEW_DEFAULT:
  12743. {
  12744. pb_read_leb_uint32(p, p_end, type_idx);
  12745. #if WASM_ENABLE_FAST_INTERP != 0
  12746. emit_uint32(loader_ctx, type_idx);
  12747. #endif
  12748. if (!check_type_index(module, module->type_count,
  12749. type_idx, error_buf,
  12750. error_buf_size)) {
  12751. goto fail;
  12752. }
  12753. if (module->types[type_idx]->type_flag
  12754. != WASM_TYPE_STRUCT) {
  12755. set_error_buf(error_buf, error_buf_size,
  12756. "unknown struct type");
  12757. goto fail;
  12758. }
  12759. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12760. int32 j, k;
  12761. uint8 value_type;
  12762. uint32 ref_type_struct_size;
  12763. WASMStructType *struct_type =
  12764. (WASMStructType *)module->types[type_idx];
  12765. k = struct_type->ref_type_map_count - 1;
  12766. for (j = struct_type->field_count - 1; j >= 0;
  12767. j--) {
  12768. value_type = struct_type->fields[j].field_type;
  12769. if (wasm_is_type_reftype(value_type)) {
  12770. if (wasm_is_type_multi_byte_type(
  12771. value_type)) {
  12772. ref_type_struct_size =
  12773. wasm_reftype_struct_size(
  12774. struct_type->ref_type_maps[k]
  12775. .ref_type);
  12776. bh_memcpy_s(
  12777. &wasm_ref_type,
  12778. (uint32)sizeof(WASMRefType),
  12779. struct_type->ref_type_maps[k]
  12780. .ref_type,
  12781. ref_type_struct_size);
  12782. k--;
  12783. }
  12784. POP_REF(value_type);
  12785. }
  12786. else {
  12787. switch (value_type) {
  12788. case VALUE_TYPE_I32:
  12789. case PACKED_TYPE_I8:
  12790. case PACKED_TYPE_I16:
  12791. POP_I32();
  12792. break;
  12793. case VALUE_TYPE_I64:
  12794. POP_I64();
  12795. break;
  12796. case VALUE_TYPE_F32:
  12797. POP_F32();
  12798. break;
  12799. case VALUE_TYPE_F64:
  12800. POP_F64();
  12801. break;
  12802. default:
  12803. set_error_buf(error_buf,
  12804. error_buf_size,
  12805. "unknown type");
  12806. goto fail;
  12807. }
  12808. }
  12809. }
  12810. }
  12811. /* PUSH struct obj, (ref $t) */
  12812. wasm_set_refheaptype_typeidx(
  12813. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12814. PUSH_REF(wasm_ref_type.ref_type);
  12815. break;
  12816. }
  12817. case WASM_OP_STRUCT_GET:
  12818. case WASM_OP_STRUCT_GET_S:
  12819. case WASM_OP_STRUCT_GET_U:
  12820. case WASM_OP_STRUCT_SET:
  12821. {
  12822. WASMStructType *struct_type;
  12823. WASMRefType *ref_type = NULL;
  12824. uint32 field_idx;
  12825. uint8 field_type;
  12826. pb_read_leb_uint32(p, p_end, type_idx);
  12827. #if WASM_ENABLE_FAST_INTERP != 0
  12828. emit_uint32(loader_ctx, type_idx);
  12829. #endif
  12830. if (!check_type_index(module, module->type_count,
  12831. type_idx, error_buf,
  12832. error_buf_size)) {
  12833. goto fail;
  12834. }
  12835. if (module->types[type_idx]->type_flag
  12836. != WASM_TYPE_STRUCT) {
  12837. set_error_buf(error_buf, error_buf_size,
  12838. "unknown struct type");
  12839. goto fail;
  12840. }
  12841. struct_type = (WASMStructType *)module->types[type_idx];
  12842. pb_read_leb_uint32(p, p_end, field_idx);
  12843. #if WASM_ENABLE_FAST_INTERP != 0
  12844. emit_uint32(loader_ctx, field_idx);
  12845. #endif
  12846. if (field_idx >= struct_type->field_count) {
  12847. set_error_buf(error_buf, error_buf_size,
  12848. "unknown struct field");
  12849. goto fail;
  12850. }
  12851. if (opcode1 == WASM_OP_STRUCT_SET
  12852. && !(struct_type->fields[field_idx].field_flags
  12853. & 1)) {
  12854. set_error_buf(error_buf, error_buf_size,
  12855. "field is immutable");
  12856. goto fail;
  12857. }
  12858. field_type = struct_type->fields[field_idx].field_type;
  12859. if (is_packed_type(field_type)) {
  12860. if (opcode1 == WASM_OP_STRUCT_GET) {
  12861. set_error_buf(error_buf, error_buf_size,
  12862. "type mismatch");
  12863. goto fail;
  12864. }
  12865. else {
  12866. field_type = VALUE_TYPE_I32;
  12867. }
  12868. }
  12869. if (wasm_is_type_multi_byte_type(field_type)) {
  12870. ref_type = wasm_reftype_map_find(
  12871. struct_type->ref_type_maps,
  12872. struct_type->ref_type_map_count, field_idx);
  12873. bh_assert(ref_type);
  12874. }
  12875. if (opcode1 == WASM_OP_STRUCT_SET) {
  12876. /* POP field */
  12877. if (wasm_is_type_multi_byte_type(field_type)) {
  12878. bh_memcpy_s(&wasm_ref_type,
  12879. (uint32)sizeof(WASMRefType),
  12880. ref_type,
  12881. wasm_reftype_struct_size(ref_type));
  12882. }
  12883. POP_REF(field_type);
  12884. /* POP struct obj, (ref null $t) */
  12885. wasm_set_refheaptype_typeidx(
  12886. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12887. POP_REF(wasm_ref_type.ref_type);
  12888. }
  12889. else {
  12890. /* POP struct obj, (ref null $t) */
  12891. wasm_set_refheaptype_typeidx(
  12892. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12893. POP_REF(wasm_ref_type.ref_type);
  12894. /* PUSH field */
  12895. if (wasm_is_type_multi_byte_type(field_type)) {
  12896. bh_memcpy_s(&wasm_ref_type,
  12897. (uint32)sizeof(WASMRefType),
  12898. ref_type,
  12899. wasm_reftype_struct_size(ref_type));
  12900. }
  12901. PUSH_REF(field_type);
  12902. }
  12903. break;
  12904. }
  12905. case WASM_OP_ARRAY_NEW:
  12906. case WASM_OP_ARRAY_NEW_DEFAULT:
  12907. case WASM_OP_ARRAY_NEW_FIXED:
  12908. case WASM_OP_ARRAY_NEW_DATA:
  12909. case WASM_OP_ARRAY_NEW_ELEM:
  12910. {
  12911. WASMArrayType *array_type;
  12912. uint8 elem_type;
  12913. uint32 u32 = 0;
  12914. pb_read_leb_uint32(p, p_end, type_idx);
  12915. #if WASM_ENABLE_FAST_INTERP != 0
  12916. emit_uint32(loader_ctx, type_idx);
  12917. #endif
  12918. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12919. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12920. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12921. pb_read_leb_uint32(p, p_end, u32);
  12922. #if WASM_ENABLE_FAST_INTERP != 0
  12923. emit_uint32(loader_ctx, u32);
  12924. #endif
  12925. }
  12926. if (!check_array_type(module, type_idx, error_buf,
  12927. error_buf_size)) {
  12928. goto fail;
  12929. }
  12930. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12931. /* length */
  12932. POP_I32();
  12933. }
  12934. array_type = (WASMArrayType *)module->types[type_idx];
  12935. elem_type = array_type->elem_type;
  12936. if (opcode1 == WASM_OP_ARRAY_NEW
  12937. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12938. if (wasm_is_type_multi_byte_type(elem_type)) {
  12939. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12940. array_type->elem_ref_type,
  12941. wasm_reftype_struct_size(
  12942. array_type->elem_ref_type));
  12943. }
  12944. if (is_packed_type(elem_type)) {
  12945. elem_type = VALUE_TYPE_I32;
  12946. }
  12947. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12948. uint32 N = u32;
  12949. for (i = 0; i < N; i++) {
  12950. if (wasm_is_type_multi_byte_type(
  12951. elem_type)) {
  12952. bh_memcpy_s(
  12953. &wasm_ref_type, sizeof(WASMRefType),
  12954. array_type->elem_ref_type,
  12955. wasm_reftype_struct_size(
  12956. array_type->elem_ref_type));
  12957. }
  12958. POP_REF(elem_type);
  12959. }
  12960. }
  12961. else
  12962. POP_REF(elem_type);
  12963. }
  12964. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12965. /* offset of data segment */
  12966. POP_I32();
  12967. if (u32 >= module->data_seg_count) {
  12968. set_error_buf(error_buf, error_buf_size,
  12969. "unknown data segment");
  12970. goto fail;
  12971. }
  12972. if (wasm_is_type_reftype(elem_type)) {
  12973. set_error_buf(error_buf, error_buf_size,
  12974. "array elem type mismatch");
  12975. goto fail;
  12976. }
  12977. }
  12978. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12979. WASMTableSeg *table_seg =
  12980. module->table_segments + u32;
  12981. /* offset of element segment */
  12982. POP_I32();
  12983. if (u32 >= module->table_seg_count) {
  12984. set_error_buf(error_buf, error_buf_size,
  12985. "unknown element segment");
  12986. goto fail;
  12987. }
  12988. if (!wasm_reftype_is_subtype_of(
  12989. table_seg->elem_type,
  12990. table_seg->elem_ref_type, elem_type,
  12991. array_type->elem_ref_type, module->types,
  12992. module->type_count)) {
  12993. set_error_buf(error_buf, error_buf_size,
  12994. "array elem type mismatch");
  12995. goto fail;
  12996. }
  12997. }
  12998. /* PUSH array obj, (ref $t) */
  12999. wasm_set_refheaptype_typeidx(
  13000. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  13001. PUSH_REF(wasm_ref_type.ref_type);
  13002. break;
  13003. }
  13004. case WASM_OP_ARRAY_GET:
  13005. case WASM_OP_ARRAY_GET_S:
  13006. case WASM_OP_ARRAY_GET_U:
  13007. case WASM_OP_ARRAY_SET:
  13008. {
  13009. uint8 elem_type;
  13010. WASMArrayType *array_type;
  13011. WASMRefType *ref_type = NULL;
  13012. pb_read_leb_uint32(p, p_end, type_idx);
  13013. #if WASM_ENABLE_FAST_INTERP != 0
  13014. emit_uint32(loader_ctx, type_idx);
  13015. #endif
  13016. if (!check_array_type(module, type_idx, error_buf,
  13017. error_buf_size)) {
  13018. goto fail;
  13019. }
  13020. array_type = (WASMArrayType *)module->types[type_idx];
  13021. if (opcode1 == WASM_OP_ARRAY_SET
  13022. && !(array_type->elem_flags & 1)) {
  13023. set_error_buf(error_buf, error_buf_size,
  13024. "array is immutable");
  13025. goto fail;
  13026. }
  13027. elem_type = array_type->elem_type;
  13028. if (is_packed_type(elem_type)) {
  13029. if (opcode1 != WASM_OP_ARRAY_GET_S
  13030. && opcode1 != WASM_OP_ARRAY_GET_U
  13031. && opcode1 != WASM_OP_ARRAY_SET) {
  13032. set_error_buf(error_buf, error_buf_size,
  13033. "type mismatch");
  13034. goto fail;
  13035. }
  13036. else {
  13037. elem_type = VALUE_TYPE_I32;
  13038. }
  13039. }
  13040. ref_type = array_type->elem_ref_type;
  13041. if (opcode1 == WASM_OP_ARRAY_SET) {
  13042. /* POP elem to set */
  13043. if (wasm_is_type_multi_byte_type(elem_type)) {
  13044. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13045. ref_type,
  13046. wasm_reftype_struct_size(ref_type));
  13047. }
  13048. POP_REF(elem_type);
  13049. }
  13050. /* elem idx */
  13051. POP_I32();
  13052. /* POP array obj, (ref null $t) */
  13053. wasm_set_refheaptype_typeidx(
  13054. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13055. POP_REF(wasm_ref_type.ref_type);
  13056. if (opcode1 != WASM_OP_ARRAY_SET) {
  13057. /* PUSH elem */
  13058. if (wasm_is_type_multi_byte_type(elem_type)) {
  13059. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13060. ref_type,
  13061. wasm_reftype_struct_size(ref_type));
  13062. }
  13063. PUSH_REF(elem_type);
  13064. }
  13065. break;
  13066. }
  13067. case WASM_OP_ARRAY_LEN:
  13068. {
  13069. POP_REF(REF_TYPE_ARRAYREF);
  13070. /* length */
  13071. PUSH_I32();
  13072. break;
  13073. }
  13074. case WASM_OP_ARRAY_FILL:
  13075. {
  13076. WASMArrayType *array_type;
  13077. uint8 elem_type;
  13078. /* typeidx */
  13079. pb_read_leb_uint32(p, p_end, type_idx);
  13080. #if WASM_ENABLE_FAST_INTERP != 0
  13081. emit_uint32(loader_ctx, type_idx);
  13082. #endif
  13083. if (!check_array_type(module, type_idx, error_buf,
  13084. error_buf_size)) {
  13085. goto fail;
  13086. }
  13087. array_type = (WASMArrayType *)module->types[type_idx];
  13088. if (!(array_type->elem_flags & 1)) {
  13089. set_error_buf(error_buf, error_buf_size,
  13090. "array is immutable");
  13091. goto fail;
  13092. }
  13093. elem_type = array_type->elem_type;
  13094. if (is_packed_type(elem_type)) {
  13095. elem_type = VALUE_TYPE_I32;
  13096. }
  13097. POP_I32(); /* length */
  13098. #if WASM_ENABLE_FAST_INTERP != 0
  13099. POP_OFFSET_TYPE(elem_type);
  13100. #endif
  13101. POP_TYPE(elem_type);
  13102. POP_I32(); /* start */
  13103. /* POP array obj, (ref null $t) */
  13104. wasm_set_refheaptype_typeidx(
  13105. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13106. POP_REF(wasm_ref_type.ref_type);
  13107. break;
  13108. }
  13109. case WASM_OP_ARRAY_COPY:
  13110. {
  13111. uint32 src_type_idx;
  13112. uint8 src_elem_type, dst_elem_type;
  13113. WASMRefType src_ref_type = { 0 },
  13114. *src_elem_ref_type = NULL;
  13115. WASMRefType dst_ref_type = { 0 },
  13116. *dst_elem_ref_type = NULL;
  13117. WASMArrayType *array_type;
  13118. /* typeidx1 */
  13119. pb_read_leb_uint32(p, p_end, type_idx);
  13120. #if WASM_ENABLE_FAST_INTERP != 0
  13121. emit_uint32(loader_ctx, type_idx);
  13122. #endif
  13123. /* typeidx2 */
  13124. pb_read_leb_uint32(p, p_end, src_type_idx);
  13125. #if WASM_ENABLE_FAST_INTERP != 0
  13126. emit_uint32(loader_ctx, src_type_idx);
  13127. #endif
  13128. if (!check_array_type(module, type_idx, error_buf,
  13129. error_buf_size)) {
  13130. goto fail;
  13131. }
  13132. if (!check_array_type(module, src_type_idx, error_buf,
  13133. error_buf_size)) {
  13134. goto fail;
  13135. }
  13136. POP_I32();
  13137. POP_I32();
  13138. /* POP array obj, (ref null $t) */
  13139. wasm_set_refheaptype_typeidx(
  13140. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13141. POP_REF(wasm_ref_type.ref_type);
  13142. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13143. &wasm_ref_type,
  13144. wasm_reftype_struct_size(&wasm_ref_type));
  13145. POP_I32();
  13146. /* POP array obj, (ref null $t) */
  13147. wasm_set_refheaptype_typeidx(
  13148. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13149. POP_REF(wasm_ref_type.ref_type);
  13150. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13151. &wasm_ref_type,
  13152. wasm_reftype_struct_size(&wasm_ref_type));
  13153. array_type = (WASMArrayType *)module->types[type_idx];
  13154. if (!(array_type->elem_flags & 1)) {
  13155. set_error_buf(error_buf, error_buf_size,
  13156. "destination array is immutable");
  13157. goto fail;
  13158. }
  13159. dst_elem_type = array_type->elem_type;
  13160. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13161. dst_elem_ref_type = array_type->elem_ref_type;
  13162. }
  13163. array_type =
  13164. (WASMArrayType *)module->types[src_type_idx];
  13165. src_elem_type = array_type->elem_type;
  13166. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13167. src_elem_ref_type = array_type->elem_ref_type;
  13168. }
  13169. if (!wasm_reftype_is_subtype_of(
  13170. src_elem_type, src_elem_ref_type, dst_elem_type,
  13171. dst_elem_ref_type, module->types,
  13172. module->type_count)) {
  13173. set_error_buf(error_buf, error_buf_size,
  13174. "array types do not match");
  13175. goto fail;
  13176. }
  13177. break;
  13178. }
  13179. case WASM_OP_REF_I31:
  13180. {
  13181. POP_I32();
  13182. wasm_set_refheaptype_common(
  13183. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13184. PUSH_REF(wasm_ref_type.ref_type);
  13185. break;
  13186. }
  13187. case WASM_OP_I31_GET_S:
  13188. case WASM_OP_I31_GET_U:
  13189. {
  13190. POP_REF(REF_TYPE_I31REF);
  13191. PUSH_I32();
  13192. break;
  13193. }
  13194. case WASM_OP_REF_TEST:
  13195. case WASM_OP_REF_CAST:
  13196. case WASM_OP_REF_TEST_NULLABLE:
  13197. case WASM_OP_REF_CAST_NULLABLE:
  13198. {
  13199. uint8 type;
  13200. pb_read_leb_int32(p, p_end, heap_type);
  13201. #if WASM_ENABLE_FAST_INTERP != 0
  13202. emit_uint32(loader_ctx, (uint32)heap_type);
  13203. #endif
  13204. if (heap_type >= 0) {
  13205. if (!check_type_index(module, module->type_count,
  13206. heap_type, error_buf,
  13207. error_buf_size)) {
  13208. goto fail;
  13209. }
  13210. }
  13211. else {
  13212. if (!wasm_is_valid_heap_type(heap_type)) {
  13213. set_error_buf(error_buf, error_buf_size,
  13214. "unknown type");
  13215. goto fail;
  13216. }
  13217. }
  13218. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13219. &wasm_ref_type, error_buf,
  13220. error_buf_size)) {
  13221. goto fail;
  13222. }
  13223. if (opcode1 == WASM_OP_REF_TEST
  13224. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13225. PUSH_I32();
  13226. else {
  13227. bool nullable =
  13228. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13229. : false;
  13230. if (heap_type >= 0 || !nullable) {
  13231. wasm_set_refheaptype_typeidx(
  13232. &wasm_ref_type.ref_ht_typeidx, nullable,
  13233. heap_type);
  13234. PUSH_REF(wasm_ref_type.ref_type);
  13235. }
  13236. else {
  13237. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13238. }
  13239. }
  13240. break;
  13241. }
  13242. case WASM_OP_BR_ON_CAST:
  13243. case WASM_OP_BR_ON_CAST_FAIL:
  13244. {
  13245. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13246. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13247. uint8 type_tmp, castflags;
  13248. uint32 depth;
  13249. int32 heap_type_dst;
  13250. bool src_nullable, dst_nullable;
  13251. CHECK_BUF(p, p_end, 1);
  13252. castflags = read_uint8(p);
  13253. #if WASM_ENABLE_FAST_INTERP != 0
  13254. /* Emit heap_type firstly */
  13255. emit_byte(loader_ctx, castflags);
  13256. #endif
  13257. p_org = p;
  13258. pb_read_leb_uint32(p, p_end, depth);
  13259. pb_read_leb_int32(p, p_end, heap_type);
  13260. #if WASM_ENABLE_FAST_INTERP != 0
  13261. /* Emit heap_type firstly */
  13262. emit_uint32(loader_ctx, (uint32)heap_type);
  13263. #endif
  13264. pb_read_leb_int32(p, p_end, heap_type_dst);
  13265. #if WASM_ENABLE_FAST_INTERP != 0
  13266. /* Emit heap_type firstly */
  13267. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13268. #endif
  13269. (void)depth;
  13270. /*
  13271. * castflags should be 0~3:
  13272. * 0: (non-null, non-null)
  13273. * 1: (null, non-null)
  13274. * 2: (non-null, null)
  13275. * 3: (null, null)
  13276. */
  13277. if (castflags > 3) {
  13278. set_error_buf(error_buf, error_buf_size,
  13279. "invalid castflags");
  13280. break;
  13281. }
  13282. src_nullable =
  13283. (castflags == 1) || (castflags == 3) ? true : false;
  13284. dst_nullable =
  13285. (castflags == 2) || (castflags == 3) ? true : false;
  13286. /* Pop and backup the stack top's ref type */
  13287. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13288. &ref_type_tmp, error_buf,
  13289. error_buf_size)) {
  13290. goto fail;
  13291. }
  13292. /* The reference type rt1 must be valid */
  13293. if (!init_ref_type(module, &ref_type1, src_nullable,
  13294. heap_type, error_buf,
  13295. error_buf_size)) {
  13296. goto fail;
  13297. }
  13298. /* The reference type rt2 must be valid. */
  13299. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13300. heap_type_dst, error_buf,
  13301. error_buf_size)) {
  13302. goto fail;
  13303. }
  13304. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13305. &ref_type2);
  13306. /* The reference type rt2 must match rt1. */
  13307. if (!wasm_reftype_is_subtype_of(
  13308. ref_type2.ref_type, &ref_type2,
  13309. ref_type1.ref_type, &ref_type1, module->types,
  13310. module->type_count)) {
  13311. set_error_buf(error_buf, error_buf_size,
  13312. "type mismatch");
  13313. goto fail;
  13314. }
  13315. p = p_org;
  13316. /* Push ref type casted for branch block check */
  13317. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13318. /* The reference type rt2 must match rt′. */
  13319. type_tmp = ref_type2.ref_type;
  13320. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13321. bh_memcpy_s(
  13322. &wasm_ref_type,
  13323. wasm_reftype_struct_size(&ref_type2),
  13324. &ref_type2,
  13325. wasm_reftype_struct_size(&ref_type2));
  13326. }
  13327. }
  13328. else {
  13329. /* The reference type rt′1 must match rt′. */
  13330. type_tmp = ref_type_diff.ref_type;
  13331. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13332. bh_memcpy_s(
  13333. &wasm_ref_type,
  13334. wasm_reftype_struct_size(&ref_type_diff),
  13335. &ref_type_diff,
  13336. wasm_reftype_struct_size(&ref_type_diff));
  13337. }
  13338. }
  13339. PUSH_REF(type_tmp);
  13340. if (!(frame_csp_tmp = check_branch_block(
  13341. loader_ctx, &p, p_end, opcode, error_buf,
  13342. error_buf_size))) {
  13343. goto fail;
  13344. }
  13345. /* Ignore heap_types */
  13346. skip_leb_uint32(p, p_end);
  13347. skip_leb_uint32(p, p_end);
  13348. /* Restore the original stack top's ref type */
  13349. POP_REF(type_tmp);
  13350. #if WASM_ENABLE_FAST_INTERP != 0
  13351. /* Erase the opnd offset emitted by POP_REF() */
  13352. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13353. #endif
  13354. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13355. type_tmp = ref_type_diff.ref_type;
  13356. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13357. bh_memcpy_s(
  13358. &wasm_ref_type,
  13359. wasm_reftype_struct_size(&ref_type_diff),
  13360. &ref_type_diff,
  13361. wasm_reftype_struct_size(&ref_type_diff));
  13362. }
  13363. }
  13364. else {
  13365. type_tmp = ref_type2.ref_type;
  13366. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13367. bh_memcpy_s(
  13368. &wasm_ref_type,
  13369. wasm_reftype_struct_size(&ref_type2),
  13370. &ref_type2,
  13371. wasm_reftype_struct_size(&ref_type2));
  13372. }
  13373. }
  13374. PUSH_REF(type_tmp);
  13375. #if WASM_ENABLE_FAST_INTERP != 0
  13376. /* Erase the opnd offset emitted by PUSH_REF() */
  13377. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13378. #endif
  13379. break;
  13380. }
  13381. case WASM_OP_ANY_CONVERT_EXTERN:
  13382. {
  13383. uint8 type;
  13384. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13385. &wasm_ref_type, error_buf,
  13386. error_buf_size)) {
  13387. goto fail;
  13388. }
  13389. if (!(type == REF_TYPE_EXTERNREF
  13390. || (type == REF_TYPE_HT_NON_NULLABLE
  13391. && wasm_ref_type.ref_ht_common.heap_type
  13392. == HEAP_TYPE_EXTERN)
  13393. || type == VALUE_TYPE_ANY)) {
  13394. set_error_buf(error_buf, error_buf_size,
  13395. "type mismatch");
  13396. goto fail;
  13397. }
  13398. if (type == REF_TYPE_EXTERNREF)
  13399. type = REF_TYPE_ANYREF;
  13400. else {
  13401. wasm_ref_type.ref_ht_common.heap_type =
  13402. HEAP_TYPE_ANY;
  13403. }
  13404. PUSH_REF(type);
  13405. break;
  13406. }
  13407. case WASM_OP_EXTERN_CONVERT_ANY:
  13408. {
  13409. uint8 type;
  13410. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13411. &wasm_ref_type, error_buf,
  13412. error_buf_size)) {
  13413. goto fail;
  13414. }
  13415. if (type == REF_TYPE_EXTERNREF
  13416. || ((type == REF_TYPE_HT_NULLABLE
  13417. || type == REF_TYPE_HT_NON_NULLABLE)
  13418. && wasm_ref_type.ref_ht_common.heap_type
  13419. == HEAP_TYPE_EXTERN)) {
  13420. set_error_buf(error_buf, error_buf_size,
  13421. "type mismatch");
  13422. goto fail;
  13423. }
  13424. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13425. /* push (ref null extern) */
  13426. type = REF_TYPE_EXTERNREF;
  13427. }
  13428. else {
  13429. /* push (ref extern) */
  13430. type = REF_TYPE_HT_NON_NULLABLE;
  13431. wasm_set_refheaptype_common(
  13432. &wasm_ref_type.ref_ht_common, false,
  13433. HEAP_TYPE_EXTERN);
  13434. }
  13435. PUSH_REF(type);
  13436. break;
  13437. }
  13438. #if WASM_ENABLE_STRINGREF != 0
  13439. case WASM_OP_STRING_NEW_UTF8:
  13440. case WASM_OP_STRING_NEW_WTF16:
  13441. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13442. case WASM_OP_STRING_NEW_WTF8:
  13443. {
  13444. uint32 memidx;
  13445. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13446. func->has_memory_operations = true;
  13447. #endif
  13448. pb_read_leb_uint32(p, p_end, memidx);
  13449. #if WASM_ENABLE_FAST_INTERP != 0
  13450. emit_uint32(loader_ctx, (uint32)memidx);
  13451. #endif
  13452. POP_I32();
  13453. POP_I32();
  13454. PUSH_REF(REF_TYPE_STRINGREF);
  13455. (void)memidx;
  13456. break;
  13457. }
  13458. case WASM_OP_STRING_CONST:
  13459. {
  13460. uint32 contents;
  13461. pb_read_leb_uint32(p, p_end, contents);
  13462. #if WASM_ENABLE_FAST_INTERP != 0
  13463. emit_uint32(loader_ctx, (uint32)contents);
  13464. #endif
  13465. PUSH_REF(REF_TYPE_STRINGREF);
  13466. (void)contents;
  13467. break;
  13468. }
  13469. case WASM_OP_STRING_MEASURE_UTF8:
  13470. case WASM_OP_STRING_MEASURE_WTF8:
  13471. case WASM_OP_STRING_MEASURE_WTF16:
  13472. {
  13473. POP_STRINGREF();
  13474. PUSH_I32();
  13475. break;
  13476. }
  13477. case WASM_OP_STRING_ENCODE_UTF8:
  13478. case WASM_OP_STRING_ENCODE_WTF16:
  13479. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13480. case WASM_OP_STRING_ENCODE_WTF8:
  13481. {
  13482. uint32 memidx;
  13483. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13484. func->has_memory_operations = true;
  13485. #endif
  13486. pb_read_leb_uint32(p, p_end, memidx);
  13487. #if WASM_ENABLE_FAST_INTERP != 0
  13488. emit_uint32(loader_ctx, (uint32)memidx);
  13489. #endif
  13490. POP_I32();
  13491. POP_STRINGREF();
  13492. PUSH_I32();
  13493. (void)memidx;
  13494. break;
  13495. }
  13496. case WASM_OP_STRING_CONCAT:
  13497. {
  13498. POP_STRINGREF();
  13499. POP_STRINGREF();
  13500. PUSH_REF(REF_TYPE_STRINGREF);
  13501. break;
  13502. }
  13503. case WASM_OP_STRING_EQ:
  13504. {
  13505. POP_STRINGREF();
  13506. POP_STRINGREF();
  13507. PUSH_I32();
  13508. break;
  13509. }
  13510. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13511. {
  13512. POP_STRINGREF();
  13513. PUSH_I32();
  13514. break;
  13515. }
  13516. case WASM_OP_STRING_AS_WTF8:
  13517. {
  13518. POP_STRINGREF();
  13519. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13520. break;
  13521. }
  13522. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13523. {
  13524. POP_I32();
  13525. POP_I32();
  13526. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13527. PUSH_I32();
  13528. break;
  13529. }
  13530. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13531. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13532. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13533. {
  13534. uint32 memidx;
  13535. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13536. func->has_memory_operations = true;
  13537. #endif
  13538. pb_read_leb_uint32(p, p_end, memidx);
  13539. #if WASM_ENABLE_FAST_INTERP != 0
  13540. emit_uint32(loader_ctx, (uint32)memidx);
  13541. #endif
  13542. POP_I32();
  13543. POP_I32();
  13544. POP_I32();
  13545. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13546. PUSH_I32();
  13547. PUSH_I32();
  13548. (void)memidx;
  13549. break;
  13550. }
  13551. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13552. {
  13553. POP_I32();
  13554. POP_I32();
  13555. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13556. PUSH_REF(REF_TYPE_STRINGREF);
  13557. break;
  13558. }
  13559. case WASM_OP_STRING_AS_WTF16:
  13560. {
  13561. POP_STRINGREF();
  13562. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13563. break;
  13564. }
  13565. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13566. {
  13567. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13568. PUSH_I32();
  13569. break;
  13570. }
  13571. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13572. {
  13573. POP_I32();
  13574. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13575. PUSH_I32();
  13576. break;
  13577. }
  13578. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13579. {
  13580. uint32 memidx;
  13581. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13582. func->has_memory_operations = true;
  13583. #endif
  13584. pb_read_leb_uint32(p, p_end, memidx);
  13585. #if WASM_ENABLE_FAST_INTERP != 0
  13586. emit_uint32(loader_ctx, (uint32)memidx);
  13587. #endif
  13588. POP_I32();
  13589. POP_I32();
  13590. POP_I32();
  13591. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13592. PUSH_I32();
  13593. (void)memidx;
  13594. break;
  13595. }
  13596. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13597. {
  13598. POP_I32();
  13599. POP_I32();
  13600. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13601. PUSH_REF(REF_TYPE_STRINGREF);
  13602. break;
  13603. }
  13604. case WASM_OP_STRING_AS_ITER:
  13605. {
  13606. POP_STRINGREF();
  13607. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13608. break;
  13609. }
  13610. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13611. {
  13612. POP_REF(REF_TYPE_STRINGVIEWITER);
  13613. PUSH_I32();
  13614. break;
  13615. }
  13616. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13617. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13618. {
  13619. POP_I32();
  13620. POP_REF(REF_TYPE_STRINGVIEWITER);
  13621. PUSH_I32();
  13622. break;
  13623. }
  13624. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13625. {
  13626. POP_I32();
  13627. POP_REF(REF_TYPE_STRINGVIEWITER);
  13628. PUSH_REF(REF_TYPE_STRINGREF);
  13629. break;
  13630. }
  13631. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13632. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13633. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13634. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13635. {
  13636. POP_I32();
  13637. POP_I32();
  13638. POP_REF(REF_TYPE_ARRAYREF);
  13639. PUSH_REF(REF_TYPE_STRINGREF);
  13640. break;
  13641. }
  13642. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13643. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13644. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13645. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13646. {
  13647. POP_I32();
  13648. POP_REF(REF_TYPE_ARRAYREF);
  13649. POP_STRINGREF();
  13650. PUSH_I32();
  13651. break;
  13652. }
  13653. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13654. default:
  13655. set_error_buf_v(error_buf, error_buf_size,
  13656. "%s %02x %02x", "unsupported opcode",
  13657. 0xfb, opcode1);
  13658. goto fail;
  13659. }
  13660. break;
  13661. }
  13662. #endif /* end of WASM_ENABLE_GC != 0 */
  13663. case WASM_OP_MISC_PREFIX:
  13664. {
  13665. uint32 opcode1;
  13666. pb_read_leb_uint32(p, p_end, opcode1);
  13667. #if WASM_ENABLE_FAST_INTERP != 0
  13668. emit_byte(loader_ctx, ((uint8)opcode1));
  13669. #endif
  13670. switch (opcode1) {
  13671. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13672. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13673. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13674. break;
  13675. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13676. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13677. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13678. break;
  13679. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13680. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13681. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13682. break;
  13683. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13684. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13685. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13686. break;
  13687. #if WASM_ENABLE_BULK_MEMORY != 0
  13688. case WASM_OP_MEMORY_INIT:
  13689. {
  13690. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13691. #if WASM_ENABLE_FAST_INTERP != 0
  13692. emit_uint32(loader_ctx, data_seg_idx);
  13693. #endif
  13694. if (module->import_memory_count == 0
  13695. && module->memory_count == 0)
  13696. goto fail_unknown_memory;
  13697. pb_read_leb_uint32(p, p_end, memidx);
  13698. check_memidx(module, memidx);
  13699. if (data_seg_idx >= module->data_seg_count) {
  13700. set_error_buf_v(error_buf, error_buf_size,
  13701. "unknown data segment %d",
  13702. data_seg_idx);
  13703. goto fail;
  13704. }
  13705. if (module->data_seg_count1 == 0)
  13706. goto fail_data_cnt_sec_require;
  13707. POP_I32();
  13708. POP_I32();
  13709. POP_MEM_OFFSET();
  13710. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13711. func->has_memory_operations = true;
  13712. #endif
  13713. #if WASM_ENABLE_WAMR_COMPILER != 0
  13714. module->is_bulk_memory_used = true;
  13715. #endif
  13716. break;
  13717. }
  13718. case WASM_OP_DATA_DROP:
  13719. {
  13720. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13721. #if WASM_ENABLE_FAST_INTERP != 0
  13722. emit_uint32(loader_ctx, data_seg_idx);
  13723. #endif
  13724. if (data_seg_idx >= module->data_seg_count) {
  13725. set_error_buf(error_buf, error_buf_size,
  13726. "unknown data segment");
  13727. goto fail;
  13728. }
  13729. if (module->data_seg_count1 == 0)
  13730. goto fail_data_cnt_sec_require;
  13731. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13732. func->has_memory_operations = true;
  13733. #endif
  13734. #if WASM_ENABLE_WAMR_COMPILER != 0
  13735. module->is_bulk_memory_used = true;
  13736. #endif
  13737. break;
  13738. }
  13739. case WASM_OP_MEMORY_COPY:
  13740. {
  13741. CHECK_BUF(p, p_end, sizeof(int16));
  13742. /* check both src and dst memory index */
  13743. pb_read_leb_uint32(p, p_end, memidx);
  13744. check_memidx(module, memidx);
  13745. pb_read_leb_uint32(p, p_end, memidx);
  13746. check_memidx(module, memidx);
  13747. if (module->import_memory_count == 0
  13748. && module->memory_count == 0)
  13749. goto fail_unknown_memory;
  13750. POP_MEM_OFFSET();
  13751. POP_MEM_OFFSET();
  13752. POP_MEM_OFFSET();
  13753. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13754. func->has_memory_operations = true;
  13755. #endif
  13756. #if WASM_ENABLE_WAMR_COMPILER != 0
  13757. module->is_bulk_memory_used = true;
  13758. #endif
  13759. break;
  13760. }
  13761. case WASM_OP_MEMORY_FILL:
  13762. {
  13763. pb_read_leb_uint32(p, p_end, memidx);
  13764. check_memidx(module, memidx);
  13765. if (module->import_memory_count == 0
  13766. && module->memory_count == 0) {
  13767. goto fail_unknown_memory;
  13768. }
  13769. POP_MEM_OFFSET();
  13770. POP_I32();
  13771. POP_MEM_OFFSET();
  13772. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13773. func->has_memory_operations = true;
  13774. #endif
  13775. #if WASM_ENABLE_WAMR_COMPILER != 0
  13776. module->is_bulk_memory_used = true;
  13777. #endif
  13778. break;
  13779. }
  13780. fail_unknown_memory:
  13781. set_error_buf(error_buf, error_buf_size,
  13782. "unknown memory 0");
  13783. goto fail;
  13784. fail_data_cnt_sec_require:
  13785. set_error_buf(error_buf, error_buf_size,
  13786. "data count section required");
  13787. goto fail;
  13788. #endif /* WASM_ENABLE_BULK_MEMORY */
  13789. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13790. case WASM_OP_TABLE_INIT:
  13791. {
  13792. uint8 seg_type = 0, tbl_type = 0;
  13793. #if WASM_ENABLE_GC != 0
  13794. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13795. #endif
  13796. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13797. pb_read_leb_uint32(p, p_end, table_idx);
  13798. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13799. #if WASM_ENABLE_GC != 0
  13800. (void **)&tbl_ref_type,
  13801. #else
  13802. NULL,
  13803. #endif
  13804. error_buf, error_buf_size))
  13805. goto fail;
  13806. if (!get_table_seg_elem_type(module, table_seg_idx,
  13807. &seg_type,
  13808. #if WASM_ENABLE_GC != 0
  13809. (void **)&seg_ref_type,
  13810. #else
  13811. NULL,
  13812. #endif
  13813. error_buf, error_buf_size))
  13814. goto fail;
  13815. #if WASM_ENABLE_GC == 0
  13816. if (seg_type != tbl_type) {
  13817. set_error_buf(error_buf, error_buf_size,
  13818. "type mismatch");
  13819. goto fail;
  13820. }
  13821. #else
  13822. if (!wasm_reftype_is_subtype_of(
  13823. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13824. module->types, module->type_count)) {
  13825. set_error_buf(error_buf, error_buf_size,
  13826. "type mismatch");
  13827. goto fail;
  13828. }
  13829. #endif
  13830. #if WASM_ENABLE_FAST_INTERP != 0
  13831. emit_uint32(loader_ctx, table_seg_idx);
  13832. emit_uint32(loader_ctx, table_idx);
  13833. #endif
  13834. POP_I32();
  13835. POP_I32();
  13836. #if WASM_ENABLE_MEMORY64 != 0
  13837. table_elem_idx_type = is_table_64bit(module, table_idx)
  13838. ? VALUE_TYPE_I64
  13839. : VALUE_TYPE_I32;
  13840. #endif
  13841. POP_TBL_ELEM_IDX();
  13842. #if WASM_ENABLE_WAMR_COMPILER != 0
  13843. module->is_ref_types_used = true;
  13844. #endif
  13845. break;
  13846. }
  13847. case WASM_OP_ELEM_DROP:
  13848. {
  13849. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13850. if (!get_table_seg_elem_type(module, table_seg_idx,
  13851. NULL, NULL, error_buf,
  13852. error_buf_size))
  13853. goto fail;
  13854. #if WASM_ENABLE_FAST_INTERP != 0
  13855. emit_uint32(loader_ctx, table_seg_idx);
  13856. #endif
  13857. #if WASM_ENABLE_WAMR_COMPILER != 0
  13858. module->is_ref_types_used = true;
  13859. #endif
  13860. break;
  13861. }
  13862. case WASM_OP_TABLE_COPY:
  13863. {
  13864. uint8 src_type, dst_type, src_tbl_idx_type,
  13865. dst_tbl_idx_type, min_tbl_idx_type;
  13866. #if WASM_ENABLE_GC != 0
  13867. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13868. #endif
  13869. uint32 src_tbl_idx, dst_tbl_idx;
  13870. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  13871. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13872. #if WASM_ENABLE_GC != 0
  13873. (void **)&dst_ref_type,
  13874. #else
  13875. NULL,
  13876. #endif
  13877. error_buf, error_buf_size))
  13878. goto fail;
  13879. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  13880. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13881. #if WASM_ENABLE_GC != 0
  13882. (void **)&src_ref_type,
  13883. #else
  13884. NULL,
  13885. #endif
  13886. error_buf, error_buf_size))
  13887. goto fail;
  13888. #if WASM_ENABLE_GC == 0
  13889. if (src_type != dst_type) {
  13890. set_error_buf(error_buf, error_buf_size,
  13891. "type mismatch");
  13892. goto fail;
  13893. }
  13894. #else
  13895. if (!wasm_reftype_is_subtype_of(
  13896. src_type, src_ref_type, dst_type, dst_ref_type,
  13897. module->types, module->type_count)) {
  13898. set_error_buf(error_buf, error_buf_size,
  13899. "type mismatch");
  13900. goto fail;
  13901. }
  13902. #endif
  13903. #if WASM_ENABLE_FAST_INTERP != 0
  13904. emit_uint32(loader_ctx, dst_tbl_idx);
  13905. emit_uint32(loader_ctx, src_tbl_idx);
  13906. #endif
  13907. #if WASM_ENABLE_MEMORY64 != 0
  13908. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13909. ? VALUE_TYPE_I64
  13910. : VALUE_TYPE_I32;
  13911. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13912. ? VALUE_TYPE_I64
  13913. : VALUE_TYPE_I32;
  13914. min_tbl_idx_type =
  13915. (src_tbl_idx_type == VALUE_TYPE_I32
  13916. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13917. ? VALUE_TYPE_I32
  13918. : VALUE_TYPE_I64;
  13919. #else
  13920. src_tbl_idx_type = VALUE_TYPE_I32;
  13921. dst_tbl_idx_type = VALUE_TYPE_I32;
  13922. min_tbl_idx_type = VALUE_TYPE_I32;
  13923. #endif
  13924. table_elem_idx_type = min_tbl_idx_type;
  13925. POP_TBL_ELEM_IDX();
  13926. table_elem_idx_type = src_tbl_idx_type;
  13927. POP_TBL_ELEM_IDX();
  13928. table_elem_idx_type = dst_tbl_idx_type;
  13929. POP_TBL_ELEM_IDX();
  13930. #if WASM_ENABLE_WAMR_COMPILER != 0
  13931. module->is_ref_types_used = true;
  13932. #endif
  13933. break;
  13934. }
  13935. case WASM_OP_TABLE_SIZE:
  13936. {
  13937. pb_read_leb_uint32(p, p_end, table_idx);
  13938. /* TODO: shall we create a new function to check
  13939. table idx instead of using below function? */
  13940. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13941. error_buf, error_buf_size))
  13942. goto fail;
  13943. #if WASM_ENABLE_FAST_INTERP != 0
  13944. emit_uint32(loader_ctx, table_idx);
  13945. #endif
  13946. #if WASM_ENABLE_MEMORY64 != 0
  13947. table_elem_idx_type = is_table_64bit(module, table_idx)
  13948. ? VALUE_TYPE_I64
  13949. : VALUE_TYPE_I32;
  13950. #endif
  13951. PUSH_TBL_ELEM_IDX();
  13952. #if WASM_ENABLE_WAMR_COMPILER != 0
  13953. module->is_ref_types_used = true;
  13954. #endif
  13955. break;
  13956. }
  13957. case WASM_OP_TABLE_GROW:
  13958. case WASM_OP_TABLE_FILL:
  13959. {
  13960. uint8 decl_type;
  13961. #if WASM_ENABLE_GC != 0
  13962. WASMRefType *ref_type = NULL;
  13963. #endif
  13964. pb_read_leb_uint32(p, p_end, table_idx);
  13965. if (!get_table_elem_type(module, table_idx, &decl_type,
  13966. #if WASM_ENABLE_GC != 0
  13967. (void **)&ref_type,
  13968. #else
  13969. NULL,
  13970. #endif
  13971. error_buf, error_buf_size))
  13972. goto fail;
  13973. #if WASM_ENABLE_GC != 0
  13974. if (wasm_is_type_multi_byte_type(decl_type)) {
  13975. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13976. ref_type,
  13977. wasm_reftype_struct_size(ref_type));
  13978. }
  13979. #endif
  13980. if (opcode1 == WASM_OP_TABLE_GROW) {
  13981. if (table_idx < module->import_table_count) {
  13982. module->import_tables[table_idx]
  13983. .u.table.table_type.possible_grow = true;
  13984. }
  13985. else {
  13986. module
  13987. ->tables[table_idx
  13988. - module->import_table_count]
  13989. .table_type.possible_grow = true;
  13990. }
  13991. }
  13992. #if WASM_ENABLE_FAST_INTERP != 0
  13993. emit_uint32(loader_ctx, table_idx);
  13994. #endif
  13995. #if WASM_ENABLE_MEMORY64 != 0
  13996. table_elem_idx_type = is_table_64bit(module, table_idx)
  13997. ? VALUE_TYPE_I64
  13998. : VALUE_TYPE_I32;
  13999. #endif
  14000. POP_TBL_ELEM_IDX();
  14001. #if WASM_ENABLE_FAST_INTERP != 0
  14002. POP_OFFSET_TYPE(decl_type);
  14003. #endif
  14004. POP_TYPE(decl_type);
  14005. if (opcode1 == WASM_OP_TABLE_GROW)
  14006. PUSH_TBL_ELEM_IDX();
  14007. else
  14008. POP_TBL_ELEM_IDX();
  14009. #if WASM_ENABLE_WAMR_COMPILER != 0
  14010. module->is_ref_types_used = true;
  14011. #endif
  14012. break;
  14013. }
  14014. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  14015. default:
  14016. set_error_buf_v(error_buf, error_buf_size,
  14017. "%s %02x %02x", "unsupported opcode",
  14018. 0xfc, opcode1);
  14019. goto fail;
  14020. }
  14021. break;
  14022. }
  14023. #if WASM_ENABLE_SIMD != 0
  14024. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  14025. || (WASM_ENABLE_FAST_INTERP != 0)
  14026. case WASM_OP_SIMD_PREFIX:
  14027. {
  14028. uint32 opcode1;
  14029. #if WASM_ENABLE_WAMR_COMPILER != 0
  14030. /* Mark the SIMD instruction is used in this module */
  14031. module->is_simd_used = true;
  14032. #endif
  14033. pb_read_leb_uint32(p, p_end, opcode1);
  14034. #if WASM_ENABLE_FAST_INTERP != 0
  14035. emit_byte(loader_ctx, opcode1);
  14036. #endif
  14037. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14038. */
  14039. switch (opcode1) {
  14040. /* memory instruction */
  14041. case SIMD_v128_load:
  14042. case SIMD_v128_load8x8_s:
  14043. case SIMD_v128_load8x8_u:
  14044. case SIMD_v128_load16x4_s:
  14045. case SIMD_v128_load16x4_u:
  14046. case SIMD_v128_load32x2_s:
  14047. case SIMD_v128_load32x2_u:
  14048. case SIMD_v128_load8_splat:
  14049. case SIMD_v128_load16_splat:
  14050. case SIMD_v128_load32_splat:
  14051. case SIMD_v128_load64_splat:
  14052. {
  14053. CHECK_MEMORY();
  14054. pb_read_leb_uint32(p, p_end, align); /* align */
  14055. if (!check_simd_memory_access_align(
  14056. opcode1, align, error_buf, error_buf_size)) {
  14057. goto fail;
  14058. }
  14059. pb_read_leb_mem_offset(p, p_end,
  14060. mem_offset); /* offset */
  14061. #if WASM_ENABLE_FAST_INTERP != 0
  14062. emit_uint32(loader_ctx, mem_offset);
  14063. #endif
  14064. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14065. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14066. func->has_memory_operations = true;
  14067. #endif
  14068. break;
  14069. }
  14070. case SIMD_v128_store:
  14071. {
  14072. CHECK_MEMORY();
  14073. pb_read_leb_uint32(p, p_end, align); /* align */
  14074. if (!check_simd_memory_access_align(
  14075. opcode1, align, error_buf, error_buf_size)) {
  14076. goto fail;
  14077. }
  14078. pb_read_leb_mem_offset(p, p_end,
  14079. mem_offset); /* offset */
  14080. #if WASM_ENABLE_FAST_INTERP != 0
  14081. emit_uint32(loader_ctx, mem_offset);
  14082. #endif
  14083. POP_V128();
  14084. POP_MEM_OFFSET();
  14085. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14086. func->has_memory_operations = true;
  14087. #endif
  14088. break;
  14089. }
  14090. /* basic operation */
  14091. case SIMD_v128_const:
  14092. {
  14093. #if WASM_ENABLE_FAST_INTERP != 0
  14094. uint64 high, low;
  14095. #endif
  14096. CHECK_BUF1(p, p_end, 16);
  14097. #if WASM_ENABLE_FAST_INTERP != 0
  14098. wasm_runtime_read_v128(p, &high, &low);
  14099. emit_uint64(loader_ctx, high);
  14100. emit_uint64(loader_ctx, low);
  14101. #endif
  14102. p += 16;
  14103. PUSH_V128();
  14104. break;
  14105. }
  14106. case SIMD_v8x16_shuffle:
  14107. {
  14108. V128 mask;
  14109. CHECK_BUF1(p, p_end, 16);
  14110. mask = read_i8x16(p, error_buf, error_buf_size);
  14111. if (!check_simd_shuffle_mask(mask, error_buf,
  14112. error_buf_size)) {
  14113. goto fail;
  14114. }
  14115. #if WASM_ENABLE_FAST_INTERP != 0
  14116. uint64 high, low;
  14117. wasm_runtime_read_v128(p, &high, &low);
  14118. emit_uint64(loader_ctx, high);
  14119. emit_uint64(loader_ctx, low);
  14120. #endif
  14121. p += 16;
  14122. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14123. break;
  14124. }
  14125. case SIMD_v8x16_swizzle:
  14126. {
  14127. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14128. break;
  14129. }
  14130. /* splat operation */
  14131. case SIMD_i8x16_splat:
  14132. case SIMD_i16x8_splat:
  14133. case SIMD_i32x4_splat:
  14134. case SIMD_i64x2_splat:
  14135. case SIMD_f32x4_splat:
  14136. case SIMD_f64x2_splat:
  14137. {
  14138. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14139. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14140. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14141. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14142. VALUE_TYPE_V128);
  14143. break;
  14144. }
  14145. /* lane operation */
  14146. case SIMD_i8x16_extract_lane_s:
  14147. case SIMD_i8x16_extract_lane_u:
  14148. case SIMD_i8x16_replace_lane:
  14149. case SIMD_i16x8_extract_lane_s:
  14150. case SIMD_i16x8_extract_lane_u:
  14151. case SIMD_i16x8_replace_lane:
  14152. case SIMD_i32x4_extract_lane:
  14153. case SIMD_i32x4_replace_lane:
  14154. case SIMD_i64x2_extract_lane:
  14155. case SIMD_i64x2_replace_lane:
  14156. case SIMD_f32x4_extract_lane:
  14157. case SIMD_f32x4_replace_lane:
  14158. case SIMD_f64x2_extract_lane:
  14159. case SIMD_f64x2_replace_lane:
  14160. {
  14161. uint8 lane;
  14162. /* clang-format off */
  14163. uint8 replace[] = {
  14164. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14165. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14166. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14167. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14168. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14169. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14170. };
  14171. uint8 push_type[] = {
  14172. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14173. VALUE_TYPE_V128,
  14174. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14175. VALUE_TYPE_V128,
  14176. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14177. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14178. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14179. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14180. };
  14181. /* clang-format on */
  14182. CHECK_BUF(p, p_end, 1);
  14183. lane = read_uint8(p);
  14184. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14185. error_buf_size)) {
  14186. goto fail;
  14187. }
  14188. #if WASM_ENABLE_FAST_INTERP != 0
  14189. emit_byte(loader_ctx, lane);
  14190. #endif
  14191. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14192. #if WASM_ENABLE_FAST_INTERP != 0
  14193. if (!(wasm_loader_pop_frame_ref_offset(
  14194. loader_ctx,
  14195. replace[opcode1
  14196. - SIMD_i8x16_extract_lane_s],
  14197. error_buf, error_buf_size)))
  14198. goto fail;
  14199. #else
  14200. if (!(wasm_loader_pop_frame_ref(
  14201. loader_ctx,
  14202. replace[opcode1
  14203. - SIMD_i8x16_extract_lane_s],
  14204. error_buf, error_buf_size)))
  14205. goto fail;
  14206. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14207. }
  14208. POP_AND_PUSH(
  14209. VALUE_TYPE_V128,
  14210. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14211. break;
  14212. }
  14213. /* i8x16 compare operation */
  14214. case SIMD_i8x16_eq:
  14215. case SIMD_i8x16_ne:
  14216. case SIMD_i8x16_lt_s:
  14217. case SIMD_i8x16_lt_u:
  14218. case SIMD_i8x16_gt_s:
  14219. case SIMD_i8x16_gt_u:
  14220. case SIMD_i8x16_le_s:
  14221. case SIMD_i8x16_le_u:
  14222. case SIMD_i8x16_ge_s:
  14223. case SIMD_i8x16_ge_u:
  14224. /* i16x8 compare operation */
  14225. case SIMD_i16x8_eq:
  14226. case SIMD_i16x8_ne:
  14227. case SIMD_i16x8_lt_s:
  14228. case SIMD_i16x8_lt_u:
  14229. case SIMD_i16x8_gt_s:
  14230. case SIMD_i16x8_gt_u:
  14231. case SIMD_i16x8_le_s:
  14232. case SIMD_i16x8_le_u:
  14233. case SIMD_i16x8_ge_s:
  14234. case SIMD_i16x8_ge_u:
  14235. /* i32x4 compare operation */
  14236. case SIMD_i32x4_eq:
  14237. case SIMD_i32x4_ne:
  14238. case SIMD_i32x4_lt_s:
  14239. case SIMD_i32x4_lt_u:
  14240. case SIMD_i32x4_gt_s:
  14241. case SIMD_i32x4_gt_u:
  14242. case SIMD_i32x4_le_s:
  14243. case SIMD_i32x4_le_u:
  14244. case SIMD_i32x4_ge_s:
  14245. case SIMD_i32x4_ge_u:
  14246. /* f32x4 compare operation */
  14247. case SIMD_f32x4_eq:
  14248. case SIMD_f32x4_ne:
  14249. case SIMD_f32x4_lt:
  14250. case SIMD_f32x4_gt:
  14251. case SIMD_f32x4_le:
  14252. case SIMD_f32x4_ge:
  14253. /* f64x2 compare operation */
  14254. case SIMD_f64x2_eq:
  14255. case SIMD_f64x2_ne:
  14256. case SIMD_f64x2_lt:
  14257. case SIMD_f64x2_gt:
  14258. case SIMD_f64x2_le:
  14259. case SIMD_f64x2_ge:
  14260. {
  14261. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14262. break;
  14263. }
  14264. /* v128 operation */
  14265. case SIMD_v128_not:
  14266. {
  14267. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14268. break;
  14269. }
  14270. case SIMD_v128_and:
  14271. case SIMD_v128_andnot:
  14272. case SIMD_v128_or:
  14273. case SIMD_v128_xor:
  14274. {
  14275. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14276. break;
  14277. }
  14278. case SIMD_v128_bitselect:
  14279. {
  14280. POP_V128();
  14281. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14282. break;
  14283. }
  14284. case SIMD_v128_any_true:
  14285. {
  14286. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14287. break;
  14288. }
  14289. /* Load Lane Operation */
  14290. case SIMD_v128_load8_lane:
  14291. case SIMD_v128_load16_lane:
  14292. case SIMD_v128_load32_lane:
  14293. case SIMD_v128_load64_lane:
  14294. case SIMD_v128_store8_lane:
  14295. case SIMD_v128_store16_lane:
  14296. case SIMD_v128_store32_lane:
  14297. case SIMD_v128_store64_lane:
  14298. {
  14299. uint8 lane;
  14300. CHECK_MEMORY();
  14301. pb_read_leb_uint32(p, p_end, align); /* align */
  14302. if (!check_simd_memory_access_align(
  14303. opcode1, align, error_buf, error_buf_size)) {
  14304. goto fail;
  14305. }
  14306. pb_read_leb_mem_offset(p, p_end,
  14307. mem_offset); /* offset */
  14308. CHECK_BUF(p, p_end, 1);
  14309. lane = read_uint8(p);
  14310. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14311. error_buf_size)) {
  14312. goto fail;
  14313. }
  14314. #if WASM_ENABLE_FAST_INTERP != 0
  14315. emit_uint32(loader_ctx, mem_offset);
  14316. #endif
  14317. POP_V128();
  14318. POP_MEM_OFFSET();
  14319. #if WASM_ENABLE_FAST_INTERP != 0
  14320. emit_byte(loader_ctx, lane);
  14321. #endif
  14322. if (opcode1 < SIMD_v128_store8_lane) {
  14323. PUSH_V128();
  14324. }
  14325. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14326. func->has_memory_operations = true;
  14327. #endif
  14328. break;
  14329. }
  14330. case SIMD_v128_load32_zero:
  14331. case SIMD_v128_load64_zero:
  14332. {
  14333. CHECK_MEMORY();
  14334. pb_read_leb_uint32(p, p_end, align); /* align */
  14335. if (!check_simd_memory_access_align(
  14336. opcode1, align, error_buf, error_buf_size)) {
  14337. goto fail;
  14338. }
  14339. pb_read_leb_mem_offset(p, p_end,
  14340. mem_offset); /* offset */
  14341. #if WASM_ENABLE_FAST_INTERP != 0
  14342. emit_uint32(loader_ctx, mem_offset);
  14343. #endif
  14344. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14345. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14346. func->has_memory_operations = true;
  14347. #endif
  14348. break;
  14349. }
  14350. /* Float conversion */
  14351. case SIMD_f32x4_demote_f64x2_zero:
  14352. case SIMD_f64x2_promote_low_f32x4_zero:
  14353. {
  14354. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14355. break;
  14356. }
  14357. /* i8x16 Operation */
  14358. case SIMD_i8x16_abs:
  14359. case SIMD_i8x16_neg:
  14360. case SIMD_i8x16_popcnt:
  14361. {
  14362. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14363. break;
  14364. }
  14365. case SIMD_i8x16_all_true:
  14366. case SIMD_i8x16_bitmask:
  14367. {
  14368. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14369. break;
  14370. }
  14371. case SIMD_i8x16_narrow_i16x8_s:
  14372. case SIMD_i8x16_narrow_i16x8_u:
  14373. {
  14374. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14375. break;
  14376. }
  14377. case SIMD_f32x4_ceil:
  14378. case SIMD_f32x4_floor:
  14379. case SIMD_f32x4_trunc:
  14380. case SIMD_f32x4_nearest:
  14381. {
  14382. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14383. break;
  14384. }
  14385. case SIMD_i8x16_shl:
  14386. case SIMD_i8x16_shr_s:
  14387. case SIMD_i8x16_shr_u:
  14388. {
  14389. POP_I32();
  14390. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14391. break;
  14392. }
  14393. case SIMD_i8x16_add:
  14394. case SIMD_i8x16_add_sat_s:
  14395. case SIMD_i8x16_add_sat_u:
  14396. case SIMD_i8x16_sub:
  14397. case SIMD_i8x16_sub_sat_s:
  14398. case SIMD_i8x16_sub_sat_u:
  14399. {
  14400. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14401. break;
  14402. }
  14403. case SIMD_f64x2_ceil:
  14404. case SIMD_f64x2_floor:
  14405. {
  14406. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14407. break;
  14408. }
  14409. case SIMD_i8x16_min_s:
  14410. case SIMD_i8x16_min_u:
  14411. case SIMD_i8x16_max_s:
  14412. case SIMD_i8x16_max_u:
  14413. {
  14414. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14415. break;
  14416. }
  14417. case SIMD_f64x2_trunc:
  14418. {
  14419. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14420. break;
  14421. }
  14422. case SIMD_i8x16_avgr_u:
  14423. {
  14424. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14425. break;
  14426. }
  14427. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14428. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14429. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14430. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14431. /* i16x8 operation */
  14432. case SIMD_i16x8_abs:
  14433. case SIMD_i16x8_neg:
  14434. {
  14435. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14436. break;
  14437. }
  14438. case SIMD_i16x8_q15mulr_sat_s:
  14439. {
  14440. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14441. break;
  14442. }
  14443. case SIMD_i16x8_all_true:
  14444. case SIMD_i16x8_bitmask:
  14445. {
  14446. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14447. break;
  14448. }
  14449. case SIMD_i16x8_narrow_i32x4_s:
  14450. case SIMD_i16x8_narrow_i32x4_u:
  14451. {
  14452. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14453. break;
  14454. }
  14455. case SIMD_i16x8_extend_low_i8x16_s:
  14456. case SIMD_i16x8_extend_high_i8x16_s:
  14457. case SIMD_i16x8_extend_low_i8x16_u:
  14458. case SIMD_i16x8_extend_high_i8x16_u:
  14459. {
  14460. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14461. break;
  14462. }
  14463. case SIMD_i16x8_shl:
  14464. case SIMD_i16x8_shr_s:
  14465. case SIMD_i16x8_shr_u:
  14466. {
  14467. POP_I32();
  14468. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14469. break;
  14470. }
  14471. case SIMD_i16x8_add:
  14472. case SIMD_i16x8_add_sat_s:
  14473. case SIMD_i16x8_add_sat_u:
  14474. case SIMD_i16x8_sub:
  14475. case SIMD_i16x8_sub_sat_s:
  14476. case SIMD_i16x8_sub_sat_u:
  14477. {
  14478. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14479. break;
  14480. }
  14481. case SIMD_f64x2_nearest:
  14482. {
  14483. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14484. break;
  14485. }
  14486. case SIMD_i16x8_mul:
  14487. case SIMD_i16x8_min_s:
  14488. case SIMD_i16x8_min_u:
  14489. case SIMD_i16x8_max_s:
  14490. case SIMD_i16x8_max_u:
  14491. case SIMD_i16x8_avgr_u:
  14492. case SIMD_i16x8_extmul_low_i8x16_s:
  14493. case SIMD_i16x8_extmul_high_i8x16_s:
  14494. case SIMD_i16x8_extmul_low_i8x16_u:
  14495. case SIMD_i16x8_extmul_high_i8x16_u:
  14496. {
  14497. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14498. break;
  14499. }
  14500. /* i32x4 operation */
  14501. case SIMD_i32x4_abs:
  14502. case SIMD_i32x4_neg:
  14503. {
  14504. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14505. break;
  14506. }
  14507. case SIMD_i32x4_all_true:
  14508. case SIMD_i32x4_bitmask:
  14509. {
  14510. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14511. break;
  14512. }
  14513. case SIMD_i32x4_extend_low_i16x8_s:
  14514. case SIMD_i32x4_extend_high_i16x8_s:
  14515. case SIMD_i32x4_extend_low_i16x8_u:
  14516. case SIMD_i32x4_extend_high_i16x8_u:
  14517. {
  14518. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14519. break;
  14520. }
  14521. case SIMD_i32x4_shl:
  14522. case SIMD_i32x4_shr_s:
  14523. case SIMD_i32x4_shr_u:
  14524. {
  14525. POP_I32();
  14526. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14527. break;
  14528. }
  14529. case SIMD_i32x4_add:
  14530. case SIMD_i32x4_sub:
  14531. case SIMD_i32x4_mul:
  14532. case SIMD_i32x4_min_s:
  14533. case SIMD_i32x4_min_u:
  14534. case SIMD_i32x4_max_s:
  14535. case SIMD_i32x4_max_u:
  14536. case SIMD_i32x4_dot_i16x8_s:
  14537. case SIMD_i32x4_extmul_low_i16x8_s:
  14538. case SIMD_i32x4_extmul_high_i16x8_s:
  14539. case SIMD_i32x4_extmul_low_i16x8_u:
  14540. case SIMD_i32x4_extmul_high_i16x8_u:
  14541. {
  14542. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14543. break;
  14544. }
  14545. /* i64x2 operation */
  14546. case SIMD_i64x2_abs:
  14547. case SIMD_i64x2_neg:
  14548. {
  14549. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14550. break;
  14551. }
  14552. case SIMD_i64x2_all_true:
  14553. case SIMD_i64x2_bitmask:
  14554. {
  14555. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14556. break;
  14557. }
  14558. case SIMD_i64x2_extend_low_i32x4_s:
  14559. case SIMD_i64x2_extend_high_i32x4_s:
  14560. case SIMD_i64x2_extend_low_i32x4_u:
  14561. case SIMD_i64x2_extend_high_i32x4_u:
  14562. {
  14563. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14564. break;
  14565. }
  14566. case SIMD_i64x2_shl:
  14567. case SIMD_i64x2_shr_s:
  14568. case SIMD_i64x2_shr_u:
  14569. {
  14570. POP_I32();
  14571. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14572. break;
  14573. }
  14574. case SIMD_i64x2_add:
  14575. case SIMD_i64x2_sub:
  14576. case SIMD_i64x2_mul:
  14577. case SIMD_i64x2_eq:
  14578. case SIMD_i64x2_ne:
  14579. case SIMD_i64x2_lt_s:
  14580. case SIMD_i64x2_gt_s:
  14581. case SIMD_i64x2_le_s:
  14582. case SIMD_i64x2_ge_s:
  14583. case SIMD_i64x2_extmul_low_i32x4_s:
  14584. case SIMD_i64x2_extmul_high_i32x4_s:
  14585. case SIMD_i64x2_extmul_low_i32x4_u:
  14586. case SIMD_i64x2_extmul_high_i32x4_u:
  14587. {
  14588. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14589. break;
  14590. }
  14591. /* f32x4 operation */
  14592. case SIMD_f32x4_abs:
  14593. case SIMD_f32x4_neg:
  14594. case SIMD_f32x4_sqrt:
  14595. {
  14596. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14597. break;
  14598. }
  14599. case SIMD_f32x4_add:
  14600. case SIMD_f32x4_sub:
  14601. case SIMD_f32x4_mul:
  14602. case SIMD_f32x4_div:
  14603. case SIMD_f32x4_min:
  14604. case SIMD_f32x4_max:
  14605. case SIMD_f32x4_pmin:
  14606. case SIMD_f32x4_pmax:
  14607. {
  14608. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14609. break;
  14610. }
  14611. /* f64x2 operation */
  14612. case SIMD_f64x2_abs:
  14613. case SIMD_f64x2_neg:
  14614. case SIMD_f64x2_sqrt:
  14615. {
  14616. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14617. break;
  14618. }
  14619. case SIMD_f64x2_add:
  14620. case SIMD_f64x2_sub:
  14621. case SIMD_f64x2_mul:
  14622. case SIMD_f64x2_div:
  14623. case SIMD_f64x2_min:
  14624. case SIMD_f64x2_max:
  14625. case SIMD_f64x2_pmin:
  14626. case SIMD_f64x2_pmax:
  14627. {
  14628. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14629. break;
  14630. }
  14631. case SIMD_i32x4_trunc_sat_f32x4_s:
  14632. case SIMD_i32x4_trunc_sat_f32x4_u:
  14633. case SIMD_f32x4_convert_i32x4_s:
  14634. case SIMD_f32x4_convert_i32x4_u:
  14635. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14636. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14637. case SIMD_f64x2_convert_low_i32x4_s:
  14638. case SIMD_f64x2_convert_low_i32x4_u:
  14639. {
  14640. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14641. break;
  14642. }
  14643. default:
  14644. {
  14645. if (error_buf != NULL) {
  14646. snprintf(error_buf, error_buf_size,
  14647. "WASM module load failed: "
  14648. "invalid opcode 0xfd %02x.",
  14649. opcode1);
  14650. }
  14651. goto fail;
  14652. }
  14653. }
  14654. break;
  14655. }
  14656. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  14657. (WASM_ENABLE_FAST_INTERP != 0) */
  14658. #endif /* end of WASM_ENABLE_SIMD */
  14659. #if WASM_ENABLE_SHARED_MEMORY != 0
  14660. case WASM_OP_ATOMIC_PREFIX:
  14661. {
  14662. uint32 opcode1;
  14663. pb_read_leb_uint32(p, p_end, opcode1);
  14664. #if WASM_ENABLE_FAST_INTERP != 0
  14665. emit_byte(loader_ctx, opcode1);
  14666. #endif
  14667. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14668. CHECK_MEMORY();
  14669. pb_read_leb_uint32(p, p_end, align); /* align */
  14670. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14671. if (!check_memory_align_equal(opcode1, align, error_buf,
  14672. error_buf_size)) {
  14673. goto fail;
  14674. }
  14675. #if WASM_ENABLE_FAST_INTERP != 0
  14676. emit_uint32(loader_ctx, mem_offset);
  14677. #endif
  14678. }
  14679. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14680. func->has_memory_operations = true;
  14681. #endif
  14682. switch (opcode1) {
  14683. case WASM_OP_ATOMIC_NOTIFY:
  14684. POP_I32();
  14685. POP_MEM_OFFSET();
  14686. PUSH_I32();
  14687. break;
  14688. case WASM_OP_ATOMIC_WAIT32:
  14689. POP_I64();
  14690. POP_I32();
  14691. POP_MEM_OFFSET();
  14692. PUSH_I32();
  14693. break;
  14694. case WASM_OP_ATOMIC_WAIT64:
  14695. POP_I64();
  14696. POP_I64();
  14697. POP_MEM_OFFSET();
  14698. PUSH_I32();
  14699. break;
  14700. case WASM_OP_ATOMIC_FENCE:
  14701. /* reserved byte 0x00 */
  14702. if (*p++ != 0x00) {
  14703. set_error_buf(error_buf, error_buf_size,
  14704. "zero byte expected");
  14705. goto fail;
  14706. }
  14707. break;
  14708. case WASM_OP_ATOMIC_I32_LOAD:
  14709. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14710. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14711. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14712. break;
  14713. case WASM_OP_ATOMIC_I32_STORE:
  14714. case WASM_OP_ATOMIC_I32_STORE8:
  14715. case WASM_OP_ATOMIC_I32_STORE16:
  14716. POP_I32();
  14717. POP_MEM_OFFSET();
  14718. break;
  14719. case WASM_OP_ATOMIC_I64_LOAD:
  14720. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14721. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14722. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14723. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14724. break;
  14725. case WASM_OP_ATOMIC_I64_STORE:
  14726. case WASM_OP_ATOMIC_I64_STORE8:
  14727. case WASM_OP_ATOMIC_I64_STORE16:
  14728. case WASM_OP_ATOMIC_I64_STORE32:
  14729. POP_I64();
  14730. POP_MEM_OFFSET();
  14731. break;
  14732. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14733. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14734. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14735. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14736. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14737. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14738. case WASM_OP_ATOMIC_RMW_I32_AND:
  14739. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14740. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14741. case WASM_OP_ATOMIC_RMW_I32_OR:
  14742. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14743. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14744. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14745. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14746. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14747. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14748. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14749. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14750. POP_I32();
  14751. POP_MEM_OFFSET();
  14752. PUSH_I32();
  14753. break;
  14754. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14755. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14756. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14757. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14758. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14759. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14760. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14761. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14762. case WASM_OP_ATOMIC_RMW_I64_AND:
  14763. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14764. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14765. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14766. case WASM_OP_ATOMIC_RMW_I64_OR:
  14767. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14768. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14769. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14770. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14771. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14772. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14773. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14774. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14775. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14776. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14777. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14778. POP_I64();
  14779. POP_MEM_OFFSET();
  14780. PUSH_I64();
  14781. break;
  14782. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14783. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14784. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14785. POP_I32();
  14786. POP_I32();
  14787. POP_MEM_OFFSET();
  14788. PUSH_I32();
  14789. break;
  14790. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14791. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14792. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14793. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14794. POP_I64();
  14795. POP_I64();
  14796. POP_MEM_OFFSET();
  14797. PUSH_I64();
  14798. break;
  14799. default:
  14800. set_error_buf_v(error_buf, error_buf_size,
  14801. "%s %02x %02x", "unsupported opcode",
  14802. 0xfe, opcode1);
  14803. goto fail;
  14804. }
  14805. break;
  14806. }
  14807. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14808. default:
  14809. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14810. "unsupported opcode", opcode);
  14811. goto fail;
  14812. }
  14813. #if WASM_ENABLE_FAST_INTERP != 0
  14814. last_op = opcode;
  14815. #endif
  14816. }
  14817. if (loader_ctx->csp_num > 0) {
  14818. /* unmatched end opcodes result from unbalanced control flow structures,
  14819. * for example, br_table with inconsistent target count (1 declared, 2
  14820. * given), or simply superfluous end opcodes */
  14821. set_error_buf(
  14822. error_buf, error_buf_size,
  14823. "unexpected end opcodes from unbalanced control flow structures");
  14824. goto fail;
  14825. }
  14826. #if WASM_ENABLE_FAST_INTERP != 0
  14827. if (loader_ctx->p_code_compiled == NULL)
  14828. goto re_scan;
  14829. func->const_cell_num = loader_ctx->i64_const_num * 2
  14830. + loader_ctx->v128_const_num * 4
  14831. + loader_ctx->i32_const_num;
  14832. if (func->const_cell_num > 0) {
  14833. if (!(func->consts =
  14834. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  14835. error_buf, error_buf_size)))
  14836. goto fail;
  14837. if (loader_ctx->i64_const_num > 0) {
  14838. bh_memcpy_s(func->consts,
  14839. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  14840. loader_ctx->i64_consts,
  14841. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  14842. }
  14843. if (loader_ctx->i32_const_num > 0) {
  14844. bh_memcpy_s(func->consts
  14845. + sizeof(int64) * loader_ctx->i64_const_num,
  14846. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  14847. loader_ctx->i32_consts,
  14848. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  14849. }
  14850. if (loader_ctx->v128_const_num > 0) {
  14851. bh_memcpy_s(func->consts,
  14852. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  14853. loader_ctx->v128_consts,
  14854. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  14855. }
  14856. }
  14857. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14858. - loader_ctx->start_dynamic_offset + 1;
  14859. #else
  14860. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14861. #endif
  14862. func->max_block_num = loader_ctx->max_csp_num;
  14863. return_value = true;
  14864. fail:
  14865. wasm_loader_ctx_destroy(loader_ctx);
  14866. (void)table_idx;
  14867. (void)table_seg_idx;
  14868. (void)data_seg_idx;
  14869. (void)i64_const;
  14870. (void)local_offset;
  14871. (void)p_org;
  14872. (void)mem_offset;
  14873. (void)align;
  14874. return return_value;
  14875. }