wasm_loader.c 608 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441
  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. emit_operand(loader_ctx, local_index);
  8247. emit_operand(loader_ctx, preserved_offset);
  8248. emit_label(opcode);
  8249. }
  8250. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8251. }
  8252. if (cur_type == VALUE_TYPE_V128) {
  8253. i += 4;
  8254. }
  8255. else if (is_32bit_type(cur_type)) {
  8256. i++;
  8257. }
  8258. else {
  8259. i += 2;
  8260. }
  8261. }
  8262. (void)error_buf;
  8263. (void)error_buf_size;
  8264. return true;
  8265. }
  8266. static bool
  8267. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8268. char *error_buf, uint32 error_buf_size)
  8269. {
  8270. uint32 i = 0;
  8271. bool preserve_local;
  8272. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8273. blocks will not influence the value of these locals */
  8274. while (i < loader_ctx->stack_cell_num) {
  8275. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8276. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8277. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8278. && (cur_offset >= 0)) {
  8279. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8280. cur_type, &preserve_local,
  8281. error_buf, error_buf_size)))
  8282. return false;
  8283. }
  8284. if (cur_type == VALUE_TYPE_V128) {
  8285. i += 4;
  8286. }
  8287. else if (is_32bit_type(cur_type)) {
  8288. i++;
  8289. }
  8290. else {
  8291. i += 2;
  8292. }
  8293. }
  8294. return true;
  8295. }
  8296. static bool
  8297. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8298. uint8 *p_code_compiled, char *error_buf,
  8299. uint32 error_buf_size)
  8300. {
  8301. BranchBlockPatch *patch =
  8302. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8303. if (!patch) {
  8304. return false;
  8305. }
  8306. patch->patch_type = patch_type;
  8307. patch->code_compiled = p_code_compiled;
  8308. if (!frame_csp->patch_list) {
  8309. frame_csp->patch_list = patch;
  8310. patch->next = NULL;
  8311. }
  8312. else {
  8313. patch->next = frame_csp->patch_list;
  8314. frame_csp->patch_list = patch;
  8315. }
  8316. return true;
  8317. }
  8318. static void
  8319. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8320. {
  8321. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8322. BranchBlockPatch *node = frame_csp->patch_list;
  8323. BranchBlockPatch *node_prev = NULL, *node_next;
  8324. if (!ctx->p_code_compiled)
  8325. return;
  8326. while (node) {
  8327. node_next = node->next;
  8328. if (node->patch_type == patch_type) {
  8329. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8330. if (node_prev == NULL) {
  8331. frame_csp->patch_list = node_next;
  8332. }
  8333. else {
  8334. node_prev->next = node_next;
  8335. }
  8336. wasm_runtime_free(node);
  8337. }
  8338. else {
  8339. node_prev = node;
  8340. }
  8341. node = node_next;
  8342. }
  8343. }
  8344. static bool
  8345. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8346. bool is_br, char *error_buf, uint32 error_buf_size)
  8347. {
  8348. /* br info layout:
  8349. * a) arity of target block
  8350. * b) total cell num of arity values
  8351. * c) each arity value's cell num
  8352. * d) each arity value's src frame offset
  8353. * e) each arity values's dst dynamic offset
  8354. * f) branch target address
  8355. *
  8356. * Note: b-e are omitted when arity is 0 so that
  8357. * interpreter can recover the br info quickly.
  8358. */
  8359. BlockType *block_type = &frame_csp->block_type;
  8360. uint8 *types = NULL, cell;
  8361. #if WASM_ENABLE_GC != 0
  8362. WASMRefTypeMap *reftype_maps;
  8363. uint32 reftype_map_count;
  8364. #endif
  8365. uint32 arity = 0;
  8366. int32 i;
  8367. int16 *frame_offset = ctx->frame_offset;
  8368. uint16 dynamic_offset;
  8369. /* Note: loop's arity is different from if and block. loop's arity is
  8370. * its parameter count while if and block arity is result count.
  8371. */
  8372. #if WASM_ENABLE_GC == 0
  8373. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8374. arity = block_type_get_param_types(block_type, &types);
  8375. else
  8376. arity = block_type_get_result_types(block_type, &types);
  8377. #else
  8378. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8379. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8380. &reftype_map_count);
  8381. else
  8382. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8383. &reftype_map_count);
  8384. #endif
  8385. /* Part a */
  8386. emit_uint32(ctx, arity);
  8387. if (arity) {
  8388. /* Part b */
  8389. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8390. /* Part c */
  8391. for (i = (int32)arity - 1; i >= 0; i--) {
  8392. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8393. emit_byte(ctx, cell);
  8394. }
  8395. /* Part d */
  8396. for (i = (int32)arity - 1; i >= 0; i--) {
  8397. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8398. frame_offset -= cell;
  8399. emit_operand(ctx, *(int16 *)(frame_offset));
  8400. }
  8401. /* Part e */
  8402. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8403. /* Use start_dynamic_offset which was set in
  8404. copy_params_to_dynamic_space */
  8405. dynamic_offset = frame_csp->start_dynamic_offset
  8406. + wasm_get_cell_num(types, arity);
  8407. else
  8408. dynamic_offset =
  8409. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8410. if (is_br)
  8411. ctx->dynamic_offset = dynamic_offset;
  8412. for (i = (int32)arity - 1; i >= 0; i--) {
  8413. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8414. dynamic_offset -= cell;
  8415. emit_operand(ctx, dynamic_offset);
  8416. }
  8417. }
  8418. /* Part f */
  8419. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8420. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8421. }
  8422. else {
  8423. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8424. error_buf, error_buf_size))
  8425. return false;
  8426. /* label address, to be patched */
  8427. wasm_loader_emit_ptr(ctx, NULL);
  8428. }
  8429. return true;
  8430. }
  8431. static bool
  8432. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8433. bool disable_emit, int16 operand_offset,
  8434. char *error_buf, uint32 error_buf_size)
  8435. {
  8436. uint32 cell_num_to_push, i;
  8437. if (type == VALUE_TYPE_VOID)
  8438. return true;
  8439. /* only check memory overflow in first traverse */
  8440. if (ctx->p_code_compiled == NULL) {
  8441. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8442. return false;
  8443. }
  8444. if (disable_emit)
  8445. *(ctx->frame_offset)++ = operand_offset;
  8446. else {
  8447. emit_operand(ctx, ctx->dynamic_offset);
  8448. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8449. ctx->dynamic_offset++;
  8450. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8451. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8452. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8453. goto fail;
  8454. }
  8455. }
  8456. }
  8457. if (is_32bit_type(type))
  8458. return true;
  8459. cell_num_to_push = wasm_value_type_cell_num(type) - 1;
  8460. for (i = 0; i < cell_num_to_push; i++) {
  8461. if (ctx->p_code_compiled == NULL) {
  8462. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8463. return false;
  8464. }
  8465. ctx->frame_offset++;
  8466. if (!disable_emit) {
  8467. ctx->dynamic_offset++;
  8468. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8469. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8470. if (ctx->max_dynamic_offset >= INT16_MAX)
  8471. goto fail;
  8472. }
  8473. }
  8474. }
  8475. return true;
  8476. fail:
  8477. set_error_buf(error_buf, error_buf_size,
  8478. "fast interpreter offset overflow");
  8479. return false;
  8480. }
  8481. /* This function should be in front of wasm_loader_pop_frame_ref
  8482. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8483. will be modified by wasm_loader_pop_frame_ref */
  8484. static bool
  8485. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8486. char *error_buf, uint32 error_buf_size)
  8487. {
  8488. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8489. then current block is the function block */
  8490. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8491. BranchBlock *cur_block = ctx->frame_csp - depth;
  8492. int32 available_stack_cell =
  8493. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8494. uint32 cell_num_to_pop;
  8495. /* Directly return success if current block is in stack
  8496. polymorphic state while stack is empty. */
  8497. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8498. return true;
  8499. if (type == VALUE_TYPE_VOID)
  8500. return true;
  8501. /* Change type to ANY when the stack top is ANY, so as to avoid
  8502. popping unneeded offsets, e.g. if type is I64/F64, we may pop
  8503. two offsets */
  8504. if (available_stack_cell > 0 && *(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  8505. type = VALUE_TYPE_ANY;
  8506. cell_num_to_pop = wasm_value_type_cell_num(type);
  8507. /* Check the offset stack bottom to ensure the frame offset
  8508. stack will not go underflow. But we don't thrown error
  8509. and return true here, because the error msg should be
  8510. given in wasm_loader_pop_frame_ref */
  8511. if (!check_offset_pop(ctx, cell_num_to_pop))
  8512. return true;
  8513. ctx->frame_offset -= cell_num_to_pop;
  8514. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8515. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8516. ctx->dynamic_offset -= cell_num_to_pop;
  8517. emit_operand(ctx, *(ctx->frame_offset));
  8518. (void)error_buf;
  8519. (void)error_buf_size;
  8520. return true;
  8521. }
  8522. static bool
  8523. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8524. bool disable_emit, int16 operand_offset,
  8525. char *error_buf, uint32 error_buf_size)
  8526. {
  8527. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8528. error_buf, error_buf_size)))
  8529. return false;
  8530. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8531. return false;
  8532. return true;
  8533. }
  8534. static bool
  8535. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8536. char *error_buf, uint32 error_buf_size)
  8537. {
  8538. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8539. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8540. return false;
  8541. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8542. return false;
  8543. return true;
  8544. }
  8545. static bool
  8546. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8547. uint8 type_push, uint8 type_pop,
  8548. bool disable_emit, int16 operand_offset,
  8549. char *error_buf, uint32 error_buf_size)
  8550. {
  8551. uint8 i;
  8552. for (i = 0; i < pop_cnt; i++) {
  8553. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8554. error_buf_size))
  8555. return false;
  8556. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8557. error_buf_size))
  8558. return false;
  8559. }
  8560. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8561. operand_offset, error_buf,
  8562. error_buf_size))
  8563. return false;
  8564. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8565. return false;
  8566. return true;
  8567. }
  8568. static int
  8569. cmp_i64_const(const void *p_i64_const1, const void *p_i64_const2)
  8570. {
  8571. int64 i64_const1 = *(int64 *)p_i64_const1;
  8572. int64 i64_const2 = *(int64 *)p_i64_const2;
  8573. return (i64_const1 < i64_const2) ? -1 : (i64_const1 > i64_const2) ? 1 : 0;
  8574. }
  8575. static int
  8576. cmp_i32_const(const void *p_i32_const1, const void *p_i32_const2)
  8577. {
  8578. int32 i32_const1 = *(int32 *)p_i32_const1;
  8579. int32 i32_const2 = *(int32 *)p_i32_const2;
  8580. return (i32_const1 < i32_const2) ? -1 : (i32_const1 > i32_const2) ? 1 : 0;
  8581. }
  8582. static int
  8583. cmp_v128_const(const void *p_v128_const1, const void *p_v128_const2)
  8584. {
  8585. V128 v128_const1 = *(V128 *)p_v128_const1;
  8586. V128 v128_const2 = *(V128 *)p_v128_const2;
  8587. return memcmp(&v128_const1, &v128_const2, sizeof(V128));
  8588. }
  8589. static bool
  8590. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8591. int16 *offset, char *error_buf,
  8592. uint32 error_buf_size)
  8593. {
  8594. if (!ctx->p_code_compiled) {
  8595. /* Treat i64 and f64 as the same by reading i64 value from
  8596. the raw bytes */
  8597. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8598. /* No slot left, emit const instead */
  8599. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 2) {
  8600. *offset = 0;
  8601. return true;
  8602. }
  8603. /* Traverse the list if the const num is small */
  8604. if (ctx->i64_const_num < 10) {
  8605. for (uint32 i = 0; i < ctx->i64_const_num; i++) {
  8606. if (ctx->i64_consts[i] == *(int64 *)value) {
  8607. *offset = -1;
  8608. return true;
  8609. }
  8610. }
  8611. }
  8612. if (ctx->i64_const_num >= ctx->i64_const_max_num) {
  8613. MEM_REALLOC(ctx->i64_consts,
  8614. sizeof(int64) * ctx->i64_const_max_num,
  8615. sizeof(int64) * (ctx->i64_const_max_num * 2));
  8616. ctx->i64_const_max_num *= 2;
  8617. }
  8618. ctx->i64_consts[ctx->i64_const_num++] = *(int64 *)value;
  8619. }
  8620. else if (type == VALUE_TYPE_V128) {
  8621. /* No slot left, emit const instead */
  8622. if (ctx->v128_const_num * 4 > INT16_MAX - 2) {
  8623. *offset = 0;
  8624. return true;
  8625. }
  8626. /* Traverse the list if the const num is small */
  8627. if (ctx->v128_const_num < 10) {
  8628. for (uint32 i = 0; i < ctx->v128_const_num; i++) {
  8629. if (memcmp(&ctx->v128_consts[i], value, sizeof(V128))
  8630. == 0) {
  8631. *offset = -1;
  8632. return true;
  8633. }
  8634. }
  8635. }
  8636. if (ctx->v128_const_num >= ctx->v128_const_max_num) {
  8637. MEM_REALLOC(ctx->v128_consts,
  8638. sizeof(V128) * ctx->v128_const_max_num,
  8639. sizeof(V128) * (ctx->v128_const_max_num * 2));
  8640. ctx->v128_const_max_num *= 2;
  8641. }
  8642. ctx->v128_consts[ctx->v128_const_num++] = *(V128 *)value;
  8643. }
  8644. else {
  8645. /* Treat i32 and f32 as the same by reading i32 value from
  8646. the raw bytes */
  8647. bh_assert(type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32);
  8648. /* No slot left, emit const instead */
  8649. if (ctx->i64_const_num * 2 + ctx->i32_const_num > INT16_MAX - 1) {
  8650. *offset = 0;
  8651. return true;
  8652. }
  8653. /* Traverse the list if the const num is small */
  8654. if (ctx->i32_const_num < 10) {
  8655. for (uint32 i = 0; i < ctx->i32_const_num; i++) {
  8656. if (ctx->i32_consts[i] == *(int32 *)value) {
  8657. *offset = -1;
  8658. return true;
  8659. }
  8660. }
  8661. }
  8662. if (ctx->i32_const_num >= ctx->i32_const_max_num) {
  8663. MEM_REALLOC(ctx->i32_consts,
  8664. sizeof(int32) * ctx->i32_const_max_num,
  8665. sizeof(int32) * (ctx->i32_const_max_num * 2));
  8666. ctx->i32_const_max_num *= 2;
  8667. }
  8668. ctx->i32_consts[ctx->i32_const_num++] = *(int32 *)value;
  8669. }
  8670. *offset = -1;
  8671. return true;
  8672. }
  8673. else {
  8674. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64) {
  8675. int64 key = *(int64 *)value, *i64_const;
  8676. i64_const = bsearch(&key, ctx->i64_consts, ctx->i64_const_num,
  8677. sizeof(int64), cmp_i64_const);
  8678. if (!i64_const) { /* not found, emit const instead */
  8679. *offset = 0;
  8680. return true;
  8681. }
  8682. *offset = -(uint32)(ctx->i64_const_num * 2 + ctx->i32_const_num)
  8683. + (uint32)(i64_const - ctx->i64_consts) * 2;
  8684. }
  8685. else if (type == VALUE_TYPE_V128) {
  8686. V128 key = *(V128 *)value, *v128_const;
  8687. v128_const = bsearch(&key, ctx->v128_consts, ctx->v128_const_num,
  8688. sizeof(V128), cmp_v128_const);
  8689. if (!v128_const) { /* not found, emit const instead */
  8690. *offset = 0;
  8691. return true;
  8692. }
  8693. *offset = -(uint32)(ctx->v128_const_num)
  8694. + (uint32)(v128_const - ctx->v128_consts);
  8695. }
  8696. else {
  8697. int32 key = *(int32 *)value, *i32_const;
  8698. i32_const = bsearch(&key, ctx->i32_consts, ctx->i32_const_num,
  8699. sizeof(int32), cmp_i32_const);
  8700. if (!i32_const) { /* not found, emit const instead */
  8701. *offset = 0;
  8702. return true;
  8703. }
  8704. *offset = -(uint32)(ctx->i32_const_num)
  8705. + (uint32)(i32_const - ctx->i32_consts);
  8706. }
  8707. return true;
  8708. }
  8709. fail:
  8710. return false;
  8711. }
  8712. /*
  8713. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8714. -- Mostly used for the binary / compare operation
  8715. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8716. -- Mostly used in block / control instructions
  8717. The POP will always emit the offset on the top of the frame_offset stack
  8718. PUSH can be used in two ways:
  8719. 1. directly PUSH:
  8720. PUSH_XXX();
  8721. will allocate a dynamic space and emit
  8722. 2. silent PUSH:
  8723. operand_offset = xxx; disable_emit = true;
  8724. PUSH_XXX();
  8725. only push the frame_offset stack, no emit
  8726. */
  8727. #define TEMPLATE_PUSH(Type) \
  8728. do { \
  8729. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8730. disable_emit, operand_offset, \
  8731. error_buf, error_buf_size)) \
  8732. goto fail; \
  8733. } while (0)
  8734. #define TEMPLATE_PUSH_REF(Type) \
  8735. do { \
  8736. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8737. operand_offset, error_buf, \
  8738. error_buf_size)) \
  8739. goto fail; \
  8740. } while (0)
  8741. #define TEMPLATE_POP(Type) \
  8742. do { \
  8743. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8744. error_buf, error_buf_size)) \
  8745. goto fail; \
  8746. } while (0)
  8747. #define TEMPLATE_POP_REF(Type) \
  8748. do { \
  8749. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8750. error_buf_size)) \
  8751. goto fail; \
  8752. } while (0)
  8753. #define PUSH_OFFSET_TYPE(type) \
  8754. do { \
  8755. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8756. operand_offset, error_buf, \
  8757. error_buf_size))) \
  8758. goto fail; \
  8759. } while (0)
  8760. #define POP_OFFSET_TYPE(type) \
  8761. do { \
  8762. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8763. error_buf_size))) \
  8764. goto fail; \
  8765. } while (0)
  8766. #define POP_AND_PUSH(type_pop, type_push) \
  8767. do { \
  8768. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8769. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8770. operand_offset, error_buf, error_buf_size))) \
  8771. goto fail; \
  8772. } while (0)
  8773. /* type of POPs should be the same */
  8774. #define POP2_AND_PUSH(type_pop, type_push) \
  8775. do { \
  8776. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8777. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8778. operand_offset, error_buf, error_buf_size))) \
  8779. goto fail; \
  8780. } while (0)
  8781. #else /* WASM_ENABLE_FAST_INTERP */
  8782. #define TEMPLATE_PUSH(Type) \
  8783. do { \
  8784. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8785. error_buf, error_buf_size))) \
  8786. goto fail; \
  8787. } while (0)
  8788. #define TEMPLATE_PUSH_REF(Type) \
  8789. do { \
  8790. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8791. error_buf_size))) \
  8792. goto fail; \
  8793. } while (0)
  8794. #define TEMPLATE_POP(Type) \
  8795. do { \
  8796. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8797. error_buf, error_buf_size))) \
  8798. goto fail; \
  8799. } while (0)
  8800. #define TEMPLATE_POP_REF(Type) \
  8801. do { \
  8802. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8803. error_buf_size))) \
  8804. goto fail; \
  8805. } while (0)
  8806. #define POP_AND_PUSH(type_pop, type_push) \
  8807. do { \
  8808. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8809. type_pop, error_buf, \
  8810. error_buf_size))) \
  8811. goto fail; \
  8812. } while (0)
  8813. /* type of POPs should be the same */
  8814. #define POP2_AND_PUSH(type_pop, type_push) \
  8815. do { \
  8816. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8817. type_pop, error_buf, \
  8818. error_buf_size))) \
  8819. goto fail; \
  8820. } while (0)
  8821. #endif /* WASM_ENABLE_FAST_INTERP */
  8822. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8823. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8824. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8825. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8826. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8827. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8828. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8829. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8830. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8831. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8832. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8833. #define PUSH_TBL_ELEM_IDX() TEMPLATE_PUSH_REF(table_elem_idx_type)
  8834. #define POP_I32() TEMPLATE_POP(I32)
  8835. #define POP_F32() TEMPLATE_POP(F32)
  8836. #define POP_I64() TEMPLATE_POP(I64)
  8837. #define POP_F64() TEMPLATE_POP(F64)
  8838. #define POP_V128() TEMPLATE_POP(V128)
  8839. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8840. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8841. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8842. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8843. #define POP_TBL_ELEM_IDX() TEMPLATE_POP_REF(table_elem_idx_type)
  8844. #if WASM_ENABLE_FAST_INTERP != 0
  8845. static bool
  8846. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8847. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8848. {
  8849. int16 operand_offset = 0;
  8850. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8851. : loader_ctx->frame_csp;
  8852. BlockType *block_type = &block->block_type;
  8853. uint8 *return_types = NULL;
  8854. #if WASM_ENABLE_GC != 0
  8855. WASMRefTypeMap *reftype_maps = NULL;
  8856. uint32 reftype_map_count;
  8857. #endif
  8858. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8859. int32 i = 0;
  8860. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8861. *frame_offset_org = NULL;
  8862. #if WASM_ENABLE_GC == 0
  8863. return_count = block_type_get_result_types(block_type, &return_types);
  8864. #else
  8865. return_count = block_type_get_result_types(
  8866. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8867. #endif
  8868. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64/V128
  8869. * instead of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8870. if (return_count == 1) {
  8871. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8872. if (block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8873. /* insert op_copy before else opcode */
  8874. if (opcode == WASM_OP_ELSE)
  8875. skip_label();
  8876. #if WASM_ENABLE_SIMDE != 0
  8877. if (cell == 4) {
  8878. emit_label(EXT_OP_COPY_STACK_TOP_V128);
  8879. }
  8880. #endif
  8881. if (cell <= 2) {
  8882. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8883. : EXT_OP_COPY_STACK_TOP_I64);
  8884. }
  8885. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8886. emit_operand(loader_ctx, block->dynamic_offset);
  8887. if (opcode == WASM_OP_ELSE) {
  8888. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8889. }
  8890. else {
  8891. loader_ctx->frame_offset -= cell;
  8892. loader_ctx->dynamic_offset = block->dynamic_offset;
  8893. PUSH_OFFSET_TYPE(return_types[0]);
  8894. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8895. }
  8896. if (opcode == WASM_OP_ELSE)
  8897. emit_label(opcode);
  8898. }
  8899. return true;
  8900. }
  8901. /* Copy stack top values to block's results which are in dynamic space.
  8902. * The instruction format:
  8903. * Part a: values count
  8904. * Part b: all values total cell num
  8905. * Part c: each value's cell_num, src offset and dst offset
  8906. * Part d: each value's src offset and dst offset
  8907. * Part e: each value's dst offset
  8908. */
  8909. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8910. dynamic_offset = dynamic_offset_org =
  8911. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8912. /* First traversal to get the count of values needed to be copied. */
  8913. for (i = (int32)return_count - 1; i >= 0; i--) {
  8914. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8915. if (frame_offset - cells < loader_ctx->frame_offset_bottom) {
  8916. set_error_buf(error_buf, error_buf_size, "frame offset underflow");
  8917. goto fail;
  8918. }
  8919. if (cells == 4) {
  8920. bool needs_copy = false;
  8921. int16 v128_dynamic = dynamic_offset - cells;
  8922. for (int j = 0; j < 4; j++) {
  8923. if (*(frame_offset - j - 1) != (v128_dynamic + j)) {
  8924. needs_copy = true;
  8925. break;
  8926. }
  8927. }
  8928. if (needs_copy) {
  8929. value_count++;
  8930. total_cel_num += cells;
  8931. }
  8932. frame_offset -= cells;
  8933. dynamic_offset = v128_dynamic;
  8934. }
  8935. else {
  8936. frame_offset -= cells;
  8937. dynamic_offset -= cells;
  8938. if (dynamic_offset != *frame_offset) {
  8939. value_count++;
  8940. total_cel_num += cells;
  8941. }
  8942. }
  8943. }
  8944. if (value_count) {
  8945. uint32 j = 0;
  8946. uint8 *emit_data = NULL, *cells = NULL;
  8947. int16 *src_offsets = NULL;
  8948. uint16 *dst_offsets = NULL;
  8949. uint64 size =
  8950. (uint64)value_count
  8951. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8952. /* Allocate memory for the emit data */
  8953. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8954. return false;
  8955. cells = emit_data;
  8956. src_offsets = (int16 *)(cells + value_count);
  8957. dst_offsets = (uint16 *)(src_offsets + value_count);
  8958. /* insert op_copy before else opcode */
  8959. if (opcode == WASM_OP_ELSE)
  8960. skip_label();
  8961. emit_label(EXT_OP_COPY_STACK_VALUES);
  8962. /* Part a) */
  8963. emit_uint32(loader_ctx, value_count);
  8964. /* Part b) */
  8965. emit_uint32(loader_ctx, total_cel_num);
  8966. /* Second traversal to get each value's cell num, src offset and dst
  8967. * offset. */
  8968. frame_offset = frame_offset_org;
  8969. dynamic_offset = dynamic_offset_org;
  8970. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8971. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8972. if (cell == 4) {
  8973. bool needs_copy = false;
  8974. int16 v128_dynamic = dynamic_offset - cell;
  8975. for (int k = 0; k < 4; k++) {
  8976. if (*(frame_offset - k - 1) != (v128_dynamic + k)) {
  8977. needs_copy = true;
  8978. break;
  8979. }
  8980. }
  8981. if (needs_copy) {
  8982. cells[j] = cell;
  8983. src_offsets[j] = *(frame_offset - cell);
  8984. dst_offsets[j] = v128_dynamic;
  8985. j++;
  8986. }
  8987. frame_offset -= cell;
  8988. dynamic_offset = v128_dynamic;
  8989. }
  8990. else {
  8991. frame_offset -= cell;
  8992. dynamic_offset -= cell;
  8993. if (dynamic_offset != *frame_offset) {
  8994. cells[j] = cell;
  8995. /* src offset */
  8996. src_offsets[j] = *frame_offset;
  8997. /* dst offset */
  8998. dst_offsets[j] = dynamic_offset;
  8999. j++;
  9000. }
  9001. }
  9002. if (opcode == WASM_OP_ELSE) {
  9003. if (cell == 4) {
  9004. for (int k = 0; k < cell; k++) {
  9005. *(frame_offset + k) = dynamic_offset + k;
  9006. }
  9007. }
  9008. else {
  9009. *frame_offset = dynamic_offset;
  9010. }
  9011. }
  9012. else {
  9013. loader_ctx->frame_offset = frame_offset;
  9014. loader_ctx->dynamic_offset = dynamic_offset;
  9015. if (!(wasm_loader_push_frame_offset(
  9016. loader_ctx, return_types[i], disable_emit,
  9017. operand_offset, error_buf, error_buf_size))) {
  9018. wasm_runtime_free(emit_data);
  9019. goto fail;
  9020. }
  9021. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9022. loader_ctx->frame_offset = frame_offset_org;
  9023. loader_ctx->dynamic_offset = dynamic_offset_org;
  9024. }
  9025. }
  9026. bh_assert(j == value_count);
  9027. /* Emit the cells, src_offsets and dst_offsets */
  9028. for (j = 0; j < value_count; j++)
  9029. emit_byte(loader_ctx, cells[j]);
  9030. for (j = 0; j < value_count; j++)
  9031. emit_operand(loader_ctx, src_offsets[j]);
  9032. for (j = 0; j < value_count; j++)
  9033. emit_operand(loader_ctx, dst_offsets[j]);
  9034. if (opcode == WASM_OP_ELSE)
  9035. emit_label(opcode);
  9036. wasm_runtime_free(emit_data);
  9037. }
  9038. return true;
  9039. fail:
  9040. return false;
  9041. }
  9042. #endif /* WASM_ENABLE_FAST_INTERP */
  9043. #define PUSH_TYPE(type) \
  9044. do { \
  9045. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  9046. error_buf_size))) \
  9047. goto fail; \
  9048. } while (0)
  9049. #define POP_TYPE(type) \
  9050. do { \
  9051. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  9052. error_buf_size))) \
  9053. goto fail; \
  9054. } while (0)
  9055. #if WASM_ENABLE_GC == 0
  9056. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9057. do { \
  9058. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9059. _start_addr, error_buf, \
  9060. error_buf_size)) \
  9061. goto fail; \
  9062. } while (0)
  9063. #define POP_CSP() \
  9064. do { \
  9065. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9066. goto fail; \
  9067. } while (0)
  9068. #else
  9069. #define PUSH_CSP(label_type, block_type, _start_addr) \
  9070. do { \
  9071. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  9072. _start_addr, error_buf, \
  9073. error_buf_size)) \
  9074. goto fail; \
  9075. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  9076. error_buf, error_buf_size)) { \
  9077. goto fail; \
  9078. } \
  9079. } while (0)
  9080. #define POP_CSP() \
  9081. do { \
  9082. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  9083. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  9084. goto fail; \
  9085. } while (0)
  9086. #endif /* end of WASM_ENABLE_GC == 0 */
  9087. #if WASM_ENABLE_GC == 0
  9088. #define GET_LOCAL_REFTYPE() (void)0
  9089. #else
  9090. #define GET_LOCAL_REFTYPE() \
  9091. do { \
  9092. if (wasm_is_type_multi_byte_type(local_type)) { \
  9093. WASMRefType *_ref_type; \
  9094. if (local_idx < param_count) \
  9095. _ref_type = wasm_reftype_map_find( \
  9096. param_reftype_maps, param_reftype_map_count, local_idx); \
  9097. else \
  9098. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  9099. local_reftype_map_count, \
  9100. local_idx - param_count); \
  9101. bh_assert(_ref_type); \
  9102. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  9103. wasm_reftype_struct_size(_ref_type)); \
  9104. } \
  9105. } while (0)
  9106. #endif /* end of WASM_ENABLE_GC == 0 */
  9107. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  9108. do { \
  9109. read_leb_uint32(p, p_end, local_idx); \
  9110. if (local_idx >= param_count + local_count) { \
  9111. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  9112. goto fail; \
  9113. } \
  9114. local_type = local_idx < param_count \
  9115. ? param_types[local_idx] \
  9116. : local_types[local_idx - param_count]; \
  9117. local_offset = local_offsets[local_idx]; \
  9118. GET_LOCAL_REFTYPE(); \
  9119. } while (0)
  9120. static bool
  9121. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  9122. {
  9123. if (module->memory_count == 0 && module->import_memory_count == 0) {
  9124. set_error_buf(error_buf, error_buf_size, "unknown memory");
  9125. return false;
  9126. }
  9127. return true;
  9128. }
  9129. #define CHECK_MEMORY() \
  9130. do { \
  9131. if (!check_memory(module, error_buf, error_buf_size)) \
  9132. goto fail; \
  9133. } while (0)
  9134. static bool
  9135. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9136. uint32 error_buf_size)
  9137. {
  9138. uint8 mem_access_aligns[] = {
  9139. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  9140. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  9141. };
  9142. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  9143. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  9144. set_error_buf(error_buf, error_buf_size,
  9145. "invalid memop flags: alignment must not be larger "
  9146. "than natural");
  9147. return false;
  9148. }
  9149. return true;
  9150. }
  9151. #if WASM_ENABLE_SIMD != 0
  9152. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  9153. || (WASM_ENABLE_FAST_INTERP != 0)
  9154. static bool
  9155. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  9156. uint32 error_buf_size)
  9157. {
  9158. uint8 mem_access_aligns[] = {
  9159. 4, /* load */
  9160. 3, 3, 3, 3, 3, 3, /* load and extend */
  9161. 0, 1, 2, 3, /* load and splat */
  9162. 4, /* store */
  9163. };
  9164. uint8 mem_access_aligns_load_lane[] = {
  9165. 0, 1, 2, 3, /* load lane */
  9166. 0, 1, 2, 3, /* store lane */
  9167. 2, 3 /* store zero */
  9168. };
  9169. if (!((opcode <= SIMD_v128_store)
  9170. || (SIMD_v128_load8_lane <= opcode
  9171. && opcode <= SIMD_v128_load64_zero))) {
  9172. set_error_buf(error_buf, error_buf_size,
  9173. "the opcode doesn't include memarg");
  9174. return false;
  9175. }
  9176. if ((opcode <= SIMD_v128_store
  9177. && align > mem_access_aligns[opcode - SIMD_v128_load])
  9178. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  9179. && align > mem_access_aligns_load_lane[opcode
  9180. - SIMD_v128_load8_lane])) {
  9181. set_error_buf(error_buf, error_buf_size,
  9182. "invalid memop flags: alignment must not be larger "
  9183. "than natural");
  9184. return false;
  9185. }
  9186. return true;
  9187. }
  9188. static bool
  9189. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  9190. uint32 error_buf_size)
  9191. {
  9192. switch (opcode) {
  9193. case SIMD_i8x16_extract_lane_s:
  9194. case SIMD_i8x16_extract_lane_u:
  9195. case SIMD_i8x16_replace_lane:
  9196. if (lane >= 16) {
  9197. goto fail;
  9198. }
  9199. break;
  9200. case SIMD_i16x8_extract_lane_s:
  9201. case SIMD_i16x8_extract_lane_u:
  9202. case SIMD_i16x8_replace_lane:
  9203. if (lane >= 8) {
  9204. goto fail;
  9205. }
  9206. break;
  9207. case SIMD_i32x4_extract_lane:
  9208. case SIMD_i32x4_replace_lane:
  9209. case SIMD_f32x4_extract_lane:
  9210. case SIMD_f32x4_replace_lane:
  9211. if (lane >= 4) {
  9212. goto fail;
  9213. }
  9214. break;
  9215. case SIMD_i64x2_extract_lane:
  9216. case SIMD_i64x2_replace_lane:
  9217. case SIMD_f64x2_extract_lane:
  9218. case SIMD_f64x2_replace_lane:
  9219. if (lane >= 2) {
  9220. goto fail;
  9221. }
  9222. break;
  9223. case SIMD_v128_load8_lane:
  9224. case SIMD_v128_load16_lane:
  9225. case SIMD_v128_load32_lane:
  9226. case SIMD_v128_load64_lane:
  9227. case SIMD_v128_store8_lane:
  9228. case SIMD_v128_store16_lane:
  9229. case SIMD_v128_store32_lane:
  9230. case SIMD_v128_store64_lane:
  9231. case SIMD_v128_load32_zero:
  9232. case SIMD_v128_load64_zero:
  9233. {
  9234. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  9235. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  9236. goto fail;
  9237. }
  9238. break;
  9239. }
  9240. default:
  9241. goto fail;
  9242. }
  9243. return true;
  9244. fail:
  9245. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9246. return false;
  9247. }
  9248. static bool
  9249. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  9250. {
  9251. uint8 i;
  9252. for (i = 0; i != 16; ++i) {
  9253. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  9254. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  9255. return false;
  9256. }
  9257. }
  9258. return true;
  9259. }
  9260. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  9261. #endif /* end of WASM_ENABLE_SIMD */
  9262. #if WASM_ENABLE_SHARED_MEMORY != 0
  9263. static bool
  9264. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  9265. uint32 error_buf_size)
  9266. {
  9267. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  9268. uint8 mem_access_aligns[] = {
  9269. 2, 3, 0, 1, 0, 1, 2,
  9270. };
  9271. uint8 expect;
  9272. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  9273. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  9274. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  9275. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  9276. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  9277. }
  9278. else {
  9279. /* 7 opcodes in every group */
  9280. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  9281. }
  9282. if (align != expect) {
  9283. set_error_buf(error_buf, error_buf_size,
  9284. "alignment isn't equal to natural");
  9285. return false;
  9286. }
  9287. return true;
  9288. }
  9289. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  9290. static bool
  9291. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  9292. char *error_buf, uint32 error_buf_size)
  9293. {
  9294. BranchBlock *target_block, *cur_block;
  9295. BlockType *target_block_type;
  9296. uint8 type, *types = NULL, *frame_ref;
  9297. uint32 arity = 0;
  9298. int32 i, available_stack_cell;
  9299. uint16 cell_num;
  9300. #if WASM_ENABLE_GC != 0
  9301. WASMRefTypeMap *frame_reftype_map;
  9302. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  9303. WASMRefType *ref_type;
  9304. uint32 reftype_map_count = 0;
  9305. int32 available_reftype_map;
  9306. bool is_type_multi_byte;
  9307. #endif
  9308. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9309. uint8 *frame_ref_after_popped = NULL;
  9310. uint8 frame_ref_tmp[4] = { 0 };
  9311. uint8 *frame_ref_buf = frame_ref_tmp;
  9312. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9313. #if WASM_ENABLE_GC != 0
  9314. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9315. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9316. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9317. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9318. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9319. #endif
  9320. #if WASM_ENABLE_FAST_INTERP != 0
  9321. int16 *frame_offset_old = loader_ctx->frame_offset;
  9322. int16 *frame_offset_after_popped = NULL;
  9323. int16 frame_offset_tmp[4] = { 0 };
  9324. int16 *frame_offset_buf = frame_offset_tmp;
  9325. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9326. #endif
  9327. bool ret = false;
  9328. bh_assert(loader_ctx->csp_num > 0);
  9329. if (loader_ctx->csp_num - 1 < depth) {
  9330. set_error_buf(error_buf, error_buf_size,
  9331. "unknown label, "
  9332. "unexpected end of section or function");
  9333. return false;
  9334. }
  9335. cur_block = loader_ctx->frame_csp - 1;
  9336. target_block = loader_ctx->frame_csp - (depth + 1);
  9337. target_block_type = &target_block->block_type;
  9338. frame_ref = loader_ctx->frame_ref;
  9339. #if WASM_ENABLE_GC != 0
  9340. frame_reftype_map = loader_ctx->frame_reftype_map;
  9341. #endif
  9342. /* Note: loop's arity is different from if and block. loop's arity is
  9343. * its parameter count while if and block arity is result count.
  9344. */
  9345. #if WASM_ENABLE_GC == 0
  9346. if (target_block->label_type == LABEL_TYPE_LOOP)
  9347. arity = block_type_get_param_types(target_block_type, &types);
  9348. else
  9349. arity = block_type_get_result_types(target_block_type, &types);
  9350. #else
  9351. if (target_block->label_type == LABEL_TYPE_LOOP)
  9352. arity = block_type_get_param_types(target_block_type, &types,
  9353. &reftype_maps, &reftype_map_count);
  9354. else
  9355. arity = block_type_get_result_types(target_block_type, &types,
  9356. &reftype_maps, &reftype_map_count);
  9357. #endif
  9358. /* If the stack is in polymorphic state, just clear the stack
  9359. * and then re-push the values to make the stack top values
  9360. * match block type. */
  9361. if (cur_block->is_stack_polymorphic) {
  9362. #if WASM_ENABLE_GC != 0
  9363. int32 j = reftype_map_count - 1;
  9364. #endif
  9365. for (i = (int32)arity - 1; i >= 0; i--) {
  9366. #if WASM_ENABLE_GC != 0
  9367. if (wasm_is_type_multi_byte_type(types[i])) {
  9368. bh_assert(reftype_maps[j].index == i);
  9369. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9370. reftype_maps[j].ref_type,
  9371. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9372. j--;
  9373. }
  9374. #endif
  9375. #if WASM_ENABLE_FAST_INTERP != 0
  9376. POP_OFFSET_TYPE(types[i]);
  9377. #endif
  9378. POP_TYPE(types[i]);
  9379. }
  9380. /* Backup stack data since it may be changed in the below
  9381. push operations, and the stack data may be used when
  9382. checking other target blocks of opcode br_table */
  9383. if (opcode == WASM_OP_BR_TABLE) {
  9384. uint64 total_size;
  9385. frame_ref_after_popped = loader_ctx->frame_ref;
  9386. total_size = (uint64)sizeof(uint8)
  9387. * (frame_ref_old - frame_ref_after_popped);
  9388. if (total_size > sizeof(frame_ref_tmp)
  9389. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9390. error_buf_size))) {
  9391. goto fail;
  9392. }
  9393. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9394. frame_ref_after_popped, (uint32)total_size);
  9395. #if WASM_ENABLE_GC != 0
  9396. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9397. total_size =
  9398. (uint64)sizeof(WASMRefTypeMap)
  9399. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9400. if (total_size > sizeof(frame_reftype_map_tmp)
  9401. && !(frame_reftype_map_buf = loader_malloc(
  9402. total_size, error_buf, error_buf_size))) {
  9403. goto fail;
  9404. }
  9405. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9406. frame_reftype_map_after_popped, (uint32)total_size);
  9407. #endif
  9408. #if WASM_ENABLE_FAST_INTERP != 0
  9409. frame_offset_after_popped = loader_ctx->frame_offset;
  9410. total_size = (uint64)sizeof(int16)
  9411. * (frame_offset_old - frame_offset_after_popped);
  9412. if (total_size > sizeof(frame_offset_tmp)
  9413. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9414. error_buf_size))) {
  9415. goto fail;
  9416. }
  9417. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9418. frame_offset_after_popped, (uint32)total_size);
  9419. #endif
  9420. }
  9421. #if WASM_ENABLE_GC != 0
  9422. j = 0;
  9423. #endif
  9424. for (i = 0; i < (int32)arity; i++) {
  9425. #if WASM_ENABLE_GC != 0
  9426. if (wasm_is_type_multi_byte_type(types[i])) {
  9427. bh_assert(reftype_maps[j].index == i);
  9428. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9429. reftype_maps[j].ref_type,
  9430. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9431. j++;
  9432. }
  9433. #endif
  9434. #if WASM_ENABLE_FAST_INTERP != 0
  9435. bool disable_emit = true;
  9436. int16 operand_offset = 0;
  9437. PUSH_OFFSET_TYPE(types[i]);
  9438. #endif
  9439. PUSH_TYPE(types[i]);
  9440. }
  9441. #if WASM_ENABLE_FAST_INTERP != 0
  9442. emit_br_info(target_block, opcode == WASM_OP_BR);
  9443. #endif
  9444. /* Restore the stack data, note that frame_ref_bottom,
  9445. frame_reftype_map_bottom, frame_offset_bottom may be
  9446. re-allocated in the above push operations */
  9447. if (opcode == WASM_OP_BR_TABLE) {
  9448. uint32 total_size;
  9449. /* The stack operand num should not be smaller than before
  9450. after pop and push operations */
  9451. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9452. loader_ctx->stack_cell_num = stack_cell_num_old;
  9453. loader_ctx->frame_ref =
  9454. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9455. total_size = (uint32)(sizeof(uint8)
  9456. * (frame_ref_old - frame_ref_after_popped));
  9457. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9458. frame_ref_buf, total_size);
  9459. #if WASM_ENABLE_GC != 0
  9460. /* The stack operand num should not be smaller than before
  9461. after pop and push operations */
  9462. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9463. loader_ctx->reftype_map_num = reftype_map_num_old;
  9464. loader_ctx->frame_reftype_map =
  9465. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9466. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9467. * (frame_reftype_map_old
  9468. - frame_reftype_map_after_popped));
  9469. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9470. total_size, frame_reftype_map_buf, total_size);
  9471. #endif
  9472. #if WASM_ENABLE_FAST_INTERP != 0
  9473. loader_ctx->frame_offset =
  9474. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9475. total_size =
  9476. (uint32)(sizeof(int16)
  9477. * (frame_offset_old - frame_offset_after_popped));
  9478. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9479. total_size, frame_offset_buf, total_size);
  9480. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9481. #endif
  9482. }
  9483. ret = true;
  9484. goto cleanup_and_return;
  9485. }
  9486. available_stack_cell =
  9487. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9488. #if WASM_ENABLE_GC != 0
  9489. available_reftype_map =
  9490. (int32)(loader_ctx->reftype_map_num
  9491. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9492. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9493. #endif
  9494. /* Check stack top values match target block type */
  9495. for (i = (int32)arity - 1; i >= 0; i--) {
  9496. type = types[i];
  9497. #if WASM_ENABLE_GC != 0
  9498. ref_type = NULL;
  9499. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9500. if (is_type_multi_byte) {
  9501. bh_assert(reftype_map);
  9502. ref_type = reftype_map->ref_type;
  9503. }
  9504. #endif
  9505. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9506. break;
  9507. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9508. #if WASM_ENABLE_GC != 0
  9509. frame_reftype_map, available_reftype_map,
  9510. #endif
  9511. type,
  9512. #if WASM_ENABLE_GC != 0
  9513. ref_type,
  9514. #endif
  9515. error_buf, error_buf_size)) {
  9516. goto fail;
  9517. }
  9518. cell_num = wasm_value_type_cell_num(types[i]);
  9519. frame_ref -= cell_num;
  9520. available_stack_cell -= cell_num;
  9521. #if WASM_ENABLE_GC != 0
  9522. if (is_type_multi_byte) {
  9523. frame_reftype_map--;
  9524. available_reftype_map--;
  9525. reftype_map--;
  9526. }
  9527. #endif
  9528. }
  9529. #if WASM_ENABLE_FAST_INTERP != 0
  9530. emit_br_info(target_block, opcode == WASM_OP_BR);
  9531. #endif
  9532. ret = true;
  9533. cleanup_and_return:
  9534. fail:
  9535. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9536. wasm_runtime_free(frame_ref_buf);
  9537. #if WASM_ENABLE_GC != 0
  9538. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9539. wasm_runtime_free(frame_reftype_map_buf);
  9540. #endif
  9541. #if WASM_ENABLE_FAST_INTERP != 0
  9542. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9543. wasm_runtime_free(frame_offset_buf);
  9544. #endif
  9545. return ret;
  9546. }
  9547. static BranchBlock *
  9548. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9549. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9550. {
  9551. uint8 *p = *p_buf, *p_end = buf_end;
  9552. BranchBlock *frame_csp_tmp;
  9553. uint32 depth;
  9554. read_leb_uint32(p, p_end, depth);
  9555. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9556. error_buf_size)) {
  9557. goto fail;
  9558. }
  9559. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9560. *p_buf = p;
  9561. return frame_csp_tmp;
  9562. fail:
  9563. return NULL;
  9564. }
  9565. #if WASM_ENABLE_EXCE_HANDLING != 0
  9566. static BranchBlock *
  9567. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9568. uint8 *buf_end, char *error_buf,
  9569. uint32 error_buf_size)
  9570. {
  9571. uint8 *p = *p_buf, *p_end = buf_end;
  9572. BranchBlock *frame_csp_tmp;
  9573. uint32 depth;
  9574. read_leb_uint32(p, p_end, depth);
  9575. /*
  9576. * Note: "delegate 0" means the surrounding block, not the
  9577. * try-delegate block itself.
  9578. *
  9579. * Note: the caller hasn't popped the try-delegate frame yet.
  9580. */
  9581. bh_assert(loader_ctx->csp_num > 0);
  9582. if (loader_ctx->csp_num - 1 <= depth) {
  9583. #if WASM_ENABLE_SPEC_TEST == 0
  9584. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9585. #else
  9586. set_error_buf(error_buf, error_buf_size, "unknown label");
  9587. #endif
  9588. goto fail;
  9589. }
  9590. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9591. #if WASM_ENABLE_FAST_INTERP != 0
  9592. emit_br_info(frame_csp_tmp, false);
  9593. #endif
  9594. *p_buf = p;
  9595. return frame_csp_tmp;
  9596. fail:
  9597. return NULL;
  9598. }
  9599. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9600. static bool
  9601. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9602. char *error_buf, uint32 error_buf_size)
  9603. {
  9604. BlockType *block_type = &block->block_type;
  9605. uint8 *return_types = NULL;
  9606. uint32 return_count = 0;
  9607. int32 available_stack_cell, return_cell_num, i;
  9608. uint8 *frame_ref = NULL;
  9609. #if WASM_ENABLE_GC != 0
  9610. WASMRefTypeMap *frame_reftype_map;
  9611. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9612. WASMRefType *ref_type;
  9613. uint32 param_count, return_reftype_map_count = 0;
  9614. int32 available_reftype_map =
  9615. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9616. #endif
  9617. available_stack_cell =
  9618. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9619. #if WASM_ENABLE_GC == 0
  9620. return_count = block_type_get_result_types(block_type, &return_types);
  9621. #else
  9622. return_count = block_type_get_result_types(block_type, &return_types,
  9623. &return_reftype_maps,
  9624. &return_reftype_map_count);
  9625. param_count =
  9626. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9627. (void)param_count;
  9628. #endif
  9629. return_cell_num =
  9630. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9631. /* If the stack is in polymorphic state, just clear the stack
  9632. * and then re-push the values to make the stack top values
  9633. * match block type. */
  9634. if (block->is_stack_polymorphic) {
  9635. #if WASM_ENABLE_GC != 0
  9636. int32 j = return_reftype_map_count - 1;
  9637. #endif
  9638. for (i = (int32)return_count - 1; i >= 0; i--) {
  9639. #if WASM_ENABLE_GC != 0
  9640. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9641. bh_assert(return_reftype_maps[j].index == i + param_count);
  9642. bh_memcpy_s(
  9643. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9644. return_reftype_maps[j].ref_type,
  9645. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9646. j--;
  9647. }
  9648. #endif
  9649. #if WASM_ENABLE_FAST_INTERP != 0
  9650. POP_OFFSET_TYPE(return_types[i]);
  9651. #endif
  9652. POP_TYPE(return_types[i]);
  9653. }
  9654. /* Check stack is empty */
  9655. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9656. set_error_buf(
  9657. error_buf, error_buf_size,
  9658. "type mismatch: stack size does not match block type");
  9659. goto fail;
  9660. }
  9661. #if WASM_ENABLE_GC != 0
  9662. j = 0;
  9663. #endif
  9664. for (i = 0; i < (int32)return_count; i++) {
  9665. #if WASM_ENABLE_GC != 0
  9666. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9667. bh_assert(return_reftype_maps[j].index == i + param_count);
  9668. bh_memcpy_s(
  9669. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9670. return_reftype_maps[j].ref_type,
  9671. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9672. j++;
  9673. }
  9674. #endif
  9675. #if WASM_ENABLE_FAST_INTERP != 0
  9676. bool disable_emit = true;
  9677. int16 operand_offset = 0;
  9678. PUSH_OFFSET_TYPE(return_types[i]);
  9679. #endif
  9680. PUSH_TYPE(return_types[i]);
  9681. }
  9682. return true;
  9683. }
  9684. if (available_stack_cell != return_cell_num) {
  9685. #if WASM_ENABLE_EXCE_HANDLING != 0
  9686. /* testspec: this error message format is expected by try_catch.wast */
  9687. snprintf(
  9688. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9689. block->label_type == LABEL_TYPE_TRY
  9690. || (block->label_type == LABEL_TYPE_CATCH
  9691. && return_cell_num > 0)
  9692. ? "instruction"
  9693. : "block",
  9694. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9695. " but stack has ",
  9696. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9697. : "");
  9698. goto fail;
  9699. #else
  9700. set_error_buf(error_buf, error_buf_size,
  9701. "type mismatch: stack size does not match block type");
  9702. goto fail;
  9703. #endif
  9704. }
  9705. /* Check stack values match return types */
  9706. frame_ref = loader_ctx->frame_ref;
  9707. #if WASM_ENABLE_GC != 0
  9708. frame_reftype_map = loader_ctx->frame_reftype_map;
  9709. return_reftype_map =
  9710. return_reftype_map_count
  9711. ? return_reftype_maps + return_reftype_map_count - 1
  9712. : NULL;
  9713. #endif
  9714. for (i = (int32)return_count - 1; i >= 0; i--) {
  9715. uint8 type = return_types[i];
  9716. #if WASM_ENABLE_GC != 0
  9717. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9718. ref_type = NULL;
  9719. if (is_type_multi_byte) {
  9720. bh_assert(return_reftype_map);
  9721. ref_type = return_reftype_map->ref_type;
  9722. }
  9723. #endif
  9724. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9725. #if WASM_ENABLE_GC != 0
  9726. frame_reftype_map, available_reftype_map,
  9727. #endif
  9728. type,
  9729. #if WASM_ENABLE_GC != 0
  9730. ref_type,
  9731. #endif
  9732. error_buf, error_buf_size))
  9733. return false;
  9734. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9735. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9736. #if WASM_ENABLE_GC != 0
  9737. if (is_type_multi_byte) {
  9738. frame_reftype_map--;
  9739. available_reftype_map--;
  9740. return_reftype_map--;
  9741. }
  9742. #endif
  9743. }
  9744. return true;
  9745. fail:
  9746. return false;
  9747. }
  9748. #if WASM_ENABLE_FAST_INTERP != 0
  9749. /* Copy parameters to dynamic space.
  9750. * 1) POP original parameter out;
  9751. * 2) Push and copy original values to dynamic space.
  9752. * The copy instruction format:
  9753. * Part a: param count
  9754. * Part b: all param total cell num
  9755. * Part c: each param's cell_num, src offset and dst offset
  9756. * Part d: each param's src offset
  9757. * Part e: each param's dst offset
  9758. */
  9759. static bool
  9760. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, char *error_buf,
  9761. uint32 error_buf_size)
  9762. {
  9763. bool ret = false;
  9764. int16 *frame_offset = NULL;
  9765. uint8 *cells = NULL, cell;
  9766. int16 *src_offsets = NULL;
  9767. uint8 *emit_data = NULL;
  9768. uint32 i;
  9769. BranchBlock *block = loader_ctx->frame_csp - 1;
  9770. BlockType *block_type = &block->block_type;
  9771. WASMFuncType *wasm_type = block_type->u.type;
  9772. uint32 param_count = block_type->u.type->param_count;
  9773. int16 condition_offset = 0;
  9774. bool disable_emit = false;
  9775. bool is_if_block = (block->label_type == LABEL_TYPE_IF ? true : false);
  9776. int16 operand_offset = 0;
  9777. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9778. bh_assert(size > 0);
  9779. /* For if block, we also need copy the condition operand offset. */
  9780. if (is_if_block)
  9781. size += sizeof(*cells) + sizeof(*src_offsets);
  9782. /* Allocate memory for the emit data */
  9783. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9784. return false;
  9785. cells = emit_data;
  9786. src_offsets = (int16 *)(cells + param_count);
  9787. if (is_if_block)
  9788. condition_offset = *loader_ctx->frame_offset;
  9789. /* POP original parameter out */
  9790. for (i = 0; i < param_count; i++) {
  9791. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9792. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9793. }
  9794. frame_offset = loader_ctx->frame_offset;
  9795. /* Get each param's cell num and src offset */
  9796. for (i = 0; i < param_count; i++) {
  9797. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9798. cells[i] = cell;
  9799. src_offsets[i] = *frame_offset;
  9800. frame_offset += cell;
  9801. }
  9802. /* emit copy instruction */
  9803. emit_label(EXT_OP_COPY_STACK_VALUES);
  9804. /* Part a) */
  9805. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9806. /* Part b) */
  9807. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9808. : wasm_type->param_cell_num);
  9809. /* Part c) */
  9810. for (i = 0; i < param_count; i++)
  9811. emit_byte(loader_ctx, cells[i]);
  9812. if (is_if_block)
  9813. emit_byte(loader_ctx, 1);
  9814. /* Part d) */
  9815. for (i = 0; i < param_count; i++)
  9816. emit_operand(loader_ctx, src_offsets[i]);
  9817. if (is_if_block)
  9818. emit_operand(loader_ctx, condition_offset);
  9819. /* Since the start offset to save the block's params and
  9820. * the start offset to save the block's results may be
  9821. * different, we remember the dynamic offset for loop block
  9822. * so that we can use it to copy the stack operands to the
  9823. * loop block's params in wasm_loader_emit_br_info. */
  9824. if (block->label_type == LABEL_TYPE_LOOP)
  9825. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9826. /* Part e) */
  9827. /* Push to dynamic space. The push will emit the dst offset. */
  9828. for (i = 0; i < param_count; i++)
  9829. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9830. if (is_if_block)
  9831. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9832. ret = true;
  9833. fail:
  9834. /* Free the emit data */
  9835. wasm_runtime_free(emit_data);
  9836. return ret;
  9837. }
  9838. #endif
  9839. #if WASM_ENABLE_GC == 0
  9840. #define RESET_REFTYPE_MAP_STACK() (void)0
  9841. #else
  9842. #define RESET_REFTYPE_MAP_STACK() \
  9843. do { \
  9844. loader_ctx->reftype_map_num = \
  9845. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9846. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9847. + loader_ctx->reftype_map_num; \
  9848. } while (0)
  9849. #endif
  9850. /* reset the stack to the state of before entering the last block */
  9851. #if WASM_ENABLE_FAST_INTERP != 0
  9852. #define RESET_STACK() \
  9853. do { \
  9854. loader_ctx->stack_cell_num = \
  9855. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9856. loader_ctx->frame_ref = \
  9857. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9858. loader_ctx->frame_offset = \
  9859. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9860. RESET_REFTYPE_MAP_STACK(); \
  9861. } while (0)
  9862. #else
  9863. #define RESET_STACK() \
  9864. do { \
  9865. loader_ctx->stack_cell_num = \
  9866. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9867. loader_ctx->frame_ref = \
  9868. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9869. RESET_REFTYPE_MAP_STACK(); \
  9870. } while (0)
  9871. #endif
  9872. /* set current block's stack polymorphic state */
  9873. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9874. do { \
  9875. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9876. _cur_block->is_stack_polymorphic = flag; \
  9877. } while (0)
  9878. #define BLOCK_HAS_PARAM(block_type) \
  9879. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9880. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9881. do { \
  9882. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9883. error_buf_size))) { \
  9884. goto fail; \
  9885. } \
  9886. } while (0)
  9887. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9888. static bool
  9889. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9890. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9891. uint32 error_buf_size)
  9892. {
  9893. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9894. return false;
  9895. }
  9896. if (table_idx < module->import_table_count) {
  9897. if (p_elem_type)
  9898. *p_elem_type =
  9899. module->import_tables[table_idx].u.table.table_type.elem_type;
  9900. #if WASM_ENABLE_GC != 0
  9901. if (p_ref_type)
  9902. *((WASMRefType **)p_ref_type) =
  9903. module->import_tables[table_idx]
  9904. .u.table.table_type.elem_ref_type;
  9905. #endif
  9906. }
  9907. else {
  9908. if (p_elem_type)
  9909. *p_elem_type =
  9910. module->tables[table_idx - module->import_table_count]
  9911. .table_type.elem_type;
  9912. #if WASM_ENABLE_GC != 0
  9913. if (p_ref_type)
  9914. *((WASMRefType **)p_ref_type) =
  9915. module->tables[table_idx - module->import_table_count]
  9916. .table_type.elem_ref_type;
  9917. #endif
  9918. }
  9919. return true;
  9920. }
  9921. static bool
  9922. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9923. uint8 *p_elem_type, void **p_elem_ref_type,
  9924. char *error_buf, uint32 error_buf_size)
  9925. {
  9926. if (table_seg_idx >= module->table_seg_count) {
  9927. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9928. table_seg_idx);
  9929. return false;
  9930. }
  9931. if (p_elem_type) {
  9932. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9933. }
  9934. #if WASM_ENABLE_GC != 0
  9935. if (p_elem_ref_type)
  9936. *((WASMRefType **)p_elem_ref_type) =
  9937. module->table_segments[table_seg_idx].elem_ref_type;
  9938. #endif
  9939. return true;
  9940. }
  9941. #endif
  9942. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9943. const uint8 *
  9944. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9945. uint32 *len)
  9946. {
  9947. WASMCustomSection *section = module->custom_section_list;
  9948. while (section) {
  9949. if ((section->name_len == strlen(name))
  9950. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9951. if (len) {
  9952. *len = section->content_len;
  9953. }
  9954. return section->content_addr;
  9955. }
  9956. section = section->next;
  9957. }
  9958. return NULL;
  9959. }
  9960. #endif
  9961. #if 0
  9962. #define HANDLE_OPCODE(opcode) #opcode
  9963. DEFINE_GOTO_TABLE(const char *, op_mnemonics);
  9964. #undef HANDLE_OPCODE
  9965. #endif
  9966. #if WASM_ENABLE_FAST_INTERP == 0
  9967. #define pb_read_leb_uint32 read_leb_uint32
  9968. #define pb_read_leb_int32 read_leb_int32
  9969. #define pb_read_leb_int64 read_leb_int64
  9970. #define pb_read_leb_memarg read_leb_memarg
  9971. #define pb_read_leb_mem_offset read_leb_mem_offset
  9972. #else
  9973. /* Read leb without malformed format check */
  9974. static uint64
  9975. read_leb_quick(uint8 **p_buf, uint32 maxbits, bool sign)
  9976. {
  9977. uint8 *buf = *p_buf;
  9978. uint64 result = 0, byte = 0;
  9979. uint32 shift = 0;
  9980. do {
  9981. byte = *buf++;
  9982. result |= ((byte & 0x7f) << shift);
  9983. shift += 7;
  9984. } while (byte & 0x80);
  9985. if (sign && (shift < maxbits) && (byte & 0x40)) {
  9986. /* Sign extend */
  9987. result |= (~((uint64)0)) << shift;
  9988. }
  9989. *p_buf = buf;
  9990. return result;
  9991. }
  9992. #define pb_read_leb_uint32(p, p_end, res) \
  9993. do { \
  9994. if (!loader_ctx->p_code_compiled) \
  9995. /* Enable format check in the first scan */ \
  9996. read_leb_uint32(p, p_end, res); \
  9997. else \
  9998. /* Disable format check in the second scan */ \
  9999. res = (uint32)read_leb_quick(&p, 32, false); \
  10000. } while (0)
  10001. #define pb_read_leb_int32(p, p_end, res) \
  10002. do { \
  10003. if (!loader_ctx->p_code_compiled) \
  10004. /* Enable format check in the first scan */ \
  10005. read_leb_int32(p, p_end, res); \
  10006. else \
  10007. /* Disable format check in the second scan */ \
  10008. res = (int32)read_leb_quick(&p, 32, true); \
  10009. } while (0)
  10010. #define pb_read_leb_int64(p, p_end, res) \
  10011. do { \
  10012. if (!loader_ctx->p_code_compiled) \
  10013. /* Enable format check in the first scan */ \
  10014. read_leb_int64(p, p_end, res); \
  10015. else \
  10016. /* Disable format check in the second scan */ \
  10017. res = (int64)read_leb_quick(&p, 64, true); \
  10018. } while (0)
  10019. #if WASM_ENABLE_MULTI_MEMORY != 0
  10020. #define pb_read_leb_memarg read_leb_memarg
  10021. #else
  10022. #define pb_read_leb_memarg pb_read_leb_uint32
  10023. #endif
  10024. #if WASM_ENABLE_MEMORY64 != 0
  10025. #define pb_read_leb_mem_offset read_leb_mem_offset
  10026. #else
  10027. #define pb_read_leb_mem_offset pb_read_leb_uint32
  10028. #endif
  10029. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  10030. static bool
  10031. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  10032. uint32 cur_func_idx, char *error_buf,
  10033. uint32 error_buf_size)
  10034. {
  10035. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  10036. uint32 param_count, local_count, global_count;
  10037. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type,
  10038. table_elem_idx_type;
  10039. BlockType func_block_type;
  10040. uint16 *local_offsets, local_offset;
  10041. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  10042. uint32 table_seg_idx, data_seg_idx, count, align, i;
  10043. mem_offset_t mem_offset;
  10044. int32 i32_const = 0;
  10045. int64 i64_const;
  10046. uint8 opcode;
  10047. bool return_value = false;
  10048. WASMLoaderContext *loader_ctx;
  10049. BranchBlock *frame_csp_tmp;
  10050. #if WASM_ENABLE_GC != 0
  10051. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  10052. uint32 param_reftype_map_count, local_reftype_map_count;
  10053. int32 heap_type;
  10054. WASMRefType wasm_ref_type = { 0 };
  10055. bool need_ref_type_map;
  10056. #endif
  10057. #if WASM_ENABLE_FAST_INTERP != 0
  10058. int16 operand_offset = 0;
  10059. uint8 last_op = 0;
  10060. bool disable_emit, preserve_local = false, if_condition_available = true;
  10061. float32 f32_const;
  10062. float64 f64_const;
  10063. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  10064. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  10065. #endif
  10066. #if WASM_ENABLE_MEMORY64 != 0
  10067. bool is_memory64 = has_module_memory64(module);
  10068. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  10069. #else
  10070. mem_offset_type = VALUE_TYPE_I32;
  10071. table_elem_idx_type = VALUE_TYPE_I32;
  10072. #endif
  10073. uint32 memidx;
  10074. global_count = module->import_global_count + module->global_count;
  10075. param_count = func->func_type->param_count;
  10076. param_types = func->func_type->types;
  10077. func_block_type.is_value_type = false;
  10078. func_block_type.u.type = func->func_type;
  10079. local_count = func->local_count;
  10080. local_types = func->local_types;
  10081. local_offsets = func->local_offsets;
  10082. #if WASM_ENABLE_GC != 0
  10083. param_reftype_maps = func->func_type->ref_type_maps;
  10084. param_reftype_map_count = func->func_type->ref_type_map_count;
  10085. local_reftype_maps = func->local_ref_type_maps;
  10086. local_reftype_map_count = func->local_ref_type_map_count;
  10087. #endif
  10088. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  10089. goto fail;
  10090. }
  10091. #if WASM_ENABLE_GC != 0
  10092. loader_ctx->module = module;
  10093. loader_ctx->ref_type_set = module->ref_type_set;
  10094. loader_ctx->ref_type_tmp = &wasm_ref_type;
  10095. #endif
  10096. #if WASM_ENABLE_FAST_INTERP != 0
  10097. /* For the first traverse, the initial value of preserved_local_offset has
  10098. * not been determined, we use the INT16_MAX to represent that a slot has
  10099. * been copied to preserve space. For second traverse, this field will be
  10100. * set to the appropriate value in wasm_loader_ctx_reinit.
  10101. * This is for Issue #1230,
  10102. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  10103. * drop opcodes need to know which slots are preserved, so those slots will
  10104. * not be treated as dynamically allocated slots */
  10105. loader_ctx->preserved_local_offset = INT16_MAX;
  10106. re_scan:
  10107. if (loader_ctx->code_compiled_size > 0) {
  10108. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  10109. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  10110. goto fail;
  10111. }
  10112. p = func->code;
  10113. func->code_compiled = loader_ctx->p_code_compiled;
  10114. func->code_compiled_size = loader_ctx->code_compiled_size;
  10115. if (loader_ctx->i64_const_num > 0) {
  10116. int64 *i64_consts_old = loader_ctx->i64_consts;
  10117. /* Sort the i64 consts */
  10118. qsort(i64_consts_old, loader_ctx->i64_const_num, sizeof(int64),
  10119. cmp_i64_const);
  10120. /* Remove the duplicated i64 consts */
  10121. uint32 k = 1;
  10122. for (i = 1; i < loader_ctx->i64_const_num; i++) {
  10123. if (i64_consts_old[i] != i64_consts_old[i - 1]) {
  10124. i64_consts_old[k++] = i64_consts_old[i];
  10125. }
  10126. }
  10127. if (k < loader_ctx->i64_const_num) {
  10128. int64 *i64_consts_new;
  10129. /* Try to reallocate memory with a smaller size */
  10130. if ((i64_consts_new =
  10131. wasm_runtime_malloc((uint32)sizeof(int64) * k))) {
  10132. bh_memcpy_s(i64_consts_new, (uint32)sizeof(int64) * k,
  10133. i64_consts_old, (uint32)sizeof(int64) * k);
  10134. /* Free the old memory */
  10135. wasm_runtime_free(i64_consts_old);
  10136. loader_ctx->i64_consts = i64_consts_new;
  10137. loader_ctx->i64_const_max_num = k;
  10138. }
  10139. loader_ctx->i64_const_num = k;
  10140. }
  10141. }
  10142. if (loader_ctx->v128_const_num > 0) {
  10143. V128 *v128_consts_old = loader_ctx->v128_consts;
  10144. /* Sort the v128 consts */
  10145. qsort(v128_consts_old, loader_ctx->v128_const_num, sizeof(V128),
  10146. cmp_v128_const);
  10147. /* Remove the duplicated v128 consts */
  10148. uint32 k = 1;
  10149. for (i = 1; i < loader_ctx->v128_const_num; i++) {
  10150. if (!(memcmp(&v128_consts_old[i], &v128_consts_old[i - 1],
  10151. sizeof(V128))
  10152. == 0)) {
  10153. v128_consts_old[k++] = v128_consts_old[i];
  10154. }
  10155. }
  10156. if (k < loader_ctx->v128_const_num) {
  10157. V128 *v128_consts_new;
  10158. /* Try to reallocate memory with a smaller size */
  10159. if ((v128_consts_new =
  10160. wasm_runtime_malloc((uint32)sizeof(V128) * k))) {
  10161. bh_memcpy_s(v128_consts_new, (uint32)sizeof(V128) * k,
  10162. v128_consts_old, (uint32)sizeof(V128) * k);
  10163. /* Free the old memory */
  10164. wasm_runtime_free(v128_consts_old);
  10165. loader_ctx->v128_consts = v128_consts_new;
  10166. loader_ctx->v128_const_max_num = k;
  10167. }
  10168. loader_ctx->v128_const_num = k;
  10169. }
  10170. }
  10171. if (loader_ctx->i32_const_num > 0) {
  10172. int32 *i32_consts_old = loader_ctx->i32_consts;
  10173. /* Sort the i32 consts */
  10174. qsort(i32_consts_old, loader_ctx->i32_const_num, sizeof(int32),
  10175. cmp_i32_const);
  10176. /* Remove the duplicated i32 consts */
  10177. uint32 k = 1;
  10178. for (i = 1; i < loader_ctx->i32_const_num; i++) {
  10179. if (i32_consts_old[i] != i32_consts_old[i - 1]) {
  10180. i32_consts_old[k++] = i32_consts_old[i];
  10181. }
  10182. }
  10183. if (k < loader_ctx->i32_const_num) {
  10184. int32 *i32_consts_new;
  10185. /* Try to reallocate memory with a smaller size */
  10186. if ((i32_consts_new =
  10187. wasm_runtime_malloc((uint32)sizeof(int32) * k))) {
  10188. bh_memcpy_s(i32_consts_new, (uint32)sizeof(int32) * k,
  10189. i32_consts_old, (uint32)sizeof(int32) * k);
  10190. /* Free the old memory */
  10191. wasm_runtime_free(i32_consts_old);
  10192. loader_ctx->i32_consts = i32_consts_new;
  10193. loader_ctx->i32_const_max_num = k;
  10194. }
  10195. loader_ctx->i32_const_num = k;
  10196. }
  10197. }
  10198. }
  10199. #endif
  10200. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  10201. while (p < p_end) {
  10202. opcode = *p++;
  10203. #if WASM_ENABLE_FAST_INTERP != 0
  10204. p_org = p;
  10205. disable_emit = false;
  10206. emit_label(opcode);
  10207. #endif
  10208. switch (opcode) {
  10209. case WASM_OP_UNREACHABLE:
  10210. RESET_STACK();
  10211. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10212. break;
  10213. case WASM_OP_NOP:
  10214. #if WASM_ENABLE_FAST_INTERP != 0
  10215. skip_label();
  10216. #endif
  10217. break;
  10218. case WASM_OP_IF:
  10219. {
  10220. #if WASM_ENABLE_FAST_INTERP != 0
  10221. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  10222. int32 available_stack_cell =
  10223. (int32)(loader_ctx->stack_cell_num
  10224. - parent_block->stack_cell_num);
  10225. if (available_stack_cell <= 0
  10226. && parent_block->is_stack_polymorphic)
  10227. if_condition_available = false;
  10228. else
  10229. if_condition_available = true;
  10230. PRESERVE_LOCAL_FOR_BLOCK();
  10231. #endif
  10232. #if WASM_ENABLE_GC == 0
  10233. POP_I32();
  10234. #endif
  10235. goto handle_op_block_and_loop;
  10236. }
  10237. case WASM_OP_BLOCK:
  10238. case WASM_OP_LOOP:
  10239. #if WASM_ENABLE_EXCE_HANDLING != 0
  10240. case WASM_OP_TRY:
  10241. if (opcode == WASM_OP_TRY) {
  10242. /*
  10243. * keep track of exception handlers to account for
  10244. * memory allocation
  10245. */
  10246. func->exception_handler_count++;
  10247. /*
  10248. * try is a block
  10249. * do nothing special, but execution continues to
  10250. * to handle_op_block_and_loop,
  10251. * and that be pushes the csp
  10252. */
  10253. }
  10254. #endif
  10255. #if WASM_ENABLE_FAST_INTERP != 0
  10256. PRESERVE_LOCAL_FOR_BLOCK();
  10257. #endif
  10258. handle_op_block_and_loop:
  10259. {
  10260. uint8 value_type;
  10261. BlockType block_type;
  10262. #if WASM_ENABLE_FAST_INTERP != 0
  10263. uint32 available_params = 0;
  10264. #endif
  10265. CHECK_BUF(p, p_end, 1);
  10266. value_type = read_uint8(p);
  10267. if (is_byte_a_type(value_type)) {
  10268. /* If the first byte is one of these special values:
  10269. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  10270. * the single return value. */
  10271. block_type.is_value_type = true;
  10272. block_type.u.value_type.type = value_type;
  10273. #if WASM_ENABLE_WAMR_COMPILER != 0
  10274. if (value_type == VALUE_TYPE_V128)
  10275. module->is_simd_used = true;
  10276. else if (value_type == VALUE_TYPE_FUNCREF
  10277. || value_type == VALUE_TYPE_EXTERNREF)
  10278. module->is_ref_types_used = true;
  10279. #endif
  10280. #if WASM_ENABLE_GC != 0
  10281. if (value_type != VALUE_TYPE_VOID) {
  10282. p_org = p;
  10283. p--;
  10284. if (!resolve_value_type((const uint8 **)&p, p_end,
  10285. module, module->type_count,
  10286. &need_ref_type_map,
  10287. &wasm_ref_type, false,
  10288. error_buf, error_buf_size)) {
  10289. goto fail;
  10290. }
  10291. if (need_ref_type_map) {
  10292. block_type.u.value_type.ref_type_map.index = 0;
  10293. if (!(block_type.u.value_type.ref_type_map
  10294. .ref_type = reftype_set_insert(
  10295. module->ref_type_set, &wasm_ref_type,
  10296. error_buf, error_buf_size))) {
  10297. goto fail;
  10298. }
  10299. }
  10300. /* Set again as the type might be changed, e.g.
  10301. (ref null any) to anyref */
  10302. block_type.u.value_type.type = wasm_ref_type.ref_type;
  10303. #if WASM_ENABLE_FAST_INTERP == 0
  10304. while (p_org < p) {
  10305. #if WASM_ENABLE_DEBUG_INTERP != 0
  10306. if (!record_fast_op(module, p_org, *p_org,
  10307. error_buf, error_buf_size)) {
  10308. goto fail;
  10309. }
  10310. #endif
  10311. /* Ignore extra bytes for interpreter */
  10312. *p_org++ = WASM_OP_NOP;
  10313. }
  10314. #endif
  10315. }
  10316. #endif /* end of WASM_ENABLE_GC != 0 */
  10317. }
  10318. else {
  10319. int32 type_index;
  10320. /* Resolve the leb128 encoded type index as block type */
  10321. p--;
  10322. p_org = p - 1;
  10323. pb_read_leb_int32(p, p_end, type_index);
  10324. if ((uint32)type_index >= module->type_count) {
  10325. set_error_buf(error_buf, error_buf_size,
  10326. "unknown type");
  10327. goto fail;
  10328. }
  10329. block_type.is_value_type = false;
  10330. block_type.u.type =
  10331. (WASMFuncType *)module->types[type_index];
  10332. #if WASM_ENABLE_FAST_INTERP == 0
  10333. /* If block use type index as block type, change the opcode
  10334. * to new extended opcode so that interpreter can resolve
  10335. * the block quickly.
  10336. */
  10337. #if WASM_ENABLE_DEBUG_INTERP != 0
  10338. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10339. error_buf_size)) {
  10340. goto fail;
  10341. }
  10342. #endif
  10343. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  10344. #endif
  10345. }
  10346. #if WASM_ENABLE_GC != 0
  10347. if (opcode == WASM_OP_IF) {
  10348. POP_I32();
  10349. }
  10350. #endif
  10351. /* Pop block parameters from stack */
  10352. if (BLOCK_HAS_PARAM(block_type)) {
  10353. WASMFuncType *wasm_type = block_type.u.type;
  10354. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10355. #if WASM_ENABLE_FAST_INTERP != 0
  10356. uint32 cell_num;
  10357. available_params = block_type.u.type->param_count;
  10358. #endif
  10359. for (i = 0; i < block_type.u.type->param_count; i++) {
  10360. int32 available_stack_cell =
  10361. (int32)(loader_ctx->stack_cell_num
  10362. - cur_block->stack_cell_num);
  10363. if (available_stack_cell <= 0
  10364. && cur_block->is_stack_polymorphic) {
  10365. #if WASM_ENABLE_FAST_INTERP != 0
  10366. available_params = i;
  10367. #endif
  10368. break;
  10369. }
  10370. POP_TYPE(
  10371. wasm_type->types[wasm_type->param_count - i - 1]);
  10372. #if WASM_ENABLE_FAST_INTERP != 0
  10373. /* decrease the frame_offset pointer accordingly to keep
  10374. * consistent with frame_ref stack */
  10375. cell_num = wasm_value_type_cell_num(
  10376. wasm_type->types[wasm_type->param_count - i - 1]);
  10377. loader_ctx->frame_offset -= cell_num;
  10378. #endif
  10379. }
  10380. }
  10381. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  10382. block_type, p);
  10383. /* Pass parameters to block */
  10384. if (BLOCK_HAS_PARAM(block_type)) {
  10385. WASMFuncType *func_type = block_type.u.type;
  10386. #if WASM_ENABLE_GC != 0
  10387. WASMRefType *ref_type;
  10388. uint32 j = 0;
  10389. #endif
  10390. for (i = 0; i < func_type->param_count; i++) {
  10391. #if WASM_ENABLE_FAST_INTERP != 0
  10392. uint32 cell_num =
  10393. wasm_value_type_cell_num(func_type->types[i]);
  10394. if (i >= available_params) {
  10395. /* make sure enough space */
  10396. if (loader_ctx->p_code_compiled == NULL) {
  10397. loader_ctx->frame_offset += cell_num;
  10398. if (!check_offset_push(loader_ctx, error_buf,
  10399. error_buf_size))
  10400. goto fail;
  10401. /* for following dummy value assignment */
  10402. loader_ctx->frame_offset -= cell_num;
  10403. }
  10404. /* If there isn't enough data on stack, push a dummy
  10405. * offset to keep the stack consistent with
  10406. * frame_ref.
  10407. * Since the stack is already in polymorphic state,
  10408. * the opcode will not be executed, so the dummy
  10409. * offset won't cause any error */
  10410. for (uint32 n = 0; n < cell_num; n++) {
  10411. *loader_ctx->frame_offset++ = 0;
  10412. }
  10413. }
  10414. else {
  10415. loader_ctx->frame_offset += cell_num;
  10416. }
  10417. #endif
  10418. #if WASM_ENABLE_GC != 0
  10419. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10420. bh_assert(func_type->ref_type_maps[j].index == i);
  10421. ref_type = func_type->ref_type_maps[j].ref_type;
  10422. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10423. ref_type,
  10424. wasm_reftype_struct_size(ref_type));
  10425. j++;
  10426. }
  10427. #endif
  10428. PUSH_TYPE(func_type->types[i]);
  10429. }
  10430. }
  10431. #if WASM_ENABLE_FAST_INTERP != 0
  10432. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  10433. skip_label();
  10434. if (BLOCK_HAS_PARAM(block_type)) {
  10435. /* Make sure params are in dynamic space */
  10436. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10437. error_buf_size))
  10438. goto fail;
  10439. }
  10440. if (opcode == WASM_OP_LOOP) {
  10441. (loader_ctx->frame_csp - 1)->code_compiled =
  10442. loader_ctx->p_code_compiled;
  10443. }
  10444. }
  10445. #if WASM_ENABLE_EXCE_HANDLING != 0
  10446. else if (opcode == WASM_OP_TRY) {
  10447. skip_label();
  10448. }
  10449. #endif
  10450. else if (opcode == WASM_OP_IF) {
  10451. BranchBlock *block = loader_ctx->frame_csp - 1;
  10452. /* If block has parameters, we should make sure they are in
  10453. * dynamic space. Otherwise, when else branch is missing,
  10454. * the later opcode may consume incorrect operand offset.
  10455. * Spec case:
  10456. * (func (export "params-id") (param i32) (result i32)
  10457. * (i32.const 1)
  10458. * (i32.const 2)
  10459. * (if (param i32 i32) (result i32 i32) (local.get 0)
  10460. * (then)) (i32.add)
  10461. * )
  10462. *
  10463. * So we should emit a copy instruction before the if.
  10464. *
  10465. * And we also need to save the parameter offsets and
  10466. * recover them before entering else branch.
  10467. *
  10468. */
  10469. if (BLOCK_HAS_PARAM(block_type)) {
  10470. uint64 size;
  10471. /* In polymorphic state, there may be no if condition on
  10472. * the stack, so the offset may not emitted */
  10473. if (if_condition_available) {
  10474. /* skip the if condition operand offset */
  10475. wasm_loader_emit_backspace(loader_ctx,
  10476. sizeof(int16));
  10477. }
  10478. /* skip the if label */
  10479. skip_label();
  10480. /* Emit a copy instruction */
  10481. if (!copy_params_to_dynamic_space(loader_ctx, error_buf,
  10482. error_buf_size))
  10483. goto fail;
  10484. /* Emit the if instruction */
  10485. emit_label(opcode);
  10486. /* Emit the new condition operand offset */
  10487. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  10488. /* Save top param_count values of frame_offset stack, so
  10489. * that we can recover it before executing else branch
  10490. */
  10491. size = sizeof(int16)
  10492. * (uint64)block_type.u.type->param_cell_num;
  10493. if (!(block->param_frame_offsets = loader_malloc(
  10494. size, error_buf, error_buf_size)))
  10495. goto fail;
  10496. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10497. loader_ctx->frame_offset
  10498. - size / sizeof(int16),
  10499. (uint32)size);
  10500. }
  10501. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10502. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10503. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10504. }
  10505. #endif
  10506. break;
  10507. }
  10508. #if WASM_ENABLE_EXCE_HANDLING != 0
  10509. case WASM_OP_THROW:
  10510. {
  10511. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10512. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10513. uint8 label_type = cur_block->label_type;
  10514. uint32 tag_index = 0;
  10515. pb_read_leb_int32(p, p_end, tag_index);
  10516. /* check validity of tag_index against module->tag_count */
  10517. /* check tag index is within the tag index space */
  10518. if (tag_index >= module->import_tag_count + module->tag_count) {
  10519. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10520. tag_index);
  10521. goto fail;
  10522. }
  10523. /* the tag_type is stored in either the WASMTag (section tags)
  10524. * or WASMTagImport (import tag) */
  10525. WASMFuncType *tag_type = NULL;
  10526. if (tag_index < module->import_tag_count) {
  10527. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10528. }
  10529. else {
  10530. tag_type =
  10531. module->tags[tag_index - module->import_tag_count]
  10532. ->tag_type;
  10533. }
  10534. if (tag_type->result_count != 0) {
  10535. set_error_buf(error_buf, error_buf_size,
  10536. "tag type signature does not return void");
  10537. goto fail;
  10538. }
  10539. int32 available_stack_cell =
  10540. (int32)(loader_ctx->stack_cell_num
  10541. - cur_block->stack_cell_num);
  10542. int32 tti;
  10543. /* Check stack values match return types by comparing tag param
  10544. * types with stack cells */
  10545. uint8 *frame_ref = loader_ctx->frame_ref;
  10546. #if WASM_ENABLE_GC != 0
  10547. WASMRefTypeMap *frame_reftype_map =
  10548. loader_ctx->frame_reftype_map;
  10549. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10550. /* Temporarily set these values since they may be used in
  10551. GET_LOCAL_REFTYPE(), remember they must be restored later */
  10552. param_reftype_maps = tag_type->ref_type_maps;
  10553. /* For tag_type function, it shouldn't have result_count = 0 */
  10554. param_reftype_map_count = tag_type->ref_type_map_count;
  10555. param_count = tag_type->param_count;
  10556. #endif
  10557. for (tti = (int32)tag_type->param_count - 1; tti >= 0; tti--) {
  10558. #if WASM_ENABLE_GC != 0
  10559. local_type = tag_type->types[tti];
  10560. local_idx = tti;
  10561. /* Get the wasm_ref_type if the local_type is multibyte
  10562. type */
  10563. GET_LOCAL_REFTYPE();
  10564. #endif
  10565. if (!check_stack_top_values(
  10566. loader_ctx, frame_ref, available_stack_cell,
  10567. #if WASM_ENABLE_GC != 0
  10568. frame_reftype_map, frame_reftype_map_num,
  10569. #endif
  10570. tag_type->types[tti],
  10571. #if WASM_ENABLE_GC != 0
  10572. &wasm_ref_type,
  10573. #endif
  10574. error_buf, error_buf_size)) {
  10575. snprintf(error_buf, error_buf_size,
  10576. "type mismatch: instruction requires [%s] but "
  10577. "stack has [%s]",
  10578. tag_type->param_count > 0
  10579. ? type2str(tag_type->types[tti])
  10580. : "",
  10581. available_stack_cell > 0
  10582. ? type2str(*(loader_ctx->frame_ref - 1))
  10583. : "");
  10584. goto fail;
  10585. }
  10586. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10587. available_stack_cell -=
  10588. wasm_value_type_cell_num(tag_type->types[tti]);
  10589. }
  10590. #if WASM_ENABLE_GC != 0
  10591. /* Restore the values */
  10592. param_reftype_maps = func->func_type->ref_type_maps;
  10593. param_reftype_map_count = func->func_type->ref_type_map_count;
  10594. param_count = func->func_type->param_count;
  10595. #endif
  10596. /* throw is stack polymorphic */
  10597. (void)label_type;
  10598. RESET_STACK();
  10599. break;
  10600. }
  10601. case WASM_OP_RETHROW:
  10602. {
  10603. /* must be done before checking branch block */
  10604. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10605. /* check the target catching block: LABEL_TYPE_CATCH */
  10606. if (!(frame_csp_tmp =
  10607. check_branch_block(loader_ctx, &p, p_end, opcode,
  10608. error_buf, error_buf_size)))
  10609. goto fail;
  10610. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10611. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10612. /* trap according to spectest (rethrow.wast) */
  10613. set_error_buf(error_buf, error_buf_size,
  10614. "invalid rethrow label");
  10615. goto fail;
  10616. }
  10617. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10618. uint8 label_type = cur_block->label_type;
  10619. (void)label_type;
  10620. /* rethrow is stack polymorphic */
  10621. RESET_STACK();
  10622. break;
  10623. }
  10624. case WASM_OP_DELEGATE:
  10625. {
  10626. /* check target block is valid */
  10627. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10628. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10629. goto fail;
  10630. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10631. uint8 label_type = cur_block->label_type;
  10632. (void)label_type;
  10633. /* DELEGATE ends the block */
  10634. POP_CSP();
  10635. break;
  10636. }
  10637. case WASM_OP_CATCH:
  10638. {
  10639. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10640. uint8 label_type = cur_block->label_type;
  10641. uint32 tag_index = 0;
  10642. pb_read_leb_int32(p, p_end, tag_index);
  10643. /* check validity of tag_index against module->tag_count */
  10644. /* check tag index is within the tag index space */
  10645. if (tag_index >= module->import_tag_count + module->tag_count) {
  10646. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10647. __FUNCTION__);
  10648. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10649. goto fail;
  10650. }
  10651. /* the tag_type is stored in either the WASMTag (section tags)
  10652. * or WASMTagImport (import tag) */
  10653. WASMFuncType *func_type = NULL;
  10654. if (tag_index < module->import_tag_count) {
  10655. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10656. }
  10657. else {
  10658. func_type =
  10659. module->tags[tag_index - module->import_tag_count]
  10660. ->tag_type;
  10661. }
  10662. if (func_type->result_count != 0) {
  10663. set_error_buf(error_buf, error_buf_size,
  10664. "tag type signature does not return void");
  10665. goto fail;
  10666. }
  10667. /* check validity of current label (expect LABEL_TYPE_TRY or
  10668. * LABEL_TYPE_CATCH) */
  10669. if ((LABEL_TYPE_CATCH != label_type)
  10670. && (LABEL_TYPE_TRY != label_type)) {
  10671. set_error_buf(error_buf, error_buf_size,
  10672. "Unexpected block sequence encountered.");
  10673. goto fail;
  10674. }
  10675. /*
  10676. * replace frame_csp by LABEL_TYPE_CATCH
  10677. */
  10678. cur_block->label_type = LABEL_TYPE_CATCH;
  10679. /* RESET_STACK removes the values pushed in TRY or previous
  10680. * CATCH Blocks */
  10681. RESET_STACK();
  10682. #if WASM_ENABLE_GC != 0
  10683. WASMRefType *ref_type;
  10684. uint32 j = 0;
  10685. #endif
  10686. /* push types on the stack according to caught type */
  10687. for (i = 0; i < func_type->param_count; i++) {
  10688. #if WASM_ENABLE_GC != 0
  10689. if (wasm_is_type_multi_byte_type(func_type->types[i])) {
  10690. bh_assert(func_type->ref_type_maps[j].index == i);
  10691. ref_type = func_type->ref_type_maps[j].ref_type;
  10692. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10693. ref_type,
  10694. wasm_reftype_struct_size(ref_type));
  10695. j++;
  10696. }
  10697. #endif
  10698. PUSH_TYPE(func_type->types[i]);
  10699. }
  10700. break;
  10701. }
  10702. case WASM_OP_CATCH_ALL:
  10703. {
  10704. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10705. /* expecting a TRY or CATCH, anything else will be considered an
  10706. * error */
  10707. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10708. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10709. set_error_buf(error_buf, error_buf_size,
  10710. "Unexpected block sequence encountered.");
  10711. goto fail;
  10712. }
  10713. /* no immediates */
  10714. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10715. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10716. /* RESET_STACK removes the values pushed in TRY or previous
  10717. * CATCH Blocks */
  10718. RESET_STACK();
  10719. /* catch_all has no tagtype and therefore no parameters */
  10720. break;
  10721. }
  10722. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10723. case WASM_OP_ELSE:
  10724. handle_op_else:
  10725. {
  10726. BranchBlock *block = NULL;
  10727. BlockType block_type;
  10728. if (loader_ctx->csp_num < 2
  10729. /* the matched if isn't found */
  10730. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10731. /* duplicated else is found */
  10732. || (loader_ctx->frame_csp - 1)->else_addr) {
  10733. set_error_buf(
  10734. error_buf, error_buf_size,
  10735. "opcode else found without matched opcode if");
  10736. goto fail;
  10737. }
  10738. block = loader_ctx->frame_csp - 1;
  10739. /* check whether if branch's stack matches its result type */
  10740. if (!check_block_stack(loader_ctx, block, error_buf,
  10741. error_buf_size))
  10742. goto fail;
  10743. block->else_addr = p - 1;
  10744. block_type = block->block_type;
  10745. #if WASM_ENABLE_GC != 0
  10746. if (!wasm_loader_init_local_use_masks(
  10747. loader_ctx, local_count, error_buf, error_buf_size)) {
  10748. goto fail;
  10749. }
  10750. #endif
  10751. #if WASM_ENABLE_FAST_INTERP != 0
  10752. /* if the result of if branch is in local or const area, add a
  10753. * copy op */
  10754. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10755. error_buf, error_buf_size)) {
  10756. goto fail;
  10757. }
  10758. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10759. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10760. #endif
  10761. RESET_STACK();
  10762. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10763. /* Pass parameters to if-false branch */
  10764. if (BLOCK_HAS_PARAM(block_type)) {
  10765. for (i = 0; i < block_type.u.type->param_count; i++)
  10766. PUSH_TYPE(block_type.u.type->types[i]);
  10767. }
  10768. #if WASM_ENABLE_FAST_INTERP != 0
  10769. /* Recover top param_count values of frame_offset stack */
  10770. if (BLOCK_HAS_PARAM((block_type))) {
  10771. uint32 size;
  10772. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10773. bh_memcpy_s(loader_ctx->frame_offset, size,
  10774. block->param_frame_offsets, size);
  10775. loader_ctx->frame_offset += (size / sizeof(int16));
  10776. }
  10777. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10778. #endif
  10779. break;
  10780. }
  10781. case WASM_OP_END:
  10782. {
  10783. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10784. /* check whether block stack matches its result type */
  10785. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10786. error_buf_size))
  10787. goto fail;
  10788. /* if there is no else branch, make a virtual else opcode for
  10789. easier integrity check and to copy the correct results to
  10790. the block return address for fast-interp mode:
  10791. change if block from `if ... end` to `if ... else end` */
  10792. if (cur_block->label_type == LABEL_TYPE_IF
  10793. && !cur_block->else_addr) {
  10794. opcode = WASM_OP_ELSE;
  10795. p--;
  10796. #if WASM_ENABLE_FAST_INTERP != 0
  10797. p_org = p;
  10798. skip_label();
  10799. disable_emit = false;
  10800. emit_label(opcode);
  10801. #endif
  10802. goto handle_op_else;
  10803. }
  10804. POP_CSP();
  10805. #if WASM_ENABLE_FAST_INTERP != 0
  10806. skip_label();
  10807. /* copy the result to the block return address */
  10808. if (!reserve_block_ret(loader_ctx, opcode, disable_emit,
  10809. error_buf, error_buf_size)) {
  10810. /* it could be tmp frame_csp allocated from opcode like
  10811. * OP_BR and not counted in loader_ctx->csp_num, it won't
  10812. * be freed in wasm_loader_ctx_destroy(loader_ctx) so need
  10813. * to free the loader_ctx->frame_csp if fails */
  10814. free_label_patch_list(loader_ctx->frame_csp);
  10815. goto fail;
  10816. }
  10817. apply_label_patch(loader_ctx, 0, PATCH_END);
  10818. free_label_patch_list(loader_ctx->frame_csp);
  10819. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10820. int32 idx;
  10821. uint8 ret_type;
  10822. emit_label(WASM_OP_RETURN);
  10823. for (idx = (int32)func->func_type->result_count - 1;
  10824. idx >= 0; idx--) {
  10825. ret_type = *(func->func_type->types
  10826. + func->func_type->param_count + idx);
  10827. POP_OFFSET_TYPE(ret_type);
  10828. }
  10829. }
  10830. #endif
  10831. if (loader_ctx->csp_num > 0) {
  10832. loader_ctx->frame_csp->end_addr = p - 1;
  10833. }
  10834. else {
  10835. /* end of function block, function will return */
  10836. if (p < p_end) {
  10837. set_error_buf(error_buf, error_buf_size,
  10838. "section size mismatch");
  10839. goto fail;
  10840. }
  10841. }
  10842. break;
  10843. }
  10844. case WASM_OP_BR:
  10845. {
  10846. if (!(frame_csp_tmp =
  10847. check_branch_block(loader_ctx, &p, p_end, opcode,
  10848. error_buf, error_buf_size)))
  10849. goto fail;
  10850. RESET_STACK();
  10851. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10852. break;
  10853. }
  10854. case WASM_OP_BR_IF:
  10855. {
  10856. POP_I32();
  10857. if (!(frame_csp_tmp =
  10858. check_branch_block(loader_ctx, &p, p_end, opcode,
  10859. error_buf, error_buf_size)))
  10860. goto fail;
  10861. break;
  10862. }
  10863. case WASM_OP_BR_TABLE:
  10864. {
  10865. uint32 depth = 0, default_arity, arity = 0;
  10866. BranchBlock *target_block;
  10867. BlockType *target_block_type;
  10868. #if WASM_ENABLE_FAST_INTERP == 0
  10869. BrTableCache *br_table_cache = NULL;
  10870. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10871. uint32 j;
  10872. #endif
  10873. pb_read_leb_uint32(p, p_end, count);
  10874. #if WASM_ENABLE_FAST_INTERP != 0
  10875. emit_uint32(loader_ctx, count);
  10876. #endif
  10877. POP_I32();
  10878. /* Get each depth and check it */
  10879. p_org = p;
  10880. for (i = 0; i <= count; i++) {
  10881. pb_read_leb_uint32(p, p_end, depth);
  10882. bh_assert(loader_ctx->csp_num > 0);
  10883. if (loader_ctx->csp_num - 1 < depth) {
  10884. set_error_buf(error_buf, error_buf_size,
  10885. "unknown label, "
  10886. "unexpected end of section or function");
  10887. goto fail;
  10888. }
  10889. }
  10890. p = p_org;
  10891. /* Get the default block's arity */
  10892. target_block = loader_ctx->frame_csp - (depth + 1);
  10893. target_block_type = &target_block->block_type;
  10894. default_arity = block_type_get_arity(target_block_type,
  10895. target_block->label_type);
  10896. #if WASM_ENABLE_FAST_INTERP == 0
  10897. p_depth_begin = p_depth = p;
  10898. #endif
  10899. for (i = 0; i <= count; i++) {
  10900. p_org = p;
  10901. pb_read_leb_uint32(p, p_end, depth);
  10902. p = p_org;
  10903. /* Get the target block's arity and check it */
  10904. target_block = loader_ctx->frame_csp - (depth + 1);
  10905. target_block_type = &target_block->block_type;
  10906. arity = block_type_get_arity(target_block_type,
  10907. target_block->label_type);
  10908. if (arity != default_arity) {
  10909. set_error_buf(error_buf, error_buf_size,
  10910. "type mismatch: br_table targets must "
  10911. "all use same result type");
  10912. goto fail;
  10913. }
  10914. if (!(frame_csp_tmp =
  10915. check_branch_block(loader_ctx, &p, p_end, opcode,
  10916. error_buf, error_buf_size))) {
  10917. goto fail;
  10918. }
  10919. #if WASM_ENABLE_FAST_INTERP == 0
  10920. if (br_table_cache) {
  10921. br_table_cache->br_depths[i] = depth;
  10922. }
  10923. else {
  10924. if (depth > 255) {
  10925. /* The depth cannot be stored in one byte,
  10926. create br_table cache to store each depth */
  10927. #if WASM_ENABLE_DEBUG_INTERP != 0
  10928. if (!record_fast_op(module, p_opcode, *p_opcode,
  10929. error_buf, error_buf_size)) {
  10930. goto fail;
  10931. }
  10932. #endif
  10933. if (!(br_table_cache = loader_malloc(
  10934. offsetof(BrTableCache, br_depths)
  10935. + sizeof(uint32)
  10936. * (uint64)(count + 1),
  10937. error_buf, error_buf_size))) {
  10938. goto fail;
  10939. }
  10940. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10941. br_table_cache->br_table_op_addr = p_opcode;
  10942. br_table_cache->br_count = count;
  10943. /* Copy previous depths which are one byte */
  10944. for (j = 0; j < i; j++) {
  10945. br_table_cache->br_depths[j] = p_depth_begin[j];
  10946. }
  10947. br_table_cache->br_depths[i] = depth;
  10948. bh_list_insert(module->br_table_cache_list,
  10949. br_table_cache);
  10950. }
  10951. else {
  10952. /* The depth can be stored in one byte, use the
  10953. byte of the leb to store it */
  10954. *p_depth++ = (uint8)depth;
  10955. }
  10956. }
  10957. #endif
  10958. }
  10959. #if WASM_ENABLE_FAST_INTERP == 0
  10960. /* Set the tailing bytes to nop */
  10961. if (br_table_cache)
  10962. p_depth = p_depth_begin;
  10963. while (p_depth < p)
  10964. *p_depth++ = WASM_OP_NOP;
  10965. #endif
  10966. RESET_STACK();
  10967. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10968. break;
  10969. }
  10970. case WASM_OP_RETURN:
  10971. {
  10972. WASMFuncType *func_type = func->func_type;
  10973. int32 idx;
  10974. uint8 ret_type;
  10975. #if WASM_ENABLE_GC != 0
  10976. uint32 j = func_type->ref_type_map_count - 1;
  10977. #endif
  10978. for (idx = (int32)func_type->result_count - 1; idx >= 0;
  10979. idx--) {
  10980. ret_type =
  10981. *(func_type->types + func_type->param_count + idx);
  10982. #if WASM_ENABLE_GC != 0
  10983. if (wasm_is_type_multi_byte_type(ret_type)) {
  10984. WASMRefType *ref_type =
  10985. func_type->ref_type_maps[j].ref_type;
  10986. bh_assert(func_type->ref_type_maps[j].index
  10987. == func_type->param_count + idx);
  10988. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10989. ref_type,
  10990. wasm_reftype_struct_size(ref_type));
  10991. j--;
  10992. }
  10993. #endif
  10994. #if WASM_ENABLE_FAST_INTERP != 0
  10995. /* emit the offset after return opcode */
  10996. POP_OFFSET_TYPE(ret_type);
  10997. #endif
  10998. POP_TYPE(ret_type);
  10999. }
  11000. RESET_STACK();
  11001. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11002. break;
  11003. }
  11004. case WASM_OP_CALL:
  11005. #if WASM_ENABLE_TAIL_CALL != 0
  11006. case WASM_OP_RETURN_CALL:
  11007. #endif
  11008. #if WASM_ENABLE_GC != 0
  11009. case WASM_OP_CALL_REF:
  11010. case WASM_OP_RETURN_CALL_REF:
  11011. #endif
  11012. {
  11013. WASMFuncType *func_type;
  11014. uint8 type;
  11015. int32 idx;
  11016. #if WASM_ENABLE_GC != 0
  11017. WASMRefType *ref_type;
  11018. uint32 type_idx1;
  11019. int32 j;
  11020. #endif
  11021. #if WASM_ENABLE_GC != 0
  11022. if (opcode == WASM_OP_CALL_REF
  11023. || opcode == WASM_OP_RETURN_CALL_REF) {
  11024. pb_read_leb_uint32(p, p_end, type_idx1);
  11025. if (!check_type_index(module, module->type_count, type_idx1,
  11026. error_buf, error_buf_size)) {
  11027. goto fail;
  11028. }
  11029. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  11030. set_error_buf(error_buf, error_buf_size,
  11031. "unknown function type");
  11032. goto fail;
  11033. }
  11034. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  11035. &type_idx, error_buf,
  11036. error_buf_size)) {
  11037. goto fail;
  11038. }
  11039. if (type == VALUE_TYPE_ANY) {
  11040. type_idx = type_idx1;
  11041. }
  11042. if (!check_type_index(module, module->type_count, type_idx,
  11043. error_buf, error_buf_size)) {
  11044. goto fail;
  11045. }
  11046. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  11047. set_error_buf(error_buf, error_buf_size,
  11048. "unknown function type");
  11049. goto fail;
  11050. }
  11051. if (!wasm_func_type_is_super_of(
  11052. (WASMFuncType *)module->types[type_idx1],
  11053. (WASMFuncType *)module->types[type_idx])) {
  11054. set_error_buf(error_buf, error_buf_size,
  11055. "function type mismatch");
  11056. goto fail;
  11057. }
  11058. func_type = (WASMFuncType *)module->types[type_idx];
  11059. }
  11060. else
  11061. #endif
  11062. {
  11063. pb_read_leb_uint32(p, p_end, func_idx);
  11064. #if WASM_ENABLE_FAST_INTERP != 0
  11065. /* we need to emit func_idx before arguments */
  11066. emit_uint32(loader_ctx, func_idx);
  11067. #endif
  11068. if (!check_function_index(module, func_idx, error_buf,
  11069. error_buf_size)) {
  11070. goto fail;
  11071. }
  11072. if (func_idx < module->import_function_count)
  11073. func_type = module->import_functions[func_idx]
  11074. .u.function.func_type;
  11075. else
  11076. func_type =
  11077. module
  11078. ->functions[func_idx
  11079. - module->import_function_count]
  11080. ->func_type;
  11081. }
  11082. if (func_type->param_count > 0) {
  11083. #if WASM_ENABLE_GC != 0
  11084. j = (int32)(func_type->result_ref_type_maps
  11085. - func_type->ref_type_maps - 1);
  11086. #endif
  11087. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11088. idx--) {
  11089. #if WASM_ENABLE_GC != 0
  11090. if (wasm_is_type_multi_byte_type(
  11091. func_type->types[idx])) {
  11092. ref_type = func_type->ref_type_maps[j].ref_type;
  11093. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11094. ref_type,
  11095. wasm_reftype_struct_size(ref_type));
  11096. j--;
  11097. }
  11098. #endif
  11099. #if WASM_ENABLE_FAST_INTERP != 0
  11100. POP_OFFSET_TYPE(func_type->types[idx]);
  11101. #endif
  11102. POP_TYPE(func_type->types[idx]);
  11103. }
  11104. }
  11105. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11106. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  11107. #endif
  11108. #if WASM_ENABLE_GC != 0
  11109. j = (int32)(func_type->result_ref_type_maps
  11110. - func_type->ref_type_maps);
  11111. #endif
  11112. for (i = 0; i < func_type->result_count; i++) {
  11113. #if WASM_ENABLE_GC != 0
  11114. if (wasm_is_type_multi_byte_type(
  11115. func_type->types[func_type->param_count + i])) {
  11116. ref_type = func_type->ref_type_maps[j].ref_type;
  11117. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11118. ref_type,
  11119. wasm_reftype_struct_size(ref_type));
  11120. j++;
  11121. }
  11122. #endif
  11123. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11124. #if WASM_ENABLE_FAST_INTERP != 0
  11125. /* Here we emit each return value's dynamic_offset. But
  11126. * in fact these offsets are continuous, so interpreter
  11127. * only need to get the first return value's offset.
  11128. */
  11129. PUSH_OFFSET_TYPE(
  11130. func_type->types[func_type->param_count + i]);
  11131. #endif
  11132. }
  11133. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  11134. }
  11135. else {
  11136. #if WASM_ENABLE_GC == 0
  11137. if (func_type->result_count
  11138. != func->func_type->result_count) {
  11139. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11140. "type mismatch: expect ",
  11141. func->func_type->result_count,
  11142. " return values but got other");
  11143. goto fail;
  11144. }
  11145. for (i = 0; i < func_type->result_count; i++) {
  11146. type = func->func_type
  11147. ->types[func->func_type->param_count + i];
  11148. if (func_type->types[func_type->param_count + i]
  11149. != type) {
  11150. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11151. "type mismatch: expect ",
  11152. type2str(type), " but got other");
  11153. goto fail;
  11154. }
  11155. }
  11156. #else
  11157. if (!wasm_func_type_result_is_subtype_of(
  11158. func_type, func->func_type, module->types,
  11159. module->type_count)) {
  11160. set_error_buf(
  11161. error_buf, error_buf_size,
  11162. "type mismatch: invalid func result types");
  11163. goto fail;
  11164. }
  11165. #endif
  11166. RESET_STACK();
  11167. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11168. }
  11169. #endif
  11170. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11171. || WASM_ENABLE_WAMR_COMPILER != 0
  11172. func->has_op_func_call = true;
  11173. #endif
  11174. (void)type;
  11175. break;
  11176. }
  11177. /*
  11178. * if disable reference type: call_indirect typeidx, 0x00
  11179. * if enable reference type: call_indirect typeidx, tableidx
  11180. */
  11181. case WASM_OP_CALL_INDIRECT:
  11182. #if WASM_ENABLE_TAIL_CALL != 0
  11183. case WASM_OP_RETURN_CALL_INDIRECT:
  11184. #endif
  11185. {
  11186. int32 idx;
  11187. WASMFuncType *func_type;
  11188. uint32 tbl_elem_type;
  11189. #if WASM_ENABLE_GC != 0
  11190. WASMRefType *elem_ref_type = NULL;
  11191. #endif
  11192. pb_read_leb_uint32(p, p_end, type_idx);
  11193. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11194. #if WASM_ENABLE_WAMR_COMPILER != 0
  11195. if (p + 1 < p_end && *p != 0x00) {
  11196. /*
  11197. * Any non-0x00 byte requires the ref types proposal.
  11198. * This is different from checking the table_idx value
  11199. * since `0x80 0x00` etc. are all valid encodings of zero.
  11200. */
  11201. module->is_ref_types_used = true;
  11202. }
  11203. #endif
  11204. pb_read_leb_uint32(p, p_end, table_idx);
  11205. #else
  11206. CHECK_BUF(p, p_end, 1);
  11207. table_idx = read_uint8(p);
  11208. #endif
  11209. if (!check_table_index(module, table_idx, error_buf,
  11210. error_buf_size)) {
  11211. goto fail;
  11212. }
  11213. tbl_elem_type =
  11214. table_idx < module->import_table_count
  11215. ? module->import_tables[table_idx]
  11216. .u.table.table_type.elem_type
  11217. : module->tables[table_idx - module->import_table_count]
  11218. .table_type.elem_type;
  11219. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  11220. if (tbl_elem_type != VALUE_TYPE_FUNCREF) {
  11221. set_error_buf_v(error_buf, error_buf_size,
  11222. "type mismatch: instruction requires table "
  11223. "of functions but table %u has externref",
  11224. table_idx);
  11225. goto fail;
  11226. }
  11227. #elif WASM_ENABLE_GC != 0
  11228. /* Table element must match type ref null func */
  11229. elem_ref_type =
  11230. table_idx < module->import_table_count
  11231. ? module->import_tables[table_idx]
  11232. .u.table.table_type.elem_ref_type
  11233. : module->tables[table_idx - module->import_table_count]
  11234. .table_type.elem_ref_type;
  11235. if (!wasm_reftype_is_subtype_of(
  11236. tbl_elem_type, elem_ref_type, REF_TYPE_FUNCREF, NULL,
  11237. module->types, module->type_count)) {
  11238. set_error_buf_v(error_buf, error_buf_size,
  11239. "type mismatch: instruction requires "
  11240. "reference type t match type ref null func"
  11241. "in table %u",
  11242. table_idx);
  11243. goto fail;
  11244. }
  11245. #else
  11246. (void)tbl_elem_type;
  11247. #endif
  11248. #if WASM_ENABLE_FAST_INTERP != 0
  11249. /* we need to emit before arguments */
  11250. #if WASM_ENABLE_TAIL_CALL != 0
  11251. emit_byte(loader_ctx, opcode);
  11252. #endif
  11253. emit_uint32(loader_ctx, type_idx);
  11254. emit_uint32(loader_ctx, table_idx);
  11255. #endif
  11256. #if WASM_ENABLE_MEMORY64 != 0
  11257. table_elem_idx_type = is_table_64bit(module, table_idx)
  11258. ? VALUE_TYPE_I64
  11259. : VALUE_TYPE_I32;
  11260. #endif
  11261. /* skip elem idx */
  11262. POP_TBL_ELEM_IDX();
  11263. if (type_idx >= module->type_count) {
  11264. set_error_buf(error_buf, error_buf_size, "unknown type");
  11265. goto fail;
  11266. }
  11267. func_type = (WASMFuncType *)module->types[type_idx];
  11268. if (func_type->param_count > 0) {
  11269. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  11270. idx--) {
  11271. #if WASM_ENABLE_FAST_INTERP != 0
  11272. POP_OFFSET_TYPE(func_type->types[idx]);
  11273. #endif
  11274. POP_TYPE(func_type->types[idx]);
  11275. }
  11276. }
  11277. #if WASM_ENABLE_TAIL_CALL != 0
  11278. if (opcode == WASM_OP_CALL_INDIRECT) {
  11279. #endif
  11280. for (i = 0; i < func_type->result_count; i++) {
  11281. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  11282. #if WASM_ENABLE_FAST_INTERP != 0
  11283. PUSH_OFFSET_TYPE(
  11284. func_type->types[func_type->param_count + i]);
  11285. #endif
  11286. }
  11287. #if WASM_ENABLE_TAIL_CALL != 0
  11288. }
  11289. else {
  11290. uint8 type;
  11291. if (func_type->result_count
  11292. != func->func_type->result_count) {
  11293. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  11294. "type mismatch: expect ",
  11295. func->func_type->result_count,
  11296. " return values but got other");
  11297. goto fail;
  11298. }
  11299. for (i = 0; i < func_type->result_count; i++) {
  11300. type = func->func_type
  11301. ->types[func->func_type->param_count + i];
  11302. if (func_type->types[func_type->param_count + i]
  11303. != type) {
  11304. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  11305. "type mismatch: expect ",
  11306. type2str(type), " but got other");
  11307. goto fail;
  11308. }
  11309. }
  11310. RESET_STACK();
  11311. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  11312. }
  11313. #endif
  11314. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11315. || WASM_ENABLE_WAMR_COMPILER != 0
  11316. func->has_op_func_call = true;
  11317. #endif
  11318. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11319. func->has_op_call_indirect = true;
  11320. #endif
  11321. break;
  11322. }
  11323. case WASM_OP_DROP:
  11324. {
  11325. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11326. int32 available_stack_cell =
  11327. (int32)(loader_ctx->stack_cell_num
  11328. - cur_block->stack_cell_num);
  11329. if (available_stack_cell <= 0
  11330. && !cur_block->is_stack_polymorphic) {
  11331. set_error_buf(error_buf, error_buf_size,
  11332. "type mismatch, opcode drop was found "
  11333. "but stack was empty");
  11334. goto fail;
  11335. }
  11336. if (available_stack_cell > 0) {
  11337. #if WASM_ENABLE_GC != 0
  11338. if (wasm_is_type_multi_byte_type(
  11339. *(loader_ctx->frame_ref - 1))) {
  11340. bh_assert((int32)(loader_ctx->reftype_map_num
  11341. - cur_block->reftype_map_num)
  11342. > 0);
  11343. loader_ctx->frame_reftype_map--;
  11344. loader_ctx->reftype_map_num--;
  11345. }
  11346. #endif
  11347. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  11348. loader_ctx->frame_ref--;
  11349. loader_ctx->stack_cell_num--;
  11350. #if WASM_ENABLE_FAST_INTERP != 0
  11351. skip_label();
  11352. loader_ctx->frame_offset--;
  11353. if ((*(loader_ctx->frame_offset)
  11354. > loader_ctx->start_dynamic_offset)
  11355. && (*(loader_ctx->frame_offset)
  11356. < loader_ctx->max_dynamic_offset))
  11357. loader_ctx->dynamic_offset--;
  11358. #endif
  11359. }
  11360. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  11361. loader_ctx->frame_ref -= 2;
  11362. loader_ctx->stack_cell_num -= 2;
  11363. #if WASM_ENABLE_FAST_INTERP == 0
  11364. *(p - 1) = WASM_OP_DROP_64;
  11365. #endif
  11366. #if WASM_ENABLE_FAST_INTERP != 0
  11367. skip_label();
  11368. loader_ctx->frame_offset -= 2;
  11369. if ((*(loader_ctx->frame_offset)
  11370. > loader_ctx->start_dynamic_offset)
  11371. && (*(loader_ctx->frame_offset)
  11372. < loader_ctx->max_dynamic_offset))
  11373. loader_ctx->dynamic_offset -= 2;
  11374. #endif
  11375. }
  11376. #if WASM_ENABLE_SIMD != 0
  11377. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11378. || (WASM_ENABLE_FAST_INTERP != 0)
  11379. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  11380. loader_ctx->frame_ref -= 4;
  11381. loader_ctx->stack_cell_num -= 4;
  11382. #if WASM_ENABLE_FAST_INTERP != 0
  11383. skip_label();
  11384. loader_ctx->frame_offset -= 4;
  11385. if ((*(loader_ctx->frame_offset)
  11386. > loader_ctx->start_dynamic_offset)
  11387. && (*(loader_ctx->frame_offset)
  11388. < loader_ctx->max_dynamic_offset))
  11389. loader_ctx->dynamic_offset -= 4;
  11390. #endif
  11391. }
  11392. #endif
  11393. #endif
  11394. else {
  11395. set_error_buf(error_buf, error_buf_size,
  11396. "type mismatch");
  11397. goto fail;
  11398. }
  11399. }
  11400. else {
  11401. #if WASM_ENABLE_FAST_INTERP != 0
  11402. skip_label();
  11403. #endif
  11404. }
  11405. break;
  11406. }
  11407. case WASM_OP_SELECT:
  11408. {
  11409. uint8 ref_type;
  11410. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11411. int32 available_stack_cell;
  11412. #if WASM_ENABLE_FAST_INTERP != 0
  11413. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11414. #endif
  11415. POP_I32();
  11416. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  11417. - cur_block->stack_cell_num);
  11418. if (available_stack_cell <= 0
  11419. && !cur_block->is_stack_polymorphic) {
  11420. set_error_buf(error_buf, error_buf_size,
  11421. "type mismatch or invalid result arity, "
  11422. "opcode select was found "
  11423. "but stack was empty");
  11424. goto fail;
  11425. }
  11426. if (available_stack_cell > 0) {
  11427. switch (*(loader_ctx->frame_ref - 1)) {
  11428. case VALUE_TYPE_I32:
  11429. case VALUE_TYPE_F32:
  11430. case VALUE_TYPE_ANY:
  11431. break;
  11432. case VALUE_TYPE_I64:
  11433. case VALUE_TYPE_F64:
  11434. #if WASM_ENABLE_FAST_INTERP == 0
  11435. *(p - 1) = WASM_OP_SELECT_64;
  11436. #endif
  11437. #if WASM_ENABLE_FAST_INTERP != 0
  11438. if (loader_ctx->p_code_compiled) {
  11439. uint8 opcode_tmp = WASM_OP_SELECT_64;
  11440. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11441. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11442. *(void **)(p_code_compiled_tmp
  11443. - sizeof(void *)) =
  11444. handle_table[opcode_tmp];
  11445. #else
  11446. #if UINTPTR_MAX == UINT64_MAX
  11447. /* emit int32 relative offset in 64-bit target
  11448. */
  11449. int32 offset =
  11450. (int32)((uint8 *)handle_table[opcode_tmp]
  11451. - (uint8 *)handle_table[0]);
  11452. *(int32 *)(p_code_compiled_tmp
  11453. - sizeof(int32)) = offset;
  11454. #else
  11455. /* emit uint32 label address in 32-bit target */
  11456. *(uint32 *)(p_code_compiled_tmp
  11457. - sizeof(uint32)) =
  11458. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11459. #endif
  11460. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11461. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11462. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11463. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11464. #else
  11465. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11466. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11467. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11468. }
  11469. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11470. break;
  11471. #if WASM_ENABLE_SIMD != 0
  11472. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  11473. || (WASM_ENABLE_FAST_INTERP != 0)
  11474. case VALUE_TYPE_V128:
  11475. break;
  11476. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  11477. (WASM_ENABLE_FAST_INTERP != 0) */
  11478. #endif /* WASM_ENABLE_SIMD != 0 */
  11479. default:
  11480. {
  11481. set_error_buf(error_buf, error_buf_size,
  11482. "type mismatch");
  11483. goto fail;
  11484. }
  11485. }
  11486. ref_type = *(loader_ctx->frame_ref - 1);
  11487. #if WASM_ENABLE_FAST_INTERP != 0
  11488. POP_OFFSET_TYPE(ref_type);
  11489. POP_TYPE(ref_type);
  11490. POP_OFFSET_TYPE(ref_type);
  11491. POP_TYPE(ref_type);
  11492. PUSH_OFFSET_TYPE(ref_type);
  11493. PUSH_TYPE(ref_type);
  11494. #else
  11495. POP2_AND_PUSH(ref_type, ref_type);
  11496. #endif
  11497. }
  11498. else {
  11499. #if WASM_ENABLE_FAST_INTERP != 0
  11500. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  11501. #endif
  11502. PUSH_TYPE(VALUE_TYPE_ANY);
  11503. }
  11504. break;
  11505. }
  11506. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  11507. case WASM_OP_SELECT_T:
  11508. {
  11509. uint8 vec_len, type;
  11510. #if WASM_ENABLE_GC != 0
  11511. WASMRefType *ref_type = NULL;
  11512. #endif
  11513. #if WASM_ENABLE_FAST_INTERP != 0
  11514. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  11515. #endif
  11516. pb_read_leb_uint32(p, p_end, vec_len);
  11517. if (vec_len != 1) {
  11518. /* typed select must have exactly one result */
  11519. set_error_buf(error_buf, error_buf_size,
  11520. "invalid result arity");
  11521. goto fail;
  11522. }
  11523. #if WASM_ENABLE_GC == 0
  11524. CHECK_BUF(p, p_end, 1);
  11525. type = read_uint8(p);
  11526. if (!is_valid_value_type_for_interpreter(type)) {
  11527. set_error_buf(error_buf, error_buf_size,
  11528. "unknown value type");
  11529. goto fail;
  11530. }
  11531. #else
  11532. p_org = p + 1;
  11533. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  11534. module->type_count, &need_ref_type_map,
  11535. &wasm_ref_type, false, error_buf,
  11536. error_buf_size)) {
  11537. goto fail;
  11538. }
  11539. type = wasm_ref_type.ref_type;
  11540. if (need_ref_type_map) {
  11541. if (!(ref_type = reftype_set_insert(
  11542. module->ref_type_set, &wasm_ref_type, error_buf,
  11543. error_buf_size))) {
  11544. goto fail;
  11545. }
  11546. }
  11547. #if WASM_ENABLE_FAST_INTERP == 0
  11548. while (p_org < p) {
  11549. #if WASM_ENABLE_DEBUG_INTERP != 0
  11550. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11551. error_buf_size)) {
  11552. goto fail;
  11553. }
  11554. #endif
  11555. /* Ignore extra bytes for interpreter */
  11556. *p_org++ = WASM_OP_NOP;
  11557. }
  11558. #endif
  11559. #endif /* end of WASM_ENABLE_GC == 0 */
  11560. POP_I32();
  11561. #if WASM_ENABLE_FAST_INTERP != 0
  11562. if (loader_ctx->p_code_compiled) {
  11563. uint8 opcode_tmp = WASM_OP_SELECT;
  11564. if (type == VALUE_TYPE_V128) {
  11565. #if (WASM_ENABLE_SIMD == 0) \
  11566. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11567. && (WASM_ENABLE_FAST_INTERP == 0))
  11568. set_error_buf(error_buf, error_buf_size,
  11569. "SIMD v128 type isn't supported");
  11570. goto fail;
  11571. #endif
  11572. }
  11573. else {
  11574. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  11575. opcode_tmp = WASM_OP_SELECT_64;
  11576. #if WASM_ENABLE_GC != 0
  11577. if (wasm_is_type_reftype(type))
  11578. opcode_tmp = WASM_OP_SELECT_T;
  11579. #endif
  11580. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  11581. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11582. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  11583. handle_table[opcode_tmp];
  11584. #else
  11585. #if UINTPTR_MAX == UINT64_MAX
  11586. /* emit int32 relative offset in 64-bit target */
  11587. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  11588. - (uint8 *)handle_table[0]);
  11589. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  11590. offset;
  11591. #else
  11592. /* emit uint32 label address in 32-bit target */
  11593. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  11594. (uint32)(uintptr_t)handle_table[opcode_tmp];
  11595. #endif
  11596. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11597. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  11598. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  11599. *(p_code_compiled_tmp - 1) = opcode_tmp;
  11600. #else
  11601. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11602. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11603. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11604. }
  11605. }
  11606. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11607. POP_REF(type);
  11608. #if WASM_ENABLE_GC != 0
  11609. if (need_ref_type_map) {
  11610. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11611. wasm_reftype_struct_size(ref_type));
  11612. }
  11613. #endif
  11614. POP_REF(type);
  11615. #if WASM_ENABLE_GC != 0
  11616. if (need_ref_type_map) {
  11617. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11618. wasm_reftype_struct_size(ref_type));
  11619. }
  11620. #endif
  11621. PUSH_REF(type);
  11622. #if WASM_ENABLE_WAMR_COMPILER != 0
  11623. module->is_ref_types_used = true;
  11624. #endif
  11625. (void)vec_len;
  11626. break;
  11627. }
  11628. /* table.get x. tables[x]. [it] -> [t] */
  11629. /* table.set x. tables[x]. [it t] -> [] */
  11630. case WASM_OP_TABLE_GET:
  11631. case WASM_OP_TABLE_SET:
  11632. {
  11633. uint8 decl_ref_type;
  11634. #if WASM_ENABLE_GC != 0
  11635. WASMRefType *ref_type;
  11636. #endif
  11637. pb_read_leb_uint32(p, p_end, table_idx);
  11638. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11639. #if WASM_ENABLE_GC != 0
  11640. (void **)&ref_type,
  11641. #else
  11642. NULL,
  11643. #endif
  11644. error_buf, error_buf_size))
  11645. goto fail;
  11646. #if WASM_ENABLE_GC != 0
  11647. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11648. bh_assert(ref_type);
  11649. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11650. ref_type, wasm_reftype_struct_size(ref_type));
  11651. }
  11652. #endif
  11653. #if WASM_ENABLE_FAST_INTERP != 0
  11654. emit_uint32(loader_ctx, table_idx);
  11655. #endif
  11656. #if WASM_ENABLE_MEMORY64 != 0
  11657. table_elem_idx_type = is_table_64bit(module, table_idx)
  11658. ? VALUE_TYPE_I64
  11659. : VALUE_TYPE_I32;
  11660. #endif
  11661. if (opcode == WASM_OP_TABLE_GET) {
  11662. POP_TBL_ELEM_IDX();
  11663. #if WASM_ENABLE_FAST_INTERP != 0
  11664. PUSH_OFFSET_TYPE(decl_ref_type);
  11665. #endif
  11666. PUSH_TYPE(decl_ref_type);
  11667. }
  11668. else {
  11669. #if WASM_ENABLE_FAST_INTERP != 0
  11670. POP_OFFSET_TYPE(decl_ref_type);
  11671. #endif
  11672. POP_TYPE(decl_ref_type);
  11673. POP_TBL_ELEM_IDX();
  11674. }
  11675. #if WASM_ENABLE_WAMR_COMPILER != 0
  11676. module->is_ref_types_used = true;
  11677. #endif
  11678. break;
  11679. }
  11680. case WASM_OP_REF_NULL:
  11681. {
  11682. uint8 ref_type;
  11683. #if WASM_ENABLE_GC == 0
  11684. CHECK_BUF(p, p_end, 1);
  11685. ref_type = read_uint8(p);
  11686. if (ref_type != VALUE_TYPE_FUNCREF
  11687. && ref_type != VALUE_TYPE_EXTERNREF) {
  11688. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11689. goto fail;
  11690. }
  11691. #else
  11692. pb_read_leb_int32(p, p_end, heap_type);
  11693. if (heap_type >= 0) {
  11694. if (!check_type_index(module, module->type_count, heap_type,
  11695. error_buf, error_buf_size)) {
  11696. goto fail;
  11697. }
  11698. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11699. true, heap_type);
  11700. ref_type = wasm_ref_type.ref_type;
  11701. }
  11702. else {
  11703. if (!wasm_is_valid_heap_type(heap_type)) {
  11704. set_error_buf(error_buf, error_buf_size,
  11705. "unknown type");
  11706. goto fail;
  11707. }
  11708. ref_type = (uint8)((int32)0x80 + heap_type);
  11709. }
  11710. #endif /* end of WASM_ENABLE_GC == 0 */
  11711. #if WASM_ENABLE_FAST_INTERP != 0
  11712. PUSH_OFFSET_TYPE(ref_type);
  11713. #endif
  11714. PUSH_TYPE(ref_type);
  11715. #if WASM_ENABLE_WAMR_COMPILER != 0
  11716. module->is_ref_types_used = true;
  11717. #endif
  11718. break;
  11719. }
  11720. case WASM_OP_REF_IS_NULL:
  11721. {
  11722. #if WASM_ENABLE_GC == 0
  11723. #if WASM_ENABLE_FAST_INTERP != 0
  11724. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11725. int32 block_stack_cell_num =
  11726. (int32)(loader_ctx->stack_cell_num
  11727. - cur_block->stack_cell_num);
  11728. if (block_stack_cell_num <= 0) {
  11729. if (!cur_block->is_stack_polymorphic) {
  11730. set_error_buf(
  11731. error_buf, error_buf_size,
  11732. "type mismatch: expect data but stack was empty");
  11733. goto fail;
  11734. }
  11735. }
  11736. else {
  11737. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11738. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11739. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11740. if (!wasm_loader_pop_frame_ref_offset(
  11741. loader_ctx, *(loader_ctx->frame_ref - 1),
  11742. error_buf, error_buf_size)) {
  11743. goto fail;
  11744. }
  11745. }
  11746. else {
  11747. set_error_buf(error_buf, error_buf_size,
  11748. "type mismatch");
  11749. goto fail;
  11750. }
  11751. }
  11752. #else
  11753. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11754. error_buf, error_buf_size)
  11755. && !wasm_loader_pop_frame_ref(loader_ctx,
  11756. VALUE_TYPE_EXTERNREF,
  11757. error_buf, error_buf_size)) {
  11758. goto fail;
  11759. }
  11760. #endif
  11761. #else /* else of WASM_ENABLE_GC == 0 */
  11762. uint8 type;
  11763. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11764. error_buf, error_buf_size)) {
  11765. goto fail;
  11766. }
  11767. #endif
  11768. PUSH_I32();
  11769. #if WASM_ENABLE_WAMR_COMPILER != 0
  11770. module->is_ref_types_used = true;
  11771. #endif
  11772. break;
  11773. }
  11774. case WASM_OP_REF_FUNC:
  11775. {
  11776. pb_read_leb_uint32(p, p_end, func_idx);
  11777. if (!check_function_index(module, func_idx, error_buf,
  11778. error_buf_size)) {
  11779. goto fail;
  11780. }
  11781. /* Refer to a forward-declared function:
  11782. the function must be an import, exported, or present in
  11783. a table elem segment or global initializer to be used as
  11784. the operand to ref.func */
  11785. if (func_idx >= module->import_function_count) {
  11786. WASMTableSeg *table_seg = module->table_segments;
  11787. bool func_declared = false;
  11788. uint32 j;
  11789. for (i = 0; i < module->global_count; i++) {
  11790. if (module->globals[i].type.val_type
  11791. == VALUE_TYPE_FUNCREF
  11792. && module->globals[i].init_expr.init_expr_type
  11793. == INIT_EXPR_TYPE_FUNCREF_CONST
  11794. && module->globals[i].init_expr.u.u32 == func_idx) {
  11795. func_declared = true;
  11796. break;
  11797. }
  11798. }
  11799. if (!func_declared) {
  11800. /* Check whether the function is declared in table segs,
  11801. note that it doesn't matter whether the table seg's
  11802. mode is passive, active or declarative. */
  11803. for (i = 0; i < module->table_seg_count;
  11804. i++, table_seg++) {
  11805. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11806. #if WASM_ENABLE_GC != 0
  11807. /* elem type is (ref null? func) or
  11808. (ref null? $t) */
  11809. || ((table_seg->elem_type
  11810. == REF_TYPE_HT_NON_NULLABLE
  11811. || table_seg->elem_type
  11812. == REF_TYPE_HT_NULLABLE)
  11813. && (table_seg->elem_ref_type->ref_ht_common
  11814. .heap_type
  11815. == HEAP_TYPE_FUNC
  11816. || table_seg->elem_ref_type
  11817. ->ref_ht_common.heap_type
  11818. > 0))
  11819. #endif
  11820. ) {
  11821. for (j = 0; j < table_seg->value_count; j++) {
  11822. if (table_seg->init_values[j].u.ref_index
  11823. == func_idx) {
  11824. func_declared = true;
  11825. break;
  11826. }
  11827. }
  11828. }
  11829. }
  11830. }
  11831. if (!func_declared) {
  11832. /* Check whether the function is exported */
  11833. for (i = 0; i < module->export_count; i++) {
  11834. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11835. && module->exports[i].index == func_idx) {
  11836. func_declared = true;
  11837. break;
  11838. }
  11839. }
  11840. }
  11841. if (!func_declared) {
  11842. set_error_buf(error_buf, error_buf_size,
  11843. "undeclared function reference");
  11844. goto fail;
  11845. }
  11846. }
  11847. #if WASM_ENABLE_FAST_INTERP != 0
  11848. emit_uint32(loader_ctx, func_idx);
  11849. #endif
  11850. #if WASM_ENABLE_GC == 0
  11851. PUSH_FUNCREF();
  11852. #else
  11853. if (func_idx < module->import_function_count)
  11854. type_idx =
  11855. module->import_functions[func_idx].u.function.type_idx;
  11856. else
  11857. type_idx = module
  11858. ->functions[func_idx
  11859. - module->import_function_count]
  11860. ->type_idx;
  11861. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11862. false, type_idx);
  11863. PUSH_REF(wasm_ref_type.ref_type);
  11864. #endif
  11865. #if WASM_ENABLE_WAMR_COMPILER != 0
  11866. module->is_ref_types_used = true;
  11867. #endif
  11868. break;
  11869. }
  11870. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11871. #if WASM_ENABLE_GC != 0
  11872. case WASM_OP_REF_AS_NON_NULL:
  11873. case WASM_OP_BR_ON_NULL:
  11874. {
  11875. uint8 type;
  11876. WASMRefType ref_type;
  11877. /* POP (ref null ht) and get the converted (ref ht) */
  11878. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11879. error_buf, error_buf_size)) {
  11880. goto fail;
  11881. }
  11882. if (opcode == WASM_OP_BR_ON_NULL) {
  11883. if (!(frame_csp_tmp =
  11884. check_branch_block(loader_ctx, &p, p_end, opcode,
  11885. error_buf, error_buf_size))) {
  11886. goto fail;
  11887. }
  11888. #if WASM_ENABLE_FAST_INTERP != 0
  11889. disable_emit = true;
  11890. #endif
  11891. }
  11892. /* PUSH the converted (ref ht) */
  11893. if (type != VALUE_TYPE_ANY) {
  11894. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11895. sizeof(WASMRefType));
  11896. }
  11897. PUSH_REF(type);
  11898. break;
  11899. }
  11900. case WASM_OP_BR_ON_NON_NULL:
  11901. {
  11902. uint8 type;
  11903. WASMRefType ref_type;
  11904. uint32 available_stack_cell =
  11905. loader_ctx->stack_cell_num
  11906. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11907. /* POP (ref null ht) and get the converted (ref ht) */
  11908. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11909. error_buf, error_buf_size)) {
  11910. goto fail;
  11911. }
  11912. #if WASM_ENABLE_FAST_INTERP != 0
  11913. disable_emit = true;
  11914. #endif
  11915. /* Temporarily PUSH back (ref ht), check brach block and
  11916. then POP it */
  11917. if (available_stack_cell
  11918. > 0) { /* stack isn't in polymorphic state */
  11919. if (type != VALUE_TYPE_ANY) {
  11920. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11921. &ref_type, sizeof(WASMRefType));
  11922. }
  11923. PUSH_REF(type);
  11924. }
  11925. if (!(frame_csp_tmp =
  11926. check_branch_block(loader_ctx, &p, p_end, opcode,
  11927. error_buf, error_buf_size))) {
  11928. goto fail;
  11929. }
  11930. if (available_stack_cell
  11931. > 0) { /* stack isn't in polymorphic state */
  11932. POP_REF(type);
  11933. #if WASM_ENABLE_FAST_INTERP != 0
  11934. /* Erase the opnd offset emitted by POP_REF() */
  11935. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11936. #endif
  11937. }
  11938. break;
  11939. }
  11940. case WASM_OP_REF_EQ:
  11941. POP_REF(REF_TYPE_EQREF);
  11942. POP_REF(REF_TYPE_EQREF);
  11943. PUSH_I32();
  11944. break;
  11945. #endif /* end of WASM_ENABLE_GC != 0 */
  11946. case WASM_OP_GET_LOCAL:
  11947. {
  11948. p_org = p - 1;
  11949. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11950. PUSH_TYPE(local_type);
  11951. #if WASM_ENABLE_GC != 0
  11952. /* Cannot get a non-nullable and unset local */
  11953. if (local_idx >= param_count
  11954. && wasm_is_reftype_htref_non_nullable(local_type)
  11955. && !wasm_loader_get_local_status(loader_ctx,
  11956. local_idx - param_count)) {
  11957. set_error_buf(error_buf, error_buf_size,
  11958. "uninitialized local");
  11959. goto fail;
  11960. }
  11961. #endif
  11962. #if WASM_ENABLE_FAST_INTERP != 0
  11963. /* Get Local is optimized out */
  11964. skip_label();
  11965. disable_emit = true;
  11966. operand_offset = local_offset;
  11967. PUSH_OFFSET_TYPE(local_type);
  11968. #else
  11969. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11970. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11971. if (local_offset < 0x80
  11972. #if WASM_ENABLE_GC != 0
  11973. && !wasm_is_type_reftype(local_type)
  11974. #endif
  11975. ) {
  11976. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11977. if (is_32bit_type(local_type)) {
  11978. *p_org++ = (uint8)local_offset;
  11979. }
  11980. else {
  11981. *p_org++ = (uint8)(local_offset | 0x80);
  11982. }
  11983. while (p_org < p) {
  11984. *p_org++ = WASM_OP_NOP;
  11985. }
  11986. }
  11987. #endif
  11988. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11989. break;
  11990. }
  11991. case WASM_OP_SET_LOCAL:
  11992. {
  11993. p_org = p - 1;
  11994. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11995. #if WASM_ENABLE_FAST_INTERP != 0
  11996. if (!(preserve_referenced_local(
  11997. loader_ctx, opcode, local_offset, local_type,
  11998. &preserve_local, error_buf, error_buf_size)))
  11999. goto fail;
  12000. if (local_offset < 256
  12001. #if WASM_ENABLE_GC != 0
  12002. && !wasm_is_type_reftype(local_type)
  12003. #endif
  12004. ) {
  12005. skip_label();
  12006. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  12007. if (loader_ctx->p_code_compiled)
  12008. STORE_U16(loader_ctx->p_code_compiled - 2,
  12009. local_offset);
  12010. loader_ctx->frame_offset--;
  12011. loader_ctx->dynamic_offset--;
  12012. }
  12013. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  12014. if (loader_ctx->p_code_compiled)
  12015. STORE_U16(loader_ctx->p_code_compiled - 2,
  12016. local_offset);
  12017. loader_ctx->frame_offset -= 2;
  12018. loader_ctx->dynamic_offset -= 2;
  12019. }
  12020. else {
  12021. if (is_32bit_type(local_type)) {
  12022. emit_label(EXT_OP_SET_LOCAL_FAST);
  12023. emit_byte(loader_ctx, (uint8)local_offset);
  12024. }
  12025. else if (is_64bit_type(local_type)) {
  12026. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  12027. emit_byte(loader_ctx, (uint8)local_offset);
  12028. }
  12029. #if WASM_ENABLE_SIMDE != 0
  12030. else if (local_type == VALUE_TYPE_V128) {
  12031. emit_label(EXT_OP_SET_LOCAL_FAST_V128);
  12032. emit_byte(loader_ctx, (uint8)local_offset);
  12033. }
  12034. #endif
  12035. else {
  12036. set_error_buf(error_buf, error_buf_size,
  12037. "unknown local type");
  12038. goto fail;
  12039. }
  12040. POP_OFFSET_TYPE(local_type);
  12041. }
  12042. }
  12043. else { /* local index larger than 255, reserve leb */
  12044. emit_uint32(loader_ctx, local_idx);
  12045. POP_OFFSET_TYPE(local_type);
  12046. }
  12047. #else
  12048. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12049. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12050. if (local_offset < 0x80
  12051. #if WASM_ENABLE_GC != 0
  12052. && !wasm_is_type_reftype(local_type)
  12053. #endif
  12054. ) {
  12055. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  12056. if (is_32bit_type(local_type)) {
  12057. *p_org++ = (uint8)local_offset;
  12058. }
  12059. else {
  12060. *p_org++ = (uint8)(local_offset | 0x80);
  12061. }
  12062. while (p_org < p) {
  12063. *p_org++ = WASM_OP_NOP;
  12064. }
  12065. }
  12066. #endif
  12067. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12068. #if WASM_ENABLE_GC != 0
  12069. if (local_idx >= param_count) {
  12070. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12071. }
  12072. #endif
  12073. POP_TYPE(local_type);
  12074. break;
  12075. }
  12076. case WASM_OP_TEE_LOCAL:
  12077. {
  12078. p_org = p - 1;
  12079. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  12080. #if WASM_ENABLE_FAST_INTERP != 0
  12081. /* If the stack is in polymorphic state, do fake pop and push on
  12082. offset stack to keep the depth of offset stack to be the
  12083. same with ref stack */
  12084. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  12085. if (cur_block->is_stack_polymorphic) {
  12086. POP_OFFSET_TYPE(local_type);
  12087. PUSH_OFFSET_TYPE(local_type);
  12088. }
  12089. #endif
  12090. POP_TYPE(local_type);
  12091. PUSH_TYPE(local_type);
  12092. #if WASM_ENABLE_FAST_INTERP != 0
  12093. if (!(preserve_referenced_local(
  12094. loader_ctx, opcode, local_offset, local_type,
  12095. &preserve_local, error_buf, error_buf_size)))
  12096. goto fail;
  12097. if (local_offset < 256
  12098. #if WASM_ENABLE_GC != 0
  12099. && !wasm_is_type_reftype(local_type)
  12100. #endif
  12101. ) {
  12102. skip_label();
  12103. if (is_32bit_type(local_type)) {
  12104. emit_label(EXT_OP_TEE_LOCAL_FAST);
  12105. emit_byte(loader_ctx, (uint8)local_offset);
  12106. }
  12107. #if WASM_ENABLE_SIMDE != 0
  12108. else if (local_type == VALUE_TYPE_V128) {
  12109. emit_label(EXT_OP_TEE_LOCAL_FAST_V128);
  12110. emit_byte(loader_ctx, (uint8)local_offset);
  12111. }
  12112. #endif
  12113. else {
  12114. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  12115. emit_byte(loader_ctx, (uint8)local_offset);
  12116. }
  12117. }
  12118. else { /* local index larger than 255, reserve leb */
  12119. emit_uint32(loader_ctx, local_idx);
  12120. }
  12121. emit_operand(loader_ctx,
  12122. *(loader_ctx->frame_offset
  12123. - wasm_value_type_cell_num(local_type)));
  12124. #else
  12125. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  12126. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  12127. if (local_offset < 0x80
  12128. #if WASM_ENABLE_GC != 0
  12129. && !wasm_is_type_reftype(local_type)
  12130. #endif
  12131. ) {
  12132. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  12133. if (is_32bit_type(local_type)) {
  12134. *p_org++ = (uint8)local_offset;
  12135. }
  12136. else {
  12137. *p_org++ = (uint8)(local_offset | 0x80);
  12138. }
  12139. while (p_org < p) {
  12140. *p_org++ = WASM_OP_NOP;
  12141. }
  12142. }
  12143. #endif
  12144. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  12145. #if WASM_ENABLE_GC != 0
  12146. if (local_idx >= param_count) {
  12147. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  12148. }
  12149. #endif
  12150. break;
  12151. }
  12152. case WASM_OP_GET_GLOBAL:
  12153. {
  12154. #if WASM_ENABLE_GC != 0
  12155. WASMRefType *ref_type;
  12156. #endif
  12157. p_org = p - 1;
  12158. pb_read_leb_uint32(p, p_end, global_idx);
  12159. if (global_idx >= global_count) {
  12160. set_error_buf(error_buf, error_buf_size, "unknown global");
  12161. goto fail;
  12162. }
  12163. global_type = global_idx < module->import_global_count
  12164. ? module->import_globals[global_idx]
  12165. .u.global.type.val_type
  12166. : module
  12167. ->globals[global_idx
  12168. - module->import_global_count]
  12169. .type.val_type;
  12170. #if WASM_ENABLE_GC != 0
  12171. ref_type =
  12172. global_idx < module->import_global_count
  12173. ? module->import_globals[global_idx].u.global.ref_type
  12174. : module
  12175. ->globals[global_idx
  12176. - module->import_global_count]
  12177. .ref_type;
  12178. if (wasm_is_type_multi_byte_type(global_type)) {
  12179. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12180. wasm_reftype_struct_size(ref_type));
  12181. }
  12182. #endif
  12183. PUSH_TYPE(global_type);
  12184. #if WASM_ENABLE_FAST_INTERP == 0
  12185. if (global_type == VALUE_TYPE_I64
  12186. || global_type == VALUE_TYPE_F64) {
  12187. #if WASM_ENABLE_DEBUG_INTERP != 0
  12188. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12189. error_buf_size)) {
  12190. goto fail;
  12191. }
  12192. #endif
  12193. *p_org = WASM_OP_GET_GLOBAL_64;
  12194. }
  12195. #else /* else of WASM_ENABLE_FAST_INTERP */
  12196. if (global_type == VALUE_TYPE_I64
  12197. || global_type == VALUE_TYPE_F64) {
  12198. skip_label();
  12199. emit_label(WASM_OP_GET_GLOBAL_64);
  12200. }
  12201. #if WASM_ENABLE_SIMDE != 0
  12202. if (global_type == VALUE_TYPE_V128) {
  12203. skip_label();
  12204. emit_label(WASM_OP_GET_GLOBAL_V128);
  12205. }
  12206. #endif /* end of WASM_ENABLE_SIMDE */
  12207. emit_uint32(loader_ctx, global_idx);
  12208. PUSH_OFFSET_TYPE(global_type);
  12209. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12210. break;
  12211. }
  12212. case WASM_OP_SET_GLOBAL:
  12213. {
  12214. bool is_mutable = false;
  12215. #if WASM_ENABLE_GC != 0
  12216. WASMRefType *ref_type;
  12217. #endif
  12218. p_org = p - 1;
  12219. pb_read_leb_uint32(p, p_end, global_idx);
  12220. if (global_idx >= global_count) {
  12221. set_error_buf(error_buf, error_buf_size, "unknown global");
  12222. goto fail;
  12223. }
  12224. is_mutable = global_idx < module->import_global_count
  12225. ? module->import_globals[global_idx]
  12226. .u.global.type.is_mutable
  12227. : module
  12228. ->globals[global_idx
  12229. - module->import_global_count]
  12230. .type.is_mutable;
  12231. if (!is_mutable) {
  12232. #if WASM_ENABLE_GC == 0
  12233. set_error_buf(error_buf, error_buf_size,
  12234. "global is immutable");
  12235. #else
  12236. set_error_buf(error_buf, error_buf_size,
  12237. "immutable global");
  12238. #endif
  12239. goto fail;
  12240. }
  12241. global_type = global_idx < module->import_global_count
  12242. ? module->import_globals[global_idx]
  12243. .u.global.type.val_type
  12244. : module
  12245. ->globals[global_idx
  12246. - module->import_global_count]
  12247. .type.val_type;
  12248. #if WASM_ENABLE_GC != 0
  12249. ref_type =
  12250. global_idx < module->import_global_count
  12251. ? module->import_globals[global_idx].u.global.ref_type
  12252. : module
  12253. ->globals[global_idx
  12254. - module->import_global_count]
  12255. .ref_type;
  12256. if (wasm_is_type_multi_byte_type(global_type)) {
  12257. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  12258. wasm_reftype_struct_size(ref_type));
  12259. }
  12260. #endif
  12261. #if WASM_ENABLE_FAST_INTERP == 0
  12262. if (global_type == VALUE_TYPE_I64
  12263. || global_type == VALUE_TYPE_F64) {
  12264. #if WASM_ENABLE_DEBUG_INTERP != 0
  12265. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12266. error_buf_size)) {
  12267. goto fail;
  12268. }
  12269. #endif
  12270. *p_org = WASM_OP_SET_GLOBAL_64;
  12271. }
  12272. else if (module->aux_stack_size > 0
  12273. && global_idx == module->aux_stack_top_global_index) {
  12274. #if WASM_ENABLE_DEBUG_INTERP != 0
  12275. if (!record_fast_op(module, p_org, *p_org, error_buf,
  12276. error_buf_size)) {
  12277. goto fail;
  12278. }
  12279. #endif
  12280. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  12281. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12282. func->has_op_set_global_aux_stack = true;
  12283. #endif
  12284. }
  12285. #else /* else of WASM_ENABLE_FAST_INTERP */
  12286. if (global_type == VALUE_TYPE_I64
  12287. || global_type == VALUE_TYPE_F64) {
  12288. skip_label();
  12289. emit_label(WASM_OP_SET_GLOBAL_64);
  12290. }
  12291. else if (module->aux_stack_size > 0
  12292. && global_idx == module->aux_stack_top_global_index) {
  12293. skip_label();
  12294. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  12295. }
  12296. #if WASM_ENABLE_SIMDE != 0
  12297. else if (global_type == VALUE_TYPE_V128) {
  12298. skip_label();
  12299. emit_label(WASM_OP_SET_GLOBAL_V128);
  12300. }
  12301. #endif /* end of WASM_ENABLE_SIMDE */
  12302. emit_uint32(loader_ctx, global_idx);
  12303. POP_OFFSET_TYPE(global_type);
  12304. #endif /* end of WASM_ENABLE_FAST_INTERP */
  12305. POP_TYPE(global_type);
  12306. break;
  12307. }
  12308. /* load */
  12309. case WASM_OP_I32_LOAD:
  12310. case WASM_OP_I32_LOAD8_S:
  12311. case WASM_OP_I32_LOAD8_U:
  12312. case WASM_OP_I32_LOAD16_S:
  12313. case WASM_OP_I32_LOAD16_U:
  12314. case WASM_OP_I64_LOAD:
  12315. case WASM_OP_I64_LOAD8_S:
  12316. case WASM_OP_I64_LOAD8_U:
  12317. case WASM_OP_I64_LOAD16_S:
  12318. case WASM_OP_I64_LOAD16_U:
  12319. case WASM_OP_I64_LOAD32_S:
  12320. case WASM_OP_I64_LOAD32_U:
  12321. case WASM_OP_F32_LOAD:
  12322. case WASM_OP_F64_LOAD:
  12323. /* store */
  12324. case WASM_OP_I32_STORE:
  12325. case WASM_OP_I32_STORE8:
  12326. case WASM_OP_I32_STORE16:
  12327. case WASM_OP_I64_STORE:
  12328. case WASM_OP_I64_STORE8:
  12329. case WASM_OP_I64_STORE16:
  12330. case WASM_OP_I64_STORE32:
  12331. case WASM_OP_F32_STORE:
  12332. case WASM_OP_F64_STORE:
  12333. {
  12334. #if WASM_ENABLE_FAST_INTERP != 0
  12335. /* change F32/F64 into I32/I64 */
  12336. if (opcode == WASM_OP_F32_LOAD) {
  12337. skip_label();
  12338. emit_label(WASM_OP_I32_LOAD);
  12339. }
  12340. else if (opcode == WASM_OP_F64_LOAD) {
  12341. skip_label();
  12342. emit_label(WASM_OP_I64_LOAD);
  12343. }
  12344. else if (opcode == WASM_OP_F32_STORE) {
  12345. skip_label();
  12346. emit_label(WASM_OP_I32_STORE);
  12347. }
  12348. else if (opcode == WASM_OP_F64_STORE) {
  12349. skip_label();
  12350. emit_label(WASM_OP_I64_STORE);
  12351. }
  12352. #endif
  12353. CHECK_MEMORY();
  12354. pb_read_leb_memarg(p, p_end, align); /* align */
  12355. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  12356. if (!check_memory_access_align(opcode, align, error_buf,
  12357. error_buf_size)) {
  12358. goto fail;
  12359. }
  12360. #if WASM_ENABLE_FAST_INTERP != 0
  12361. emit_uint32(loader_ctx, mem_offset);
  12362. #endif
  12363. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12364. func->has_memory_operations = true;
  12365. #endif
  12366. switch (opcode) {
  12367. /* load */
  12368. case WASM_OP_I32_LOAD:
  12369. case WASM_OP_I32_LOAD8_S:
  12370. case WASM_OP_I32_LOAD8_U:
  12371. case WASM_OP_I32_LOAD16_S:
  12372. case WASM_OP_I32_LOAD16_U:
  12373. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  12374. break;
  12375. case WASM_OP_I64_LOAD:
  12376. case WASM_OP_I64_LOAD8_S:
  12377. case WASM_OP_I64_LOAD8_U:
  12378. case WASM_OP_I64_LOAD16_S:
  12379. case WASM_OP_I64_LOAD16_U:
  12380. case WASM_OP_I64_LOAD32_S:
  12381. case WASM_OP_I64_LOAD32_U:
  12382. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  12383. break;
  12384. case WASM_OP_F32_LOAD:
  12385. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  12386. break;
  12387. case WASM_OP_F64_LOAD:
  12388. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  12389. break;
  12390. /* store */
  12391. case WASM_OP_I32_STORE:
  12392. case WASM_OP_I32_STORE8:
  12393. case WASM_OP_I32_STORE16:
  12394. POP_I32();
  12395. POP_MEM_OFFSET();
  12396. break;
  12397. case WASM_OP_I64_STORE:
  12398. case WASM_OP_I64_STORE8:
  12399. case WASM_OP_I64_STORE16:
  12400. case WASM_OP_I64_STORE32:
  12401. POP_I64();
  12402. POP_MEM_OFFSET();
  12403. break;
  12404. case WASM_OP_F32_STORE:
  12405. POP_F32();
  12406. POP_MEM_OFFSET();
  12407. break;
  12408. case WASM_OP_F64_STORE:
  12409. POP_F64();
  12410. POP_MEM_OFFSET();
  12411. break;
  12412. default:
  12413. break;
  12414. }
  12415. break;
  12416. }
  12417. case WASM_OP_MEMORY_SIZE:
  12418. CHECK_MEMORY();
  12419. pb_read_leb_uint32(p, p_end, memidx);
  12420. check_memidx(module, memidx);
  12421. PUSH_PAGE_COUNT();
  12422. module->possible_memory_grow = true;
  12423. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12424. func->has_memory_operations = true;
  12425. #endif
  12426. break;
  12427. case WASM_OP_MEMORY_GROW:
  12428. CHECK_MEMORY();
  12429. pb_read_leb_uint32(p, p_end, memidx);
  12430. check_memidx(module, memidx);
  12431. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  12432. module->possible_memory_grow = true;
  12433. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  12434. || WASM_ENABLE_WAMR_COMPILER != 0
  12435. func->has_op_memory_grow = true;
  12436. #endif
  12437. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12438. func->has_memory_operations = true;
  12439. #endif
  12440. break;
  12441. case WASM_OP_I32_CONST:
  12442. pb_read_leb_int32(p, p_end, i32_const);
  12443. #if WASM_ENABLE_FAST_INTERP != 0
  12444. skip_label();
  12445. disable_emit = true;
  12446. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  12447. if (operand_offset == 0) {
  12448. disable_emit = false;
  12449. emit_label(WASM_OP_I32_CONST);
  12450. emit_uint32(loader_ctx, i32_const);
  12451. }
  12452. #else
  12453. (void)i32_const;
  12454. #endif
  12455. PUSH_I32();
  12456. break;
  12457. case WASM_OP_I64_CONST:
  12458. pb_read_leb_int64(p, p_end, i64_const);
  12459. #if WASM_ENABLE_FAST_INTERP != 0
  12460. skip_label();
  12461. disable_emit = true;
  12462. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  12463. if (operand_offset == 0) {
  12464. disable_emit = false;
  12465. emit_label(WASM_OP_I64_CONST);
  12466. emit_uint64(loader_ctx, i64_const);
  12467. }
  12468. #endif
  12469. PUSH_I64();
  12470. break;
  12471. case WASM_OP_F32_CONST:
  12472. CHECK_BUF(p, p_end, sizeof(float32));
  12473. p += sizeof(float32);
  12474. #if WASM_ENABLE_FAST_INTERP != 0
  12475. skip_label();
  12476. disable_emit = true;
  12477. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  12478. sizeof(float32));
  12479. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  12480. if (operand_offset == 0) {
  12481. disable_emit = false;
  12482. emit_label(WASM_OP_F32_CONST);
  12483. emit_float32(loader_ctx, f32_const);
  12484. }
  12485. #endif
  12486. PUSH_F32();
  12487. break;
  12488. case WASM_OP_F64_CONST:
  12489. CHECK_BUF(p, p_end, sizeof(float64));
  12490. p += sizeof(float64);
  12491. #if WASM_ENABLE_FAST_INTERP != 0
  12492. skip_label();
  12493. disable_emit = true;
  12494. /* Some MCU may require 8-byte align */
  12495. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  12496. sizeof(float64));
  12497. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  12498. if (operand_offset == 0) {
  12499. disable_emit = false;
  12500. emit_label(WASM_OP_F64_CONST);
  12501. emit_float64(loader_ctx, f64_const);
  12502. }
  12503. #endif
  12504. PUSH_F64();
  12505. break;
  12506. case WASM_OP_I32_EQZ:
  12507. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12508. break;
  12509. case WASM_OP_I32_EQ:
  12510. case WASM_OP_I32_NE:
  12511. case WASM_OP_I32_LT_S:
  12512. case WASM_OP_I32_LT_U:
  12513. case WASM_OP_I32_GT_S:
  12514. case WASM_OP_I32_GT_U:
  12515. case WASM_OP_I32_LE_S:
  12516. case WASM_OP_I32_LE_U:
  12517. case WASM_OP_I32_GE_S:
  12518. case WASM_OP_I32_GE_U:
  12519. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12520. break;
  12521. case WASM_OP_I64_EQZ:
  12522. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12523. break;
  12524. case WASM_OP_I64_EQ:
  12525. case WASM_OP_I64_NE:
  12526. case WASM_OP_I64_LT_S:
  12527. case WASM_OP_I64_LT_U:
  12528. case WASM_OP_I64_GT_S:
  12529. case WASM_OP_I64_GT_U:
  12530. case WASM_OP_I64_LE_S:
  12531. case WASM_OP_I64_LE_U:
  12532. case WASM_OP_I64_GE_S:
  12533. case WASM_OP_I64_GE_U:
  12534. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12535. break;
  12536. case WASM_OP_F32_EQ:
  12537. case WASM_OP_F32_NE:
  12538. case WASM_OP_F32_LT:
  12539. case WASM_OP_F32_GT:
  12540. case WASM_OP_F32_LE:
  12541. case WASM_OP_F32_GE:
  12542. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12543. break;
  12544. case WASM_OP_F64_EQ:
  12545. case WASM_OP_F64_NE:
  12546. case WASM_OP_F64_LT:
  12547. case WASM_OP_F64_GT:
  12548. case WASM_OP_F64_LE:
  12549. case WASM_OP_F64_GE:
  12550. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12551. break;
  12552. case WASM_OP_I32_CLZ:
  12553. case WASM_OP_I32_CTZ:
  12554. case WASM_OP_I32_POPCNT:
  12555. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12556. break;
  12557. case WASM_OP_I32_ADD:
  12558. case WASM_OP_I32_SUB:
  12559. case WASM_OP_I32_MUL:
  12560. case WASM_OP_I32_DIV_S:
  12561. case WASM_OP_I32_DIV_U:
  12562. case WASM_OP_I32_REM_S:
  12563. case WASM_OP_I32_REM_U:
  12564. case WASM_OP_I32_AND:
  12565. case WASM_OP_I32_OR:
  12566. case WASM_OP_I32_XOR:
  12567. case WASM_OP_I32_SHL:
  12568. case WASM_OP_I32_SHR_S:
  12569. case WASM_OP_I32_SHR_U:
  12570. case WASM_OP_I32_ROTL:
  12571. case WASM_OP_I32_ROTR:
  12572. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12573. break;
  12574. case WASM_OP_I64_CLZ:
  12575. case WASM_OP_I64_CTZ:
  12576. case WASM_OP_I64_POPCNT:
  12577. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12578. break;
  12579. case WASM_OP_I64_ADD:
  12580. case WASM_OP_I64_SUB:
  12581. case WASM_OP_I64_MUL:
  12582. case WASM_OP_I64_DIV_S:
  12583. case WASM_OP_I64_DIV_U:
  12584. case WASM_OP_I64_REM_S:
  12585. case WASM_OP_I64_REM_U:
  12586. case WASM_OP_I64_AND:
  12587. case WASM_OP_I64_OR:
  12588. case WASM_OP_I64_XOR:
  12589. case WASM_OP_I64_SHL:
  12590. case WASM_OP_I64_SHR_S:
  12591. case WASM_OP_I64_SHR_U:
  12592. case WASM_OP_I64_ROTL:
  12593. case WASM_OP_I64_ROTR:
  12594. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12595. break;
  12596. case WASM_OP_F32_ABS:
  12597. case WASM_OP_F32_NEG:
  12598. case WASM_OP_F32_CEIL:
  12599. case WASM_OP_F32_FLOOR:
  12600. case WASM_OP_F32_TRUNC:
  12601. case WASM_OP_F32_NEAREST:
  12602. case WASM_OP_F32_SQRT:
  12603. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12604. break;
  12605. case WASM_OP_F32_ADD:
  12606. case WASM_OP_F32_SUB:
  12607. case WASM_OP_F32_MUL:
  12608. case WASM_OP_F32_DIV:
  12609. case WASM_OP_F32_MIN:
  12610. case WASM_OP_F32_MAX:
  12611. case WASM_OP_F32_COPYSIGN:
  12612. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  12613. break;
  12614. case WASM_OP_F64_ABS:
  12615. case WASM_OP_F64_NEG:
  12616. case WASM_OP_F64_CEIL:
  12617. case WASM_OP_F64_FLOOR:
  12618. case WASM_OP_F64_TRUNC:
  12619. case WASM_OP_F64_NEAREST:
  12620. case WASM_OP_F64_SQRT:
  12621. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12622. break;
  12623. case WASM_OP_F64_ADD:
  12624. case WASM_OP_F64_SUB:
  12625. case WASM_OP_F64_MUL:
  12626. case WASM_OP_F64_DIV:
  12627. case WASM_OP_F64_MIN:
  12628. case WASM_OP_F64_MAX:
  12629. case WASM_OP_F64_COPYSIGN:
  12630. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12631. break;
  12632. case WASM_OP_I32_WRAP_I64:
  12633. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12634. break;
  12635. case WASM_OP_I32_TRUNC_S_F32:
  12636. case WASM_OP_I32_TRUNC_U_F32:
  12637. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12638. break;
  12639. case WASM_OP_I32_TRUNC_S_F64:
  12640. case WASM_OP_I32_TRUNC_U_F64:
  12641. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12642. break;
  12643. case WASM_OP_I64_EXTEND_S_I32:
  12644. case WASM_OP_I64_EXTEND_U_I32:
  12645. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12646. break;
  12647. case WASM_OP_I64_TRUNC_S_F32:
  12648. case WASM_OP_I64_TRUNC_U_F32:
  12649. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12650. break;
  12651. case WASM_OP_I64_TRUNC_S_F64:
  12652. case WASM_OP_I64_TRUNC_U_F64:
  12653. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12654. break;
  12655. case WASM_OP_F32_CONVERT_S_I32:
  12656. case WASM_OP_F32_CONVERT_U_I32:
  12657. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12658. break;
  12659. case WASM_OP_F32_CONVERT_S_I64:
  12660. case WASM_OP_F32_CONVERT_U_I64:
  12661. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12662. break;
  12663. case WASM_OP_F32_DEMOTE_F64:
  12664. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12665. break;
  12666. case WASM_OP_F64_CONVERT_S_I32:
  12667. case WASM_OP_F64_CONVERT_U_I32:
  12668. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12669. break;
  12670. case WASM_OP_F64_CONVERT_S_I64:
  12671. case WASM_OP_F64_CONVERT_U_I64:
  12672. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12673. break;
  12674. case WASM_OP_F64_PROMOTE_F32:
  12675. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12676. break;
  12677. case WASM_OP_I32_REINTERPRET_F32:
  12678. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12679. break;
  12680. case WASM_OP_I64_REINTERPRET_F64:
  12681. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12682. break;
  12683. case WASM_OP_F32_REINTERPRET_I32:
  12684. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12685. break;
  12686. case WASM_OP_F64_REINTERPRET_I64:
  12687. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12688. break;
  12689. case WASM_OP_I32_EXTEND8_S:
  12690. case WASM_OP_I32_EXTEND16_S:
  12691. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12692. break;
  12693. case WASM_OP_I64_EXTEND8_S:
  12694. case WASM_OP_I64_EXTEND16_S:
  12695. case WASM_OP_I64_EXTEND32_S:
  12696. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12697. break;
  12698. #if WASM_ENABLE_GC != 0
  12699. case WASM_OP_GC_PREFIX:
  12700. {
  12701. uint32 opcode1;
  12702. pb_read_leb_uint32(p, p_end, opcode1);
  12703. #if WASM_ENABLE_FAST_INTERP != 0
  12704. emit_byte(loader_ctx, ((uint8)opcode1));
  12705. #endif
  12706. switch (opcode1) {
  12707. case WASM_OP_STRUCT_NEW:
  12708. case WASM_OP_STRUCT_NEW_DEFAULT:
  12709. {
  12710. pb_read_leb_uint32(p, p_end, type_idx);
  12711. #if WASM_ENABLE_FAST_INTERP != 0
  12712. emit_uint32(loader_ctx, type_idx);
  12713. #endif
  12714. if (!check_type_index(module, module->type_count,
  12715. type_idx, error_buf,
  12716. error_buf_size)) {
  12717. goto fail;
  12718. }
  12719. if (module->types[type_idx]->type_flag
  12720. != WASM_TYPE_STRUCT) {
  12721. set_error_buf(error_buf, error_buf_size,
  12722. "unknown struct type");
  12723. goto fail;
  12724. }
  12725. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12726. int32 j, k;
  12727. uint8 value_type;
  12728. uint32 ref_type_struct_size;
  12729. WASMStructType *struct_type =
  12730. (WASMStructType *)module->types[type_idx];
  12731. k = struct_type->ref_type_map_count - 1;
  12732. for (j = struct_type->field_count - 1; j >= 0;
  12733. j--) {
  12734. value_type = struct_type->fields[j].field_type;
  12735. if (wasm_is_type_reftype(value_type)) {
  12736. if (wasm_is_type_multi_byte_type(
  12737. value_type)) {
  12738. ref_type_struct_size =
  12739. wasm_reftype_struct_size(
  12740. struct_type->ref_type_maps[k]
  12741. .ref_type);
  12742. bh_memcpy_s(
  12743. &wasm_ref_type,
  12744. (uint32)sizeof(WASMRefType),
  12745. struct_type->ref_type_maps[k]
  12746. .ref_type,
  12747. ref_type_struct_size);
  12748. k--;
  12749. }
  12750. POP_REF(value_type);
  12751. }
  12752. else {
  12753. switch (value_type) {
  12754. case VALUE_TYPE_I32:
  12755. case PACKED_TYPE_I8:
  12756. case PACKED_TYPE_I16:
  12757. POP_I32();
  12758. break;
  12759. case VALUE_TYPE_I64:
  12760. POP_I64();
  12761. break;
  12762. case VALUE_TYPE_F32:
  12763. POP_F32();
  12764. break;
  12765. case VALUE_TYPE_F64:
  12766. POP_F64();
  12767. break;
  12768. default:
  12769. set_error_buf(error_buf,
  12770. error_buf_size,
  12771. "unknown type");
  12772. goto fail;
  12773. }
  12774. }
  12775. }
  12776. }
  12777. /* PUSH struct obj, (ref $t) */
  12778. wasm_set_refheaptype_typeidx(
  12779. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12780. PUSH_REF(wasm_ref_type.ref_type);
  12781. break;
  12782. }
  12783. case WASM_OP_STRUCT_GET:
  12784. case WASM_OP_STRUCT_GET_S:
  12785. case WASM_OP_STRUCT_GET_U:
  12786. case WASM_OP_STRUCT_SET:
  12787. {
  12788. WASMStructType *struct_type;
  12789. WASMRefType *ref_type = NULL;
  12790. uint32 field_idx;
  12791. uint8 field_type;
  12792. pb_read_leb_uint32(p, p_end, type_idx);
  12793. #if WASM_ENABLE_FAST_INTERP != 0
  12794. emit_uint32(loader_ctx, type_idx);
  12795. #endif
  12796. if (!check_type_index(module, module->type_count,
  12797. type_idx, error_buf,
  12798. error_buf_size)) {
  12799. goto fail;
  12800. }
  12801. if (module->types[type_idx]->type_flag
  12802. != WASM_TYPE_STRUCT) {
  12803. set_error_buf(error_buf, error_buf_size,
  12804. "unknown struct type");
  12805. goto fail;
  12806. }
  12807. struct_type = (WASMStructType *)module->types[type_idx];
  12808. pb_read_leb_uint32(p, p_end, field_idx);
  12809. #if WASM_ENABLE_FAST_INTERP != 0
  12810. emit_uint32(loader_ctx, field_idx);
  12811. #endif
  12812. if (field_idx >= struct_type->field_count) {
  12813. set_error_buf(error_buf, error_buf_size,
  12814. "unknown struct field");
  12815. goto fail;
  12816. }
  12817. if (opcode1 == WASM_OP_STRUCT_SET
  12818. && !(struct_type->fields[field_idx].field_flags
  12819. & 1)) {
  12820. set_error_buf(error_buf, error_buf_size,
  12821. "field is immutable");
  12822. goto fail;
  12823. }
  12824. field_type = struct_type->fields[field_idx].field_type;
  12825. if (is_packed_type(field_type)) {
  12826. if (opcode1 == WASM_OP_STRUCT_GET) {
  12827. set_error_buf(error_buf, error_buf_size,
  12828. "type mismatch");
  12829. goto fail;
  12830. }
  12831. else {
  12832. field_type = VALUE_TYPE_I32;
  12833. }
  12834. }
  12835. if (wasm_is_type_multi_byte_type(field_type)) {
  12836. ref_type = wasm_reftype_map_find(
  12837. struct_type->ref_type_maps,
  12838. struct_type->ref_type_map_count, field_idx);
  12839. bh_assert(ref_type);
  12840. }
  12841. if (opcode1 == WASM_OP_STRUCT_SET) {
  12842. /* POP field */
  12843. if (wasm_is_type_multi_byte_type(field_type)) {
  12844. bh_memcpy_s(&wasm_ref_type,
  12845. (uint32)sizeof(WASMRefType),
  12846. ref_type,
  12847. wasm_reftype_struct_size(ref_type));
  12848. }
  12849. POP_REF(field_type);
  12850. /* POP struct obj, (ref null $t) */
  12851. wasm_set_refheaptype_typeidx(
  12852. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12853. POP_REF(wasm_ref_type.ref_type);
  12854. }
  12855. else {
  12856. /* POP struct obj, (ref null $t) */
  12857. wasm_set_refheaptype_typeidx(
  12858. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12859. POP_REF(wasm_ref_type.ref_type);
  12860. /* PUSH field */
  12861. if (wasm_is_type_multi_byte_type(field_type)) {
  12862. bh_memcpy_s(&wasm_ref_type,
  12863. (uint32)sizeof(WASMRefType),
  12864. ref_type,
  12865. wasm_reftype_struct_size(ref_type));
  12866. }
  12867. PUSH_REF(field_type);
  12868. }
  12869. break;
  12870. }
  12871. case WASM_OP_ARRAY_NEW:
  12872. case WASM_OP_ARRAY_NEW_DEFAULT:
  12873. case WASM_OP_ARRAY_NEW_FIXED:
  12874. case WASM_OP_ARRAY_NEW_DATA:
  12875. case WASM_OP_ARRAY_NEW_ELEM:
  12876. {
  12877. WASMArrayType *array_type;
  12878. uint8 elem_type;
  12879. uint32 u32 = 0;
  12880. pb_read_leb_uint32(p, p_end, type_idx);
  12881. #if WASM_ENABLE_FAST_INTERP != 0
  12882. emit_uint32(loader_ctx, type_idx);
  12883. #endif
  12884. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12885. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12886. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12887. pb_read_leb_uint32(p, p_end, u32);
  12888. #if WASM_ENABLE_FAST_INTERP != 0
  12889. emit_uint32(loader_ctx, u32);
  12890. #endif
  12891. }
  12892. if (!check_array_type(module, type_idx, error_buf,
  12893. error_buf_size)) {
  12894. goto fail;
  12895. }
  12896. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12897. /* length */
  12898. POP_I32();
  12899. }
  12900. array_type = (WASMArrayType *)module->types[type_idx];
  12901. elem_type = array_type->elem_type;
  12902. if (opcode1 == WASM_OP_ARRAY_NEW
  12903. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12904. if (wasm_is_type_multi_byte_type(elem_type)) {
  12905. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12906. array_type->elem_ref_type,
  12907. wasm_reftype_struct_size(
  12908. array_type->elem_ref_type));
  12909. }
  12910. if (is_packed_type(elem_type)) {
  12911. elem_type = VALUE_TYPE_I32;
  12912. }
  12913. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12914. uint32 N = u32;
  12915. for (i = 0; i < N; i++) {
  12916. if (wasm_is_type_multi_byte_type(
  12917. elem_type)) {
  12918. bh_memcpy_s(
  12919. &wasm_ref_type, sizeof(WASMRefType),
  12920. array_type->elem_ref_type,
  12921. wasm_reftype_struct_size(
  12922. array_type->elem_ref_type));
  12923. }
  12924. POP_REF(elem_type);
  12925. }
  12926. }
  12927. else
  12928. POP_REF(elem_type);
  12929. }
  12930. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12931. /* offset of data segment */
  12932. POP_I32();
  12933. if (u32 >= module->data_seg_count) {
  12934. set_error_buf(error_buf, error_buf_size,
  12935. "unknown data segment");
  12936. goto fail;
  12937. }
  12938. if (wasm_is_type_reftype(elem_type)) {
  12939. set_error_buf(error_buf, error_buf_size,
  12940. "array elem type mismatch");
  12941. goto fail;
  12942. }
  12943. }
  12944. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12945. WASMTableSeg *table_seg =
  12946. module->table_segments + u32;
  12947. /* offset of element segment */
  12948. POP_I32();
  12949. if (u32 >= module->table_seg_count) {
  12950. set_error_buf(error_buf, error_buf_size,
  12951. "unknown element segment");
  12952. goto fail;
  12953. }
  12954. if (!wasm_reftype_is_subtype_of(
  12955. table_seg->elem_type,
  12956. table_seg->elem_ref_type, elem_type,
  12957. array_type->elem_ref_type, module->types,
  12958. module->type_count)) {
  12959. set_error_buf(error_buf, error_buf_size,
  12960. "array elem type mismatch");
  12961. goto fail;
  12962. }
  12963. }
  12964. /* PUSH array obj, (ref $t) */
  12965. wasm_set_refheaptype_typeidx(
  12966. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12967. PUSH_REF(wasm_ref_type.ref_type);
  12968. break;
  12969. }
  12970. case WASM_OP_ARRAY_GET:
  12971. case WASM_OP_ARRAY_GET_S:
  12972. case WASM_OP_ARRAY_GET_U:
  12973. case WASM_OP_ARRAY_SET:
  12974. {
  12975. uint8 elem_type;
  12976. WASMArrayType *array_type;
  12977. WASMRefType *ref_type = NULL;
  12978. pb_read_leb_uint32(p, p_end, type_idx);
  12979. #if WASM_ENABLE_FAST_INTERP != 0
  12980. emit_uint32(loader_ctx, type_idx);
  12981. #endif
  12982. if (!check_array_type(module, type_idx, error_buf,
  12983. error_buf_size)) {
  12984. goto fail;
  12985. }
  12986. array_type = (WASMArrayType *)module->types[type_idx];
  12987. if (opcode1 == WASM_OP_ARRAY_SET
  12988. && !(array_type->elem_flags & 1)) {
  12989. set_error_buf(error_buf, error_buf_size,
  12990. "array is immutable");
  12991. goto fail;
  12992. }
  12993. elem_type = array_type->elem_type;
  12994. if (is_packed_type(elem_type)) {
  12995. if (opcode1 != WASM_OP_ARRAY_GET_S
  12996. && opcode1 != WASM_OP_ARRAY_GET_U
  12997. && opcode1 != WASM_OP_ARRAY_SET) {
  12998. set_error_buf(error_buf, error_buf_size,
  12999. "type mismatch");
  13000. goto fail;
  13001. }
  13002. else {
  13003. elem_type = VALUE_TYPE_I32;
  13004. }
  13005. }
  13006. ref_type = array_type->elem_ref_type;
  13007. if (opcode1 == WASM_OP_ARRAY_SET) {
  13008. /* POP elem to set */
  13009. if (wasm_is_type_multi_byte_type(elem_type)) {
  13010. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13011. ref_type,
  13012. wasm_reftype_struct_size(ref_type));
  13013. }
  13014. POP_REF(elem_type);
  13015. }
  13016. /* elem idx */
  13017. POP_I32();
  13018. /* POP array obj, (ref null $t) */
  13019. wasm_set_refheaptype_typeidx(
  13020. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13021. POP_REF(wasm_ref_type.ref_type);
  13022. if (opcode1 != WASM_OP_ARRAY_SET) {
  13023. /* PUSH elem */
  13024. if (wasm_is_type_multi_byte_type(elem_type)) {
  13025. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13026. ref_type,
  13027. wasm_reftype_struct_size(ref_type));
  13028. }
  13029. PUSH_REF(elem_type);
  13030. }
  13031. break;
  13032. }
  13033. case WASM_OP_ARRAY_LEN:
  13034. {
  13035. POP_REF(REF_TYPE_ARRAYREF);
  13036. /* length */
  13037. PUSH_I32();
  13038. break;
  13039. }
  13040. case WASM_OP_ARRAY_FILL:
  13041. {
  13042. WASMArrayType *array_type;
  13043. uint8 elem_type;
  13044. /* typeidx */
  13045. pb_read_leb_uint32(p, p_end, type_idx);
  13046. #if WASM_ENABLE_FAST_INTERP != 0
  13047. emit_uint32(loader_ctx, type_idx);
  13048. #endif
  13049. if (!check_array_type(module, type_idx, error_buf,
  13050. error_buf_size)) {
  13051. goto fail;
  13052. }
  13053. array_type = (WASMArrayType *)module->types[type_idx];
  13054. if (!(array_type->elem_flags & 1)) {
  13055. set_error_buf(error_buf, error_buf_size,
  13056. "array is immutable");
  13057. goto fail;
  13058. }
  13059. elem_type = array_type->elem_type;
  13060. if (is_packed_type(elem_type)) {
  13061. elem_type = VALUE_TYPE_I32;
  13062. }
  13063. POP_I32(); /* length */
  13064. #if WASM_ENABLE_FAST_INTERP != 0
  13065. POP_OFFSET_TYPE(elem_type);
  13066. #endif
  13067. POP_TYPE(elem_type);
  13068. POP_I32(); /* start */
  13069. /* POP array obj, (ref null $t) */
  13070. wasm_set_refheaptype_typeidx(
  13071. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13072. POP_REF(wasm_ref_type.ref_type);
  13073. break;
  13074. }
  13075. case WASM_OP_ARRAY_COPY:
  13076. {
  13077. uint32 src_type_idx;
  13078. uint8 src_elem_type, dst_elem_type;
  13079. WASMRefType src_ref_type = { 0 },
  13080. *src_elem_ref_type = NULL;
  13081. WASMRefType dst_ref_type = { 0 },
  13082. *dst_elem_ref_type = NULL;
  13083. WASMArrayType *array_type;
  13084. /* typeidx1 */
  13085. pb_read_leb_uint32(p, p_end, type_idx);
  13086. #if WASM_ENABLE_FAST_INTERP != 0
  13087. emit_uint32(loader_ctx, type_idx);
  13088. #endif
  13089. /* typeidx2 */
  13090. pb_read_leb_uint32(p, p_end, src_type_idx);
  13091. #if WASM_ENABLE_FAST_INTERP != 0
  13092. emit_uint32(loader_ctx, src_type_idx);
  13093. #endif
  13094. if (!check_array_type(module, type_idx, error_buf,
  13095. error_buf_size)) {
  13096. goto fail;
  13097. }
  13098. if (!check_array_type(module, src_type_idx, error_buf,
  13099. error_buf_size)) {
  13100. goto fail;
  13101. }
  13102. POP_I32();
  13103. POP_I32();
  13104. /* POP array obj, (ref null $t) */
  13105. wasm_set_refheaptype_typeidx(
  13106. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  13107. POP_REF(wasm_ref_type.ref_type);
  13108. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  13109. &wasm_ref_type,
  13110. wasm_reftype_struct_size(&wasm_ref_type));
  13111. POP_I32();
  13112. /* POP array obj, (ref null $t) */
  13113. wasm_set_refheaptype_typeidx(
  13114. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  13115. POP_REF(wasm_ref_type.ref_type);
  13116. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  13117. &wasm_ref_type,
  13118. wasm_reftype_struct_size(&wasm_ref_type));
  13119. array_type = (WASMArrayType *)module->types[type_idx];
  13120. if (!(array_type->elem_flags & 1)) {
  13121. set_error_buf(error_buf, error_buf_size,
  13122. "destination array is immutable");
  13123. goto fail;
  13124. }
  13125. dst_elem_type = array_type->elem_type;
  13126. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  13127. dst_elem_ref_type = array_type->elem_ref_type;
  13128. }
  13129. array_type =
  13130. (WASMArrayType *)module->types[src_type_idx];
  13131. src_elem_type = array_type->elem_type;
  13132. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  13133. src_elem_ref_type = array_type->elem_ref_type;
  13134. }
  13135. if (!wasm_reftype_is_subtype_of(
  13136. src_elem_type, src_elem_ref_type, dst_elem_type,
  13137. dst_elem_ref_type, module->types,
  13138. module->type_count)) {
  13139. set_error_buf(error_buf, error_buf_size,
  13140. "array types do not match");
  13141. goto fail;
  13142. }
  13143. break;
  13144. }
  13145. case WASM_OP_REF_I31:
  13146. {
  13147. POP_I32();
  13148. wasm_set_refheaptype_common(
  13149. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  13150. PUSH_REF(wasm_ref_type.ref_type);
  13151. break;
  13152. }
  13153. case WASM_OP_I31_GET_S:
  13154. case WASM_OP_I31_GET_U:
  13155. {
  13156. POP_REF(REF_TYPE_I31REF);
  13157. PUSH_I32();
  13158. break;
  13159. }
  13160. case WASM_OP_REF_TEST:
  13161. case WASM_OP_REF_CAST:
  13162. case WASM_OP_REF_TEST_NULLABLE:
  13163. case WASM_OP_REF_CAST_NULLABLE:
  13164. {
  13165. uint8 type;
  13166. pb_read_leb_int32(p, p_end, heap_type);
  13167. #if WASM_ENABLE_FAST_INTERP != 0
  13168. emit_uint32(loader_ctx, (uint32)heap_type);
  13169. #endif
  13170. if (heap_type >= 0) {
  13171. if (!check_type_index(module, module->type_count,
  13172. heap_type, error_buf,
  13173. error_buf_size)) {
  13174. goto fail;
  13175. }
  13176. }
  13177. else {
  13178. if (!wasm_is_valid_heap_type(heap_type)) {
  13179. set_error_buf(error_buf, error_buf_size,
  13180. "unknown type");
  13181. goto fail;
  13182. }
  13183. }
  13184. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13185. &wasm_ref_type, error_buf,
  13186. error_buf_size)) {
  13187. goto fail;
  13188. }
  13189. if (opcode1 == WASM_OP_REF_TEST
  13190. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  13191. PUSH_I32();
  13192. else {
  13193. bool nullable =
  13194. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  13195. : false;
  13196. if (heap_type >= 0 || !nullable) {
  13197. wasm_set_refheaptype_typeidx(
  13198. &wasm_ref_type.ref_ht_typeidx, nullable,
  13199. heap_type);
  13200. PUSH_REF(wasm_ref_type.ref_type);
  13201. }
  13202. else {
  13203. PUSH_REF((uint8)((int32)0x80 + heap_type));
  13204. }
  13205. }
  13206. break;
  13207. }
  13208. case WASM_OP_BR_ON_CAST:
  13209. case WASM_OP_BR_ON_CAST_FAIL:
  13210. {
  13211. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  13212. ref_type2 = { 0 }, ref_type_diff = { 0 };
  13213. uint8 type_tmp, castflags;
  13214. uint32 depth;
  13215. int32 heap_type_dst;
  13216. bool src_nullable, dst_nullable;
  13217. CHECK_BUF(p, p_end, 1);
  13218. castflags = read_uint8(p);
  13219. #if WASM_ENABLE_FAST_INTERP != 0
  13220. /* Emit heap_type firstly */
  13221. emit_byte(loader_ctx, castflags);
  13222. #endif
  13223. p_org = p;
  13224. pb_read_leb_uint32(p, p_end, depth);
  13225. pb_read_leb_int32(p, p_end, heap_type);
  13226. #if WASM_ENABLE_FAST_INTERP != 0
  13227. /* Emit heap_type firstly */
  13228. emit_uint32(loader_ctx, (uint32)heap_type);
  13229. #endif
  13230. pb_read_leb_int32(p, p_end, heap_type_dst);
  13231. #if WASM_ENABLE_FAST_INTERP != 0
  13232. /* Emit heap_type firstly */
  13233. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  13234. #endif
  13235. (void)depth;
  13236. /*
  13237. * castflags should be 0~3:
  13238. * 0: (non-null, non-null)
  13239. * 1: (null, non-null)
  13240. * 2: (non-null, null)
  13241. * 3: (null, null)
  13242. */
  13243. if (castflags > 3) {
  13244. set_error_buf(error_buf, error_buf_size,
  13245. "invalid castflags");
  13246. break;
  13247. }
  13248. src_nullable =
  13249. (castflags == 1) || (castflags == 3) ? true : false;
  13250. dst_nullable =
  13251. (castflags == 2) || (castflags == 3) ? true : false;
  13252. /* Pop and backup the stack top's ref type */
  13253. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  13254. &ref_type_tmp, error_buf,
  13255. error_buf_size)) {
  13256. goto fail;
  13257. }
  13258. /* The reference type rt1 must be valid */
  13259. if (!init_ref_type(module, &ref_type1, src_nullable,
  13260. heap_type, error_buf,
  13261. error_buf_size)) {
  13262. goto fail;
  13263. }
  13264. /* The reference type rt2 must be valid. */
  13265. if (!init_ref_type(module, &ref_type2, dst_nullable,
  13266. heap_type_dst, error_buf,
  13267. error_buf_size)) {
  13268. goto fail;
  13269. }
  13270. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  13271. &ref_type2);
  13272. /* The reference type rt2 must match rt1. */
  13273. if (!wasm_reftype_is_subtype_of(
  13274. ref_type2.ref_type, &ref_type2,
  13275. ref_type1.ref_type, &ref_type1, module->types,
  13276. module->type_count)) {
  13277. set_error_buf(error_buf, error_buf_size,
  13278. "type mismatch");
  13279. goto fail;
  13280. }
  13281. p = p_org;
  13282. /* Push ref type casted for branch block check */
  13283. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13284. /* The reference type rt2 must match rt′. */
  13285. type_tmp = ref_type2.ref_type;
  13286. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13287. bh_memcpy_s(
  13288. &wasm_ref_type,
  13289. wasm_reftype_struct_size(&ref_type2),
  13290. &ref_type2,
  13291. wasm_reftype_struct_size(&ref_type2));
  13292. }
  13293. }
  13294. else {
  13295. /* The reference type rt′1 must match rt′. */
  13296. type_tmp = ref_type_diff.ref_type;
  13297. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13298. bh_memcpy_s(
  13299. &wasm_ref_type,
  13300. wasm_reftype_struct_size(&ref_type_diff),
  13301. &ref_type_diff,
  13302. wasm_reftype_struct_size(&ref_type_diff));
  13303. }
  13304. }
  13305. PUSH_REF(type_tmp);
  13306. if (!(frame_csp_tmp = check_branch_block(
  13307. loader_ctx, &p, p_end, opcode, error_buf,
  13308. error_buf_size))) {
  13309. goto fail;
  13310. }
  13311. /* Ignore heap_types */
  13312. skip_leb_uint32(p, p_end);
  13313. skip_leb_uint32(p, p_end);
  13314. /* Restore the original stack top's ref type */
  13315. POP_REF(type_tmp);
  13316. #if WASM_ENABLE_FAST_INTERP != 0
  13317. /* Erase the opnd offset emitted by POP_REF() */
  13318. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13319. #endif
  13320. if (opcode1 == WASM_OP_BR_ON_CAST) {
  13321. type_tmp = ref_type_diff.ref_type;
  13322. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13323. bh_memcpy_s(
  13324. &wasm_ref_type,
  13325. wasm_reftype_struct_size(&ref_type_diff),
  13326. &ref_type_diff,
  13327. wasm_reftype_struct_size(&ref_type_diff));
  13328. }
  13329. }
  13330. else {
  13331. type_tmp = ref_type2.ref_type;
  13332. if (wasm_is_type_multi_byte_type(type_tmp)) {
  13333. bh_memcpy_s(
  13334. &wasm_ref_type,
  13335. wasm_reftype_struct_size(&ref_type2),
  13336. &ref_type2,
  13337. wasm_reftype_struct_size(&ref_type2));
  13338. }
  13339. }
  13340. PUSH_REF(type_tmp);
  13341. #if WASM_ENABLE_FAST_INTERP != 0
  13342. /* Erase the opnd offset emitted by PUSH_REF() */
  13343. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  13344. #endif
  13345. break;
  13346. }
  13347. case WASM_OP_ANY_CONVERT_EXTERN:
  13348. {
  13349. uint8 type;
  13350. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13351. &wasm_ref_type, error_buf,
  13352. error_buf_size)) {
  13353. goto fail;
  13354. }
  13355. if (!(type == REF_TYPE_EXTERNREF
  13356. || (type == REF_TYPE_HT_NON_NULLABLE
  13357. && wasm_ref_type.ref_ht_common.heap_type
  13358. == HEAP_TYPE_EXTERN)
  13359. || type == VALUE_TYPE_ANY)) {
  13360. set_error_buf(error_buf, error_buf_size,
  13361. "type mismatch");
  13362. goto fail;
  13363. }
  13364. if (type == REF_TYPE_EXTERNREF)
  13365. type = REF_TYPE_ANYREF;
  13366. else {
  13367. wasm_ref_type.ref_ht_common.heap_type =
  13368. HEAP_TYPE_ANY;
  13369. }
  13370. PUSH_REF(type);
  13371. break;
  13372. }
  13373. case WASM_OP_EXTERN_CONVERT_ANY:
  13374. {
  13375. uint8 type;
  13376. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  13377. &wasm_ref_type, error_buf,
  13378. error_buf_size)) {
  13379. goto fail;
  13380. }
  13381. if (type == REF_TYPE_EXTERNREF
  13382. || ((type == REF_TYPE_HT_NULLABLE
  13383. || type == REF_TYPE_HT_NON_NULLABLE)
  13384. && wasm_ref_type.ref_ht_common.heap_type
  13385. == HEAP_TYPE_EXTERN)) {
  13386. set_error_buf(error_buf, error_buf_size,
  13387. "type mismatch");
  13388. goto fail;
  13389. }
  13390. if (type != REF_TYPE_HT_NON_NULLABLE) {
  13391. /* push (ref null extern) */
  13392. type = REF_TYPE_EXTERNREF;
  13393. }
  13394. else {
  13395. /* push (ref extern) */
  13396. type = REF_TYPE_HT_NON_NULLABLE;
  13397. wasm_set_refheaptype_common(
  13398. &wasm_ref_type.ref_ht_common, false,
  13399. HEAP_TYPE_EXTERN);
  13400. }
  13401. PUSH_REF(type);
  13402. break;
  13403. }
  13404. #if WASM_ENABLE_STRINGREF != 0
  13405. case WASM_OP_STRING_NEW_UTF8:
  13406. case WASM_OP_STRING_NEW_WTF16:
  13407. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  13408. case WASM_OP_STRING_NEW_WTF8:
  13409. {
  13410. uint32 memidx;
  13411. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13412. func->has_memory_operations = true;
  13413. #endif
  13414. pb_read_leb_uint32(p, p_end, memidx);
  13415. #if WASM_ENABLE_FAST_INTERP != 0
  13416. emit_uint32(loader_ctx, (uint32)memidx);
  13417. #endif
  13418. POP_I32();
  13419. POP_I32();
  13420. PUSH_REF(REF_TYPE_STRINGREF);
  13421. (void)memidx;
  13422. break;
  13423. }
  13424. case WASM_OP_STRING_CONST:
  13425. {
  13426. uint32 contents;
  13427. pb_read_leb_uint32(p, p_end, contents);
  13428. #if WASM_ENABLE_FAST_INTERP != 0
  13429. emit_uint32(loader_ctx, (uint32)contents);
  13430. #endif
  13431. PUSH_REF(REF_TYPE_STRINGREF);
  13432. (void)contents;
  13433. break;
  13434. }
  13435. case WASM_OP_STRING_MEASURE_UTF8:
  13436. case WASM_OP_STRING_MEASURE_WTF8:
  13437. case WASM_OP_STRING_MEASURE_WTF16:
  13438. {
  13439. POP_STRINGREF();
  13440. PUSH_I32();
  13441. break;
  13442. }
  13443. case WASM_OP_STRING_ENCODE_UTF8:
  13444. case WASM_OP_STRING_ENCODE_WTF16:
  13445. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  13446. case WASM_OP_STRING_ENCODE_WTF8:
  13447. {
  13448. uint32 memidx;
  13449. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13450. func->has_memory_operations = true;
  13451. #endif
  13452. pb_read_leb_uint32(p, p_end, memidx);
  13453. #if WASM_ENABLE_FAST_INTERP != 0
  13454. emit_uint32(loader_ctx, (uint32)memidx);
  13455. #endif
  13456. POP_I32();
  13457. POP_STRINGREF();
  13458. PUSH_I32();
  13459. (void)memidx;
  13460. break;
  13461. }
  13462. case WASM_OP_STRING_CONCAT:
  13463. {
  13464. POP_STRINGREF();
  13465. POP_STRINGREF();
  13466. PUSH_REF(REF_TYPE_STRINGREF);
  13467. break;
  13468. }
  13469. case WASM_OP_STRING_EQ:
  13470. {
  13471. POP_STRINGREF();
  13472. POP_STRINGREF();
  13473. PUSH_I32();
  13474. break;
  13475. }
  13476. case WASM_OP_STRING_IS_USV_SEQUENCE:
  13477. {
  13478. POP_STRINGREF();
  13479. PUSH_I32();
  13480. break;
  13481. }
  13482. case WASM_OP_STRING_AS_WTF8:
  13483. {
  13484. POP_STRINGREF();
  13485. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  13486. break;
  13487. }
  13488. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  13489. {
  13490. POP_I32();
  13491. POP_I32();
  13492. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13493. PUSH_I32();
  13494. break;
  13495. }
  13496. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  13497. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  13498. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  13499. {
  13500. uint32 memidx;
  13501. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13502. func->has_memory_operations = true;
  13503. #endif
  13504. pb_read_leb_uint32(p, p_end, memidx);
  13505. #if WASM_ENABLE_FAST_INTERP != 0
  13506. emit_uint32(loader_ctx, (uint32)memidx);
  13507. #endif
  13508. POP_I32();
  13509. POP_I32();
  13510. POP_I32();
  13511. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13512. PUSH_I32();
  13513. PUSH_I32();
  13514. (void)memidx;
  13515. break;
  13516. }
  13517. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  13518. {
  13519. POP_I32();
  13520. POP_I32();
  13521. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  13522. PUSH_REF(REF_TYPE_STRINGREF);
  13523. break;
  13524. }
  13525. case WASM_OP_STRING_AS_WTF16:
  13526. {
  13527. POP_STRINGREF();
  13528. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  13529. break;
  13530. }
  13531. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  13532. {
  13533. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13534. PUSH_I32();
  13535. break;
  13536. }
  13537. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  13538. {
  13539. POP_I32();
  13540. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13541. PUSH_I32();
  13542. break;
  13543. }
  13544. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  13545. {
  13546. uint32 memidx;
  13547. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13548. func->has_memory_operations = true;
  13549. #endif
  13550. pb_read_leb_uint32(p, p_end, memidx);
  13551. #if WASM_ENABLE_FAST_INTERP != 0
  13552. emit_uint32(loader_ctx, (uint32)memidx);
  13553. #endif
  13554. POP_I32();
  13555. POP_I32();
  13556. POP_I32();
  13557. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13558. PUSH_I32();
  13559. (void)memidx;
  13560. break;
  13561. }
  13562. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  13563. {
  13564. POP_I32();
  13565. POP_I32();
  13566. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  13567. PUSH_REF(REF_TYPE_STRINGREF);
  13568. break;
  13569. }
  13570. case WASM_OP_STRING_AS_ITER:
  13571. {
  13572. POP_STRINGREF();
  13573. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  13574. break;
  13575. }
  13576. case WASM_OP_STRINGVIEW_ITER_NEXT:
  13577. {
  13578. POP_REF(REF_TYPE_STRINGVIEWITER);
  13579. PUSH_I32();
  13580. break;
  13581. }
  13582. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  13583. case WASM_OP_STRINGVIEW_ITER_REWIND:
  13584. {
  13585. POP_I32();
  13586. POP_REF(REF_TYPE_STRINGVIEWITER);
  13587. PUSH_I32();
  13588. break;
  13589. }
  13590. case WASM_OP_STRINGVIEW_ITER_SLICE:
  13591. {
  13592. POP_I32();
  13593. POP_REF(REF_TYPE_STRINGVIEWITER);
  13594. PUSH_REF(REF_TYPE_STRINGREF);
  13595. break;
  13596. }
  13597. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  13598. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  13599. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  13600. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  13601. {
  13602. POP_I32();
  13603. POP_I32();
  13604. POP_REF(REF_TYPE_ARRAYREF);
  13605. PUSH_REF(REF_TYPE_STRINGREF);
  13606. break;
  13607. }
  13608. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  13609. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  13610. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  13611. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  13612. {
  13613. POP_I32();
  13614. POP_REF(REF_TYPE_ARRAYREF);
  13615. POP_STRINGREF();
  13616. PUSH_I32();
  13617. break;
  13618. }
  13619. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  13620. default:
  13621. set_error_buf_v(error_buf, error_buf_size,
  13622. "%s %02x %02x", "unsupported opcode",
  13623. 0xfb, opcode1);
  13624. goto fail;
  13625. }
  13626. break;
  13627. }
  13628. #endif /* end of WASM_ENABLE_GC != 0 */
  13629. case WASM_OP_MISC_PREFIX:
  13630. {
  13631. uint32 opcode1;
  13632. pb_read_leb_uint32(p, p_end, opcode1);
  13633. #if WASM_ENABLE_FAST_INTERP != 0
  13634. emit_byte(loader_ctx, ((uint8)opcode1));
  13635. #endif
  13636. switch (opcode1) {
  13637. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13638. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13639. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13640. break;
  13641. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13642. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13643. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13644. break;
  13645. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13646. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13647. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13648. break;
  13649. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13650. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13651. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13652. break;
  13653. #if WASM_ENABLE_BULK_MEMORY != 0
  13654. case WASM_OP_MEMORY_INIT:
  13655. {
  13656. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13657. #if WASM_ENABLE_FAST_INTERP != 0
  13658. emit_uint32(loader_ctx, data_seg_idx);
  13659. #endif
  13660. if (module->import_memory_count == 0
  13661. && module->memory_count == 0)
  13662. goto fail_unknown_memory;
  13663. pb_read_leb_uint32(p, p_end, memidx);
  13664. check_memidx(module, memidx);
  13665. if (data_seg_idx >= module->data_seg_count) {
  13666. set_error_buf_v(error_buf, error_buf_size,
  13667. "unknown data segment %d",
  13668. data_seg_idx);
  13669. goto fail;
  13670. }
  13671. if (module->data_seg_count1 == 0)
  13672. goto fail_data_cnt_sec_require;
  13673. POP_I32();
  13674. POP_I32();
  13675. POP_MEM_OFFSET();
  13676. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13677. func->has_memory_operations = true;
  13678. #endif
  13679. #if WASM_ENABLE_WAMR_COMPILER != 0
  13680. module->is_bulk_memory_used = true;
  13681. #endif
  13682. break;
  13683. }
  13684. case WASM_OP_DATA_DROP:
  13685. {
  13686. pb_read_leb_uint32(p, p_end, data_seg_idx);
  13687. #if WASM_ENABLE_FAST_INTERP != 0
  13688. emit_uint32(loader_ctx, data_seg_idx);
  13689. #endif
  13690. if (data_seg_idx >= module->data_seg_count) {
  13691. set_error_buf(error_buf, error_buf_size,
  13692. "unknown data segment");
  13693. goto fail;
  13694. }
  13695. if (module->data_seg_count1 == 0)
  13696. goto fail_data_cnt_sec_require;
  13697. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13698. func->has_memory_operations = true;
  13699. #endif
  13700. #if WASM_ENABLE_WAMR_COMPILER != 0
  13701. module->is_bulk_memory_used = true;
  13702. #endif
  13703. break;
  13704. }
  13705. case WASM_OP_MEMORY_COPY:
  13706. {
  13707. CHECK_BUF(p, p_end, sizeof(int16));
  13708. /* check both src and dst memory index */
  13709. pb_read_leb_uint32(p, p_end, memidx);
  13710. check_memidx(module, memidx);
  13711. pb_read_leb_uint32(p, p_end, memidx);
  13712. check_memidx(module, memidx);
  13713. if (module->import_memory_count == 0
  13714. && module->memory_count == 0)
  13715. goto fail_unknown_memory;
  13716. POP_MEM_OFFSET();
  13717. POP_MEM_OFFSET();
  13718. POP_MEM_OFFSET();
  13719. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13720. func->has_memory_operations = true;
  13721. #endif
  13722. #if WASM_ENABLE_WAMR_COMPILER != 0
  13723. module->is_bulk_memory_used = true;
  13724. #endif
  13725. break;
  13726. }
  13727. case WASM_OP_MEMORY_FILL:
  13728. {
  13729. pb_read_leb_uint32(p, p_end, memidx);
  13730. check_memidx(module, memidx);
  13731. if (module->import_memory_count == 0
  13732. && module->memory_count == 0) {
  13733. goto fail_unknown_memory;
  13734. }
  13735. POP_MEM_OFFSET();
  13736. POP_I32();
  13737. POP_MEM_OFFSET();
  13738. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13739. func->has_memory_operations = true;
  13740. #endif
  13741. #if WASM_ENABLE_WAMR_COMPILER != 0
  13742. module->is_bulk_memory_used = true;
  13743. #endif
  13744. break;
  13745. }
  13746. fail_unknown_memory:
  13747. set_error_buf(error_buf, error_buf_size,
  13748. "unknown memory 0");
  13749. goto fail;
  13750. fail_data_cnt_sec_require:
  13751. set_error_buf(error_buf, error_buf_size,
  13752. "data count section required");
  13753. goto fail;
  13754. #endif /* WASM_ENABLE_BULK_MEMORY */
  13755. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13756. case WASM_OP_TABLE_INIT:
  13757. {
  13758. uint8 seg_type = 0, tbl_type = 0;
  13759. #if WASM_ENABLE_GC != 0
  13760. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13761. #endif
  13762. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13763. pb_read_leb_uint32(p, p_end, table_idx);
  13764. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13765. #if WASM_ENABLE_GC != 0
  13766. (void **)&tbl_ref_type,
  13767. #else
  13768. NULL,
  13769. #endif
  13770. error_buf, error_buf_size))
  13771. goto fail;
  13772. if (!get_table_seg_elem_type(module, table_seg_idx,
  13773. &seg_type,
  13774. #if WASM_ENABLE_GC != 0
  13775. (void **)&seg_ref_type,
  13776. #else
  13777. NULL,
  13778. #endif
  13779. error_buf, error_buf_size))
  13780. goto fail;
  13781. #if WASM_ENABLE_GC == 0
  13782. if (seg_type != tbl_type) {
  13783. set_error_buf(error_buf, error_buf_size,
  13784. "type mismatch");
  13785. goto fail;
  13786. }
  13787. #else
  13788. if (!wasm_reftype_is_subtype_of(
  13789. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13790. module->types, module->type_count)) {
  13791. set_error_buf(error_buf, error_buf_size,
  13792. "type mismatch");
  13793. goto fail;
  13794. }
  13795. #endif
  13796. #if WASM_ENABLE_FAST_INTERP != 0
  13797. emit_uint32(loader_ctx, table_seg_idx);
  13798. emit_uint32(loader_ctx, table_idx);
  13799. #endif
  13800. POP_I32();
  13801. POP_I32();
  13802. #if WASM_ENABLE_MEMORY64 != 0
  13803. table_elem_idx_type = is_table_64bit(module, table_idx)
  13804. ? VALUE_TYPE_I64
  13805. : VALUE_TYPE_I32;
  13806. #endif
  13807. POP_TBL_ELEM_IDX();
  13808. #if WASM_ENABLE_WAMR_COMPILER != 0
  13809. module->is_ref_types_used = true;
  13810. #endif
  13811. break;
  13812. }
  13813. case WASM_OP_ELEM_DROP:
  13814. {
  13815. pb_read_leb_uint32(p, p_end, table_seg_idx);
  13816. if (!get_table_seg_elem_type(module, table_seg_idx,
  13817. NULL, NULL, error_buf,
  13818. error_buf_size))
  13819. goto fail;
  13820. #if WASM_ENABLE_FAST_INTERP != 0
  13821. emit_uint32(loader_ctx, table_seg_idx);
  13822. #endif
  13823. #if WASM_ENABLE_WAMR_COMPILER != 0
  13824. module->is_ref_types_used = true;
  13825. #endif
  13826. break;
  13827. }
  13828. case WASM_OP_TABLE_COPY:
  13829. {
  13830. uint8 src_type, dst_type, src_tbl_idx_type,
  13831. dst_tbl_idx_type, min_tbl_idx_type;
  13832. #if WASM_ENABLE_GC != 0
  13833. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13834. #endif
  13835. uint32 src_tbl_idx, dst_tbl_idx;
  13836. pb_read_leb_uint32(p, p_end, dst_tbl_idx);
  13837. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13838. #if WASM_ENABLE_GC != 0
  13839. (void **)&dst_ref_type,
  13840. #else
  13841. NULL,
  13842. #endif
  13843. error_buf, error_buf_size))
  13844. goto fail;
  13845. pb_read_leb_uint32(p, p_end, src_tbl_idx);
  13846. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13847. #if WASM_ENABLE_GC != 0
  13848. (void **)&src_ref_type,
  13849. #else
  13850. NULL,
  13851. #endif
  13852. error_buf, error_buf_size))
  13853. goto fail;
  13854. #if WASM_ENABLE_GC == 0
  13855. if (src_type != dst_type) {
  13856. set_error_buf(error_buf, error_buf_size,
  13857. "type mismatch");
  13858. goto fail;
  13859. }
  13860. #else
  13861. if (!wasm_reftype_is_subtype_of(
  13862. src_type, src_ref_type, dst_type, dst_ref_type,
  13863. module->types, module->type_count)) {
  13864. set_error_buf(error_buf, error_buf_size,
  13865. "type mismatch");
  13866. goto fail;
  13867. }
  13868. #endif
  13869. #if WASM_ENABLE_FAST_INTERP != 0
  13870. emit_uint32(loader_ctx, dst_tbl_idx);
  13871. emit_uint32(loader_ctx, src_tbl_idx);
  13872. #endif
  13873. #if WASM_ENABLE_MEMORY64 != 0
  13874. src_tbl_idx_type = is_table_64bit(module, src_tbl_idx)
  13875. ? VALUE_TYPE_I64
  13876. : VALUE_TYPE_I32;
  13877. dst_tbl_idx_type = is_table_64bit(module, dst_tbl_idx)
  13878. ? VALUE_TYPE_I64
  13879. : VALUE_TYPE_I32;
  13880. min_tbl_idx_type =
  13881. (src_tbl_idx_type == VALUE_TYPE_I32
  13882. || dst_tbl_idx_type == VALUE_TYPE_I32)
  13883. ? VALUE_TYPE_I32
  13884. : VALUE_TYPE_I64;
  13885. #else
  13886. src_tbl_idx_type = VALUE_TYPE_I32;
  13887. dst_tbl_idx_type = VALUE_TYPE_I32;
  13888. min_tbl_idx_type = VALUE_TYPE_I32;
  13889. #endif
  13890. table_elem_idx_type = min_tbl_idx_type;
  13891. POP_TBL_ELEM_IDX();
  13892. table_elem_idx_type = src_tbl_idx_type;
  13893. POP_TBL_ELEM_IDX();
  13894. table_elem_idx_type = dst_tbl_idx_type;
  13895. POP_TBL_ELEM_IDX();
  13896. #if WASM_ENABLE_WAMR_COMPILER != 0
  13897. module->is_ref_types_used = true;
  13898. #endif
  13899. break;
  13900. }
  13901. case WASM_OP_TABLE_SIZE:
  13902. {
  13903. pb_read_leb_uint32(p, p_end, table_idx);
  13904. /* TODO: shall we create a new function to check
  13905. table idx instead of using below function? */
  13906. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13907. error_buf, error_buf_size))
  13908. goto fail;
  13909. #if WASM_ENABLE_FAST_INTERP != 0
  13910. emit_uint32(loader_ctx, table_idx);
  13911. #endif
  13912. #if WASM_ENABLE_MEMORY64 != 0
  13913. table_elem_idx_type = is_table_64bit(module, table_idx)
  13914. ? VALUE_TYPE_I64
  13915. : VALUE_TYPE_I32;
  13916. #endif
  13917. PUSH_TBL_ELEM_IDX();
  13918. #if WASM_ENABLE_WAMR_COMPILER != 0
  13919. module->is_ref_types_used = true;
  13920. #endif
  13921. break;
  13922. }
  13923. case WASM_OP_TABLE_GROW:
  13924. case WASM_OP_TABLE_FILL:
  13925. {
  13926. uint8 decl_type;
  13927. #if WASM_ENABLE_GC != 0
  13928. WASMRefType *ref_type = NULL;
  13929. #endif
  13930. pb_read_leb_uint32(p, p_end, table_idx);
  13931. if (!get_table_elem_type(module, table_idx, &decl_type,
  13932. #if WASM_ENABLE_GC != 0
  13933. (void **)&ref_type,
  13934. #else
  13935. NULL,
  13936. #endif
  13937. error_buf, error_buf_size))
  13938. goto fail;
  13939. #if WASM_ENABLE_GC != 0
  13940. if (wasm_is_type_multi_byte_type(decl_type)) {
  13941. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13942. ref_type,
  13943. wasm_reftype_struct_size(ref_type));
  13944. }
  13945. #endif
  13946. if (opcode1 == WASM_OP_TABLE_GROW) {
  13947. if (table_idx < module->import_table_count) {
  13948. module->import_tables[table_idx]
  13949. .u.table.table_type.possible_grow = true;
  13950. }
  13951. else {
  13952. module
  13953. ->tables[table_idx
  13954. - module->import_table_count]
  13955. .table_type.possible_grow = true;
  13956. }
  13957. }
  13958. #if WASM_ENABLE_FAST_INTERP != 0
  13959. emit_uint32(loader_ctx, table_idx);
  13960. #endif
  13961. #if WASM_ENABLE_MEMORY64 != 0
  13962. table_elem_idx_type = is_table_64bit(module, table_idx)
  13963. ? VALUE_TYPE_I64
  13964. : VALUE_TYPE_I32;
  13965. #endif
  13966. POP_TBL_ELEM_IDX();
  13967. #if WASM_ENABLE_FAST_INTERP != 0
  13968. POP_OFFSET_TYPE(decl_type);
  13969. #endif
  13970. POP_TYPE(decl_type);
  13971. if (opcode1 == WASM_OP_TABLE_GROW)
  13972. PUSH_TBL_ELEM_IDX();
  13973. else
  13974. POP_TBL_ELEM_IDX();
  13975. #if WASM_ENABLE_WAMR_COMPILER != 0
  13976. module->is_ref_types_used = true;
  13977. #endif
  13978. break;
  13979. }
  13980. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13981. default:
  13982. set_error_buf_v(error_buf, error_buf_size,
  13983. "%s %02x %02x", "unsupported opcode",
  13984. 0xfc, opcode1);
  13985. goto fail;
  13986. }
  13987. break;
  13988. }
  13989. #if WASM_ENABLE_SIMD != 0
  13990. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) \
  13991. || (WASM_ENABLE_FAST_INTERP != 0)
  13992. case WASM_OP_SIMD_PREFIX:
  13993. {
  13994. uint32 opcode1;
  13995. #if WASM_ENABLE_WAMR_COMPILER != 0
  13996. /* Mark the SIMD instruction is used in this module */
  13997. module->is_simd_used = true;
  13998. #endif
  13999. pb_read_leb_uint32(p, p_end, opcode1);
  14000. #if WASM_ENABLE_FAST_INTERP != 0
  14001. emit_byte(loader_ctx, opcode1);
  14002. #endif
  14003. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  14004. */
  14005. switch (opcode1) {
  14006. /* memory instruction */
  14007. case SIMD_v128_load:
  14008. case SIMD_v128_load8x8_s:
  14009. case SIMD_v128_load8x8_u:
  14010. case SIMD_v128_load16x4_s:
  14011. case SIMD_v128_load16x4_u:
  14012. case SIMD_v128_load32x2_s:
  14013. case SIMD_v128_load32x2_u:
  14014. case SIMD_v128_load8_splat:
  14015. case SIMD_v128_load16_splat:
  14016. case SIMD_v128_load32_splat:
  14017. case SIMD_v128_load64_splat:
  14018. {
  14019. CHECK_MEMORY();
  14020. pb_read_leb_uint32(p, p_end, align); /* align */
  14021. if (!check_simd_memory_access_align(
  14022. opcode1, align, error_buf, error_buf_size)) {
  14023. goto fail;
  14024. }
  14025. pb_read_leb_mem_offset(p, p_end,
  14026. mem_offset); /* offset */
  14027. #if WASM_ENABLE_FAST_INTERP != 0
  14028. emit_uint32(loader_ctx, mem_offset);
  14029. #endif
  14030. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14031. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14032. func->has_memory_operations = true;
  14033. #endif
  14034. break;
  14035. }
  14036. case SIMD_v128_store:
  14037. {
  14038. CHECK_MEMORY();
  14039. pb_read_leb_uint32(p, p_end, align); /* align */
  14040. if (!check_simd_memory_access_align(
  14041. opcode1, align, error_buf, error_buf_size)) {
  14042. goto fail;
  14043. }
  14044. pb_read_leb_mem_offset(p, p_end,
  14045. mem_offset); /* offset */
  14046. #if WASM_ENABLE_FAST_INTERP != 0
  14047. emit_uint32(loader_ctx, mem_offset);
  14048. #endif
  14049. POP_V128();
  14050. POP_MEM_OFFSET();
  14051. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14052. func->has_memory_operations = true;
  14053. #endif
  14054. break;
  14055. }
  14056. /* basic operation */
  14057. case SIMD_v128_const:
  14058. {
  14059. uint64 high, low;
  14060. CHECK_BUF1(p, p_end, 16);
  14061. #if WASM_ENABLE_FAST_INTERP != 0
  14062. wasm_runtime_read_v128(p, &high, &low);
  14063. emit_uint64(loader_ctx, high);
  14064. emit_uint64(loader_ctx, low);
  14065. #endif
  14066. p += 16;
  14067. PUSH_V128();
  14068. break;
  14069. }
  14070. case SIMD_v8x16_shuffle:
  14071. {
  14072. V128 mask;
  14073. CHECK_BUF1(p, p_end, 16);
  14074. mask = read_i8x16(p, error_buf, error_buf_size);
  14075. if (!check_simd_shuffle_mask(mask, error_buf,
  14076. error_buf_size)) {
  14077. goto fail;
  14078. }
  14079. #if WASM_ENABLE_FAST_INTERP != 0
  14080. uint64 high, low;
  14081. wasm_runtime_read_v128(p, &high, &low);
  14082. emit_uint64(loader_ctx, high);
  14083. emit_uint64(loader_ctx, low);
  14084. #endif
  14085. p += 16;
  14086. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14087. break;
  14088. }
  14089. case SIMD_v8x16_swizzle:
  14090. {
  14091. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14092. break;
  14093. }
  14094. /* splat operation */
  14095. case SIMD_i8x16_splat:
  14096. case SIMD_i16x8_splat:
  14097. case SIMD_i32x4_splat:
  14098. case SIMD_i64x2_splat:
  14099. case SIMD_f32x4_splat:
  14100. case SIMD_f64x2_splat:
  14101. {
  14102. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  14103. VALUE_TYPE_I32, VALUE_TYPE_I64,
  14104. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  14105. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  14106. VALUE_TYPE_V128);
  14107. break;
  14108. }
  14109. /* lane operation */
  14110. case SIMD_i8x16_extract_lane_s:
  14111. case SIMD_i8x16_extract_lane_u:
  14112. case SIMD_i8x16_replace_lane:
  14113. case SIMD_i16x8_extract_lane_s:
  14114. case SIMD_i16x8_extract_lane_u:
  14115. case SIMD_i16x8_replace_lane:
  14116. case SIMD_i32x4_extract_lane:
  14117. case SIMD_i32x4_replace_lane:
  14118. case SIMD_i64x2_extract_lane:
  14119. case SIMD_i64x2_replace_lane:
  14120. case SIMD_f32x4_extract_lane:
  14121. case SIMD_f32x4_replace_lane:
  14122. case SIMD_f64x2_extract_lane:
  14123. case SIMD_f64x2_replace_lane:
  14124. {
  14125. uint8 lane;
  14126. /* clang-format off */
  14127. uint8 replace[] = {
  14128. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  14129. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  14130. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  14131. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  14132. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  14133. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  14134. };
  14135. uint8 push_type[] = {
  14136. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14137. VALUE_TYPE_V128,
  14138. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  14139. VALUE_TYPE_V128,
  14140. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  14141. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  14142. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  14143. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  14144. };
  14145. /* clang-format on */
  14146. CHECK_BUF(p, p_end, 1);
  14147. lane = read_uint8(p);
  14148. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14149. error_buf_size)) {
  14150. goto fail;
  14151. }
  14152. #if WASM_ENABLE_FAST_INTERP != 0
  14153. emit_byte(loader_ctx, lane);
  14154. #endif
  14155. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  14156. #if WASM_ENABLE_FAST_INTERP != 0
  14157. if (!(wasm_loader_pop_frame_ref_offset(
  14158. loader_ctx,
  14159. replace[opcode1
  14160. - SIMD_i8x16_extract_lane_s],
  14161. error_buf, error_buf_size)))
  14162. goto fail;
  14163. #else
  14164. if (!(wasm_loader_pop_frame_ref(
  14165. loader_ctx,
  14166. replace[opcode1
  14167. - SIMD_i8x16_extract_lane_s],
  14168. error_buf, error_buf_size)))
  14169. goto fail;
  14170. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  14171. }
  14172. POP_AND_PUSH(
  14173. VALUE_TYPE_V128,
  14174. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  14175. break;
  14176. }
  14177. /* i8x16 compare operation */
  14178. case SIMD_i8x16_eq:
  14179. case SIMD_i8x16_ne:
  14180. case SIMD_i8x16_lt_s:
  14181. case SIMD_i8x16_lt_u:
  14182. case SIMD_i8x16_gt_s:
  14183. case SIMD_i8x16_gt_u:
  14184. case SIMD_i8x16_le_s:
  14185. case SIMD_i8x16_le_u:
  14186. case SIMD_i8x16_ge_s:
  14187. case SIMD_i8x16_ge_u:
  14188. /* i16x8 compare operation */
  14189. case SIMD_i16x8_eq:
  14190. case SIMD_i16x8_ne:
  14191. case SIMD_i16x8_lt_s:
  14192. case SIMD_i16x8_lt_u:
  14193. case SIMD_i16x8_gt_s:
  14194. case SIMD_i16x8_gt_u:
  14195. case SIMD_i16x8_le_s:
  14196. case SIMD_i16x8_le_u:
  14197. case SIMD_i16x8_ge_s:
  14198. case SIMD_i16x8_ge_u:
  14199. /* i32x4 compare operation */
  14200. case SIMD_i32x4_eq:
  14201. case SIMD_i32x4_ne:
  14202. case SIMD_i32x4_lt_s:
  14203. case SIMD_i32x4_lt_u:
  14204. case SIMD_i32x4_gt_s:
  14205. case SIMD_i32x4_gt_u:
  14206. case SIMD_i32x4_le_s:
  14207. case SIMD_i32x4_le_u:
  14208. case SIMD_i32x4_ge_s:
  14209. case SIMD_i32x4_ge_u:
  14210. /* f32x4 compare operation */
  14211. case SIMD_f32x4_eq:
  14212. case SIMD_f32x4_ne:
  14213. case SIMD_f32x4_lt:
  14214. case SIMD_f32x4_gt:
  14215. case SIMD_f32x4_le:
  14216. case SIMD_f32x4_ge:
  14217. /* f64x2 compare operation */
  14218. case SIMD_f64x2_eq:
  14219. case SIMD_f64x2_ne:
  14220. case SIMD_f64x2_lt:
  14221. case SIMD_f64x2_gt:
  14222. case SIMD_f64x2_le:
  14223. case SIMD_f64x2_ge:
  14224. {
  14225. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14226. break;
  14227. }
  14228. /* v128 operation */
  14229. case SIMD_v128_not:
  14230. {
  14231. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14232. break;
  14233. }
  14234. case SIMD_v128_and:
  14235. case SIMD_v128_andnot:
  14236. case SIMD_v128_or:
  14237. case SIMD_v128_xor:
  14238. {
  14239. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14240. break;
  14241. }
  14242. case SIMD_v128_bitselect:
  14243. {
  14244. POP_V128();
  14245. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14246. break;
  14247. }
  14248. case SIMD_v128_any_true:
  14249. {
  14250. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14251. break;
  14252. }
  14253. /* Load Lane Operation */
  14254. case SIMD_v128_load8_lane:
  14255. case SIMD_v128_load16_lane:
  14256. case SIMD_v128_load32_lane:
  14257. case SIMD_v128_load64_lane:
  14258. case SIMD_v128_store8_lane:
  14259. case SIMD_v128_store16_lane:
  14260. case SIMD_v128_store32_lane:
  14261. case SIMD_v128_store64_lane:
  14262. {
  14263. uint8 lane;
  14264. CHECK_MEMORY();
  14265. pb_read_leb_uint32(p, p_end, align); /* align */
  14266. if (!check_simd_memory_access_align(
  14267. opcode1, align, error_buf, error_buf_size)) {
  14268. goto fail;
  14269. }
  14270. pb_read_leb_mem_offset(p, p_end,
  14271. mem_offset); /* offset */
  14272. CHECK_BUF(p, p_end, 1);
  14273. lane = read_uint8(p);
  14274. if (!check_simd_access_lane(opcode1, lane, error_buf,
  14275. error_buf_size)) {
  14276. goto fail;
  14277. }
  14278. #if WASM_ENABLE_FAST_INTERP != 0
  14279. emit_uint32(loader_ctx, mem_offset);
  14280. #endif
  14281. POP_V128();
  14282. POP_MEM_OFFSET();
  14283. #if WASM_ENABLE_FAST_INTERP != 0
  14284. emit_byte(loader_ctx, lane);
  14285. #endif
  14286. if (opcode1 < SIMD_v128_store8_lane) {
  14287. PUSH_V128();
  14288. }
  14289. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14290. func->has_memory_operations = true;
  14291. #endif
  14292. break;
  14293. }
  14294. case SIMD_v128_load32_zero:
  14295. case SIMD_v128_load64_zero:
  14296. {
  14297. CHECK_MEMORY();
  14298. pb_read_leb_uint32(p, p_end, align); /* align */
  14299. if (!check_simd_memory_access_align(
  14300. opcode1, align, error_buf, error_buf_size)) {
  14301. goto fail;
  14302. }
  14303. pb_read_leb_mem_offset(p, p_end,
  14304. mem_offset); /* offset */
  14305. #if WASM_ENABLE_FAST_INTERP != 0
  14306. emit_uint32(loader_ctx, mem_offset);
  14307. #endif
  14308. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  14309. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14310. func->has_memory_operations = true;
  14311. #endif
  14312. break;
  14313. }
  14314. /* Float conversion */
  14315. case SIMD_f32x4_demote_f64x2_zero:
  14316. case SIMD_f64x2_promote_low_f32x4_zero:
  14317. {
  14318. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14319. break;
  14320. }
  14321. /* i8x16 Operation */
  14322. case SIMD_i8x16_abs:
  14323. case SIMD_i8x16_neg:
  14324. case SIMD_i8x16_popcnt:
  14325. {
  14326. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14327. break;
  14328. }
  14329. case SIMD_i8x16_all_true:
  14330. case SIMD_i8x16_bitmask:
  14331. {
  14332. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14333. break;
  14334. }
  14335. case SIMD_i8x16_narrow_i16x8_s:
  14336. case SIMD_i8x16_narrow_i16x8_u:
  14337. {
  14338. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14339. break;
  14340. }
  14341. case SIMD_f32x4_ceil:
  14342. case SIMD_f32x4_floor:
  14343. case SIMD_f32x4_trunc:
  14344. case SIMD_f32x4_nearest:
  14345. {
  14346. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14347. break;
  14348. }
  14349. case SIMD_i8x16_shl:
  14350. case SIMD_i8x16_shr_s:
  14351. case SIMD_i8x16_shr_u:
  14352. {
  14353. POP_I32();
  14354. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14355. break;
  14356. }
  14357. case SIMD_i8x16_add:
  14358. case SIMD_i8x16_add_sat_s:
  14359. case SIMD_i8x16_add_sat_u:
  14360. case SIMD_i8x16_sub:
  14361. case SIMD_i8x16_sub_sat_s:
  14362. case SIMD_i8x16_sub_sat_u:
  14363. {
  14364. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14365. break;
  14366. }
  14367. case SIMD_f64x2_ceil:
  14368. case SIMD_f64x2_floor:
  14369. {
  14370. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14371. break;
  14372. }
  14373. case SIMD_i8x16_min_s:
  14374. case SIMD_i8x16_min_u:
  14375. case SIMD_i8x16_max_s:
  14376. case SIMD_i8x16_max_u:
  14377. {
  14378. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14379. break;
  14380. }
  14381. case SIMD_f64x2_trunc:
  14382. {
  14383. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14384. break;
  14385. }
  14386. case SIMD_i8x16_avgr_u:
  14387. {
  14388. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14389. break;
  14390. }
  14391. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  14392. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  14393. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  14394. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  14395. /* i16x8 operation */
  14396. case SIMD_i16x8_abs:
  14397. case SIMD_i16x8_neg:
  14398. {
  14399. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14400. break;
  14401. }
  14402. case SIMD_i16x8_q15mulr_sat_s:
  14403. {
  14404. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14405. break;
  14406. }
  14407. case SIMD_i16x8_all_true:
  14408. case SIMD_i16x8_bitmask:
  14409. {
  14410. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14411. break;
  14412. }
  14413. case SIMD_i16x8_narrow_i32x4_s:
  14414. case SIMD_i16x8_narrow_i32x4_u:
  14415. {
  14416. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14417. break;
  14418. }
  14419. case SIMD_i16x8_extend_low_i8x16_s:
  14420. case SIMD_i16x8_extend_high_i8x16_s:
  14421. case SIMD_i16x8_extend_low_i8x16_u:
  14422. case SIMD_i16x8_extend_high_i8x16_u:
  14423. {
  14424. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14425. break;
  14426. }
  14427. case SIMD_i16x8_shl:
  14428. case SIMD_i16x8_shr_s:
  14429. case SIMD_i16x8_shr_u:
  14430. {
  14431. POP_I32();
  14432. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14433. break;
  14434. }
  14435. case SIMD_i16x8_add:
  14436. case SIMD_i16x8_add_sat_s:
  14437. case SIMD_i16x8_add_sat_u:
  14438. case SIMD_i16x8_sub:
  14439. case SIMD_i16x8_sub_sat_s:
  14440. case SIMD_i16x8_sub_sat_u:
  14441. {
  14442. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14443. break;
  14444. }
  14445. case SIMD_f64x2_nearest:
  14446. {
  14447. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14448. break;
  14449. }
  14450. case SIMD_i16x8_mul:
  14451. case SIMD_i16x8_min_s:
  14452. case SIMD_i16x8_min_u:
  14453. case SIMD_i16x8_max_s:
  14454. case SIMD_i16x8_max_u:
  14455. case SIMD_i16x8_avgr_u:
  14456. case SIMD_i16x8_extmul_low_i8x16_s:
  14457. case SIMD_i16x8_extmul_high_i8x16_s:
  14458. case SIMD_i16x8_extmul_low_i8x16_u:
  14459. case SIMD_i16x8_extmul_high_i8x16_u:
  14460. {
  14461. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14462. break;
  14463. }
  14464. /* i32x4 operation */
  14465. case SIMD_i32x4_abs:
  14466. case SIMD_i32x4_neg:
  14467. {
  14468. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14469. break;
  14470. }
  14471. case SIMD_i32x4_all_true:
  14472. case SIMD_i32x4_bitmask:
  14473. {
  14474. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14475. break;
  14476. }
  14477. case SIMD_i32x4_extend_low_i16x8_s:
  14478. case SIMD_i32x4_extend_high_i16x8_s:
  14479. case SIMD_i32x4_extend_low_i16x8_u:
  14480. case SIMD_i32x4_extend_high_i16x8_u:
  14481. {
  14482. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14483. break;
  14484. }
  14485. case SIMD_i32x4_shl:
  14486. case SIMD_i32x4_shr_s:
  14487. case SIMD_i32x4_shr_u:
  14488. {
  14489. POP_I32();
  14490. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14491. break;
  14492. }
  14493. case SIMD_i32x4_add:
  14494. case SIMD_i32x4_sub:
  14495. case SIMD_i32x4_mul:
  14496. case SIMD_i32x4_min_s:
  14497. case SIMD_i32x4_min_u:
  14498. case SIMD_i32x4_max_s:
  14499. case SIMD_i32x4_max_u:
  14500. case SIMD_i32x4_dot_i16x8_s:
  14501. case SIMD_i32x4_extmul_low_i16x8_s:
  14502. case SIMD_i32x4_extmul_high_i16x8_s:
  14503. case SIMD_i32x4_extmul_low_i16x8_u:
  14504. case SIMD_i32x4_extmul_high_i16x8_u:
  14505. {
  14506. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14507. break;
  14508. }
  14509. /* i64x2 operation */
  14510. case SIMD_i64x2_abs:
  14511. case SIMD_i64x2_neg:
  14512. {
  14513. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14514. break;
  14515. }
  14516. case SIMD_i64x2_all_true:
  14517. case SIMD_i64x2_bitmask:
  14518. {
  14519. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  14520. break;
  14521. }
  14522. case SIMD_i64x2_extend_low_i32x4_s:
  14523. case SIMD_i64x2_extend_high_i32x4_s:
  14524. case SIMD_i64x2_extend_low_i32x4_u:
  14525. case SIMD_i64x2_extend_high_i32x4_u:
  14526. {
  14527. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14528. break;
  14529. }
  14530. case SIMD_i64x2_shl:
  14531. case SIMD_i64x2_shr_s:
  14532. case SIMD_i64x2_shr_u:
  14533. {
  14534. POP_I32();
  14535. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14536. break;
  14537. }
  14538. case SIMD_i64x2_add:
  14539. case SIMD_i64x2_sub:
  14540. case SIMD_i64x2_mul:
  14541. case SIMD_i64x2_eq:
  14542. case SIMD_i64x2_ne:
  14543. case SIMD_i64x2_lt_s:
  14544. case SIMD_i64x2_gt_s:
  14545. case SIMD_i64x2_le_s:
  14546. case SIMD_i64x2_ge_s:
  14547. case SIMD_i64x2_extmul_low_i32x4_s:
  14548. case SIMD_i64x2_extmul_high_i32x4_s:
  14549. case SIMD_i64x2_extmul_low_i32x4_u:
  14550. case SIMD_i64x2_extmul_high_i32x4_u:
  14551. {
  14552. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14553. break;
  14554. }
  14555. /* f32x4 operation */
  14556. case SIMD_f32x4_abs:
  14557. case SIMD_f32x4_neg:
  14558. case SIMD_f32x4_sqrt:
  14559. {
  14560. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14561. break;
  14562. }
  14563. case SIMD_f32x4_add:
  14564. case SIMD_f32x4_sub:
  14565. case SIMD_f32x4_mul:
  14566. case SIMD_f32x4_div:
  14567. case SIMD_f32x4_min:
  14568. case SIMD_f32x4_max:
  14569. case SIMD_f32x4_pmin:
  14570. case SIMD_f32x4_pmax:
  14571. {
  14572. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14573. break;
  14574. }
  14575. /* f64x2 operation */
  14576. case SIMD_f64x2_abs:
  14577. case SIMD_f64x2_neg:
  14578. case SIMD_f64x2_sqrt:
  14579. {
  14580. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14581. break;
  14582. }
  14583. case SIMD_f64x2_add:
  14584. case SIMD_f64x2_sub:
  14585. case SIMD_f64x2_mul:
  14586. case SIMD_f64x2_div:
  14587. case SIMD_f64x2_min:
  14588. case SIMD_f64x2_max:
  14589. case SIMD_f64x2_pmin:
  14590. case SIMD_f64x2_pmax:
  14591. {
  14592. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14593. break;
  14594. }
  14595. case SIMD_i32x4_trunc_sat_f32x4_s:
  14596. case SIMD_i32x4_trunc_sat_f32x4_u:
  14597. case SIMD_f32x4_convert_i32x4_s:
  14598. case SIMD_f32x4_convert_i32x4_u:
  14599. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  14600. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  14601. case SIMD_f64x2_convert_low_i32x4_s:
  14602. case SIMD_f64x2_convert_low_i32x4_u:
  14603. {
  14604. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  14605. break;
  14606. }
  14607. default:
  14608. {
  14609. if (error_buf != NULL) {
  14610. snprintf(error_buf, error_buf_size,
  14611. "WASM module load failed: "
  14612. "invalid opcode 0xfd %02x.",
  14613. opcode1);
  14614. }
  14615. goto fail;
  14616. }
  14617. }
  14618. break;
  14619. }
  14620. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) || \
  14621. (WASM_ENABLE_FAST_INTERP != 0) */
  14622. #endif /* end of WASM_ENABLE_SIMD */
  14623. #if WASM_ENABLE_SHARED_MEMORY != 0
  14624. case WASM_OP_ATOMIC_PREFIX:
  14625. {
  14626. uint32 opcode1;
  14627. pb_read_leb_uint32(p, p_end, opcode1);
  14628. #if WASM_ENABLE_FAST_INTERP != 0
  14629. emit_byte(loader_ctx, opcode1);
  14630. #endif
  14631. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  14632. CHECK_MEMORY();
  14633. pb_read_leb_uint32(p, p_end, align); /* align */
  14634. pb_read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  14635. if (!check_memory_align_equal(opcode1, align, error_buf,
  14636. error_buf_size)) {
  14637. goto fail;
  14638. }
  14639. #if WASM_ENABLE_FAST_INTERP != 0
  14640. emit_uint32(loader_ctx, mem_offset);
  14641. #endif
  14642. }
  14643. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  14644. func->has_memory_operations = true;
  14645. #endif
  14646. switch (opcode1) {
  14647. case WASM_OP_ATOMIC_NOTIFY:
  14648. POP_I32();
  14649. POP_MEM_OFFSET();
  14650. PUSH_I32();
  14651. break;
  14652. case WASM_OP_ATOMIC_WAIT32:
  14653. POP_I64();
  14654. POP_I32();
  14655. POP_MEM_OFFSET();
  14656. PUSH_I32();
  14657. break;
  14658. case WASM_OP_ATOMIC_WAIT64:
  14659. POP_I64();
  14660. POP_I64();
  14661. POP_MEM_OFFSET();
  14662. PUSH_I32();
  14663. break;
  14664. case WASM_OP_ATOMIC_FENCE:
  14665. /* reserved byte 0x00 */
  14666. if (*p++ != 0x00) {
  14667. set_error_buf(error_buf, error_buf_size,
  14668. "zero byte expected");
  14669. goto fail;
  14670. }
  14671. break;
  14672. case WASM_OP_ATOMIC_I32_LOAD:
  14673. case WASM_OP_ATOMIC_I32_LOAD8_U:
  14674. case WASM_OP_ATOMIC_I32_LOAD16_U:
  14675. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  14676. break;
  14677. case WASM_OP_ATOMIC_I32_STORE:
  14678. case WASM_OP_ATOMIC_I32_STORE8:
  14679. case WASM_OP_ATOMIC_I32_STORE16:
  14680. POP_I32();
  14681. POP_MEM_OFFSET();
  14682. break;
  14683. case WASM_OP_ATOMIC_I64_LOAD:
  14684. case WASM_OP_ATOMIC_I64_LOAD8_U:
  14685. case WASM_OP_ATOMIC_I64_LOAD16_U:
  14686. case WASM_OP_ATOMIC_I64_LOAD32_U:
  14687. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  14688. break;
  14689. case WASM_OP_ATOMIC_I64_STORE:
  14690. case WASM_OP_ATOMIC_I64_STORE8:
  14691. case WASM_OP_ATOMIC_I64_STORE16:
  14692. case WASM_OP_ATOMIC_I64_STORE32:
  14693. POP_I64();
  14694. POP_MEM_OFFSET();
  14695. break;
  14696. case WASM_OP_ATOMIC_RMW_I32_ADD:
  14697. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  14698. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  14699. case WASM_OP_ATOMIC_RMW_I32_SUB:
  14700. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  14701. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  14702. case WASM_OP_ATOMIC_RMW_I32_AND:
  14703. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  14704. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  14705. case WASM_OP_ATOMIC_RMW_I32_OR:
  14706. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14707. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14708. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14709. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14710. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14711. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14712. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14713. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14714. POP_I32();
  14715. POP_MEM_OFFSET();
  14716. PUSH_I32();
  14717. break;
  14718. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14719. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14720. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14721. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14722. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14723. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14724. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14725. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14726. case WASM_OP_ATOMIC_RMW_I64_AND:
  14727. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14728. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14729. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14730. case WASM_OP_ATOMIC_RMW_I64_OR:
  14731. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14732. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14733. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14734. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14735. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14736. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14737. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14738. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14739. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14740. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14741. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14742. POP_I64();
  14743. POP_MEM_OFFSET();
  14744. PUSH_I64();
  14745. break;
  14746. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14747. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14748. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14749. POP_I32();
  14750. POP_I32();
  14751. POP_MEM_OFFSET();
  14752. PUSH_I32();
  14753. break;
  14754. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14755. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14756. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14757. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14758. POP_I64();
  14759. POP_I64();
  14760. POP_MEM_OFFSET();
  14761. PUSH_I64();
  14762. break;
  14763. default:
  14764. set_error_buf_v(error_buf, error_buf_size,
  14765. "%s %02x %02x", "unsupported opcode",
  14766. 0xfe, opcode1);
  14767. goto fail;
  14768. }
  14769. break;
  14770. }
  14771. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14772. default:
  14773. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14774. "unsupported opcode", opcode);
  14775. goto fail;
  14776. }
  14777. #if WASM_ENABLE_FAST_INTERP != 0
  14778. last_op = opcode;
  14779. #endif
  14780. }
  14781. if (loader_ctx->csp_num > 0) {
  14782. /* unmatched end opcodes result from unbalanced control flow structures,
  14783. * for example, br_table with inconsistent target count (1 declared, 2
  14784. * given), or simply superfluous end opcodes */
  14785. set_error_buf(
  14786. error_buf, error_buf_size,
  14787. "unexpected end opcodes from unbalanced control flow structures");
  14788. goto fail;
  14789. }
  14790. #if WASM_ENABLE_FAST_INTERP != 0
  14791. if (loader_ctx->p_code_compiled == NULL)
  14792. goto re_scan;
  14793. func->const_cell_num = loader_ctx->i64_const_num * 2
  14794. + loader_ctx->v128_const_num * 4
  14795. + loader_ctx->i32_const_num;
  14796. if (func->const_cell_num > 0) {
  14797. if (!(func->consts =
  14798. loader_malloc((uint64)sizeof(uint32) * func->const_cell_num,
  14799. error_buf, error_buf_size)))
  14800. goto fail;
  14801. if (loader_ctx->i64_const_num > 0) {
  14802. bh_memcpy_s(func->consts,
  14803. (uint32)sizeof(int64) * loader_ctx->i64_const_num,
  14804. loader_ctx->i64_consts,
  14805. (uint32)sizeof(int64) * loader_ctx->i64_const_num);
  14806. }
  14807. if (loader_ctx->i32_const_num > 0) {
  14808. bh_memcpy_s(func->consts
  14809. + sizeof(int64) * loader_ctx->i64_const_num,
  14810. (uint32)sizeof(int32) * loader_ctx->i32_const_num,
  14811. loader_ctx->i32_consts,
  14812. (uint32)sizeof(int32) * loader_ctx->i32_const_num);
  14813. }
  14814. if (loader_ctx->v128_const_num > 0) {
  14815. bh_memcpy_s(func->consts,
  14816. (uint32)sizeof(V128) * loader_ctx->v128_const_num,
  14817. loader_ctx->v128_consts,
  14818. (uint32)sizeof(V128) * loader_ctx->v128_const_num);
  14819. }
  14820. }
  14821. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14822. - loader_ctx->start_dynamic_offset + 1;
  14823. #else
  14824. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14825. #endif
  14826. func->max_block_num = loader_ctx->max_csp_num;
  14827. return_value = true;
  14828. fail:
  14829. wasm_loader_ctx_destroy(loader_ctx);
  14830. (void)table_idx;
  14831. (void)table_seg_idx;
  14832. (void)data_seg_idx;
  14833. (void)i64_const;
  14834. (void)local_offset;
  14835. (void)p_org;
  14836. (void)mem_offset;
  14837. (void)align;
  14838. return return_value;
  14839. }