wasm_loader.c 575 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader.h"
  6. #include "bh_common.h"
  7. #include "bh_log.h"
  8. #include "wasm.h"
  9. #include "wasm_opcode.h"
  10. #include "wasm_runtime.h"
  11. #include "wasm_loader_common.h"
  12. #include "../common/wasm_native.h"
  13. #include "../common/wasm_memory.h"
  14. #if WASM_ENABLE_GC != 0
  15. #include "../common/gc/gc_type.h"
  16. #include "../common/gc/gc_object.h"
  17. #endif
  18. #if WASM_ENABLE_DEBUG_INTERP != 0
  19. #include "../libraries/debug-engine/debug_engine.h"
  20. #endif
  21. #if WASM_ENABLE_FAST_JIT != 0
  22. #include "../fast-jit/jit_compiler.h"
  23. #include "../fast-jit/jit_codecache.h"
  24. #endif
  25. #if WASM_ENABLE_JIT != 0
  26. #include "../compilation/aot_llvm.h"
  27. #endif
  28. #ifndef TRACE_WASM_LOADER
  29. #define TRACE_WASM_LOADER 0
  30. #endif
  31. /* Read a value of given type from the address pointed to by the given
  32. pointer and increase the pointer to the position just after the
  33. value being read. */
  34. #define TEMPLATE_READ_VALUE(Type, p) \
  35. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  36. #if WASM_ENABLE_MEMORY64 != 0
  37. static bool
  38. has_module_memory64(WASMModule *module)
  39. {
  40. /* TODO: multi-memories for now assuming the memory idx type is consistent
  41. * across multi-memories */
  42. if (module->import_memory_count > 0)
  43. return !!(module->import_memories[0].u.memory.flags & MEMORY64_FLAG);
  44. else if (module->memory_count > 0)
  45. return !!(module->memories[0].flags & MEMORY64_FLAG);
  46. return false;
  47. }
  48. #endif
  49. static void
  50. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  51. {
  52. if (error_buf != NULL) {
  53. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  54. string);
  55. }
  56. }
  57. #if WASM_ENABLE_MEMORY64 != 0
  58. static void
  59. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  60. {
  61. if (error_buf != NULL) {
  62. snprintf(error_buf, error_buf_size, "offset out of range");
  63. }
  64. }
  65. #endif
  66. static void
  67. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  68. {
  69. va_list args;
  70. char buf[128];
  71. if (error_buf != NULL) {
  72. va_start(args, format);
  73. vsnprintf(buf, sizeof(buf), format, args);
  74. va_end(args);
  75. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  76. }
  77. }
  78. static bool
  79. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  80. char *error_buf, uint32 error_buf_size)
  81. {
  82. if ((uintptr_t)buf + length < (uintptr_t)buf
  83. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  84. set_error_buf(error_buf, error_buf_size,
  85. "unexpected end of section or function");
  86. return false;
  87. }
  88. return true;
  89. }
  90. static bool
  91. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  92. char *error_buf, uint32 error_buf_size)
  93. {
  94. if ((uintptr_t)buf + length < (uintptr_t)buf
  95. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  96. set_error_buf(error_buf, error_buf_size, "unexpected end");
  97. return false;
  98. }
  99. return true;
  100. }
  101. #define CHECK_BUF(buf, buf_end, length) \
  102. do { \
  103. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  104. goto fail; \
  105. } \
  106. } while (0)
  107. #define CHECK_BUF1(buf, buf_end, length) \
  108. do { \
  109. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  110. goto fail; \
  111. } \
  112. } while (0)
  113. #define skip_leb(p) while (*p++ & 0x80)
  114. #define skip_leb_int64(p, p_end) skip_leb(p)
  115. #define skip_leb_uint32(p, p_end) skip_leb(p)
  116. #define skip_leb_int32(p, p_end) skip_leb(p)
  117. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  118. static bool
  119. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  120. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  121. {
  122. const uint8 *buf = *p_buf;
  123. uint64 result = 0;
  124. uint32 shift = 0;
  125. uint32 offset = 0, bcnt = 0;
  126. uint64 byte;
  127. while (true) {
  128. /* uN or SN must not exceed ceil(N/7) bytes */
  129. if (bcnt + 1 > (maxbits + 6) / 7) {
  130. set_error_buf(error_buf, error_buf_size,
  131. "integer representation too long");
  132. return false;
  133. }
  134. CHECK_BUF(buf, buf_end, offset + 1);
  135. byte = buf[offset];
  136. offset += 1;
  137. result |= ((byte & 0x7f) << shift);
  138. shift += 7;
  139. bcnt += 1;
  140. if ((byte & 0x80) == 0) {
  141. break;
  142. }
  143. }
  144. if (!sign && maxbits == 32 && shift >= maxbits) {
  145. /* The top bits set represent values > 32 bits */
  146. if (((uint8)byte) & 0xf0)
  147. goto fail_integer_too_large;
  148. }
  149. else if (sign && maxbits == 32) {
  150. if (shift < maxbits) {
  151. /* Sign extend, second-highest bit is the sign bit */
  152. if ((uint8)byte & 0x40)
  153. result |= (~((uint64)0)) << shift;
  154. }
  155. else {
  156. /* The top bits should be a sign-extension of the sign bit */
  157. bool sign_bit_set = ((uint8)byte) & 0x8;
  158. int top_bits = ((uint8)byte) & 0xf0;
  159. if ((sign_bit_set && top_bits != 0x70)
  160. || (!sign_bit_set && top_bits != 0))
  161. goto fail_integer_too_large;
  162. }
  163. }
  164. else if (sign && maxbits == 64) {
  165. if (shift < maxbits) {
  166. /* Sign extend, second-highest bit is the sign bit */
  167. if ((uint8)byte & 0x40)
  168. result |= (~((uint64)0)) << shift;
  169. }
  170. else {
  171. /* The top bits should be a sign-extension of the sign bit */
  172. bool sign_bit_set = ((uint8)byte) & 0x1;
  173. int top_bits = ((uint8)byte) & 0xfe;
  174. if ((sign_bit_set && top_bits != 0x7e)
  175. || (!sign_bit_set && top_bits != 0))
  176. goto fail_integer_too_large;
  177. }
  178. }
  179. *p_buf += offset;
  180. *p_result = result;
  181. return true;
  182. fail_integer_too_large:
  183. set_error_buf(error_buf, error_buf_size, "integer too large");
  184. fail:
  185. return false;
  186. }
  187. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  188. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  189. #define read_leb_int64(p, p_end, res) \
  190. do { \
  191. uint64 res64; \
  192. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  193. error_buf_size)) \
  194. goto fail; \
  195. res = (int64)res64; \
  196. } while (0)
  197. #if WASM_ENABLE_MEMORY64 != 0
  198. #define read_leb_mem_offset(p, p_end, res) \
  199. do { \
  200. uint64 res64; \
  201. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  202. &res64, error_buf, error_buf_size)) { \
  203. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  204. goto fail; \
  205. } \
  206. res = (mem_offset_t)res64; \
  207. } while (0)
  208. #else
  209. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  210. #endif
  211. #define read_leb_uint32(p, p_end, res) \
  212. do { \
  213. uint64 res64; \
  214. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  215. error_buf_size)) \
  216. goto fail; \
  217. res = (uint32)res64; \
  218. } while (0)
  219. #define read_leb_int32(p, p_end, res) \
  220. do { \
  221. uint64 res64; \
  222. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  223. error_buf_size)) \
  224. goto fail; \
  225. res = (int32)res64; \
  226. } while (0)
  227. static char *
  228. type2str(uint8 type)
  229. {
  230. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  231. #if WASM_ENABLE_GC != 0
  232. char *type_str_ref[] = { "stringview_iter",
  233. "stringview_wtf16",
  234. "(ref null ht)",
  235. "(ref ht)",
  236. "", /* reserved */
  237. "stringview_wtf8",
  238. "stringref",
  239. "", /* reserved */
  240. "", /* reserved */
  241. "arrayref",
  242. "structref",
  243. "i32ref",
  244. "eqref",
  245. "anyref",
  246. "externref",
  247. "funcref",
  248. "nullref",
  249. "nullexternref",
  250. "nullfuncref" };
  251. #endif
  252. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  253. return type_str[type - VALUE_TYPE_V128];
  254. #if WASM_ENABLE_GC != 0
  255. else if (wasm_is_type_reftype(type))
  256. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  257. #endif
  258. else if (type == VALUE_TYPE_FUNCREF)
  259. return "funcref";
  260. else if (type == VALUE_TYPE_EXTERNREF)
  261. return "externref";
  262. else
  263. return "unknown type";
  264. }
  265. static bool
  266. is_32bit_type(uint8 type)
  267. {
  268. if (type == VALUE_TYPE_I32
  269. || type == VALUE_TYPE_F32
  270. /* the operand stack is in polymorphic state */
  271. || type == VALUE_TYPE_ANY
  272. #if WASM_ENABLE_GC != 0
  273. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  274. #elif WASM_ENABLE_REF_TYPES != 0
  275. /* For reference types, we use uint32 index to represent
  276. the funcref and externref */
  277. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  278. #endif
  279. )
  280. return true;
  281. return false;
  282. }
  283. static bool
  284. is_64bit_type(uint8 type)
  285. {
  286. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  287. #if WASM_ENABLE_GC != 0
  288. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  289. #endif
  290. )
  291. return true;
  292. return false;
  293. }
  294. static bool
  295. is_value_type(uint8 type)
  296. {
  297. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  298. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  299. #if WASM_ENABLE_GC != 0
  300. /* reference types, 0x65 to 0x70 */
  301. || wasm_is_type_reftype(type)
  302. #elif WASM_ENABLE_REF_TYPES != 0
  303. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  304. #endif
  305. #if WASM_ENABLE_SIMD != 0
  306. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  307. || type == VALUE_TYPE_V128 /* 0x7B */
  308. #endif
  309. #endif
  310. )
  311. return true;
  312. return false;
  313. }
  314. #if WASM_ENABLE_GC != 0
  315. static bool
  316. is_packed_type(uint8 type)
  317. {
  318. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  319. }
  320. #endif
  321. static bool
  322. is_byte_a_type(uint8 type)
  323. {
  324. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  325. }
  326. #if WASM_ENABLE_SIMD != 0
  327. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  328. static V128
  329. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  330. {
  331. V128 result;
  332. uint8 i;
  333. for (i = 0; i != 16; ++i) {
  334. result.i8x16[i] = read_uint8(p_buf);
  335. }
  336. return result;
  337. }
  338. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  339. #endif /* end of WASM_ENABLE_SIMD */
  340. static void *
  341. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  342. {
  343. void *mem;
  344. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  345. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  346. return NULL;
  347. }
  348. memset(mem, 0, (uint32)size);
  349. return mem;
  350. }
  351. static void *
  352. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  353. uint32 error_buf_size)
  354. {
  355. uint8 *mem_new;
  356. bh_assert(size_new > size_old);
  357. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  358. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  359. memset(mem_new + size_old, 0, size_new - size_old);
  360. wasm_runtime_free(mem_old);
  361. }
  362. return mem_new;
  363. }
  364. #define MEM_REALLOC(mem, size_old, size_new) \
  365. do { \
  366. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  367. error_buf_size); \
  368. if (!mem_new) \
  369. goto fail; \
  370. mem = mem_new; \
  371. } while (0)
  372. #if WASM_ENABLE_GC != 0
  373. static bool
  374. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  375. char *error_buf, uint32 error_buf_size)
  376. {
  377. if (type_index >= type_count) {
  378. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  379. type_index);
  380. return false;
  381. }
  382. return true;
  383. }
  384. static bool
  385. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  386. uint32 error_buf_size)
  387. {
  388. if (!check_type_index(module, module->type_count, type_index, error_buf,
  389. error_buf_size)) {
  390. return false;
  391. }
  392. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  393. set_error_buf(error_buf, error_buf_size, "unkown array type");
  394. return false;
  395. }
  396. return true;
  397. }
  398. #endif
  399. static bool
  400. check_function_index(const WASMModule *module, uint32 function_index,
  401. char *error_buf, uint32 error_buf_size)
  402. {
  403. if (function_index
  404. >= module->import_function_count + module->function_count) {
  405. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  406. function_index);
  407. return false;
  408. }
  409. return true;
  410. }
  411. typedef struct InitValue {
  412. uint8 type;
  413. uint8 flag;
  414. #if WASM_ENABLE_GC != 0
  415. uint8 gc_opcode;
  416. WASMRefType ref_type;
  417. #endif
  418. WASMValue value;
  419. } InitValue;
  420. typedef struct ConstExprContext {
  421. uint32 sp;
  422. uint32 size;
  423. WASMModule *module;
  424. InitValue *stack;
  425. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  426. } ConstExprContext;
  427. static void
  428. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  429. {
  430. ctx->sp = 0;
  431. ctx->module = module;
  432. ctx->stack = ctx->data;
  433. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  434. }
  435. static bool
  436. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  437. #if WASM_ENABLE_GC != 0
  438. WASMRefType *ref_type, uint8 gc_opcode,
  439. #endif
  440. WASMValue *value, char *error_buf, uint32 error_buf_size)
  441. {
  442. InitValue *cur_value;
  443. if (ctx->sp >= ctx->size) {
  444. if (ctx->stack != ctx->data) {
  445. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  446. (ctx->size + 4) * sizeof(InitValue));
  447. }
  448. else {
  449. if (!(ctx->stack =
  450. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  451. error_buf, error_buf_size))) {
  452. goto fail;
  453. }
  454. }
  455. ctx->size += 4;
  456. }
  457. cur_value = &ctx->stack[ctx->sp++];
  458. cur_value->type = type;
  459. cur_value->flag = flag;
  460. cur_value->value = *value;
  461. #if WASM_ENABLE_GC != 0
  462. cur_value->gc_opcode = gc_opcode;
  463. if (wasm_is_type_multi_byte_type(type)) {
  464. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  465. ref_type, wasm_reftype_struct_size(ref_type));
  466. }
  467. #endif
  468. return true;
  469. fail:
  470. return false;
  471. }
  472. static bool
  473. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  474. #if WASM_ENABLE_GC != 0
  475. WASMRefType *ref_type, uint8 *p_gc_opcode,
  476. #endif
  477. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  478. {
  479. InitValue *cur_value;
  480. if (ctx->sp == 0) {
  481. set_error_buf(error_buf, error_buf_size,
  482. "type mismatch: const expr stack underflow");
  483. return false;
  484. }
  485. cur_value = &ctx->stack[--ctx->sp];
  486. #if WASM_ENABLE_GC == 0
  487. if (cur_value->type != type) {
  488. set_error_buf(error_buf, error_buf_size, "type mismatch");
  489. return false;
  490. }
  491. #else
  492. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  493. ref_type, ctx->module->types,
  494. ctx->module->type_count)) {
  495. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  496. "type mismatch: expect ", type2str(type),
  497. " but got other");
  498. goto fail;
  499. }
  500. if ((ctx->sp != 0) && (cur_value->flag == WASM_OP_GC_PREFIX)
  501. && (cur_value->gc_opcode != WASM_OP_REF_I31)) {
  502. /* To reduce complexity, we don't allow initialize struct fields/array
  503. * element with references, so struct/array must be at the bottom of the
  504. * init value stack */
  505. set_error_buf(
  506. error_buf, error_buf_size,
  507. "struct or array as field is not supported in constant expr");
  508. goto fail;
  509. }
  510. #endif
  511. if (p_flag)
  512. *p_flag = cur_value->flag;
  513. if (p_value)
  514. *p_value = cur_value->value;
  515. #if WASM_ENABLE_GC != 0
  516. if (p_gc_opcode)
  517. *p_gc_opcode = cur_value->gc_opcode;
  518. #endif
  519. return true;
  520. #if WASM_ENABLE_GC != 0
  521. fail:
  522. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  523. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  524. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  525. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  526. wasm_runtime_free(cur_value->value.data);
  527. }
  528. return false;
  529. #endif
  530. }
  531. static void
  532. destroy_const_expr_stack(ConstExprContext *ctx)
  533. {
  534. #if WASM_ENABLE_GC != 0
  535. uint32 i;
  536. for (i = 0; i < ctx->sp; i++) {
  537. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  538. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  539. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  540. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  541. wasm_runtime_free(ctx->stack[i].value.data);
  542. }
  543. }
  544. #endif
  545. if (ctx->stack != ctx->data) {
  546. wasm_runtime_free(ctx->stack);
  547. }
  548. }
  549. #if WASM_ENABLE_GC != 0
  550. static void
  551. destroy_init_expr(InitializerExpression *expr)
  552. {
  553. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  554. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  555. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  556. wasm_runtime_free(expr->u.data);
  557. }
  558. }
  559. #endif /* end of WASM_ENABLE_GC != 0 */
  560. static bool
  561. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  562. InitializerExpression *init_expr, uint8 type, void *ref_type,
  563. char *error_buf, uint32 error_buf_size)
  564. {
  565. const uint8 *p = *p_buf, *p_end = buf_end;
  566. uint8 flag, *p_float;
  567. uint32 i;
  568. ConstExprContext const_expr_ctx = { 0 };
  569. WASMValue cur_value;
  570. #if WASM_ENABLE_GC != 0
  571. uint32 opcode1, type_idx;
  572. uint8 opcode;
  573. WASMRefType cur_ref_type = { 0 };
  574. #endif
  575. init_const_expr_stack(&const_expr_ctx, module);
  576. CHECK_BUF(p, p_end, 1);
  577. flag = read_uint8(p);
  578. while (flag != WASM_OP_END) {
  579. switch (flag) {
  580. /* i32.const */
  581. case INIT_EXPR_TYPE_I32_CONST:
  582. read_leb_int32(p, p_end, cur_value.i32);
  583. if (!push_const_expr_stack(
  584. &const_expr_ctx, flag, VALUE_TYPE_I32,
  585. #if WASM_ENABLE_GC != 0
  586. NULL, 0,
  587. #endif
  588. &cur_value, error_buf, error_buf_size))
  589. goto fail;
  590. break;
  591. /* i64.const */
  592. case INIT_EXPR_TYPE_I64_CONST:
  593. read_leb_int64(p, p_end, cur_value.i64);
  594. if (!push_const_expr_stack(
  595. &const_expr_ctx, flag, VALUE_TYPE_I64,
  596. #if WASM_ENABLE_GC != 0
  597. NULL, 0,
  598. #endif
  599. &cur_value, error_buf, error_buf_size))
  600. goto fail;
  601. break;
  602. /* f32.const */
  603. case INIT_EXPR_TYPE_F32_CONST:
  604. CHECK_BUF(p, p_end, 4);
  605. p_float = (uint8 *)&cur_value.f32;
  606. for (i = 0; i < sizeof(float32); i++)
  607. *p_float++ = *p++;
  608. if (!push_const_expr_stack(
  609. &const_expr_ctx, flag, VALUE_TYPE_F32,
  610. #if WASM_ENABLE_GC != 0
  611. NULL, 0,
  612. #endif
  613. &cur_value, error_buf, error_buf_size))
  614. goto fail;
  615. break;
  616. /* f64.const */
  617. case INIT_EXPR_TYPE_F64_CONST:
  618. CHECK_BUF(p, p_end, 8);
  619. p_float = (uint8 *)&cur_value.f64;
  620. for (i = 0; i < sizeof(float64); i++)
  621. *p_float++ = *p++;
  622. if (!push_const_expr_stack(
  623. &const_expr_ctx, flag, VALUE_TYPE_F64,
  624. #if WASM_ENABLE_GC != 0
  625. NULL, 0,
  626. #endif
  627. &cur_value, error_buf, error_buf_size))
  628. goto fail;
  629. break;
  630. #if WASM_ENABLE_SIMD != 0
  631. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  632. /* v128.const */
  633. case INIT_EXPR_TYPE_V128_CONST:
  634. {
  635. uint64 high, low;
  636. CHECK_BUF(p, p_end, 1);
  637. (void)read_uint8(p);
  638. CHECK_BUF(p, p_end, 16);
  639. wasm_runtime_read_v128(p, &high, &low);
  640. p += 16;
  641. cur_value.v128.i64x2[0] = high;
  642. cur_value.v128.i64x2[1] = low;
  643. if (!push_const_expr_stack(
  644. &const_expr_ctx, flag, VALUE_TYPE_V128,
  645. #if WASM_ENABLE_GC != 0
  646. NULL, 0,
  647. #endif
  648. &cur_value, error_buf, error_buf_size))
  649. goto fail;
  650. #if WASM_ENABLE_WAMR_COMPILER != 0
  651. /* If any init_expr is v128.const, mark SIMD used */
  652. module->is_simd_used = true;
  653. #endif
  654. break;
  655. }
  656. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  657. #endif /* end of WASM_ENABLE_SIMD */
  658. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  659. /* ref.func */
  660. case INIT_EXPR_TYPE_FUNCREF_CONST:
  661. {
  662. uint32 func_idx;
  663. read_leb_uint32(p, p_end, func_idx);
  664. cur_value.ref_index = func_idx;
  665. if (!check_function_index(module, func_idx, error_buf,
  666. error_buf_size)) {
  667. goto fail;
  668. }
  669. #if WASM_ENABLE_GC == 0
  670. if (!push_const_expr_stack(&const_expr_ctx, flag,
  671. VALUE_TYPE_FUNCREF, &cur_value,
  672. error_buf, error_buf_size))
  673. goto fail;
  674. #else
  675. if (func_idx < module->import_function_count) {
  676. type_idx =
  677. module->import_functions[func_idx].u.function.type_idx;
  678. }
  679. else {
  680. type_idx = module
  681. ->functions[func_idx
  682. - module->import_function_count]
  683. ->type_idx;
  684. }
  685. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  686. false, type_idx);
  687. if (!push_const_expr_stack(&const_expr_ctx, flag,
  688. cur_ref_type.ref_type, &cur_ref_type,
  689. 0, &cur_value, error_buf,
  690. error_buf_size))
  691. goto fail;
  692. #endif
  693. #if WASM_ENABLE_WAMR_COMPILER != 0
  694. module->is_ref_types_used = true;
  695. #endif
  696. break;
  697. }
  698. /* ref.null */
  699. case INIT_EXPR_TYPE_REFNULL_CONST:
  700. {
  701. uint8 type1;
  702. CHECK_BUF(p, p_end, 1);
  703. type1 = read_uint8(p);
  704. #if WASM_ENABLE_GC == 0
  705. cur_value.ref_index = NULL_REF;
  706. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  707. &cur_value, error_buf,
  708. error_buf_size))
  709. goto fail;
  710. #else
  711. cur_value.gc_obj = NULL_REF;
  712. if (!is_byte_a_type(type1)) {
  713. p--;
  714. read_leb_uint32(p, p_end, type_idx);
  715. if (!check_type_index(module, module->type_count, type_idx,
  716. error_buf, error_buf_size))
  717. goto fail;
  718. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  719. true, type_idx);
  720. if (!push_const_expr_stack(&const_expr_ctx, flag,
  721. cur_ref_type.ref_type,
  722. &cur_ref_type, 0, &cur_value,
  723. error_buf, error_buf_size))
  724. goto fail;
  725. }
  726. else {
  727. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  728. NULL, 0, &cur_value, error_buf,
  729. error_buf_size))
  730. goto fail;
  731. }
  732. #endif
  733. #if WASM_ENABLE_WAMR_COMPILER != 0
  734. module->is_ref_types_used = true;
  735. #endif
  736. break;
  737. }
  738. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  739. /* get_global */
  740. case INIT_EXPR_TYPE_GET_GLOBAL:
  741. {
  742. uint32 global_idx;
  743. uint8 global_type;
  744. read_leb_uint32(p, p_end, cur_value.global_index);
  745. global_idx = cur_value.global_index;
  746. /*
  747. * Currently, constant expressions occurring as initializers
  748. * of globals are further constrained in that contained
  749. * global.get instructions are
  750. * only allowed to refer to imported globals.
  751. *
  752. * https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions
  753. */
  754. if (global_idx >= module->import_global_count
  755. /* make spec test happy */
  756. #if WASM_ENABLE_GC != 0
  757. + module->global_count
  758. #endif
  759. ) {
  760. set_error_buf_v(error_buf, error_buf_size,
  761. "unknown global %u", global_idx);
  762. goto fail;
  763. }
  764. if (
  765. /* make spec test happy */
  766. #if WASM_ENABLE_GC != 0
  767. global_idx < module->import_global_count &&
  768. #endif
  769. module->import_globals[global_idx]
  770. .u.global.type.is_mutable) {
  771. set_error_buf_v(error_buf, error_buf_size,
  772. "constant expression required");
  773. goto fail;
  774. }
  775. if (global_idx < module->import_global_count) {
  776. global_type = module->import_globals[global_idx]
  777. .u.global.type.val_type;
  778. #if WASM_ENABLE_GC != 0
  779. if (wasm_is_type_multi_byte_type(global_type)) {
  780. WASMRefType *global_ref_type =
  781. module->import_globals[global_idx]
  782. .u.global.ref_type;
  783. bh_memcpy_s(&cur_ref_type,
  784. wasm_reftype_struct_size(global_ref_type),
  785. global_ref_type,
  786. wasm_reftype_struct_size(global_ref_type));
  787. }
  788. #endif
  789. }
  790. else {
  791. global_type =
  792. module
  793. ->globals[global_idx - module->import_global_count]
  794. .type.val_type;
  795. #if WASM_ENABLE_GC != 0
  796. if (wasm_is_type_multi_byte_type(global_type)) {
  797. WASMRefType *global_ref_type =
  798. module
  799. ->globals[global_idx
  800. - module->import_global_count]
  801. .ref_type;
  802. bh_memcpy_s(&cur_ref_type,
  803. wasm_reftype_struct_size(global_ref_type),
  804. global_ref_type,
  805. wasm_reftype_struct_size(global_ref_type));
  806. }
  807. #endif
  808. }
  809. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  810. #if WASM_ENABLE_GC != 0
  811. &cur_ref_type, 0,
  812. #endif
  813. &cur_value, error_buf,
  814. error_buf_size))
  815. goto fail;
  816. break;
  817. }
  818. #if WASM_ENABLE_GC != 0
  819. /* struct.new and array.new */
  820. case WASM_OP_GC_PREFIX:
  821. {
  822. read_leb_uint32(p, p_end, opcode1);
  823. switch (opcode1) {
  824. case WASM_OP_STRUCT_NEW:
  825. {
  826. WASMStructType *struct_type;
  827. WASMStructNewInitValues *struct_init_values = NULL;
  828. uint32 field_count;
  829. read_leb_uint32(p, p_end, type_idx);
  830. if (!check_type_index(module, module->type_count,
  831. type_idx, error_buf,
  832. error_buf_size)) {
  833. goto fail;
  834. }
  835. struct_type = (WASMStructType *)module->types[type_idx];
  836. if (struct_type->base_type.type_flag
  837. != WASM_TYPE_STRUCT) {
  838. set_error_buf(error_buf, error_buf_size,
  839. "unkown struct type");
  840. goto fail;
  841. }
  842. field_count = struct_type->field_count;
  843. if (!(struct_init_values = loader_malloc(
  844. offsetof(WASMStructNewInitValues, fields)
  845. + (uint64)field_count * sizeof(WASMValue),
  846. error_buf, error_buf_size))) {
  847. goto fail;
  848. }
  849. struct_init_values->count = field_count;
  850. for (i = field_count; i > 0; i--) {
  851. WASMRefType *field_ref_type = NULL;
  852. uint32 field_idx = i - 1;
  853. uint8 field_type =
  854. struct_type->fields[field_idx].field_type;
  855. if (wasm_is_type_multi_byte_type(field_type)) {
  856. field_ref_type = wasm_reftype_map_find(
  857. struct_type->ref_type_maps,
  858. struct_type->ref_type_map_count, field_idx);
  859. }
  860. if (is_packed_type(field_type)) {
  861. field_type = VALUE_TYPE_I32;
  862. }
  863. if (!pop_const_expr_stack(
  864. &const_expr_ctx, NULL, field_type,
  865. field_ref_type, NULL,
  866. &struct_init_values->fields[field_idx],
  867. error_buf, error_buf_size)) {
  868. wasm_runtime_free(struct_init_values);
  869. goto fail;
  870. }
  871. }
  872. cur_value.data = struct_init_values;
  873. wasm_set_refheaptype_typeidx(
  874. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  875. if (!push_const_expr_stack(
  876. &const_expr_ctx, flag, cur_ref_type.ref_type,
  877. &cur_ref_type, (uint8)opcode1, &cur_value,
  878. error_buf, error_buf_size)) {
  879. wasm_runtime_free(struct_init_values);
  880. goto fail;
  881. }
  882. break;
  883. }
  884. case WASM_OP_STRUCT_NEW_DEFAULT:
  885. {
  886. read_leb_uint32(p, p_end, cur_value.type_index);
  887. type_idx = cur_value.type_index;
  888. if (!check_type_index(module, module->type_count,
  889. type_idx, error_buf,
  890. error_buf_size)) {
  891. goto fail;
  892. }
  893. if (module->types[type_idx]->type_flag
  894. != WASM_TYPE_STRUCT) {
  895. set_error_buf(error_buf, error_buf_size,
  896. "unkown struct type");
  897. goto fail;
  898. }
  899. cur_value.type_index = type_idx;
  900. wasm_set_refheaptype_typeidx(
  901. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  902. if (!push_const_expr_stack(
  903. &const_expr_ctx, flag, cur_ref_type.ref_type,
  904. &cur_ref_type, (uint8)opcode1, &cur_value,
  905. error_buf, error_buf_size)) {
  906. goto fail;
  907. }
  908. break;
  909. }
  910. case WASM_OP_ARRAY_NEW:
  911. case WASM_OP_ARRAY_NEW_DEFAULT:
  912. case WASM_OP_ARRAY_NEW_FIXED:
  913. {
  914. WASMArrayNewInitValues *array_init_values = NULL;
  915. WASMArrayType *array_type = NULL;
  916. WASMRefType *elem_ref_type = NULL;
  917. uint64 total_size;
  918. uint8 elem_type;
  919. read_leb_uint32(p, p_end, cur_value.type_index);
  920. type_idx = cur_value.type_index;
  921. if (!check_type_index(module, module->type_count,
  922. type_idx, error_buf,
  923. error_buf_size)) {
  924. goto fail;
  925. }
  926. array_type = (WASMArrayType *)module->types[type_idx];
  927. if (array_type->base_type.type_flag
  928. != WASM_TYPE_ARRAY) {
  929. set_error_buf(error_buf, error_buf_size,
  930. "unkown array type");
  931. goto fail;
  932. }
  933. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  934. elem_type = array_type->elem_type;
  935. if (wasm_is_type_multi_byte_type(elem_type)) {
  936. elem_ref_type = array_type->elem_ref_type;
  937. }
  938. if (is_packed_type(elem_type)) {
  939. elem_type = VALUE_TYPE_I32;
  940. }
  941. if (opcode1 == WASM_OP_ARRAY_NEW) {
  942. WASMValue len_val;
  943. if (!(array_init_values = loader_malloc(
  944. sizeof(WASMArrayNewInitValues),
  945. error_buf, error_buf_size))) {
  946. goto fail;
  947. }
  948. array_init_values->type_idx = type_idx;
  949. if (!pop_const_expr_stack(
  950. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  951. NULL, NULL, &len_val, error_buf,
  952. error_buf_size)) {
  953. wasm_runtime_free(array_init_values);
  954. goto fail;
  955. }
  956. array_init_values->length = len_val.i32;
  957. if (!pop_const_expr_stack(
  958. &const_expr_ctx, NULL, elem_type,
  959. elem_ref_type, NULL,
  960. &array_init_values->elem_data[0],
  961. error_buf, error_buf_size)) {
  962. wasm_runtime_free(array_init_values);
  963. goto fail;
  964. }
  965. cur_value.data = array_init_values;
  966. }
  967. else {
  968. /* WASM_OP_ARRAY_NEW_FIXED */
  969. uint32 len;
  970. read_leb_uint32(p, p_end, len);
  971. total_size =
  972. (uint64)offsetof(WASMArrayNewInitValues,
  973. elem_data)
  974. + (uint64)sizeof(WASMValue) * len;
  975. if (!(array_init_values =
  976. loader_malloc(total_size, error_buf,
  977. error_buf_size))) {
  978. goto fail;
  979. }
  980. array_init_values->type_idx = type_idx;
  981. array_init_values->length = len;
  982. for (i = len; i > 0; i--) {
  983. if (!pop_const_expr_stack(
  984. &const_expr_ctx, NULL, elem_type,
  985. elem_ref_type, NULL,
  986. &array_init_values
  987. ->elem_data[i - 1],
  988. error_buf, error_buf_size)) {
  989. wasm_runtime_free(array_init_values);
  990. goto fail;
  991. }
  992. }
  993. cur_value.data = array_init_values;
  994. }
  995. }
  996. else {
  997. /* WASM_OP_ARRAY_NEW_DEFAULT */
  998. WASMValue len_val;
  999. uint32 len;
  1000. /* POP(i32) */
  1001. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1002. VALUE_TYPE_I32, NULL,
  1003. NULL, &len_val, error_buf,
  1004. error_buf_size)) {
  1005. goto fail;
  1006. }
  1007. len = len_val.i32;
  1008. cur_value.array_new_default.type_index = type_idx;
  1009. cur_value.array_new_default.length = len;
  1010. }
  1011. wasm_set_refheaptype_typeidx(
  1012. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1013. if (!push_const_expr_stack(
  1014. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1015. &cur_ref_type, (uint8)opcode1, &cur_value,
  1016. error_buf, error_buf_size)) {
  1017. if (array_init_values) {
  1018. wasm_runtime_free(array_init_values);
  1019. }
  1020. goto fail;
  1021. }
  1022. break;
  1023. }
  1024. case WASM_OP_ANY_CONVERT_EXTERN:
  1025. {
  1026. set_error_buf(error_buf, error_buf_size,
  1027. "unsupported constant expression of "
  1028. "extern.internalize");
  1029. goto fail;
  1030. }
  1031. case WASM_OP_EXTERN_CONVERT_ANY:
  1032. {
  1033. set_error_buf(error_buf, error_buf_size,
  1034. "unsupported constant expression of "
  1035. "extern.externalize");
  1036. goto fail;
  1037. }
  1038. case WASM_OP_REF_I31:
  1039. {
  1040. /* POP(i32) */
  1041. if (!pop_const_expr_stack(
  1042. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1043. NULL, &cur_value, error_buf, error_buf_size)) {
  1044. goto fail;
  1045. }
  1046. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1047. false, HEAP_TYPE_I31);
  1048. if (!push_const_expr_stack(
  1049. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1050. &cur_ref_type, (uint8)opcode1, &cur_value,
  1051. error_buf, error_buf_size)) {
  1052. goto fail;
  1053. }
  1054. break;
  1055. }
  1056. default:
  1057. set_error_buf(
  1058. error_buf, error_buf_size,
  1059. "type mismatch or constant expression required");
  1060. goto fail;
  1061. }
  1062. break;
  1063. }
  1064. #endif /* end of WASM_ENABLE_GC != 0 */
  1065. default:
  1066. {
  1067. set_error_buf(error_buf, error_buf_size,
  1068. "illegal opcode "
  1069. "or constant expression required "
  1070. "or type mismatch");
  1071. goto fail;
  1072. }
  1073. }
  1074. CHECK_BUF(p, p_end, 1);
  1075. flag = read_uint8(p);
  1076. }
  1077. /* There should be only one value left on the init value stack */
  1078. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1079. #if WASM_ENABLE_GC != 0
  1080. ref_type, &opcode,
  1081. #endif
  1082. &cur_value, error_buf, error_buf_size)) {
  1083. goto fail;
  1084. }
  1085. if (const_expr_ctx.sp != 0) {
  1086. set_error_buf(error_buf, error_buf_size,
  1087. "type mismatch: illegal constant opcode sequence");
  1088. goto fail;
  1089. }
  1090. init_expr->init_expr_type = flag;
  1091. init_expr->u = cur_value;
  1092. #if WASM_ENABLE_GC != 0
  1093. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1094. switch (opcode) {
  1095. case WASM_OP_STRUCT_NEW:
  1096. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1097. break;
  1098. case WASM_OP_STRUCT_NEW_DEFAULT:
  1099. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1100. break;
  1101. case WASM_OP_ARRAY_NEW:
  1102. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1103. break;
  1104. case WASM_OP_ARRAY_NEW_DEFAULT:
  1105. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1106. break;
  1107. case WASM_OP_ARRAY_NEW_FIXED:
  1108. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1109. break;
  1110. case WASM_OP_REF_I31:
  1111. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1112. break;
  1113. default:
  1114. bh_assert(0);
  1115. break;
  1116. }
  1117. }
  1118. #endif /* end of WASM_ENABLE_GC != 0 */
  1119. *p_buf = p;
  1120. destroy_const_expr_stack(&const_expr_ctx);
  1121. return true;
  1122. fail:
  1123. destroy_const_expr_stack(&const_expr_ctx);
  1124. return false;
  1125. }
  1126. static bool
  1127. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1128. {
  1129. if (mutable >= 2) {
  1130. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1131. return false;
  1132. }
  1133. return true;
  1134. }
  1135. #if WASM_ENABLE_GC != 0
  1136. static void
  1137. destroy_func_type(WASMFuncType *type)
  1138. {
  1139. /* Destroy the reference type hash set */
  1140. if (type->ref_type_maps)
  1141. wasm_runtime_free(type->ref_type_maps);
  1142. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1143. && WASM_ENABLE_LAZY_JIT != 0
  1144. if (type->call_to_llvm_jit_from_fast_jit)
  1145. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1146. #endif
  1147. /* Free the type */
  1148. wasm_runtime_free(type);
  1149. }
  1150. static void
  1151. destroy_struct_type(WASMStructType *type)
  1152. {
  1153. if (type->ref_type_maps)
  1154. wasm_runtime_free(type->ref_type_maps);
  1155. wasm_runtime_free(type);
  1156. }
  1157. static void
  1158. destroy_array_type(WASMArrayType *type)
  1159. {
  1160. wasm_runtime_free(type);
  1161. }
  1162. static void
  1163. destroy_wasm_type(WASMType *type)
  1164. {
  1165. if (type->ref_count > 1) {
  1166. /* The type is referenced by other types
  1167. of current wasm module */
  1168. type->ref_count--;
  1169. return;
  1170. }
  1171. if (type->type_flag == WASM_TYPE_FUNC)
  1172. destroy_func_type((WASMFuncType *)type);
  1173. else if (type->type_flag == WASM_TYPE_STRUCT)
  1174. destroy_struct_type((WASMStructType *)type);
  1175. else if (type->type_flag == WASM_TYPE_ARRAY)
  1176. destroy_array_type((WASMArrayType *)type);
  1177. else {
  1178. bh_assert(0);
  1179. }
  1180. }
  1181. /* Resolve (ref null ht) or (ref ht) */
  1182. static bool
  1183. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1184. WASMModule *module, uint32 type_count, bool nullable,
  1185. WASMRefType *ref_type, char *error_buf,
  1186. uint32 error_buf_size)
  1187. {
  1188. const uint8 *p = *p_buf, *p_end = buf_end;
  1189. ref_type->ref_type =
  1190. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1191. ref_type->ref_ht_common.nullable = nullable;
  1192. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1193. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1194. /* heap type is (type i), i : typeidx, >= 0 */
  1195. if (!check_type_index(module, type_count,
  1196. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1197. error_buf_size)) {
  1198. return false;
  1199. }
  1200. }
  1201. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1202. /* heap type is func, extern, any, eq, i31 or data */
  1203. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1204. return false;
  1205. }
  1206. *p_buf = p;
  1207. return true;
  1208. fail:
  1209. return false;
  1210. }
  1211. static bool
  1212. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1213. WASMModule *module, uint32 type_count,
  1214. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1215. bool allow_packed_type, char *error_buf,
  1216. uint32 error_buf_size)
  1217. {
  1218. const uint8 *p = *p_buf, *p_end = buf_end;
  1219. uint8 type;
  1220. memset(ref_type, 0, sizeof(WASMRefType));
  1221. CHECK_BUF(p, p_end, 1);
  1222. type = read_uint8(p);
  1223. if (wasm_is_reftype_htref_nullable(type)) {
  1224. /* (ref null ht) */
  1225. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1226. ref_type, error_buf, error_buf_size))
  1227. return false;
  1228. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1229. *p_need_ref_type_map = true;
  1230. else {
  1231. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1232. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1233. multi-byte type to one-byte type to reduce the footprint and
  1234. the complexity of type equal/subtype checking */
  1235. ref_type->ref_type =
  1236. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1237. *p_need_ref_type_map = false;
  1238. }
  1239. }
  1240. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1241. /* (ref ht) */
  1242. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1243. ref_type, error_buf, error_buf_size))
  1244. return false;
  1245. *p_need_ref_type_map = true;
  1246. #if WASM_ENABLE_STRINGREF != 0
  1247. /* covert (ref string) to stringref */
  1248. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1249. ref_type->ref_type =
  1250. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1251. *p_need_ref_type_map = false;
  1252. }
  1253. #endif
  1254. }
  1255. else {
  1256. /* type which can be represented by one byte */
  1257. if (!is_value_type(type)
  1258. && !(allow_packed_type && is_packed_type(type))) {
  1259. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1260. return false;
  1261. }
  1262. ref_type->ref_type = type;
  1263. *p_need_ref_type_map = false;
  1264. #if WASM_ENABLE_WAMR_COMPILER != 0
  1265. /* If any value's type is v128, mark the module as SIMD used */
  1266. if (type == VALUE_TYPE_V128)
  1267. module->is_simd_used = true;
  1268. #endif
  1269. }
  1270. *p_buf = p;
  1271. return true;
  1272. fail:
  1273. return false;
  1274. }
  1275. static WASMRefType *
  1276. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1277. char *error_buf, uint32 error_buf_size)
  1278. {
  1279. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1280. if (!ret) {
  1281. set_error_buf(error_buf, error_buf_size,
  1282. "insert ref type to hash set failed");
  1283. }
  1284. return ret;
  1285. }
  1286. static bool
  1287. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1288. uint32 type_count, uint32 type_idx, char *error_buf,
  1289. uint32 error_buf_size)
  1290. {
  1291. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1292. uint32 param_count, result_count, i, j = 0;
  1293. uint32 param_cell_num, ret_cell_num;
  1294. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1295. uint64 total_size;
  1296. bool need_ref_type_map;
  1297. WASMRefType ref_type;
  1298. WASMFuncType *type = NULL;
  1299. /* Parse first time to resolve param count, result count and
  1300. ref type map count */
  1301. read_leb_uint32(p, p_end, param_count);
  1302. p_org = p;
  1303. for (i = 0; i < param_count; i++) {
  1304. if (!resolve_value_type(&p, p_end, module, type_count,
  1305. &need_ref_type_map, &ref_type, false, error_buf,
  1306. error_buf_size)) {
  1307. return false;
  1308. }
  1309. if (need_ref_type_map)
  1310. ref_type_map_count++;
  1311. }
  1312. read_leb_uint32(p, p_end, result_count);
  1313. for (i = 0; i < result_count; i++) {
  1314. if (!resolve_value_type(&p, p_end, module, type_count,
  1315. &need_ref_type_map, &ref_type, false, error_buf,
  1316. error_buf_size)) {
  1317. return false;
  1318. }
  1319. if (need_ref_type_map) {
  1320. ref_type_map_count++;
  1321. result_ref_type_map_count++;
  1322. }
  1323. }
  1324. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1325. "ref type map count: %d",
  1326. type_idx, param_count, result_count, ref_type_map_count);
  1327. /* Parse second time to resolve param types, result types and
  1328. ref type map info */
  1329. p = p_org;
  1330. total_size = offsetof(WASMFuncType, types)
  1331. + sizeof(uint8) * (uint64)(param_count + result_count);
  1332. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1333. return false;
  1334. }
  1335. if (ref_type_map_count > 0) {
  1336. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1337. if (!(type->ref_type_maps =
  1338. loader_malloc(total_size, error_buf, error_buf_size))) {
  1339. goto fail;
  1340. }
  1341. }
  1342. type->base_type.type_flag = WASM_TYPE_FUNC;
  1343. type->param_count = param_count;
  1344. type->result_count = result_count;
  1345. type->ref_type_map_count = ref_type_map_count;
  1346. if (ref_type_map_count > 0) {
  1347. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1348. - result_ref_type_map_count;
  1349. }
  1350. for (i = 0; i < param_count; i++) {
  1351. if (!resolve_value_type(&p, p_end, module, type_count,
  1352. &need_ref_type_map, &ref_type, false, error_buf,
  1353. error_buf_size)) {
  1354. goto fail;
  1355. }
  1356. type->types[i] = ref_type.ref_type;
  1357. if (need_ref_type_map) {
  1358. type->ref_type_maps[j].index = i;
  1359. if (!(type->ref_type_maps[j++].ref_type =
  1360. reftype_set_insert(module->ref_type_set, &ref_type,
  1361. error_buf, error_buf_size))) {
  1362. goto fail;
  1363. }
  1364. }
  1365. }
  1366. read_leb_uint32(p, p_end, result_count);
  1367. for (i = 0; i < result_count; i++) {
  1368. if (!resolve_value_type(&p, p_end, module, type_count,
  1369. &need_ref_type_map, &ref_type, false, error_buf,
  1370. error_buf_size)) {
  1371. goto fail;
  1372. }
  1373. type->types[param_count + i] = ref_type.ref_type;
  1374. if (need_ref_type_map) {
  1375. type->ref_type_maps[j].index = param_count + i;
  1376. if (!(type->ref_type_maps[j++].ref_type =
  1377. reftype_set_insert(module->ref_type_set, &ref_type,
  1378. error_buf, error_buf_size))) {
  1379. goto fail;
  1380. }
  1381. }
  1382. }
  1383. bh_assert(j == type->ref_type_map_count);
  1384. #if TRACE_WASM_LOADER != 0
  1385. os_printf("type %d = ", type_idx);
  1386. wasm_dump_func_type(type);
  1387. #endif
  1388. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1389. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1390. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1391. set_error_buf(error_buf, error_buf_size,
  1392. "param count or result count too large");
  1393. goto fail;
  1394. }
  1395. type->param_cell_num = (uint16)param_cell_num;
  1396. type->ret_cell_num = (uint16)ret_cell_num;
  1397. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1398. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1399. #endif
  1400. #if WASM_ENABLE_WAMR_COMPILER != 0
  1401. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1402. if (type->types[i] == VALUE_TYPE_V128)
  1403. module->is_simd_used = true;
  1404. }
  1405. #endif
  1406. *p_buf = p;
  1407. module->types[type_idx] = (WASMType *)type;
  1408. return true;
  1409. fail:
  1410. if (type)
  1411. destroy_func_type(type);
  1412. return false;
  1413. }
  1414. static bool
  1415. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1416. WASMModule *module, uint32 type_count, uint32 type_idx,
  1417. char *error_buf, uint32 error_buf_size)
  1418. {
  1419. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1420. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1421. uint32 i, j = 0, offset;
  1422. uint16 *reference_table;
  1423. uint64 total_size;
  1424. uint8 mutable;
  1425. bool need_ref_type_map;
  1426. WASMRefType ref_type;
  1427. WASMStructType *type = NULL;
  1428. /* Parse first time to resolve field count and ref type map count */
  1429. read_leb_uint32(p, p_end, field_count);
  1430. p_org = p;
  1431. for (i = 0; i < field_count; i++) {
  1432. if (!resolve_value_type(&p, p_end, module, type_count,
  1433. &need_ref_type_map, &ref_type, true, error_buf,
  1434. error_buf_size)) {
  1435. return false;
  1436. }
  1437. if (need_ref_type_map)
  1438. ref_type_map_count++;
  1439. if (wasm_is_type_reftype(ref_type.ref_type))
  1440. ref_field_count++;
  1441. CHECK_BUF(p, p_end, 1);
  1442. mutable = read_uint8(p);
  1443. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1444. return false;
  1445. }
  1446. }
  1447. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1448. type_idx, field_count, ref_type_map_count);
  1449. /* Parse second time to resolve field types and ref type map info */
  1450. p = p_org;
  1451. total_size = offsetof(WASMStructType, fields)
  1452. + sizeof(WASMStructFieldType) * (uint64)field_count
  1453. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1454. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1455. return false;
  1456. }
  1457. if (ref_type_map_count > 0) {
  1458. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1459. if (!(type->ref_type_maps =
  1460. loader_malloc(total_size, error_buf, error_buf_size))) {
  1461. goto fail;
  1462. }
  1463. }
  1464. type->reference_table = reference_table =
  1465. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1466. + sizeof(WASMStructFieldType) * field_count);
  1467. *reference_table++ = ref_field_count;
  1468. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1469. type->field_count = field_count;
  1470. type->ref_type_map_count = ref_type_map_count;
  1471. offset = (uint32)offsetof(WASMStructObject, field_data);
  1472. for (i = 0; i < field_count; i++) {
  1473. if (!resolve_value_type(&p, p_end, module, type_count,
  1474. &need_ref_type_map, &ref_type, true, error_buf,
  1475. error_buf_size)) {
  1476. goto fail;
  1477. }
  1478. type->fields[i].field_type = ref_type.ref_type;
  1479. if (need_ref_type_map) {
  1480. type->ref_type_maps[j].index = i;
  1481. if (!(type->ref_type_maps[j++].ref_type =
  1482. reftype_set_insert(module->ref_type_set, &ref_type,
  1483. error_buf, error_buf_size))) {
  1484. goto fail;
  1485. }
  1486. }
  1487. CHECK_BUF(p, p_end, 1);
  1488. type->fields[i].field_flags = read_uint8(p);
  1489. type->fields[i].field_size =
  1490. (uint8)wasm_reftype_size(ref_type.ref_type);
  1491. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1492. || defined(BUILD_TARGET_X86_32))
  1493. if (type->fields[i].field_size == 2)
  1494. offset = align_uint(offset, 2);
  1495. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1496. offset = align_uint(offset, 4);
  1497. #endif
  1498. type->fields[i].field_offset = offset;
  1499. if (wasm_is_type_reftype(ref_type.ref_type))
  1500. *reference_table++ = offset;
  1501. offset += type->fields[i].field_size;
  1502. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1503. type->fields[i].field_flags, type->fields[i].field_type);
  1504. }
  1505. type->total_size = offset;
  1506. bh_assert(j == type->ref_type_map_count);
  1507. #if TRACE_WASM_LOADER != 0
  1508. os_printf("type %d = ", type_idx);
  1509. wasm_dump_struct_type(type);
  1510. #endif
  1511. *p_buf = p;
  1512. module->types[type_idx] = (WASMType *)type;
  1513. return true;
  1514. fail:
  1515. if (type)
  1516. destroy_struct_type(type);
  1517. return false;
  1518. }
  1519. static bool
  1520. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1521. WASMModule *module, uint32 type_count, uint32 type_idx,
  1522. char *error_buf, uint32 error_buf_size)
  1523. {
  1524. const uint8 *p = *p_buf, *p_end = buf_end;
  1525. uint8 mutable;
  1526. bool need_ref_type_map;
  1527. WASMRefType ref_type;
  1528. WASMArrayType *type = NULL;
  1529. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1530. &ref_type, true, error_buf, error_buf_size)) {
  1531. return false;
  1532. }
  1533. CHECK_BUF(p, p_end, 1);
  1534. mutable = read_uint8(p);
  1535. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1536. return false;
  1537. }
  1538. LOG_VERBOSE("type %u: array", type_idx);
  1539. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1540. error_buf_size))) {
  1541. return false;
  1542. }
  1543. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1544. type->elem_flags = mutable;
  1545. type->elem_type = ref_type.ref_type;
  1546. if (need_ref_type_map) {
  1547. if (!(type->elem_ref_type =
  1548. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1549. error_buf_size))) {
  1550. goto fail;
  1551. }
  1552. }
  1553. #if TRACE_WASM_LOADER != 0
  1554. os_printf("type %d = ", type_idx);
  1555. wasm_dump_array_type(type);
  1556. #endif
  1557. *p_buf = p;
  1558. module->types[type_idx] = (WASMType *)type;
  1559. return true;
  1560. fail:
  1561. if (type)
  1562. destroy_array_type(type);
  1563. return false;
  1564. }
  1565. static bool
  1566. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1567. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1568. {
  1569. if (heap_type >= 0) {
  1570. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1571. error_buf_size)) {
  1572. return false;
  1573. }
  1574. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1575. heap_type);
  1576. }
  1577. else {
  1578. if (!wasm_is_valid_heap_type(heap_type)) {
  1579. set_error_buf(error_buf, error_buf_size, "unknown type");
  1580. return false;
  1581. }
  1582. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1583. heap_type);
  1584. if (nullable) {
  1585. /* For (ref null func/extern/any/eq/i31/data),
  1586. they are same as
  1587. funcref/externref/anyref/eqref/i31ref/dataref,
  1588. we convert the multi-byte type to one-byte
  1589. type to reduce the footprint and the
  1590. complexity of type equal/subtype checking */
  1591. ref_type->ref_type =
  1592. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1593. }
  1594. }
  1595. return true;
  1596. }
  1597. static void
  1598. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1599. WASMRefType *ref_type2)
  1600. {
  1601. /**
  1602. * The difference rt1 ∖ rt2 between two reference types is defined as
  1603. * follows:
  1604. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1605. * (ref ht2) = (ref null?1 ht1)
  1606. */
  1607. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1608. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1609. ref_type1, wasm_reftype_struct_size(ref_type1));
  1610. }
  1611. else {
  1612. ref_type_diff->ref_type = ref_type1->ref_type;
  1613. }
  1614. if (ref_type2->ref_ht_common.nullable) {
  1615. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1616. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1617. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1618. (int32)ref_type_diff->ref_type - 0x80);
  1619. }
  1620. else {
  1621. ref_type_diff->ref_ht_typeidx.nullable = false;
  1622. }
  1623. }
  1624. }
  1625. #else /* else of WASM_ENABLE_GC != 0 */
  1626. static void
  1627. destroy_wasm_type(WASMType *type)
  1628. {
  1629. if (type->ref_count > 1) {
  1630. /* The type is referenced by other types
  1631. of current wasm module */
  1632. type->ref_count--;
  1633. return;
  1634. }
  1635. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1636. && WASM_ENABLE_LAZY_JIT != 0
  1637. if (type->call_to_llvm_jit_from_fast_jit)
  1638. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1639. #endif
  1640. wasm_runtime_free(type);
  1641. }
  1642. #endif /* end of WASM_ENABLE_GC != 0 */
  1643. static bool
  1644. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1645. char *error_buf, uint32 error_buf_size)
  1646. {
  1647. const uint8 *p = buf, *p_end = buf_end;
  1648. uint32 type_count, i;
  1649. uint64 total_size;
  1650. uint8 flag;
  1651. #if WASM_ENABLE_GC != 0
  1652. uint32 processed_type_count = 0;
  1653. #endif
  1654. read_leb_uint32(p, p_end, type_count);
  1655. if (type_count) {
  1656. module->type_count = type_count;
  1657. total_size = sizeof(WASMType *) * (uint64)type_count;
  1658. if (!(module->types =
  1659. loader_malloc(total_size, error_buf, error_buf_size))) {
  1660. return false;
  1661. }
  1662. #if WASM_ENABLE_GC == 0
  1663. for (i = 0; i < type_count; i++) {
  1664. WASMFuncType *type;
  1665. const uint8 *p_org;
  1666. uint32 param_count, result_count, j;
  1667. uint32 param_cell_num, ret_cell_num;
  1668. CHECK_BUF(p, p_end, 1);
  1669. flag = read_uint8(p);
  1670. if (flag != 0x60) {
  1671. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1672. return false;
  1673. }
  1674. read_leb_uint32(p, p_end, param_count);
  1675. /* Resolve param count and result count firstly */
  1676. p_org = p;
  1677. CHECK_BUF(p, p_end, param_count);
  1678. p += param_count;
  1679. read_leb_uint32(p, p_end, result_count);
  1680. CHECK_BUF(p, p_end, result_count);
  1681. p = p_org;
  1682. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1683. set_error_buf(error_buf, error_buf_size,
  1684. "param count or result count too large");
  1685. return false;
  1686. }
  1687. total_size = offsetof(WASMFuncType, types)
  1688. + sizeof(uint8) * (uint64)(param_count + result_count);
  1689. if (!(type = module->types[i] =
  1690. loader_malloc(total_size, error_buf, error_buf_size))) {
  1691. return false;
  1692. }
  1693. /* Resolve param types and result types */
  1694. type->ref_count = 1;
  1695. type->param_count = (uint16)param_count;
  1696. type->result_count = (uint16)result_count;
  1697. for (j = 0; j < param_count; j++) {
  1698. CHECK_BUF(p, p_end, 1);
  1699. type->types[j] = read_uint8(p);
  1700. }
  1701. read_leb_uint32(p, p_end, result_count);
  1702. for (j = 0; j < result_count; j++) {
  1703. CHECK_BUF(p, p_end, 1);
  1704. type->types[param_count + j] = read_uint8(p);
  1705. }
  1706. for (j = 0; j < param_count + result_count; j++) {
  1707. if (!is_value_type(type->types[j])) {
  1708. set_error_buf(error_buf, error_buf_size,
  1709. "unknown value type");
  1710. return false;
  1711. }
  1712. }
  1713. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1714. ret_cell_num =
  1715. wasm_get_cell_num(type->types + param_count, result_count);
  1716. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1717. set_error_buf(error_buf, error_buf_size,
  1718. "param count or result count too large");
  1719. return false;
  1720. }
  1721. type->param_cell_num = (uint16)param_cell_num;
  1722. type->ret_cell_num = (uint16)ret_cell_num;
  1723. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1724. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1725. #endif
  1726. #if WASM_ENABLE_WAMR_COMPILER != 0
  1727. for (j = 0; j < type->param_count + type->result_count; j++) {
  1728. if (type->types[j] == VALUE_TYPE_V128)
  1729. module->is_simd_used = true;
  1730. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1731. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1732. module->is_ref_types_used = true;
  1733. }
  1734. #endif
  1735. /* If there is already a same type created, use it instead */
  1736. for (j = 0; j < i; j++) {
  1737. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1738. if (module->types[j]->ref_count == UINT16_MAX) {
  1739. set_error_buf(error_buf, error_buf_size,
  1740. "wasm type's ref count too large");
  1741. return false;
  1742. }
  1743. destroy_wasm_type(type);
  1744. module->types[i] = module->types[j];
  1745. module->types[j]->ref_count++;
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. #else /* else of WASM_ENABLE_GC == 0 */
  1751. for (i = 0; i < type_count; i++) {
  1752. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1753. uint32 rec_count = 1, j;
  1754. bool is_sub_final = true;
  1755. CHECK_BUF(p, p_end, 1);
  1756. flag = read_uint8(p);
  1757. if (flag == DEFINED_TYPE_REC) {
  1758. read_leb_uint32(p, p_end, rec_count);
  1759. if (rec_count > 1) {
  1760. uint64 new_total_size;
  1761. /* integer overflow */
  1762. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1763. set_error_buf(error_buf, error_buf_size,
  1764. "recursive type count too large");
  1765. return false;
  1766. }
  1767. module->type_count += rec_count - 1;
  1768. new_total_size =
  1769. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1770. if (new_total_size > UINT32_MAX) {
  1771. set_error_buf(error_buf, error_buf_size,
  1772. "allocate memory failed");
  1773. return false;
  1774. }
  1775. MEM_REALLOC(module->types, (uint32)total_size,
  1776. (uint32)new_total_size);
  1777. total_size = new_total_size;
  1778. }
  1779. LOG_VERBOSE("Processing rec group [%d-%d]",
  1780. processed_type_count,
  1781. processed_type_count + rec_count - 1);
  1782. }
  1783. else {
  1784. p--;
  1785. }
  1786. for (j = 0; j < rec_count; j++) {
  1787. WASMType *cur_type = NULL;
  1788. CHECK_BUF(p, p_end, 1);
  1789. flag = read_uint8(p);
  1790. parent_type_idx = -1;
  1791. if (flag == DEFINED_TYPE_SUB
  1792. || flag == DEFINED_TYPE_SUB_FINAL) {
  1793. read_leb_uint32(p, p_end, super_type_count);
  1794. if (super_type_count > 1) {
  1795. set_error_buf(error_buf, error_buf_size,
  1796. "super type count too large");
  1797. return false;
  1798. }
  1799. if (super_type_count > 0) {
  1800. read_leb_uint32(p, p_end, parent_type_idx);
  1801. if (parent_type_idx >= processed_type_count + j) {
  1802. set_error_buf_v(error_buf, error_buf_size,
  1803. "unknown type %d", parent_type_idx);
  1804. return false;
  1805. }
  1806. if (module->types[parent_type_idx]->is_sub_final) {
  1807. set_error_buf(error_buf, error_buf_size,
  1808. "sub type can not inherit from "
  1809. "a final super type");
  1810. return false;
  1811. }
  1812. }
  1813. if (flag == DEFINED_TYPE_SUB)
  1814. is_sub_final = false;
  1815. CHECK_BUF(p, p_end, 1);
  1816. flag = read_uint8(p);
  1817. }
  1818. if (flag == DEFINED_TYPE_FUNC) {
  1819. if (!resolve_func_type(&p, buf_end, module,
  1820. processed_type_count + rec_count,
  1821. processed_type_count + j, error_buf,
  1822. error_buf_size)) {
  1823. return false;
  1824. }
  1825. }
  1826. else if (flag == DEFINED_TYPE_STRUCT) {
  1827. if (!resolve_struct_type(&p, buf_end, module,
  1828. processed_type_count + rec_count,
  1829. processed_type_count + j,
  1830. error_buf, error_buf_size)) {
  1831. return false;
  1832. }
  1833. }
  1834. else if (flag == DEFINED_TYPE_ARRAY) {
  1835. if (!resolve_array_type(&p, buf_end, module,
  1836. processed_type_count + rec_count,
  1837. processed_type_count + j, error_buf,
  1838. error_buf_size)) {
  1839. return false;
  1840. }
  1841. }
  1842. else {
  1843. set_error_buf(error_buf, error_buf_size,
  1844. "invalid type flag");
  1845. return false;
  1846. }
  1847. cur_type = module->types[processed_type_count + j];
  1848. cur_type->ref_count = 1;
  1849. cur_type->parent_type_idx = parent_type_idx;
  1850. cur_type->is_sub_final = is_sub_final;
  1851. cur_type->rec_count = rec_count;
  1852. cur_type->rec_idx = j;
  1853. cur_type->rec_begin_type_idx = processed_type_count;
  1854. }
  1855. /* resolve subtyping relationship in current rec group */
  1856. for (j = 0; j < rec_count; j++) {
  1857. WASMType *cur_type = module->types[processed_type_count + j];
  1858. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1859. WASMType *parent_type =
  1860. module->types[cur_type->parent_type_idx];
  1861. cur_type->parent_type = parent_type;
  1862. cur_type->root_type = parent_type->root_type;
  1863. if (parent_type->inherit_depth == UINT16_MAX) {
  1864. set_error_buf(error_buf, error_buf_size,
  1865. "parent type's inherit depth too large");
  1866. return false;
  1867. }
  1868. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1869. }
  1870. else {
  1871. cur_type->parent_type = NULL;
  1872. cur_type->root_type = cur_type;
  1873. cur_type->inherit_depth = 0;
  1874. }
  1875. }
  1876. for (j = 0; j < rec_count; j++) {
  1877. WASMType *cur_type = module->types[processed_type_count + j];
  1878. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1879. WASMType *parent_type =
  1880. module->types[cur_type->parent_type_idx];
  1881. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1882. module->types,
  1883. module->type_count)) {
  1884. set_error_buf(error_buf, error_buf_size,
  1885. "sub type does not match super type");
  1886. return false;
  1887. }
  1888. }
  1889. }
  1890. /* If there is already an equivalence type or a group of equivalence
  1891. recursive types created, use it or them instead */
  1892. for (j = 0; j < processed_type_count;) {
  1893. WASMType *src_type = module->types[j];
  1894. WASMType *cur_type = module->types[processed_type_count];
  1895. uint32 k, src_rec_count;
  1896. src_rec_count = src_type->rec_count;
  1897. if (src_rec_count != rec_count) {
  1898. /* no type equivalence */
  1899. j += src_rec_count;
  1900. continue;
  1901. }
  1902. for (k = 0; k < rec_count; k++) {
  1903. src_type = module->types[j + k];
  1904. cur_type = module->types[processed_type_count + k];
  1905. if (!wasm_type_equal(src_type, cur_type, module->types,
  1906. module->type_count)) {
  1907. break;
  1908. }
  1909. }
  1910. if (k < rec_count) {
  1911. /* no type equivalence */
  1912. j += src_rec_count;
  1913. continue;
  1914. }
  1915. /* type equivalence */
  1916. for (k = 0; k < rec_count; k++) {
  1917. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1918. set_error_buf(error_buf, error_buf_size,
  1919. "wasm type's ref count too large");
  1920. return false;
  1921. }
  1922. destroy_wasm_type(module->types[processed_type_count + k]);
  1923. module->types[processed_type_count + k] =
  1924. module->types[j + k];
  1925. module->types[j + k]->ref_count++;
  1926. }
  1927. break;
  1928. }
  1929. if (rec_count > 1) {
  1930. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1931. processed_type_count,
  1932. processed_type_count + rec_count - 1);
  1933. }
  1934. processed_type_count += rec_count;
  1935. }
  1936. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1937. * module->type_count,
  1938. error_buf, error_buf_size))) {
  1939. return false;
  1940. }
  1941. #endif /* end of WASM_ENABLE_GC == 0 */
  1942. }
  1943. if (p != p_end) {
  1944. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1945. return false;
  1946. }
  1947. LOG_VERBOSE("Load type section success.\n");
  1948. return true;
  1949. fail:
  1950. return false;
  1951. }
  1952. static void
  1953. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1954. {
  1955. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  1956. ? init_size * 2
  1957. : WASM_TABLE_MAX_SIZE;
  1958. if (max_size_flag) {
  1959. /* module defines the table limitation */
  1960. bh_assert(init_size <= *max_size);
  1961. if (init_size < *max_size) {
  1962. *max_size =
  1963. *max_size < default_max_size ? *max_size : default_max_size;
  1964. }
  1965. }
  1966. else {
  1967. /* partial defined table limitation, gives a default value */
  1968. *max_size = default_max_size;
  1969. }
  1970. }
  1971. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1972. /**
  1973. * Find export item of a module with export info:
  1974. * module name, field name and export kind
  1975. */
  1976. static WASMExport *
  1977. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1978. const char *field_name, uint8 export_kind,
  1979. char *error_buf, uint32 error_buf_size)
  1980. {
  1981. WASMExport *export =
  1982. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1983. export_kind, error_buf, error_buf_size);
  1984. return export;
  1985. }
  1986. #endif
  1987. #if WASM_ENABLE_MULTI_MODULE != 0
  1988. static WASMFunction *
  1989. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1990. const WASMFuncType *expected_function_type,
  1991. char *error_buf, uint32 error_buf_size)
  1992. {
  1993. WASMModuleCommon *module_reg;
  1994. WASMFunction *function = NULL;
  1995. WASMExport *export = NULL;
  1996. WASMModule *module = NULL;
  1997. WASMFuncType *target_function_type = NULL;
  1998. module_reg = wasm_runtime_find_module_registered(module_name);
  1999. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2000. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2001. function_name);
  2002. set_error_buf(error_buf, error_buf_size, "unknown import");
  2003. return NULL;
  2004. }
  2005. module = (WASMModule *)module_reg;
  2006. export =
  2007. wasm_loader_find_export(module, module_name, function_name,
  2008. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2009. if (!export) {
  2010. return NULL;
  2011. }
  2012. /* resolve function type and function */
  2013. if (export->index < module->import_function_count) {
  2014. target_function_type =
  2015. module->import_functions[export->index].u.function.func_type;
  2016. function = module->import_functions[export->index]
  2017. .u.function.import_func_linked;
  2018. }
  2019. else {
  2020. target_function_type =
  2021. module->functions[export->index - module->import_function_count]
  2022. ->func_type;
  2023. function =
  2024. module->functions[export->index - module->import_function_count];
  2025. }
  2026. /* check function type */
  2027. if (!wasm_type_equal((WASMType *)expected_function_type,
  2028. (WASMType *)target_function_type, module->types,
  2029. module->type_count)) {
  2030. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2031. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2032. return NULL;
  2033. }
  2034. return function;
  2035. }
  2036. static WASMTable *
  2037. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2038. uint32 init_size, uint32 max_size, char *error_buf,
  2039. uint32 error_buf_size)
  2040. {
  2041. WASMModuleCommon *module_reg;
  2042. WASMTable *table = NULL;
  2043. WASMExport *export = NULL;
  2044. WASMModule *module = NULL;
  2045. module_reg = wasm_runtime_find_module_registered(module_name);
  2046. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2047. LOG_DEBUG("can not find a module named %s for table", module_name);
  2048. set_error_buf(error_buf, error_buf_size, "unknown import");
  2049. return NULL;
  2050. }
  2051. module = (WASMModule *)module_reg;
  2052. export =
  2053. wasm_loader_find_export(module, module_name, table_name,
  2054. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2055. if (!export) {
  2056. return NULL;
  2057. }
  2058. /* resolve table and check the init/max size */
  2059. if (export->index < module->import_table_count) {
  2060. table =
  2061. module->import_tables[export->index].u.table.import_table_linked;
  2062. }
  2063. else {
  2064. table = &(module->tables[export->index - module->import_table_count]);
  2065. }
  2066. if (table->init_size < init_size || table->max_size > max_size) {
  2067. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2068. module_name, table_name, table->init_size, table->max_size,
  2069. init_size, max_size);
  2070. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2071. return NULL;
  2072. }
  2073. return table;
  2074. }
  2075. static WASMMemory *
  2076. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2077. uint32 init_page_count, uint32 max_page_count,
  2078. char *error_buf, uint32 error_buf_size)
  2079. {
  2080. WASMModuleCommon *module_reg;
  2081. WASMMemory *memory = NULL;
  2082. WASMExport *export = NULL;
  2083. WASMModule *module = NULL;
  2084. module_reg = wasm_runtime_find_module_registered(module_name);
  2085. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2086. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2087. set_error_buf(error_buf, error_buf_size, "unknown import");
  2088. return NULL;
  2089. }
  2090. module = (WASMModule *)module_reg;
  2091. export =
  2092. wasm_loader_find_export(module, module_name, memory_name,
  2093. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2094. if (!export) {
  2095. return NULL;
  2096. }
  2097. /* resolve memory and check the init/max page count */
  2098. if (export->index < module->import_memory_count) {
  2099. memory = module->import_memories[export->index]
  2100. .u.memory.import_memory_linked;
  2101. }
  2102. else {
  2103. memory =
  2104. &(module->memories[export->index - module->import_memory_count]);
  2105. }
  2106. if (memory->init_page_count < init_page_count
  2107. || memory->max_page_count > max_page_count) {
  2108. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2109. module_name, memory_name, memory->init_page_count,
  2110. memory->max_page_count, init_page_count, max_page_count);
  2111. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2112. return NULL;
  2113. }
  2114. return memory;
  2115. }
  2116. static WASMGlobal *
  2117. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2118. uint8 type, bool is_mutable, char *error_buf,
  2119. uint32 error_buf_size)
  2120. {
  2121. WASMModuleCommon *module_reg;
  2122. WASMGlobal *global = NULL;
  2123. WASMExport *export = NULL;
  2124. WASMModule *module = NULL;
  2125. module_reg = wasm_runtime_find_module_registered(module_name);
  2126. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2127. LOG_DEBUG("can not find a module named %s for global", module_name);
  2128. set_error_buf(error_buf, error_buf_size, "unknown import");
  2129. return NULL;
  2130. }
  2131. module = (WASMModule *)module_reg;
  2132. export =
  2133. wasm_loader_find_export(module, module_name, global_name,
  2134. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2135. if (!export) {
  2136. return NULL;
  2137. }
  2138. /* resolve and check the global */
  2139. if (export->index < module->import_global_count) {
  2140. global =
  2141. module->import_globals[export->index].u.global.import_global_linked;
  2142. }
  2143. else {
  2144. global =
  2145. &(module->globals[export->index - module->import_global_count]);
  2146. }
  2147. if (global->type.val_type != type
  2148. || global->type.is_mutable != is_mutable) {
  2149. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2150. module_name, global_name, global->type.val_type,
  2151. global->type.is_mutable, type, is_mutable);
  2152. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2153. return NULL;
  2154. }
  2155. return global;
  2156. }
  2157. #if WASM_ENABLE_TAGS != 0
  2158. static WASMTag *
  2159. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2160. const WASMType *expected_tag_type,
  2161. uint32 *linked_tag_index, char *error_buf,
  2162. uint32 error_buf_size)
  2163. {
  2164. WASMModuleCommon *module_reg;
  2165. WASMTag *tag = NULL;
  2166. WASMExport *export = NULL;
  2167. WASMModule *module = NULL;
  2168. module_reg = wasm_runtime_find_module_registered(module_name);
  2169. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2170. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2171. tag_name);
  2172. set_error_buf(error_buf, error_buf_size, "unknown import");
  2173. return NULL;
  2174. }
  2175. module = (WASMModule *)module_reg;
  2176. export =
  2177. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2178. error_buf, error_buf_size);
  2179. if (!export) {
  2180. return NULL;
  2181. }
  2182. /* resolve tag type and tag */
  2183. if (export->index < module->import_tag_count) {
  2184. /* importing an imported tag from the submodule */
  2185. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2186. }
  2187. else {
  2188. /* importing an section tag from the submodule */
  2189. tag = module->tags[export->index - module->import_tag_count];
  2190. }
  2191. /* check function type */
  2192. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2193. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2194. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2195. return NULL;
  2196. }
  2197. if (linked_tag_index != NULL) {
  2198. *linked_tag_index = export->index;
  2199. }
  2200. return tag;
  2201. }
  2202. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2203. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2204. static bool
  2205. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2206. const WASMModule *parent_module,
  2207. const char *sub_module_name, const char *function_name,
  2208. WASMFunctionImport *function, char *error_buf,
  2209. uint32 error_buf_size)
  2210. {
  2211. const uint8 *p = *p_buf, *p_end = buf_end;
  2212. uint32 declare_type_index = 0;
  2213. WASMFuncType *declare_func_type = NULL;
  2214. WASMFunction *linked_func = NULL;
  2215. #if WASM_ENABLE_MULTI_MODULE != 0
  2216. WASMModule *sub_module = NULL;
  2217. #endif
  2218. const char *linked_signature = NULL;
  2219. void *linked_attachment = NULL;
  2220. bool linked_call_conv_raw = false;
  2221. bool is_native_symbol = false;
  2222. read_leb_uint32(p, p_end, declare_type_index);
  2223. *p_buf = p;
  2224. if (declare_type_index >= parent_module->type_count) {
  2225. set_error_buf(error_buf, error_buf_size, "unknown type");
  2226. return false;
  2227. }
  2228. #if WASM_ENABLE_GC != 0
  2229. function->type_idx = declare_type_index;
  2230. #endif
  2231. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2232. declare_type_index = wasm_get_smallest_type_idx(
  2233. parent_module->types, parent_module->type_count, declare_type_index);
  2234. #endif
  2235. declare_func_type =
  2236. (WASMFuncType *)parent_module->types[declare_type_index];
  2237. /* lookup registered native symbols first */
  2238. linked_func = wasm_native_resolve_symbol(
  2239. sub_module_name, function_name, declare_func_type, &linked_signature,
  2240. &linked_attachment, &linked_call_conv_raw);
  2241. if (linked_func) {
  2242. is_native_symbol = true;
  2243. }
  2244. #if WASM_ENABLE_MULTI_MODULE != 0
  2245. else {
  2246. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2247. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2248. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2249. error_buf_size);
  2250. if (!sub_module) {
  2251. return false;
  2252. }
  2253. }
  2254. linked_func = wasm_loader_resolve_function(
  2255. sub_module_name, function_name, declare_func_type, error_buf,
  2256. error_buf_size);
  2257. }
  2258. #endif
  2259. function->module_name = (char *)sub_module_name;
  2260. function->field_name = (char *)function_name;
  2261. function->func_type = declare_func_type;
  2262. /* func_ptr_linked is for native registered symbol */
  2263. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2264. function->signature = linked_signature;
  2265. function->attachment = linked_attachment;
  2266. function->call_conv_raw = linked_call_conv_raw;
  2267. #if WASM_ENABLE_MULTI_MODULE != 0
  2268. function->import_module = is_native_symbol ? NULL : sub_module;
  2269. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2270. #endif
  2271. return true;
  2272. fail:
  2273. return false;
  2274. }
  2275. static bool
  2276. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2277. uint32 error_buf_size)
  2278. {
  2279. if (max_size < init_size) {
  2280. set_error_buf(error_buf, error_buf_size,
  2281. "size minimum must not be greater than maximum");
  2282. return false;
  2283. }
  2284. return true;
  2285. }
  2286. static bool
  2287. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2288. WASMModule *parent_module, const char *sub_module_name,
  2289. const char *table_name, WASMTableImport *table,
  2290. char *error_buf, uint32 error_buf_size)
  2291. {
  2292. const uint8 *p = *p_buf, *p_end = buf_end;
  2293. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2294. declare_init_size = 0, declare_max_size = 0;
  2295. #if WASM_ENABLE_MULTI_MODULE != 0
  2296. WASMModule *sub_module = NULL;
  2297. WASMTable *linked_table = NULL;
  2298. #endif
  2299. #if WASM_ENABLE_GC != 0
  2300. WASMRefType ref_type;
  2301. bool need_ref_type_map;
  2302. #endif
  2303. #if WASM_ENABLE_GC == 0
  2304. CHECK_BUF(p, p_end, 1);
  2305. /* 0x70 or 0x6F */
  2306. declare_elem_type = read_uint8(p);
  2307. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2308. #if WASM_ENABLE_REF_TYPES != 0
  2309. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2310. #endif
  2311. ) {
  2312. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2313. return false;
  2314. }
  2315. #else /* else of WASM_ENABLE_GC == 0 */
  2316. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2317. &need_ref_type_map, &ref_type, false, error_buf,
  2318. error_buf_size)) {
  2319. return false;
  2320. }
  2321. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2322. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2323. return false;
  2324. }
  2325. declare_elem_type = ref_type.ref_type;
  2326. if (need_ref_type_map) {
  2327. if (!(table->elem_ref_type =
  2328. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2329. error_buf, error_buf_size))) {
  2330. return false;
  2331. }
  2332. }
  2333. #if TRACE_WASM_LOADER != 0
  2334. os_printf("import table type: ");
  2335. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2336. os_printf("\n");
  2337. #endif
  2338. #endif /* end of WASM_ENABLE_GC == 0 */
  2339. read_leb_uint32(p, p_end, declare_max_size_flag);
  2340. if (declare_max_size_flag > 1) {
  2341. set_error_buf(error_buf, error_buf_size, "integer too large");
  2342. return false;
  2343. }
  2344. read_leb_uint32(p, p_end, declare_init_size);
  2345. if (declare_max_size_flag) {
  2346. read_leb_uint32(p, p_end, declare_max_size);
  2347. if (!check_table_max_size(declare_init_size, declare_max_size,
  2348. error_buf, error_buf_size))
  2349. return false;
  2350. }
  2351. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2352. &declare_max_size);
  2353. *p_buf = p;
  2354. #if WASM_ENABLE_MULTI_MODULE != 0
  2355. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2356. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2357. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2358. error_buf_size);
  2359. if (!sub_module) {
  2360. return false;
  2361. }
  2362. linked_table = wasm_loader_resolve_table(
  2363. sub_module_name, table_name, declare_init_size, declare_max_size,
  2364. error_buf, error_buf_size);
  2365. if (!linked_table) {
  2366. return false;
  2367. }
  2368. /* reset with linked table limit */
  2369. declare_elem_type = linked_table->elem_type;
  2370. declare_init_size = linked_table->init_size;
  2371. declare_max_size = linked_table->max_size;
  2372. declare_max_size_flag = linked_table->flags;
  2373. table->import_table_linked = linked_table;
  2374. table->import_module = sub_module;
  2375. }
  2376. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2377. /* (table (export "table") 10 20 funcref) */
  2378. /* we need this section working in wamrc */
  2379. if (!strcmp("spectest", sub_module_name)) {
  2380. const uint32 spectest_table_init_size = 10;
  2381. const uint32 spectest_table_max_size = 20;
  2382. if (strcmp("table", table_name)) {
  2383. set_error_buf(error_buf, error_buf_size,
  2384. "incompatible import type or unknown import");
  2385. return false;
  2386. }
  2387. if (declare_init_size > spectest_table_init_size
  2388. || declare_max_size < spectest_table_max_size) {
  2389. set_error_buf(error_buf, error_buf_size,
  2390. "incompatible import type");
  2391. return false;
  2392. }
  2393. declare_init_size = spectest_table_init_size;
  2394. declare_max_size = spectest_table_max_size;
  2395. }
  2396. /* now we believe all declaration are ok */
  2397. table->elem_type = declare_elem_type;
  2398. table->init_size = declare_init_size;
  2399. table->flags = declare_max_size_flag;
  2400. table->max_size = declare_max_size;
  2401. #if WASM_ENABLE_WAMR_COMPILER != 0
  2402. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2403. parent_module->is_ref_types_used = true;
  2404. #endif
  2405. (void)parent_module;
  2406. return true;
  2407. fail:
  2408. return false;
  2409. }
  2410. static bool
  2411. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2412. uint32 error_buf_size)
  2413. {
  2414. uint32 default_max_size =
  2415. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2416. if (!is_memory64 && init_size > default_max_size) {
  2417. set_error_buf(error_buf, error_buf_size,
  2418. "memory size must be at most 65536 pages (4GiB)");
  2419. return false;
  2420. }
  2421. #if WASM_ENABLE_MEMORY64 != 0
  2422. else if (is_memory64 && init_size > default_max_size) {
  2423. set_error_buf(
  2424. error_buf, error_buf_size,
  2425. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2426. return false;
  2427. }
  2428. #endif
  2429. return true;
  2430. }
  2431. static bool
  2432. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2433. char *error_buf, uint32 error_buf_size)
  2434. {
  2435. uint32 default_max_size =
  2436. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2437. if (max_size < init_size) {
  2438. set_error_buf(error_buf, error_buf_size,
  2439. "size minimum must not be greater than maximum");
  2440. return false;
  2441. }
  2442. if (!is_memory64 && max_size > default_max_size) {
  2443. set_error_buf(error_buf, error_buf_size,
  2444. "memory size must be at most 65536 pages (4GiB)");
  2445. return false;
  2446. }
  2447. #if WASM_ENABLE_MEMORY64 != 0
  2448. else if (is_memory64 && max_size > default_max_size) {
  2449. set_error_buf(
  2450. error_buf, error_buf_size,
  2451. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2452. return false;
  2453. }
  2454. #endif
  2455. return true;
  2456. }
  2457. static bool
  2458. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2459. WASMModule *parent_module, const char *sub_module_name,
  2460. const char *memory_name, WASMMemoryImport *memory,
  2461. char *error_buf, uint32 error_buf_size)
  2462. {
  2463. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2464. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2465. uint32 pool_size = wasm_runtime_memory_pool_size();
  2466. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2467. / DEFAULT_NUM_BYTES_PER_PAGE;
  2468. #else
  2469. uint32 max_page_count;
  2470. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2471. uint32 mem_flag = 0;
  2472. bool is_memory64 = false;
  2473. uint32 declare_init_page_count = 0;
  2474. uint32 declare_max_page_count = 0;
  2475. #if WASM_ENABLE_MULTI_MODULE != 0
  2476. WASMModule *sub_module = NULL;
  2477. WASMMemory *linked_memory = NULL;
  2478. #endif
  2479. p_org = p;
  2480. read_leb_uint32(p, p_end, mem_flag);
  2481. is_memory64 = mem_flag & MEMORY64_FLAG;
  2482. if (p - p_org > 1) {
  2483. LOG_VERBOSE("integer representation too long");
  2484. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2485. return false;
  2486. }
  2487. if (!wasm_memory_check_flags(mem_flag, error_buf, error_buf_size, false)) {
  2488. return false;
  2489. }
  2490. read_leb_uint32(p, p_end, declare_init_page_count);
  2491. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2492. error_buf_size)) {
  2493. return false;
  2494. }
  2495. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2496. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2497. #endif
  2498. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2499. read_leb_uint32(p, p_end, declare_max_page_count);
  2500. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2501. declare_max_page_count, error_buf,
  2502. error_buf_size)) {
  2503. return false;
  2504. }
  2505. if (declare_max_page_count > max_page_count) {
  2506. declare_max_page_count = max_page_count;
  2507. }
  2508. }
  2509. else {
  2510. /* Limit the maximum memory size to max_page_count */
  2511. declare_max_page_count = max_page_count;
  2512. }
  2513. #if WASM_ENABLE_MULTI_MODULE != 0
  2514. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2515. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2516. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2517. error_buf_size);
  2518. if (!sub_module) {
  2519. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2520. /* Avoid memory import failure when wasi-threads is enabled
  2521. and the memory is shared */
  2522. if (!(mem_flag & SHARED_MEMORY_FLAG))
  2523. return false;
  2524. #else
  2525. return false;
  2526. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2527. }
  2528. else {
  2529. linked_memory = wasm_loader_resolve_memory(
  2530. sub_module_name, memory_name, declare_init_page_count,
  2531. declare_max_page_count, error_buf, error_buf_size);
  2532. if (!linked_memory) {
  2533. return false;
  2534. }
  2535. /**
  2536. * reset with linked memory limit
  2537. */
  2538. memory->import_module = sub_module;
  2539. memory->import_memory_linked = linked_memory;
  2540. declare_init_page_count = linked_memory->init_page_count;
  2541. declare_max_page_count = linked_memory->max_page_count;
  2542. }
  2543. }
  2544. #endif
  2545. /* (memory (export "memory") 1 2) */
  2546. if (!strcmp("spectest", sub_module_name)) {
  2547. uint32 spectest_memory_init_page = 1;
  2548. uint32 spectest_memory_max_page = 2;
  2549. if (strcmp("memory", memory_name)) {
  2550. set_error_buf(error_buf, error_buf_size,
  2551. "incompatible import type or unknown import");
  2552. return false;
  2553. }
  2554. if (declare_init_page_count > spectest_memory_init_page
  2555. || declare_max_page_count < spectest_memory_max_page) {
  2556. set_error_buf(error_buf, error_buf_size,
  2557. "incompatible import type");
  2558. return false;
  2559. }
  2560. declare_init_page_count = spectest_memory_init_page;
  2561. declare_max_page_count = spectest_memory_max_page;
  2562. }
  2563. /* now we believe all declaration are ok */
  2564. memory->flags = mem_flag;
  2565. memory->init_page_count = declare_init_page_count;
  2566. memory->max_page_count = declare_max_page_count;
  2567. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2568. *p_buf = p;
  2569. (void)parent_module;
  2570. return true;
  2571. fail:
  2572. return false;
  2573. }
  2574. #if WASM_ENABLE_TAGS != 0
  2575. static bool
  2576. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2577. const WASMModule *parent_module, /* this module ! */
  2578. const char *sub_module_name, const char *tag_name,
  2579. WASMTagImport *tag, /* structure to fill */
  2580. char *error_buf, uint32 error_buf_size)
  2581. {
  2582. /* attribute and type of the import statement */
  2583. uint8 declare_tag_attribute;
  2584. uint32 declare_type_index;
  2585. const uint8 *p = *p_buf, *p_end = buf_end;
  2586. #if WASM_ENABLE_MULTI_MODULE != 0
  2587. WASMModule *sub_module = NULL;
  2588. #endif
  2589. /* get the one byte attribute */
  2590. CHECK_BUF(p, p_end, 1);
  2591. declare_tag_attribute = read_uint8(p);
  2592. if (declare_tag_attribute != 0) {
  2593. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2594. goto fail;
  2595. }
  2596. /* get type */
  2597. read_leb_uint32(p, p_end, declare_type_index);
  2598. /* compare against module->types */
  2599. if (declare_type_index >= parent_module->type_count) {
  2600. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2601. goto fail;
  2602. }
  2603. WASMFuncType *declare_tag_type =
  2604. (WASMFuncType *)parent_module->types[declare_type_index];
  2605. /* check, that the type of the declared tag returns void */
  2606. if (declare_tag_type->result_count != 0) {
  2607. set_error_buf(error_buf, error_buf_size,
  2608. "tag type signature does not return void");
  2609. goto fail;
  2610. }
  2611. #if WASM_ENABLE_MULTI_MODULE != 0
  2612. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2613. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2614. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2615. error_buf_size);
  2616. if (!sub_module) {
  2617. return false;
  2618. }
  2619. /* wasm_loader_resolve_tag checks, that the imported tag
  2620. * and the declared tag have the same type
  2621. */
  2622. uint32 linked_tag_index = 0;
  2623. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2624. sub_module_name, tag_name, declare_tag_type,
  2625. &linked_tag_index /* out */, error_buf, error_buf_size);
  2626. if (linked_tag) {
  2627. tag->import_module = sub_module;
  2628. tag->import_tag_linked = linked_tag;
  2629. tag->import_tag_index_linked = linked_tag_index;
  2630. }
  2631. }
  2632. #endif
  2633. /* store to module tag declarations */
  2634. tag->attribute = declare_tag_attribute;
  2635. tag->type = declare_type_index;
  2636. tag->module_name = (char *)sub_module_name;
  2637. tag->field_name = (char *)tag_name;
  2638. tag->tag_type = declare_tag_type;
  2639. *p_buf = p;
  2640. (void)parent_module;
  2641. LOG_VERBOSE("Load tag import success\n");
  2642. return true;
  2643. fail:
  2644. return false;
  2645. }
  2646. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2647. static bool
  2648. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2649. WASMModule *parent_module, char *sub_module_name,
  2650. char *global_name, WASMGlobalImport *global, char *error_buf,
  2651. uint32 error_buf_size)
  2652. {
  2653. const uint8 *p = *p_buf, *p_end = buf_end;
  2654. uint8 declare_type = 0;
  2655. uint8 declare_mutable = 0;
  2656. #if WASM_ENABLE_MULTI_MODULE != 0
  2657. WASMModule *sub_module = NULL;
  2658. WASMGlobal *linked_global = NULL;
  2659. #endif
  2660. #if WASM_ENABLE_GC != 0
  2661. WASMRefType ref_type;
  2662. bool need_ref_type_map;
  2663. #endif
  2664. bool ret = false;
  2665. #if WASM_ENABLE_GC == 0
  2666. CHECK_BUF(p, p_end, 2);
  2667. declare_type = read_uint8(p);
  2668. declare_mutable = read_uint8(p);
  2669. #else
  2670. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2671. &need_ref_type_map, &ref_type, false, error_buf,
  2672. error_buf_size)) {
  2673. return false;
  2674. }
  2675. declare_type = ref_type.ref_type;
  2676. if (need_ref_type_map) {
  2677. if (!(global->ref_type =
  2678. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2679. error_buf, error_buf_size))) {
  2680. return false;
  2681. }
  2682. }
  2683. #if TRACE_WASM_LOADER != 0
  2684. os_printf("import global type: ");
  2685. wasm_dump_value_type(declare_type, global->ref_type);
  2686. os_printf("\n");
  2687. #endif
  2688. CHECK_BUF(p, p_end, 1);
  2689. declare_mutable = read_uint8(p);
  2690. #endif /* end of WASM_ENABLE_GC == 0 */
  2691. *p_buf = p;
  2692. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2693. return false;
  2694. }
  2695. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2696. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2697. global);
  2698. if (ret) {
  2699. if (global->type.val_type != declare_type
  2700. || global->type.is_mutable != declare_mutable) {
  2701. set_error_buf(error_buf, error_buf_size,
  2702. "incompatible import type");
  2703. return false;
  2704. }
  2705. global->is_linked = true;
  2706. }
  2707. #endif
  2708. #if WASM_ENABLE_MULTI_MODULE != 0
  2709. if (!global->is_linked
  2710. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2711. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2712. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2713. error_buf_size);
  2714. if (!sub_module) {
  2715. return false;
  2716. }
  2717. /* check sub modules */
  2718. linked_global = wasm_loader_resolve_global(
  2719. sub_module_name, global_name, declare_type, declare_mutable,
  2720. error_buf, error_buf_size);
  2721. if (linked_global) {
  2722. global->import_module = sub_module;
  2723. global->import_global_linked = linked_global;
  2724. global->is_linked = true;
  2725. }
  2726. }
  2727. #endif
  2728. global->module_name = sub_module_name;
  2729. global->field_name = global_name;
  2730. global->type.val_type = declare_type;
  2731. global->type.is_mutable = (declare_mutable == 1);
  2732. #if WASM_ENABLE_WAMR_COMPILER != 0
  2733. if (global->type.val_type == VALUE_TYPE_V128)
  2734. parent_module->is_simd_used = true;
  2735. else if (global->type.val_type == VALUE_TYPE_EXTERNREF)
  2736. parent_module->is_ref_types_used = true;
  2737. #endif
  2738. (void)parent_module;
  2739. (void)ret;
  2740. return true;
  2741. fail:
  2742. return false;
  2743. }
  2744. static bool
  2745. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2746. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2747. {
  2748. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2749. #if WASM_ENABLE_GC != 0
  2750. WASMRefType ref_type;
  2751. bool need_ref_type_map;
  2752. #endif
  2753. #if WASM_ENABLE_GC == 0
  2754. CHECK_BUF(p, p_end, 1);
  2755. /* 0x70 or 0x6F */
  2756. table->elem_type = read_uint8(p);
  2757. if (VALUE_TYPE_FUNCREF != table->elem_type
  2758. #if WASM_ENABLE_REF_TYPES != 0
  2759. && VALUE_TYPE_EXTERNREF != table->elem_type
  2760. #endif
  2761. ) {
  2762. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2763. return false;
  2764. }
  2765. #else /* else of WASM_ENABLE_GC == 0 */
  2766. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2767. &need_ref_type_map, &ref_type, false, error_buf,
  2768. error_buf_size)) {
  2769. return false;
  2770. }
  2771. table->elem_type = ref_type.ref_type;
  2772. if (need_ref_type_map) {
  2773. if (!(table->elem_ref_type =
  2774. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2775. error_buf_size))) {
  2776. return false;
  2777. }
  2778. }
  2779. #if TRACE_WASM_LOADER != 0
  2780. os_printf("table type: ");
  2781. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2782. os_printf("\n");
  2783. #endif
  2784. #endif /* end of WASM_ENABLE_GC == 0 */
  2785. p_org = p;
  2786. read_leb_uint32(p, p_end, table->flags);
  2787. #if WASM_ENABLE_SHARED_MEMORY == 0
  2788. if (p - p_org > 1) {
  2789. set_error_buf(error_buf, error_buf_size,
  2790. "integer representation too long");
  2791. return false;
  2792. }
  2793. if (table->flags > 1) {
  2794. set_error_buf(error_buf, error_buf_size, "integer too large");
  2795. return false;
  2796. }
  2797. #else
  2798. if (p - p_org > 1) {
  2799. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2800. return false;
  2801. }
  2802. if (table->flags == 2) {
  2803. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2804. return false;
  2805. }
  2806. if (table->flags > 1) {
  2807. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2808. return false;
  2809. }
  2810. #endif
  2811. read_leb_uint32(p, p_end, table->init_size);
  2812. if (table->flags) {
  2813. read_leb_uint32(p, p_end, table->max_size);
  2814. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2815. error_buf_size))
  2816. return false;
  2817. }
  2818. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2819. #if WASM_ENABLE_WAMR_COMPILER != 0
  2820. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2821. module->is_ref_types_used = true;
  2822. #endif
  2823. *p_buf = p;
  2824. return true;
  2825. fail:
  2826. return false;
  2827. }
  2828. static bool
  2829. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2830. char *error_buf, uint32 error_buf_size)
  2831. {
  2832. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2833. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2834. uint32 pool_size = wasm_runtime_memory_pool_size();
  2835. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2836. / DEFAULT_NUM_BYTES_PER_PAGE;
  2837. #else
  2838. uint32 max_page_count;
  2839. #endif
  2840. bool is_memory64 = false;
  2841. p_org = p;
  2842. read_leb_uint32(p, p_end, memory->flags);
  2843. is_memory64 = memory->flags & MEMORY64_FLAG;
  2844. if (p - p_org > 1) {
  2845. LOG_VERBOSE("integer representation too long");
  2846. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2847. return false;
  2848. }
  2849. if (!wasm_memory_check_flags(memory->flags, error_buf, error_buf_size,
  2850. false)) {
  2851. return false;
  2852. }
  2853. read_leb_uint32(p, p_end, memory->init_page_count);
  2854. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2855. error_buf_size))
  2856. return false;
  2857. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2858. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2859. #endif
  2860. if (memory->flags & 1) {
  2861. read_leb_uint32(p, p_end, memory->max_page_count);
  2862. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2863. memory->max_page_count, error_buf,
  2864. error_buf_size))
  2865. return false;
  2866. if (memory->max_page_count > max_page_count)
  2867. memory->max_page_count = max_page_count;
  2868. }
  2869. else {
  2870. /* Limit the maximum memory size to max_page_count */
  2871. memory->max_page_count = max_page_count;
  2872. }
  2873. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2874. *p_buf = p;
  2875. return true;
  2876. fail:
  2877. return false;
  2878. }
  2879. static bool
  2880. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2881. bool is_load_from_file_buf, char *error_buf,
  2882. uint32 error_buf_size)
  2883. {
  2884. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2885. uint32 import_count, name_len, type_index, i, u32, flags;
  2886. uint64 total_size;
  2887. WASMImport *import;
  2888. WASMImport *import_functions = NULL, *import_tables = NULL;
  2889. WASMImport *import_memories = NULL, *import_globals = NULL;
  2890. #if WASM_ENABLE_TAGS != 0
  2891. WASMImport *import_tags = NULL;
  2892. #endif
  2893. char *sub_module_name, *field_name;
  2894. uint8 u8, kind, global_type;
  2895. read_leb_uint32(p, p_end, import_count);
  2896. if (import_count) {
  2897. module->import_count = import_count;
  2898. total_size = sizeof(WASMImport) * (uint64)import_count;
  2899. if (!(module->imports =
  2900. loader_malloc(total_size, error_buf, error_buf_size))) {
  2901. return false;
  2902. }
  2903. p_old = p;
  2904. /* Scan firstly to get import count of each type */
  2905. for (i = 0; i < import_count; i++) {
  2906. /* module name */
  2907. read_leb_uint32(p, p_end, name_len);
  2908. CHECK_BUF(p, p_end, name_len);
  2909. p += name_len;
  2910. /* field name */
  2911. read_leb_uint32(p, p_end, name_len);
  2912. CHECK_BUF(p, p_end, name_len);
  2913. p += name_len;
  2914. CHECK_BUF(p, p_end, 1);
  2915. /* 0x00/0x01/0x02/0x03/0x04 */
  2916. kind = read_uint8(p);
  2917. switch (kind) {
  2918. case IMPORT_KIND_FUNC: /* import function */
  2919. read_leb_uint32(p, p_end, type_index);
  2920. module->import_function_count++;
  2921. break;
  2922. case IMPORT_KIND_TABLE: /* import table */
  2923. CHECK_BUF(p, p_end, 1);
  2924. /* 0x70 */
  2925. u8 = read_uint8(p);
  2926. read_leb_uint32(p, p_end, flags);
  2927. read_leb_uint32(p, p_end, u32);
  2928. if (flags & 1)
  2929. read_leb_uint32(p, p_end, u32);
  2930. module->import_table_count++;
  2931. if (module->import_table_count > 1) {
  2932. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2933. set_error_buf(error_buf, error_buf_size,
  2934. "multiple tables");
  2935. return false;
  2936. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2937. module->is_ref_types_used = true;
  2938. #endif
  2939. }
  2940. break;
  2941. case IMPORT_KIND_MEMORY: /* import memory */
  2942. read_leb_uint32(p, p_end, flags);
  2943. read_leb_uint32(p, p_end, u32);
  2944. if (flags & 1)
  2945. read_leb_uint32(p, p_end, u32);
  2946. module->import_memory_count++;
  2947. if (module->import_memory_count > 1) {
  2948. set_error_buf(error_buf, error_buf_size,
  2949. "multiple memories");
  2950. return false;
  2951. }
  2952. break;
  2953. #if WASM_ENABLE_TAGS != 0
  2954. case IMPORT_KIND_TAG: /* import tags */
  2955. /* it only counts the number of tags to import */
  2956. module->import_tag_count++;
  2957. CHECK_BUF(p, p_end, 1);
  2958. u8 = read_uint8(p);
  2959. read_leb_uint32(p, p_end, type_index);
  2960. break;
  2961. #endif
  2962. case IMPORT_KIND_GLOBAL: /* import global */
  2963. #if WASM_ENABLE_GC != 0
  2964. /* valtype */
  2965. CHECK_BUF(p, p_end, 1);
  2966. global_type = read_uint8(p);
  2967. if (wasm_is_type_multi_byte_type(global_type)) {
  2968. int32 heap_type;
  2969. read_leb_int32(p, p_end, heap_type);
  2970. (void)heap_type;
  2971. }
  2972. /* mutability */
  2973. CHECK_BUF(p, p_end, 1);
  2974. p += 1;
  2975. #else
  2976. CHECK_BUF(p, p_end, 2);
  2977. p += 2;
  2978. #endif
  2979. (void)global_type;
  2980. module->import_global_count++;
  2981. break;
  2982. default:
  2983. set_error_buf(error_buf, error_buf_size,
  2984. "invalid import kind");
  2985. return false;
  2986. }
  2987. }
  2988. if (module->import_function_count)
  2989. import_functions = module->import_functions = module->imports;
  2990. if (module->import_table_count)
  2991. import_tables = module->import_tables =
  2992. module->imports + module->import_function_count;
  2993. if (module->import_memory_count)
  2994. import_memories = module->import_memories =
  2995. module->imports + module->import_function_count
  2996. + module->import_table_count;
  2997. #if WASM_ENABLE_TAGS != 0
  2998. if (module->import_tag_count)
  2999. import_tags = module->import_tags =
  3000. module->imports + module->import_function_count
  3001. + module->import_table_count + module->import_memory_count;
  3002. if (module->import_global_count)
  3003. import_globals = module->import_globals =
  3004. module->imports + module->import_function_count
  3005. + module->import_table_count + module->import_memory_count
  3006. + module->import_tag_count;
  3007. #else
  3008. if (module->import_global_count)
  3009. import_globals = module->import_globals =
  3010. module->imports + module->import_function_count
  3011. + module->import_table_count + module->import_memory_count;
  3012. #endif
  3013. p = p_old;
  3014. /* Scan again to resolve the data */
  3015. for (i = 0; i < import_count; i++) {
  3016. /* load module name */
  3017. read_leb_uint32(p, p_end, name_len);
  3018. CHECK_BUF(p, p_end, name_len);
  3019. if (!(sub_module_name = wasm_const_str_list_insert(
  3020. p, name_len, module, is_load_from_file_buf, error_buf,
  3021. error_buf_size))) {
  3022. return false;
  3023. }
  3024. p += name_len;
  3025. /* load field name */
  3026. read_leb_uint32(p, p_end, name_len);
  3027. CHECK_BUF(p, p_end, name_len);
  3028. if (!(field_name = wasm_const_str_list_insert(
  3029. p, name_len, module, is_load_from_file_buf, error_buf,
  3030. error_buf_size))) {
  3031. return false;
  3032. }
  3033. p += name_len;
  3034. CHECK_BUF(p, p_end, 1);
  3035. /* 0x00/0x01/0x02/0x03/0x4 */
  3036. kind = read_uint8(p);
  3037. switch (kind) {
  3038. case IMPORT_KIND_FUNC: /* import function */
  3039. bh_assert(import_functions);
  3040. import = import_functions++;
  3041. if (!load_function_import(
  3042. &p, p_end, module, sub_module_name, field_name,
  3043. &import->u.function, error_buf, error_buf_size)) {
  3044. return false;
  3045. }
  3046. break;
  3047. case IMPORT_KIND_TABLE: /* import table */
  3048. bh_assert(import_tables);
  3049. import = import_tables++;
  3050. if (!load_table_import(&p, p_end, module, sub_module_name,
  3051. field_name, &import->u.table,
  3052. error_buf, error_buf_size)) {
  3053. LOG_DEBUG("can not import such a table (%s,%s)",
  3054. sub_module_name, field_name);
  3055. return false;
  3056. }
  3057. break;
  3058. case IMPORT_KIND_MEMORY: /* import memory */
  3059. bh_assert(import_memories);
  3060. import = import_memories++;
  3061. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3062. field_name, &import->u.memory,
  3063. error_buf, error_buf_size)) {
  3064. return false;
  3065. }
  3066. break;
  3067. #if WASM_ENABLE_TAGS != 0
  3068. case IMPORT_KIND_TAG:
  3069. bh_assert(import_tags);
  3070. import = import_tags++;
  3071. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3072. field_name, &import->u.tag, error_buf,
  3073. error_buf_size)) {
  3074. return false;
  3075. }
  3076. break;
  3077. #endif
  3078. case IMPORT_KIND_GLOBAL: /* import global */
  3079. bh_assert(import_globals);
  3080. import = import_globals++;
  3081. if (!load_global_import(&p, p_end, module, sub_module_name,
  3082. field_name, &import->u.global,
  3083. error_buf, error_buf_size)) {
  3084. return false;
  3085. }
  3086. break;
  3087. default:
  3088. set_error_buf(error_buf, error_buf_size,
  3089. "invalid import kind");
  3090. return false;
  3091. }
  3092. import->kind = kind;
  3093. import->u.names.module_name = sub_module_name;
  3094. import->u.names.field_name = field_name;
  3095. }
  3096. #if WASM_ENABLE_LIBC_WASI != 0
  3097. import = module->import_functions;
  3098. for (i = 0; i < module->import_function_count; i++, import++) {
  3099. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3100. || !strcmp(import->u.names.module_name,
  3101. "wasi_snapshot_preview1")) {
  3102. module->import_wasi_api = true;
  3103. break;
  3104. }
  3105. }
  3106. #endif
  3107. }
  3108. if (p != p_end) {
  3109. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3110. return false;
  3111. }
  3112. LOG_VERBOSE("Load import section success.\n");
  3113. (void)u8;
  3114. (void)u32;
  3115. (void)type_index;
  3116. return true;
  3117. fail:
  3118. return false;
  3119. }
  3120. static bool
  3121. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3122. uint32 error_buf_size)
  3123. {
  3124. WASMFuncType *param_type = func->func_type;
  3125. uint32 param_count = param_type->param_count;
  3126. uint8 *param_types = param_type->types;
  3127. uint32 local_count = func->local_count;
  3128. uint8 *local_types = func->local_types;
  3129. uint32 i, local_offset = 0;
  3130. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3131. /*
  3132. * Only allocate memory when total_size is not 0,
  3133. * or the return value of malloc(0) might be NULL on some platforms,
  3134. * which causes wasm loader return false.
  3135. */
  3136. if (total_size > 0
  3137. && !(func->local_offsets =
  3138. loader_malloc(total_size, error_buf, error_buf_size))) {
  3139. return false;
  3140. }
  3141. for (i = 0; i < param_count; i++) {
  3142. func->local_offsets[i] = (uint16)local_offset;
  3143. local_offset += wasm_value_type_cell_num(param_types[i]);
  3144. }
  3145. for (i = 0; i < local_count; i++) {
  3146. func->local_offsets[param_count + i] = (uint16)local_offset;
  3147. local_offset += wasm_value_type_cell_num(local_types[i]);
  3148. }
  3149. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3150. return true;
  3151. }
  3152. static bool
  3153. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3154. const uint8 *buf_code, const uint8 *buf_code_end,
  3155. WASMModule *module, char *error_buf,
  3156. uint32 error_buf_size)
  3157. {
  3158. const uint8 *p = buf, *p_end = buf_end;
  3159. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3160. uint32 func_count;
  3161. uint64 total_size;
  3162. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3163. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3164. uint8 type;
  3165. WASMFunction *func;
  3166. #if WASM_ENABLE_GC != 0
  3167. bool need_ref_type_map;
  3168. WASMRefType ref_type;
  3169. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3170. #endif
  3171. read_leb_uint32(p, p_end, func_count);
  3172. if (buf_code)
  3173. read_leb_uint32(p_code, buf_code_end, code_count);
  3174. if (func_count != code_count) {
  3175. set_error_buf(error_buf, error_buf_size,
  3176. "function and code section have inconsistent lengths or "
  3177. "unexpected end");
  3178. return false;
  3179. }
  3180. if (func_count) {
  3181. module->function_count = func_count;
  3182. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3183. if (!(module->functions =
  3184. loader_malloc(total_size, error_buf, error_buf_size))) {
  3185. return false;
  3186. }
  3187. for (i = 0; i < func_count; i++) {
  3188. /* Resolve function type */
  3189. read_leb_uint32(p, p_end, type_index);
  3190. if (type_index >= module->type_count) {
  3191. set_error_buf(error_buf, error_buf_size, "unknown type");
  3192. return false;
  3193. }
  3194. #if WASM_ENABLE_GC != 0
  3195. type_index_org = type_index;
  3196. #endif
  3197. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3198. && WASM_ENABLE_GC == 0
  3199. type_index = wasm_get_smallest_type_idx(
  3200. module->types, module->type_count, type_index);
  3201. #endif
  3202. read_leb_uint32(p_code, buf_code_end, code_size);
  3203. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3204. set_error_buf(error_buf, error_buf_size,
  3205. "invalid function code size");
  3206. return false;
  3207. }
  3208. /* Resolve local set count */
  3209. p_code_end = p_code + code_size;
  3210. local_count = 0;
  3211. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3212. p_code_save = p_code;
  3213. #if WASM_ENABLE_GC != 0
  3214. ref_type_map_count = 0;
  3215. #endif
  3216. /* Calculate total local count */
  3217. for (j = 0; j < local_set_count; j++) {
  3218. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3219. if (sub_local_count > UINT32_MAX - local_count) {
  3220. set_error_buf(error_buf, error_buf_size, "too many locals");
  3221. return false;
  3222. }
  3223. #if WASM_ENABLE_GC == 0
  3224. CHECK_BUF(p_code, buf_code_end, 1);
  3225. /* 0x7F/0x7E/0x7D/0x7C */
  3226. type = read_uint8(p_code);
  3227. local_count += sub_local_count;
  3228. #if WASM_ENABLE_WAMR_COMPILER != 0
  3229. /* If any value's type is v128, mark the module as SIMD used */
  3230. if (type == VALUE_TYPE_V128)
  3231. module->is_simd_used = true;
  3232. #endif
  3233. #else
  3234. if (!resolve_value_type(&p_code, buf_code_end, module,
  3235. module->type_count, &need_ref_type_map,
  3236. &ref_type, false, error_buf,
  3237. error_buf_size)) {
  3238. return false;
  3239. }
  3240. local_count += sub_local_count;
  3241. if (need_ref_type_map)
  3242. ref_type_map_count += sub_local_count;
  3243. #endif
  3244. }
  3245. /* Alloc memory, layout: function structure + local types */
  3246. code_size = (uint32)(p_code_end - p_code);
  3247. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3248. if (!(func = module->functions[i] =
  3249. loader_malloc(total_size, error_buf, error_buf_size))) {
  3250. return false;
  3251. }
  3252. #if WASM_ENABLE_GC != 0
  3253. if (ref_type_map_count > 0) {
  3254. total_size =
  3255. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3256. if (!(func->local_ref_type_maps = loader_malloc(
  3257. total_size, error_buf, error_buf_size))) {
  3258. return false;
  3259. }
  3260. func->local_ref_type_map_count = ref_type_map_count;
  3261. }
  3262. #endif
  3263. /* Set function type, local count, code size and code body */
  3264. func->func_type = (WASMFuncType *)module->types[type_index];
  3265. func->local_count = local_count;
  3266. if (local_count > 0)
  3267. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3268. func->code_size = code_size;
  3269. /*
  3270. * we shall make a copy of code body [p_code, p_code + code_size]
  3271. * when we are worrying about inappropriate releasing behaviour.
  3272. * all code bodies are actually in a buffer which user allocates in
  3273. * his embedding environment and we don't have power on them.
  3274. * it will be like:
  3275. * code_body_cp = malloc(code_size);
  3276. * memcpy(code_body_cp, p_code, code_size);
  3277. * func->code = code_body_cp;
  3278. */
  3279. func->code = (uint8 *)p_code;
  3280. #if WASM_ENABLE_GC != 0
  3281. func->type_idx = type_index_org;
  3282. #endif
  3283. #if WASM_ENABLE_GC != 0
  3284. t = 0;
  3285. #endif
  3286. /* Load each local type */
  3287. p_code = p_code_save;
  3288. local_type_index = 0;
  3289. for (j = 0; j < local_set_count; j++) {
  3290. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3291. /* Note: sub_local_count is allowed to be 0 */
  3292. if (local_type_index > UINT32_MAX - sub_local_count
  3293. || local_type_index + sub_local_count > local_count) {
  3294. set_error_buf(error_buf, error_buf_size,
  3295. "invalid local count");
  3296. return false;
  3297. }
  3298. #if WASM_ENABLE_GC == 0
  3299. CHECK_BUF(p_code, buf_code_end, 1);
  3300. /* 0x7F/0x7E/0x7D/0x7C */
  3301. type = read_uint8(p_code);
  3302. if (!is_value_type(type)) {
  3303. if (type == VALUE_TYPE_V128)
  3304. set_error_buf(error_buf, error_buf_size,
  3305. "v128 value type requires simd feature");
  3306. else if (type == VALUE_TYPE_FUNCREF
  3307. || type == VALUE_TYPE_EXTERNREF)
  3308. set_error_buf(error_buf, error_buf_size,
  3309. "ref value type requires "
  3310. "reference types feature");
  3311. else
  3312. set_error_buf_v(error_buf, error_buf_size,
  3313. "invalid local type 0x%02X", type);
  3314. return false;
  3315. }
  3316. #else
  3317. if (!resolve_value_type(&p_code, buf_code_end, module,
  3318. module->type_count, &need_ref_type_map,
  3319. &ref_type, false, error_buf,
  3320. error_buf_size)) {
  3321. return false;
  3322. }
  3323. if (need_ref_type_map) {
  3324. WASMRefType *ref_type_tmp;
  3325. if (!(ref_type_tmp = reftype_set_insert(
  3326. module->ref_type_set, &ref_type, error_buf,
  3327. error_buf_size))) {
  3328. return false;
  3329. }
  3330. for (k = 0; k < sub_local_count; k++) {
  3331. func->local_ref_type_maps[t + k].ref_type =
  3332. ref_type_tmp;
  3333. func->local_ref_type_maps[t + k].index =
  3334. local_type_index + k;
  3335. }
  3336. t += sub_local_count;
  3337. }
  3338. type = ref_type.ref_type;
  3339. #endif
  3340. for (k = 0; k < sub_local_count; k++) {
  3341. func->local_types[local_type_index++] = type;
  3342. }
  3343. #if WASM_ENABLE_WAMR_COMPILER != 0
  3344. if (type == VALUE_TYPE_V128)
  3345. module->is_simd_used = true;
  3346. else if (type == VALUE_TYPE_FUNCREF
  3347. || type == VALUE_TYPE_EXTERNREF)
  3348. module->is_ref_types_used = true;
  3349. #endif
  3350. }
  3351. bh_assert(local_type_index == func->local_count);
  3352. #if WASM_ENABLE_GC != 0
  3353. bh_assert(t == func->local_ref_type_map_count);
  3354. #if TRACE_WASM_LOADER != 0
  3355. os_printf("func %u, local types: [", i);
  3356. k = 0;
  3357. for (j = 0; j < func->local_count; j++) {
  3358. WASMRefType *ref_type_tmp = NULL;
  3359. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3360. bh_assert(j == func->local_ref_type_maps[k].index);
  3361. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3362. }
  3363. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3364. if (j < func->local_count - 1)
  3365. os_printf(" ");
  3366. }
  3367. os_printf("]\n");
  3368. #endif
  3369. #endif
  3370. func->param_cell_num = func->func_type->param_cell_num;
  3371. func->ret_cell_num = func->func_type->ret_cell_num;
  3372. local_cell_num =
  3373. wasm_get_cell_num(func->local_types, func->local_count);
  3374. if (local_cell_num > UINT16_MAX) {
  3375. set_error_buf(error_buf, error_buf_size,
  3376. "local count too large");
  3377. return false;
  3378. }
  3379. func->local_cell_num = (uint16)local_cell_num;
  3380. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3381. return false;
  3382. p_code = p_code_end;
  3383. }
  3384. }
  3385. if (p != p_end) {
  3386. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3387. return false;
  3388. }
  3389. LOG_VERBOSE("Load function section success.\n");
  3390. return true;
  3391. fail:
  3392. return false;
  3393. }
  3394. static bool
  3395. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3396. char *error_buf, uint32 error_buf_size)
  3397. {
  3398. const uint8 *p = buf, *p_end = buf_end;
  3399. uint32 table_count, i;
  3400. uint64 total_size;
  3401. WASMTable *table;
  3402. read_leb_uint32(p, p_end, table_count);
  3403. if (module->import_table_count + table_count > 1) {
  3404. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3405. /* a total of one table is allowed */
  3406. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3407. return false;
  3408. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3409. module->is_ref_types_used = true;
  3410. #endif
  3411. }
  3412. if (table_count) {
  3413. module->table_count = table_count;
  3414. total_size = sizeof(WASMTable) * (uint64)table_count;
  3415. if (!(module->tables =
  3416. loader_malloc(total_size, error_buf, error_buf_size))) {
  3417. return false;
  3418. }
  3419. /* load each table */
  3420. table = module->tables;
  3421. for (i = 0; i < table_count; i++, table++) {
  3422. #if WASM_ENABLE_GC != 0
  3423. uint8 flag;
  3424. bool has_init = false;
  3425. CHECK_BUF(buf, buf_end, 1);
  3426. flag = read_uint8(p);
  3427. if (flag == TABLE_INIT_EXPR_FLAG) {
  3428. CHECK_BUF(buf, buf_end, 1);
  3429. flag = read_uint8(p);
  3430. if (flag != 0x00) {
  3431. set_error_buf(error_buf, error_buf_size,
  3432. "invalid leading bytes for table");
  3433. return false;
  3434. }
  3435. has_init = true;
  3436. }
  3437. else {
  3438. p--;
  3439. }
  3440. #endif /* end of WASM_ENABLE_GC != 0 */
  3441. if (!load_table(&p, p_end, module, table, error_buf,
  3442. error_buf_size))
  3443. return false;
  3444. #if WASM_ENABLE_GC != 0
  3445. if (has_init) {
  3446. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3447. table->elem_type, table->elem_ref_type,
  3448. error_buf, error_buf_size))
  3449. return false;
  3450. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3451. && table->init_expr.init_expr_type
  3452. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3453. set_error_buf(
  3454. error_buf, error_buf_size,
  3455. "unsupported initializer expression for table");
  3456. return false;
  3457. }
  3458. }
  3459. else {
  3460. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3461. set_error_buf(
  3462. error_buf, error_buf_size,
  3463. "type mismatch: non-nullable table without init expr");
  3464. return false;
  3465. }
  3466. }
  3467. #endif /* end of WASM_ENABLE_GC != 0 */
  3468. #if WASM_ENABLE_WAMR_COMPILER != 0
  3469. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3470. module->is_ref_types_used = true;
  3471. #endif
  3472. }
  3473. }
  3474. if (p != p_end) {
  3475. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3476. return false;
  3477. }
  3478. LOG_VERBOSE("Load table section success.\n");
  3479. return true;
  3480. fail:
  3481. return false;
  3482. }
  3483. static bool
  3484. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3485. char *error_buf, uint32 error_buf_size)
  3486. {
  3487. const uint8 *p = buf, *p_end = buf_end;
  3488. uint32 memory_count, i;
  3489. uint64 total_size;
  3490. WASMMemory *memory;
  3491. read_leb_uint32(p, p_end, memory_count);
  3492. /* a total of one memory is allowed */
  3493. if (module->import_memory_count + memory_count > 1) {
  3494. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3495. return false;
  3496. }
  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. module->global_count = 0;
  3534. if (global_count) {
  3535. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3536. if (!(module->globals =
  3537. loader_malloc(total_size, error_buf, error_buf_size))) {
  3538. return false;
  3539. }
  3540. global = module->globals;
  3541. for (i = 0; i < global_count; i++, global++) {
  3542. #if WASM_ENABLE_GC == 0
  3543. CHECK_BUF(p, p_end, 2);
  3544. global->type.val_type = read_uint8(p);
  3545. mutable = read_uint8(p);
  3546. #else
  3547. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3548. &need_ref_type_map, &ref_type, false,
  3549. error_buf, error_buf_size)) {
  3550. return false;
  3551. }
  3552. global->type.val_type = ref_type.ref_type;
  3553. CHECK_BUF(p, p_end, 1);
  3554. mutable = read_uint8(p);
  3555. #endif /* end of WASM_ENABLE_GC */
  3556. #if WASM_ENABLE_WAMR_COMPILER != 0
  3557. if (global->type.val_type == VALUE_TYPE_V128)
  3558. module->is_simd_used = true;
  3559. else if (global->type.val_type == VALUE_TYPE_FUNCREF
  3560. || global->type.val_type == VALUE_TYPE_EXTERNREF)
  3561. module->is_ref_types_used = true;
  3562. #endif
  3563. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3564. return false;
  3565. }
  3566. global->type.is_mutable = mutable ? true : false;
  3567. /* initialize expression */
  3568. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3569. global->type.val_type,
  3570. #if WASM_ENABLE_GC == 0
  3571. NULL,
  3572. #else
  3573. &ref_type,
  3574. #endif
  3575. error_buf, error_buf_size))
  3576. return false;
  3577. #if WASM_ENABLE_GC != 0
  3578. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3579. uint8 global_type;
  3580. WASMRefType *global_ref_type;
  3581. uint32 global_idx = global->init_expr.u.global_index;
  3582. if (global->init_expr.u.global_index
  3583. >= module->import_global_count + i) {
  3584. set_error_buf(error_buf, error_buf_size, "unknown global");
  3585. return false;
  3586. }
  3587. if (global_idx < module->import_global_count) {
  3588. global_type = module->import_globals[global_idx]
  3589. .u.global.type.val_type;
  3590. global_ref_type =
  3591. module->import_globals[global_idx].u.global.ref_type;
  3592. }
  3593. else {
  3594. global_type =
  3595. module
  3596. ->globals[global_idx - module->import_global_count]
  3597. .type.val_type;
  3598. global_ref_type =
  3599. module
  3600. ->globals[global_idx - module->import_global_count]
  3601. .ref_type;
  3602. }
  3603. if (!wasm_reftype_is_subtype_of(
  3604. global_type, global_ref_type, global->type.val_type,
  3605. global->ref_type, module->types, module->type_count)) {
  3606. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3607. return false;
  3608. }
  3609. }
  3610. if (need_ref_type_map) {
  3611. if (!(global->ref_type =
  3612. reftype_set_insert(module->ref_type_set, &ref_type,
  3613. error_buf, error_buf_size))) {
  3614. return false;
  3615. }
  3616. }
  3617. #if TRACE_WASM_LOADER != 0
  3618. os_printf("global type: ");
  3619. wasm_dump_value_type(global->type, global->ref_type);
  3620. os_printf("\n");
  3621. #endif
  3622. #endif
  3623. module->global_count++;
  3624. }
  3625. bh_assert(module->global_count == global_count);
  3626. }
  3627. if (p != p_end) {
  3628. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3629. return false;
  3630. }
  3631. LOG_VERBOSE("Load global section success.\n");
  3632. return true;
  3633. fail:
  3634. return false;
  3635. }
  3636. static bool
  3637. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3638. bool is_load_from_file_buf, char *error_buf,
  3639. uint32 error_buf_size)
  3640. {
  3641. const uint8 *p = buf, *p_end = buf_end;
  3642. uint32 export_count, i, j, index;
  3643. uint64 total_size;
  3644. uint32 str_len;
  3645. WASMExport *export;
  3646. const char *name;
  3647. read_leb_uint32(p, p_end, export_count);
  3648. if (export_count) {
  3649. module->export_count = export_count;
  3650. total_size = sizeof(WASMExport) * (uint64)export_count;
  3651. if (!(module->exports =
  3652. loader_malloc(total_size, error_buf, error_buf_size))) {
  3653. return false;
  3654. }
  3655. export = module->exports;
  3656. for (i = 0; i < export_count; i++, export ++) {
  3657. #if WASM_ENABLE_THREAD_MGR == 0
  3658. if (p == p_end) {
  3659. /* export section with inconsistent count:
  3660. n export declared, but less than n given */
  3661. set_error_buf(error_buf, error_buf_size,
  3662. "length out of bounds");
  3663. return false;
  3664. }
  3665. #endif
  3666. read_leb_uint32(p, p_end, str_len);
  3667. CHECK_BUF(p, p_end, str_len);
  3668. for (j = 0; j < i; j++) {
  3669. name = module->exports[j].name;
  3670. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3671. set_error_buf(error_buf, error_buf_size,
  3672. "duplicate export name");
  3673. return false;
  3674. }
  3675. }
  3676. if (!(export->name = wasm_const_str_list_insert(
  3677. p, str_len, module, is_load_from_file_buf, error_buf,
  3678. error_buf_size))) {
  3679. return false;
  3680. }
  3681. p += str_len;
  3682. CHECK_BUF(p, p_end, 1);
  3683. export->kind = read_uint8(p);
  3684. read_leb_uint32(p, p_end, index);
  3685. export->index = index;
  3686. switch (export->kind) {
  3687. /* function index */
  3688. case EXPORT_KIND_FUNC:
  3689. if (index >= module->function_count
  3690. + module->import_function_count) {
  3691. set_error_buf(error_buf, error_buf_size,
  3692. "unknown function");
  3693. return false;
  3694. }
  3695. #if WASM_ENABLE_SIMD != 0
  3696. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3697. /* TODO: check func type, if it has v128 param or result,
  3698. report error */
  3699. #endif
  3700. #endif
  3701. break;
  3702. /* table index */
  3703. case EXPORT_KIND_TABLE:
  3704. if (index
  3705. >= module->table_count + module->import_table_count) {
  3706. set_error_buf(error_buf, error_buf_size,
  3707. "unknown table");
  3708. return false;
  3709. }
  3710. break;
  3711. /* memory index */
  3712. case EXPORT_KIND_MEMORY:
  3713. if (index
  3714. >= module->memory_count + module->import_memory_count) {
  3715. set_error_buf(error_buf, error_buf_size,
  3716. "unknown memory");
  3717. return false;
  3718. }
  3719. break;
  3720. #if WASM_ENABLE_TAGS != 0
  3721. /* export tag */
  3722. case EXPORT_KIND_TAG:
  3723. if (index >= module->tag_count + module->import_tag_count) {
  3724. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3725. return false;
  3726. }
  3727. break;
  3728. #endif
  3729. /* global index */
  3730. case EXPORT_KIND_GLOBAL:
  3731. if (index
  3732. >= module->global_count + module->import_global_count) {
  3733. set_error_buf(error_buf, error_buf_size,
  3734. "unknown global");
  3735. return false;
  3736. }
  3737. break;
  3738. default:
  3739. set_error_buf(error_buf, error_buf_size,
  3740. "invalid export kind");
  3741. return false;
  3742. }
  3743. }
  3744. }
  3745. if (p != p_end) {
  3746. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3747. return false;
  3748. }
  3749. LOG_VERBOSE("Load export section success.\n");
  3750. return true;
  3751. fail:
  3752. return false;
  3753. }
  3754. static bool
  3755. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3756. uint32 error_buf_size)
  3757. {
  3758. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3759. if (table_index != 0) {
  3760. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3761. return false;
  3762. }
  3763. #endif
  3764. if (table_index >= module->import_table_count + module->table_count) {
  3765. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3766. table_index);
  3767. return false;
  3768. }
  3769. return true;
  3770. }
  3771. static bool
  3772. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3773. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3774. {
  3775. const uint8 *p = *p_buf, *p_end = buf_end;
  3776. uint32 table_index;
  3777. read_leb_uint32(p, p_end, table_index);
  3778. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3779. return false;
  3780. }
  3781. *p_table_index = table_index;
  3782. *p_buf = p;
  3783. return true;
  3784. fail:
  3785. return false;
  3786. }
  3787. /* Element segments must match element type of table */
  3788. static bool
  3789. check_table_elem_type(WASMModule *module, uint32 table_index,
  3790. uint32 type_from_elem_seg, char *error_buf,
  3791. uint32 error_buf_size)
  3792. {
  3793. uint32 table_declared_elem_type;
  3794. if (table_index < module->import_table_count)
  3795. table_declared_elem_type =
  3796. module->import_tables[table_index].u.table.elem_type;
  3797. else
  3798. table_declared_elem_type = (module->tables + table_index)->elem_type;
  3799. if (table_declared_elem_type == type_from_elem_seg)
  3800. return true;
  3801. #if WASM_ENABLE_GC != 0
  3802. /*
  3803. * balance in: anyref, funcref, (ref.null func) and (ref.func)
  3804. */
  3805. if (table_declared_elem_type == REF_TYPE_ANYREF)
  3806. return true;
  3807. if (table_declared_elem_type == VALUE_TYPE_FUNCREF
  3808. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3809. return true;
  3810. if (table_declared_elem_type == REF_TYPE_HT_NULLABLE
  3811. && type_from_elem_seg == REF_TYPE_HT_NON_NULLABLE)
  3812. return true;
  3813. #endif
  3814. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3815. return false;
  3816. }
  3817. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3818. static bool
  3819. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3820. uint32 *p_elem_type,
  3821. #if WASM_ENABLE_GC != 0
  3822. WASMRefType **p_elem_ref_type,
  3823. #endif
  3824. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3825. {
  3826. const uint8 *p = *p_buf, *p_end = buf_end;
  3827. uint8 elem_type;
  3828. #if WASM_ENABLE_GC != 0
  3829. WASMRefType elem_ref_type;
  3830. bool need_ref_type_map;
  3831. #endif
  3832. CHECK_BUF(p, p_end, 1);
  3833. elem_type = read_uint8(p);
  3834. if (elemkind_zero) {
  3835. if (elem_type != 0) {
  3836. set_error_buf(error_buf, error_buf_size,
  3837. "invalid reference type or unknown type");
  3838. return false;
  3839. }
  3840. else {
  3841. *p_elem_type = VALUE_TYPE_FUNCREF;
  3842. *p_buf = p;
  3843. return true;
  3844. }
  3845. }
  3846. #if WASM_ENABLE_GC == 0
  3847. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3848. set_error_buf(error_buf, error_buf_size,
  3849. "invalid reference type or unknown type");
  3850. return false;
  3851. }
  3852. *p_elem_type = elem_type;
  3853. #else
  3854. p--;
  3855. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3856. module->type_count, &need_ref_type_map,
  3857. &elem_ref_type, false, error_buf, error_buf_size)) {
  3858. return false;
  3859. }
  3860. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3861. set_error_buf(error_buf, error_buf_size,
  3862. "invalid reference type or unknown type");
  3863. return false;
  3864. }
  3865. *p_elem_type = elem_ref_type.ref_type;
  3866. if (need_ref_type_map) {
  3867. if (!(*p_elem_ref_type =
  3868. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3869. error_buf, error_buf_size))) {
  3870. return false;
  3871. }
  3872. }
  3873. #endif
  3874. *p_buf = p;
  3875. return true;
  3876. fail:
  3877. return false;
  3878. }
  3879. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3880. static bool
  3881. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3882. WASMModule *module, WASMTableSeg *table_segment,
  3883. char *error_buf, uint32 error_buf_size)
  3884. {
  3885. const uint8 *p = *p_buf, *p_end = buf_end;
  3886. uint32 function_count, function_index = 0, i;
  3887. uint64 total_size;
  3888. read_leb_uint32(p, p_end, function_count);
  3889. table_segment->value_count = function_count;
  3890. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3891. if (total_size > 0
  3892. && !(table_segment->init_values =
  3893. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3894. error_buf_size))) {
  3895. return false;
  3896. }
  3897. for (i = 0; i < function_count; i++) {
  3898. InitializerExpression *init_expr = &table_segment->init_values[i];
  3899. read_leb_uint32(p, p_end, function_index);
  3900. if (!check_function_index(module, function_index, error_buf,
  3901. error_buf_size)) {
  3902. return false;
  3903. }
  3904. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3905. init_expr->u.ref_index = function_index;
  3906. }
  3907. *p_buf = p;
  3908. return true;
  3909. fail:
  3910. return false;
  3911. }
  3912. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3913. static bool
  3914. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3915. WASMModule *module, WASMTableSeg *table_segment,
  3916. char *error_buf, uint32 error_buf_size)
  3917. {
  3918. const uint8 *p = *p_buf, *p_end = buf_end;
  3919. uint32 ref_count, i;
  3920. uint64 total_size;
  3921. read_leb_uint32(p, p_end, ref_count);
  3922. table_segment->value_count = ref_count;
  3923. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3924. if (total_size > 0
  3925. && !(table_segment->init_values =
  3926. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3927. error_buf_size))) {
  3928. return false;
  3929. }
  3930. for (i = 0; i < ref_count; i++) {
  3931. InitializerExpression *init_expr = &table_segment->init_values[i];
  3932. if (!load_init_expr(module, &p, p_end, init_expr,
  3933. table_segment->elem_type,
  3934. #if WASM_ENABLE_GC == 0
  3935. NULL,
  3936. #else
  3937. table_segment->elem_ref_type,
  3938. #endif
  3939. error_buf, error_buf_size))
  3940. return false;
  3941. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3942. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3943. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3944. && init_expr->init_expr_type
  3945. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3946. }
  3947. *p_buf = p;
  3948. return true;
  3949. fail:
  3950. return false;
  3951. }
  3952. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3953. static bool
  3954. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3955. WASMModule *module, char *error_buf,
  3956. uint32 error_buf_size)
  3957. {
  3958. const uint8 *p = buf, *p_end = buf_end;
  3959. uint32 table_segment_count, i;
  3960. uint64 total_size;
  3961. WASMTableSeg *table_segment;
  3962. read_leb_uint32(p, p_end, table_segment_count);
  3963. if (table_segment_count) {
  3964. module->table_seg_count = table_segment_count;
  3965. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3966. if (!(module->table_segments =
  3967. loader_malloc(total_size, error_buf, error_buf_size))) {
  3968. return false;
  3969. }
  3970. table_segment = module->table_segments;
  3971. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3972. if (p >= p_end) {
  3973. set_error_buf(error_buf, error_buf_size,
  3974. "invalid value type or "
  3975. "invalid elements segment kind");
  3976. return false;
  3977. }
  3978. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3979. read_leb_uint32(p, p_end, table_segment->mode);
  3980. /* last three bits */
  3981. table_segment->mode = table_segment->mode & 0x07;
  3982. switch (table_segment->mode) {
  3983. /* elemkind/elemtype + active */
  3984. case 0:
  3985. case 4:
  3986. {
  3987. #if WASM_ENABLE_GC != 0
  3988. if (table_segment->mode == 0) {
  3989. /* vec(funcidx), set elem type to (ref func) */
  3990. WASMRefType elem_ref_type = { 0 };
  3991. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3992. wasm_set_refheaptype_common(
  3993. &elem_ref_type.ref_ht_common, false,
  3994. HEAP_TYPE_FUNC);
  3995. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3996. module->ref_type_set, &elem_ref_type,
  3997. error_buf, error_buf_size)))
  3998. return false;
  3999. }
  4000. else {
  4001. /* vec(expr), set elem type to funcref */
  4002. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4003. }
  4004. #else
  4005. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4006. #endif
  4007. table_segment->table_index = 0;
  4008. if (!check_table_index(module, table_segment->table_index,
  4009. error_buf, error_buf_size))
  4010. return false;
  4011. if (!load_init_expr(
  4012. module, &p, p_end, &table_segment->base_offset,
  4013. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4014. return false;
  4015. if (table_segment->mode == 0) {
  4016. /* vec(funcidx) */
  4017. if (!load_func_index_vec(&p, p_end, module,
  4018. table_segment, error_buf,
  4019. error_buf_size))
  4020. return false;
  4021. }
  4022. else {
  4023. /* vec(expr) */
  4024. if (!load_init_expr_vec(&p, p_end, module,
  4025. table_segment, error_buf,
  4026. error_buf_size))
  4027. return false;
  4028. }
  4029. if (!check_table_elem_type(module,
  4030. table_segment->table_index,
  4031. table_segment->elem_type,
  4032. error_buf, error_buf_size))
  4033. return false;
  4034. break;
  4035. }
  4036. /* elemkind + passive/declarative */
  4037. case 1:
  4038. case 3:
  4039. if (!load_elem_type(module, &p, p_end,
  4040. &table_segment->elem_type,
  4041. #if WASM_ENABLE_GC != 0
  4042. &table_segment->elem_ref_type,
  4043. #endif
  4044. true, error_buf, error_buf_size))
  4045. return false;
  4046. /* vec(funcidx) */
  4047. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4048. error_buf, error_buf_size))
  4049. return false;
  4050. break;
  4051. /* elemkind/elemtype + table_idx + active */
  4052. case 2:
  4053. case 6:
  4054. if (!load_table_index(&p, p_end, module,
  4055. &table_segment->table_index,
  4056. error_buf, error_buf_size))
  4057. return false;
  4058. if (!load_init_expr(
  4059. module, &p, p_end, &table_segment->base_offset,
  4060. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4061. return false;
  4062. if (!load_elem_type(module, &p, p_end,
  4063. &table_segment->elem_type,
  4064. #if WASM_ENABLE_GC != 0
  4065. &table_segment->elem_ref_type,
  4066. #endif
  4067. table_segment->mode == 2 ? true : false,
  4068. error_buf, error_buf_size))
  4069. return false;
  4070. if (table_segment->mode == 2) {
  4071. /* vec(funcidx) */
  4072. if (!load_func_index_vec(&p, p_end, module,
  4073. table_segment, error_buf,
  4074. error_buf_size))
  4075. return false;
  4076. }
  4077. else {
  4078. /* vec(expr) */
  4079. if (!load_init_expr_vec(&p, p_end, module,
  4080. table_segment, error_buf,
  4081. error_buf_size))
  4082. return false;
  4083. }
  4084. if (!check_table_elem_type(module,
  4085. table_segment->table_index,
  4086. table_segment->elem_type,
  4087. error_buf, error_buf_size))
  4088. return false;
  4089. break;
  4090. case 5:
  4091. case 7:
  4092. if (!load_elem_type(module, &p, p_end,
  4093. &table_segment->elem_type,
  4094. #if WASM_ENABLE_GC != 0
  4095. &table_segment->elem_ref_type,
  4096. #endif
  4097. false, error_buf, error_buf_size))
  4098. return false;
  4099. /* vec(expr) */
  4100. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4101. error_buf, error_buf_size))
  4102. return false;
  4103. break;
  4104. default:
  4105. set_error_buf(error_buf, error_buf_size,
  4106. "unknown element segment kind");
  4107. return false;
  4108. }
  4109. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4110. /*
  4111. * like: 00 41 05 0b 04 00 01 00 01
  4112. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4113. */
  4114. if (!load_table_index(&p, p_end, module,
  4115. &table_segment->table_index, error_buf,
  4116. error_buf_size))
  4117. return false;
  4118. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4119. VALUE_TYPE_I32, NULL, error_buf,
  4120. error_buf_size))
  4121. return false;
  4122. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4123. error_buf, error_buf_size))
  4124. return false;
  4125. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4126. if (!check_table_elem_type(module, table_segment->table_index,
  4127. table_segment->elem_type, error_buf,
  4128. error_buf_size))
  4129. return false;
  4130. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4131. #if WASM_ENABLE_WAMR_COMPILER != 0
  4132. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4133. module->is_ref_types_used = true;
  4134. #endif
  4135. }
  4136. }
  4137. if (p != p_end) {
  4138. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4139. return false;
  4140. }
  4141. LOG_VERBOSE("Load table segment section success.\n");
  4142. return true;
  4143. fail:
  4144. return false;
  4145. }
  4146. static bool
  4147. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4148. WASMModule *module, char *error_buf,
  4149. uint32 error_buf_size)
  4150. {
  4151. const uint8 *p = buf, *p_end = buf_end;
  4152. uint32 data_seg_count, i, mem_index, data_seg_len;
  4153. uint64 total_size;
  4154. WASMDataSeg *dataseg;
  4155. InitializerExpression init_expr;
  4156. #if WASM_ENABLE_BULK_MEMORY != 0
  4157. bool is_passive = false;
  4158. uint32 mem_flag;
  4159. #endif
  4160. uint8 mem_offset_type = VALUE_TYPE_I32;
  4161. read_leb_uint32(p, p_end, data_seg_count);
  4162. #if WASM_ENABLE_BULK_MEMORY != 0
  4163. if ((module->data_seg_count1 != 0)
  4164. && (data_seg_count != module->data_seg_count1)) {
  4165. set_error_buf(error_buf, error_buf_size,
  4166. "data count and data section have inconsistent lengths");
  4167. return false;
  4168. }
  4169. #endif
  4170. if (data_seg_count) {
  4171. module->data_seg_count = data_seg_count;
  4172. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4173. if (!(module->data_segments =
  4174. loader_malloc(total_size, error_buf, error_buf_size))) {
  4175. return false;
  4176. }
  4177. for (i = 0; i < data_seg_count; i++) {
  4178. read_leb_uint32(p, p_end, mem_index);
  4179. #if WASM_ENABLE_BULK_MEMORY != 0
  4180. is_passive = false;
  4181. mem_flag = mem_index & 0x03;
  4182. switch (mem_flag) {
  4183. case 0x01:
  4184. is_passive = true;
  4185. #if WASM_ENABLE_WAMR_COMPILER != 0
  4186. module->is_bulk_memory_used = true;
  4187. #endif
  4188. break;
  4189. case 0x00:
  4190. /* no memory index, treat index as 0 */
  4191. mem_index = 0;
  4192. goto check_mem_index;
  4193. case 0x02:
  4194. /* read following memory index */
  4195. read_leb_uint32(p, p_end, mem_index);
  4196. #if WASM_ENABLE_WAMR_COMPILER != 0
  4197. module->is_bulk_memory_used = true;
  4198. #endif
  4199. check_mem_index:
  4200. if (mem_index
  4201. >= module->import_memory_count + module->memory_count) {
  4202. set_error_buf_v(error_buf, error_buf_size,
  4203. "unknown memory %d", mem_index);
  4204. return false;
  4205. }
  4206. break;
  4207. case 0x03:
  4208. default:
  4209. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4210. return false;
  4211. break;
  4212. }
  4213. #else
  4214. if (mem_index
  4215. >= module->import_memory_count + module->memory_count) {
  4216. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4217. mem_index);
  4218. return false;
  4219. }
  4220. #endif /* WASM_ENABLE_BULK_MEMORY */
  4221. #if WASM_ENABLE_BULK_MEMORY != 0
  4222. if (!is_passive)
  4223. #endif
  4224. {
  4225. #if WASM_ENABLE_MEMORY64 != 0
  4226. /* This memory_flag is from memory instead of data segment */
  4227. uint8 memory_flag;
  4228. if (module->import_memory_count > 0) {
  4229. memory_flag =
  4230. module->import_memories[mem_index].u.memory.flags;
  4231. }
  4232. else {
  4233. memory_flag =
  4234. module
  4235. ->memories[mem_index - module->import_memory_count]
  4236. .flags;
  4237. }
  4238. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4239. : VALUE_TYPE_I32;
  4240. #else
  4241. mem_offset_type = VALUE_TYPE_I32;
  4242. #endif
  4243. }
  4244. #if WASM_ENABLE_BULK_MEMORY != 0
  4245. if (!is_passive)
  4246. #endif
  4247. if (!load_init_expr(module, &p, p_end, &init_expr,
  4248. mem_offset_type, NULL, error_buf,
  4249. error_buf_size))
  4250. return false;
  4251. read_leb_uint32(p, p_end, data_seg_len);
  4252. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4253. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4254. return false;
  4255. }
  4256. #if WASM_ENABLE_BULK_MEMORY != 0
  4257. dataseg->is_passive = is_passive;
  4258. if (!is_passive)
  4259. #endif
  4260. {
  4261. bh_memcpy_s(&dataseg->base_offset,
  4262. sizeof(InitializerExpression), &init_expr,
  4263. sizeof(InitializerExpression));
  4264. dataseg->memory_index = mem_index;
  4265. }
  4266. dataseg->data_length = data_seg_len;
  4267. CHECK_BUF(p, p_end, data_seg_len);
  4268. dataseg->data = (uint8 *)p;
  4269. p += data_seg_len;
  4270. }
  4271. }
  4272. if (p != p_end) {
  4273. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4274. return false;
  4275. }
  4276. LOG_VERBOSE("Load data segment section success.\n");
  4277. return true;
  4278. fail:
  4279. return false;
  4280. }
  4281. #if WASM_ENABLE_BULK_MEMORY != 0
  4282. static bool
  4283. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4284. WASMModule *module, char *error_buf,
  4285. uint32 error_buf_size)
  4286. {
  4287. const uint8 *p = buf, *p_end = buf_end;
  4288. uint32 data_seg_count1 = 0;
  4289. read_leb_uint32(p, p_end, data_seg_count1);
  4290. module->data_seg_count1 = data_seg_count1;
  4291. if (p != p_end) {
  4292. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4293. return false;
  4294. }
  4295. #if WASM_ENABLE_WAMR_COMPILER != 0
  4296. module->is_bulk_memory_used = true;
  4297. #endif
  4298. LOG_VERBOSE("Load datacount section success.\n");
  4299. return true;
  4300. fail:
  4301. return false;
  4302. }
  4303. #endif
  4304. #if WASM_ENABLE_TAGS != 0
  4305. static bool
  4306. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4307. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4308. uint32 error_buf_size)
  4309. {
  4310. (void)buf_code;
  4311. (void)buf_code_end;
  4312. const uint8 *p = buf, *p_end = buf_end;
  4313. size_t total_size = 0;
  4314. /* number of tags defined in the section */
  4315. uint32 section_tag_count = 0;
  4316. uint8 tag_attribute;
  4317. uint32 tag_type;
  4318. WASMTag *tag = NULL;
  4319. /* get tag count */
  4320. read_leb_uint32(p, p_end, section_tag_count);
  4321. module->tag_count = section_tag_count;
  4322. if (section_tag_count) {
  4323. total_size = sizeof(WASMTag *) * module->tag_count;
  4324. if (!(module->tags =
  4325. loader_malloc(total_size, error_buf, error_buf_size))) {
  4326. return false;
  4327. }
  4328. /* load each tag, imported tags precede the tags */
  4329. uint32 tag_index;
  4330. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4331. /* get the one byte attribute */
  4332. CHECK_BUF(p, p_end, 1);
  4333. tag_attribute = read_uint8(p);
  4334. /* get type */
  4335. read_leb_uint32(p, p_end, tag_type);
  4336. /* compare against module->types */
  4337. if (tag_type >= module->type_count) {
  4338. set_error_buf(error_buf, error_buf_size, "unknown type");
  4339. return false;
  4340. }
  4341. /* get return type (must be 0) */
  4342. /* check, that the type of the referred tag returns void */
  4343. WASMFuncType *func_type = (WASMFuncType *)module->types[tag_type];
  4344. if (func_type->result_count != 0) {
  4345. set_error_buf(error_buf, error_buf_size,
  4346. "non-empty tag result type");
  4347. goto fail;
  4348. }
  4349. if (!(tag = module->tags[tag_index] = loader_malloc(
  4350. sizeof(WASMTag), error_buf, error_buf_size))) {
  4351. return false;
  4352. }
  4353. /* store to module tag declarations */
  4354. tag->attribute = tag_attribute;
  4355. tag->type = tag_type;
  4356. tag->tag_type = func_type;
  4357. }
  4358. }
  4359. if (p != p_end) {
  4360. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4361. return false;
  4362. }
  4363. LOG_VERBOSE("Load tag section success.\n");
  4364. return true;
  4365. fail:
  4366. return false;
  4367. }
  4368. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4369. static bool
  4370. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4371. const uint8 *buf_func_end, WASMModule *module,
  4372. char *error_buf, uint32 error_buf_size)
  4373. {
  4374. const uint8 *p = buf, *p_end = buf_end;
  4375. const uint8 *p_func = buf_func;
  4376. uint32 func_count = 0, code_count;
  4377. /* code has been loaded in function section, so pass it here, just check
  4378. * whether function and code section have inconsistent lengths */
  4379. read_leb_uint32(p, p_end, code_count);
  4380. if (buf_func)
  4381. read_leb_uint32(p_func, buf_func_end, func_count);
  4382. if (func_count != code_count) {
  4383. set_error_buf(error_buf, error_buf_size,
  4384. "function and code section have inconsistent lengths");
  4385. return false;
  4386. }
  4387. LOG_VERBOSE("Load code segment section success.\n");
  4388. (void)module;
  4389. return true;
  4390. fail:
  4391. return false;
  4392. }
  4393. static bool
  4394. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4395. char *error_buf, uint32 error_buf_size)
  4396. {
  4397. const uint8 *p = buf, *p_end = buf_end;
  4398. WASMFuncType *type;
  4399. uint32 start_function;
  4400. read_leb_uint32(p, p_end, start_function);
  4401. if (start_function
  4402. >= module->function_count + module->import_function_count) {
  4403. set_error_buf(error_buf, error_buf_size, "unknown function");
  4404. return false;
  4405. }
  4406. if (start_function < module->import_function_count)
  4407. type = module->import_functions[start_function].u.function.func_type;
  4408. else
  4409. type = module->functions[start_function - module->import_function_count]
  4410. ->func_type;
  4411. if (type->param_count != 0 || type->result_count != 0) {
  4412. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4413. return false;
  4414. }
  4415. module->start_function = start_function;
  4416. if (p != p_end) {
  4417. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4418. return false;
  4419. }
  4420. LOG_VERBOSE("Load start section success.\n");
  4421. return true;
  4422. fail:
  4423. return false;
  4424. }
  4425. #if WASM_ENABLE_STRINGREF != 0
  4426. static bool
  4427. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4428. WASMModule *module, bool is_load_from_file_buf,
  4429. char *error_buf, uint32 error_buf_size)
  4430. {
  4431. const uint8 *p = buf, *p_end = buf_end;
  4432. int32 deferred_count, immediate_count, string_length, i;
  4433. uint64 total_size;
  4434. read_leb_uint32(p, p_end, deferred_count);
  4435. read_leb_uint32(p, p_end, immediate_count);
  4436. /* proposal set deferred_count for future extension */
  4437. if (deferred_count != 0) {
  4438. goto fail;
  4439. }
  4440. if (immediate_count > 0) {
  4441. total_size = sizeof(char *) * (uint64)immediate_count;
  4442. if (!(module->string_literal_ptrs =
  4443. loader_malloc(total_size, error_buf, error_buf_size))) {
  4444. goto fail;
  4445. }
  4446. module->string_literal_count = immediate_count;
  4447. total_size = sizeof(uint32) * (uint64)immediate_count;
  4448. if (!(module->string_literal_lengths =
  4449. loader_malloc(total_size, error_buf, error_buf_size))) {
  4450. goto fail;
  4451. }
  4452. for (i = 0; i < immediate_count; i++) {
  4453. read_leb_uint32(p, p_end, string_length);
  4454. CHECK_BUF(p, p_end, string_length);
  4455. module->string_literal_ptrs[i] = p;
  4456. module->string_literal_lengths[i] = string_length;
  4457. p += string_length;
  4458. }
  4459. }
  4460. if (p != p_end) {
  4461. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4462. goto fail;
  4463. }
  4464. LOG_VERBOSE("Load stringref section success.\n");
  4465. return true;
  4466. fail:
  4467. return false;
  4468. }
  4469. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4470. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4471. static bool
  4472. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4473. bool is_load_from_file_buf, char *error_buf,
  4474. uint32 error_buf_size)
  4475. {
  4476. const uint8 *p = buf, *p_end = buf_end;
  4477. uint32 name_type, subsection_size;
  4478. uint32 previous_name_type = 0;
  4479. uint32 num_func_name;
  4480. uint32 func_index;
  4481. uint32 previous_func_index = ~0U;
  4482. uint32 func_name_len;
  4483. uint32 name_index;
  4484. int i = 0;
  4485. if (p >= p_end) {
  4486. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4487. return false;
  4488. }
  4489. while (p < p_end) {
  4490. read_leb_uint32(p, p_end, name_type);
  4491. if (i != 0) {
  4492. if (name_type == previous_name_type) {
  4493. set_error_buf(error_buf, error_buf_size,
  4494. "duplicate sub-section");
  4495. return false;
  4496. }
  4497. if (name_type < previous_name_type) {
  4498. set_error_buf(error_buf, error_buf_size,
  4499. "out-of-order sub-section");
  4500. return false;
  4501. }
  4502. }
  4503. previous_name_type = name_type;
  4504. read_leb_uint32(p, p_end, subsection_size);
  4505. CHECK_BUF(p, p_end, subsection_size);
  4506. switch (name_type) {
  4507. case SUB_SECTION_TYPE_FUNC:
  4508. if (subsection_size) {
  4509. read_leb_uint32(p, p_end, num_func_name);
  4510. for (name_index = 0; name_index < num_func_name;
  4511. name_index++) {
  4512. read_leb_uint32(p, p_end, func_index);
  4513. if (func_index == previous_func_index) {
  4514. set_error_buf(error_buf, error_buf_size,
  4515. "duplicate function name");
  4516. return false;
  4517. }
  4518. if (func_index < previous_func_index
  4519. && previous_func_index != ~0U) {
  4520. set_error_buf(error_buf, error_buf_size,
  4521. "out-of-order function index ");
  4522. return false;
  4523. }
  4524. previous_func_index = func_index;
  4525. read_leb_uint32(p, p_end, func_name_len);
  4526. CHECK_BUF(p, p_end, func_name_len);
  4527. /* Skip the import functions */
  4528. if (func_index >= module->import_function_count) {
  4529. func_index -= module->import_function_count;
  4530. if (func_index >= module->function_count) {
  4531. set_error_buf(error_buf, error_buf_size,
  4532. "out-of-range function index");
  4533. return false;
  4534. }
  4535. if (!(module->functions[func_index]->field_name =
  4536. wasm_const_str_list_insert(
  4537. p, func_name_len, module,
  4538. #if WASM_ENABLE_WAMR_COMPILER != 0
  4539. false,
  4540. #else
  4541. is_load_from_file_buf,
  4542. #endif
  4543. error_buf, error_buf_size))) {
  4544. return false;
  4545. }
  4546. }
  4547. p += func_name_len;
  4548. }
  4549. }
  4550. break;
  4551. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4552. */
  4553. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4554. default:
  4555. p = p + subsection_size;
  4556. break;
  4557. }
  4558. i++;
  4559. }
  4560. return true;
  4561. fail:
  4562. return false;
  4563. }
  4564. #endif
  4565. static bool
  4566. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4567. bool is_load_from_file_buf, char *error_buf,
  4568. uint32 error_buf_size)
  4569. {
  4570. const uint8 *p = buf, *p_end = buf_end;
  4571. char section_name[32];
  4572. uint32 name_len, buffer_len;
  4573. if (p >= p_end) {
  4574. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4575. return false;
  4576. }
  4577. read_leb_uint32(p, p_end, name_len);
  4578. if (p + name_len > p_end) {
  4579. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4580. return false;
  4581. }
  4582. if (!wasm_check_utf8_str(p, name_len)) {
  4583. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4584. return false;
  4585. }
  4586. buffer_len = sizeof(section_name);
  4587. memset(section_name, 0, buffer_len);
  4588. if (name_len < buffer_len) {
  4589. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4590. }
  4591. else {
  4592. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4593. memset(section_name + buffer_len - 4, '.', 3);
  4594. }
  4595. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4596. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4597. module->name_section_buf = buf;
  4598. module->name_section_buf_end = buf_end;
  4599. p += name_len;
  4600. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  4601. error_buf_size);
  4602. LOG_VERBOSE("Load custom name section success.");
  4603. return true;
  4604. }
  4605. #endif
  4606. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4607. {
  4608. WASMCustomSection *section =
  4609. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4610. if (!section) {
  4611. return false;
  4612. }
  4613. section->name_addr = (char *)p;
  4614. section->name_len = name_len;
  4615. section->content_addr = (uint8 *)(p + name_len);
  4616. section->content_len = (uint32)(p_end - p - name_len);
  4617. section->next = module->custom_section_list;
  4618. module->custom_section_list = section;
  4619. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4620. return true;
  4621. }
  4622. #endif
  4623. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4624. (void)is_load_from_file_buf;
  4625. (void)module;
  4626. return true;
  4627. fail:
  4628. return false;
  4629. }
  4630. static void
  4631. calculate_global_data_offset(WASMModule *module)
  4632. {
  4633. uint32 i, data_offset;
  4634. data_offset = 0;
  4635. for (i = 0; i < module->import_global_count; i++) {
  4636. WASMGlobalImport *import_global =
  4637. &((module->import_globals + i)->u.global);
  4638. #if WASM_ENABLE_FAST_JIT != 0
  4639. import_global->data_offset = data_offset;
  4640. #endif
  4641. data_offset += wasm_value_type_size(import_global->type.val_type);
  4642. }
  4643. for (i = 0; i < module->global_count; i++) {
  4644. WASMGlobal *global = module->globals + i;
  4645. #if WASM_ENABLE_FAST_JIT != 0
  4646. global->data_offset = data_offset;
  4647. #endif
  4648. data_offset += wasm_value_type_size(global->type.val_type);
  4649. }
  4650. module->global_data_size = data_offset;
  4651. }
  4652. #if WASM_ENABLE_FAST_JIT != 0
  4653. static bool
  4654. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4655. uint32 error_buf_size)
  4656. {
  4657. #if WASM_ENABLE_LAZY_JIT != 0
  4658. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4659. #endif
  4660. uint32 i;
  4661. if (!module->function_count)
  4662. return true;
  4663. if (!(module->fast_jit_func_ptrs =
  4664. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4665. error_buf_size))) {
  4666. return false;
  4667. }
  4668. #if WASM_ENABLE_LAZY_JIT != 0
  4669. for (i = 0; i < module->function_count; i++) {
  4670. module->fast_jit_func_ptrs[i] =
  4671. jit_globals->compile_fast_jit_and_then_call;
  4672. }
  4673. #endif
  4674. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4675. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4676. set_error_buf(error_buf, error_buf_size,
  4677. "init fast jit thread lock failed");
  4678. return false;
  4679. }
  4680. module->fast_jit_thread_locks_inited[i] = true;
  4681. }
  4682. return true;
  4683. }
  4684. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4685. #if WASM_ENABLE_JIT != 0
  4686. static bool
  4687. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4688. uint32 error_buf_size)
  4689. {
  4690. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4691. AOTCompOption option = { 0 };
  4692. char *aot_last_error;
  4693. uint64 size;
  4694. #if WASM_ENABLE_GC != 0
  4695. bool gc_enabled = true;
  4696. #else
  4697. bool gc_enabled = false;
  4698. #endif
  4699. if (module->function_count == 0)
  4700. return true;
  4701. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4702. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4703. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4704. return false;
  4705. }
  4706. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4707. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4708. os_mutex_destroy(&module->tierup_wait_lock);
  4709. return false;
  4710. }
  4711. module->tierup_wait_lock_inited = true;
  4712. #endif
  4713. size = sizeof(void *) * (uint64)module->function_count
  4714. + sizeof(bool) * (uint64)module->function_count;
  4715. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4716. return false;
  4717. }
  4718. module->func_ptrs_compiled =
  4719. (bool *)((uint8 *)module->func_ptrs
  4720. + sizeof(void *) * module->function_count);
  4721. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4722. if (!module->comp_data) {
  4723. aot_last_error = aot_get_last_error();
  4724. bh_assert(aot_last_error != NULL);
  4725. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4726. return false;
  4727. }
  4728. option.is_jit_mode = true;
  4729. option.opt_level = llvm_jit_options->opt_level;
  4730. option.size_level = llvm_jit_options->size_level;
  4731. option.segue_flags = llvm_jit_options->segue_flags;
  4732. option.quick_invoke_c_api_import =
  4733. llvm_jit_options->quick_invoke_c_api_import;
  4734. #if WASM_ENABLE_BULK_MEMORY != 0
  4735. option.enable_bulk_memory = true;
  4736. #endif
  4737. #if WASM_ENABLE_THREAD_MGR != 0
  4738. option.enable_thread_mgr = true;
  4739. #endif
  4740. #if WASM_ENABLE_TAIL_CALL != 0
  4741. option.enable_tail_call = true;
  4742. #endif
  4743. #if WASM_ENABLE_SIMD != 0
  4744. option.enable_simd = true;
  4745. #endif
  4746. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4747. option.enable_ref_types = true;
  4748. #elif WASM_ENABLE_GC != 0
  4749. option.enable_gc = true;
  4750. #endif
  4751. option.enable_aux_stack_check = true;
  4752. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4753. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4754. option.enable_aux_stack_frame = true;
  4755. #endif
  4756. #if WASM_ENABLE_PERF_PROFILING != 0
  4757. option.enable_perf_profiling = true;
  4758. #endif
  4759. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4760. option.enable_memory_profiling = true;
  4761. option.enable_stack_estimation = true;
  4762. #endif
  4763. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4764. if (!module->comp_ctx) {
  4765. aot_last_error = aot_get_last_error();
  4766. bh_assert(aot_last_error != NULL);
  4767. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4768. return false;
  4769. }
  4770. return true;
  4771. }
  4772. static bool
  4773. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4774. uint32 error_buf_size)
  4775. {
  4776. char *aot_last_error;
  4777. uint32 i;
  4778. if (module->function_count == 0)
  4779. return true;
  4780. if (!aot_compile_wasm(module->comp_ctx)) {
  4781. aot_last_error = aot_get_last_error();
  4782. bh_assert(aot_last_error != NULL);
  4783. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4784. return false;
  4785. }
  4786. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4787. if (module->orcjit_stop_compiling)
  4788. return false;
  4789. #endif
  4790. bh_print_time("Begin to lookup llvm jit functions");
  4791. for (i = 0; i < module->function_count; i++) {
  4792. LLVMOrcJITTargetAddress func_addr = 0;
  4793. LLVMErrorRef error;
  4794. char func_name[48];
  4795. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4796. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4797. func_name);
  4798. if (error != LLVMErrorSuccess) {
  4799. char *err_msg = LLVMGetErrorMessage(error);
  4800. set_error_buf_v(error_buf, error_buf_size,
  4801. "failed to compile llvm jit function: %s", err_msg);
  4802. LLVMDisposeErrorMessage(err_msg);
  4803. return false;
  4804. }
  4805. /**
  4806. * No need to lock the func_ptr[func_idx] here as it is basic
  4807. * data type, the load/store for it can be finished by one cpu
  4808. * instruction, and there can be only one cpu instruction
  4809. * loading/storing at the same time.
  4810. */
  4811. module->func_ptrs[i] = (void *)func_addr;
  4812. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4813. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4814. if (module->orcjit_stop_compiling)
  4815. return false;
  4816. #endif
  4817. }
  4818. bh_print_time("End lookup llvm jit functions");
  4819. return true;
  4820. }
  4821. #endif /* end of WASM_ENABLE_JIT != 0 */
  4822. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4823. && WASM_ENABLE_LAZY_JIT != 0
  4824. static void *
  4825. init_llvm_jit_functions_stage2_callback(void *arg)
  4826. {
  4827. WASMModule *module = (WASMModule *)arg;
  4828. char error_buf[128];
  4829. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4830. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4831. module->orcjit_stop_compiling = true;
  4832. return NULL;
  4833. }
  4834. os_mutex_lock(&module->tierup_wait_lock);
  4835. module->llvm_jit_inited = true;
  4836. os_cond_broadcast(&module->tierup_wait_cond);
  4837. os_mutex_unlock(&module->tierup_wait_lock);
  4838. return NULL;
  4839. }
  4840. #endif
  4841. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4842. /* The callback function to compile jit functions */
  4843. static void *
  4844. orcjit_thread_callback(void *arg)
  4845. {
  4846. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4847. #if WASM_ENABLE_JIT != 0
  4848. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4849. #endif
  4850. WASMModule *module = thread_arg->module;
  4851. uint32 group_idx = thread_arg->group_idx;
  4852. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4853. uint32 func_count = module->function_count;
  4854. uint32 i;
  4855. #if WASM_ENABLE_FAST_JIT != 0
  4856. /* Compile fast jit functions of this group */
  4857. for (i = group_idx; i < func_count; i += group_stride) {
  4858. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4859. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4860. break;
  4861. }
  4862. if (module->orcjit_stop_compiling) {
  4863. return NULL;
  4864. }
  4865. }
  4866. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4867. os_mutex_lock(&module->tierup_wait_lock);
  4868. module->fast_jit_ready_groups++;
  4869. os_mutex_unlock(&module->tierup_wait_lock);
  4870. #endif
  4871. #endif
  4872. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4873. && WASM_ENABLE_LAZY_JIT != 0
  4874. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4875. for (i = group_idx; i < func_count;
  4876. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4877. uint32 j;
  4878. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4879. if (i + j * group_stride < func_count) {
  4880. if (!jit_compiler_set_call_to_fast_jit(
  4881. module,
  4882. i + j * group_stride + module->import_function_count)) {
  4883. LOG_ERROR(
  4884. "failed to compile call_to_fast_jit for func %u\n",
  4885. i + j * group_stride + module->import_function_count);
  4886. module->orcjit_stop_compiling = true;
  4887. return NULL;
  4888. }
  4889. }
  4890. if (module->orcjit_stop_compiling) {
  4891. return NULL;
  4892. }
  4893. }
  4894. }
  4895. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4896. fast jit functions are compiled */
  4897. os_mutex_lock(&module->tierup_wait_lock);
  4898. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4899. && module->fast_jit_ready_groups >= group_stride)) {
  4900. os_cond_reltimedwait(&module->tierup_wait_cond,
  4901. &module->tierup_wait_lock, 10000);
  4902. if (module->orcjit_stop_compiling) {
  4903. /* init_llvm_jit_functions_stage2 failed */
  4904. os_mutex_unlock(&module->tierup_wait_lock);
  4905. return NULL;
  4906. }
  4907. }
  4908. os_mutex_unlock(&module->tierup_wait_lock);
  4909. #endif
  4910. #if WASM_ENABLE_JIT != 0
  4911. /* Compile llvm jit functions of this group */
  4912. for (i = group_idx; i < func_count;
  4913. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4914. LLVMOrcJITTargetAddress func_addr = 0;
  4915. LLVMErrorRef error;
  4916. char func_name[48];
  4917. typedef void (*F)(void);
  4918. union {
  4919. F f;
  4920. void *v;
  4921. } u;
  4922. uint32 j;
  4923. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4924. "_wrapper");
  4925. LOG_DEBUG("compile llvm jit func %s", func_name);
  4926. error =
  4927. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4928. if (error != LLVMErrorSuccess) {
  4929. char *err_msg = LLVMGetErrorMessage(error);
  4930. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4931. LLVMDisposeErrorMessage(err_msg);
  4932. break;
  4933. }
  4934. /* Call the jit wrapper function to trigger its compilation, so as
  4935. to compile the actual jit functions, since we add the latter to
  4936. function list in the PartitionFunction callback */
  4937. u.v = (void *)func_addr;
  4938. u.f();
  4939. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4940. if (i + j * group_stride < func_count) {
  4941. module->func_ptrs_compiled[i + j * group_stride] = true;
  4942. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4943. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4944. i + j * group_stride);
  4945. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4946. func_name);
  4947. if (error != LLVMErrorSuccess) {
  4948. char *err_msg = LLVMGetErrorMessage(error);
  4949. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4950. err_msg);
  4951. LLVMDisposeErrorMessage(err_msg);
  4952. /* Ignore current llvm jit func, as its func ptr is
  4953. previous set to call_to_fast_jit, which also works */
  4954. continue;
  4955. }
  4956. jit_compiler_set_llvm_jit_func_ptr(
  4957. module,
  4958. i + j * group_stride + module->import_function_count,
  4959. (void *)func_addr);
  4960. /* Try to switch to call this llvm jit function instead of
  4961. fast jit function from fast jit jitted code */
  4962. jit_compiler_set_call_to_llvm_jit(
  4963. module,
  4964. i + j * group_stride + module->import_function_count);
  4965. #endif
  4966. }
  4967. }
  4968. if (module->orcjit_stop_compiling) {
  4969. break;
  4970. }
  4971. }
  4972. #endif
  4973. return NULL;
  4974. }
  4975. static void
  4976. orcjit_stop_compile_threads(WASMModule *module)
  4977. {
  4978. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4979. / sizeof(OrcJitThreadArg));
  4980. module->orcjit_stop_compiling = true;
  4981. for (i = 0; i < thread_num; i++) {
  4982. if (module->orcjit_threads[i])
  4983. os_thread_join(module->orcjit_threads[i], NULL);
  4984. }
  4985. }
  4986. static bool
  4987. compile_jit_functions(WASMModule *module, char *error_buf,
  4988. uint32 error_buf_size)
  4989. {
  4990. uint32 thread_num =
  4991. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4992. uint32 i, j;
  4993. bh_print_time("Begin to compile jit functions");
  4994. /* Create threads to compile the jit functions */
  4995. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4996. #if WASM_ENABLE_JIT != 0
  4997. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4998. #endif
  4999. module->orcjit_thread_args[i].module = module;
  5000. module->orcjit_thread_args[i].group_idx = i;
  5001. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  5002. (void *)&module->orcjit_thread_args[i],
  5003. APP_THREAD_STACK_SIZE_DEFAULT)
  5004. != 0) {
  5005. set_error_buf(error_buf, error_buf_size,
  5006. "create orcjit compile thread failed");
  5007. /* Terminate the threads created */
  5008. module->orcjit_stop_compiling = true;
  5009. for (j = 0; j < i; j++) {
  5010. os_thread_join(module->orcjit_threads[j], NULL);
  5011. }
  5012. return false;
  5013. }
  5014. }
  5015. #if WASM_ENABLE_LAZY_JIT == 0
  5016. /* Wait until all jit functions are compiled for eager mode */
  5017. for (i = 0; i < thread_num; i++) {
  5018. if (module->orcjit_threads[i])
  5019. os_thread_join(module->orcjit_threads[i], NULL);
  5020. }
  5021. #if WASM_ENABLE_FAST_JIT != 0
  5022. /* Ensure all the fast-jit functions are compiled */
  5023. for (i = 0; i < module->function_count; i++) {
  5024. if (!jit_compiler_is_compiled(module,
  5025. i + module->import_function_count)) {
  5026. set_error_buf(error_buf, error_buf_size,
  5027. "failed to compile fast jit function");
  5028. return false;
  5029. }
  5030. }
  5031. #endif
  5032. #if WASM_ENABLE_JIT != 0
  5033. /* Ensure all the llvm-jit functions are compiled */
  5034. for (i = 0; i < module->function_count; i++) {
  5035. if (!module->func_ptrs_compiled[i]) {
  5036. set_error_buf(error_buf, error_buf_size,
  5037. "failed to compile llvm jit function");
  5038. return false;
  5039. }
  5040. }
  5041. #endif
  5042. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5043. bh_print_time("End compile jit functions");
  5044. return true;
  5045. }
  5046. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5047. static bool
  5048. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5049. uint32 cur_func_idx, char *error_buf,
  5050. uint32 error_buf_size);
  5051. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5052. void **
  5053. wasm_interp_get_handle_table();
  5054. static void **handle_table;
  5055. #endif
  5056. static bool
  5057. load_from_sections(WASMModule *module, WASMSection *sections,
  5058. bool is_load_from_file_buf, char *error_buf,
  5059. uint32 error_buf_size)
  5060. {
  5061. WASMExport *export;
  5062. WASMSection *section = sections;
  5063. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5064. *buf_func = NULL, *buf_func_end = NULL;
  5065. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5066. WASMGlobal *aux_stack_top_global = NULL, *global;
  5067. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5068. aux_stack_top = (uint64)-1LL;
  5069. uint32 global_index, func_index, i;
  5070. uint32 aux_data_end_global_index = (uint32)-1;
  5071. uint32 aux_heap_base_global_index = (uint32)-1;
  5072. WASMFuncType *func_type;
  5073. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5074. /* Find code and function sections if have */
  5075. while (section) {
  5076. if (section->section_type == SECTION_TYPE_CODE) {
  5077. buf_code = section->section_body;
  5078. buf_code_end = buf_code + section->section_body_size;
  5079. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5080. module->buf_code = (uint8 *)buf_code;
  5081. module->buf_code_size = section->section_body_size;
  5082. #endif
  5083. }
  5084. else if (section->section_type == SECTION_TYPE_FUNC) {
  5085. buf_func = section->section_body;
  5086. buf_func_end = buf_func + section->section_body_size;
  5087. }
  5088. section = section->next;
  5089. }
  5090. section = sections;
  5091. while (section) {
  5092. buf = section->section_body;
  5093. buf_end = buf + section->section_body_size;
  5094. switch (section->section_type) {
  5095. case SECTION_TYPE_USER:
  5096. /* unsupported user section, ignore it. */
  5097. if (!load_user_section(buf, buf_end, module,
  5098. is_load_from_file_buf, error_buf,
  5099. error_buf_size))
  5100. return false;
  5101. break;
  5102. case SECTION_TYPE_TYPE:
  5103. if (!load_type_section(buf, buf_end, module, error_buf,
  5104. error_buf_size))
  5105. return false;
  5106. break;
  5107. case SECTION_TYPE_IMPORT:
  5108. if (!load_import_section(buf, buf_end, module,
  5109. is_load_from_file_buf, error_buf,
  5110. error_buf_size))
  5111. return false;
  5112. break;
  5113. case SECTION_TYPE_FUNC:
  5114. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5115. module, error_buf, error_buf_size))
  5116. return false;
  5117. break;
  5118. case SECTION_TYPE_TABLE:
  5119. if (!load_table_section(buf, buf_end, module, error_buf,
  5120. error_buf_size))
  5121. return false;
  5122. break;
  5123. case SECTION_TYPE_MEMORY:
  5124. if (!load_memory_section(buf, buf_end, module, error_buf,
  5125. error_buf_size))
  5126. return false;
  5127. break;
  5128. #if WASM_ENABLE_TAGS != 0
  5129. case SECTION_TYPE_TAG:
  5130. /* load tag declaration section */
  5131. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5132. module, error_buf, error_buf_size))
  5133. return false;
  5134. break;
  5135. #endif
  5136. case SECTION_TYPE_GLOBAL:
  5137. if (!load_global_section(buf, buf_end, module, error_buf,
  5138. error_buf_size))
  5139. return false;
  5140. break;
  5141. case SECTION_TYPE_EXPORT:
  5142. if (!load_export_section(buf, buf_end, module,
  5143. is_load_from_file_buf, error_buf,
  5144. error_buf_size))
  5145. return false;
  5146. break;
  5147. case SECTION_TYPE_START:
  5148. if (!load_start_section(buf, buf_end, module, error_buf,
  5149. error_buf_size))
  5150. return false;
  5151. break;
  5152. case SECTION_TYPE_ELEM:
  5153. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5154. error_buf_size))
  5155. return false;
  5156. break;
  5157. case SECTION_TYPE_CODE:
  5158. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5159. module, error_buf, error_buf_size))
  5160. return false;
  5161. break;
  5162. case SECTION_TYPE_DATA:
  5163. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  5164. error_buf_size))
  5165. return false;
  5166. break;
  5167. #if WASM_ENABLE_BULK_MEMORY != 0
  5168. case SECTION_TYPE_DATACOUNT:
  5169. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5170. error_buf_size))
  5171. return false;
  5172. break;
  5173. #endif
  5174. #if WASM_ENABLE_STRINGREF != 0
  5175. case SECTION_TYPE_STRINGREF:
  5176. if (!load_stringref_section(buf, buf_end, module,
  5177. is_load_from_file_buf, error_buf,
  5178. error_buf_size))
  5179. return false;
  5180. break;
  5181. #endif
  5182. default:
  5183. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5184. return false;
  5185. }
  5186. section = section->next;
  5187. }
  5188. module->aux_data_end_global_index = (uint32)-1;
  5189. module->aux_heap_base_global_index = (uint32)-1;
  5190. module->aux_stack_top_global_index = (uint32)-1;
  5191. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5192. export = module->exports;
  5193. for (i = 0; i < module->export_count; i++, export ++) {
  5194. if (export->kind == EXPORT_KIND_GLOBAL) {
  5195. if (!strcmp(export->name, "__heap_base")) {
  5196. global_index = export->index - module->import_global_count;
  5197. global = module->globals + global_index;
  5198. if (global->type.val_type == VALUE_TYPE_I32
  5199. && !global->type.is_mutable
  5200. && global->init_expr.init_expr_type
  5201. == INIT_EXPR_TYPE_I32_CONST) {
  5202. aux_heap_base_global = global;
  5203. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5204. aux_heap_base_global_index = export->index;
  5205. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5206. aux_heap_base);
  5207. }
  5208. }
  5209. else if (!strcmp(export->name, "__data_end")) {
  5210. global_index = export->index - module->import_global_count;
  5211. global = module->globals + global_index;
  5212. if (global->type.val_type == VALUE_TYPE_I32
  5213. && !global->type.is_mutable
  5214. && global->init_expr.init_expr_type
  5215. == INIT_EXPR_TYPE_I32_CONST) {
  5216. aux_data_end_global = global;
  5217. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5218. aux_data_end_global_index = export->index;
  5219. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5220. aux_data_end);
  5221. aux_data_end = align_uint64(aux_data_end, 16);
  5222. }
  5223. }
  5224. /* For module compiled with -pthread option, the global is:
  5225. [0] stack_top <-- 0
  5226. [1] tls_pointer
  5227. [2] tls_size
  5228. [3] data_end <-- 3
  5229. [4] global_base
  5230. [5] heap_base <-- 5
  5231. [6] dso_handle
  5232. For module compiled without -pthread option:
  5233. [0] stack_top <-- 0
  5234. [1] data_end <-- 1
  5235. [2] global_base
  5236. [3] heap_base <-- 3
  5237. [4] dso_handle
  5238. */
  5239. if (aux_data_end_global && aux_heap_base_global
  5240. && aux_data_end <= aux_heap_base) {
  5241. module->aux_data_end_global_index = aux_data_end_global_index;
  5242. module->aux_data_end = aux_data_end;
  5243. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5244. module->aux_heap_base = aux_heap_base;
  5245. /* Resolve aux stack top global */
  5246. for (global_index = 0; global_index < module->global_count;
  5247. global_index++) {
  5248. global = module->globals + global_index;
  5249. if (global->type.is_mutable /* heap_base and data_end is
  5250. not mutable */
  5251. && global->type.val_type == VALUE_TYPE_I32
  5252. && global->init_expr.init_expr_type
  5253. == INIT_EXPR_TYPE_I32_CONST
  5254. && (uint64)(uint32)global->init_expr.u.i32
  5255. <= aux_heap_base) {
  5256. aux_stack_top_global = global;
  5257. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5258. module->aux_stack_top_global_index =
  5259. module->import_global_count + global_index;
  5260. module->aux_stack_bottom = aux_stack_top;
  5261. module->aux_stack_size =
  5262. aux_stack_top > aux_data_end
  5263. ? (uint32)(aux_stack_top - aux_data_end)
  5264. : (uint32)aux_stack_top;
  5265. LOG_VERBOSE(
  5266. "Found aux stack top global, value: %" PRIu64 ", "
  5267. "global index: %d, stack size: %d",
  5268. aux_stack_top, global_index,
  5269. module->aux_stack_size);
  5270. break;
  5271. }
  5272. }
  5273. if (!aux_stack_top_global) {
  5274. /* Auxiliary stack global isn't found, it must be unused
  5275. in the wasm app, as if it is used, the global must be
  5276. defined. Here we set it to __heap_base global and set
  5277. its size to 0. */
  5278. aux_stack_top_global = aux_heap_base_global;
  5279. aux_stack_top = aux_heap_base;
  5280. module->aux_stack_top_global_index =
  5281. module->aux_heap_base_global_index;
  5282. module->aux_stack_bottom = aux_stack_top;
  5283. module->aux_stack_size = 0;
  5284. }
  5285. break;
  5286. }
  5287. }
  5288. }
  5289. module->malloc_function = (uint32)-1;
  5290. module->free_function = (uint32)-1;
  5291. module->retain_function = (uint32)-1;
  5292. /* Resolve malloc/free function exported by wasm module */
  5293. #if WASM_ENABLE_MEMORY64 != 0
  5294. if (has_module_memory64(module))
  5295. malloc_free_io_type = VALUE_TYPE_I64;
  5296. #endif
  5297. export = module->exports;
  5298. for (i = 0; i < module->export_count; i++, export ++) {
  5299. if (export->kind == EXPORT_KIND_FUNC) {
  5300. if (!strcmp(export->name, "malloc")
  5301. && export->index >= module->import_function_count) {
  5302. func_index = export->index - module->import_function_count;
  5303. func_type = module->functions[func_index]->func_type;
  5304. if (func_type->param_count == 1 && func_type->result_count == 1
  5305. && func_type->types[0] == malloc_free_io_type
  5306. && func_type->types[1] == malloc_free_io_type) {
  5307. bh_assert(module->malloc_function == (uint32)-1);
  5308. module->malloc_function = export->index;
  5309. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5310. export->name, export->index);
  5311. }
  5312. }
  5313. else if (!strcmp(export->name, "__new")
  5314. && export->index >= module->import_function_count) {
  5315. /* __new && __pin for AssemblyScript */
  5316. func_index = export->index - module->import_function_count;
  5317. func_type = module->functions[func_index]->func_type;
  5318. if (func_type->param_count == 2 && func_type->result_count == 1
  5319. && func_type->types[0] == malloc_free_io_type
  5320. && func_type->types[1] == VALUE_TYPE_I32
  5321. && func_type->types[2] == malloc_free_io_type) {
  5322. uint32 j;
  5323. WASMExport *export_tmp;
  5324. bh_assert(module->malloc_function == (uint32)-1);
  5325. module->malloc_function = export->index;
  5326. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5327. export->name, export->index);
  5328. /* resolve retain function.
  5329. If not found, reset malloc function index */
  5330. export_tmp = module->exports;
  5331. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5332. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5333. && (!strcmp(export_tmp->name, "__retain")
  5334. || (!strcmp(export_tmp->name, "__pin")))
  5335. && (export_tmp->index
  5336. >= module->import_function_count)) {
  5337. func_index = export_tmp->index
  5338. - module->import_function_count;
  5339. func_type =
  5340. module->functions[func_index]->func_type;
  5341. if (func_type->param_count == 1
  5342. && func_type->result_count == 1
  5343. && func_type->types[0] == malloc_free_io_type
  5344. && func_type->types[1] == malloc_free_io_type) {
  5345. bh_assert(module->retain_function
  5346. == (uint32)-1);
  5347. module->retain_function = export_tmp->index;
  5348. LOG_VERBOSE("Found retain function, name: %s, "
  5349. "index: %u",
  5350. export_tmp->name,
  5351. export_tmp->index);
  5352. break;
  5353. }
  5354. }
  5355. }
  5356. if (j == module->export_count) {
  5357. module->malloc_function = (uint32)-1;
  5358. LOG_VERBOSE("Can't find retain function,"
  5359. "reset malloc function index to -1");
  5360. }
  5361. }
  5362. }
  5363. else if (((!strcmp(export->name, "free"))
  5364. || (!strcmp(export->name, "__release"))
  5365. || (!strcmp(export->name, "__unpin")))
  5366. && export->index >= module->import_function_count) {
  5367. func_index = export->index - module->import_function_count;
  5368. func_type = module->functions[func_index]->func_type;
  5369. if (func_type->param_count == 1 && func_type->result_count == 0
  5370. && func_type->types[0] == malloc_free_io_type) {
  5371. bh_assert(module->free_function == (uint32)-1);
  5372. module->free_function = export->index;
  5373. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5374. export->name, export->index);
  5375. }
  5376. }
  5377. }
  5378. }
  5379. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5380. handle_table = wasm_interp_get_handle_table();
  5381. #endif
  5382. for (i = 0; i < module->function_count; i++) {
  5383. WASMFunction *func = module->functions[i];
  5384. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5385. error_buf_size)) {
  5386. return false;
  5387. }
  5388. if (i == module->function_count - 1
  5389. && func->code + func->code_size != buf_code_end) {
  5390. set_error_buf(error_buf, error_buf_size,
  5391. "code section size mismatch");
  5392. return false;
  5393. }
  5394. }
  5395. if (!module->possible_memory_grow) {
  5396. WASMMemoryImport *memory_import;
  5397. WASMMemory *memory;
  5398. if (aux_data_end_global && aux_heap_base_global
  5399. && aux_stack_top_global) {
  5400. uint64 init_memory_size;
  5401. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5402. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5403. * valid range of uint32 */
  5404. if (shrunk_memory_size <= UINT32_MAX) {
  5405. if (module->import_memory_count) {
  5406. memory_import = &module->import_memories[0].u.memory;
  5407. init_memory_size = (uint64)memory_import->num_bytes_per_page
  5408. * memory_import->init_page_count;
  5409. if (shrunk_memory_size <= init_memory_size) {
  5410. /* Reset memory info to decrease memory usage */
  5411. memory_import->num_bytes_per_page =
  5412. (uint32)shrunk_memory_size;
  5413. memory_import->init_page_count = 1;
  5414. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5415. shrunk_memory_size);
  5416. }
  5417. }
  5418. if (module->memory_count) {
  5419. memory = &module->memories[0];
  5420. init_memory_size = (uint64)memory->num_bytes_per_page
  5421. * memory->init_page_count;
  5422. if (shrunk_memory_size <= init_memory_size) {
  5423. /* Reset memory info to decrease memory usage */
  5424. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5425. memory->init_page_count = 1;
  5426. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5427. shrunk_memory_size);
  5428. }
  5429. }
  5430. }
  5431. }
  5432. #if WASM_ENABLE_MULTI_MODULE == 0
  5433. if (module->import_memory_count) {
  5434. memory_import = &module->import_memories[0].u.memory;
  5435. /* Only resize the memory to one big page if num_bytes_per_page is
  5436. * in valid range of uint32 */
  5437. if (memory_import->init_page_count < DEFAULT_MAX_PAGES) {
  5438. memory_import->num_bytes_per_page *=
  5439. memory_import->init_page_count;
  5440. if (memory_import->init_page_count > 0)
  5441. memory_import->init_page_count =
  5442. memory_import->max_page_count = 1;
  5443. else
  5444. memory_import->init_page_count =
  5445. memory_import->max_page_count = 0;
  5446. }
  5447. }
  5448. if (module->memory_count) {
  5449. memory = &module->memories[0];
  5450. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5451. * valid range of uint32 */
  5452. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5453. memory->num_bytes_per_page *= memory->init_page_count;
  5454. if (memory->init_page_count > 0)
  5455. memory->init_page_count = memory->max_page_count = 1;
  5456. else
  5457. memory->init_page_count = memory->max_page_count = 0;
  5458. }
  5459. }
  5460. #endif
  5461. }
  5462. calculate_global_data_offset(module);
  5463. #if WASM_ENABLE_FAST_JIT != 0
  5464. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5465. return false;
  5466. }
  5467. #endif
  5468. #if WASM_ENABLE_JIT != 0
  5469. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5470. return false;
  5471. }
  5472. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5473. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5474. return false;
  5475. }
  5476. #else
  5477. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5478. thread fast jit execution, since applying llvm optimizations in
  5479. aot_compile_wasm may cost a lot of time.
  5480. Create thread with enough native stack to apply llvm optimizations */
  5481. if (os_thread_create(&module->llvm_jit_init_thread,
  5482. init_llvm_jit_functions_stage2_callback,
  5483. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5484. != 0) {
  5485. set_error_buf(error_buf, error_buf_size,
  5486. "create orcjit compile thread failed");
  5487. return false;
  5488. }
  5489. #endif
  5490. #endif
  5491. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5492. /* Create threads to compile the jit functions */
  5493. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5494. return false;
  5495. }
  5496. #endif
  5497. #if WASM_ENABLE_MEMORY_TRACING != 0
  5498. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5499. #endif
  5500. return true;
  5501. }
  5502. static WASMModule *
  5503. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5504. {
  5505. WASMModule *module =
  5506. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5507. bh_list_status ret;
  5508. if (!module) {
  5509. return NULL;
  5510. }
  5511. module->module_type = Wasm_Module_Bytecode;
  5512. /* Set start_function to -1, means no start function */
  5513. module->start_function = (uint32)-1;
  5514. module->name = name;
  5515. module->is_binary_freeable = false;
  5516. #if WASM_ENABLE_FAST_INTERP == 0
  5517. module->br_table_cache_list = &module->br_table_cache_list_head;
  5518. ret = bh_list_init(module->br_table_cache_list);
  5519. bh_assert(ret == BH_LIST_SUCCESS);
  5520. #endif
  5521. #if WASM_ENABLE_MULTI_MODULE != 0
  5522. module->import_module_list = &module->import_module_list_head;
  5523. ret = bh_list_init(module->import_module_list);
  5524. bh_assert(ret == BH_LIST_SUCCESS);
  5525. #endif
  5526. #if WASM_ENABLE_DEBUG_INTERP != 0
  5527. ret = bh_list_init(&module->fast_opcode_list);
  5528. bh_assert(ret == BH_LIST_SUCCESS);
  5529. #endif
  5530. #if WASM_ENABLE_GC != 0
  5531. if (!(module->ref_type_set =
  5532. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5533. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5534. goto fail1;
  5535. }
  5536. if (os_mutex_init(&module->rtt_type_lock)) {
  5537. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5538. goto fail2;
  5539. }
  5540. #endif
  5541. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5542. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5543. && WASM_ENABLE_LAZY_JIT != 0)
  5544. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5545. set_error_buf(error_buf, error_buf_size,
  5546. "init instance list lock failed");
  5547. goto fail3;
  5548. }
  5549. #endif
  5550. (void)ret;
  5551. return module;
  5552. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5553. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5554. && WASM_ENABLE_LAZY_JIT != 0)
  5555. fail3:
  5556. #endif
  5557. #if WASM_ENABLE_GC != 0
  5558. os_mutex_destroy(&module->rtt_type_lock);
  5559. fail2:
  5560. bh_hash_map_destroy(module->ref_type_set);
  5561. fail1:
  5562. #endif
  5563. wasm_runtime_free(module);
  5564. return NULL;
  5565. }
  5566. #if WASM_ENABLE_DEBUG_INTERP != 0
  5567. static bool
  5568. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5569. uint32 error_buf_size)
  5570. {
  5571. WASMFastOPCodeNode *fast_op =
  5572. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5573. if (fast_op) {
  5574. fast_op->offset = pos - module->load_addr;
  5575. fast_op->orig_op = orig_op;
  5576. bh_list_insert(&module->fast_opcode_list, fast_op);
  5577. }
  5578. return fast_op ? true : false;
  5579. }
  5580. #endif
  5581. WASMModule *
  5582. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5583. uint32 error_buf_size)
  5584. {
  5585. WASMModule *module = create_module("", error_buf, error_buf_size);
  5586. if (!module)
  5587. return NULL;
  5588. if (!load_from_sections(module, section_list, false, error_buf,
  5589. error_buf_size)) {
  5590. wasm_loader_unload(module);
  5591. return NULL;
  5592. }
  5593. LOG_VERBOSE("Load module from sections success.\n");
  5594. return module;
  5595. }
  5596. static void
  5597. destroy_sections(WASMSection *section_list)
  5598. {
  5599. WASMSection *section = section_list, *next;
  5600. while (section) {
  5601. next = section->next;
  5602. wasm_runtime_free(section);
  5603. section = next;
  5604. }
  5605. }
  5606. /* clang-format off */
  5607. static uint8 section_ids[] = {
  5608. SECTION_TYPE_USER,
  5609. SECTION_TYPE_TYPE,
  5610. SECTION_TYPE_IMPORT,
  5611. SECTION_TYPE_FUNC,
  5612. SECTION_TYPE_TABLE,
  5613. SECTION_TYPE_MEMORY,
  5614. #if WASM_ENABLE_TAGS != 0
  5615. SECTION_TYPE_TAG,
  5616. #endif
  5617. #if WASM_ENABLE_STRINGREF != 0
  5618. /* must immediately precede the global section,
  5619. or where the global section would be */
  5620. SECTION_TYPE_STRINGREF,
  5621. #endif
  5622. SECTION_TYPE_GLOBAL,
  5623. SECTION_TYPE_EXPORT,
  5624. SECTION_TYPE_START,
  5625. SECTION_TYPE_ELEM,
  5626. #if WASM_ENABLE_BULK_MEMORY != 0
  5627. SECTION_TYPE_DATACOUNT,
  5628. #endif
  5629. SECTION_TYPE_CODE,
  5630. SECTION_TYPE_DATA
  5631. };
  5632. /* clang-format on */
  5633. static uint8
  5634. get_section_index(uint8 section_type)
  5635. {
  5636. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5637. for (uint8 i = 0; i < max_id; i++) {
  5638. if (section_type == section_ids[i])
  5639. return i;
  5640. }
  5641. return (uint8)-1;
  5642. }
  5643. static bool
  5644. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5645. char *error_buf, uint32 error_buf_size)
  5646. {
  5647. WASMSection *section_list_end = NULL, *section;
  5648. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  5649. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5650. uint32 section_size;
  5651. bh_assert(!*p_section_list);
  5652. p += 8;
  5653. while (p < p_end) {
  5654. CHECK_BUF(p, p_end, 1);
  5655. section_type = read_uint8(p);
  5656. section_index = get_section_index(section_type);
  5657. if (section_index != (uint8)-1) {
  5658. if (section_type != SECTION_TYPE_USER) {
  5659. /* Custom sections may be inserted at any place,
  5660. while other sections must occur at most once
  5661. and in prescribed order. */
  5662. if (last_section_index != (uint8)-1
  5663. && (section_index <= last_section_index)) {
  5664. set_error_buf(error_buf, error_buf_size,
  5665. "unexpected content after last section or "
  5666. "junk after last section");
  5667. return false;
  5668. }
  5669. last_section_index = section_index;
  5670. }
  5671. read_leb_uint32(p, p_end, section_size);
  5672. CHECK_BUF1(p, p_end, section_size);
  5673. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5674. error_buf_size))) {
  5675. return false;
  5676. }
  5677. section->section_type = section_type;
  5678. section->section_body = (uint8 *)p;
  5679. section->section_body_size = section_size;
  5680. if (!section_list_end)
  5681. *p_section_list = section_list_end = section;
  5682. else {
  5683. section_list_end->next = section;
  5684. section_list_end = section;
  5685. }
  5686. p += section_size;
  5687. }
  5688. else {
  5689. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5690. return false;
  5691. }
  5692. }
  5693. return true;
  5694. fail:
  5695. return false;
  5696. }
  5697. static void
  5698. exchange32(uint8 *p_data)
  5699. {
  5700. uint8 value = *p_data;
  5701. *p_data = *(p_data + 3);
  5702. *(p_data + 3) = value;
  5703. value = *(p_data + 1);
  5704. *(p_data + 1) = *(p_data + 2);
  5705. *(p_data + 2) = value;
  5706. }
  5707. static union {
  5708. int a;
  5709. char b;
  5710. } __ue = { .a = 1 };
  5711. #define is_little_endian() (__ue.b == 1)
  5712. static bool
  5713. load(const uint8 *buf, uint32 size, WASMModule *module,
  5714. bool wasm_binary_freeable, char *error_buf, uint32 error_buf_size)
  5715. {
  5716. const uint8 *buf_end = buf + size;
  5717. const uint8 *p = buf, *p_end = buf_end;
  5718. uint32 magic_number, version;
  5719. WASMSection *section_list = NULL;
  5720. CHECK_BUF1(p, p_end, sizeof(uint32));
  5721. magic_number = read_uint32(p);
  5722. if (!is_little_endian())
  5723. exchange32((uint8 *)&magic_number);
  5724. if (magic_number != WASM_MAGIC_NUMBER) {
  5725. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5726. return false;
  5727. }
  5728. CHECK_BUF1(p, p_end, sizeof(uint32));
  5729. version = read_uint32(p);
  5730. if (!is_little_endian())
  5731. exchange32((uint8 *)&version);
  5732. if (version != WASM_CURRENT_VERSION) {
  5733. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5734. return false;
  5735. }
  5736. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5737. || !load_from_sections(module, section_list, !wasm_binary_freeable,
  5738. error_buf, error_buf_size)) {
  5739. destroy_sections(section_list);
  5740. return false;
  5741. }
  5742. destroy_sections(section_list);
  5743. return true;
  5744. fail:
  5745. return false;
  5746. }
  5747. #if WASM_ENABLE_LIBC_WASI != 0
  5748. /**
  5749. * refer to
  5750. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5751. */
  5752. static bool
  5753. check_wasi_abi_compatibility(const WASMModule *module,
  5754. #if WASM_ENABLE_MULTI_MODULE != 0
  5755. bool main_module,
  5756. #endif
  5757. char *error_buf, uint32 error_buf_size)
  5758. {
  5759. /**
  5760. * be careful with:
  5761. * wasi compatible modules(command/reactor) which don't import any wasi
  5762. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5763. * reactor can depend on nothing. At the same time, each has its own entry
  5764. * point.
  5765. *
  5766. * observations:
  5767. * - clang always injects `_start` into a command
  5768. * - clang always injects `_initialize` into a reactor
  5769. * - `iwasm -f` allows to run a function in the reactor
  5770. *
  5771. * strong assumptions:
  5772. * - no one will define either `_start` or `_initialize` on purpose
  5773. * - `_start` should always be `void _start(void)`
  5774. * - `_initialize` should always be `void _initialize(void)`
  5775. *
  5776. */
  5777. /* clang-format off */
  5778. /**
  5779. *
  5780. * | | import_wasi_api True | | import_wasi_api False | |
  5781. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5782. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5783. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5784. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5785. */
  5786. /* clang-format on */
  5787. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5788. uint32 import_function_count = module->import_function_count;
  5789. WASMFuncType *func_type;
  5790. /* (func (export "_start") (...) */
  5791. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5792. error_buf, error_buf_size);
  5793. if (start) {
  5794. if (start->index < import_function_count) {
  5795. set_error_buf(
  5796. error_buf, error_buf_size,
  5797. "the builtin _start function can not be an import function");
  5798. return false;
  5799. }
  5800. func_type =
  5801. module->functions[start->index - import_function_count]->func_type;
  5802. if (func_type->param_count || func_type->result_count) {
  5803. set_error_buf(error_buf, error_buf_size,
  5804. "the signature of builtin _start function is wrong");
  5805. return false;
  5806. }
  5807. }
  5808. else {
  5809. /* (func (export "_initialize") (...) */
  5810. initialize =
  5811. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5812. error_buf, error_buf_size);
  5813. if (initialize) {
  5814. if (initialize->index < import_function_count) {
  5815. set_error_buf(error_buf, error_buf_size,
  5816. "the builtin _initialize function can not be an "
  5817. "import function");
  5818. return false;
  5819. }
  5820. func_type =
  5821. module->functions[initialize->index - import_function_count]
  5822. ->func_type;
  5823. if (func_type->param_count || func_type->result_count) {
  5824. set_error_buf(
  5825. error_buf, error_buf_size,
  5826. "the signature of builtin _initialize function is wrong");
  5827. return false;
  5828. }
  5829. }
  5830. }
  5831. /* filter out non-wasi compatible modules */
  5832. if (!module->import_wasi_api && !start && !initialize) {
  5833. return true;
  5834. }
  5835. /* should have one at least */
  5836. if (module->import_wasi_api && !start && !initialize) {
  5837. LOG_WARNING("warning: a module with WASI apis should be either "
  5838. "a command or a reactor");
  5839. }
  5840. /*
  5841. * there is at least one of `_start` and `_initialize` in below cases.
  5842. * according to the assumption, they should be all wasi compatible
  5843. */
  5844. #if WASM_ENABLE_MULTI_MODULE != 0
  5845. /* filter out commands (with `_start`) cases */
  5846. if (start && !main_module) {
  5847. set_error_buf(
  5848. error_buf, error_buf_size,
  5849. "a command (with _start function) can not be a sub-module");
  5850. return false;
  5851. }
  5852. #endif
  5853. /*
  5854. * it is ok a reactor acts as a main module,
  5855. * so skip the check about (with `_initialize`)
  5856. */
  5857. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5858. error_buf, error_buf_size);
  5859. if (!memory
  5860. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5861. /*
  5862. * with wasi-threads, it's still an open question if a memory
  5863. * should be exported.
  5864. *
  5865. * https://github.com/WebAssembly/wasi-threads/issues/22
  5866. * https://github.com/WebAssembly/WASI/issues/502
  5867. *
  5868. * Note: this code assumes the number of memories is at most 1.
  5869. */
  5870. && module->import_memory_count == 0
  5871. #endif
  5872. ) {
  5873. set_error_buf(error_buf, error_buf_size,
  5874. "a module with WASI apis must export memory by default");
  5875. return false;
  5876. }
  5877. return true;
  5878. }
  5879. #endif
  5880. WASMModule *
  5881. wasm_loader_load(uint8 *buf, uint32 size,
  5882. #if WASM_ENABLE_MULTI_MODULE != 0
  5883. bool main_module,
  5884. #endif
  5885. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5886. {
  5887. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5888. if (!module) {
  5889. return NULL;
  5890. }
  5891. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5892. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5893. module->load_addr = (uint8 *)buf;
  5894. module->load_size = size;
  5895. #endif
  5896. if (!load(buf, size, module, args->wasm_binary_freeable, error_buf,
  5897. error_buf_size)) {
  5898. goto fail;
  5899. }
  5900. #if WASM_ENABLE_LIBC_WASI != 0
  5901. /* Check the WASI application ABI */
  5902. if (!check_wasi_abi_compatibility(module,
  5903. #if WASM_ENABLE_MULTI_MODULE != 0
  5904. main_module,
  5905. #endif
  5906. error_buf, error_buf_size)) {
  5907. goto fail;
  5908. }
  5909. #endif
  5910. LOG_VERBOSE("Load module success.\n");
  5911. return module;
  5912. fail:
  5913. wasm_loader_unload(module);
  5914. return NULL;
  5915. }
  5916. void
  5917. wasm_loader_unload(WASMModule *module)
  5918. {
  5919. uint32 i;
  5920. if (!module)
  5921. return;
  5922. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5923. && WASM_ENABLE_LAZY_JIT != 0
  5924. module->orcjit_stop_compiling = true;
  5925. if (module->llvm_jit_init_thread)
  5926. os_thread_join(module->llvm_jit_init_thread, NULL);
  5927. #endif
  5928. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5929. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5930. module internal data after they were freed */
  5931. orcjit_stop_compile_threads(module);
  5932. #endif
  5933. #if WASM_ENABLE_JIT != 0
  5934. if (module->func_ptrs)
  5935. wasm_runtime_free(module->func_ptrs);
  5936. if (module->comp_ctx)
  5937. aot_destroy_comp_context(module->comp_ctx);
  5938. if (module->comp_data)
  5939. aot_destroy_comp_data(module->comp_data);
  5940. #endif
  5941. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5942. && WASM_ENABLE_LAZY_JIT != 0
  5943. if (module->tierup_wait_lock_inited) {
  5944. os_mutex_destroy(&module->tierup_wait_lock);
  5945. os_cond_destroy(&module->tierup_wait_cond);
  5946. }
  5947. #endif
  5948. if (module->types) {
  5949. for (i = 0; i < module->type_count; i++) {
  5950. if (module->types[i])
  5951. destroy_wasm_type(module->types[i]);
  5952. }
  5953. wasm_runtime_free(module->types);
  5954. }
  5955. if (module->imports)
  5956. wasm_runtime_free(module->imports);
  5957. if (module->functions) {
  5958. for (i = 0; i < module->function_count; i++) {
  5959. if (module->functions[i]) {
  5960. if (module->functions[i]->local_offsets)
  5961. wasm_runtime_free(module->functions[i]->local_offsets);
  5962. #if WASM_ENABLE_FAST_INTERP != 0
  5963. if (module->functions[i]->code_compiled)
  5964. wasm_runtime_free(module->functions[i]->code_compiled);
  5965. if (module->functions[i]->consts)
  5966. wasm_runtime_free(module->functions[i]->consts);
  5967. #endif
  5968. #if WASM_ENABLE_FAST_JIT != 0
  5969. if (module->functions[i]->fast_jit_jitted_code) {
  5970. jit_code_cache_free(
  5971. module->functions[i]->fast_jit_jitted_code);
  5972. }
  5973. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5974. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5975. jit_code_cache_free(
  5976. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5977. }
  5978. #endif
  5979. #endif
  5980. #if WASM_ENABLE_GC != 0
  5981. if (module->functions[i]->local_ref_type_maps) {
  5982. wasm_runtime_free(
  5983. module->functions[i]->local_ref_type_maps);
  5984. }
  5985. #endif
  5986. wasm_runtime_free(module->functions[i]);
  5987. }
  5988. }
  5989. wasm_runtime_free(module->functions);
  5990. }
  5991. if (module->tables) {
  5992. #if WASM_ENABLE_GC != 0
  5993. for (i = 0; i < module->table_count; i++) {
  5994. destroy_init_expr(&module->tables[i].init_expr);
  5995. }
  5996. #endif
  5997. wasm_runtime_free(module->tables);
  5998. }
  5999. if (module->memories)
  6000. wasm_runtime_free(module->memories);
  6001. if (module->globals) {
  6002. #if WASM_ENABLE_GC != 0
  6003. for (i = 0; i < module->global_count; i++) {
  6004. destroy_init_expr(&module->globals[i].init_expr);
  6005. }
  6006. #endif
  6007. wasm_runtime_free(module->globals);
  6008. }
  6009. #if WASM_ENABLE_TAGS != 0
  6010. if (module->tags) {
  6011. for (i = 0; i < module->tag_count; i++) {
  6012. if (module->tags[i])
  6013. wasm_runtime_free(module->tags[i]);
  6014. }
  6015. wasm_runtime_free(module->tags);
  6016. }
  6017. #endif
  6018. if (module->exports)
  6019. wasm_runtime_free(module->exports);
  6020. if (module->table_segments) {
  6021. for (i = 0; i < module->table_seg_count; i++) {
  6022. if (module->table_segments[i].init_values) {
  6023. #if WASM_ENABLE_GC != 0
  6024. uint32 j;
  6025. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6026. destroy_init_expr(
  6027. &module->table_segments[i].init_values[j]);
  6028. }
  6029. #endif
  6030. wasm_runtime_free(module->table_segments[i].init_values);
  6031. }
  6032. }
  6033. wasm_runtime_free(module->table_segments);
  6034. }
  6035. if (module->data_segments) {
  6036. for (i = 0; i < module->data_seg_count; i++) {
  6037. if (module->data_segments[i])
  6038. wasm_runtime_free(module->data_segments[i]);
  6039. }
  6040. wasm_runtime_free(module->data_segments);
  6041. }
  6042. if (module->const_str_list) {
  6043. StringNode *node = module->const_str_list, *node_next;
  6044. while (node) {
  6045. node_next = node->next;
  6046. wasm_runtime_free(node);
  6047. node = node_next;
  6048. }
  6049. }
  6050. #if WASM_ENABLE_STRINGREF != 0
  6051. if (module->string_literal_ptrs) {
  6052. wasm_runtime_free((void *)module->string_literal_ptrs);
  6053. }
  6054. if (module->string_literal_lengths) {
  6055. wasm_runtime_free(module->string_literal_lengths);
  6056. }
  6057. #endif
  6058. #if WASM_ENABLE_FAST_INTERP == 0
  6059. if (module->br_table_cache_list) {
  6060. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6061. BrTableCache *node_next;
  6062. while (node) {
  6063. node_next = bh_list_elem_next(node);
  6064. wasm_runtime_free(node);
  6065. node = node_next;
  6066. }
  6067. }
  6068. #endif
  6069. #if WASM_ENABLE_MULTI_MODULE != 0
  6070. /* just release the sub module list */
  6071. if (module->import_module_list) {
  6072. WASMRegisteredModule *node =
  6073. bh_list_first_elem(module->import_module_list);
  6074. while (node) {
  6075. WASMRegisteredModule *next = bh_list_elem_next(node);
  6076. bh_list_remove(module->import_module_list, node);
  6077. /*
  6078. * unload(sub_module) will be triggered during runtime_destroy().
  6079. * every module in the global module list will be unloaded one by
  6080. * one. so don't worry.
  6081. */
  6082. wasm_runtime_free(node);
  6083. /*
  6084. * the module file reading buffer will be released
  6085. * in runtime_destroy()
  6086. */
  6087. node = next;
  6088. }
  6089. }
  6090. #endif
  6091. #if WASM_ENABLE_DEBUG_INTERP != 0
  6092. WASMFastOPCodeNode *fast_opcode =
  6093. bh_list_first_elem(&module->fast_opcode_list);
  6094. while (fast_opcode) {
  6095. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6096. wasm_runtime_free(fast_opcode);
  6097. fast_opcode = next;
  6098. }
  6099. #endif
  6100. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6101. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6102. && WASM_ENABLE_LAZY_JIT != 0)
  6103. os_mutex_destroy(&module->instance_list_lock);
  6104. #endif
  6105. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6106. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6107. #endif
  6108. #if WASM_ENABLE_FAST_JIT != 0
  6109. if (module->fast_jit_func_ptrs) {
  6110. wasm_runtime_free(module->fast_jit_func_ptrs);
  6111. }
  6112. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6113. if (module->fast_jit_thread_locks_inited[i]) {
  6114. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6115. }
  6116. }
  6117. #endif
  6118. #if WASM_ENABLE_GC != 0
  6119. os_mutex_destroy(&module->rtt_type_lock);
  6120. bh_hash_map_destroy(module->ref_type_set);
  6121. if (module->rtt_types) {
  6122. for (i = 0; i < module->type_count; i++) {
  6123. if (module->rtt_types[i])
  6124. wasm_runtime_free(module->rtt_types[i]);
  6125. }
  6126. wasm_runtime_free(module->rtt_types);
  6127. }
  6128. #if WASM_ENABLE_STRINGREF != 0
  6129. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6130. if (module->stringref_rtts[i])
  6131. wasm_runtime_free(module->stringref_rtts[i]);
  6132. }
  6133. #endif
  6134. #endif
  6135. wasm_runtime_free(module);
  6136. }
  6137. bool
  6138. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6139. const uint8 *start_addr, const uint8 *code_end_addr,
  6140. uint8 label_type, uint8 **p_else_addr,
  6141. uint8 **p_end_addr)
  6142. {
  6143. const uint8 *p = start_addr, *p_end = code_end_addr;
  6144. uint8 *else_addr = NULL;
  6145. char error_buf[128];
  6146. uint32 block_nested_depth = 1, count, i, j, t;
  6147. uint32 error_buf_size = sizeof(error_buf);
  6148. uint8 opcode, u8;
  6149. BlockAddr block_stack[16] = { { 0 } }, *block;
  6150. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6151. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6152. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6153. if (block[j].start_addr == start_addr) {
  6154. /* Cache hit */
  6155. *p_else_addr = block[j].else_addr;
  6156. *p_end_addr = block[j].end_addr;
  6157. return true;
  6158. }
  6159. }
  6160. /* Cache unhit */
  6161. block_stack[0].start_addr = start_addr;
  6162. while (p < code_end_addr) {
  6163. opcode = *p++;
  6164. #if WASM_ENABLE_DEBUG_INTERP != 0
  6165. op_break_retry:
  6166. #endif
  6167. switch (opcode) {
  6168. case WASM_OP_UNREACHABLE:
  6169. case WASM_OP_NOP:
  6170. break;
  6171. #if WASM_ENABLE_EXCE_HANDLING != 0
  6172. case WASM_OP_TRY:
  6173. u8 = read_uint8(p);
  6174. if (block_nested_depth
  6175. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6176. block_stack[block_nested_depth].start_addr = p;
  6177. block_stack[block_nested_depth].else_addr = NULL;
  6178. }
  6179. block_nested_depth++;
  6180. break;
  6181. case EXT_OP_TRY:
  6182. skip_leb_uint32(p, p_end);
  6183. if (block_nested_depth
  6184. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6185. block_stack[block_nested_depth].start_addr = p;
  6186. block_stack[block_nested_depth].else_addr = NULL;
  6187. }
  6188. block_nested_depth++;
  6189. break;
  6190. case WASM_OP_CATCH:
  6191. if (block_nested_depth == 1) {
  6192. *p_end_addr = (uint8 *)(p - 1);
  6193. /* stop search and return the address of the catch block */
  6194. return true;
  6195. }
  6196. break;
  6197. case WASM_OP_CATCH_ALL:
  6198. if (block_nested_depth == 1) {
  6199. *p_end_addr = (uint8 *)(p - 1);
  6200. /* stop search and return the address of the catch_all block
  6201. */
  6202. return true;
  6203. }
  6204. break;
  6205. case WASM_OP_THROW:
  6206. /* skip tag_index */
  6207. skip_leb(p);
  6208. break;
  6209. case WASM_OP_RETHROW:
  6210. /* skip depth */
  6211. skip_leb(p);
  6212. break;
  6213. case WASM_OP_DELEGATE:
  6214. if (block_nested_depth == 1) {
  6215. *p_end_addr = (uint8 *)(p - 1);
  6216. return true;
  6217. }
  6218. else {
  6219. skip_leb(p);
  6220. /* the DELEGATE opcode ends the tryblock, */
  6221. block_nested_depth--;
  6222. if (block_nested_depth
  6223. < sizeof(block_stack) / sizeof(BlockAddr))
  6224. block_stack[block_nested_depth].end_addr =
  6225. (uint8 *)(p - 1);
  6226. }
  6227. break;
  6228. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6229. case WASM_OP_BLOCK:
  6230. case WASM_OP_LOOP:
  6231. case WASM_OP_IF:
  6232. {
  6233. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6234. u8 = read_uint8(p);
  6235. if (is_byte_a_type(u8)) {
  6236. #if WASM_ENABLE_GC != 0
  6237. if (wasm_is_type_multi_byte_type(u8)) {
  6238. /* the possible extra bytes of GC ref type have been
  6239. modified to OP_NOP, no need to resolve them again */
  6240. }
  6241. #endif
  6242. }
  6243. else {
  6244. p--;
  6245. skip_leb_uint32(p, p_end);
  6246. }
  6247. if (block_nested_depth
  6248. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6249. block_stack[block_nested_depth].start_addr = p;
  6250. block_stack[block_nested_depth].else_addr = NULL;
  6251. }
  6252. block_nested_depth++;
  6253. break;
  6254. }
  6255. case EXT_OP_BLOCK:
  6256. case EXT_OP_LOOP:
  6257. case EXT_OP_IF:
  6258. /* block type */
  6259. skip_leb_uint32(p, p_end);
  6260. if (block_nested_depth
  6261. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6262. block_stack[block_nested_depth].start_addr = p;
  6263. block_stack[block_nested_depth].else_addr = NULL;
  6264. }
  6265. block_nested_depth++;
  6266. break;
  6267. case WASM_OP_ELSE:
  6268. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6269. else_addr = (uint8 *)(p - 1);
  6270. if (block_nested_depth - 1
  6271. < sizeof(block_stack) / sizeof(BlockAddr))
  6272. block_stack[block_nested_depth - 1].else_addr =
  6273. (uint8 *)(p - 1);
  6274. break;
  6275. case WASM_OP_END:
  6276. if (block_nested_depth == 1) {
  6277. if (label_type == LABEL_TYPE_IF)
  6278. *p_else_addr = else_addr;
  6279. *p_end_addr = (uint8 *)(p - 1);
  6280. block_stack[0].end_addr = (uint8 *)(p - 1);
  6281. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6282. t++) {
  6283. start_addr = block_stack[t].start_addr;
  6284. if (start_addr) {
  6285. i = ((uintptr_t)start_addr)
  6286. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6287. block =
  6288. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6289. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6290. if (!block[j].start_addr)
  6291. break;
  6292. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6293. memmove(block + 1, block,
  6294. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6295. * sizeof(BlockAddr));
  6296. j = 0;
  6297. }
  6298. block[j].start_addr = block_stack[t].start_addr;
  6299. block[j].else_addr = block_stack[t].else_addr;
  6300. block[j].end_addr = block_stack[t].end_addr;
  6301. }
  6302. else
  6303. break;
  6304. }
  6305. return true;
  6306. }
  6307. else {
  6308. block_nested_depth--;
  6309. if (block_nested_depth
  6310. < sizeof(block_stack) / sizeof(BlockAddr))
  6311. block_stack[block_nested_depth].end_addr =
  6312. (uint8 *)(p - 1);
  6313. }
  6314. break;
  6315. case WASM_OP_BR:
  6316. case WASM_OP_BR_IF:
  6317. skip_leb_uint32(p, p_end); /* labelidx */
  6318. break;
  6319. case WASM_OP_BR_TABLE:
  6320. read_leb_uint32(p, p_end, count); /* label num */
  6321. #if WASM_ENABLE_FAST_INTERP != 0
  6322. for (i = 0; i <= count; i++) /* labelidxs */
  6323. skip_leb_uint32(p, p_end);
  6324. #else
  6325. p += count + 1;
  6326. while (*p == WASM_OP_NOP)
  6327. p++;
  6328. #endif
  6329. break;
  6330. #if WASM_ENABLE_FAST_INTERP == 0
  6331. case EXT_OP_BR_TABLE_CACHE:
  6332. read_leb_uint32(p, p_end, count); /* label num */
  6333. while (*p == WASM_OP_NOP)
  6334. p++;
  6335. break;
  6336. #endif
  6337. case WASM_OP_RETURN:
  6338. break;
  6339. case WASM_OP_CALL:
  6340. #if WASM_ENABLE_TAIL_CALL != 0
  6341. case WASM_OP_RETURN_CALL:
  6342. #endif
  6343. skip_leb_uint32(p, p_end); /* funcidx */
  6344. break;
  6345. case WASM_OP_CALL_INDIRECT:
  6346. #if WASM_ENABLE_TAIL_CALL != 0
  6347. case WASM_OP_RETURN_CALL_INDIRECT:
  6348. #endif
  6349. skip_leb_uint32(p, p_end); /* typeidx */
  6350. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6351. u8 = read_uint8(p); /* 0x00 */
  6352. #else
  6353. skip_leb_uint32(p, p_end); /* tableidx */
  6354. #endif
  6355. break;
  6356. #if WASM_ENABLE_GC != 0
  6357. case WASM_OP_CALL_REF:
  6358. case WASM_OP_RETURN_CALL_REF:
  6359. skip_leb_uint32(p, p_end); /* typeidx */
  6360. break;
  6361. #endif
  6362. case WASM_OP_DROP:
  6363. case WASM_OP_SELECT:
  6364. case WASM_OP_DROP_64:
  6365. case WASM_OP_SELECT_64:
  6366. break;
  6367. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6368. case WASM_OP_SELECT_T:
  6369. {
  6370. skip_leb_uint32(p, p_end); /* vec length */
  6371. u8 = read_uint8(p); /* typeidx */
  6372. /* the possible extra bytes of GC ref type have been
  6373. modified to OP_NOP, no need to resolve them again */
  6374. break;
  6375. }
  6376. case WASM_OP_TABLE_GET:
  6377. case WASM_OP_TABLE_SET:
  6378. skip_leb_uint32(p, p_end); /* table index */
  6379. break;
  6380. case WASM_OP_REF_NULL:
  6381. {
  6382. u8 = read_uint8(p); /* type */
  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. skip_leb_uint32(p, p_end);
  6394. }
  6395. break;
  6396. }
  6397. case WASM_OP_REF_IS_NULL:
  6398. break;
  6399. case WASM_OP_REF_FUNC:
  6400. skip_leb_uint32(p, p_end); /* func index */
  6401. break;
  6402. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6403. #if WASM_ENABLE_GC != 0
  6404. case WASM_OP_REF_AS_NON_NULL:
  6405. case WASM_OP_REF_EQ:
  6406. break;
  6407. case WASM_OP_BR_ON_NULL:
  6408. case WASM_OP_BR_ON_NON_NULL:
  6409. skip_leb_uint32(p, p_end); /* label index */
  6410. break;
  6411. #endif /* end of WASM_ENABLE_GC != 0 */
  6412. case WASM_OP_GET_LOCAL:
  6413. case WASM_OP_SET_LOCAL:
  6414. case WASM_OP_TEE_LOCAL:
  6415. case WASM_OP_GET_GLOBAL:
  6416. case WASM_OP_SET_GLOBAL:
  6417. case WASM_OP_GET_GLOBAL_64:
  6418. case WASM_OP_SET_GLOBAL_64:
  6419. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6420. skip_leb_uint32(p, p_end); /* local index */
  6421. break;
  6422. case EXT_OP_GET_LOCAL_FAST:
  6423. case EXT_OP_SET_LOCAL_FAST:
  6424. case EXT_OP_TEE_LOCAL_FAST:
  6425. CHECK_BUF(p, p_end, 1);
  6426. p++;
  6427. break;
  6428. case WASM_OP_I32_LOAD:
  6429. case WASM_OP_I64_LOAD:
  6430. case WASM_OP_F32_LOAD:
  6431. case WASM_OP_F64_LOAD:
  6432. case WASM_OP_I32_LOAD8_S:
  6433. case WASM_OP_I32_LOAD8_U:
  6434. case WASM_OP_I32_LOAD16_S:
  6435. case WASM_OP_I32_LOAD16_U:
  6436. case WASM_OP_I64_LOAD8_S:
  6437. case WASM_OP_I64_LOAD8_U:
  6438. case WASM_OP_I64_LOAD16_S:
  6439. case WASM_OP_I64_LOAD16_U:
  6440. case WASM_OP_I64_LOAD32_S:
  6441. case WASM_OP_I64_LOAD32_U:
  6442. case WASM_OP_I32_STORE:
  6443. case WASM_OP_I64_STORE:
  6444. case WASM_OP_F32_STORE:
  6445. case WASM_OP_F64_STORE:
  6446. case WASM_OP_I32_STORE8:
  6447. case WASM_OP_I32_STORE16:
  6448. case WASM_OP_I64_STORE8:
  6449. case WASM_OP_I64_STORE16:
  6450. case WASM_OP_I64_STORE32:
  6451. skip_leb_uint32(p, p_end); /* align */
  6452. skip_leb_mem_offset(p, p_end); /* offset */
  6453. break;
  6454. case WASM_OP_MEMORY_SIZE:
  6455. case WASM_OP_MEMORY_GROW:
  6456. skip_leb_uint32(p, p_end); /* 0x00 */
  6457. break;
  6458. case WASM_OP_I32_CONST:
  6459. skip_leb_int32(p, p_end);
  6460. break;
  6461. case WASM_OP_I64_CONST:
  6462. skip_leb_int64(p, p_end);
  6463. break;
  6464. case WASM_OP_F32_CONST:
  6465. p += sizeof(float32);
  6466. break;
  6467. case WASM_OP_F64_CONST:
  6468. p += sizeof(float64);
  6469. break;
  6470. case WASM_OP_I32_EQZ:
  6471. case WASM_OP_I32_EQ:
  6472. case WASM_OP_I32_NE:
  6473. case WASM_OP_I32_LT_S:
  6474. case WASM_OP_I32_LT_U:
  6475. case WASM_OP_I32_GT_S:
  6476. case WASM_OP_I32_GT_U:
  6477. case WASM_OP_I32_LE_S:
  6478. case WASM_OP_I32_LE_U:
  6479. case WASM_OP_I32_GE_S:
  6480. case WASM_OP_I32_GE_U:
  6481. case WASM_OP_I64_EQZ:
  6482. case WASM_OP_I64_EQ:
  6483. case WASM_OP_I64_NE:
  6484. case WASM_OP_I64_LT_S:
  6485. case WASM_OP_I64_LT_U:
  6486. case WASM_OP_I64_GT_S:
  6487. case WASM_OP_I64_GT_U:
  6488. case WASM_OP_I64_LE_S:
  6489. case WASM_OP_I64_LE_U:
  6490. case WASM_OP_I64_GE_S:
  6491. case WASM_OP_I64_GE_U:
  6492. case WASM_OP_F32_EQ:
  6493. case WASM_OP_F32_NE:
  6494. case WASM_OP_F32_LT:
  6495. case WASM_OP_F32_GT:
  6496. case WASM_OP_F32_LE:
  6497. case WASM_OP_F32_GE:
  6498. case WASM_OP_F64_EQ:
  6499. case WASM_OP_F64_NE:
  6500. case WASM_OP_F64_LT:
  6501. case WASM_OP_F64_GT:
  6502. case WASM_OP_F64_LE:
  6503. case WASM_OP_F64_GE:
  6504. case WASM_OP_I32_CLZ:
  6505. case WASM_OP_I32_CTZ:
  6506. case WASM_OP_I32_POPCNT:
  6507. case WASM_OP_I32_ADD:
  6508. case WASM_OP_I32_SUB:
  6509. case WASM_OP_I32_MUL:
  6510. case WASM_OP_I32_DIV_S:
  6511. case WASM_OP_I32_DIV_U:
  6512. case WASM_OP_I32_REM_S:
  6513. case WASM_OP_I32_REM_U:
  6514. case WASM_OP_I32_AND:
  6515. case WASM_OP_I32_OR:
  6516. case WASM_OP_I32_XOR:
  6517. case WASM_OP_I32_SHL:
  6518. case WASM_OP_I32_SHR_S:
  6519. case WASM_OP_I32_SHR_U:
  6520. case WASM_OP_I32_ROTL:
  6521. case WASM_OP_I32_ROTR:
  6522. case WASM_OP_I64_CLZ:
  6523. case WASM_OP_I64_CTZ:
  6524. case WASM_OP_I64_POPCNT:
  6525. case WASM_OP_I64_ADD:
  6526. case WASM_OP_I64_SUB:
  6527. case WASM_OP_I64_MUL:
  6528. case WASM_OP_I64_DIV_S:
  6529. case WASM_OP_I64_DIV_U:
  6530. case WASM_OP_I64_REM_S:
  6531. case WASM_OP_I64_REM_U:
  6532. case WASM_OP_I64_AND:
  6533. case WASM_OP_I64_OR:
  6534. case WASM_OP_I64_XOR:
  6535. case WASM_OP_I64_SHL:
  6536. case WASM_OP_I64_SHR_S:
  6537. case WASM_OP_I64_SHR_U:
  6538. case WASM_OP_I64_ROTL:
  6539. case WASM_OP_I64_ROTR:
  6540. case WASM_OP_F32_ABS:
  6541. case WASM_OP_F32_NEG:
  6542. case WASM_OP_F32_CEIL:
  6543. case WASM_OP_F32_FLOOR:
  6544. case WASM_OP_F32_TRUNC:
  6545. case WASM_OP_F32_NEAREST:
  6546. case WASM_OP_F32_SQRT:
  6547. case WASM_OP_F32_ADD:
  6548. case WASM_OP_F32_SUB:
  6549. case WASM_OP_F32_MUL:
  6550. case WASM_OP_F32_DIV:
  6551. case WASM_OP_F32_MIN:
  6552. case WASM_OP_F32_MAX:
  6553. case WASM_OP_F32_COPYSIGN:
  6554. case WASM_OP_F64_ABS:
  6555. case WASM_OP_F64_NEG:
  6556. case WASM_OP_F64_CEIL:
  6557. case WASM_OP_F64_FLOOR:
  6558. case WASM_OP_F64_TRUNC:
  6559. case WASM_OP_F64_NEAREST:
  6560. case WASM_OP_F64_SQRT:
  6561. case WASM_OP_F64_ADD:
  6562. case WASM_OP_F64_SUB:
  6563. case WASM_OP_F64_MUL:
  6564. case WASM_OP_F64_DIV:
  6565. case WASM_OP_F64_MIN:
  6566. case WASM_OP_F64_MAX:
  6567. case WASM_OP_F64_COPYSIGN:
  6568. case WASM_OP_I32_WRAP_I64:
  6569. case WASM_OP_I32_TRUNC_S_F32:
  6570. case WASM_OP_I32_TRUNC_U_F32:
  6571. case WASM_OP_I32_TRUNC_S_F64:
  6572. case WASM_OP_I32_TRUNC_U_F64:
  6573. case WASM_OP_I64_EXTEND_S_I32:
  6574. case WASM_OP_I64_EXTEND_U_I32:
  6575. case WASM_OP_I64_TRUNC_S_F32:
  6576. case WASM_OP_I64_TRUNC_U_F32:
  6577. case WASM_OP_I64_TRUNC_S_F64:
  6578. case WASM_OP_I64_TRUNC_U_F64:
  6579. case WASM_OP_F32_CONVERT_S_I32:
  6580. case WASM_OP_F32_CONVERT_U_I32:
  6581. case WASM_OP_F32_CONVERT_S_I64:
  6582. case WASM_OP_F32_CONVERT_U_I64:
  6583. case WASM_OP_F32_DEMOTE_F64:
  6584. case WASM_OP_F64_CONVERT_S_I32:
  6585. case WASM_OP_F64_CONVERT_U_I32:
  6586. case WASM_OP_F64_CONVERT_S_I64:
  6587. case WASM_OP_F64_CONVERT_U_I64:
  6588. case WASM_OP_F64_PROMOTE_F32:
  6589. case WASM_OP_I32_REINTERPRET_F32:
  6590. case WASM_OP_I64_REINTERPRET_F64:
  6591. case WASM_OP_F32_REINTERPRET_I32:
  6592. case WASM_OP_F64_REINTERPRET_I64:
  6593. case WASM_OP_I32_EXTEND8_S:
  6594. case WASM_OP_I32_EXTEND16_S:
  6595. case WASM_OP_I64_EXTEND8_S:
  6596. case WASM_OP_I64_EXTEND16_S:
  6597. case WASM_OP_I64_EXTEND32_S:
  6598. break;
  6599. #if WASM_ENABLE_GC != 0
  6600. case WASM_OP_GC_PREFIX:
  6601. {
  6602. uint32 opcode1;
  6603. read_leb_uint32(p, p_end, opcode1);
  6604. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6605. is no larger than UINT8_MAX */
  6606. opcode = (uint8)opcode1;
  6607. switch (opcode) {
  6608. case WASM_OP_STRUCT_NEW:
  6609. case WASM_OP_STRUCT_NEW_DEFAULT:
  6610. skip_leb_uint32(p, p_end); /* typeidx */
  6611. break;
  6612. case WASM_OP_STRUCT_GET:
  6613. case WASM_OP_STRUCT_GET_S:
  6614. case WASM_OP_STRUCT_GET_U:
  6615. case WASM_OP_STRUCT_SET:
  6616. skip_leb_uint32(p, p_end); /* typeidx */
  6617. skip_leb_uint32(p, p_end); /* fieldidx */
  6618. break;
  6619. case WASM_OP_ARRAY_NEW:
  6620. case WASM_OP_ARRAY_NEW_DEFAULT:
  6621. case WASM_OP_ARRAY_GET:
  6622. case WASM_OP_ARRAY_GET_S:
  6623. case WASM_OP_ARRAY_GET_U:
  6624. case WASM_OP_ARRAY_SET:
  6625. case WASM_OP_ARRAY_FILL:
  6626. skip_leb_uint32(p, p_end); /* typeidx */
  6627. break;
  6628. case WASM_OP_ARRAY_COPY:
  6629. skip_leb_uint32(p, p_end); /* typeidx1 */
  6630. skip_leb_uint32(p, p_end); /* typeidx2 */
  6631. break;
  6632. case WASM_OP_ARRAY_LEN:
  6633. break;
  6634. case WASM_OP_ARRAY_NEW_FIXED:
  6635. case WASM_OP_ARRAY_NEW_DATA:
  6636. case WASM_OP_ARRAY_NEW_ELEM:
  6637. skip_leb_uint32(p, p_end); /* typeidx */
  6638. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6639. break;
  6640. case WASM_OP_REF_I31:
  6641. case WASM_OP_I31_GET_S:
  6642. case WASM_OP_I31_GET_U:
  6643. break;
  6644. case WASM_OP_REF_TEST:
  6645. case WASM_OP_REF_CAST:
  6646. case WASM_OP_REF_TEST_NULLABLE:
  6647. case WASM_OP_REF_CAST_NULLABLE:
  6648. skip_leb_int32(p, p_end); /* heaptype */
  6649. break;
  6650. case WASM_OP_BR_ON_CAST:
  6651. case WASM_OP_BR_ON_CAST_FAIL:
  6652. p += sizeof(uint8); /* castflag */
  6653. skip_leb_uint32(p, p_end); /* labelidx */
  6654. skip_leb_int32(p, p_end); /* heaptype */
  6655. skip_leb_int32(p, p_end); /* heaptype2 */
  6656. break;
  6657. case WASM_OP_ANY_CONVERT_EXTERN:
  6658. case WASM_OP_EXTERN_CONVERT_ANY:
  6659. break;
  6660. #if WASM_ENABLE_STRINGREF != 0
  6661. case WASM_OP_STRING_NEW_UTF8:
  6662. case WASM_OP_STRING_NEW_WTF16:
  6663. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6664. case WASM_OP_STRING_NEW_WTF8:
  6665. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6666. break;
  6667. case WASM_OP_STRING_CONST:
  6668. skip_leb_int32(p, p_end); /* contents */
  6669. break;
  6670. case WASM_OP_STRING_MEASURE_UTF8:
  6671. case WASM_OP_STRING_MEASURE_WTF8:
  6672. case WASM_OP_STRING_MEASURE_WTF16:
  6673. break;
  6674. case WASM_OP_STRING_ENCODE_UTF8:
  6675. case WASM_OP_STRING_ENCODE_WTF16:
  6676. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6677. case WASM_OP_STRING_ENCODE_WTF8:
  6678. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6679. break;
  6680. case WASM_OP_STRING_CONCAT:
  6681. case WASM_OP_STRING_EQ:
  6682. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6683. case WASM_OP_STRING_AS_WTF8:
  6684. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6685. break;
  6686. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6687. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6688. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6689. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6690. break;
  6691. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6692. case WASM_OP_STRING_AS_WTF16:
  6693. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6694. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6695. break;
  6696. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6697. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6698. break;
  6699. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6700. case WASM_OP_STRING_AS_ITER:
  6701. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6702. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6703. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6704. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6705. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6706. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6707. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6708. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6709. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6710. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6711. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6712. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6713. break;
  6714. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6715. default:
  6716. return false;
  6717. }
  6718. break;
  6719. }
  6720. #endif /* end of WASM_ENABLE_GC != 0 */
  6721. case WASM_OP_MISC_PREFIX:
  6722. {
  6723. uint32 opcode1;
  6724. read_leb_uint32(p, p_end, opcode1);
  6725. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6726. is no larger than UINT8_MAX */
  6727. opcode = (uint8)opcode1;
  6728. switch (opcode) {
  6729. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6730. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6731. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6732. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6733. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6734. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6735. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6736. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6737. break;
  6738. #if WASM_ENABLE_BULK_MEMORY != 0
  6739. case WASM_OP_MEMORY_INIT:
  6740. skip_leb_uint32(p, p_end);
  6741. /* skip memory idx */
  6742. p++;
  6743. break;
  6744. case WASM_OP_DATA_DROP:
  6745. skip_leb_uint32(p, p_end);
  6746. break;
  6747. case WASM_OP_MEMORY_COPY:
  6748. /* skip two memory idx */
  6749. p += 2;
  6750. break;
  6751. case WASM_OP_MEMORY_FILL:
  6752. /* skip memory idx */
  6753. p++;
  6754. break;
  6755. #endif /* WASM_ENABLE_BULK_MEMORY */
  6756. #if WASM_ENABLE_REF_TYPES != 0
  6757. case WASM_OP_TABLE_INIT:
  6758. case WASM_OP_TABLE_COPY:
  6759. /* tableidx */
  6760. skip_leb_uint32(p, p_end);
  6761. /* elemidx */
  6762. skip_leb_uint32(p, p_end);
  6763. break;
  6764. case WASM_OP_ELEM_DROP:
  6765. /* elemidx */
  6766. skip_leb_uint32(p, p_end);
  6767. break;
  6768. case WASM_OP_TABLE_SIZE:
  6769. case WASM_OP_TABLE_GROW:
  6770. case WASM_OP_TABLE_FILL:
  6771. skip_leb_uint32(p, p_end); /* table idx */
  6772. break;
  6773. #endif /* WASM_ENABLE_REF_TYPES */
  6774. default:
  6775. return false;
  6776. }
  6777. break;
  6778. }
  6779. #if WASM_ENABLE_SIMD != 0
  6780. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6781. case WASM_OP_SIMD_PREFIX:
  6782. {
  6783. /* TODO: memory64 offset type changes */
  6784. uint32 opcode1;
  6785. read_leb_uint32(p, p_end, opcode1);
  6786. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6787. is no larger than UINT8_MAX */
  6788. opcode = (uint8)opcode1;
  6789. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6790. */
  6791. switch (opcode) {
  6792. case SIMD_v128_load:
  6793. case SIMD_v128_load8x8_s:
  6794. case SIMD_v128_load8x8_u:
  6795. case SIMD_v128_load16x4_s:
  6796. case SIMD_v128_load16x4_u:
  6797. case SIMD_v128_load32x2_s:
  6798. case SIMD_v128_load32x2_u:
  6799. case SIMD_v128_load8_splat:
  6800. case SIMD_v128_load16_splat:
  6801. case SIMD_v128_load32_splat:
  6802. case SIMD_v128_load64_splat:
  6803. case SIMD_v128_store:
  6804. /* memarg align */
  6805. skip_leb_uint32(p, p_end);
  6806. /* memarg offset*/
  6807. skip_leb_uint32(p, p_end);
  6808. break;
  6809. case SIMD_v128_const:
  6810. case SIMD_v8x16_shuffle:
  6811. /* immByte[16] immLaneId[16] */
  6812. CHECK_BUF1(p, p_end, 16);
  6813. p += 16;
  6814. break;
  6815. case SIMD_i8x16_extract_lane_s:
  6816. case SIMD_i8x16_extract_lane_u:
  6817. case SIMD_i8x16_replace_lane:
  6818. case SIMD_i16x8_extract_lane_s:
  6819. case SIMD_i16x8_extract_lane_u:
  6820. case SIMD_i16x8_replace_lane:
  6821. case SIMD_i32x4_extract_lane:
  6822. case SIMD_i32x4_replace_lane:
  6823. case SIMD_i64x2_extract_lane:
  6824. case SIMD_i64x2_replace_lane:
  6825. case SIMD_f32x4_extract_lane:
  6826. case SIMD_f32x4_replace_lane:
  6827. case SIMD_f64x2_extract_lane:
  6828. case SIMD_f64x2_replace_lane:
  6829. /* ImmLaneId */
  6830. CHECK_BUF(p, p_end, 1);
  6831. p++;
  6832. break;
  6833. case SIMD_v128_load8_lane:
  6834. case SIMD_v128_load16_lane:
  6835. case SIMD_v128_load32_lane:
  6836. case SIMD_v128_load64_lane:
  6837. case SIMD_v128_store8_lane:
  6838. case SIMD_v128_store16_lane:
  6839. case SIMD_v128_store32_lane:
  6840. case SIMD_v128_store64_lane:
  6841. /* memarg align */
  6842. skip_leb_uint32(p, p_end);
  6843. /* memarg offset*/
  6844. skip_leb_uint32(p, p_end);
  6845. /* ImmLaneId */
  6846. CHECK_BUF(p, p_end, 1);
  6847. p++;
  6848. break;
  6849. case SIMD_v128_load32_zero:
  6850. case SIMD_v128_load64_zero:
  6851. /* memarg align */
  6852. skip_leb_uint32(p, p_end);
  6853. /* memarg offset*/
  6854. skip_leb_uint32(p, p_end);
  6855. break;
  6856. default:
  6857. /*
  6858. * since latest SIMD specific used almost every value
  6859. * from 0x00 to 0xff, the default branch will present
  6860. * all opcodes without imm
  6861. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6862. */
  6863. break;
  6864. }
  6865. break;
  6866. }
  6867. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6868. #endif /* end of WASM_ENABLE_SIMD */
  6869. #if WASM_ENABLE_SHARED_MEMORY != 0
  6870. case WASM_OP_ATOMIC_PREFIX:
  6871. {
  6872. /* TODO: memory64 offset type changes */
  6873. uint32 opcode1;
  6874. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6875. read_leb_uint32(p, p_end, opcode1);
  6876. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6877. is no larger than UINT8_MAX */
  6878. opcode = (uint8)opcode1;
  6879. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6880. skip_leb_uint32(p, p_end); /* align */
  6881. skip_leb_mem_offset(p, p_end); /* offset */
  6882. }
  6883. else {
  6884. /* atomic.fence doesn't have memarg */
  6885. p++;
  6886. }
  6887. break;
  6888. }
  6889. #endif
  6890. #if WASM_ENABLE_DEBUG_INTERP != 0
  6891. case DEBUG_OP_BREAK:
  6892. {
  6893. WASMDebugInstance *debug_instance =
  6894. wasm_exec_env_get_instance(exec_env);
  6895. char original_opcode[1];
  6896. uint64 size = 1;
  6897. WASMModuleInstance *module_inst =
  6898. (WASMModuleInstance *)exec_env->module_inst;
  6899. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6900. ? (p - 1) - module_inst->module->load_addr
  6901. : ~0;
  6902. if (debug_instance) {
  6903. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6904. original_opcode, &size)
  6905. && size == 1) {
  6906. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6907. "with %02x: ",
  6908. original_opcode[0]);
  6909. opcode = original_opcode[0];
  6910. goto op_break_retry;
  6911. }
  6912. }
  6913. break;
  6914. }
  6915. #endif
  6916. default:
  6917. return false;
  6918. }
  6919. }
  6920. (void)u8;
  6921. (void)exec_env;
  6922. return false;
  6923. fail:
  6924. return false;
  6925. }
  6926. #if WASM_ENABLE_FAST_INTERP != 0
  6927. #if WASM_DEBUG_PREPROCESSOR != 0
  6928. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6929. #else
  6930. #define LOG_OP(...) (void)0
  6931. #endif
  6932. #define PATCH_ELSE 0
  6933. #define PATCH_END 1
  6934. typedef struct BranchBlockPatch {
  6935. struct BranchBlockPatch *next;
  6936. uint8 patch_type;
  6937. uint8 *code_compiled;
  6938. } BranchBlockPatch;
  6939. #endif
  6940. typedef struct BranchBlock {
  6941. uint8 label_type;
  6942. BlockType block_type;
  6943. uint8 *start_addr;
  6944. uint8 *else_addr;
  6945. uint8 *end_addr;
  6946. uint32 stack_cell_num;
  6947. #if WASM_ENABLE_GC != 0
  6948. uint32 reftype_map_num;
  6949. /* Indicate which local is used inside current block, used to validate
  6950. * local.get with non-nullable ref types */
  6951. uint8 *local_use_mask;
  6952. uint32 local_use_mask_size;
  6953. #endif
  6954. #if WASM_ENABLE_FAST_INTERP != 0
  6955. uint16 dynamic_offset;
  6956. uint8 *code_compiled;
  6957. BranchBlockPatch *patch_list;
  6958. /* This is used to save params frame_offset of of if block */
  6959. int16 *param_frame_offsets;
  6960. /* This is used to recover dynamic offset for else branch */
  6961. uint16 start_dynamic_offset;
  6962. #endif
  6963. /* Indicate the operand stack is in polymorphic state.
  6964. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6965. * to polymorphic state until the block's 'end' opcode is processed.
  6966. * If stack is in polymorphic state and stack is empty, instruction can
  6967. * pop any type of value directly without decreasing stack top pointer
  6968. * and stack cell num. */
  6969. bool is_stack_polymorphic;
  6970. } BranchBlock;
  6971. typedef struct WASMLoaderContext {
  6972. /* frame ref stack */
  6973. uint8 *frame_ref;
  6974. uint8 *frame_ref_bottom;
  6975. uint8 *frame_ref_boundary;
  6976. uint32 frame_ref_size;
  6977. uint32 stack_cell_num;
  6978. uint32 max_stack_cell_num;
  6979. #if WASM_ENABLE_GC != 0
  6980. /* frame reftype map stack */
  6981. WASMRefTypeMap *frame_reftype_map;
  6982. WASMRefTypeMap *frame_reftype_map_bottom;
  6983. WASMRefTypeMap *frame_reftype_map_boundary;
  6984. uint32 frame_reftype_map_size;
  6985. uint32 reftype_map_num;
  6986. uint32 max_reftype_map_num;
  6987. /* Current module */
  6988. WASMModule *module;
  6989. /* Current module's ref_type_set */
  6990. HashMap *ref_type_set;
  6991. /* Always point to local variable ref_type of
  6992. wasm_loader_prepare_bytecode */
  6993. WASMRefType *ref_type_tmp;
  6994. #endif
  6995. /* frame csp stack */
  6996. BranchBlock *frame_csp;
  6997. BranchBlock *frame_csp_bottom;
  6998. BranchBlock *frame_csp_boundary;
  6999. uint32 frame_csp_size;
  7000. uint32 csp_num;
  7001. uint32 max_csp_num;
  7002. #if WASM_ENABLE_FAST_INTERP != 0
  7003. /* frame offset stack */
  7004. int16 *frame_offset;
  7005. int16 *frame_offset_bottom;
  7006. int16 *frame_offset_boundary;
  7007. uint32 frame_offset_size;
  7008. int16 dynamic_offset;
  7009. int16 start_dynamic_offset;
  7010. int16 max_dynamic_offset;
  7011. /* preserved local offset */
  7012. int16 preserved_local_offset;
  7013. /* const buffer */
  7014. uint8 *const_buf;
  7015. uint16 num_const;
  7016. uint16 const_cell_num;
  7017. uint32 const_buf_size;
  7018. /* processed code */
  7019. uint8 *p_code_compiled;
  7020. uint8 *p_code_compiled_end;
  7021. uint32 code_compiled_size;
  7022. /* If the last opcode will be dropped, the peak memory usage will be larger
  7023. * than the final code_compiled_size, we record the peak size to ensure
  7024. * there will not be invalid memory access during second traverse */
  7025. uint32 code_compiled_peak_size;
  7026. #endif
  7027. } WASMLoaderContext;
  7028. typedef struct Const {
  7029. WASMValue value;
  7030. uint16 slot_index;
  7031. uint8 value_type;
  7032. } Const;
  7033. #define CHECK_CSP_PUSH() \
  7034. do { \
  7035. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7036. MEM_REALLOC( \
  7037. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7038. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7039. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7040. ctx->frame_csp_boundary = \
  7041. ctx->frame_csp_bottom \
  7042. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7043. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7044. } \
  7045. } while (0)
  7046. #define CHECK_CSP_POP() \
  7047. do { \
  7048. if (ctx->csp_num < 1) { \
  7049. set_error_buf(error_buf, error_buf_size, \
  7050. "type mismatch: " \
  7051. "expect data but block stack was empty"); \
  7052. goto fail; \
  7053. } \
  7054. } while (0)
  7055. #if WASM_ENABLE_FAST_INTERP != 0
  7056. static bool
  7057. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7058. uint32 error_buf_size)
  7059. {
  7060. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7061. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7062. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7063. ctx->frame_offset_size + 16);
  7064. ctx->frame_offset_size += 16;
  7065. ctx->frame_offset_boundary =
  7066. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7067. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7068. }
  7069. return true;
  7070. fail:
  7071. return false;
  7072. }
  7073. static bool
  7074. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7075. {
  7076. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7077. return false;
  7078. return true;
  7079. }
  7080. static void
  7081. free_label_patch_list(BranchBlock *frame_csp)
  7082. {
  7083. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7084. BranchBlockPatch *next;
  7085. while (label_patch != NULL) {
  7086. next = label_patch->next;
  7087. wasm_runtime_free(label_patch);
  7088. label_patch = next;
  7089. }
  7090. frame_csp->patch_list = NULL;
  7091. }
  7092. static void
  7093. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7094. {
  7095. BranchBlock *tmp_csp = frame_csp;
  7096. for (uint32 i = 0; i < csp_num; i++) {
  7097. free_label_patch_list(tmp_csp);
  7098. tmp_csp++;
  7099. }
  7100. }
  7101. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7102. #if WASM_ENABLE_GC != 0
  7103. static bool
  7104. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7105. char *error_buf, uint32 error_buf_size)
  7106. {
  7107. BranchBlock *current_csp = ctx->frame_csp - 1;
  7108. uint32 local_mask_size;
  7109. if (local_count == 0) {
  7110. current_csp->local_use_mask_size = 0;
  7111. return true;
  7112. }
  7113. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7114. * else branch, we don't need to allocate memory again */
  7115. if (!current_csp->local_use_mask) {
  7116. local_mask_size = (local_count + 7) / sizeof(uint8);
  7117. if (!(current_csp->local_use_mask =
  7118. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7119. return false;
  7120. }
  7121. current_csp->local_use_mask_size = local_mask_size;
  7122. }
  7123. else {
  7124. local_mask_size = current_csp->local_use_mask_size;
  7125. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7126. }
  7127. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7128. /* For non-function blocks, inherit the use status from parent block */
  7129. BranchBlock *parent_csp = current_csp - 1;
  7130. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7131. bh_assert(parent_csp->local_use_mask);
  7132. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7133. parent_csp->local_use_mask, local_mask_size);
  7134. }
  7135. return true;
  7136. }
  7137. static void
  7138. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7139. {
  7140. BranchBlock *current_csp = ctx->frame_csp - 1;
  7141. bh_assert(current_csp->local_use_mask
  7142. || current_csp->local_use_mask_size == 0);
  7143. if (current_csp->local_use_mask) {
  7144. wasm_runtime_free(current_csp->local_use_mask);
  7145. }
  7146. current_csp->local_use_mask = NULL;
  7147. current_csp->local_use_mask_size = 0;
  7148. }
  7149. static void
  7150. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7151. {
  7152. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7153. uint32 i;
  7154. for (i = 0; i < ctx->csp_num; i++) {
  7155. if (tmp_csp->local_use_mask) {
  7156. wasm_runtime_free(tmp_csp->local_use_mask);
  7157. tmp_csp->local_use_mask = NULL;
  7158. tmp_csp->local_use_mask_size = 0;
  7159. }
  7160. tmp_csp++;
  7161. }
  7162. }
  7163. static void
  7164. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7165. {
  7166. BranchBlock *current_csp = ctx->frame_csp - 1;
  7167. uint32 byte_offset = index / sizeof(uint8);
  7168. uint32 bit_offset = index % sizeof(uint8);
  7169. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7170. bh_assert(current_csp->local_use_mask);
  7171. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7172. }
  7173. static bool
  7174. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7175. {
  7176. BranchBlock *current_csp = ctx->frame_csp - 1;
  7177. uint32 byte_offset = index / sizeof(uint8);
  7178. uint32 bit_offset = index % sizeof(uint8);
  7179. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7180. bh_assert(current_csp->local_use_mask);
  7181. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7182. ? true
  7183. : false;
  7184. }
  7185. #endif /* end of WASM_ENABLE_GC != 0 */
  7186. static void
  7187. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7188. {
  7189. if (ctx) {
  7190. if (ctx->frame_ref_bottom)
  7191. wasm_runtime_free(ctx->frame_ref_bottom);
  7192. #if WASM_ENABLE_GC != 0
  7193. if (ctx->frame_reftype_map_bottom)
  7194. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7195. #endif
  7196. if (ctx->frame_csp_bottom) {
  7197. #if WASM_ENABLE_FAST_INTERP != 0
  7198. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7199. #endif
  7200. #if WASM_ENABLE_GC != 0
  7201. wasm_loader_clean_all_local_use_masks(ctx);
  7202. #endif
  7203. wasm_runtime_free(ctx->frame_csp_bottom);
  7204. }
  7205. #if WASM_ENABLE_FAST_INTERP != 0
  7206. if (ctx->frame_offset_bottom)
  7207. wasm_runtime_free(ctx->frame_offset_bottom);
  7208. if (ctx->const_buf)
  7209. wasm_runtime_free(ctx->const_buf);
  7210. #endif
  7211. wasm_runtime_free(ctx);
  7212. }
  7213. }
  7214. static WASMLoaderContext *
  7215. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7216. {
  7217. WASMLoaderContext *loader_ctx =
  7218. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7219. if (!loader_ctx)
  7220. return NULL;
  7221. loader_ctx->frame_ref_size = 32;
  7222. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7223. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7224. goto fail;
  7225. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7226. #if WASM_ENABLE_GC != 0
  7227. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7228. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7229. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7230. error_buf_size)))
  7231. goto fail;
  7232. loader_ctx->frame_reftype_map_boundary =
  7233. loader_ctx->frame_reftype_map_bottom + 16;
  7234. #endif
  7235. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7236. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7237. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7238. goto fail;
  7239. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7240. #if WASM_ENABLE_EXCE_HANDLING != 0
  7241. func->exception_handler_count = 0;
  7242. #endif
  7243. #if WASM_ENABLE_FAST_INTERP != 0
  7244. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7245. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7246. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7247. error_buf_size)))
  7248. goto fail;
  7249. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7250. loader_ctx->num_const = 0;
  7251. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7252. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7253. error_buf, error_buf_size)))
  7254. goto fail;
  7255. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7256. set_error_buf(error_buf, error_buf_size,
  7257. "fast interpreter offset overflow");
  7258. goto fail;
  7259. }
  7260. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7261. loader_ctx->max_dynamic_offset =
  7262. func->param_cell_num + func->local_cell_num;
  7263. #endif
  7264. return loader_ctx;
  7265. fail:
  7266. wasm_loader_ctx_destroy(loader_ctx);
  7267. return NULL;
  7268. }
  7269. static bool
  7270. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7271. uint32 error_buf_size)
  7272. {
  7273. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7274. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7275. /* Increase the frame ref stack */
  7276. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7277. ctx->frame_ref_size + 16);
  7278. ctx->frame_ref_size += 16;
  7279. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7280. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7281. }
  7282. #if WASM_ENABLE_GC != 0
  7283. if (wasm_is_type_multi_byte_type(type)
  7284. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7285. /* Increase the frame reftype map stack */
  7286. bh_assert(
  7287. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7288. * sizeof(WASMRefTypeMap))
  7289. == ctx->frame_reftype_map_size);
  7290. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7291. ctx->frame_reftype_map_size
  7292. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7293. ctx->frame_reftype_map =
  7294. ctx->frame_reftype_map_bottom
  7295. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7296. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7297. ctx->frame_reftype_map_boundary =
  7298. ctx->frame_reftype_map_bottom
  7299. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7300. }
  7301. #endif
  7302. return true;
  7303. fail:
  7304. return false;
  7305. }
  7306. static bool
  7307. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7308. uint32 error_buf_size)
  7309. {
  7310. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7311. uint32 i;
  7312. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7313. return false;
  7314. #if WASM_ENABLE_GC != 0
  7315. if (wasm_is_type_multi_byte_type(type)) {
  7316. WASMRefType *ref_type;
  7317. if (!(ref_type =
  7318. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7319. error_buf, error_buf_size))) {
  7320. return false;
  7321. }
  7322. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7323. /* Increase the frame reftype map stack */
  7324. bh_assert((uint32)((ctx->frame_reftype_map
  7325. - ctx->frame_reftype_map_bottom)
  7326. * sizeof(WASMRefTypeMap))
  7327. == ctx->frame_reftype_map_size);
  7328. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7329. ctx->frame_reftype_map_size,
  7330. ctx->frame_reftype_map_size
  7331. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7332. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7333. + ctx->frame_reftype_map_size
  7334. / ((uint32)sizeof(WASMRefTypeMap));
  7335. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7336. ctx->frame_reftype_map_boundary =
  7337. ctx->frame_reftype_map_bottom
  7338. + ctx->frame_reftype_map_size
  7339. / ((uint32)sizeof(WASMRefTypeMap));
  7340. }
  7341. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7342. ctx->frame_reftype_map->ref_type = ref_type;
  7343. ctx->frame_reftype_map++;
  7344. ctx->reftype_map_num++;
  7345. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7346. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7347. }
  7348. #endif
  7349. for (i = 0; i < type_cell_num; i++)
  7350. *ctx->frame_ref++ = type;
  7351. ctx->stack_cell_num += type_cell_num;
  7352. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7353. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7354. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7355. set_error_buf(error_buf, error_buf_size,
  7356. "operand stack depth limit exceeded");
  7357. return false;
  7358. }
  7359. }
  7360. return true;
  7361. #if WASM_ENABLE_GC != 0
  7362. fail:
  7363. return false;
  7364. #endif
  7365. }
  7366. static bool
  7367. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7368. int32 stack_cell_num,
  7369. #if WASM_ENABLE_GC != 0
  7370. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7371. #endif
  7372. uint8 type,
  7373. #if WASM_ENABLE_GC != 0
  7374. WASMRefType *ref_type,
  7375. #endif
  7376. char *error_buf, uint32 error_buf_size)
  7377. {
  7378. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7379. #if WASM_ENABLE_GC != 0
  7380. WASMRefType *frame_reftype = NULL;
  7381. #endif
  7382. if (stack_cell_num < type_cell_num) {
  7383. set_error_buf(error_buf, error_buf_size,
  7384. "type mismatch: expect data but stack was empty");
  7385. return false;
  7386. }
  7387. #if WASM_ENABLE_GC == 0
  7388. for (i = 0; i < type_cell_num; i++) {
  7389. if (*(frame_ref - 1 - i) != type) {
  7390. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7391. "type mismatch: expect ", type2str(type),
  7392. " but got other");
  7393. return false;
  7394. }
  7395. }
  7396. #else
  7397. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7398. bh_assert(reftype_map_num > 0);
  7399. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7400. }
  7401. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7402. ref_type, ctx->module->types,
  7403. ctx->module->type_count)) {
  7404. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7405. "type mismatch: expect ", type2str(type),
  7406. " but got other");
  7407. return false;
  7408. }
  7409. for (i = 0; i < type_cell_num - 1; i++) {
  7410. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7411. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7412. "type mismatch: expect ", type2str(type),
  7413. " but got other");
  7414. return false;
  7415. }
  7416. }
  7417. #endif
  7418. return true;
  7419. }
  7420. static bool
  7421. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7422. uint32 error_buf_size)
  7423. {
  7424. int32 block_stack_cell_num =
  7425. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7426. #if WASM_ENABLE_GC != 0
  7427. int32 reftype_map_num =
  7428. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7429. #endif
  7430. if (block_stack_cell_num > 0) {
  7431. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7432. /* the stack top is a value of any type, return success */
  7433. return true;
  7434. }
  7435. #if WASM_ENABLE_GC != 0
  7436. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7437. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7438. WASMRefType *stack_top_ref_type = NULL;
  7439. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7440. bh_assert(reftype_map_num > 0);
  7441. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7442. }
  7443. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7444. ctx->ref_type_tmp, ctx->module->types,
  7445. ctx->module->type_count)) {
  7446. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7447. uint32 ref_type_struct_size =
  7448. wasm_reftype_struct_size(stack_top_ref_type);
  7449. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7450. stack_top_ref_type, ref_type_struct_size);
  7451. }
  7452. return true;
  7453. }
  7454. }
  7455. #endif
  7456. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7457. #if WASM_ENABLE_GC != 0
  7458. ctx->frame_reftype_map, reftype_map_num,
  7459. #endif
  7460. type,
  7461. #if WASM_ENABLE_GC != 0
  7462. ctx->ref_type_tmp,
  7463. #endif
  7464. error_buf, error_buf_size)) {
  7465. return false;
  7466. }
  7467. return true;
  7468. }
  7469. static bool
  7470. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7471. uint32 error_buf_size)
  7472. {
  7473. BranchBlock *cur_block = ctx->frame_csp - 1;
  7474. int32 available_stack_cell =
  7475. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7476. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7477. /* Directly return success if current block is in stack
  7478. polymorphic state while stack is empty. */
  7479. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7480. return true;
  7481. if (type == VALUE_TYPE_VOID)
  7482. return true;
  7483. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7484. return false;
  7485. bh_assert(available_stack_cell > 0);
  7486. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7487. type = VALUE_TYPE_ANY;
  7488. cell_num_to_pop = 1;
  7489. }
  7490. ctx->frame_ref -= cell_num_to_pop;
  7491. ctx->stack_cell_num -= cell_num_to_pop;
  7492. #if WASM_ENABLE_GC != 0
  7493. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7494. ctx->frame_reftype_map--;
  7495. ctx->reftype_map_num--;
  7496. }
  7497. #endif
  7498. return true;
  7499. }
  7500. #if WASM_ENABLE_GC != 0
  7501. /* Get the stack top element of current block */
  7502. static bool
  7503. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7504. WASMRefType **p_ref_type, char *error_buf,
  7505. uint32 error_buf_size)
  7506. {
  7507. BranchBlock *cur_block = ctx->frame_csp - 1;
  7508. int32 available_stack_cell =
  7509. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7510. if (available_stack_cell <= 0) {
  7511. /* Directly return success if current block is in stack
  7512. polymorphic state while stack is empty. */
  7513. if (cur_block->is_stack_polymorphic) {
  7514. *p_type = VALUE_TYPE_ANY;
  7515. return true;
  7516. }
  7517. else {
  7518. set_error_buf(
  7519. error_buf, error_buf_size,
  7520. "type mismatch: expect data but block stack was empty");
  7521. return false;
  7522. }
  7523. }
  7524. *p_type = *(ctx->frame_ref - 1);
  7525. if (wasm_is_type_multi_byte_type(*p_type)) {
  7526. int32 available_reftype_map =
  7527. (int32)(ctx->reftype_map_num
  7528. - (ctx->frame_csp - 1)->reftype_map_num);
  7529. bh_assert(available_reftype_map > 0);
  7530. (void)available_reftype_map;
  7531. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7532. }
  7533. return true;
  7534. }
  7535. #if WASM_ENABLE_FAST_INTERP != 0
  7536. static bool
  7537. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7538. char *error_buf, uint32 error_buf_size);
  7539. #endif
  7540. /* Check whether the stack top elem is a heap object, and if yes,
  7541. pop and return it */
  7542. static bool
  7543. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7544. WASMRefType *ref_ht_ret, char *error_buf,
  7545. uint32 error_buf_size)
  7546. {
  7547. uint8 type = 0;
  7548. WASMRefType *ref_type = NULL;
  7549. /* Get stack top element */
  7550. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7551. error_buf_size)) {
  7552. return false;
  7553. }
  7554. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7555. /* stack top isn't a ref type */
  7556. && !wasm_is_type_reftype(type)) {
  7557. set_error_buf(error_buf, error_buf_size,
  7558. "type mismatch: expect heap object but got others");
  7559. return false;
  7560. }
  7561. /* POP stack top */
  7562. if (wasm_is_type_multi_byte_type(type)) {
  7563. bh_assert(ref_type);
  7564. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7565. wasm_reftype_struct_size(ref_type));
  7566. }
  7567. #if WASM_ENABLE_FAST_INTERP != 0
  7568. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7569. error_buf_size)) {
  7570. return false;
  7571. }
  7572. #else
  7573. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7574. return false;
  7575. }
  7576. #endif
  7577. if (p_type)
  7578. *p_type = type;
  7579. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7580. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7581. wasm_reftype_struct_size(ref_type));
  7582. }
  7583. return true;
  7584. }
  7585. /* Check whether the stack top elem is subtype of (ref null ht),
  7586. and if yes, pop it and return the converted (ref ht) */
  7587. static bool
  7588. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7589. WASMRefType *ref_ht_ret, char *error_buf,
  7590. uint32 error_buf_size)
  7591. {
  7592. uint8 type = 0;
  7593. WASMRefType ref_type = { 0 };
  7594. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7595. error_buf_size)) {
  7596. return false;
  7597. }
  7598. /* Convert to related (ref ht) and return */
  7599. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7600. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7601. */
  7602. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7603. HEAP_TYPE_ARRAY
  7604. + (type - REF_TYPE_ARRAYREF));
  7605. type = ref_ht_ret->ref_type;
  7606. }
  7607. else if (wasm_is_reftype_htref_nullable(type)
  7608. || wasm_is_reftype_htref_non_nullable(type)) {
  7609. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7610. wasm_reftype_struct_size(&ref_type));
  7611. /* Convert to (ref ht) */
  7612. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7613. ref_ht_ret->ref_ht_common.nullable = false;
  7614. type = ref_ht_ret->ref_type;
  7615. }
  7616. *p_type = type;
  7617. return true;
  7618. }
  7619. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7620. and if yes, pop it and return the type_idx */
  7621. static bool
  7622. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7623. uint32 *p_type_idx, char *error_buf,
  7624. uint32 error_buf_size)
  7625. {
  7626. uint8 type = 0;
  7627. int32 type_idx = -1;
  7628. WASMRefType *ref_type = NULL;
  7629. /* Get stack top element */
  7630. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7631. error_buf_size)) {
  7632. return false;
  7633. }
  7634. if (type != VALUE_TYPE_ANY) {
  7635. /* stack top isn't (ref null $t) */
  7636. if (!((wasm_is_reftype_htref_nullable(type)
  7637. || wasm_is_reftype_htref_non_nullable(type))
  7638. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7639. set_error_buf(error_buf, error_buf_size,
  7640. "type mismatch: expect (ref null $t) but got others");
  7641. return false;
  7642. }
  7643. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7644. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7645. wasm_reftype_struct_size(ref_type));
  7646. }
  7647. /* POP stack top */
  7648. #if WASM_ENABLE_FAST_INTERP != 0
  7649. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7650. error_buf_size)) {
  7651. return false;
  7652. }
  7653. #else
  7654. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7655. return false;
  7656. }
  7657. #endif
  7658. /* Convert to type_idx and return */
  7659. *p_type = type;
  7660. if (type != VALUE_TYPE_ANY)
  7661. *p_type_idx = (uint32)type_idx;
  7662. return true;
  7663. }
  7664. #endif /* WASM_ENABLE_GC != 0 */
  7665. #if WASM_ENABLE_FAST_INTERP == 0
  7666. static bool
  7667. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7668. uint8 type_push, uint8 type_pop, char *error_buf,
  7669. uint32 error_buf_size)
  7670. {
  7671. for (int i = 0; i < pop_cnt; i++) {
  7672. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7673. error_buf_size))
  7674. return false;
  7675. }
  7676. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7677. return false;
  7678. return true;
  7679. }
  7680. #endif
  7681. static bool
  7682. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7683. BlockType block_type, uint8 *start_addr,
  7684. char *error_buf, uint32 error_buf_size)
  7685. {
  7686. CHECK_CSP_PUSH();
  7687. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7688. ctx->frame_csp->label_type = label_type;
  7689. ctx->frame_csp->block_type = block_type;
  7690. ctx->frame_csp->start_addr = start_addr;
  7691. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7692. #if WASM_ENABLE_GC != 0
  7693. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7694. #endif
  7695. #if WASM_ENABLE_FAST_INTERP != 0
  7696. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7697. ctx->frame_csp->patch_list = NULL;
  7698. #endif
  7699. ctx->frame_csp++;
  7700. ctx->csp_num++;
  7701. if (ctx->csp_num > ctx->max_csp_num) {
  7702. ctx->max_csp_num = ctx->csp_num;
  7703. if (ctx->max_csp_num > UINT16_MAX) {
  7704. set_error_buf(error_buf, error_buf_size,
  7705. "label stack depth limit exceeded");
  7706. return false;
  7707. }
  7708. }
  7709. return true;
  7710. fail:
  7711. return false;
  7712. }
  7713. static bool
  7714. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7715. uint32 error_buf_size)
  7716. {
  7717. CHECK_CSP_POP();
  7718. #if WASM_ENABLE_FAST_INTERP != 0
  7719. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7720. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7721. #endif
  7722. ctx->frame_csp--;
  7723. ctx->csp_num--;
  7724. return true;
  7725. fail:
  7726. return false;
  7727. }
  7728. #if WASM_ENABLE_FAST_INTERP != 0
  7729. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7730. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7731. #define emit_label(opcode) \
  7732. do { \
  7733. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7734. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7735. } while (0)
  7736. #define skip_label() \
  7737. do { \
  7738. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7739. LOG_OP("\ndelete last op\n"); \
  7740. } while (0)
  7741. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7742. #if UINTPTR_MAX == UINT64_MAX
  7743. #define emit_label(opcode) \
  7744. do { \
  7745. int32 offset = \
  7746. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7747. /* emit int32 relative offset in 64-bit target */ \
  7748. wasm_loader_emit_uint32(loader_ctx, offset); \
  7749. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7750. } while (0)
  7751. #else
  7752. #define emit_label(opcode) \
  7753. do { \
  7754. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7755. /* emit uint32 label address in 32-bit target */ \
  7756. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7757. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7758. } while (0)
  7759. #endif
  7760. #define skip_label() \
  7761. do { \
  7762. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7763. LOG_OP("\ndelete last op\n"); \
  7764. } while (0)
  7765. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7766. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7767. #define emit_label(opcode) \
  7768. do { \
  7769. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7770. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7771. } while (0)
  7772. #define skip_label() \
  7773. do { \
  7774. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7775. LOG_OP("\ndelete last op\n"); \
  7776. } while (0)
  7777. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7778. #define emit_empty_label_addr_and_frame_ip(type) \
  7779. do { \
  7780. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7781. loader_ctx->p_code_compiled, error_buf, \
  7782. error_buf_size)) \
  7783. goto fail; \
  7784. /* label address, to be patched */ \
  7785. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7786. } while (0)
  7787. #define emit_br_info(frame_csp, is_br) \
  7788. do { \
  7789. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7790. error_buf_size)) \
  7791. goto fail; \
  7792. } while (0)
  7793. #define LAST_OP_OUTPUT_I32() \
  7794. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7795. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7796. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7797. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7798. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7799. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7800. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7801. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7802. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7803. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7804. || (last_op == EXT_OP_COPY_STACK_TOP)
  7805. #define LAST_OP_OUTPUT_I64() \
  7806. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7807. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7808. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7809. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7810. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7811. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7812. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7813. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7814. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7815. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7816. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7817. #define GET_CONST_OFFSET(type, val) \
  7818. do { \
  7819. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7820. &operand_offset, error_buf, \
  7821. error_buf_size))) \
  7822. goto fail; \
  7823. } while (0)
  7824. #define GET_CONST_F32_OFFSET(type, fval) \
  7825. do { \
  7826. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7827. &operand_offset, error_buf, \
  7828. error_buf_size))) \
  7829. goto fail; \
  7830. } while (0)
  7831. #define GET_CONST_F64_OFFSET(type, fval) \
  7832. do { \
  7833. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7834. &operand_offset, error_buf, \
  7835. error_buf_size))) \
  7836. goto fail; \
  7837. } while (0)
  7838. #define emit_operand(ctx, offset) \
  7839. do { \
  7840. wasm_loader_emit_int16(ctx, offset); \
  7841. LOG_OP("%d\t", offset); \
  7842. } while (0)
  7843. #define emit_byte(ctx, byte) \
  7844. do { \
  7845. wasm_loader_emit_uint8(ctx, byte); \
  7846. LOG_OP("%d\t", byte); \
  7847. } while (0)
  7848. #define emit_uint32(ctx, value) \
  7849. do { \
  7850. wasm_loader_emit_uint32(ctx, value); \
  7851. LOG_OP("%d\t", value); \
  7852. } while (0)
  7853. #define emit_uint64(ctx, value) \
  7854. do { \
  7855. wasm_loader_emit_const(ctx, &value, false); \
  7856. LOG_OP("%lld\t", value); \
  7857. } while (0)
  7858. #define emit_float32(ctx, value) \
  7859. do { \
  7860. wasm_loader_emit_const(ctx, &value, true); \
  7861. LOG_OP("%f\t", value); \
  7862. } while (0)
  7863. #define emit_float64(ctx, value) \
  7864. do { \
  7865. wasm_loader_emit_const(ctx, &value, false); \
  7866. LOG_OP("%f\t", value); \
  7867. } while (0)
  7868. static bool
  7869. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7870. {
  7871. if (!(ctx->p_code_compiled =
  7872. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7873. return false;
  7874. ctx->p_code_compiled_end =
  7875. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7876. /* clean up frame ref */
  7877. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7878. ctx->frame_ref = ctx->frame_ref_bottom;
  7879. ctx->stack_cell_num = 0;
  7880. #if WASM_ENABLE_GC != 0
  7881. /* clean up reftype map */
  7882. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7883. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7884. ctx->reftype_map_num = 0;
  7885. #endif
  7886. /* clean up frame csp */
  7887. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7888. ctx->frame_csp = ctx->frame_csp_bottom;
  7889. ctx->csp_num = 0;
  7890. ctx->max_csp_num = 0;
  7891. /* clean up frame offset */
  7892. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7893. ctx->frame_offset = ctx->frame_offset_bottom;
  7894. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7895. /* init preserved local offsets */
  7896. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7897. /* const buf is reserved */
  7898. return true;
  7899. }
  7900. static void
  7901. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7902. {
  7903. ctx->code_compiled_size += size;
  7904. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7905. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7906. }
  7907. }
  7908. static void
  7909. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7910. {
  7911. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7912. if (ctx->p_code_compiled) {
  7913. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7914. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7915. #endif
  7916. bh_memcpy_s(ctx->p_code_compiled,
  7917. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7918. value, size);
  7919. ctx->p_code_compiled += size;
  7920. }
  7921. else {
  7922. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7923. bh_assert((ctx->code_compiled_size & 1) == 0);
  7924. #endif
  7925. increase_compiled_code_space(ctx, size);
  7926. }
  7927. }
  7928. static void
  7929. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7930. {
  7931. if (ctx->p_code_compiled) {
  7932. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7933. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7934. #endif
  7935. STORE_U32(ctx->p_code_compiled, value);
  7936. ctx->p_code_compiled += sizeof(uint32);
  7937. }
  7938. else {
  7939. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7940. bh_assert((ctx->code_compiled_size & 1) == 0);
  7941. #endif
  7942. increase_compiled_code_space(ctx, sizeof(uint32));
  7943. }
  7944. }
  7945. static void
  7946. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7947. {
  7948. if (ctx->p_code_compiled) {
  7949. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7950. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7951. #endif
  7952. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7953. ctx->p_code_compiled += sizeof(int16);
  7954. }
  7955. else {
  7956. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7957. bh_assert((ctx->code_compiled_size & 1) == 0);
  7958. #endif
  7959. increase_compiled_code_space(ctx, sizeof(uint16));
  7960. }
  7961. }
  7962. static void
  7963. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7964. {
  7965. if (ctx->p_code_compiled) {
  7966. *(ctx->p_code_compiled) = value;
  7967. ctx->p_code_compiled += sizeof(uint8);
  7968. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7969. ctx->p_code_compiled++;
  7970. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7971. #endif
  7972. }
  7973. else {
  7974. increase_compiled_code_space(ctx, sizeof(uint8));
  7975. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7976. increase_compiled_code_space(ctx, sizeof(uint8));
  7977. bh_assert((ctx->code_compiled_size & 1) == 0);
  7978. #endif
  7979. }
  7980. }
  7981. static void
  7982. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  7983. {
  7984. if (ctx->p_code_compiled) {
  7985. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7986. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7987. #endif
  7988. STORE_PTR(ctx->p_code_compiled, value);
  7989. ctx->p_code_compiled += sizeof(void *);
  7990. }
  7991. else {
  7992. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7993. bh_assert((ctx->code_compiled_size & 1) == 0);
  7994. #endif
  7995. increase_compiled_code_space(ctx, sizeof(void *));
  7996. }
  7997. }
  7998. static void
  7999. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  8000. {
  8001. if (ctx->p_code_compiled) {
  8002. ctx->p_code_compiled -= size;
  8003. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8004. if (size == sizeof(uint8)) {
  8005. ctx->p_code_compiled--;
  8006. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  8007. }
  8008. #endif
  8009. }
  8010. else {
  8011. ctx->code_compiled_size -= size;
  8012. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  8013. if (size == sizeof(uint8)) {
  8014. ctx->code_compiled_size--;
  8015. bh_assert((ctx->code_compiled_size & 1) == 0);
  8016. }
  8017. #endif
  8018. }
  8019. }
  8020. static bool
  8021. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8022. uint32 local_index, uint32 local_type,
  8023. bool *preserved, char *error_buf,
  8024. uint32 error_buf_size)
  8025. {
  8026. uint32 i = 0;
  8027. int16 preserved_offset = (int16)local_index;
  8028. *preserved = false;
  8029. while (i < loader_ctx->stack_cell_num) {
  8030. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8031. /* move previous local into dynamic space before a set/tee_local opcode
  8032. */
  8033. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8034. if (!(*preserved)) {
  8035. *preserved = true;
  8036. skip_label();
  8037. preserved_offset = loader_ctx->preserved_local_offset;
  8038. if (loader_ctx->p_code_compiled) {
  8039. bh_assert(preserved_offset != (int16)local_index);
  8040. }
  8041. if (is_32bit_type(local_type)) {
  8042. /* Only increase preserve offset in the second traversal */
  8043. if (loader_ctx->p_code_compiled)
  8044. loader_ctx->preserved_local_offset++;
  8045. emit_label(EXT_OP_COPY_STACK_TOP);
  8046. }
  8047. else {
  8048. if (loader_ctx->p_code_compiled)
  8049. loader_ctx->preserved_local_offset += 2;
  8050. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8051. }
  8052. emit_operand(loader_ctx, local_index);
  8053. emit_operand(loader_ctx, preserved_offset);
  8054. emit_label(opcode);
  8055. }
  8056. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8057. }
  8058. if (is_32bit_type(cur_type))
  8059. i++;
  8060. else
  8061. i += 2;
  8062. }
  8063. (void)error_buf;
  8064. (void)error_buf_size;
  8065. return true;
  8066. }
  8067. static bool
  8068. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8069. char *error_buf, uint32 error_buf_size)
  8070. {
  8071. uint32 i = 0;
  8072. bool preserve_local;
  8073. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8074. blocks will not influence the value of these locals */
  8075. while (i < loader_ctx->stack_cell_num) {
  8076. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8077. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8078. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8079. && (cur_offset >= 0)) {
  8080. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8081. cur_type, &preserve_local,
  8082. error_buf, error_buf_size)))
  8083. return false;
  8084. }
  8085. if (is_32bit_type(cur_type)) {
  8086. i++;
  8087. }
  8088. else {
  8089. i += 2;
  8090. }
  8091. }
  8092. return true;
  8093. }
  8094. static bool
  8095. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8096. uint8 *p_code_compiled, char *error_buf,
  8097. uint32 error_buf_size)
  8098. {
  8099. BranchBlockPatch *patch =
  8100. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8101. if (!patch) {
  8102. return false;
  8103. }
  8104. patch->patch_type = patch_type;
  8105. patch->code_compiled = p_code_compiled;
  8106. if (!frame_csp->patch_list) {
  8107. frame_csp->patch_list = patch;
  8108. patch->next = NULL;
  8109. }
  8110. else {
  8111. patch->next = frame_csp->patch_list;
  8112. frame_csp->patch_list = patch;
  8113. }
  8114. return true;
  8115. }
  8116. static void
  8117. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8118. {
  8119. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8120. BranchBlockPatch *node = frame_csp->patch_list;
  8121. BranchBlockPatch *node_prev = NULL, *node_next;
  8122. if (!ctx->p_code_compiled)
  8123. return;
  8124. while (node) {
  8125. node_next = node->next;
  8126. if (node->patch_type == patch_type) {
  8127. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8128. if (node_prev == NULL) {
  8129. frame_csp->patch_list = node_next;
  8130. }
  8131. else {
  8132. node_prev->next = node_next;
  8133. }
  8134. wasm_runtime_free(node);
  8135. }
  8136. else {
  8137. node_prev = node;
  8138. }
  8139. node = node_next;
  8140. }
  8141. }
  8142. static bool
  8143. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8144. bool is_br, char *error_buf, uint32 error_buf_size)
  8145. {
  8146. /* br info layout:
  8147. * a) arity of target block
  8148. * b) total cell num of arity values
  8149. * c) each arity value's cell num
  8150. * d) each arity value's src frame offset
  8151. * e) each arity values's dst dynamic offset
  8152. * f) branch target address
  8153. *
  8154. * Note: b-e are omitted when arity is 0 so that
  8155. * interpreter can recover the br info quickly.
  8156. */
  8157. BlockType *block_type = &frame_csp->block_type;
  8158. uint8 *types = NULL, cell;
  8159. #if WASM_ENABLE_GC != 0
  8160. WASMRefTypeMap *reftype_maps;
  8161. uint32 reftype_map_count;
  8162. #endif
  8163. uint32 arity = 0;
  8164. int32 i;
  8165. int16 *frame_offset = ctx->frame_offset;
  8166. uint16 dynamic_offset;
  8167. /* Note: loop's arity is different from if and block. loop's arity is
  8168. * its parameter count while if and block arity is result count.
  8169. */
  8170. #if WASM_ENABLE_GC == 0
  8171. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8172. arity = block_type_get_param_types(block_type, &types);
  8173. else
  8174. arity = block_type_get_result_types(block_type, &types);
  8175. #else
  8176. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8177. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8178. &reftype_map_count);
  8179. else
  8180. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8181. &reftype_map_count);
  8182. #endif
  8183. /* Part a */
  8184. emit_uint32(ctx, arity);
  8185. if (arity) {
  8186. /* Part b */
  8187. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8188. /* Part c */
  8189. for (i = (int32)arity - 1; i >= 0; i--) {
  8190. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8191. emit_byte(ctx, cell);
  8192. }
  8193. /* Part d */
  8194. for (i = (int32)arity - 1; i >= 0; i--) {
  8195. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8196. frame_offset -= cell;
  8197. emit_operand(ctx, *(int16 *)(frame_offset));
  8198. }
  8199. /* Part e */
  8200. dynamic_offset =
  8201. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8202. if (is_br)
  8203. ctx->dynamic_offset = dynamic_offset;
  8204. for (i = (int32)arity - 1; i >= 0; i--) {
  8205. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8206. dynamic_offset -= cell;
  8207. emit_operand(ctx, dynamic_offset);
  8208. }
  8209. }
  8210. /* Part f */
  8211. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8212. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8213. }
  8214. else {
  8215. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8216. error_buf, error_buf_size))
  8217. return false;
  8218. /* label address, to be patched */
  8219. wasm_loader_emit_ptr(ctx, NULL);
  8220. }
  8221. return true;
  8222. }
  8223. static bool
  8224. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8225. bool disable_emit, int16 operand_offset,
  8226. char *error_buf, uint32 error_buf_size)
  8227. {
  8228. if (type == VALUE_TYPE_VOID)
  8229. return true;
  8230. /* only check memory overflow in first traverse */
  8231. if (ctx->p_code_compiled == NULL) {
  8232. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8233. return false;
  8234. }
  8235. if (disable_emit)
  8236. *(ctx->frame_offset)++ = operand_offset;
  8237. else {
  8238. emit_operand(ctx, ctx->dynamic_offset);
  8239. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8240. ctx->dynamic_offset++;
  8241. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8242. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8243. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8244. goto fail;
  8245. }
  8246. }
  8247. }
  8248. if (is_32bit_type(type))
  8249. return true;
  8250. if (ctx->p_code_compiled == NULL) {
  8251. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8252. return false;
  8253. }
  8254. ctx->frame_offset++;
  8255. if (!disable_emit) {
  8256. ctx->dynamic_offset++;
  8257. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8258. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8259. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8260. goto fail;
  8261. }
  8262. }
  8263. }
  8264. return true;
  8265. fail:
  8266. set_error_buf(error_buf, error_buf_size,
  8267. "fast interpreter offset overflow");
  8268. return false;
  8269. }
  8270. /* This function should be in front of wasm_loader_pop_frame_ref
  8271. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8272. will be modified by wasm_loader_pop_frame_ref */
  8273. static bool
  8274. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8275. char *error_buf, uint32 error_buf_size)
  8276. {
  8277. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8278. then current block is the function block */
  8279. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8280. BranchBlock *cur_block = ctx->frame_csp - depth;
  8281. int32 available_stack_cell =
  8282. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8283. /* Directly return success if current block is in stack
  8284. * polymorphic state while stack is empty. */
  8285. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8286. return true;
  8287. if (type == VALUE_TYPE_VOID)
  8288. return true;
  8289. if (is_32bit_type(type)) {
  8290. /* Check the offset stack bottom to ensure the frame offset
  8291. stack will not go underflow. But we don't thrown error
  8292. and return true here, because the error msg should be
  8293. given in wasm_loader_pop_frame_ref */
  8294. if (!check_offset_pop(ctx, 1))
  8295. return true;
  8296. ctx->frame_offset -= 1;
  8297. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8298. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8299. ctx->dynamic_offset -= 1;
  8300. }
  8301. else {
  8302. if (!check_offset_pop(ctx, 2))
  8303. return true;
  8304. ctx->frame_offset -= 2;
  8305. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8306. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8307. ctx->dynamic_offset -= 2;
  8308. }
  8309. emit_operand(ctx, *(ctx->frame_offset));
  8310. (void)error_buf;
  8311. (void)error_buf_size;
  8312. return true;
  8313. }
  8314. static bool
  8315. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8316. bool disable_emit, int16 operand_offset,
  8317. char *error_buf, uint32 error_buf_size)
  8318. {
  8319. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8320. error_buf, error_buf_size)))
  8321. return false;
  8322. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8323. return false;
  8324. return true;
  8325. }
  8326. static bool
  8327. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8328. char *error_buf, uint32 error_buf_size)
  8329. {
  8330. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8331. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8332. return false;
  8333. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8334. return false;
  8335. return true;
  8336. }
  8337. static bool
  8338. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8339. uint8 type_push, uint8 type_pop,
  8340. bool disable_emit, int16 operand_offset,
  8341. char *error_buf, uint32 error_buf_size)
  8342. {
  8343. uint8 i;
  8344. for (i = 0; i < pop_cnt; i++) {
  8345. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8346. error_buf_size))
  8347. return false;
  8348. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8349. error_buf_size))
  8350. return false;
  8351. }
  8352. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8353. operand_offset, error_buf,
  8354. error_buf_size))
  8355. return false;
  8356. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8357. return false;
  8358. return true;
  8359. }
  8360. static bool
  8361. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8362. int16 *offset, char *error_buf,
  8363. uint32 error_buf_size)
  8364. {
  8365. int8 bytes_to_increase;
  8366. int16 operand_offset = 0;
  8367. Const *c;
  8368. /* Search existing constant */
  8369. for (c = (Const *)ctx->const_buf;
  8370. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8371. /* TODO: handle v128 type? */
  8372. if ((type == c->value_type)
  8373. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8374. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8375. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8376. || (type == VALUE_TYPE_FUNCREF
  8377. && *(int32 *)value == c->value.i32)
  8378. || (type == VALUE_TYPE_EXTERNREF
  8379. && *(int32 *)value == c->value.i32)
  8380. #endif
  8381. || (type == VALUE_TYPE_F64
  8382. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8383. || (type == VALUE_TYPE_F32
  8384. && (0
  8385. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8386. operand_offset = c->slot_index;
  8387. break;
  8388. }
  8389. if (is_32bit_type(c->value_type))
  8390. operand_offset += 1;
  8391. else
  8392. operand_offset += 2;
  8393. }
  8394. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8395. /* New constant, append to the const buffer */
  8396. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8397. bytes_to_increase = 2;
  8398. }
  8399. else {
  8400. bytes_to_increase = 1;
  8401. }
  8402. /* The max cell num of const buffer is 32768 since the valid index range
  8403. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8404. * full */
  8405. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8406. *offset = 0;
  8407. return true;
  8408. }
  8409. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8410. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8411. ctx->const_buf_size + 4 * sizeof(Const));
  8412. ctx->const_buf_size += 4 * sizeof(Const);
  8413. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8414. }
  8415. c->value_type = type;
  8416. switch (type) {
  8417. case VALUE_TYPE_F64:
  8418. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8419. sizeof(float64));
  8420. ctx->const_cell_num += 2;
  8421. /* The const buf will be reversed, we use the second cell */
  8422. /* of the i64/f64 const so the final offset is correct */
  8423. operand_offset++;
  8424. break;
  8425. case VALUE_TYPE_I64:
  8426. c->value.i64 = *(int64 *)value;
  8427. ctx->const_cell_num += 2;
  8428. operand_offset++;
  8429. break;
  8430. case VALUE_TYPE_F32:
  8431. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8432. sizeof(float32));
  8433. ctx->const_cell_num++;
  8434. break;
  8435. case VALUE_TYPE_I32:
  8436. c->value.i32 = *(int32 *)value;
  8437. ctx->const_cell_num++;
  8438. break;
  8439. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8440. case VALUE_TYPE_EXTERNREF:
  8441. case VALUE_TYPE_FUNCREF:
  8442. c->value.i32 = *(int32 *)value;
  8443. ctx->const_cell_num++;
  8444. break;
  8445. #endif
  8446. default:
  8447. break;
  8448. }
  8449. c->slot_index = operand_offset;
  8450. ctx->num_const++;
  8451. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8452. (int64)c->value.i64);
  8453. }
  8454. /* use negative index for const */
  8455. operand_offset = -(operand_offset + 1);
  8456. *offset = operand_offset;
  8457. return true;
  8458. fail:
  8459. return false;
  8460. }
  8461. /*
  8462. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8463. -- Mostly used for the binary / compare operation
  8464. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8465. -- Mostly used in block / control instructions
  8466. The POP will always emit the offset on the top of the frame_offset stack
  8467. PUSH can be used in two ways:
  8468. 1. directly PUSH:
  8469. PUSH_XXX();
  8470. will allocate a dynamic space and emit
  8471. 2. silent PUSH:
  8472. operand_offset = xxx; disable_emit = true;
  8473. PUSH_XXX();
  8474. only push the frame_offset stack, no emit
  8475. */
  8476. #define TEMPLATE_PUSH(Type) \
  8477. do { \
  8478. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8479. disable_emit, operand_offset, \
  8480. error_buf, error_buf_size)) \
  8481. goto fail; \
  8482. } while (0)
  8483. #define TEMPLATE_PUSH_REF(Type) \
  8484. do { \
  8485. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8486. operand_offset, error_buf, \
  8487. error_buf_size)) \
  8488. goto fail; \
  8489. } while (0)
  8490. #define TEMPLATE_POP(Type) \
  8491. do { \
  8492. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8493. error_buf, error_buf_size)) \
  8494. goto fail; \
  8495. } while (0)
  8496. #define TEMPLATE_POP_REF(Type) \
  8497. do { \
  8498. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8499. error_buf_size)) \
  8500. goto fail; \
  8501. } while (0)
  8502. #define PUSH_OFFSET_TYPE(type) \
  8503. do { \
  8504. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8505. operand_offset, error_buf, \
  8506. error_buf_size))) \
  8507. goto fail; \
  8508. } while (0)
  8509. #define POP_OFFSET_TYPE(type) \
  8510. do { \
  8511. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8512. error_buf_size))) \
  8513. goto fail; \
  8514. } while (0)
  8515. #define POP_AND_PUSH(type_pop, type_push) \
  8516. do { \
  8517. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8518. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8519. operand_offset, error_buf, error_buf_size))) \
  8520. goto fail; \
  8521. } while (0)
  8522. /* type of POPs should be the same */
  8523. #define POP2_AND_PUSH(type_pop, type_push) \
  8524. do { \
  8525. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8526. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8527. operand_offset, error_buf, error_buf_size))) \
  8528. goto fail; \
  8529. } while (0)
  8530. #else /* WASM_ENABLE_FAST_INTERP */
  8531. #define TEMPLATE_PUSH(Type) \
  8532. do { \
  8533. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8534. error_buf, error_buf_size))) \
  8535. goto fail; \
  8536. } while (0)
  8537. #define TEMPLATE_PUSH_REF(Type) \
  8538. do { \
  8539. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8540. error_buf_size))) \
  8541. goto fail; \
  8542. } while (0)
  8543. #define TEMPLATE_POP(Type) \
  8544. do { \
  8545. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8546. error_buf, error_buf_size))) \
  8547. goto fail; \
  8548. } while (0)
  8549. #define TEMPLATE_POP_REF(Type) \
  8550. do { \
  8551. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8552. error_buf_size))) \
  8553. goto fail; \
  8554. } while (0)
  8555. #define POP_AND_PUSH(type_pop, type_push) \
  8556. do { \
  8557. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8558. type_pop, error_buf, \
  8559. error_buf_size))) \
  8560. goto fail; \
  8561. } while (0)
  8562. /* type of POPs should be the same */
  8563. #define POP2_AND_PUSH(type_pop, type_push) \
  8564. do { \
  8565. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8566. type_pop, error_buf, \
  8567. error_buf_size))) \
  8568. goto fail; \
  8569. } while (0)
  8570. #endif /* WASM_ENABLE_FAST_INTERP */
  8571. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8572. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8573. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8574. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8575. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8576. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8577. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8578. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8579. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8580. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8581. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8582. #define POP_I32() TEMPLATE_POP(I32)
  8583. #define POP_F32() TEMPLATE_POP(F32)
  8584. #define POP_I64() TEMPLATE_POP(I64)
  8585. #define POP_F64() TEMPLATE_POP(F64)
  8586. #define POP_V128() TEMPLATE_POP(V128)
  8587. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8588. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8589. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8590. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8591. #if WASM_ENABLE_FAST_INTERP != 0
  8592. static bool
  8593. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8594. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8595. {
  8596. int16 operand_offset = 0;
  8597. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8598. : loader_ctx->frame_csp;
  8599. BlockType *block_type = &block->block_type;
  8600. uint8 *return_types = NULL;
  8601. #if WASM_ENABLE_GC != 0
  8602. WASMRefTypeMap *reftype_maps = NULL;
  8603. uint32 reftype_map_count;
  8604. #endif
  8605. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8606. int32 i = 0;
  8607. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8608. *frame_offset_org = NULL;
  8609. #if WASM_ENABLE_GC == 0
  8610. return_count = block_type_get_result_types(block_type, &return_types);
  8611. #else
  8612. return_count = block_type_get_result_types(
  8613. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8614. #endif
  8615. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8616. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8617. if (return_count == 1) {
  8618. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8619. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8620. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8621. /* insert op_copy before else opcode */
  8622. if (opcode == WASM_OP_ELSE)
  8623. skip_label();
  8624. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8625. : EXT_OP_COPY_STACK_TOP_I64);
  8626. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8627. emit_operand(loader_ctx, block->dynamic_offset);
  8628. if (opcode == WASM_OP_ELSE) {
  8629. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8630. }
  8631. else {
  8632. loader_ctx->frame_offset -= cell;
  8633. loader_ctx->dynamic_offset = block->dynamic_offset;
  8634. PUSH_OFFSET_TYPE(return_types[0]);
  8635. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8636. }
  8637. if (opcode == WASM_OP_ELSE)
  8638. emit_label(opcode);
  8639. }
  8640. return true;
  8641. }
  8642. /* Copy stack top values to block's results which are in dynamic space.
  8643. * The instruction format:
  8644. * Part a: values count
  8645. * Part b: all values total cell num
  8646. * Part c: each value's cell_num, src offset and dst offset
  8647. * Part d: each value's src offset and dst offset
  8648. * Part e: each value's dst offset
  8649. */
  8650. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8651. dynamic_offset = dynamic_offset_org =
  8652. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8653. /* First traversal to get the count of values needed to be copied. */
  8654. for (i = (int32)return_count - 1; i >= 0; i--) {
  8655. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8656. frame_offset -= cells;
  8657. dynamic_offset -= cells;
  8658. if (dynamic_offset != *frame_offset) {
  8659. value_count++;
  8660. total_cel_num += cells;
  8661. }
  8662. }
  8663. if (value_count) {
  8664. uint32 j = 0;
  8665. uint8 *emit_data = NULL, *cells = NULL;
  8666. int16 *src_offsets = NULL;
  8667. uint16 *dst_offsets = NULL;
  8668. uint64 size =
  8669. (uint64)value_count
  8670. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8671. /* Allocate memory for the emit data */
  8672. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8673. return false;
  8674. cells = emit_data;
  8675. src_offsets = (int16 *)(cells + value_count);
  8676. dst_offsets = (uint16 *)(src_offsets + value_count);
  8677. /* insert op_copy before else opcode */
  8678. if (opcode == WASM_OP_ELSE)
  8679. skip_label();
  8680. emit_label(EXT_OP_COPY_STACK_VALUES);
  8681. /* Part a) */
  8682. emit_uint32(loader_ctx, value_count);
  8683. /* Part b) */
  8684. emit_uint32(loader_ctx, total_cel_num);
  8685. /* Second traversal to get each value's cell num, src offset and dst
  8686. * offset. */
  8687. frame_offset = frame_offset_org;
  8688. dynamic_offset = dynamic_offset_org;
  8689. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8690. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8691. frame_offset -= cell;
  8692. dynamic_offset -= cell;
  8693. if (dynamic_offset != *frame_offset) {
  8694. /* cell num */
  8695. cells[j] = cell;
  8696. /* src offset */
  8697. src_offsets[j] = *frame_offset;
  8698. /* dst offset */
  8699. dst_offsets[j] = dynamic_offset;
  8700. j++;
  8701. }
  8702. if (opcode == WASM_OP_ELSE) {
  8703. *frame_offset = dynamic_offset;
  8704. }
  8705. else {
  8706. loader_ctx->frame_offset = frame_offset;
  8707. loader_ctx->dynamic_offset = dynamic_offset;
  8708. PUSH_OFFSET_TYPE(return_types[i]);
  8709. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8710. loader_ctx->frame_offset = frame_offset_org;
  8711. loader_ctx->dynamic_offset = dynamic_offset_org;
  8712. }
  8713. }
  8714. bh_assert(j == value_count);
  8715. /* Emit the cells, src_offsets and dst_offsets */
  8716. for (j = 0; j < value_count; j++)
  8717. emit_byte(loader_ctx, cells[j]);
  8718. for (j = 0; j < value_count; j++)
  8719. emit_operand(loader_ctx, src_offsets[j]);
  8720. for (j = 0; j < value_count; j++)
  8721. emit_operand(loader_ctx, dst_offsets[j]);
  8722. if (opcode == WASM_OP_ELSE)
  8723. emit_label(opcode);
  8724. wasm_runtime_free(emit_data);
  8725. }
  8726. return true;
  8727. fail:
  8728. return false;
  8729. }
  8730. #endif /* WASM_ENABLE_FAST_INTERP */
  8731. #define RESERVE_BLOCK_RET() \
  8732. do { \
  8733. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8734. error_buf_size)) \
  8735. goto fail; \
  8736. } while (0)
  8737. #define PUSH_TYPE(type) \
  8738. do { \
  8739. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8740. error_buf_size))) \
  8741. goto fail; \
  8742. } while (0)
  8743. #define POP_TYPE(type) \
  8744. do { \
  8745. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8746. error_buf_size))) \
  8747. goto fail; \
  8748. } while (0)
  8749. #if WASM_ENABLE_GC == 0
  8750. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8751. do { \
  8752. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8753. _start_addr, error_buf, \
  8754. error_buf_size)) \
  8755. goto fail; \
  8756. } while (0)
  8757. #define POP_CSP() \
  8758. do { \
  8759. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8760. goto fail; \
  8761. } while (0)
  8762. #else
  8763. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8764. do { \
  8765. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8766. _start_addr, error_buf, \
  8767. error_buf_size)) \
  8768. goto fail; \
  8769. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8770. error_buf, error_buf_size)) { \
  8771. goto fail; \
  8772. } \
  8773. } while (0)
  8774. #define POP_CSP() \
  8775. do { \
  8776. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8777. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8778. goto fail; \
  8779. } while (0)
  8780. #endif /* end of WASM_ENABLE_GC == 0 */
  8781. #if WASM_ENABLE_GC == 0
  8782. #define GET_LOCAL_REFTYPE() (void)0
  8783. #else
  8784. #define GET_LOCAL_REFTYPE() \
  8785. do { \
  8786. if (wasm_is_type_multi_byte_type(local_type)) { \
  8787. WASMRefType *_ref_type; \
  8788. if (local_idx < param_count) \
  8789. _ref_type = wasm_reftype_map_find( \
  8790. param_reftype_maps, param_reftype_map_count, local_idx); \
  8791. else \
  8792. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8793. local_reftype_map_count, \
  8794. local_idx - param_count); \
  8795. bh_assert(_ref_type); \
  8796. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8797. wasm_reftype_struct_size(_ref_type)); \
  8798. } \
  8799. } while (0)
  8800. #endif /* end of WASM_ENABLE_GC == 0 */
  8801. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8802. do { \
  8803. read_leb_uint32(p, p_end, local_idx); \
  8804. if (local_idx >= param_count + local_count) { \
  8805. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8806. goto fail; \
  8807. } \
  8808. local_type = local_idx < param_count \
  8809. ? param_types[local_idx] \
  8810. : local_types[local_idx - param_count]; \
  8811. local_offset = local_offsets[local_idx]; \
  8812. GET_LOCAL_REFTYPE(); \
  8813. } while (0)
  8814. static bool
  8815. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8816. {
  8817. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8818. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8819. return false;
  8820. }
  8821. return true;
  8822. }
  8823. #define CHECK_MEMORY() \
  8824. do { \
  8825. if (!check_memory(module, error_buf, error_buf_size)) \
  8826. goto fail; \
  8827. } while (0)
  8828. static bool
  8829. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8830. uint32 error_buf_size)
  8831. {
  8832. uint8 mem_access_aligns[] = {
  8833. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8834. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8835. };
  8836. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8837. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8838. set_error_buf(error_buf, error_buf_size,
  8839. "alignment must not be larger than natural");
  8840. return false;
  8841. }
  8842. return true;
  8843. }
  8844. #if WASM_ENABLE_SIMD != 0
  8845. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8846. static bool
  8847. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8848. uint32 error_buf_size)
  8849. {
  8850. uint8 mem_access_aligns[] = {
  8851. 4, /* load */
  8852. 3, 3, 3, 3, 3, 3, /* load and extend */
  8853. 0, 1, 2, 3, /* load and splat */
  8854. 4, /* store */
  8855. };
  8856. uint8 mem_access_aligns_load_lane[] = {
  8857. 0, 1, 2, 3, /* load lane */
  8858. 0, 1, 2, 3, /* store lane */
  8859. 2, 3 /* store zero */
  8860. };
  8861. if (!((opcode <= SIMD_v128_store)
  8862. || (SIMD_v128_load8_lane <= opcode
  8863. && opcode <= SIMD_v128_load64_zero))) {
  8864. set_error_buf(error_buf, error_buf_size,
  8865. "the opcode doesn't include memarg");
  8866. return false;
  8867. }
  8868. if ((opcode <= SIMD_v128_store
  8869. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8870. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8871. && align > mem_access_aligns_load_lane[opcode
  8872. - SIMD_v128_load8_lane])) {
  8873. set_error_buf(error_buf, error_buf_size,
  8874. "alignment must not be larger than natural");
  8875. return false;
  8876. }
  8877. return true;
  8878. }
  8879. static bool
  8880. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8881. uint32 error_buf_size)
  8882. {
  8883. switch (opcode) {
  8884. case SIMD_i8x16_extract_lane_s:
  8885. case SIMD_i8x16_extract_lane_u:
  8886. case SIMD_i8x16_replace_lane:
  8887. if (lane >= 16) {
  8888. goto fail;
  8889. }
  8890. break;
  8891. case SIMD_i16x8_extract_lane_s:
  8892. case SIMD_i16x8_extract_lane_u:
  8893. case SIMD_i16x8_replace_lane:
  8894. if (lane >= 8) {
  8895. goto fail;
  8896. }
  8897. break;
  8898. case SIMD_i32x4_extract_lane:
  8899. case SIMD_i32x4_replace_lane:
  8900. case SIMD_f32x4_extract_lane:
  8901. case SIMD_f32x4_replace_lane:
  8902. if (lane >= 4) {
  8903. goto fail;
  8904. }
  8905. break;
  8906. case SIMD_i64x2_extract_lane:
  8907. case SIMD_i64x2_replace_lane:
  8908. case SIMD_f64x2_extract_lane:
  8909. case SIMD_f64x2_replace_lane:
  8910. if (lane >= 2) {
  8911. goto fail;
  8912. }
  8913. break;
  8914. case SIMD_v128_load8_lane:
  8915. case SIMD_v128_load16_lane:
  8916. case SIMD_v128_load32_lane:
  8917. case SIMD_v128_load64_lane:
  8918. case SIMD_v128_store8_lane:
  8919. case SIMD_v128_store16_lane:
  8920. case SIMD_v128_store32_lane:
  8921. case SIMD_v128_store64_lane:
  8922. case SIMD_v128_load32_zero:
  8923. case SIMD_v128_load64_zero:
  8924. {
  8925. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8926. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8927. goto fail;
  8928. }
  8929. break;
  8930. }
  8931. default:
  8932. goto fail;
  8933. }
  8934. return true;
  8935. fail:
  8936. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8937. return false;
  8938. }
  8939. static bool
  8940. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8941. {
  8942. uint8 i;
  8943. for (i = 0; i != 16; ++i) {
  8944. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8945. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8946. return false;
  8947. }
  8948. }
  8949. return true;
  8950. }
  8951. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8952. #endif /* end of WASM_ENABLE_SIMD */
  8953. #if WASM_ENABLE_SHARED_MEMORY != 0
  8954. static bool
  8955. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8956. uint32 error_buf_size)
  8957. {
  8958. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8959. uint8 mem_access_aligns[] = {
  8960. 2, 3, 0, 1, 0, 1, 2,
  8961. };
  8962. uint8 expect;
  8963. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8964. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8965. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8966. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8967. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8968. }
  8969. else {
  8970. /* 7 opcodes in every group */
  8971. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8972. }
  8973. if (align != expect) {
  8974. set_error_buf(error_buf, error_buf_size,
  8975. "alignment isn't equal to natural");
  8976. return false;
  8977. }
  8978. return true;
  8979. }
  8980. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8981. static bool
  8982. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  8983. char *error_buf, uint32 error_buf_size)
  8984. {
  8985. BranchBlock *target_block, *cur_block;
  8986. BlockType *target_block_type;
  8987. uint8 type, *types = NULL, *frame_ref;
  8988. uint32 arity = 0;
  8989. int32 i, available_stack_cell;
  8990. uint16 cell_num;
  8991. #if WASM_ENABLE_GC != 0
  8992. WASMRefTypeMap *frame_reftype_map;
  8993. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  8994. WASMRefType *ref_type;
  8995. uint32 reftype_map_count = 0;
  8996. int32 available_reftype_map;
  8997. bool is_type_multi_byte;
  8998. #endif
  8999. uint8 *frame_ref_old = loader_ctx->frame_ref;
  9000. uint8 *frame_ref_after_popped = NULL;
  9001. uint8 frame_ref_tmp[4] = { 0 };
  9002. uint8 *frame_ref_buf = frame_ref_tmp;
  9003. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  9004. #if WASM_ENABLE_GC != 0
  9005. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  9006. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  9007. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  9008. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  9009. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  9010. #endif
  9011. #if WASM_ENABLE_FAST_INTERP != 0
  9012. int16 *frame_offset_old = loader_ctx->frame_offset;
  9013. int16 *frame_offset_after_popped = NULL;
  9014. int16 frame_offset_tmp[4] = { 0 };
  9015. int16 *frame_offset_buf = frame_offset_tmp;
  9016. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9017. #endif
  9018. bool ret = false;
  9019. bh_assert(loader_ctx->csp_num > 0);
  9020. if (loader_ctx->csp_num - 1 < depth) {
  9021. set_error_buf(error_buf, error_buf_size,
  9022. "unknown label, "
  9023. "unexpected end of section or function");
  9024. return false;
  9025. }
  9026. cur_block = loader_ctx->frame_csp - 1;
  9027. target_block = loader_ctx->frame_csp - (depth + 1);
  9028. target_block_type = &target_block->block_type;
  9029. frame_ref = loader_ctx->frame_ref;
  9030. #if WASM_ENABLE_GC != 0
  9031. frame_reftype_map = loader_ctx->frame_reftype_map;
  9032. #endif
  9033. /* Note: loop's arity is different from if and block. loop's arity is
  9034. * its parameter count while if and block arity is result count.
  9035. */
  9036. #if WASM_ENABLE_GC == 0
  9037. if (target_block->label_type == LABEL_TYPE_LOOP)
  9038. arity = block_type_get_param_types(target_block_type, &types);
  9039. else
  9040. arity = block_type_get_result_types(target_block_type, &types);
  9041. #else
  9042. if (target_block->label_type == LABEL_TYPE_LOOP)
  9043. arity = block_type_get_param_types(target_block_type, &types,
  9044. &reftype_maps, &reftype_map_count);
  9045. else
  9046. arity = block_type_get_result_types(target_block_type, &types,
  9047. &reftype_maps, &reftype_map_count);
  9048. #endif
  9049. /* If the stack is in polymorphic state, just clear the stack
  9050. * and then re-push the values to make the stack top values
  9051. * match block type. */
  9052. if (cur_block->is_stack_polymorphic) {
  9053. #if WASM_ENABLE_GC != 0
  9054. int32 j = reftype_map_count - 1;
  9055. #endif
  9056. for (i = (int32)arity - 1; i >= 0; i--) {
  9057. #if WASM_ENABLE_GC != 0
  9058. if (wasm_is_type_multi_byte_type(types[i])) {
  9059. bh_assert(reftype_maps[j].index == i);
  9060. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9061. reftype_maps[j].ref_type,
  9062. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9063. j--;
  9064. }
  9065. #endif
  9066. #if WASM_ENABLE_FAST_INTERP != 0
  9067. POP_OFFSET_TYPE(types[i]);
  9068. #endif
  9069. POP_TYPE(types[i]);
  9070. }
  9071. /* Backup stack data since it may be changed in the below
  9072. push operations, and the stack data may be used when
  9073. checking other target blocks of opcode br_table */
  9074. if (opcode == WASM_OP_BR_TABLE) {
  9075. uint64 total_size;
  9076. frame_ref_after_popped = loader_ctx->frame_ref;
  9077. total_size = (uint64)sizeof(uint8)
  9078. * (frame_ref_old - frame_ref_after_popped);
  9079. if (total_size > sizeof(frame_ref_tmp)
  9080. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9081. error_buf_size))) {
  9082. goto fail;
  9083. }
  9084. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9085. frame_ref_after_popped, (uint32)total_size);
  9086. #if WASM_ENABLE_GC != 0
  9087. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9088. total_size =
  9089. (uint64)sizeof(WASMRefTypeMap)
  9090. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9091. if (total_size > sizeof(frame_reftype_map_tmp)
  9092. && !(frame_reftype_map_buf = loader_malloc(
  9093. total_size, error_buf, error_buf_size))) {
  9094. goto fail;
  9095. }
  9096. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9097. frame_reftype_map_after_popped, (uint32)total_size);
  9098. #endif
  9099. #if WASM_ENABLE_FAST_INTERP != 0
  9100. frame_offset_after_popped = loader_ctx->frame_offset;
  9101. total_size = (uint64)sizeof(int16)
  9102. * (frame_offset_old - frame_offset_after_popped);
  9103. if (total_size > sizeof(frame_offset_tmp)
  9104. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9105. error_buf_size))) {
  9106. goto fail;
  9107. }
  9108. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9109. frame_offset_after_popped, (uint32)total_size);
  9110. #endif
  9111. }
  9112. #if WASM_ENABLE_GC != 0
  9113. j = 0;
  9114. #endif
  9115. for (i = 0; i < (int32)arity; i++) {
  9116. #if WASM_ENABLE_GC != 0
  9117. if (wasm_is_type_multi_byte_type(types[i])) {
  9118. bh_assert(reftype_maps[j].index == i);
  9119. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9120. reftype_maps[j].ref_type,
  9121. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9122. j++;
  9123. }
  9124. #endif
  9125. #if WASM_ENABLE_FAST_INTERP != 0
  9126. bool disable_emit = true;
  9127. int16 operand_offset = 0;
  9128. PUSH_OFFSET_TYPE(types[i]);
  9129. #endif
  9130. PUSH_TYPE(types[i]);
  9131. }
  9132. #if WASM_ENABLE_FAST_INTERP != 0
  9133. emit_br_info(target_block, opcode == WASM_OP_BR);
  9134. #endif
  9135. /* Restore the stack data, note that frame_ref_bottom,
  9136. frame_reftype_map_bottom, frame_offset_bottom may be
  9137. re-allocated in the above push operations */
  9138. if (opcode == WASM_OP_BR_TABLE) {
  9139. uint32 total_size;
  9140. /* The stack operand num should not be smaller than before
  9141. after pop and push operations */
  9142. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9143. loader_ctx->stack_cell_num = stack_cell_num_old;
  9144. loader_ctx->frame_ref =
  9145. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9146. total_size = (uint32)(sizeof(uint8)
  9147. * (frame_ref_old - frame_ref_after_popped));
  9148. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9149. frame_ref_buf, total_size);
  9150. #if WASM_ENABLE_GC != 0
  9151. /* The stack operand num should not be smaller than before
  9152. after pop and push operations */
  9153. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9154. loader_ctx->reftype_map_num = reftype_map_num_old;
  9155. loader_ctx->frame_reftype_map =
  9156. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9157. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9158. * (frame_reftype_map_old
  9159. - frame_reftype_map_after_popped));
  9160. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9161. total_size, frame_reftype_map_buf, total_size);
  9162. #endif
  9163. #if WASM_ENABLE_FAST_INTERP != 0
  9164. loader_ctx->frame_offset =
  9165. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9166. total_size =
  9167. (uint32)(sizeof(int16)
  9168. * (frame_offset_old - frame_offset_after_popped));
  9169. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9170. total_size, frame_offset_buf, total_size);
  9171. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9172. #endif
  9173. }
  9174. ret = true;
  9175. goto cleanup_and_return;
  9176. }
  9177. available_stack_cell =
  9178. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9179. #if WASM_ENABLE_GC != 0
  9180. available_reftype_map =
  9181. (int32)(loader_ctx->reftype_map_num
  9182. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9183. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9184. #endif
  9185. /* Check stack top values match target block type */
  9186. for (i = (int32)arity - 1; i >= 0; i--) {
  9187. type = types[i];
  9188. #if WASM_ENABLE_GC != 0
  9189. ref_type = NULL;
  9190. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9191. if (is_type_multi_byte) {
  9192. bh_assert(reftype_map);
  9193. ref_type = reftype_map->ref_type;
  9194. }
  9195. #endif
  9196. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9197. break;
  9198. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9199. #if WASM_ENABLE_GC != 0
  9200. frame_reftype_map, available_reftype_map,
  9201. #endif
  9202. type,
  9203. #if WASM_ENABLE_GC != 0
  9204. ref_type,
  9205. #endif
  9206. error_buf, error_buf_size)) {
  9207. goto fail;
  9208. }
  9209. cell_num = wasm_value_type_cell_num(types[i]);
  9210. frame_ref -= cell_num;
  9211. available_stack_cell -= cell_num;
  9212. #if WASM_ENABLE_GC != 0
  9213. if (is_type_multi_byte) {
  9214. frame_reftype_map--;
  9215. available_reftype_map--;
  9216. reftype_map--;
  9217. }
  9218. #endif
  9219. }
  9220. #if WASM_ENABLE_FAST_INTERP != 0
  9221. emit_br_info(target_block, opcode == WASM_OP_BR);
  9222. #endif
  9223. ret = true;
  9224. cleanup_and_return:
  9225. fail:
  9226. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9227. wasm_runtime_free(frame_ref_buf);
  9228. #if WASM_ENABLE_GC != 0
  9229. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9230. wasm_runtime_free(frame_reftype_map_buf);
  9231. #endif
  9232. #if WASM_ENABLE_FAST_INTERP != 0
  9233. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9234. wasm_runtime_free(frame_offset_buf);
  9235. #endif
  9236. return ret;
  9237. }
  9238. static BranchBlock *
  9239. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9240. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9241. {
  9242. uint8 *p = *p_buf, *p_end = buf_end;
  9243. BranchBlock *frame_csp_tmp;
  9244. uint32 depth;
  9245. read_leb_uint32(p, p_end, depth);
  9246. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9247. error_buf_size)) {
  9248. goto fail;
  9249. }
  9250. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9251. *p_buf = p;
  9252. return frame_csp_tmp;
  9253. fail:
  9254. return NULL;
  9255. }
  9256. #if WASM_ENABLE_EXCE_HANDLING != 0
  9257. static BranchBlock *
  9258. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9259. uint8 *buf_end, char *error_buf,
  9260. uint32 error_buf_size)
  9261. {
  9262. uint8 *p = *p_buf, *p_end = buf_end;
  9263. BranchBlock *frame_csp_tmp;
  9264. uint32 depth;
  9265. read_leb_uint32(p, p_end, depth);
  9266. /*
  9267. * Note: "delegate 0" means the surrounding block, not the
  9268. * try-delegate block itself.
  9269. *
  9270. * Note: the caller hasn't popped the try-delegate frame yet.
  9271. */
  9272. bh_assert(loader_ctx->csp_num > 0);
  9273. if (loader_ctx->csp_num - 1 <= depth) {
  9274. #if WASM_ENABLE_SPEC_TEST == 0
  9275. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9276. #else
  9277. set_error_buf(error_buf, error_buf_size, "unknown label");
  9278. #endif
  9279. goto fail;
  9280. }
  9281. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9282. #if WASM_ENABLE_FAST_INTERP != 0
  9283. emit_br_info(frame_csp_tmp, false);
  9284. #endif
  9285. *p_buf = p;
  9286. return frame_csp_tmp;
  9287. fail:
  9288. return NULL;
  9289. }
  9290. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9291. static bool
  9292. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9293. char *error_buf, uint32 error_buf_size)
  9294. {
  9295. BlockType *block_type = &block->block_type;
  9296. uint8 *return_types = NULL;
  9297. uint32 return_count = 0;
  9298. int32 available_stack_cell, return_cell_num, i;
  9299. uint8 *frame_ref = NULL;
  9300. #if WASM_ENABLE_GC != 0
  9301. WASMRefTypeMap *frame_reftype_map;
  9302. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9303. WASMRefType *ref_type;
  9304. uint32 param_count, return_reftype_map_count = 0;
  9305. int32 available_reftype_map =
  9306. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9307. #endif
  9308. available_stack_cell =
  9309. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9310. #if WASM_ENABLE_GC == 0
  9311. return_count = block_type_get_result_types(block_type, &return_types);
  9312. #else
  9313. return_count = block_type_get_result_types(block_type, &return_types,
  9314. &return_reftype_maps,
  9315. &return_reftype_map_count);
  9316. param_count =
  9317. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9318. (void)param_count;
  9319. #endif
  9320. return_cell_num =
  9321. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9322. /* If the stack is in polymorphic state, just clear the stack
  9323. * and then re-push the values to make the stack top values
  9324. * match block type. */
  9325. if (block->is_stack_polymorphic) {
  9326. #if WASM_ENABLE_GC != 0
  9327. int32 j = return_reftype_map_count - 1;
  9328. #endif
  9329. for (i = (int32)return_count - 1; i >= 0; i--) {
  9330. #if WASM_ENABLE_GC != 0
  9331. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9332. bh_assert(return_reftype_maps[j].index == i + param_count);
  9333. bh_memcpy_s(
  9334. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9335. return_reftype_maps[j].ref_type,
  9336. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9337. j--;
  9338. }
  9339. #endif
  9340. #if WASM_ENABLE_FAST_INTERP != 0
  9341. POP_OFFSET_TYPE(return_types[i]);
  9342. #endif
  9343. POP_TYPE(return_types[i]);
  9344. }
  9345. /* Check stack is empty */
  9346. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9347. set_error_buf(
  9348. error_buf, error_buf_size,
  9349. "type mismatch: stack size does not match block type");
  9350. goto fail;
  9351. }
  9352. #if WASM_ENABLE_GC != 0
  9353. j = 0;
  9354. #endif
  9355. for (i = 0; i < (int32)return_count; i++) {
  9356. #if WASM_ENABLE_GC != 0
  9357. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9358. bh_assert(return_reftype_maps[j].index == i + param_count);
  9359. bh_memcpy_s(
  9360. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9361. return_reftype_maps[j].ref_type,
  9362. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9363. j++;
  9364. }
  9365. #endif
  9366. #if WASM_ENABLE_FAST_INTERP != 0
  9367. bool disable_emit = true;
  9368. int16 operand_offset = 0;
  9369. PUSH_OFFSET_TYPE(return_types[i]);
  9370. #endif
  9371. PUSH_TYPE(return_types[i]);
  9372. }
  9373. return true;
  9374. }
  9375. if (available_stack_cell != return_cell_num) {
  9376. #if WASM_ENABLE_EXCE_HANDLING != 0
  9377. /* testspec: this error message format is expected by try_catch.wast */
  9378. snprintf(
  9379. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9380. block->label_type == LABEL_TYPE_TRY
  9381. || (block->label_type == LABEL_TYPE_CATCH
  9382. && return_cell_num > 0)
  9383. ? "instruction"
  9384. : "block",
  9385. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9386. " but stack has ",
  9387. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9388. : "");
  9389. goto fail;
  9390. #else
  9391. set_error_buf(error_buf, error_buf_size,
  9392. "type mismatch: stack size does not match block type");
  9393. goto fail;
  9394. #endif
  9395. }
  9396. /* Check stack values match return types */
  9397. frame_ref = loader_ctx->frame_ref;
  9398. #if WASM_ENABLE_GC != 0
  9399. frame_reftype_map = loader_ctx->frame_reftype_map;
  9400. return_reftype_map =
  9401. return_reftype_map_count
  9402. ? return_reftype_maps + return_reftype_map_count - 1
  9403. : NULL;
  9404. #endif
  9405. for (i = (int32)return_count - 1; i >= 0; i--) {
  9406. uint8 type = return_types[i];
  9407. #if WASM_ENABLE_GC != 0
  9408. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9409. ref_type = NULL;
  9410. if (is_type_multi_byte) {
  9411. bh_assert(return_reftype_map);
  9412. ref_type = return_reftype_map->ref_type;
  9413. }
  9414. #endif
  9415. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9416. #if WASM_ENABLE_GC != 0
  9417. frame_reftype_map, available_reftype_map,
  9418. #endif
  9419. type,
  9420. #if WASM_ENABLE_GC != 0
  9421. ref_type,
  9422. #endif
  9423. error_buf, error_buf_size))
  9424. return false;
  9425. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9426. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9427. #if WASM_ENABLE_GC != 0
  9428. if (is_type_multi_byte) {
  9429. frame_reftype_map--;
  9430. available_reftype_map--;
  9431. return_reftype_map--;
  9432. }
  9433. #endif
  9434. }
  9435. return true;
  9436. fail:
  9437. return false;
  9438. }
  9439. #if WASM_ENABLE_FAST_INTERP != 0
  9440. /* Copy parameters to dynamic space.
  9441. * 1) POP original parameter out;
  9442. * 2) Push and copy original values to dynamic space.
  9443. * The copy instruction format:
  9444. * Part a: param count
  9445. * Part b: all param total cell num
  9446. * Part c: each param's cell_num, src offset and dst offset
  9447. * Part d: each param's src offset
  9448. * Part e: each param's dst offset
  9449. */
  9450. static bool
  9451. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  9452. char *error_buf, uint32 error_buf_size)
  9453. {
  9454. bool ret = false;
  9455. int16 *frame_offset = NULL;
  9456. uint8 *cells = NULL, cell;
  9457. int16 *src_offsets = NULL;
  9458. uint8 *emit_data = NULL;
  9459. uint32 i;
  9460. BranchBlock *block = loader_ctx->frame_csp - 1;
  9461. BlockType *block_type = &block->block_type;
  9462. WASMFuncType *wasm_type = block_type->u.type;
  9463. uint32 param_count = block_type->u.type->param_count;
  9464. int16 condition_offset = 0;
  9465. bool disable_emit = false;
  9466. int16 operand_offset = 0;
  9467. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9468. bh_assert(size > 0);
  9469. /* For if block, we also need copy the condition operand offset. */
  9470. if (is_if_block)
  9471. size += sizeof(*cells) + sizeof(*src_offsets);
  9472. /* Allocate memory for the emit data */
  9473. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9474. return false;
  9475. cells = emit_data;
  9476. src_offsets = (int16 *)(cells + param_count);
  9477. if (is_if_block)
  9478. condition_offset = *loader_ctx->frame_offset;
  9479. /* POP original parameter out */
  9480. for (i = 0; i < param_count; i++) {
  9481. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9482. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9483. }
  9484. frame_offset = loader_ctx->frame_offset;
  9485. /* Get each param's cell num and src offset */
  9486. for (i = 0; i < param_count; i++) {
  9487. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9488. cells[i] = cell;
  9489. src_offsets[i] = *frame_offset;
  9490. frame_offset += cell;
  9491. }
  9492. /* emit copy instruction */
  9493. emit_label(EXT_OP_COPY_STACK_VALUES);
  9494. /* Part a) */
  9495. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9496. /* Part b) */
  9497. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9498. : wasm_type->param_cell_num);
  9499. /* Part c) */
  9500. for (i = 0; i < param_count; i++)
  9501. emit_byte(loader_ctx, cells[i]);
  9502. if (is_if_block)
  9503. emit_byte(loader_ctx, 1);
  9504. /* Part d) */
  9505. for (i = 0; i < param_count; i++)
  9506. emit_operand(loader_ctx, src_offsets[i]);
  9507. if (is_if_block)
  9508. emit_operand(loader_ctx, condition_offset);
  9509. /* Part e) */
  9510. /* Push to dynamic space. The push will emit the dst offset. */
  9511. for (i = 0; i < param_count; i++)
  9512. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9513. if (is_if_block)
  9514. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9515. ret = true;
  9516. fail:
  9517. /* Free the emit data */
  9518. wasm_runtime_free(emit_data);
  9519. return ret;
  9520. }
  9521. #endif
  9522. #if WASM_ENABLE_GC == 0
  9523. #define RESET_REFTYPE_MAP_STACK() (void)0
  9524. #else
  9525. #define RESET_REFTYPE_MAP_STACK() \
  9526. do { \
  9527. loader_ctx->reftype_map_num = \
  9528. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9529. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9530. + loader_ctx->reftype_map_num; \
  9531. } while (0)
  9532. #endif
  9533. /* reset the stack to the state of before entering the last block */
  9534. #if WASM_ENABLE_FAST_INTERP != 0
  9535. #define RESET_STACK() \
  9536. do { \
  9537. loader_ctx->stack_cell_num = \
  9538. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9539. loader_ctx->frame_ref = \
  9540. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9541. loader_ctx->frame_offset = \
  9542. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9543. RESET_REFTYPE_MAP_STACK(); \
  9544. } while (0)
  9545. #else
  9546. #define RESET_STACK() \
  9547. do { \
  9548. loader_ctx->stack_cell_num = \
  9549. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9550. loader_ctx->frame_ref = \
  9551. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9552. RESET_REFTYPE_MAP_STACK(); \
  9553. } while (0)
  9554. #endif
  9555. /* set current block's stack polymorphic state */
  9556. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9557. do { \
  9558. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9559. _cur_block->is_stack_polymorphic = flag; \
  9560. } while (0)
  9561. #define BLOCK_HAS_PARAM(block_type) \
  9562. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9563. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9564. do { \
  9565. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9566. error_buf_size))) { \
  9567. goto fail; \
  9568. } \
  9569. } while (0)
  9570. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9571. static bool
  9572. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9573. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9574. uint32 error_buf_size)
  9575. {
  9576. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9577. return false;
  9578. }
  9579. if (table_idx < module->import_table_count) {
  9580. if (p_elem_type)
  9581. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9582. #if WASM_ENABLE_GC != 0
  9583. if (p_ref_type)
  9584. *((WASMRefType **)p_ref_type) =
  9585. module->import_tables[table_idx].u.table.elem_ref_type;
  9586. #endif
  9587. }
  9588. else {
  9589. if (p_elem_type)
  9590. *p_elem_type =
  9591. module->tables[module->import_table_count + table_idx]
  9592. .elem_type;
  9593. #if WASM_ENABLE_GC != 0
  9594. if (p_ref_type)
  9595. *((WASMRefType **)p_ref_type) =
  9596. module->tables[module->import_table_count + table_idx]
  9597. .elem_ref_type;
  9598. #endif
  9599. }
  9600. return true;
  9601. }
  9602. static bool
  9603. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9604. uint8 *p_elem_type, void **p_elem_ref_type,
  9605. char *error_buf, uint32 error_buf_size)
  9606. {
  9607. if (table_seg_idx >= module->table_seg_count) {
  9608. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9609. table_seg_idx);
  9610. return false;
  9611. }
  9612. if (p_elem_type) {
  9613. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9614. }
  9615. #if WASM_ENABLE_GC != 0
  9616. if (p_elem_ref_type)
  9617. *((WASMRefType **)p_elem_ref_type) =
  9618. module->table_segments[table_seg_idx].elem_ref_type;
  9619. #endif
  9620. return true;
  9621. }
  9622. #endif
  9623. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9624. const uint8 *
  9625. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9626. uint32 *len)
  9627. {
  9628. WASMCustomSection *section = module->custom_section_list;
  9629. while (section) {
  9630. if ((section->name_len == strlen(name))
  9631. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9632. if (len) {
  9633. *len = section->content_len;
  9634. }
  9635. return section->content_addr;
  9636. }
  9637. section = section->next;
  9638. }
  9639. return NULL;
  9640. }
  9641. #endif
  9642. static bool
  9643. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9644. uint32 cur_func_idx, char *error_buf,
  9645. uint32 error_buf_size)
  9646. {
  9647. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9648. uint32 param_count, local_count, global_count;
  9649. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9650. BlockType func_block_type;
  9651. uint16 *local_offsets, local_offset;
  9652. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9653. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9654. mem_offset_t mem_offset;
  9655. int32 i32_const = 0;
  9656. int64 i64_const;
  9657. uint8 opcode;
  9658. bool return_value = false;
  9659. WASMLoaderContext *loader_ctx;
  9660. BranchBlock *frame_csp_tmp;
  9661. #if WASM_ENABLE_GC != 0
  9662. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9663. uint32 param_reftype_map_count, local_reftype_map_count;
  9664. int32 heap_type;
  9665. WASMRefType wasm_ref_type = { 0 };
  9666. bool need_ref_type_map;
  9667. #endif
  9668. #if WASM_ENABLE_FAST_INTERP != 0
  9669. uint8 *func_const_end, *func_const = NULL;
  9670. int16 operand_offset = 0;
  9671. uint8 last_op = 0;
  9672. bool disable_emit, preserve_local = false, if_condition_available = true;
  9673. float32 f32_const;
  9674. float64 f64_const;
  9675. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9676. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9677. #endif
  9678. #if WASM_ENABLE_MEMORY64 != 0
  9679. bool is_memory64 = has_module_memory64(module);
  9680. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9681. #else
  9682. mem_offset_type = VALUE_TYPE_I32;
  9683. #endif
  9684. global_count = module->import_global_count + module->global_count;
  9685. param_count = func->func_type->param_count;
  9686. param_types = func->func_type->types;
  9687. func_block_type.is_value_type = false;
  9688. func_block_type.u.type = func->func_type;
  9689. local_count = func->local_count;
  9690. local_types = func->local_types;
  9691. local_offsets = func->local_offsets;
  9692. #if WASM_ENABLE_GC != 0
  9693. param_reftype_maps = func->func_type->ref_type_maps;
  9694. param_reftype_map_count = func->func_type->ref_type_map_count;
  9695. local_reftype_maps = func->local_ref_type_maps;
  9696. local_reftype_map_count = func->local_ref_type_map_count;
  9697. #endif
  9698. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9699. goto fail;
  9700. }
  9701. #if WASM_ENABLE_GC != 0
  9702. loader_ctx->module = module;
  9703. loader_ctx->ref_type_set = module->ref_type_set;
  9704. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9705. #endif
  9706. #if WASM_ENABLE_FAST_INTERP != 0
  9707. /* For the first traverse, the initial value of preserved_local_offset has
  9708. * not been determined, we use the INT16_MAX to represent that a slot has
  9709. * been copied to preserve space. For second traverse, this field will be
  9710. * set to the appropriate value in wasm_loader_ctx_reinit.
  9711. * This is for Issue #1230,
  9712. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9713. * drop opcodes need to know which slots are preserved, so those slots will
  9714. * not be treated as dynamically allocated slots */
  9715. loader_ctx->preserved_local_offset = INT16_MAX;
  9716. re_scan:
  9717. if (loader_ctx->code_compiled_size > 0) {
  9718. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9719. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9720. goto fail;
  9721. }
  9722. p = func->code;
  9723. func->code_compiled = loader_ctx->p_code_compiled;
  9724. func->code_compiled_size = loader_ctx->code_compiled_size;
  9725. }
  9726. #endif
  9727. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9728. while (p < p_end) {
  9729. opcode = *p++;
  9730. #if WASM_ENABLE_FAST_INTERP != 0
  9731. p_org = p;
  9732. disable_emit = false;
  9733. emit_label(opcode);
  9734. #endif
  9735. switch (opcode) {
  9736. case WASM_OP_UNREACHABLE:
  9737. RESET_STACK();
  9738. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9739. break;
  9740. case WASM_OP_NOP:
  9741. #if WASM_ENABLE_FAST_INTERP != 0
  9742. skip_label();
  9743. #endif
  9744. break;
  9745. case WASM_OP_IF:
  9746. {
  9747. #if WASM_ENABLE_FAST_INTERP != 0
  9748. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9749. int32 available_stack_cell =
  9750. (int32)(loader_ctx->stack_cell_num
  9751. - parent_block->stack_cell_num);
  9752. if (available_stack_cell <= 0
  9753. && parent_block->is_stack_polymorphic)
  9754. if_condition_available = false;
  9755. else
  9756. if_condition_available = true;
  9757. PRESERVE_LOCAL_FOR_BLOCK();
  9758. #endif
  9759. #if WASM_ENABLE_GC == 0
  9760. POP_I32();
  9761. #endif
  9762. goto handle_op_block_and_loop;
  9763. }
  9764. case WASM_OP_BLOCK:
  9765. case WASM_OP_LOOP:
  9766. #if WASM_ENABLE_EXCE_HANDLING != 0
  9767. case WASM_OP_TRY:
  9768. if (opcode == WASM_OP_TRY) {
  9769. /*
  9770. * keep track of exception handlers to account for
  9771. * memory allocation
  9772. */
  9773. func->exception_handler_count++;
  9774. /*
  9775. * try is a block
  9776. * do nothing special, but execution continues to
  9777. * to handle_op_block_and_loop,
  9778. * and that be pushes the csp
  9779. */
  9780. }
  9781. #endif
  9782. #if WASM_ENABLE_FAST_INTERP != 0
  9783. PRESERVE_LOCAL_FOR_BLOCK();
  9784. #endif
  9785. handle_op_block_and_loop:
  9786. {
  9787. uint8 value_type;
  9788. BlockType block_type;
  9789. #if WASM_ENABLE_FAST_INTERP != 0
  9790. uint32 available_params = 0;
  9791. #endif
  9792. CHECK_BUF(p, p_end, 1);
  9793. value_type = read_uint8(p);
  9794. if (is_byte_a_type(value_type)) {
  9795. /* If the first byte is one of these special values:
  9796. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9797. * the single return value. */
  9798. block_type.is_value_type = true;
  9799. block_type.u.value_type.type = value_type;
  9800. #if WASM_ENABLE_WAMR_COMPILER != 0
  9801. if (value_type == VALUE_TYPE_V128)
  9802. module->is_simd_used = true;
  9803. else if (value_type == VALUE_TYPE_FUNCREF
  9804. || value_type == VALUE_TYPE_EXTERNREF)
  9805. module->is_ref_types_used = true;
  9806. #endif
  9807. #if WASM_ENABLE_GC != 0
  9808. if (value_type != VALUE_TYPE_VOID) {
  9809. p_org = p;
  9810. p--;
  9811. if (!resolve_value_type((const uint8 **)&p, p_end,
  9812. module, module->type_count,
  9813. &need_ref_type_map,
  9814. &wasm_ref_type, false,
  9815. error_buf, error_buf_size)) {
  9816. goto fail;
  9817. }
  9818. if (need_ref_type_map) {
  9819. block_type.u.value_type.ref_type_map.index = 0;
  9820. if (!(block_type.u.value_type.ref_type_map
  9821. .ref_type = reftype_set_insert(
  9822. module->ref_type_set, &wasm_ref_type,
  9823. error_buf, error_buf_size))) {
  9824. goto fail;
  9825. }
  9826. }
  9827. /* Set again as the type might be changed, e.g.
  9828. (ref null any) to anyref */
  9829. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9830. #if WASM_ENABLE_FAST_INTERP == 0
  9831. while (p_org < p) {
  9832. #if WASM_ENABLE_DEBUG_INTERP != 0
  9833. if (!record_fast_op(module, p_org, *p_org,
  9834. error_buf, error_buf_size)) {
  9835. goto fail;
  9836. }
  9837. #endif
  9838. /* Ignore extra bytes for interpreter */
  9839. *p_org++ = WASM_OP_NOP;
  9840. }
  9841. #endif
  9842. }
  9843. #endif /* end of WASM_ENABLE_GC != 0 */
  9844. }
  9845. else {
  9846. uint32 type_index;
  9847. /* Resolve the leb128 encoded type index as block type */
  9848. p--;
  9849. p_org = p - 1;
  9850. read_leb_uint32(p, p_end, type_index);
  9851. if (type_index >= module->type_count) {
  9852. set_error_buf(error_buf, error_buf_size,
  9853. "unknown type");
  9854. goto fail;
  9855. }
  9856. block_type.is_value_type = false;
  9857. block_type.u.type =
  9858. (WASMFuncType *)module->types[type_index];
  9859. #if WASM_ENABLE_FAST_INTERP == 0
  9860. /* If block use type index as block type, change the opcode
  9861. * to new extended opcode so that interpreter can resolve
  9862. * the block quickly.
  9863. */
  9864. #if WASM_ENABLE_DEBUG_INTERP != 0
  9865. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9866. error_buf_size)) {
  9867. goto fail;
  9868. }
  9869. #endif
  9870. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9871. #endif
  9872. }
  9873. #if WASM_ENABLE_GC != 0
  9874. if (opcode == WASM_OP_IF) {
  9875. POP_I32();
  9876. }
  9877. #endif
  9878. /* Pop block parameters from stack */
  9879. if (BLOCK_HAS_PARAM(block_type)) {
  9880. WASMFuncType *wasm_type = block_type.u.type;
  9881. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9882. #if WASM_ENABLE_FAST_INTERP != 0
  9883. uint32 cell_num;
  9884. available_params = block_type.u.type->param_count;
  9885. #endif
  9886. for (i = 0; i < block_type.u.type->param_count; i++) {
  9887. int32 available_stack_cell =
  9888. (int32)(loader_ctx->stack_cell_num
  9889. - cur_block->stack_cell_num);
  9890. if (available_stack_cell <= 0
  9891. && cur_block->is_stack_polymorphic) {
  9892. #if WASM_ENABLE_FAST_INTERP != 0
  9893. available_params = i;
  9894. #endif
  9895. break;
  9896. }
  9897. POP_TYPE(
  9898. wasm_type->types[wasm_type->param_count - i - 1]);
  9899. #if WASM_ENABLE_FAST_INTERP != 0
  9900. /* decrease the frame_offset pointer accordingly to keep
  9901. * consistent with frame_ref stack */
  9902. cell_num = wasm_value_type_cell_num(
  9903. wasm_type->types[wasm_type->param_count - i - 1]);
  9904. loader_ctx->frame_offset -= cell_num;
  9905. #endif
  9906. }
  9907. }
  9908. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9909. block_type, p);
  9910. /* Pass parameters to block */
  9911. if (BLOCK_HAS_PARAM(block_type)) {
  9912. for (i = 0; i < block_type.u.type->param_count; i++) {
  9913. #if WASM_ENABLE_FAST_INTERP != 0
  9914. uint32 cell_num = wasm_value_type_cell_num(
  9915. block_type.u.type->types[i]);
  9916. if (i >= available_params) {
  9917. /* If there isn't enough data on stack, push a dummy
  9918. * offset to keep the stack consistent with
  9919. * frame_ref.
  9920. * Since the stack is already in polymorphic state,
  9921. * the opcode will not be executed, so the dummy
  9922. * offset won't cause any error */
  9923. uint32 n;
  9924. for (n = 0; n < cell_num; n++) {
  9925. if (loader_ctx->p_code_compiled == NULL) {
  9926. if (!check_offset_push(loader_ctx,
  9927. error_buf,
  9928. error_buf_size))
  9929. goto fail;
  9930. }
  9931. *loader_ctx->frame_offset++ = 0;
  9932. }
  9933. }
  9934. else {
  9935. loader_ctx->frame_offset += cell_num;
  9936. }
  9937. #endif
  9938. PUSH_TYPE(block_type.u.type->types[i]);
  9939. }
  9940. }
  9941. #if WASM_ENABLE_FAST_INTERP != 0
  9942. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  9943. skip_label();
  9944. if (BLOCK_HAS_PARAM(block_type)) {
  9945. /* Make sure params are in dynamic space */
  9946. if (!copy_params_to_dynamic_space(
  9947. loader_ctx, false, error_buf, error_buf_size))
  9948. goto fail;
  9949. }
  9950. if (opcode == WASM_OP_LOOP) {
  9951. (loader_ctx->frame_csp - 1)->code_compiled =
  9952. loader_ctx->p_code_compiled;
  9953. }
  9954. }
  9955. #if WASM_ENABLE_EXCE_HANDLING != 0
  9956. else if (opcode == WASM_OP_TRY) {
  9957. skip_label();
  9958. }
  9959. #endif
  9960. else if (opcode == WASM_OP_IF) {
  9961. BranchBlock *block = loader_ctx->frame_csp - 1;
  9962. /* If block has parameters, we should make sure they are in
  9963. * dynamic space. Otherwise, when else branch is missing,
  9964. * the later opcode may consume incorrect operand offset.
  9965. * Spec case:
  9966. * (func (export "params-id") (param i32) (result i32)
  9967. * (i32.const 1)
  9968. * (i32.const 2)
  9969. * (if (param i32 i32) (result i32 i32) (local.get 0)
  9970. * (then)) (i32.add)
  9971. * )
  9972. *
  9973. * So we should emit a copy instruction before the if.
  9974. *
  9975. * And we also need to save the parameter offsets and
  9976. * recover them before entering else branch.
  9977. *
  9978. */
  9979. if (BLOCK_HAS_PARAM(block_type)) {
  9980. uint64 size;
  9981. /* In polymorphic state, there may be no if condition on
  9982. * the stack, so the offset may not emitted */
  9983. if (if_condition_available) {
  9984. /* skip the if condition operand offset */
  9985. wasm_loader_emit_backspace(loader_ctx,
  9986. sizeof(int16));
  9987. }
  9988. /* skip the if label */
  9989. skip_label();
  9990. /* Emit a copy instruction */
  9991. if (!copy_params_to_dynamic_space(
  9992. loader_ctx, true, error_buf, error_buf_size))
  9993. goto fail;
  9994. /* Emit the if instruction */
  9995. emit_label(opcode);
  9996. /* Emit the new condition operand offset */
  9997. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  9998. /* Save top param_count values of frame_offset stack, so
  9999. * that we can recover it before executing else branch
  10000. */
  10001. size = sizeof(int16)
  10002. * (uint64)block_type.u.type->param_cell_num;
  10003. if (!(block->param_frame_offsets = loader_malloc(
  10004. size, error_buf, error_buf_size)))
  10005. goto fail;
  10006. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  10007. loader_ctx->frame_offset
  10008. - size / sizeof(int16),
  10009. (uint32)size);
  10010. }
  10011. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  10012. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  10013. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10014. }
  10015. #endif
  10016. break;
  10017. }
  10018. #if WASM_ENABLE_EXCE_HANDLING != 0
  10019. case WASM_OP_THROW:
  10020. {
  10021. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10022. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10023. uint8 label_type = cur_block->label_type;
  10024. uint32 tag_index = 0;
  10025. read_leb_int32(p, p_end, tag_index);
  10026. /* check validity of tag_index against module->tag_count */
  10027. /* check tag index is within the tag index space */
  10028. if (tag_index >= module->import_tag_count + module->tag_count) {
  10029. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10030. tag_index);
  10031. goto fail;
  10032. }
  10033. /* the tag_type is stored in either the WASMTag (section tags)
  10034. * or WASMTagImport (import tag) */
  10035. WASMFuncType *tag_type = NULL;
  10036. if (tag_index < module->import_tag_count) {
  10037. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10038. }
  10039. else {
  10040. tag_type =
  10041. module->tags[tag_index - module->import_tag_count]
  10042. ->tag_type;
  10043. }
  10044. if (tag_type->result_count != 0) {
  10045. set_error_buf(error_buf, error_buf_size,
  10046. "tag type signature does not return void");
  10047. goto fail;
  10048. }
  10049. int32 available_stack_cell =
  10050. (int32)(loader_ctx->stack_cell_num
  10051. - cur_block->stack_cell_num);
  10052. /* Check stack values match return types by comparing tag param
  10053. * types with stack cells */
  10054. uint8 *frame_ref = loader_ctx->frame_ref;
  10055. #if WASM_ENABLE_GC != 0
  10056. WASMRefTypeMap *frame_reftype_map =
  10057. loader_ctx->frame_reftype_map;
  10058. uint32 frame_reftype_map_num = loader_ctx->reftype_map_num;
  10059. param_reftype_maps = tag_type->ref_type_maps;
  10060. /* For tag_type function, it shouldn't have result_count = 0 */
  10061. param_reftype_map_count = tag_type->ref_type_map_count;
  10062. param_count = (int32)tag_type->param_count;
  10063. #endif
  10064. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  10065. tti--) {
  10066. #if WASM_ENABLE_GC != 0
  10067. local_type = tag_type->types[tti];
  10068. local_idx = tti;
  10069. /* Get the wasm_ref_type if the local_type is multibyte
  10070. * type */
  10071. GET_LOCAL_REFTYPE();
  10072. #endif
  10073. if (!check_stack_top_values(
  10074. loader_ctx, frame_ref, available_stack_cell,
  10075. #if WASM_ENABLE_GC != 0
  10076. frame_reftype_map, frame_reftype_map_num,
  10077. #endif
  10078. tag_type->types[tti],
  10079. #if WASM_ENABLE_GC != 0
  10080. &wasm_ref_type,
  10081. #endif
  10082. error_buf, error_buf_size)) {
  10083. snprintf(error_buf, error_buf_size,
  10084. "type mismatch: instruction requires [%s] but "
  10085. "stack has [%s]",
  10086. tag_type->param_count > 0
  10087. ? type2str(tag_type->types[tti])
  10088. : "",
  10089. available_stack_cell > 0
  10090. ? type2str(*(loader_ctx->frame_ref - 1))
  10091. : "");
  10092. goto fail;
  10093. }
  10094. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10095. available_stack_cell -=
  10096. wasm_value_type_cell_num(tag_type->types[tti]);
  10097. }
  10098. /* throw is stack polymorphic */
  10099. (void)label_type;
  10100. RESET_STACK();
  10101. break;
  10102. }
  10103. case WASM_OP_RETHROW:
  10104. {
  10105. /* must be done before checking branch block */
  10106. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10107. /* check the target catching block: LABEL_TYPE_CATCH */
  10108. if (!(frame_csp_tmp =
  10109. check_branch_block(loader_ctx, &p, p_end, opcode,
  10110. error_buf, error_buf_size)))
  10111. goto fail;
  10112. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10113. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10114. /* trap according to spectest (rethrow.wast) */
  10115. set_error_buf(error_buf, error_buf_size,
  10116. "invalid rethrow label");
  10117. goto fail;
  10118. }
  10119. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10120. uint8 label_type = cur_block->label_type;
  10121. (void)label_type;
  10122. /* rethrow is stack polymorphic */
  10123. RESET_STACK();
  10124. break;
  10125. }
  10126. case WASM_OP_DELEGATE:
  10127. {
  10128. /* check target block is valid */
  10129. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10130. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10131. goto fail;
  10132. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10133. uint8 label_type = cur_block->label_type;
  10134. (void)label_type;
  10135. /* DELEGATE ends the block */
  10136. POP_CSP();
  10137. break;
  10138. }
  10139. case WASM_OP_CATCH:
  10140. {
  10141. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10142. uint8 label_type = cur_block->label_type;
  10143. uint32 tag_index = 0;
  10144. read_leb_int32(p, p_end, tag_index);
  10145. /* check validity of tag_index against module->tag_count */
  10146. /* check tag index is within the tag index space */
  10147. if (tag_index >= module->import_tag_count + module->tag_count) {
  10148. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10149. __FUNCTION__);
  10150. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10151. goto fail;
  10152. }
  10153. /* the tag_type is stored in either the WASMTag (section tags)
  10154. * or WASMTagImport (import tag) */
  10155. WASMFuncType *func_type = NULL;
  10156. if (tag_index < module->import_tag_count) {
  10157. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10158. }
  10159. else {
  10160. func_type =
  10161. module->tags[tag_index - module->import_tag_count]
  10162. ->tag_type;
  10163. }
  10164. if (func_type->result_count != 0) {
  10165. set_error_buf(error_buf, error_buf_size,
  10166. "tag type signature does not return void");
  10167. goto fail;
  10168. }
  10169. /* check validity of current label (expect LABEL_TYPE_TRY or
  10170. * LABEL_TYPE_CATCH) */
  10171. if ((LABEL_TYPE_CATCH != label_type)
  10172. && (LABEL_TYPE_TRY != label_type)) {
  10173. set_error_buf(error_buf, error_buf_size,
  10174. "Unexpected block sequence encountered.");
  10175. goto fail;
  10176. }
  10177. BlockType new_block_type;
  10178. new_block_type.is_value_type = false;
  10179. new_block_type.u.type = func_type;
  10180. /*
  10181. * replace frame_csp by LABEL_TYPE_CATCH
  10182. */
  10183. cur_block->label_type = LABEL_TYPE_CATCH;
  10184. /* RESET_STACK removes the values pushed in TRY or pervious
  10185. * CATCH Blocks */
  10186. RESET_STACK();
  10187. /* push types on the stack according to caught type */
  10188. if (BLOCK_HAS_PARAM(new_block_type)) {
  10189. for (i = 0; i < new_block_type.u.type->param_count; i++)
  10190. PUSH_TYPE(new_block_type.u.type->types[i]);
  10191. }
  10192. break;
  10193. }
  10194. case WASM_OP_CATCH_ALL:
  10195. {
  10196. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10197. /* expecting a TRY or CATCH, anything else will be considered an
  10198. * error */
  10199. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10200. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10201. set_error_buf(error_buf, error_buf_size,
  10202. "Unexpected block sequence encountered.");
  10203. goto fail;
  10204. }
  10205. /* no immediates */
  10206. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10207. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10208. /* RESET_STACK removes the values pushed in TRY or pervious
  10209. * CATCH Blocks */
  10210. RESET_STACK();
  10211. /* catch_all has no tagtype and therefore no parameters */
  10212. break;
  10213. }
  10214. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10215. case WASM_OP_ELSE:
  10216. handle_op_else:
  10217. {
  10218. BranchBlock *block = NULL;
  10219. BlockType block_type;
  10220. if (loader_ctx->csp_num < 2
  10221. /* the matched if isn't found */
  10222. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10223. /* duplicated else is found */
  10224. || (loader_ctx->frame_csp - 1)->else_addr) {
  10225. set_error_buf(
  10226. error_buf, error_buf_size,
  10227. "opcode else found without matched opcode if");
  10228. goto fail;
  10229. }
  10230. block = loader_ctx->frame_csp - 1;
  10231. /* check whether if branch's stack matches its result type */
  10232. if (!check_block_stack(loader_ctx, block, error_buf,
  10233. error_buf_size))
  10234. goto fail;
  10235. block->else_addr = p - 1;
  10236. block_type = block->block_type;
  10237. #if WASM_ENABLE_GC != 0
  10238. if (!wasm_loader_init_local_use_masks(
  10239. loader_ctx, local_count, error_buf, error_buf_size)) {
  10240. goto fail;
  10241. }
  10242. #endif
  10243. #if WASM_ENABLE_FAST_INTERP != 0
  10244. /* if the result of if branch is in local or const area, add a
  10245. * copy op */
  10246. RESERVE_BLOCK_RET();
  10247. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10248. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10249. #endif
  10250. RESET_STACK();
  10251. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10252. /* Pass parameters to if-false branch */
  10253. if (BLOCK_HAS_PARAM(block_type)) {
  10254. for (i = 0; i < block_type.u.type->param_count; i++)
  10255. PUSH_TYPE(block_type.u.type->types[i]);
  10256. }
  10257. #if WASM_ENABLE_FAST_INTERP != 0
  10258. /* Recover top param_count values of frame_offset stack */
  10259. if (BLOCK_HAS_PARAM((block_type))) {
  10260. uint32 size;
  10261. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10262. bh_memcpy_s(loader_ctx->frame_offset, size,
  10263. block->param_frame_offsets, size);
  10264. loader_ctx->frame_offset += (size / sizeof(int16));
  10265. }
  10266. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10267. #endif
  10268. break;
  10269. }
  10270. case WASM_OP_END:
  10271. {
  10272. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10273. /* check whether block stack matches its result type */
  10274. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10275. error_buf_size))
  10276. goto fail;
  10277. /* if there is no else branch, make a virtual else opcode for
  10278. easier integrity check and to copy the correct results to
  10279. the block return address for fast-interp mode:
  10280. change if block from `if ... end` to `if ... else end` */
  10281. if (cur_block->label_type == LABEL_TYPE_IF
  10282. && !cur_block->else_addr) {
  10283. opcode = WASM_OP_ELSE;
  10284. p--;
  10285. #if WASM_ENABLE_FAST_INTERP != 0
  10286. p_org = p;
  10287. skip_label();
  10288. disable_emit = false;
  10289. emit_label(opcode);
  10290. #endif
  10291. goto handle_op_else;
  10292. }
  10293. POP_CSP();
  10294. #if WASM_ENABLE_FAST_INTERP != 0
  10295. skip_label();
  10296. /* copy the result to the block return address */
  10297. RESERVE_BLOCK_RET();
  10298. apply_label_patch(loader_ctx, 0, PATCH_END);
  10299. free_label_patch_list(loader_ctx->frame_csp);
  10300. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10301. int32 idx;
  10302. uint8 ret_type;
  10303. emit_label(WASM_OP_RETURN);
  10304. for (idx = (int32)func->func_type->result_count - 1;
  10305. idx >= 0; idx--) {
  10306. ret_type = *(func->func_type->types
  10307. + func->func_type->param_count + idx);
  10308. POP_OFFSET_TYPE(ret_type);
  10309. }
  10310. }
  10311. #endif
  10312. if (loader_ctx->csp_num > 0) {
  10313. loader_ctx->frame_csp->end_addr = p - 1;
  10314. }
  10315. else {
  10316. /* end of function block, function will return */
  10317. if (p < p_end) {
  10318. set_error_buf(error_buf, error_buf_size,
  10319. "section size mismatch");
  10320. goto fail;
  10321. }
  10322. }
  10323. break;
  10324. }
  10325. case WASM_OP_BR:
  10326. {
  10327. if (!(frame_csp_tmp =
  10328. check_branch_block(loader_ctx, &p, p_end, opcode,
  10329. error_buf, error_buf_size)))
  10330. goto fail;
  10331. RESET_STACK();
  10332. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10333. break;
  10334. }
  10335. case WASM_OP_BR_IF:
  10336. {
  10337. POP_I32();
  10338. if (!(frame_csp_tmp =
  10339. check_branch_block(loader_ctx, &p, p_end, opcode,
  10340. error_buf, error_buf_size)))
  10341. goto fail;
  10342. break;
  10343. }
  10344. case WASM_OP_BR_TABLE:
  10345. {
  10346. uint32 depth = 0, default_arity, arity = 0;
  10347. BranchBlock *target_block;
  10348. BlockType *target_block_type;
  10349. #if WASM_ENABLE_FAST_INTERP == 0
  10350. BrTableCache *br_table_cache = NULL;
  10351. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10352. uint32 j;
  10353. #endif
  10354. read_leb_uint32(p, p_end, count);
  10355. #if WASM_ENABLE_FAST_INTERP != 0
  10356. emit_uint32(loader_ctx, count);
  10357. #endif
  10358. POP_I32();
  10359. /* Get each depth and check it */
  10360. p_org = p;
  10361. for (i = 0; i <= count; i++) {
  10362. read_leb_uint32(p, p_end, depth);
  10363. bh_assert(loader_ctx->csp_num > 0);
  10364. if (loader_ctx->csp_num - 1 < depth) {
  10365. set_error_buf(error_buf, error_buf_size,
  10366. "unknown label, "
  10367. "unexpected end of section or function");
  10368. goto fail;
  10369. }
  10370. }
  10371. p = p_org;
  10372. /* Get the default block's arity */
  10373. target_block = loader_ctx->frame_csp - (depth + 1);
  10374. target_block_type = &target_block->block_type;
  10375. default_arity = block_type_get_arity(target_block_type,
  10376. target_block->label_type);
  10377. #if WASM_ENABLE_FAST_INTERP == 0
  10378. p_depth_begin = p_depth = p;
  10379. #endif
  10380. for (i = 0; i <= count; i++) {
  10381. p_org = p;
  10382. read_leb_uint32(p, p_end, depth);
  10383. p = p_org;
  10384. /* Get the target block's arity and check it */
  10385. target_block = loader_ctx->frame_csp - (depth + 1);
  10386. target_block_type = &target_block->block_type;
  10387. arity = block_type_get_arity(target_block_type,
  10388. target_block->label_type);
  10389. if (arity != default_arity) {
  10390. set_error_buf(error_buf, error_buf_size,
  10391. "type mismatch: br_table targets must "
  10392. "all use same result type");
  10393. goto fail;
  10394. }
  10395. if (!(frame_csp_tmp =
  10396. check_branch_block(loader_ctx, &p, p_end, opcode,
  10397. error_buf, error_buf_size))) {
  10398. goto fail;
  10399. }
  10400. #if WASM_ENABLE_FAST_INTERP == 0
  10401. if (br_table_cache) {
  10402. br_table_cache->br_depths[i] = depth;
  10403. }
  10404. else {
  10405. if (depth > 255) {
  10406. /* The depth cannot be stored in one byte,
  10407. create br_table cache to store each depth */
  10408. #if WASM_ENABLE_DEBUG_INTERP != 0
  10409. if (!record_fast_op(module, p_opcode, *p_opcode,
  10410. error_buf, error_buf_size)) {
  10411. goto fail;
  10412. }
  10413. #endif
  10414. if (!(br_table_cache = loader_malloc(
  10415. offsetof(BrTableCache, br_depths)
  10416. + sizeof(uint32)
  10417. * (uint64)(count + 1),
  10418. error_buf, error_buf_size))) {
  10419. goto fail;
  10420. }
  10421. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10422. br_table_cache->br_table_op_addr = p_opcode;
  10423. br_table_cache->br_count = count;
  10424. /* Copy previous depths which are one byte */
  10425. for (j = 0; j < i; j++) {
  10426. br_table_cache->br_depths[j] = p_depth_begin[j];
  10427. }
  10428. br_table_cache->br_depths[i] = depth;
  10429. bh_list_insert(module->br_table_cache_list,
  10430. br_table_cache);
  10431. }
  10432. else {
  10433. /* The depth can be stored in one byte, use the
  10434. byte of the leb to store it */
  10435. *p_depth++ = (uint8)depth;
  10436. }
  10437. }
  10438. #endif
  10439. }
  10440. #if WASM_ENABLE_FAST_INTERP == 0
  10441. /* Set the tailing bytes to nop */
  10442. if (br_table_cache)
  10443. p_depth = p_depth_begin;
  10444. while (p_depth < p)
  10445. *p_depth++ = WASM_OP_NOP;
  10446. #endif
  10447. RESET_STACK();
  10448. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10449. break;
  10450. }
  10451. case WASM_OP_RETURN:
  10452. {
  10453. int32 idx;
  10454. uint8 ret_type;
  10455. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  10456. idx--) {
  10457. ret_type = *(func->func_type->types
  10458. + func->func_type->param_count + idx);
  10459. #if WASM_ENABLE_FAST_INTERP != 0
  10460. /* emit the offset after return opcode */
  10461. POP_OFFSET_TYPE(ret_type);
  10462. #endif
  10463. POP_TYPE(ret_type);
  10464. }
  10465. RESET_STACK();
  10466. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10467. break;
  10468. }
  10469. case WASM_OP_CALL:
  10470. #if WASM_ENABLE_TAIL_CALL != 0
  10471. case WASM_OP_RETURN_CALL:
  10472. #endif
  10473. #if WASM_ENABLE_GC != 0
  10474. case WASM_OP_CALL_REF:
  10475. case WASM_OP_RETURN_CALL_REF:
  10476. #endif
  10477. {
  10478. WASMFuncType *func_type;
  10479. uint8 type;
  10480. int32 idx;
  10481. #if WASM_ENABLE_GC != 0
  10482. WASMRefType *ref_type;
  10483. uint32 type_idx1;
  10484. int32 j;
  10485. #endif
  10486. #if WASM_ENABLE_GC != 0
  10487. if (opcode == WASM_OP_CALL_REF
  10488. || opcode == WASM_OP_RETURN_CALL_REF) {
  10489. read_leb_uint32(p, p_end, type_idx1);
  10490. if (!check_type_index(module, module->type_count, type_idx1,
  10491. error_buf, error_buf_size)) {
  10492. goto fail;
  10493. }
  10494. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10495. set_error_buf(error_buf, error_buf_size,
  10496. "unkown function type");
  10497. goto fail;
  10498. }
  10499. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10500. &type_idx, error_buf,
  10501. error_buf_size)) {
  10502. goto fail;
  10503. }
  10504. if (type == VALUE_TYPE_ANY) {
  10505. type_idx = type_idx1;
  10506. }
  10507. if (!check_type_index(module, module->type_count, type_idx,
  10508. error_buf, error_buf_size)) {
  10509. goto fail;
  10510. }
  10511. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10512. set_error_buf(error_buf, error_buf_size,
  10513. "unkown function type");
  10514. goto fail;
  10515. }
  10516. if (!wasm_func_type_is_super_of(
  10517. (WASMFuncType *)module->types[type_idx1],
  10518. (WASMFuncType *)module->types[type_idx])) {
  10519. set_error_buf(error_buf, error_buf_size,
  10520. "function type mismatch");
  10521. goto fail;
  10522. }
  10523. func_type = (WASMFuncType *)module->types[type_idx];
  10524. }
  10525. else
  10526. #endif
  10527. {
  10528. read_leb_uint32(p, p_end, func_idx);
  10529. #if WASM_ENABLE_FAST_INTERP != 0
  10530. /* we need to emit func_idx before arguments */
  10531. emit_uint32(loader_ctx, func_idx);
  10532. #endif
  10533. if (!check_function_index(module, func_idx, error_buf,
  10534. error_buf_size)) {
  10535. goto fail;
  10536. }
  10537. if (func_idx < module->import_function_count)
  10538. func_type = module->import_functions[func_idx]
  10539. .u.function.func_type;
  10540. else
  10541. func_type =
  10542. module
  10543. ->functions[func_idx
  10544. - module->import_function_count]
  10545. ->func_type;
  10546. }
  10547. if (func_type->param_count > 0) {
  10548. #if WASM_ENABLE_GC != 0
  10549. j = (int32)(func_type->result_ref_type_maps
  10550. - func_type->ref_type_maps - 1);
  10551. #endif
  10552. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10553. idx--) {
  10554. #if WASM_ENABLE_GC != 0
  10555. if (wasm_is_type_multi_byte_type(
  10556. func_type->types[idx])) {
  10557. ref_type = func_type->ref_type_maps[j].ref_type;
  10558. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10559. ref_type,
  10560. wasm_reftype_struct_size(ref_type));
  10561. j--;
  10562. }
  10563. #endif
  10564. #if WASM_ENABLE_FAST_INTERP != 0
  10565. POP_OFFSET_TYPE(func_type->types[idx]);
  10566. #endif
  10567. POP_TYPE(func_type->types[idx]);
  10568. }
  10569. }
  10570. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10571. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10572. #endif
  10573. #if WASM_ENABLE_GC != 0
  10574. j = (int32)(func_type->result_ref_type_maps
  10575. - func_type->ref_type_maps);
  10576. #endif
  10577. for (i = 0; i < func_type->result_count; i++) {
  10578. #if WASM_ENABLE_GC != 0
  10579. if (wasm_is_type_multi_byte_type(
  10580. func_type->types[func_type->param_count + i])) {
  10581. ref_type = func_type->ref_type_maps[j].ref_type;
  10582. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10583. ref_type,
  10584. wasm_reftype_struct_size(ref_type));
  10585. j++;
  10586. }
  10587. #endif
  10588. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10589. #if WASM_ENABLE_FAST_INTERP != 0
  10590. /* Here we emit each return value's dynamic_offset. But
  10591. * in fact these offsets are continuous, so interpreter
  10592. * only need to get the first return value's offset.
  10593. */
  10594. PUSH_OFFSET_TYPE(
  10595. func_type->types[func_type->param_count + i]);
  10596. #endif
  10597. }
  10598. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10599. }
  10600. else {
  10601. #if WASM_ENABLE_GC == 0
  10602. if (func_type->result_count
  10603. != func->func_type->result_count) {
  10604. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10605. "type mismatch: expect ",
  10606. func->func_type->result_count,
  10607. " return values but got other");
  10608. goto fail;
  10609. }
  10610. for (i = 0; i < func_type->result_count; i++) {
  10611. type = func->func_type
  10612. ->types[func->func_type->param_count + i];
  10613. if (func_type->types[func_type->param_count + i]
  10614. != type) {
  10615. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10616. "type mismatch: expect ",
  10617. type2str(type), " but got other");
  10618. goto fail;
  10619. }
  10620. }
  10621. #else
  10622. if (!wasm_func_type_result_is_subtype_of(
  10623. func_type, func->func_type, module->types,
  10624. module->type_count)) {
  10625. set_error_buf(
  10626. error_buf, error_buf_size,
  10627. "type mismatch: invalid func result types");
  10628. goto fail;
  10629. }
  10630. #endif
  10631. RESET_STACK();
  10632. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10633. }
  10634. #endif
  10635. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10636. || WASM_ENABLE_WAMR_COMPILER != 0
  10637. func->has_op_func_call = true;
  10638. #endif
  10639. (void)type;
  10640. break;
  10641. }
  10642. /*
  10643. * if disable reference type: call_indirect typeidx, 0x00
  10644. * if enable reference type: call_indirect typeidx, tableidx
  10645. */
  10646. case WASM_OP_CALL_INDIRECT:
  10647. #if WASM_ENABLE_TAIL_CALL != 0
  10648. case WASM_OP_RETURN_CALL_INDIRECT:
  10649. #endif
  10650. {
  10651. int32 idx;
  10652. WASMFuncType *func_type;
  10653. read_leb_uint32(p, p_end, type_idx);
  10654. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10655. read_leb_uint32(p, p_end, table_idx);
  10656. #else
  10657. CHECK_BUF(p, p_end, 1);
  10658. table_idx = read_uint8(p);
  10659. #endif
  10660. if (!check_table_index(module, table_idx, error_buf,
  10661. error_buf_size)) {
  10662. goto fail;
  10663. }
  10664. #if WASM_ENABLE_FAST_INTERP != 0
  10665. /* we need to emit before arguments */
  10666. #if WASM_ENABLE_TAIL_CALL != 0
  10667. emit_byte(loader_ctx, opcode);
  10668. #endif
  10669. emit_uint32(loader_ctx, type_idx);
  10670. emit_uint32(loader_ctx, table_idx);
  10671. #endif
  10672. /* skip elem idx */
  10673. POP_I32();
  10674. if (type_idx >= module->type_count) {
  10675. set_error_buf(error_buf, error_buf_size, "unknown type");
  10676. goto fail;
  10677. }
  10678. func_type = (WASMFuncType *)module->types[type_idx];
  10679. if (func_type->param_count > 0) {
  10680. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10681. idx--) {
  10682. #if WASM_ENABLE_FAST_INTERP != 0
  10683. POP_OFFSET_TYPE(func_type->types[idx]);
  10684. #endif
  10685. POP_TYPE(func_type->types[idx]);
  10686. }
  10687. }
  10688. #if WASM_ENABLE_TAIL_CALL != 0
  10689. if (opcode == WASM_OP_CALL_INDIRECT) {
  10690. #endif
  10691. for (i = 0; i < func_type->result_count; i++) {
  10692. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10693. #if WASM_ENABLE_FAST_INTERP != 0
  10694. PUSH_OFFSET_TYPE(
  10695. func_type->types[func_type->param_count + i]);
  10696. #endif
  10697. }
  10698. #if WASM_ENABLE_TAIL_CALL != 0
  10699. }
  10700. else {
  10701. uint8 type;
  10702. if (func_type->result_count
  10703. != func->func_type->result_count) {
  10704. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10705. "type mismatch: expect ",
  10706. func->func_type->result_count,
  10707. " return values but got other");
  10708. goto fail;
  10709. }
  10710. for (i = 0; i < func_type->result_count; i++) {
  10711. type = func->func_type
  10712. ->types[func->func_type->param_count + i];
  10713. if (func_type->types[func_type->param_count + i]
  10714. != type) {
  10715. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10716. "type mismatch: expect ",
  10717. type2str(type), " but got other");
  10718. goto fail;
  10719. }
  10720. }
  10721. RESET_STACK();
  10722. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10723. }
  10724. #endif
  10725. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10726. || WASM_ENABLE_WAMR_COMPILER != 0
  10727. func->has_op_func_call = true;
  10728. #endif
  10729. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10730. func->has_op_call_indirect = true;
  10731. #endif
  10732. break;
  10733. }
  10734. case WASM_OP_DROP:
  10735. {
  10736. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10737. int32 available_stack_cell =
  10738. (int32)(loader_ctx->stack_cell_num
  10739. - cur_block->stack_cell_num);
  10740. if (available_stack_cell <= 0
  10741. && !cur_block->is_stack_polymorphic) {
  10742. set_error_buf(error_buf, error_buf_size,
  10743. "type mismatch, opcode drop was found "
  10744. "but stack was empty");
  10745. goto fail;
  10746. }
  10747. if (available_stack_cell > 0) {
  10748. #if WASM_ENABLE_GC != 0
  10749. if (wasm_is_type_multi_byte_type(
  10750. *(loader_ctx->frame_ref - 1))) {
  10751. bh_assert((int32)(loader_ctx->reftype_map_num
  10752. - cur_block->reftype_map_num)
  10753. > 0);
  10754. loader_ctx->frame_reftype_map--;
  10755. loader_ctx->reftype_map_num--;
  10756. }
  10757. #endif
  10758. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10759. loader_ctx->frame_ref--;
  10760. loader_ctx->stack_cell_num--;
  10761. #if WASM_ENABLE_FAST_INTERP != 0
  10762. skip_label();
  10763. loader_ctx->frame_offset--;
  10764. if ((*(loader_ctx->frame_offset)
  10765. > loader_ctx->start_dynamic_offset)
  10766. && (*(loader_ctx->frame_offset)
  10767. < loader_ctx->max_dynamic_offset))
  10768. loader_ctx->dynamic_offset--;
  10769. #endif
  10770. }
  10771. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10772. loader_ctx->frame_ref -= 2;
  10773. loader_ctx->stack_cell_num -= 2;
  10774. #if WASM_ENABLE_FAST_INTERP == 0
  10775. *(p - 1) = WASM_OP_DROP_64;
  10776. #endif
  10777. #if WASM_ENABLE_FAST_INTERP != 0
  10778. skip_label();
  10779. loader_ctx->frame_offset -= 2;
  10780. if ((*(loader_ctx->frame_offset)
  10781. > loader_ctx->start_dynamic_offset)
  10782. && (*(loader_ctx->frame_offset)
  10783. < loader_ctx->max_dynamic_offset))
  10784. loader_ctx->dynamic_offset -= 2;
  10785. #endif
  10786. }
  10787. #if WASM_ENABLE_SIMD != 0
  10788. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10789. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10790. loader_ctx->frame_ref -= 4;
  10791. loader_ctx->stack_cell_num -= 4;
  10792. }
  10793. #endif
  10794. #endif
  10795. else {
  10796. set_error_buf(error_buf, error_buf_size,
  10797. "type mismatch");
  10798. goto fail;
  10799. }
  10800. }
  10801. else {
  10802. #if WASM_ENABLE_FAST_INTERP != 0
  10803. skip_label();
  10804. #endif
  10805. }
  10806. break;
  10807. }
  10808. case WASM_OP_SELECT:
  10809. {
  10810. uint8 ref_type;
  10811. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10812. int32 available_stack_cell;
  10813. #if WASM_ENABLE_FAST_INTERP != 0
  10814. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10815. #endif
  10816. POP_I32();
  10817. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10818. - cur_block->stack_cell_num);
  10819. if (available_stack_cell <= 0
  10820. && !cur_block->is_stack_polymorphic) {
  10821. set_error_buf(error_buf, error_buf_size,
  10822. "type mismatch or invalid result arity, "
  10823. "opcode select was found "
  10824. "but stack was empty");
  10825. goto fail;
  10826. }
  10827. if (available_stack_cell > 0) {
  10828. switch (*(loader_ctx->frame_ref - 1)) {
  10829. case VALUE_TYPE_I32:
  10830. case VALUE_TYPE_F32:
  10831. case VALUE_TYPE_ANY:
  10832. break;
  10833. case VALUE_TYPE_I64:
  10834. case VALUE_TYPE_F64:
  10835. #if WASM_ENABLE_FAST_INTERP == 0
  10836. *(p - 1) = WASM_OP_SELECT_64;
  10837. #endif
  10838. #if WASM_ENABLE_FAST_INTERP != 0
  10839. if (loader_ctx->p_code_compiled) {
  10840. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10841. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10842. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10843. *(void **)(p_code_compiled_tmp
  10844. - sizeof(void *)) =
  10845. handle_table[opcode_tmp];
  10846. #else
  10847. #if UINTPTR_MAX == UINT64_MAX
  10848. /* emit int32 relative offset in 64-bit target
  10849. */
  10850. int32 offset =
  10851. (int32)((uint8 *)handle_table[opcode_tmp]
  10852. - (uint8 *)handle_table[0]);
  10853. *(int32 *)(p_code_compiled_tmp
  10854. - sizeof(int32)) = offset;
  10855. #else
  10856. /* emit uint32 label address in 32-bit target */
  10857. *(uint32 *)(p_code_compiled_tmp
  10858. - sizeof(uint32)) =
  10859. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10860. #endif
  10861. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10862. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10863. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10864. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10865. #else
  10866. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10867. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10868. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10869. }
  10870. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10871. break;
  10872. #if WASM_ENABLE_SIMD != 0
  10873. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10874. case VALUE_TYPE_V128:
  10875. break;
  10876. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10877. #endif /* WASM_ENABLE_SIMD != 0 */
  10878. default:
  10879. {
  10880. set_error_buf(error_buf, error_buf_size,
  10881. "type mismatch");
  10882. goto fail;
  10883. }
  10884. }
  10885. ref_type = *(loader_ctx->frame_ref - 1);
  10886. #if WASM_ENABLE_FAST_INTERP != 0
  10887. POP_OFFSET_TYPE(ref_type);
  10888. POP_TYPE(ref_type);
  10889. POP_OFFSET_TYPE(ref_type);
  10890. POP_TYPE(ref_type);
  10891. PUSH_OFFSET_TYPE(ref_type);
  10892. PUSH_TYPE(ref_type);
  10893. #else
  10894. POP2_AND_PUSH(ref_type, ref_type);
  10895. #endif
  10896. }
  10897. else {
  10898. #if WASM_ENABLE_FAST_INTERP != 0
  10899. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  10900. #endif
  10901. PUSH_TYPE(VALUE_TYPE_ANY);
  10902. }
  10903. break;
  10904. }
  10905. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10906. case WASM_OP_SELECT_T:
  10907. {
  10908. uint8 vec_len, type;
  10909. #if WASM_ENABLE_GC != 0
  10910. WASMRefType *ref_type = NULL;
  10911. #endif
  10912. #if WASM_ENABLE_FAST_INTERP != 0
  10913. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10914. #endif
  10915. read_leb_uint32(p, p_end, vec_len);
  10916. if (vec_len != 1) {
  10917. /* typed select must have exactly one result */
  10918. set_error_buf(error_buf, error_buf_size,
  10919. "invalid result arity");
  10920. goto fail;
  10921. }
  10922. #if WASM_ENABLE_GC == 0
  10923. CHECK_BUF(p, p_end, 1);
  10924. type = read_uint8(p);
  10925. if (!is_value_type(type)) {
  10926. set_error_buf(error_buf, error_buf_size,
  10927. "unknown value type");
  10928. goto fail;
  10929. }
  10930. #else
  10931. p_org = p + 1;
  10932. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  10933. module->type_count, &need_ref_type_map,
  10934. &wasm_ref_type, false, error_buf,
  10935. error_buf_size)) {
  10936. goto fail;
  10937. }
  10938. type = wasm_ref_type.ref_type;
  10939. if (need_ref_type_map) {
  10940. if (!(ref_type = reftype_set_insert(
  10941. module->ref_type_set, &wasm_ref_type, error_buf,
  10942. error_buf_size))) {
  10943. goto fail;
  10944. }
  10945. }
  10946. #if WASM_ENABLE_FAST_INTERP == 0
  10947. while (p_org < p) {
  10948. #if WASM_ENABLE_DEBUG_INTERP != 0
  10949. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10950. error_buf_size)) {
  10951. goto fail;
  10952. }
  10953. #endif
  10954. /* Ignore extra bytes for interpreter */
  10955. *p_org++ = WASM_OP_NOP;
  10956. }
  10957. #endif
  10958. #endif /* end of WASM_ENABLE_GC == 0 */
  10959. POP_I32();
  10960. #if WASM_ENABLE_FAST_INTERP != 0
  10961. if (loader_ctx->p_code_compiled) {
  10962. uint8 opcode_tmp = WASM_OP_SELECT;
  10963. if (type == VALUE_TYPE_V128) {
  10964. #if (WASM_ENABLE_SIMD == 0) \
  10965. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  10966. set_error_buf(error_buf, error_buf_size,
  10967. "SIMD v128 type isn't supported");
  10968. goto fail;
  10969. #endif
  10970. }
  10971. else {
  10972. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  10973. opcode_tmp = WASM_OP_SELECT_64;
  10974. #if WASM_ENABLE_GC != 0
  10975. if (wasm_is_type_reftype(type))
  10976. opcode_tmp = WASM_OP_SELECT_T;
  10977. #endif
  10978. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10979. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10980. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  10981. handle_table[opcode_tmp];
  10982. #else
  10983. #if UINTPTR_MAX == UINT64_MAX
  10984. /* emit int32 relative offset in 64-bit target */
  10985. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  10986. - (uint8 *)handle_table[0]);
  10987. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  10988. offset;
  10989. #else
  10990. /* emit uint32 label address in 32-bit target */
  10991. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  10992. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10993. #endif
  10994. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10995. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10996. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10997. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10998. #else
  10999. *(p_code_compiled_tmp - 2) = opcode_tmp;
  11000. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  11001. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  11002. }
  11003. }
  11004. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11005. POP_REF(type);
  11006. #if WASM_ENABLE_GC != 0
  11007. if (need_ref_type_map) {
  11008. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11009. wasm_reftype_struct_size(ref_type));
  11010. }
  11011. #endif
  11012. POP_REF(type);
  11013. #if WASM_ENABLE_GC != 0
  11014. if (need_ref_type_map) {
  11015. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11016. wasm_reftype_struct_size(ref_type));
  11017. }
  11018. #endif
  11019. PUSH_REF(type);
  11020. #if WASM_ENABLE_WAMR_COMPILER != 0
  11021. module->is_ref_types_used = true;
  11022. #endif
  11023. (void)vec_len;
  11024. break;
  11025. }
  11026. /* table.get x. tables[x]. [i32] -> [t] */
  11027. /* table.set x. tables[x]. [i32 t] -> [] */
  11028. case WASM_OP_TABLE_GET:
  11029. case WASM_OP_TABLE_SET:
  11030. {
  11031. uint8 decl_ref_type;
  11032. #if WASM_ENABLE_GC != 0
  11033. WASMRefType *ref_type;
  11034. #endif
  11035. read_leb_uint32(p, p_end, table_idx);
  11036. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11037. #if WASM_ENABLE_GC != 0
  11038. (void **)&ref_type,
  11039. #else
  11040. NULL,
  11041. #endif
  11042. error_buf, error_buf_size))
  11043. goto fail;
  11044. #if WASM_ENABLE_GC != 0
  11045. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11046. bh_assert(ref_type);
  11047. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11048. ref_type, wasm_reftype_struct_size(ref_type));
  11049. }
  11050. #endif
  11051. #if WASM_ENABLE_FAST_INTERP != 0
  11052. emit_uint32(loader_ctx, table_idx);
  11053. #endif
  11054. if (opcode == WASM_OP_TABLE_GET) {
  11055. POP_I32();
  11056. #if WASM_ENABLE_FAST_INTERP != 0
  11057. PUSH_OFFSET_TYPE(decl_ref_type);
  11058. #endif
  11059. PUSH_TYPE(decl_ref_type);
  11060. }
  11061. else {
  11062. #if WASM_ENABLE_FAST_INTERP != 0
  11063. POP_OFFSET_TYPE(decl_ref_type);
  11064. #endif
  11065. POP_TYPE(decl_ref_type);
  11066. POP_I32();
  11067. }
  11068. #if WASM_ENABLE_WAMR_COMPILER != 0
  11069. module->is_ref_types_used = true;
  11070. #endif
  11071. break;
  11072. }
  11073. case WASM_OP_REF_NULL:
  11074. {
  11075. uint8 ref_type;
  11076. #if WASM_ENABLE_GC == 0
  11077. CHECK_BUF(p, p_end, 1);
  11078. ref_type = read_uint8(p);
  11079. if (ref_type != VALUE_TYPE_FUNCREF
  11080. && ref_type != VALUE_TYPE_EXTERNREF) {
  11081. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11082. goto fail;
  11083. }
  11084. #else
  11085. read_leb_int32(p, p_end, heap_type);
  11086. if (heap_type >= 0) {
  11087. if (!check_type_index(module, module->type_count, heap_type,
  11088. error_buf, error_buf_size)) {
  11089. goto fail;
  11090. }
  11091. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11092. true, heap_type);
  11093. ref_type = wasm_ref_type.ref_type;
  11094. }
  11095. else {
  11096. if (!wasm_is_valid_heap_type(heap_type)) {
  11097. set_error_buf(error_buf, error_buf_size,
  11098. "unknown type");
  11099. goto fail;
  11100. }
  11101. ref_type = (uint8)((int32)0x80 + heap_type);
  11102. }
  11103. #endif /* end of WASM_ENABLE_GC == 0 */
  11104. #if WASM_ENABLE_FAST_INTERP != 0
  11105. PUSH_OFFSET_TYPE(ref_type);
  11106. #endif
  11107. PUSH_TYPE(ref_type);
  11108. #if WASM_ENABLE_WAMR_COMPILER != 0
  11109. module->is_ref_types_used = true;
  11110. #endif
  11111. break;
  11112. }
  11113. case WASM_OP_REF_IS_NULL:
  11114. {
  11115. #if WASM_ENABLE_GC == 0
  11116. #if WASM_ENABLE_FAST_INTERP != 0
  11117. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11118. int32 block_stack_cell_num =
  11119. (int32)(loader_ctx->stack_cell_num
  11120. - cur_block->stack_cell_num);
  11121. if (block_stack_cell_num <= 0) {
  11122. if (!cur_block->is_stack_polymorphic) {
  11123. set_error_buf(
  11124. error_buf, error_buf_size,
  11125. "type mismatch: expect data but stack was empty");
  11126. goto fail;
  11127. }
  11128. }
  11129. else {
  11130. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11131. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11132. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11133. if (!wasm_loader_pop_frame_ref_offset(
  11134. loader_ctx, *(loader_ctx->frame_ref - 1),
  11135. error_buf, error_buf_size)) {
  11136. goto fail;
  11137. }
  11138. }
  11139. else {
  11140. set_error_buf(error_buf, error_buf_size,
  11141. "type mismatch");
  11142. goto fail;
  11143. }
  11144. }
  11145. #else
  11146. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11147. error_buf, error_buf_size)
  11148. && !wasm_loader_pop_frame_ref(loader_ctx,
  11149. VALUE_TYPE_EXTERNREF,
  11150. error_buf, error_buf_size)) {
  11151. goto fail;
  11152. }
  11153. #endif
  11154. #else /* else of WASM_ENABLE_GC == 0 */
  11155. uint8 type;
  11156. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11157. error_buf, error_buf_size)) {
  11158. goto fail;
  11159. }
  11160. #endif
  11161. PUSH_I32();
  11162. #if WASM_ENABLE_WAMR_COMPILER != 0
  11163. module->is_ref_types_used = true;
  11164. #endif
  11165. break;
  11166. }
  11167. case WASM_OP_REF_FUNC:
  11168. {
  11169. read_leb_uint32(p, p_end, func_idx);
  11170. if (!check_function_index(module, func_idx, error_buf,
  11171. error_buf_size)) {
  11172. goto fail;
  11173. }
  11174. /* Refer to a forward-declared function:
  11175. the function must be an import, exported, or present in
  11176. a table elem segment or global initializer to be used as
  11177. the operand to ref.func */
  11178. if (func_idx >= module->import_function_count) {
  11179. WASMTableSeg *table_seg = module->table_segments;
  11180. bool func_declared = false;
  11181. uint32 j;
  11182. for (i = 0; i < module->global_count; i++) {
  11183. if (module->globals[i].type.val_type
  11184. == VALUE_TYPE_FUNCREF
  11185. && module->globals[i].init_expr.init_expr_type
  11186. == INIT_EXPR_TYPE_FUNCREF_CONST
  11187. && module->globals[i].init_expr.u.u32 == func_idx) {
  11188. func_declared = true;
  11189. break;
  11190. }
  11191. }
  11192. if (!func_declared) {
  11193. /* Check whether the function is declared in table segs,
  11194. note that it doesn't matter whether the table seg's
  11195. mode is passive, active or declarative. */
  11196. for (i = 0; i < module->table_seg_count;
  11197. i++, table_seg++) {
  11198. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11199. #if WASM_ENABLE_GC != 0
  11200. /* elem type is (ref null? func) or
  11201. (ref null? $t) */
  11202. || ((table_seg->elem_type
  11203. == REF_TYPE_HT_NON_NULLABLE
  11204. || table_seg->elem_type
  11205. == REF_TYPE_HT_NULLABLE)
  11206. && (table_seg->elem_ref_type->ref_ht_common
  11207. .heap_type
  11208. == HEAP_TYPE_FUNC
  11209. || table_seg->elem_ref_type
  11210. ->ref_ht_common.heap_type
  11211. > 0))
  11212. #endif
  11213. ) {
  11214. for (j = 0; j < table_seg->value_count; j++) {
  11215. if (table_seg->init_values[j].u.ref_index
  11216. == func_idx) {
  11217. func_declared = true;
  11218. break;
  11219. }
  11220. }
  11221. }
  11222. }
  11223. }
  11224. if (!func_declared) {
  11225. /* Check whether the function is exported */
  11226. for (i = 0; i < module->export_count; i++) {
  11227. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11228. && module->exports[i].index == func_idx) {
  11229. func_declared = true;
  11230. break;
  11231. }
  11232. }
  11233. }
  11234. if (!func_declared) {
  11235. set_error_buf(error_buf, error_buf_size,
  11236. "undeclared function reference");
  11237. goto fail;
  11238. }
  11239. }
  11240. #if WASM_ENABLE_FAST_INTERP != 0
  11241. emit_uint32(loader_ctx, func_idx);
  11242. #endif
  11243. #if WASM_ENABLE_GC == 0
  11244. PUSH_FUNCREF();
  11245. #else
  11246. if (func_idx < module->import_function_count)
  11247. type_idx =
  11248. module->import_functions[func_idx].u.function.type_idx;
  11249. else
  11250. type_idx = module
  11251. ->functions[func_idx
  11252. - module->import_function_count]
  11253. ->type_idx;
  11254. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11255. false, type_idx);
  11256. PUSH_REF(wasm_ref_type.ref_type);
  11257. #endif
  11258. #if WASM_ENABLE_WAMR_COMPILER != 0
  11259. module->is_ref_types_used = true;
  11260. #endif
  11261. break;
  11262. }
  11263. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11264. #if WASM_ENABLE_GC != 0
  11265. case WASM_OP_REF_AS_NON_NULL:
  11266. case WASM_OP_BR_ON_NULL:
  11267. {
  11268. uint8 type;
  11269. WASMRefType ref_type;
  11270. /* POP (ref null ht) and get the converted (ref ht) */
  11271. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11272. error_buf, error_buf_size)) {
  11273. goto fail;
  11274. }
  11275. if (opcode == WASM_OP_BR_ON_NULL) {
  11276. if (!(frame_csp_tmp =
  11277. check_branch_block(loader_ctx, &p, p_end, opcode,
  11278. error_buf, error_buf_size))) {
  11279. goto fail;
  11280. }
  11281. #if WASM_ENABLE_FAST_INTERP != 0
  11282. disable_emit = true;
  11283. #endif
  11284. }
  11285. /* PUSH the converted (ref ht) */
  11286. if (type != VALUE_TYPE_ANY) {
  11287. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11288. sizeof(WASMRefType));
  11289. }
  11290. PUSH_REF(type);
  11291. break;
  11292. }
  11293. case WASM_OP_BR_ON_NON_NULL:
  11294. {
  11295. uint8 type;
  11296. WASMRefType ref_type;
  11297. uint32 available_stack_cell =
  11298. loader_ctx->stack_cell_num
  11299. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11300. /* POP (ref null ht) and get the converted (ref ht) */
  11301. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11302. error_buf, error_buf_size)) {
  11303. goto fail;
  11304. }
  11305. #if WASM_ENABLE_FAST_INTERP != 0
  11306. disable_emit = true;
  11307. #endif
  11308. /* Temporarily PUSH back (ref ht), check brach block and
  11309. then POP it */
  11310. if (available_stack_cell
  11311. > 0) { /* stack isn't in polymorphic state */
  11312. if (type != VALUE_TYPE_ANY) {
  11313. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11314. &ref_type, sizeof(WASMRefType));
  11315. }
  11316. PUSH_REF(type);
  11317. }
  11318. if (!(frame_csp_tmp =
  11319. check_branch_block(loader_ctx, &p, p_end, opcode,
  11320. error_buf, error_buf_size))) {
  11321. goto fail;
  11322. }
  11323. if (available_stack_cell
  11324. > 0) { /* stack isn't in polymorphic state */
  11325. POP_REF(type);
  11326. #if WASM_ENABLE_FAST_INTERP != 0
  11327. /* Erase the opnd offset emitted by POP_REF() */
  11328. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11329. #endif
  11330. }
  11331. break;
  11332. }
  11333. case WASM_OP_REF_EQ:
  11334. POP_REF(REF_TYPE_EQREF);
  11335. POP_REF(REF_TYPE_EQREF);
  11336. PUSH_I32();
  11337. break;
  11338. #endif /* end of WASM_ENABLE_GC != 0 */
  11339. case WASM_OP_GET_LOCAL:
  11340. {
  11341. p_org = p - 1;
  11342. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11343. PUSH_TYPE(local_type);
  11344. #if WASM_ENABLE_GC != 0
  11345. /* Cannot get a non-nullable and unset local */
  11346. if (local_idx >= param_count
  11347. && wasm_is_reftype_htref_non_nullable(local_type)
  11348. && !wasm_loader_get_local_status(loader_ctx,
  11349. local_idx - param_count)) {
  11350. set_error_buf(error_buf, error_buf_size,
  11351. "uninitialized local");
  11352. goto fail;
  11353. }
  11354. #endif
  11355. #if WASM_ENABLE_FAST_INTERP != 0
  11356. /* Get Local is optimized out */
  11357. skip_label();
  11358. disable_emit = true;
  11359. operand_offset = local_offset;
  11360. PUSH_OFFSET_TYPE(local_type);
  11361. #else
  11362. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11363. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11364. if (local_offset < 0x80
  11365. #if WASM_ENABLE_GC != 0
  11366. && !wasm_is_type_reftype(local_type)
  11367. #endif
  11368. ) {
  11369. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11370. if (is_32bit_type(local_type)) {
  11371. *p_org++ = (uint8)local_offset;
  11372. }
  11373. else {
  11374. *p_org++ = (uint8)(local_offset | 0x80);
  11375. }
  11376. while (p_org < p) {
  11377. *p_org++ = WASM_OP_NOP;
  11378. }
  11379. }
  11380. #endif
  11381. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11382. break;
  11383. }
  11384. case WASM_OP_SET_LOCAL:
  11385. {
  11386. p_org = p - 1;
  11387. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11388. #if WASM_ENABLE_FAST_INTERP != 0
  11389. if (!(preserve_referenced_local(
  11390. loader_ctx, opcode, local_offset, local_type,
  11391. &preserve_local, error_buf, error_buf_size)))
  11392. goto fail;
  11393. if (local_offset < 256
  11394. #if WASM_ENABLE_GC != 0
  11395. && !wasm_is_type_reftype(local_type)
  11396. #endif
  11397. ) {
  11398. skip_label();
  11399. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11400. if (loader_ctx->p_code_compiled)
  11401. STORE_U16(loader_ctx->p_code_compiled - 2,
  11402. local_offset);
  11403. loader_ctx->frame_offset--;
  11404. loader_ctx->dynamic_offset--;
  11405. }
  11406. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11407. if (loader_ctx->p_code_compiled)
  11408. STORE_U16(loader_ctx->p_code_compiled - 2,
  11409. local_offset);
  11410. loader_ctx->frame_offset -= 2;
  11411. loader_ctx->dynamic_offset -= 2;
  11412. }
  11413. else {
  11414. if (is_32bit_type(local_type)) {
  11415. emit_label(EXT_OP_SET_LOCAL_FAST);
  11416. emit_byte(loader_ctx, (uint8)local_offset);
  11417. }
  11418. else {
  11419. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11420. emit_byte(loader_ctx, (uint8)local_offset);
  11421. }
  11422. POP_OFFSET_TYPE(local_type);
  11423. }
  11424. }
  11425. else { /* local index larger than 255, reserve leb */
  11426. emit_uint32(loader_ctx, local_idx);
  11427. POP_OFFSET_TYPE(local_type);
  11428. }
  11429. #else
  11430. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11431. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11432. if (local_offset < 0x80
  11433. #if WASM_ENABLE_GC != 0
  11434. && !wasm_is_type_reftype(local_type)
  11435. #endif
  11436. ) {
  11437. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11438. if (is_32bit_type(local_type)) {
  11439. *p_org++ = (uint8)local_offset;
  11440. }
  11441. else {
  11442. *p_org++ = (uint8)(local_offset | 0x80);
  11443. }
  11444. while (p_org < p) {
  11445. *p_org++ = WASM_OP_NOP;
  11446. }
  11447. }
  11448. #endif
  11449. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11450. #if WASM_ENABLE_GC != 0
  11451. if (local_idx >= param_count) {
  11452. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11453. }
  11454. #endif
  11455. POP_TYPE(local_type);
  11456. break;
  11457. }
  11458. case WASM_OP_TEE_LOCAL:
  11459. {
  11460. p_org = p - 1;
  11461. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11462. #if WASM_ENABLE_FAST_INTERP != 0
  11463. /* If the stack is in polymorphic state, do fake pop and push on
  11464. offset stack to keep the depth of offset stack to be the
  11465. same with ref stack */
  11466. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11467. if (cur_block->is_stack_polymorphic) {
  11468. POP_OFFSET_TYPE(local_type);
  11469. PUSH_OFFSET_TYPE(local_type);
  11470. }
  11471. #endif
  11472. POP_TYPE(local_type);
  11473. PUSH_TYPE(local_type);
  11474. #if WASM_ENABLE_FAST_INTERP != 0
  11475. if (!(preserve_referenced_local(
  11476. loader_ctx, opcode, local_offset, local_type,
  11477. &preserve_local, error_buf, error_buf_size)))
  11478. goto fail;
  11479. if (local_offset < 256
  11480. #if WASM_ENABLE_GC != 0
  11481. && !wasm_is_type_reftype(local_type)
  11482. #endif
  11483. ) {
  11484. skip_label();
  11485. if (is_32bit_type(local_type)) {
  11486. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11487. emit_byte(loader_ctx, (uint8)local_offset);
  11488. }
  11489. else {
  11490. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11491. emit_byte(loader_ctx, (uint8)local_offset);
  11492. }
  11493. }
  11494. else { /* local index larger than 255, reserve leb */
  11495. emit_uint32(loader_ctx, local_idx);
  11496. }
  11497. emit_operand(loader_ctx,
  11498. *(loader_ctx->frame_offset
  11499. - wasm_value_type_cell_num(local_type)));
  11500. #else
  11501. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11502. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11503. if (local_offset < 0x80
  11504. #if WASM_ENABLE_GC != 0
  11505. && !wasm_is_type_reftype(local_type)
  11506. #endif
  11507. ) {
  11508. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11509. if (is_32bit_type(local_type)) {
  11510. *p_org++ = (uint8)local_offset;
  11511. }
  11512. else {
  11513. *p_org++ = (uint8)(local_offset | 0x80);
  11514. }
  11515. while (p_org < p) {
  11516. *p_org++ = WASM_OP_NOP;
  11517. }
  11518. }
  11519. #endif
  11520. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11521. #if WASM_ENABLE_GC != 0
  11522. if (local_idx >= param_count) {
  11523. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11524. }
  11525. #endif
  11526. break;
  11527. }
  11528. case WASM_OP_GET_GLOBAL:
  11529. {
  11530. #if WASM_ENABLE_GC != 0
  11531. WASMRefType *ref_type;
  11532. #endif
  11533. p_org = p - 1;
  11534. read_leb_uint32(p, p_end, global_idx);
  11535. if (global_idx >= global_count) {
  11536. set_error_buf(error_buf, error_buf_size, "unknown global");
  11537. goto fail;
  11538. }
  11539. global_type = global_idx < module->import_global_count
  11540. ? module->import_globals[global_idx]
  11541. .u.global.type.val_type
  11542. : module
  11543. ->globals[global_idx
  11544. - module->import_global_count]
  11545. .type.val_type;
  11546. #if WASM_ENABLE_GC != 0
  11547. ref_type =
  11548. global_idx < module->import_global_count
  11549. ? module->import_globals[global_idx].u.global.ref_type
  11550. : module
  11551. ->globals[global_idx
  11552. - module->import_global_count]
  11553. .ref_type;
  11554. if (wasm_is_type_multi_byte_type(global_type)) {
  11555. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11556. wasm_reftype_struct_size(ref_type));
  11557. }
  11558. #endif
  11559. PUSH_TYPE(global_type);
  11560. #if WASM_ENABLE_FAST_INTERP == 0
  11561. if (global_type == VALUE_TYPE_I64
  11562. || global_type == VALUE_TYPE_F64) {
  11563. #if WASM_ENABLE_DEBUG_INTERP != 0
  11564. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11565. error_buf_size)) {
  11566. goto fail;
  11567. }
  11568. #endif
  11569. *p_org = WASM_OP_GET_GLOBAL_64;
  11570. }
  11571. #else /* else of WASM_ENABLE_FAST_INTERP */
  11572. if (global_type == VALUE_TYPE_I64
  11573. || global_type == VALUE_TYPE_F64) {
  11574. skip_label();
  11575. emit_label(WASM_OP_GET_GLOBAL_64);
  11576. }
  11577. emit_uint32(loader_ctx, global_idx);
  11578. PUSH_OFFSET_TYPE(global_type);
  11579. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11580. break;
  11581. }
  11582. case WASM_OP_SET_GLOBAL:
  11583. {
  11584. bool is_mutable = false;
  11585. #if WASM_ENABLE_GC != 0
  11586. WASMRefType *ref_type;
  11587. #endif
  11588. p_org = p - 1;
  11589. read_leb_uint32(p, p_end, global_idx);
  11590. if (global_idx >= global_count) {
  11591. set_error_buf(error_buf, error_buf_size, "unknown global");
  11592. goto fail;
  11593. }
  11594. is_mutable = global_idx < module->import_global_count
  11595. ? module->import_globals[global_idx]
  11596. .u.global.type.is_mutable
  11597. : module
  11598. ->globals[global_idx
  11599. - module->import_global_count]
  11600. .type.is_mutable;
  11601. if (!is_mutable) {
  11602. #if WASM_ENABLE_GC == 0
  11603. set_error_buf(error_buf, error_buf_size,
  11604. "global is immutable");
  11605. #else
  11606. set_error_buf(error_buf, error_buf_size,
  11607. "immutable global");
  11608. #endif
  11609. goto fail;
  11610. }
  11611. global_type = global_idx < module->import_global_count
  11612. ? module->import_globals[global_idx]
  11613. .u.global.type.val_type
  11614. : module
  11615. ->globals[global_idx
  11616. - module->import_global_count]
  11617. .type.val_type;
  11618. #if WASM_ENABLE_GC != 0
  11619. ref_type =
  11620. global_idx < module->import_global_count
  11621. ? module->import_globals[global_idx].u.global.ref_type
  11622. : module
  11623. ->globals[global_idx
  11624. - module->import_global_count]
  11625. .ref_type;
  11626. if (wasm_is_type_multi_byte_type(global_type)) {
  11627. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11628. wasm_reftype_struct_size(ref_type));
  11629. }
  11630. #endif
  11631. #if WASM_ENABLE_FAST_INTERP == 0
  11632. if (global_type == VALUE_TYPE_I64
  11633. || global_type == VALUE_TYPE_F64) {
  11634. #if WASM_ENABLE_DEBUG_INTERP != 0
  11635. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11636. error_buf_size)) {
  11637. goto fail;
  11638. }
  11639. #endif
  11640. *p_org = WASM_OP_SET_GLOBAL_64;
  11641. }
  11642. else if (module->aux_stack_size > 0
  11643. && global_idx == module->aux_stack_top_global_index) {
  11644. #if WASM_ENABLE_DEBUG_INTERP != 0
  11645. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11646. error_buf_size)) {
  11647. goto fail;
  11648. }
  11649. #endif
  11650. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11651. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11652. func->has_op_set_global_aux_stack = true;
  11653. #endif
  11654. }
  11655. #else /* else of WASM_ENABLE_FAST_INTERP */
  11656. if (global_type == VALUE_TYPE_I64
  11657. || global_type == VALUE_TYPE_F64) {
  11658. skip_label();
  11659. emit_label(WASM_OP_SET_GLOBAL_64);
  11660. }
  11661. else if (module->aux_stack_size > 0
  11662. && global_idx == module->aux_stack_top_global_index) {
  11663. skip_label();
  11664. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11665. }
  11666. emit_uint32(loader_ctx, global_idx);
  11667. POP_OFFSET_TYPE(global_type);
  11668. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11669. POP_TYPE(global_type);
  11670. break;
  11671. }
  11672. /* load */
  11673. case WASM_OP_I32_LOAD:
  11674. case WASM_OP_I32_LOAD8_S:
  11675. case WASM_OP_I32_LOAD8_U:
  11676. case WASM_OP_I32_LOAD16_S:
  11677. case WASM_OP_I32_LOAD16_U:
  11678. case WASM_OP_I64_LOAD:
  11679. case WASM_OP_I64_LOAD8_S:
  11680. case WASM_OP_I64_LOAD8_U:
  11681. case WASM_OP_I64_LOAD16_S:
  11682. case WASM_OP_I64_LOAD16_U:
  11683. case WASM_OP_I64_LOAD32_S:
  11684. case WASM_OP_I64_LOAD32_U:
  11685. case WASM_OP_F32_LOAD:
  11686. case WASM_OP_F64_LOAD:
  11687. /* store */
  11688. case WASM_OP_I32_STORE:
  11689. case WASM_OP_I32_STORE8:
  11690. case WASM_OP_I32_STORE16:
  11691. case WASM_OP_I64_STORE:
  11692. case WASM_OP_I64_STORE8:
  11693. case WASM_OP_I64_STORE16:
  11694. case WASM_OP_I64_STORE32:
  11695. case WASM_OP_F32_STORE:
  11696. case WASM_OP_F64_STORE:
  11697. {
  11698. #if WASM_ENABLE_FAST_INTERP != 0
  11699. /* change F32/F64 into I32/I64 */
  11700. if (opcode == WASM_OP_F32_LOAD) {
  11701. skip_label();
  11702. emit_label(WASM_OP_I32_LOAD);
  11703. }
  11704. else if (opcode == WASM_OP_F64_LOAD) {
  11705. skip_label();
  11706. emit_label(WASM_OP_I64_LOAD);
  11707. }
  11708. else if (opcode == WASM_OP_F32_STORE) {
  11709. skip_label();
  11710. emit_label(WASM_OP_I32_STORE);
  11711. }
  11712. else if (opcode == WASM_OP_F64_STORE) {
  11713. skip_label();
  11714. emit_label(WASM_OP_I64_STORE);
  11715. }
  11716. #endif
  11717. CHECK_MEMORY();
  11718. read_leb_uint32(p, p_end, align); /* align */
  11719. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11720. if (!check_memory_access_align(opcode, align, error_buf,
  11721. error_buf_size)) {
  11722. goto fail;
  11723. }
  11724. #if WASM_ENABLE_FAST_INTERP != 0
  11725. emit_uint32(loader_ctx, mem_offset);
  11726. #endif
  11727. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11728. func->has_memory_operations = true;
  11729. #endif
  11730. switch (opcode) {
  11731. /* load */
  11732. case WASM_OP_I32_LOAD:
  11733. case WASM_OP_I32_LOAD8_S:
  11734. case WASM_OP_I32_LOAD8_U:
  11735. case WASM_OP_I32_LOAD16_S:
  11736. case WASM_OP_I32_LOAD16_U:
  11737. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11738. break;
  11739. case WASM_OP_I64_LOAD:
  11740. case WASM_OP_I64_LOAD8_S:
  11741. case WASM_OP_I64_LOAD8_U:
  11742. case WASM_OP_I64_LOAD16_S:
  11743. case WASM_OP_I64_LOAD16_U:
  11744. case WASM_OP_I64_LOAD32_S:
  11745. case WASM_OP_I64_LOAD32_U:
  11746. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11747. break;
  11748. case WASM_OP_F32_LOAD:
  11749. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11750. break;
  11751. case WASM_OP_F64_LOAD:
  11752. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11753. break;
  11754. /* store */
  11755. case WASM_OP_I32_STORE:
  11756. case WASM_OP_I32_STORE8:
  11757. case WASM_OP_I32_STORE16:
  11758. POP_I32();
  11759. POP_MEM_OFFSET();
  11760. break;
  11761. case WASM_OP_I64_STORE:
  11762. case WASM_OP_I64_STORE8:
  11763. case WASM_OP_I64_STORE16:
  11764. case WASM_OP_I64_STORE32:
  11765. POP_I64();
  11766. POP_MEM_OFFSET();
  11767. break;
  11768. case WASM_OP_F32_STORE:
  11769. POP_F32();
  11770. POP_MEM_OFFSET();
  11771. break;
  11772. case WASM_OP_F64_STORE:
  11773. POP_F64();
  11774. POP_MEM_OFFSET();
  11775. break;
  11776. default:
  11777. break;
  11778. }
  11779. break;
  11780. }
  11781. case WASM_OP_MEMORY_SIZE:
  11782. CHECK_MEMORY();
  11783. /* reserved byte 0x00 */
  11784. if (*p++ != 0x00) {
  11785. set_error_buf(error_buf, error_buf_size,
  11786. "zero byte expected");
  11787. goto fail;
  11788. }
  11789. PUSH_PAGE_COUNT();
  11790. module->possible_memory_grow = true;
  11791. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11792. func->has_memory_operations = true;
  11793. #endif
  11794. break;
  11795. case WASM_OP_MEMORY_GROW:
  11796. CHECK_MEMORY();
  11797. /* reserved byte 0x00 */
  11798. if (*p++ != 0x00) {
  11799. set_error_buf(error_buf, error_buf_size,
  11800. "zero byte expected");
  11801. goto fail;
  11802. }
  11803. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11804. module->possible_memory_grow = true;
  11805. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11806. || WASM_ENABLE_WAMR_COMPILER != 0
  11807. func->has_op_memory_grow = true;
  11808. #endif
  11809. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11810. func->has_memory_operations = true;
  11811. #endif
  11812. break;
  11813. case WASM_OP_I32_CONST:
  11814. read_leb_int32(p, p_end, i32_const);
  11815. #if WASM_ENABLE_FAST_INTERP != 0
  11816. skip_label();
  11817. disable_emit = true;
  11818. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11819. if (operand_offset == 0) {
  11820. disable_emit = false;
  11821. emit_label(WASM_OP_I32_CONST);
  11822. emit_uint32(loader_ctx, i32_const);
  11823. }
  11824. #else
  11825. (void)i32_const;
  11826. #endif
  11827. PUSH_I32();
  11828. break;
  11829. case WASM_OP_I64_CONST:
  11830. read_leb_int64(p, p_end, i64_const);
  11831. #if WASM_ENABLE_FAST_INTERP != 0
  11832. skip_label();
  11833. disable_emit = true;
  11834. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11835. if (operand_offset == 0) {
  11836. disable_emit = false;
  11837. emit_label(WASM_OP_I64_CONST);
  11838. emit_uint64(loader_ctx, i64_const);
  11839. }
  11840. #endif
  11841. PUSH_I64();
  11842. break;
  11843. case WASM_OP_F32_CONST:
  11844. CHECK_BUF(p, p_end, sizeof(float32));
  11845. p += sizeof(float32);
  11846. #if WASM_ENABLE_FAST_INTERP != 0
  11847. skip_label();
  11848. disable_emit = true;
  11849. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11850. sizeof(float32));
  11851. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11852. if (operand_offset == 0) {
  11853. disable_emit = false;
  11854. emit_label(WASM_OP_F32_CONST);
  11855. emit_float32(loader_ctx, f32_const);
  11856. }
  11857. #endif
  11858. PUSH_F32();
  11859. break;
  11860. case WASM_OP_F64_CONST:
  11861. CHECK_BUF(p, p_end, sizeof(float64));
  11862. p += sizeof(float64);
  11863. #if WASM_ENABLE_FAST_INTERP != 0
  11864. skip_label();
  11865. disable_emit = true;
  11866. /* Some MCU may require 8-byte align */
  11867. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11868. sizeof(float64));
  11869. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11870. if (operand_offset == 0) {
  11871. disable_emit = false;
  11872. emit_label(WASM_OP_F64_CONST);
  11873. emit_float64(loader_ctx, f64_const);
  11874. }
  11875. #endif
  11876. PUSH_F64();
  11877. break;
  11878. case WASM_OP_I32_EQZ:
  11879. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11880. break;
  11881. case WASM_OP_I32_EQ:
  11882. case WASM_OP_I32_NE:
  11883. case WASM_OP_I32_LT_S:
  11884. case WASM_OP_I32_LT_U:
  11885. case WASM_OP_I32_GT_S:
  11886. case WASM_OP_I32_GT_U:
  11887. case WASM_OP_I32_LE_S:
  11888. case WASM_OP_I32_LE_U:
  11889. case WASM_OP_I32_GE_S:
  11890. case WASM_OP_I32_GE_U:
  11891. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11892. break;
  11893. case WASM_OP_I64_EQZ:
  11894. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11895. break;
  11896. case WASM_OP_I64_EQ:
  11897. case WASM_OP_I64_NE:
  11898. case WASM_OP_I64_LT_S:
  11899. case WASM_OP_I64_LT_U:
  11900. case WASM_OP_I64_GT_S:
  11901. case WASM_OP_I64_GT_U:
  11902. case WASM_OP_I64_LE_S:
  11903. case WASM_OP_I64_LE_U:
  11904. case WASM_OP_I64_GE_S:
  11905. case WASM_OP_I64_GE_U:
  11906. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11907. break;
  11908. case WASM_OP_F32_EQ:
  11909. case WASM_OP_F32_NE:
  11910. case WASM_OP_F32_LT:
  11911. case WASM_OP_F32_GT:
  11912. case WASM_OP_F32_LE:
  11913. case WASM_OP_F32_GE:
  11914. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11915. break;
  11916. case WASM_OP_F64_EQ:
  11917. case WASM_OP_F64_NE:
  11918. case WASM_OP_F64_LT:
  11919. case WASM_OP_F64_GT:
  11920. case WASM_OP_F64_LE:
  11921. case WASM_OP_F64_GE:
  11922. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11923. break;
  11924. case WASM_OP_I32_CLZ:
  11925. case WASM_OP_I32_CTZ:
  11926. case WASM_OP_I32_POPCNT:
  11927. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11928. break;
  11929. case WASM_OP_I32_ADD:
  11930. case WASM_OP_I32_SUB:
  11931. case WASM_OP_I32_MUL:
  11932. case WASM_OP_I32_DIV_S:
  11933. case WASM_OP_I32_DIV_U:
  11934. case WASM_OP_I32_REM_S:
  11935. case WASM_OP_I32_REM_U:
  11936. case WASM_OP_I32_AND:
  11937. case WASM_OP_I32_OR:
  11938. case WASM_OP_I32_XOR:
  11939. case WASM_OP_I32_SHL:
  11940. case WASM_OP_I32_SHR_S:
  11941. case WASM_OP_I32_SHR_U:
  11942. case WASM_OP_I32_ROTL:
  11943. case WASM_OP_I32_ROTR:
  11944. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11945. break;
  11946. case WASM_OP_I64_CLZ:
  11947. case WASM_OP_I64_CTZ:
  11948. case WASM_OP_I64_POPCNT:
  11949. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11950. break;
  11951. case WASM_OP_I64_ADD:
  11952. case WASM_OP_I64_SUB:
  11953. case WASM_OP_I64_MUL:
  11954. case WASM_OP_I64_DIV_S:
  11955. case WASM_OP_I64_DIV_U:
  11956. case WASM_OP_I64_REM_S:
  11957. case WASM_OP_I64_REM_U:
  11958. case WASM_OP_I64_AND:
  11959. case WASM_OP_I64_OR:
  11960. case WASM_OP_I64_XOR:
  11961. case WASM_OP_I64_SHL:
  11962. case WASM_OP_I64_SHR_S:
  11963. case WASM_OP_I64_SHR_U:
  11964. case WASM_OP_I64_ROTL:
  11965. case WASM_OP_I64_ROTR:
  11966. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11967. break;
  11968. case WASM_OP_F32_ABS:
  11969. case WASM_OP_F32_NEG:
  11970. case WASM_OP_F32_CEIL:
  11971. case WASM_OP_F32_FLOOR:
  11972. case WASM_OP_F32_TRUNC:
  11973. case WASM_OP_F32_NEAREST:
  11974. case WASM_OP_F32_SQRT:
  11975. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11976. break;
  11977. case WASM_OP_F32_ADD:
  11978. case WASM_OP_F32_SUB:
  11979. case WASM_OP_F32_MUL:
  11980. case WASM_OP_F32_DIV:
  11981. case WASM_OP_F32_MIN:
  11982. case WASM_OP_F32_MAX:
  11983. case WASM_OP_F32_COPYSIGN:
  11984. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11985. break;
  11986. case WASM_OP_F64_ABS:
  11987. case WASM_OP_F64_NEG:
  11988. case WASM_OP_F64_CEIL:
  11989. case WASM_OP_F64_FLOOR:
  11990. case WASM_OP_F64_TRUNC:
  11991. case WASM_OP_F64_NEAREST:
  11992. case WASM_OP_F64_SQRT:
  11993. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11994. break;
  11995. case WASM_OP_F64_ADD:
  11996. case WASM_OP_F64_SUB:
  11997. case WASM_OP_F64_MUL:
  11998. case WASM_OP_F64_DIV:
  11999. case WASM_OP_F64_MIN:
  12000. case WASM_OP_F64_MAX:
  12001. case WASM_OP_F64_COPYSIGN:
  12002. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  12003. break;
  12004. case WASM_OP_I32_WRAP_I64:
  12005. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12006. break;
  12007. case WASM_OP_I32_TRUNC_S_F32:
  12008. case WASM_OP_I32_TRUNC_U_F32:
  12009. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12010. break;
  12011. case WASM_OP_I32_TRUNC_S_F64:
  12012. case WASM_OP_I32_TRUNC_U_F64:
  12013. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12014. break;
  12015. case WASM_OP_I64_EXTEND_S_I32:
  12016. case WASM_OP_I64_EXTEND_U_I32:
  12017. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12018. break;
  12019. case WASM_OP_I64_TRUNC_S_F32:
  12020. case WASM_OP_I64_TRUNC_U_F32:
  12021. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12022. break;
  12023. case WASM_OP_I64_TRUNC_S_F64:
  12024. case WASM_OP_I64_TRUNC_U_F64:
  12025. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12026. break;
  12027. case WASM_OP_F32_CONVERT_S_I32:
  12028. case WASM_OP_F32_CONVERT_U_I32:
  12029. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12030. break;
  12031. case WASM_OP_F32_CONVERT_S_I64:
  12032. case WASM_OP_F32_CONVERT_U_I64:
  12033. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12034. break;
  12035. case WASM_OP_F32_DEMOTE_F64:
  12036. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12037. break;
  12038. case WASM_OP_F64_CONVERT_S_I32:
  12039. case WASM_OP_F64_CONVERT_U_I32:
  12040. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12041. break;
  12042. case WASM_OP_F64_CONVERT_S_I64:
  12043. case WASM_OP_F64_CONVERT_U_I64:
  12044. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12045. break;
  12046. case WASM_OP_F64_PROMOTE_F32:
  12047. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12048. break;
  12049. case WASM_OP_I32_REINTERPRET_F32:
  12050. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12051. break;
  12052. case WASM_OP_I64_REINTERPRET_F64:
  12053. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12054. break;
  12055. case WASM_OP_F32_REINTERPRET_I32:
  12056. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12057. break;
  12058. case WASM_OP_F64_REINTERPRET_I64:
  12059. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12060. break;
  12061. case WASM_OP_I32_EXTEND8_S:
  12062. case WASM_OP_I32_EXTEND16_S:
  12063. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12064. break;
  12065. case WASM_OP_I64_EXTEND8_S:
  12066. case WASM_OP_I64_EXTEND16_S:
  12067. case WASM_OP_I64_EXTEND32_S:
  12068. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12069. break;
  12070. #if WASM_ENABLE_GC != 0
  12071. case WASM_OP_GC_PREFIX:
  12072. {
  12073. uint32 opcode1;
  12074. read_leb_uint32(p, p_end, opcode1);
  12075. #if WASM_ENABLE_FAST_INTERP != 0
  12076. emit_byte(loader_ctx, ((uint8)opcode1));
  12077. #endif
  12078. switch (opcode1) {
  12079. case WASM_OP_STRUCT_NEW:
  12080. case WASM_OP_STRUCT_NEW_DEFAULT:
  12081. {
  12082. read_leb_uint32(p, p_end, type_idx);
  12083. #if WASM_ENABLE_FAST_INTERP != 0
  12084. emit_uint32(loader_ctx, type_idx);
  12085. #endif
  12086. if (!check_type_index(module, module->type_count,
  12087. type_idx, error_buf,
  12088. error_buf_size)) {
  12089. goto fail;
  12090. }
  12091. if (module->types[type_idx]->type_flag
  12092. != WASM_TYPE_STRUCT) {
  12093. set_error_buf(error_buf, error_buf_size,
  12094. "unkown struct type");
  12095. goto fail;
  12096. }
  12097. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12098. int32 j, k;
  12099. uint8 value_type;
  12100. uint32 ref_type_struct_size;
  12101. WASMStructType *struct_type =
  12102. (WASMStructType *)module->types[type_idx];
  12103. k = struct_type->ref_type_map_count - 1;
  12104. for (j = struct_type->field_count - 1; j >= 0;
  12105. j--) {
  12106. value_type = struct_type->fields[j].field_type;
  12107. if (wasm_is_type_reftype(value_type)) {
  12108. if (wasm_is_type_multi_byte_type(
  12109. value_type)) {
  12110. ref_type_struct_size =
  12111. wasm_reftype_struct_size(
  12112. struct_type->ref_type_maps[k]
  12113. .ref_type);
  12114. bh_memcpy_s(
  12115. &wasm_ref_type,
  12116. (uint32)sizeof(WASMRefType),
  12117. struct_type->ref_type_maps[k]
  12118. .ref_type,
  12119. ref_type_struct_size);
  12120. k--;
  12121. }
  12122. POP_REF(value_type);
  12123. }
  12124. else {
  12125. switch (value_type) {
  12126. case VALUE_TYPE_I32:
  12127. case PACKED_TYPE_I8:
  12128. case PACKED_TYPE_I16:
  12129. POP_I32();
  12130. break;
  12131. case VALUE_TYPE_I64:
  12132. POP_I64();
  12133. break;
  12134. case VALUE_TYPE_F32:
  12135. POP_F32();
  12136. break;
  12137. case VALUE_TYPE_F64:
  12138. POP_F64();
  12139. break;
  12140. default:
  12141. set_error_buf(error_buf,
  12142. error_buf_size,
  12143. "unknown type");
  12144. goto fail;
  12145. }
  12146. }
  12147. }
  12148. }
  12149. /* PUSH struct obj, (ref $t) */
  12150. wasm_set_refheaptype_typeidx(
  12151. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12152. PUSH_REF(wasm_ref_type.ref_type);
  12153. break;
  12154. }
  12155. case WASM_OP_STRUCT_GET:
  12156. case WASM_OP_STRUCT_GET_S:
  12157. case WASM_OP_STRUCT_GET_U:
  12158. case WASM_OP_STRUCT_SET:
  12159. {
  12160. WASMStructType *struct_type;
  12161. WASMRefType *ref_type = NULL;
  12162. uint32 field_idx;
  12163. uint8 field_type;
  12164. read_leb_uint32(p, p_end, type_idx);
  12165. #if WASM_ENABLE_FAST_INTERP != 0
  12166. emit_uint32(loader_ctx, type_idx);
  12167. #endif
  12168. if (!check_type_index(module, module->type_count,
  12169. type_idx, error_buf,
  12170. error_buf_size)) {
  12171. goto fail;
  12172. }
  12173. if (module->types[type_idx]->type_flag
  12174. != WASM_TYPE_STRUCT) {
  12175. set_error_buf(error_buf, error_buf_size,
  12176. "unknown struct type");
  12177. goto fail;
  12178. }
  12179. struct_type = (WASMStructType *)module->types[type_idx];
  12180. read_leb_uint32(p, p_end, field_idx);
  12181. #if WASM_ENABLE_FAST_INTERP != 0
  12182. emit_uint32(loader_ctx, field_idx);
  12183. #endif
  12184. if (field_idx >= struct_type->field_count) {
  12185. set_error_buf(error_buf, error_buf_size,
  12186. "unknown struct field");
  12187. goto fail;
  12188. }
  12189. if (opcode1 == WASM_OP_STRUCT_SET
  12190. && !(struct_type->fields[field_idx].field_flags
  12191. & 1)) {
  12192. set_error_buf(error_buf, error_buf_size,
  12193. "field is immutable");
  12194. goto fail;
  12195. }
  12196. field_type = struct_type->fields[field_idx].field_type;
  12197. if (is_packed_type(field_type)) {
  12198. if (opcode1 == WASM_OP_STRUCT_GET) {
  12199. set_error_buf(error_buf, error_buf_size,
  12200. "type mismatch");
  12201. goto fail;
  12202. }
  12203. else {
  12204. field_type = VALUE_TYPE_I32;
  12205. }
  12206. }
  12207. if (wasm_is_type_multi_byte_type(field_type)) {
  12208. ref_type = wasm_reftype_map_find(
  12209. struct_type->ref_type_maps,
  12210. struct_type->ref_type_map_count, field_idx);
  12211. bh_assert(ref_type);
  12212. }
  12213. if (opcode1 == WASM_OP_STRUCT_SET) {
  12214. /* POP field */
  12215. if (wasm_is_type_multi_byte_type(field_type)) {
  12216. bh_memcpy_s(&wasm_ref_type,
  12217. (uint32)sizeof(WASMRefType),
  12218. ref_type,
  12219. wasm_reftype_struct_size(ref_type));
  12220. }
  12221. POP_REF(field_type);
  12222. /* POP struct obj, (ref null $t) */
  12223. wasm_set_refheaptype_typeidx(
  12224. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12225. POP_REF(wasm_ref_type.ref_type);
  12226. }
  12227. else {
  12228. /* POP struct obj, (ref null $t) */
  12229. wasm_set_refheaptype_typeidx(
  12230. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12231. POP_REF(wasm_ref_type.ref_type);
  12232. /* PUSH field */
  12233. if (wasm_is_type_multi_byte_type(field_type)) {
  12234. bh_memcpy_s(&wasm_ref_type,
  12235. (uint32)sizeof(WASMRefType),
  12236. ref_type,
  12237. wasm_reftype_struct_size(ref_type));
  12238. }
  12239. PUSH_REF(field_type);
  12240. }
  12241. break;
  12242. }
  12243. case WASM_OP_ARRAY_NEW:
  12244. case WASM_OP_ARRAY_NEW_DEFAULT:
  12245. case WASM_OP_ARRAY_NEW_FIXED:
  12246. case WASM_OP_ARRAY_NEW_DATA:
  12247. case WASM_OP_ARRAY_NEW_ELEM:
  12248. {
  12249. WASMArrayType *array_type;
  12250. uint8 elem_type;
  12251. uint32 u32 = 0;
  12252. read_leb_uint32(p, p_end, type_idx);
  12253. #if WASM_ENABLE_FAST_INTERP != 0
  12254. emit_uint32(loader_ctx, type_idx);
  12255. #endif
  12256. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12257. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12258. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12259. read_leb_uint32(p, p_end, u32);
  12260. #if WASM_ENABLE_FAST_INTERP != 0
  12261. emit_uint32(loader_ctx, u32);
  12262. #endif
  12263. }
  12264. if (!check_array_type(module, type_idx, error_buf,
  12265. error_buf_size)) {
  12266. goto fail;
  12267. }
  12268. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12269. /* length */
  12270. POP_I32();
  12271. }
  12272. array_type = (WASMArrayType *)module->types[type_idx];
  12273. elem_type = array_type->elem_type;
  12274. if (opcode1 == WASM_OP_ARRAY_NEW
  12275. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12276. if (wasm_is_type_multi_byte_type(elem_type)) {
  12277. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12278. array_type->elem_ref_type,
  12279. wasm_reftype_struct_size(
  12280. array_type->elem_ref_type));
  12281. }
  12282. if (is_packed_type(elem_type)) {
  12283. elem_type = VALUE_TYPE_I32;
  12284. }
  12285. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12286. uint32 N = u32;
  12287. for (i = 0; i < N; i++) {
  12288. if (wasm_is_type_multi_byte_type(
  12289. elem_type)) {
  12290. bh_memcpy_s(
  12291. &wasm_ref_type, sizeof(WASMRefType),
  12292. array_type->elem_ref_type,
  12293. wasm_reftype_struct_size(
  12294. array_type->elem_ref_type));
  12295. }
  12296. POP_REF(elem_type);
  12297. }
  12298. }
  12299. else
  12300. POP_REF(elem_type);
  12301. }
  12302. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12303. /* offset of data segment */
  12304. POP_I32();
  12305. if (u32 >= module->data_seg_count) {
  12306. set_error_buf(error_buf, error_buf_size,
  12307. "unknown data segment");
  12308. goto fail;
  12309. }
  12310. if (wasm_is_type_reftype(elem_type)) {
  12311. set_error_buf(error_buf, error_buf_size,
  12312. "array elem type mismatch");
  12313. goto fail;
  12314. }
  12315. }
  12316. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12317. WASMTableSeg *table_seg =
  12318. module->table_segments + u32;
  12319. /* offset of element segment */
  12320. POP_I32();
  12321. if (u32 >= module->table_seg_count) {
  12322. set_error_buf(error_buf, error_buf_size,
  12323. "unknown element segment");
  12324. goto fail;
  12325. }
  12326. if (!wasm_reftype_is_subtype_of(
  12327. table_seg->elem_type,
  12328. table_seg->elem_ref_type, elem_type,
  12329. array_type->elem_ref_type, module->types,
  12330. module->type_count)) {
  12331. set_error_buf(error_buf, error_buf_size,
  12332. "array elem type mismatch");
  12333. goto fail;
  12334. }
  12335. }
  12336. /* PUSH array obj, (ref $t) */
  12337. wasm_set_refheaptype_typeidx(
  12338. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12339. PUSH_REF(wasm_ref_type.ref_type);
  12340. break;
  12341. }
  12342. case WASM_OP_ARRAY_GET:
  12343. case WASM_OP_ARRAY_GET_S:
  12344. case WASM_OP_ARRAY_GET_U:
  12345. case WASM_OP_ARRAY_SET:
  12346. {
  12347. uint8 elem_type;
  12348. WASMArrayType *array_type;
  12349. WASMRefType *ref_type = NULL;
  12350. read_leb_uint32(p, p_end, type_idx);
  12351. #if WASM_ENABLE_FAST_INTERP != 0
  12352. emit_uint32(loader_ctx, type_idx);
  12353. #endif
  12354. if (!check_array_type(module, type_idx, error_buf,
  12355. error_buf_size)) {
  12356. goto fail;
  12357. }
  12358. array_type = (WASMArrayType *)module->types[type_idx];
  12359. if (opcode1 == WASM_OP_ARRAY_SET
  12360. && !(array_type->elem_flags & 1)) {
  12361. set_error_buf(error_buf, error_buf_size,
  12362. "array is immutable");
  12363. goto fail;
  12364. }
  12365. elem_type = array_type->elem_type;
  12366. if (is_packed_type(elem_type)) {
  12367. if (opcode1 != WASM_OP_ARRAY_GET_S
  12368. && opcode1 != WASM_OP_ARRAY_GET_U
  12369. && opcode1 != WASM_OP_ARRAY_SET) {
  12370. set_error_buf(error_buf, error_buf_size,
  12371. "type mismatch");
  12372. goto fail;
  12373. }
  12374. else {
  12375. elem_type = VALUE_TYPE_I32;
  12376. }
  12377. }
  12378. ref_type = array_type->elem_ref_type;
  12379. if (opcode1 == WASM_OP_ARRAY_SET) {
  12380. /* POP elem to set */
  12381. if (wasm_is_type_multi_byte_type(elem_type)) {
  12382. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12383. ref_type,
  12384. wasm_reftype_struct_size(ref_type));
  12385. }
  12386. POP_REF(elem_type);
  12387. }
  12388. /* elem idx */
  12389. POP_I32();
  12390. /* POP array obj, (ref null $t) */
  12391. wasm_set_refheaptype_typeidx(
  12392. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12393. POP_REF(wasm_ref_type.ref_type);
  12394. if (opcode1 != WASM_OP_ARRAY_SET) {
  12395. /* PUSH elem */
  12396. if (wasm_is_type_multi_byte_type(elem_type)) {
  12397. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12398. ref_type,
  12399. wasm_reftype_struct_size(ref_type));
  12400. }
  12401. PUSH_REF(elem_type);
  12402. }
  12403. break;
  12404. }
  12405. case WASM_OP_ARRAY_LEN:
  12406. {
  12407. POP_REF(REF_TYPE_ARRAYREF);
  12408. /* length */
  12409. PUSH_I32();
  12410. break;
  12411. }
  12412. case WASM_OP_ARRAY_FILL:
  12413. {
  12414. WASMArrayType *array_type;
  12415. uint8 elem_type;
  12416. /* typeidx */
  12417. read_leb_uint32(p, p_end, type_idx);
  12418. #if WASM_ENABLE_FAST_INTERP != 0
  12419. emit_uint32(loader_ctx, type_idx);
  12420. #endif
  12421. if (!check_array_type(module, type_idx, error_buf,
  12422. error_buf_size)) {
  12423. goto fail;
  12424. }
  12425. array_type = (WASMArrayType *)module->types[type_idx];
  12426. if (!(array_type->elem_flags & 1)) {
  12427. set_error_buf(error_buf, error_buf_size,
  12428. "array is immutable");
  12429. goto fail;
  12430. }
  12431. elem_type = array_type->elem_type;
  12432. if (is_packed_type(elem_type)) {
  12433. elem_type = VALUE_TYPE_I32;
  12434. }
  12435. POP_I32(); /* length */
  12436. #if WASM_ENABLE_FAST_INTERP != 0
  12437. POP_OFFSET_TYPE(elem_type);
  12438. #endif
  12439. POP_TYPE(elem_type);
  12440. POP_I32(); /* start */
  12441. /* POP array obj, (ref null $t) */
  12442. wasm_set_refheaptype_typeidx(
  12443. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12444. POP_REF(wasm_ref_type.ref_type);
  12445. break;
  12446. }
  12447. case WASM_OP_ARRAY_COPY:
  12448. {
  12449. uint32 src_type_idx;
  12450. uint8 src_elem_type, dst_elem_type;
  12451. WASMRefType src_ref_type = { 0 },
  12452. *src_elem_ref_type = NULL;
  12453. WASMRefType dst_ref_type = { 0 },
  12454. *dst_elem_ref_type = NULL;
  12455. WASMArrayType *array_type;
  12456. /* typeidx1 */
  12457. read_leb_uint32(p, p_end, type_idx);
  12458. #if WASM_ENABLE_FAST_INTERP != 0
  12459. emit_uint32(loader_ctx, type_idx);
  12460. #endif
  12461. /* typeidx2 */
  12462. read_leb_uint32(p, p_end, src_type_idx);
  12463. #if WASM_ENABLE_FAST_INTERP != 0
  12464. emit_uint32(loader_ctx, src_type_idx);
  12465. #endif
  12466. if (!check_array_type(module, type_idx, error_buf,
  12467. error_buf_size)) {
  12468. goto fail;
  12469. }
  12470. if (!check_array_type(module, src_type_idx, error_buf,
  12471. error_buf_size)) {
  12472. goto fail;
  12473. }
  12474. POP_I32();
  12475. POP_I32();
  12476. /* POP array obj, (ref null $t) */
  12477. wasm_set_refheaptype_typeidx(
  12478. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12479. POP_REF(wasm_ref_type.ref_type);
  12480. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12481. &wasm_ref_type,
  12482. wasm_reftype_struct_size(&wasm_ref_type));
  12483. POP_I32();
  12484. /* POP array obj, (ref null $t) */
  12485. wasm_set_refheaptype_typeidx(
  12486. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12487. POP_REF(wasm_ref_type.ref_type);
  12488. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12489. &wasm_ref_type,
  12490. wasm_reftype_struct_size(&wasm_ref_type));
  12491. array_type = (WASMArrayType *)module->types[type_idx];
  12492. if (!(array_type->elem_flags & 1)) {
  12493. set_error_buf(error_buf, error_buf_size,
  12494. "destination array is immutable");
  12495. goto fail;
  12496. }
  12497. dst_elem_type = array_type->elem_type;
  12498. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12499. dst_elem_ref_type = array_type->elem_ref_type;
  12500. }
  12501. array_type =
  12502. (WASMArrayType *)module->types[src_type_idx];
  12503. src_elem_type = array_type->elem_type;
  12504. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12505. src_elem_ref_type = array_type->elem_ref_type;
  12506. }
  12507. if (!wasm_reftype_is_subtype_of(
  12508. src_elem_type, src_elem_ref_type, dst_elem_type,
  12509. dst_elem_ref_type, module->types,
  12510. module->type_count)) {
  12511. set_error_buf(error_buf, error_buf_size,
  12512. "array types do not match");
  12513. goto fail;
  12514. }
  12515. break;
  12516. }
  12517. case WASM_OP_REF_I31:
  12518. {
  12519. POP_I32();
  12520. wasm_set_refheaptype_common(
  12521. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12522. PUSH_REF(wasm_ref_type.ref_type);
  12523. break;
  12524. }
  12525. case WASM_OP_I31_GET_S:
  12526. case WASM_OP_I31_GET_U:
  12527. {
  12528. POP_REF(REF_TYPE_I31REF);
  12529. PUSH_I32();
  12530. break;
  12531. }
  12532. case WASM_OP_REF_TEST:
  12533. case WASM_OP_REF_CAST:
  12534. case WASM_OP_REF_TEST_NULLABLE:
  12535. case WASM_OP_REF_CAST_NULLABLE:
  12536. {
  12537. uint8 type;
  12538. read_leb_int32(p, p_end, heap_type);
  12539. #if WASM_ENABLE_FAST_INTERP != 0
  12540. emit_uint32(loader_ctx, (uint32)heap_type);
  12541. #endif
  12542. if (heap_type >= 0) {
  12543. if (!check_type_index(module, module->type_count,
  12544. heap_type, error_buf,
  12545. error_buf_size)) {
  12546. goto fail;
  12547. }
  12548. }
  12549. else {
  12550. if (!wasm_is_valid_heap_type(heap_type)) {
  12551. set_error_buf(error_buf, error_buf_size,
  12552. "unknown type");
  12553. goto fail;
  12554. }
  12555. }
  12556. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12557. &wasm_ref_type, error_buf,
  12558. error_buf_size)) {
  12559. goto fail;
  12560. }
  12561. if (opcode1 == WASM_OP_REF_TEST
  12562. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12563. PUSH_I32();
  12564. else {
  12565. bool nullable =
  12566. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12567. : false;
  12568. if (heap_type >= 0 || !nullable) {
  12569. wasm_set_refheaptype_typeidx(
  12570. &wasm_ref_type.ref_ht_typeidx, nullable,
  12571. heap_type);
  12572. PUSH_REF(wasm_ref_type.ref_type);
  12573. }
  12574. else {
  12575. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12576. }
  12577. }
  12578. break;
  12579. }
  12580. case WASM_OP_BR_ON_CAST:
  12581. case WASM_OP_BR_ON_CAST_FAIL:
  12582. {
  12583. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12584. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12585. uint8 type_tmp, castflags;
  12586. uint32 depth;
  12587. int32 heap_type_dst;
  12588. bool src_nullable, dst_nullable;
  12589. CHECK_BUF(p, p_end, 1);
  12590. castflags = read_uint8(p);
  12591. #if WASM_ENABLE_FAST_INTERP != 0
  12592. /* Emit heap_type firstly */
  12593. emit_byte(loader_ctx, castflags);
  12594. #endif
  12595. p_org = p;
  12596. read_leb_uint32(p, p_end, depth);
  12597. read_leb_int32(p, p_end, heap_type);
  12598. #if WASM_ENABLE_FAST_INTERP != 0
  12599. /* Emit heap_type firstly */
  12600. emit_uint32(loader_ctx, (uint32)heap_type);
  12601. #endif
  12602. read_leb_int32(p, p_end, heap_type_dst);
  12603. #if WASM_ENABLE_FAST_INTERP != 0
  12604. /* Emit heap_type firstly */
  12605. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12606. #endif
  12607. (void)depth;
  12608. /*
  12609. * castflags should be 0~3:
  12610. * 0: (non-null, non-null)
  12611. * 1: (null, non-null)
  12612. * 2: (non-null, null)
  12613. * 3: (null, null)
  12614. */
  12615. if (castflags > 3) {
  12616. set_error_buf(error_buf, error_buf_size,
  12617. "invalid castflags");
  12618. break;
  12619. }
  12620. src_nullable =
  12621. (castflags == 1) || (castflags == 3) ? true : false;
  12622. dst_nullable =
  12623. (castflags == 2) || (castflags == 3) ? true : false;
  12624. /* Pop and backup the stack top's ref type */
  12625. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12626. &ref_type_tmp, error_buf,
  12627. error_buf_size)) {
  12628. goto fail;
  12629. }
  12630. /* The reference type rt1 must be valid */
  12631. if (!init_ref_type(module, &ref_type1, src_nullable,
  12632. heap_type, error_buf,
  12633. error_buf_size)) {
  12634. goto fail;
  12635. }
  12636. /* The reference type rt2 must be valid. */
  12637. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12638. heap_type_dst, error_buf,
  12639. error_buf_size)) {
  12640. goto fail;
  12641. }
  12642. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12643. &ref_type2);
  12644. /* The reference type rt2 must match rt1. */
  12645. if (!wasm_reftype_is_subtype_of(
  12646. ref_type2.ref_type, &ref_type2,
  12647. ref_type1.ref_type, &ref_type1, module->types,
  12648. module->type_count)) {
  12649. set_error_buf(error_buf, error_buf_size,
  12650. "type mismatch");
  12651. goto fail;
  12652. }
  12653. p = p_org;
  12654. /* Push ref type casted for branch block check */
  12655. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12656. /* The reference type rt2 must match rt′. */
  12657. type_tmp = ref_type2.ref_type;
  12658. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12659. bh_memcpy_s(
  12660. &wasm_ref_type,
  12661. wasm_reftype_struct_size(&ref_type2),
  12662. &ref_type2,
  12663. wasm_reftype_struct_size(&ref_type2));
  12664. }
  12665. }
  12666. else {
  12667. /* The reference type rt′1 must match rt′. */
  12668. type_tmp = ref_type_diff.ref_type;
  12669. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12670. bh_memcpy_s(
  12671. &wasm_ref_type,
  12672. wasm_reftype_struct_size(&ref_type_diff),
  12673. &ref_type_diff,
  12674. wasm_reftype_struct_size(&ref_type_diff));
  12675. }
  12676. }
  12677. PUSH_REF(type_tmp);
  12678. if (!(frame_csp_tmp = check_branch_block(
  12679. loader_ctx, &p, p_end, opcode, error_buf,
  12680. error_buf_size))) {
  12681. goto fail;
  12682. }
  12683. /* Ignore heap_types */
  12684. skip_leb_uint32(p, p_end);
  12685. skip_leb_uint32(p, p_end);
  12686. /* Restore the original stack top's ref type */
  12687. POP_REF(type_tmp);
  12688. #if WASM_ENABLE_FAST_INTERP != 0
  12689. /* Erase the opnd offset emitted by POP_REF() */
  12690. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12691. #endif
  12692. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12693. type_tmp = ref_type_diff.ref_type;
  12694. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12695. bh_memcpy_s(
  12696. &wasm_ref_type,
  12697. wasm_reftype_struct_size(&ref_type_diff),
  12698. &ref_type_diff,
  12699. wasm_reftype_struct_size(&ref_type_diff));
  12700. }
  12701. }
  12702. else {
  12703. type_tmp = ref_type2.ref_type;
  12704. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12705. bh_memcpy_s(
  12706. &wasm_ref_type,
  12707. wasm_reftype_struct_size(&ref_type2),
  12708. &ref_type2,
  12709. wasm_reftype_struct_size(&ref_type2));
  12710. }
  12711. }
  12712. PUSH_REF(type_tmp);
  12713. #if WASM_ENABLE_FAST_INTERP != 0
  12714. /* Erase the opnd offset emitted by PUSH_REF() */
  12715. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12716. #endif
  12717. break;
  12718. }
  12719. case WASM_OP_ANY_CONVERT_EXTERN:
  12720. {
  12721. uint8 type;
  12722. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12723. &wasm_ref_type, error_buf,
  12724. error_buf_size)) {
  12725. goto fail;
  12726. }
  12727. if (!(type == REF_TYPE_EXTERNREF
  12728. || (type == REF_TYPE_HT_NON_NULLABLE
  12729. && wasm_ref_type.ref_ht_common.heap_type
  12730. == HEAP_TYPE_EXTERN)
  12731. || type == VALUE_TYPE_ANY)) {
  12732. set_error_buf(error_buf, error_buf_size,
  12733. "type mismatch");
  12734. goto fail;
  12735. }
  12736. if (type == REF_TYPE_EXTERNREF)
  12737. type = REF_TYPE_ANYREF;
  12738. else {
  12739. wasm_ref_type.ref_ht_common.heap_type =
  12740. HEAP_TYPE_ANY;
  12741. }
  12742. PUSH_REF(type);
  12743. break;
  12744. }
  12745. case WASM_OP_EXTERN_CONVERT_ANY:
  12746. {
  12747. uint8 type;
  12748. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12749. &wasm_ref_type, error_buf,
  12750. error_buf_size)) {
  12751. goto fail;
  12752. }
  12753. if (type == REF_TYPE_EXTERNREF
  12754. || ((type == REF_TYPE_HT_NULLABLE
  12755. || type == REF_TYPE_HT_NON_NULLABLE)
  12756. && wasm_ref_type.ref_ht_common.heap_type
  12757. == HEAP_TYPE_EXTERN)) {
  12758. set_error_buf(error_buf, error_buf_size,
  12759. "type mismatch");
  12760. goto fail;
  12761. }
  12762. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12763. /* push (ref null extern) */
  12764. type = REF_TYPE_EXTERNREF;
  12765. }
  12766. else {
  12767. /* push (ref extern) */
  12768. type = REF_TYPE_HT_NON_NULLABLE;
  12769. wasm_set_refheaptype_common(
  12770. &wasm_ref_type.ref_ht_common, false,
  12771. HEAP_TYPE_EXTERN);
  12772. }
  12773. PUSH_REF(type);
  12774. break;
  12775. }
  12776. #if WASM_ENABLE_STRINGREF != 0
  12777. case WASM_OP_STRING_NEW_UTF8:
  12778. case WASM_OP_STRING_NEW_WTF16:
  12779. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12780. case WASM_OP_STRING_NEW_WTF8:
  12781. {
  12782. uint32 memidx;
  12783. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12784. func->has_memory_operations = true;
  12785. #endif
  12786. read_leb_uint32(p, p_end, memidx);
  12787. #if WASM_ENABLE_FAST_INTERP != 0
  12788. emit_uint32(loader_ctx, (uint32)memidx);
  12789. #endif
  12790. POP_I32();
  12791. POP_I32();
  12792. PUSH_REF(REF_TYPE_STRINGREF);
  12793. (void)memidx;
  12794. break;
  12795. }
  12796. case WASM_OP_STRING_CONST:
  12797. {
  12798. uint32 contents;
  12799. read_leb_uint32(p, p_end, contents);
  12800. #if WASM_ENABLE_FAST_INTERP != 0
  12801. emit_uint32(loader_ctx, (uint32)contents);
  12802. #endif
  12803. PUSH_REF(REF_TYPE_STRINGREF);
  12804. (void)contents;
  12805. break;
  12806. }
  12807. case WASM_OP_STRING_MEASURE_UTF8:
  12808. case WASM_OP_STRING_MEASURE_WTF8:
  12809. case WASM_OP_STRING_MEASURE_WTF16:
  12810. {
  12811. POP_STRINGREF();
  12812. PUSH_I32();
  12813. break;
  12814. }
  12815. case WASM_OP_STRING_ENCODE_UTF8:
  12816. case WASM_OP_STRING_ENCODE_WTF16:
  12817. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12818. case WASM_OP_STRING_ENCODE_WTF8:
  12819. {
  12820. uint32 memidx;
  12821. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12822. func->has_memory_operations = true;
  12823. #endif
  12824. read_leb_uint32(p, p_end, memidx);
  12825. #if WASM_ENABLE_FAST_INTERP != 0
  12826. emit_uint32(loader_ctx, (uint32)memidx);
  12827. #endif
  12828. POP_I32();
  12829. POP_STRINGREF();
  12830. PUSH_I32();
  12831. (void)memidx;
  12832. break;
  12833. }
  12834. case WASM_OP_STRING_CONCAT:
  12835. {
  12836. POP_STRINGREF();
  12837. POP_STRINGREF();
  12838. PUSH_REF(REF_TYPE_STRINGREF);
  12839. break;
  12840. }
  12841. case WASM_OP_STRING_EQ:
  12842. {
  12843. POP_STRINGREF();
  12844. POP_STRINGREF();
  12845. PUSH_I32();
  12846. break;
  12847. }
  12848. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12849. {
  12850. POP_STRINGREF();
  12851. PUSH_I32();
  12852. break;
  12853. }
  12854. case WASM_OP_STRING_AS_WTF8:
  12855. {
  12856. POP_STRINGREF();
  12857. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12858. break;
  12859. }
  12860. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12861. {
  12862. POP_I32();
  12863. POP_I32();
  12864. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12865. PUSH_I32();
  12866. break;
  12867. }
  12868. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12869. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12870. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12871. {
  12872. uint32 memidx;
  12873. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12874. func->has_memory_operations = true;
  12875. #endif
  12876. read_leb_uint32(p, p_end, memidx);
  12877. #if WASM_ENABLE_FAST_INTERP != 0
  12878. emit_uint32(loader_ctx, (uint32)memidx);
  12879. #endif
  12880. POP_I32();
  12881. POP_I32();
  12882. POP_I32();
  12883. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12884. PUSH_I32();
  12885. PUSH_I32();
  12886. (void)memidx;
  12887. break;
  12888. }
  12889. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12890. {
  12891. POP_I32();
  12892. POP_I32();
  12893. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12894. PUSH_REF(REF_TYPE_STRINGREF);
  12895. break;
  12896. }
  12897. case WASM_OP_STRING_AS_WTF16:
  12898. {
  12899. POP_STRINGREF();
  12900. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12901. break;
  12902. }
  12903. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12904. {
  12905. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12906. PUSH_I32();
  12907. break;
  12908. }
  12909. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  12910. {
  12911. POP_I32();
  12912. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12913. PUSH_I32();
  12914. break;
  12915. }
  12916. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  12917. {
  12918. uint32 memidx;
  12919. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12920. func->has_memory_operations = true;
  12921. #endif
  12922. read_leb_uint32(p, p_end, memidx);
  12923. #if WASM_ENABLE_FAST_INTERP != 0
  12924. emit_uint32(loader_ctx, (uint32)memidx);
  12925. #endif
  12926. POP_I32();
  12927. POP_I32();
  12928. POP_I32();
  12929. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12930. PUSH_I32();
  12931. (void)memidx;
  12932. break;
  12933. }
  12934. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  12935. {
  12936. POP_I32();
  12937. POP_I32();
  12938. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12939. PUSH_REF(REF_TYPE_STRINGREF);
  12940. break;
  12941. }
  12942. case WASM_OP_STRING_AS_ITER:
  12943. {
  12944. POP_STRINGREF();
  12945. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  12946. break;
  12947. }
  12948. case WASM_OP_STRINGVIEW_ITER_NEXT:
  12949. {
  12950. POP_REF(REF_TYPE_STRINGVIEWITER);
  12951. PUSH_I32();
  12952. break;
  12953. }
  12954. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  12955. case WASM_OP_STRINGVIEW_ITER_REWIND:
  12956. {
  12957. POP_I32();
  12958. POP_REF(REF_TYPE_STRINGVIEWITER);
  12959. PUSH_I32();
  12960. break;
  12961. }
  12962. case WASM_OP_STRINGVIEW_ITER_SLICE:
  12963. {
  12964. POP_I32();
  12965. POP_REF(REF_TYPE_STRINGVIEWITER);
  12966. PUSH_REF(REF_TYPE_STRINGREF);
  12967. break;
  12968. }
  12969. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  12970. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  12971. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  12972. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  12973. {
  12974. POP_I32();
  12975. POP_I32();
  12976. POP_REF(REF_TYPE_ARRAYREF);
  12977. PUSH_REF(REF_TYPE_STRINGREF);
  12978. break;
  12979. }
  12980. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  12981. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  12982. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  12983. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  12984. {
  12985. POP_I32();
  12986. POP_REF(REF_TYPE_ARRAYREF);
  12987. POP_STRINGREF();
  12988. PUSH_I32();
  12989. break;
  12990. }
  12991. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  12992. default:
  12993. set_error_buf_v(error_buf, error_buf_size,
  12994. "%s %02x %02x", "unsupported opcode",
  12995. 0xfb, opcode1);
  12996. goto fail;
  12997. }
  12998. break;
  12999. }
  13000. #endif /* end of WASM_ENABLE_GC != 0 */
  13001. case WASM_OP_MISC_PREFIX:
  13002. {
  13003. uint32 opcode1;
  13004. read_leb_uint32(p, p_end, opcode1);
  13005. #if WASM_ENABLE_FAST_INTERP != 0
  13006. emit_byte(loader_ctx, ((uint8)opcode1));
  13007. #endif
  13008. switch (opcode1) {
  13009. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13010. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13011. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13012. break;
  13013. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13014. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13015. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13016. break;
  13017. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13018. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13019. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13020. break;
  13021. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13022. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13023. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13024. break;
  13025. #if WASM_ENABLE_BULK_MEMORY != 0
  13026. case WASM_OP_MEMORY_INIT:
  13027. {
  13028. read_leb_uint32(p, p_end, data_seg_idx);
  13029. #if WASM_ENABLE_FAST_INTERP != 0
  13030. emit_uint32(loader_ctx, data_seg_idx);
  13031. #endif
  13032. if (module->import_memory_count == 0
  13033. && module->memory_count == 0)
  13034. goto fail_unknown_memory;
  13035. if (*p++ != 0x00)
  13036. goto fail_zero_byte_expected;
  13037. if (data_seg_idx >= module->data_seg_count) {
  13038. set_error_buf_v(error_buf, error_buf_size,
  13039. "unknown data segment %d",
  13040. data_seg_idx);
  13041. goto fail;
  13042. }
  13043. if (module->data_seg_count1 == 0)
  13044. goto fail_data_cnt_sec_require;
  13045. POP_I32();
  13046. POP_I32();
  13047. POP_MEM_OFFSET();
  13048. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13049. func->has_memory_operations = true;
  13050. #endif
  13051. #if WASM_ENABLE_WAMR_COMPILER != 0
  13052. module->is_bulk_memory_used = true;
  13053. #endif
  13054. break;
  13055. }
  13056. case WASM_OP_DATA_DROP:
  13057. {
  13058. read_leb_uint32(p, p_end, data_seg_idx);
  13059. #if WASM_ENABLE_FAST_INTERP != 0
  13060. emit_uint32(loader_ctx, data_seg_idx);
  13061. #endif
  13062. if (data_seg_idx >= module->data_seg_count) {
  13063. set_error_buf(error_buf, error_buf_size,
  13064. "unknown data segment");
  13065. goto fail;
  13066. }
  13067. if (module->data_seg_count1 == 0)
  13068. goto fail_data_cnt_sec_require;
  13069. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13070. func->has_memory_operations = true;
  13071. #endif
  13072. #if WASM_ENABLE_WAMR_COMPILER != 0
  13073. module->is_bulk_memory_used = true;
  13074. #endif
  13075. break;
  13076. }
  13077. case WASM_OP_MEMORY_COPY:
  13078. {
  13079. CHECK_BUF(p, p_end, sizeof(int16));
  13080. /* both src and dst memory index should be 0 */
  13081. if (*(int16 *)p != 0x0000)
  13082. goto fail_zero_byte_expected;
  13083. p += 2;
  13084. if (module->import_memory_count == 0
  13085. && module->memory_count == 0)
  13086. goto fail_unknown_memory;
  13087. POP_MEM_OFFSET();
  13088. POP_MEM_OFFSET();
  13089. POP_MEM_OFFSET();
  13090. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13091. func->has_memory_operations = true;
  13092. #endif
  13093. #if WASM_ENABLE_WAMR_COMPILER != 0
  13094. module->is_bulk_memory_used = true;
  13095. #endif
  13096. break;
  13097. }
  13098. case WASM_OP_MEMORY_FILL:
  13099. {
  13100. if (*p++ != 0x00) {
  13101. goto fail_zero_byte_expected;
  13102. }
  13103. if (module->import_memory_count == 0
  13104. && module->memory_count == 0) {
  13105. goto fail_unknown_memory;
  13106. }
  13107. POP_MEM_OFFSET();
  13108. POP_I32();
  13109. POP_MEM_OFFSET();
  13110. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13111. func->has_memory_operations = true;
  13112. #endif
  13113. #if WASM_ENABLE_WAMR_COMPILER != 0
  13114. module->is_bulk_memory_used = true;
  13115. #endif
  13116. break;
  13117. }
  13118. fail_zero_byte_expected:
  13119. set_error_buf(error_buf, error_buf_size,
  13120. "zero byte expected");
  13121. goto fail;
  13122. fail_unknown_memory:
  13123. set_error_buf(error_buf, error_buf_size,
  13124. "unknown memory 0");
  13125. goto fail;
  13126. fail_data_cnt_sec_require:
  13127. set_error_buf(error_buf, error_buf_size,
  13128. "data count section required");
  13129. goto fail;
  13130. #endif /* WASM_ENABLE_BULK_MEMORY */
  13131. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13132. case WASM_OP_TABLE_INIT:
  13133. {
  13134. uint8 seg_type = 0, tbl_type = 0;
  13135. #if WASM_ENABLE_GC != 0
  13136. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13137. #endif
  13138. read_leb_uint32(p, p_end, table_seg_idx);
  13139. read_leb_uint32(p, p_end, table_idx);
  13140. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13141. #if WASM_ENABLE_GC != 0
  13142. (void **)&tbl_ref_type,
  13143. #else
  13144. NULL,
  13145. #endif
  13146. error_buf, error_buf_size))
  13147. goto fail;
  13148. if (!get_table_seg_elem_type(module, table_seg_idx,
  13149. &seg_type,
  13150. #if WASM_ENABLE_GC != 0
  13151. (void **)&seg_ref_type,
  13152. #else
  13153. NULL,
  13154. #endif
  13155. error_buf, error_buf_size))
  13156. goto fail;
  13157. #if WASM_ENABLE_GC == 0
  13158. if (seg_type != tbl_type) {
  13159. set_error_buf(error_buf, error_buf_size,
  13160. "type mismatch");
  13161. goto fail;
  13162. }
  13163. #else
  13164. if (!wasm_reftype_is_subtype_of(
  13165. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13166. module->types, module->type_count)) {
  13167. set_error_buf(error_buf, error_buf_size,
  13168. "type mismatch");
  13169. goto fail;
  13170. }
  13171. #endif
  13172. #if WASM_ENABLE_FAST_INTERP != 0
  13173. emit_uint32(loader_ctx, table_seg_idx);
  13174. emit_uint32(loader_ctx, table_idx);
  13175. #endif
  13176. POP_I32();
  13177. POP_I32();
  13178. POP_I32();
  13179. #if WASM_ENABLE_WAMR_COMPILER != 0
  13180. module->is_ref_types_used = true;
  13181. #endif
  13182. break;
  13183. }
  13184. case WASM_OP_ELEM_DROP:
  13185. {
  13186. read_leb_uint32(p, p_end, table_seg_idx);
  13187. if (!get_table_seg_elem_type(module, table_seg_idx,
  13188. NULL, NULL, error_buf,
  13189. error_buf_size))
  13190. goto fail;
  13191. #if WASM_ENABLE_FAST_INTERP != 0
  13192. emit_uint32(loader_ctx, table_seg_idx);
  13193. #endif
  13194. #if WASM_ENABLE_WAMR_COMPILER != 0
  13195. module->is_ref_types_used = true;
  13196. #endif
  13197. break;
  13198. }
  13199. case WASM_OP_TABLE_COPY:
  13200. {
  13201. uint8 src_type, dst_type;
  13202. #if WASM_ENABLE_GC != 0
  13203. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13204. #endif
  13205. uint32 src_tbl_idx, dst_tbl_idx;
  13206. read_leb_uint32(p, p_end, dst_tbl_idx);
  13207. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13208. #if WASM_ENABLE_GC != 0
  13209. (void **)&dst_ref_type,
  13210. #else
  13211. NULL,
  13212. #endif
  13213. error_buf, error_buf_size))
  13214. goto fail;
  13215. read_leb_uint32(p, p_end, src_tbl_idx);
  13216. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13217. #if WASM_ENABLE_GC != 0
  13218. (void **)&src_ref_type,
  13219. #else
  13220. NULL,
  13221. #endif
  13222. error_buf, error_buf_size))
  13223. goto fail;
  13224. #if WASM_ENABLE_GC == 0
  13225. if (src_type != dst_type) {
  13226. set_error_buf(error_buf, error_buf_size,
  13227. "type mismatch");
  13228. goto fail;
  13229. }
  13230. #else
  13231. if (!wasm_reftype_is_subtype_of(
  13232. src_type, src_ref_type, dst_type, dst_ref_type,
  13233. module->types, module->type_count)) {
  13234. set_error_buf(error_buf, error_buf_size,
  13235. "type mismatch");
  13236. goto fail;
  13237. }
  13238. #endif
  13239. #if WASM_ENABLE_FAST_INTERP != 0
  13240. emit_uint32(loader_ctx, dst_tbl_idx);
  13241. emit_uint32(loader_ctx, src_tbl_idx);
  13242. #endif
  13243. POP_I32();
  13244. POP_I32();
  13245. POP_I32();
  13246. #if WASM_ENABLE_WAMR_COMPILER != 0
  13247. module->is_ref_types_used = true;
  13248. #endif
  13249. break;
  13250. }
  13251. case WASM_OP_TABLE_SIZE:
  13252. {
  13253. read_leb_uint32(p, p_end, table_idx);
  13254. /* TODO: shall we create a new function to check
  13255. table idx instead of using below function? */
  13256. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13257. error_buf, error_buf_size))
  13258. goto fail;
  13259. #if WASM_ENABLE_FAST_INTERP != 0
  13260. emit_uint32(loader_ctx, table_idx);
  13261. #endif
  13262. PUSH_I32();
  13263. #if WASM_ENABLE_WAMR_COMPILER != 0
  13264. module->is_ref_types_used = true;
  13265. #endif
  13266. break;
  13267. }
  13268. case WASM_OP_TABLE_GROW:
  13269. case WASM_OP_TABLE_FILL:
  13270. {
  13271. uint8 decl_type;
  13272. #if WASM_ENABLE_GC != 0
  13273. WASMRefType *ref_type = NULL;
  13274. #endif
  13275. read_leb_uint32(p, p_end, table_idx);
  13276. if (!get_table_elem_type(module, table_idx, &decl_type,
  13277. #if WASM_ENABLE_GC != 0
  13278. (void **)&ref_type,
  13279. #else
  13280. NULL,
  13281. #endif
  13282. error_buf, error_buf_size))
  13283. goto fail;
  13284. #if WASM_ENABLE_GC != 0
  13285. if (wasm_is_type_multi_byte_type(decl_type)) {
  13286. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13287. ref_type,
  13288. wasm_reftype_struct_size(ref_type));
  13289. }
  13290. #endif
  13291. if (opcode1 == WASM_OP_TABLE_GROW) {
  13292. if (table_idx < module->import_table_count) {
  13293. module->import_tables[table_idx]
  13294. .u.table.possible_grow = true;
  13295. }
  13296. else {
  13297. module
  13298. ->tables[table_idx
  13299. - module->import_table_count]
  13300. .possible_grow = true;
  13301. }
  13302. }
  13303. #if WASM_ENABLE_FAST_INTERP != 0
  13304. emit_uint32(loader_ctx, table_idx);
  13305. #endif
  13306. POP_I32();
  13307. #if WASM_ENABLE_FAST_INTERP != 0
  13308. POP_OFFSET_TYPE(decl_type);
  13309. #endif
  13310. POP_TYPE(decl_type);
  13311. if (opcode1 == WASM_OP_TABLE_GROW)
  13312. PUSH_I32();
  13313. else
  13314. POP_I32();
  13315. #if WASM_ENABLE_WAMR_COMPILER != 0
  13316. module->is_ref_types_used = true;
  13317. #endif
  13318. break;
  13319. }
  13320. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13321. default:
  13322. set_error_buf_v(error_buf, error_buf_size,
  13323. "%s %02x %02x", "unsupported opcode",
  13324. 0xfc, opcode1);
  13325. goto fail;
  13326. }
  13327. break;
  13328. }
  13329. #if WASM_ENABLE_SIMD != 0
  13330. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13331. case WASM_OP_SIMD_PREFIX:
  13332. {
  13333. /* TODO: memory64 offset type changes */
  13334. uint32 opcode1;
  13335. #if WASM_ENABLE_WAMR_COMPILER != 0
  13336. /* Mark the SIMD instruction is used in this module */
  13337. module->is_simd_used = true;
  13338. #endif
  13339. read_leb_uint32(p, p_end, opcode1);
  13340. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13341. */
  13342. switch (opcode1) {
  13343. /* memory instruction */
  13344. case SIMD_v128_load:
  13345. case SIMD_v128_load8x8_s:
  13346. case SIMD_v128_load8x8_u:
  13347. case SIMD_v128_load16x4_s:
  13348. case SIMD_v128_load16x4_u:
  13349. case SIMD_v128_load32x2_s:
  13350. case SIMD_v128_load32x2_u:
  13351. case SIMD_v128_load8_splat:
  13352. case SIMD_v128_load16_splat:
  13353. case SIMD_v128_load32_splat:
  13354. case SIMD_v128_load64_splat:
  13355. {
  13356. CHECK_MEMORY();
  13357. read_leb_uint32(p, p_end, align); /* align */
  13358. if (!check_simd_memory_access_align(
  13359. opcode1, align, error_buf, error_buf_size)) {
  13360. goto fail;
  13361. }
  13362. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13363. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13364. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13365. func->has_memory_operations = true;
  13366. #endif
  13367. break;
  13368. }
  13369. case SIMD_v128_store:
  13370. {
  13371. CHECK_MEMORY();
  13372. read_leb_uint32(p, p_end, align); /* align */
  13373. if (!check_simd_memory_access_align(
  13374. opcode1, align, error_buf, error_buf_size)) {
  13375. goto fail;
  13376. }
  13377. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13378. POP_V128();
  13379. POP_MEM_OFFSET();
  13380. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13381. func->has_memory_operations = true;
  13382. #endif
  13383. break;
  13384. }
  13385. /* basic operation */
  13386. case SIMD_v128_const:
  13387. {
  13388. CHECK_BUF1(p, p_end, 16);
  13389. p += 16;
  13390. PUSH_V128();
  13391. break;
  13392. }
  13393. case SIMD_v8x16_shuffle:
  13394. {
  13395. V128 mask;
  13396. CHECK_BUF1(p, p_end, 16);
  13397. mask = read_i8x16(p, error_buf, error_buf_size);
  13398. p += 16;
  13399. if (!check_simd_shuffle_mask(mask, error_buf,
  13400. error_buf_size)) {
  13401. goto fail;
  13402. }
  13403. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13404. break;
  13405. }
  13406. case SIMD_v8x16_swizzle:
  13407. {
  13408. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13409. break;
  13410. }
  13411. /* splat operation */
  13412. case SIMD_i8x16_splat:
  13413. case SIMD_i16x8_splat:
  13414. case SIMD_i32x4_splat:
  13415. case SIMD_i64x2_splat:
  13416. case SIMD_f32x4_splat:
  13417. case SIMD_f64x2_splat:
  13418. {
  13419. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13420. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13421. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13422. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13423. VALUE_TYPE_V128);
  13424. break;
  13425. }
  13426. /* lane operation */
  13427. case SIMD_i8x16_extract_lane_s:
  13428. case SIMD_i8x16_extract_lane_u:
  13429. case SIMD_i8x16_replace_lane:
  13430. case SIMD_i16x8_extract_lane_s:
  13431. case SIMD_i16x8_extract_lane_u:
  13432. case SIMD_i16x8_replace_lane:
  13433. case SIMD_i32x4_extract_lane:
  13434. case SIMD_i32x4_replace_lane:
  13435. case SIMD_i64x2_extract_lane:
  13436. case SIMD_i64x2_replace_lane:
  13437. case SIMD_f32x4_extract_lane:
  13438. case SIMD_f32x4_replace_lane:
  13439. case SIMD_f64x2_extract_lane:
  13440. case SIMD_f64x2_replace_lane:
  13441. {
  13442. uint8 lane;
  13443. /* clang-format off */
  13444. uint8 replace[] = {
  13445. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13446. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13447. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13448. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13449. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13450. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13451. };
  13452. uint8 push_type[] = {
  13453. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13454. VALUE_TYPE_V128,
  13455. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13456. VALUE_TYPE_V128,
  13457. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13458. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13459. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13460. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13461. };
  13462. /* clang-format on */
  13463. CHECK_BUF(p, p_end, 1);
  13464. lane = read_uint8(p);
  13465. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13466. error_buf_size)) {
  13467. goto fail;
  13468. }
  13469. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13470. if (!(wasm_loader_pop_frame_ref(
  13471. loader_ctx,
  13472. replace[opcode1
  13473. - SIMD_i8x16_extract_lane_s],
  13474. error_buf, error_buf_size)))
  13475. goto fail;
  13476. }
  13477. POP_AND_PUSH(
  13478. VALUE_TYPE_V128,
  13479. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13480. break;
  13481. }
  13482. /* i8x16 compare operation */
  13483. case SIMD_i8x16_eq:
  13484. case SIMD_i8x16_ne:
  13485. case SIMD_i8x16_lt_s:
  13486. case SIMD_i8x16_lt_u:
  13487. case SIMD_i8x16_gt_s:
  13488. case SIMD_i8x16_gt_u:
  13489. case SIMD_i8x16_le_s:
  13490. case SIMD_i8x16_le_u:
  13491. case SIMD_i8x16_ge_s:
  13492. case SIMD_i8x16_ge_u:
  13493. /* i16x8 compare operation */
  13494. case SIMD_i16x8_eq:
  13495. case SIMD_i16x8_ne:
  13496. case SIMD_i16x8_lt_s:
  13497. case SIMD_i16x8_lt_u:
  13498. case SIMD_i16x8_gt_s:
  13499. case SIMD_i16x8_gt_u:
  13500. case SIMD_i16x8_le_s:
  13501. case SIMD_i16x8_le_u:
  13502. case SIMD_i16x8_ge_s:
  13503. case SIMD_i16x8_ge_u:
  13504. /* i32x4 compare operation */
  13505. case SIMD_i32x4_eq:
  13506. case SIMD_i32x4_ne:
  13507. case SIMD_i32x4_lt_s:
  13508. case SIMD_i32x4_lt_u:
  13509. case SIMD_i32x4_gt_s:
  13510. case SIMD_i32x4_gt_u:
  13511. case SIMD_i32x4_le_s:
  13512. case SIMD_i32x4_le_u:
  13513. case SIMD_i32x4_ge_s:
  13514. case SIMD_i32x4_ge_u:
  13515. /* f32x4 compare operation */
  13516. case SIMD_f32x4_eq:
  13517. case SIMD_f32x4_ne:
  13518. case SIMD_f32x4_lt:
  13519. case SIMD_f32x4_gt:
  13520. case SIMD_f32x4_le:
  13521. case SIMD_f32x4_ge:
  13522. /* f64x2 compare operation */
  13523. case SIMD_f64x2_eq:
  13524. case SIMD_f64x2_ne:
  13525. case SIMD_f64x2_lt:
  13526. case SIMD_f64x2_gt:
  13527. case SIMD_f64x2_le:
  13528. case SIMD_f64x2_ge:
  13529. {
  13530. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13531. break;
  13532. }
  13533. /* v128 operation */
  13534. case SIMD_v128_not:
  13535. {
  13536. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13537. break;
  13538. }
  13539. case SIMD_v128_and:
  13540. case SIMD_v128_andnot:
  13541. case SIMD_v128_or:
  13542. case SIMD_v128_xor:
  13543. {
  13544. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13545. break;
  13546. }
  13547. case SIMD_v128_bitselect:
  13548. {
  13549. POP_V128();
  13550. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13551. break;
  13552. }
  13553. case SIMD_v128_any_true:
  13554. {
  13555. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13556. break;
  13557. }
  13558. /* Load Lane Operation */
  13559. case SIMD_v128_load8_lane:
  13560. case SIMD_v128_load16_lane:
  13561. case SIMD_v128_load32_lane:
  13562. case SIMD_v128_load64_lane:
  13563. case SIMD_v128_store8_lane:
  13564. case SIMD_v128_store16_lane:
  13565. case SIMD_v128_store32_lane:
  13566. case SIMD_v128_store64_lane:
  13567. {
  13568. uint8 lane;
  13569. CHECK_MEMORY();
  13570. read_leb_uint32(p, p_end, align); /* align */
  13571. if (!check_simd_memory_access_align(
  13572. opcode1, align, error_buf, error_buf_size)) {
  13573. goto fail;
  13574. }
  13575. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13576. CHECK_BUF(p, p_end, 1);
  13577. lane = read_uint8(p);
  13578. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13579. error_buf_size)) {
  13580. goto fail;
  13581. }
  13582. POP_V128();
  13583. POP_MEM_OFFSET();
  13584. if (opcode1 < SIMD_v128_store8_lane) {
  13585. PUSH_V128();
  13586. }
  13587. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13588. func->has_memory_operations = true;
  13589. #endif
  13590. break;
  13591. }
  13592. case SIMD_v128_load32_zero:
  13593. case SIMD_v128_load64_zero:
  13594. {
  13595. CHECK_MEMORY();
  13596. read_leb_uint32(p, p_end, align); /* align */
  13597. if (!check_simd_memory_access_align(
  13598. opcode1, align, error_buf, error_buf_size)) {
  13599. goto fail;
  13600. }
  13601. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13602. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_V128);
  13603. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13604. func->has_memory_operations = true;
  13605. #endif
  13606. break;
  13607. }
  13608. /* Float conversion */
  13609. case SIMD_f32x4_demote_f64x2_zero:
  13610. case SIMD_f64x2_promote_low_f32x4_zero:
  13611. {
  13612. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13613. break;
  13614. }
  13615. /* i8x16 Operation */
  13616. case SIMD_i8x16_abs:
  13617. case SIMD_i8x16_neg:
  13618. case SIMD_i8x16_popcnt:
  13619. {
  13620. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13621. break;
  13622. }
  13623. case SIMD_i8x16_all_true:
  13624. case SIMD_i8x16_bitmask:
  13625. {
  13626. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13627. break;
  13628. }
  13629. case SIMD_i8x16_narrow_i16x8_s:
  13630. case SIMD_i8x16_narrow_i16x8_u:
  13631. {
  13632. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13633. break;
  13634. }
  13635. case SIMD_f32x4_ceil:
  13636. case SIMD_f32x4_floor:
  13637. case SIMD_f32x4_trunc:
  13638. case SIMD_f32x4_nearest:
  13639. {
  13640. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13641. break;
  13642. }
  13643. case SIMD_i8x16_shl:
  13644. case SIMD_i8x16_shr_s:
  13645. case SIMD_i8x16_shr_u:
  13646. {
  13647. POP_I32();
  13648. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13649. break;
  13650. }
  13651. case SIMD_i8x16_add:
  13652. case SIMD_i8x16_add_sat_s:
  13653. case SIMD_i8x16_add_sat_u:
  13654. case SIMD_i8x16_sub:
  13655. case SIMD_i8x16_sub_sat_s:
  13656. case SIMD_i8x16_sub_sat_u:
  13657. {
  13658. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13659. break;
  13660. }
  13661. case SIMD_f64x2_ceil:
  13662. case SIMD_f64x2_floor:
  13663. {
  13664. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13665. break;
  13666. }
  13667. case SIMD_i8x16_min_s:
  13668. case SIMD_i8x16_min_u:
  13669. case SIMD_i8x16_max_s:
  13670. case SIMD_i8x16_max_u:
  13671. {
  13672. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13673. break;
  13674. }
  13675. case SIMD_f64x2_trunc:
  13676. {
  13677. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13678. break;
  13679. }
  13680. case SIMD_i8x16_avgr_u:
  13681. {
  13682. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13683. break;
  13684. }
  13685. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13686. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13687. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13688. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13689. /* i16x8 operation */
  13690. case SIMD_i16x8_abs:
  13691. case SIMD_i16x8_neg:
  13692. {
  13693. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13694. break;
  13695. }
  13696. case SIMD_i16x8_q15mulr_sat_s:
  13697. {
  13698. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13699. break;
  13700. }
  13701. case SIMD_i16x8_all_true:
  13702. case SIMD_i16x8_bitmask:
  13703. {
  13704. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13705. break;
  13706. }
  13707. case SIMD_i16x8_narrow_i32x4_s:
  13708. case SIMD_i16x8_narrow_i32x4_u:
  13709. {
  13710. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13711. break;
  13712. }
  13713. case SIMD_i16x8_extend_low_i8x16_s:
  13714. case SIMD_i16x8_extend_high_i8x16_s:
  13715. case SIMD_i16x8_extend_low_i8x16_u:
  13716. case SIMD_i16x8_extend_high_i8x16_u:
  13717. {
  13718. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13719. break;
  13720. }
  13721. case SIMD_i16x8_shl:
  13722. case SIMD_i16x8_shr_s:
  13723. case SIMD_i16x8_shr_u:
  13724. {
  13725. POP_I32();
  13726. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13727. break;
  13728. }
  13729. case SIMD_i16x8_add:
  13730. case SIMD_i16x8_add_sat_s:
  13731. case SIMD_i16x8_add_sat_u:
  13732. case SIMD_i16x8_sub:
  13733. case SIMD_i16x8_sub_sat_s:
  13734. case SIMD_i16x8_sub_sat_u:
  13735. {
  13736. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13737. break;
  13738. }
  13739. case SIMD_f64x2_nearest:
  13740. {
  13741. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13742. break;
  13743. }
  13744. case SIMD_i16x8_mul:
  13745. case SIMD_i16x8_min_s:
  13746. case SIMD_i16x8_min_u:
  13747. case SIMD_i16x8_max_s:
  13748. case SIMD_i16x8_max_u:
  13749. case SIMD_i16x8_avgr_u:
  13750. case SIMD_i16x8_extmul_low_i8x16_s:
  13751. case SIMD_i16x8_extmul_high_i8x16_s:
  13752. case SIMD_i16x8_extmul_low_i8x16_u:
  13753. case SIMD_i16x8_extmul_high_i8x16_u:
  13754. {
  13755. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13756. break;
  13757. }
  13758. /* i32x4 operation */
  13759. case SIMD_i32x4_abs:
  13760. case SIMD_i32x4_neg:
  13761. {
  13762. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13763. break;
  13764. }
  13765. case SIMD_i32x4_all_true:
  13766. case SIMD_i32x4_bitmask:
  13767. {
  13768. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13769. break;
  13770. }
  13771. case SIMD_i32x4_extend_low_i16x8_s:
  13772. case SIMD_i32x4_extend_high_i16x8_s:
  13773. case SIMD_i32x4_extend_low_i16x8_u:
  13774. case SIMD_i32x4_extend_high_i16x8_u:
  13775. {
  13776. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13777. break;
  13778. }
  13779. case SIMD_i32x4_shl:
  13780. case SIMD_i32x4_shr_s:
  13781. case SIMD_i32x4_shr_u:
  13782. {
  13783. POP_I32();
  13784. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13785. break;
  13786. }
  13787. case SIMD_i32x4_add:
  13788. case SIMD_i32x4_sub:
  13789. case SIMD_i32x4_mul:
  13790. case SIMD_i32x4_min_s:
  13791. case SIMD_i32x4_min_u:
  13792. case SIMD_i32x4_max_s:
  13793. case SIMD_i32x4_max_u:
  13794. case SIMD_i32x4_dot_i16x8_s:
  13795. case SIMD_i32x4_extmul_low_i16x8_s:
  13796. case SIMD_i32x4_extmul_high_i16x8_s:
  13797. case SIMD_i32x4_extmul_low_i16x8_u:
  13798. case SIMD_i32x4_extmul_high_i16x8_u:
  13799. {
  13800. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13801. break;
  13802. }
  13803. /* i64x2 operation */
  13804. case SIMD_i64x2_abs:
  13805. case SIMD_i64x2_neg:
  13806. {
  13807. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13808. break;
  13809. }
  13810. case SIMD_i64x2_all_true:
  13811. case SIMD_i64x2_bitmask:
  13812. {
  13813. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13814. break;
  13815. }
  13816. case SIMD_i64x2_extend_low_i32x4_s:
  13817. case SIMD_i64x2_extend_high_i32x4_s:
  13818. case SIMD_i64x2_extend_low_i32x4_u:
  13819. case SIMD_i64x2_extend_high_i32x4_u:
  13820. {
  13821. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13822. break;
  13823. }
  13824. case SIMD_i64x2_shl:
  13825. case SIMD_i64x2_shr_s:
  13826. case SIMD_i64x2_shr_u:
  13827. {
  13828. POP_I32();
  13829. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13830. break;
  13831. }
  13832. case SIMD_i64x2_add:
  13833. case SIMD_i64x2_sub:
  13834. case SIMD_i64x2_mul:
  13835. case SIMD_i64x2_eq:
  13836. case SIMD_i64x2_ne:
  13837. case SIMD_i64x2_lt_s:
  13838. case SIMD_i64x2_gt_s:
  13839. case SIMD_i64x2_le_s:
  13840. case SIMD_i64x2_ge_s:
  13841. case SIMD_i64x2_extmul_low_i32x4_s:
  13842. case SIMD_i64x2_extmul_high_i32x4_s:
  13843. case SIMD_i64x2_extmul_low_i32x4_u:
  13844. case SIMD_i64x2_extmul_high_i32x4_u:
  13845. {
  13846. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13847. break;
  13848. }
  13849. /* f32x4 operation */
  13850. case SIMD_f32x4_abs:
  13851. case SIMD_f32x4_neg:
  13852. case SIMD_f32x4_sqrt:
  13853. {
  13854. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13855. break;
  13856. }
  13857. case SIMD_f32x4_add:
  13858. case SIMD_f32x4_sub:
  13859. case SIMD_f32x4_mul:
  13860. case SIMD_f32x4_div:
  13861. case SIMD_f32x4_min:
  13862. case SIMD_f32x4_max:
  13863. case SIMD_f32x4_pmin:
  13864. case SIMD_f32x4_pmax:
  13865. {
  13866. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13867. break;
  13868. }
  13869. /* f64x2 operation */
  13870. case SIMD_f64x2_abs:
  13871. case SIMD_f64x2_neg:
  13872. case SIMD_f64x2_sqrt:
  13873. {
  13874. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13875. break;
  13876. }
  13877. case SIMD_f64x2_add:
  13878. case SIMD_f64x2_sub:
  13879. case SIMD_f64x2_mul:
  13880. case SIMD_f64x2_div:
  13881. case SIMD_f64x2_min:
  13882. case SIMD_f64x2_max:
  13883. case SIMD_f64x2_pmin:
  13884. case SIMD_f64x2_pmax:
  13885. {
  13886. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13887. break;
  13888. }
  13889. case SIMD_i32x4_trunc_sat_f32x4_s:
  13890. case SIMD_i32x4_trunc_sat_f32x4_u:
  13891. case SIMD_f32x4_convert_i32x4_s:
  13892. case SIMD_f32x4_convert_i32x4_u:
  13893. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13894. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13895. case SIMD_f64x2_convert_low_i32x4_s:
  13896. case SIMD_f64x2_convert_low_i32x4_u:
  13897. {
  13898. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13899. break;
  13900. }
  13901. default:
  13902. {
  13903. if (error_buf != NULL) {
  13904. snprintf(error_buf, error_buf_size,
  13905. "WASM module load failed: "
  13906. "invalid opcode 0xfd %02x.",
  13907. opcode1);
  13908. }
  13909. goto fail;
  13910. }
  13911. }
  13912. break;
  13913. }
  13914. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  13915. #endif /* end of WASM_ENABLE_SIMD */
  13916. #if WASM_ENABLE_SHARED_MEMORY != 0
  13917. case WASM_OP_ATOMIC_PREFIX:
  13918. {
  13919. uint32 opcode1;
  13920. read_leb_uint32(p, p_end, opcode1);
  13921. #if WASM_ENABLE_FAST_INTERP != 0
  13922. emit_byte(loader_ctx, opcode1);
  13923. #endif
  13924. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  13925. CHECK_MEMORY();
  13926. read_leb_uint32(p, p_end, align); /* align */
  13927. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13928. if (!check_memory_align_equal(opcode1, align, error_buf,
  13929. error_buf_size)) {
  13930. goto fail;
  13931. }
  13932. #if WASM_ENABLE_FAST_INTERP != 0
  13933. emit_uint32(loader_ctx, mem_offset);
  13934. #endif
  13935. }
  13936. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13937. func->has_memory_operations = true;
  13938. #endif
  13939. switch (opcode1) {
  13940. case WASM_OP_ATOMIC_NOTIFY:
  13941. POP_I32();
  13942. POP_MEM_OFFSET();
  13943. PUSH_I32();
  13944. break;
  13945. case WASM_OP_ATOMIC_WAIT32:
  13946. POP_I64();
  13947. POP_I32();
  13948. POP_MEM_OFFSET();
  13949. PUSH_I32();
  13950. break;
  13951. case WASM_OP_ATOMIC_WAIT64:
  13952. POP_I64();
  13953. POP_I64();
  13954. POP_MEM_OFFSET();
  13955. PUSH_I32();
  13956. break;
  13957. case WASM_OP_ATOMIC_FENCE:
  13958. /* reserved byte 0x00 */
  13959. if (*p++ != 0x00) {
  13960. set_error_buf(error_buf, error_buf_size,
  13961. "zero byte expected");
  13962. goto fail;
  13963. }
  13964. break;
  13965. case WASM_OP_ATOMIC_I32_LOAD:
  13966. case WASM_OP_ATOMIC_I32_LOAD8_U:
  13967. case WASM_OP_ATOMIC_I32_LOAD16_U:
  13968. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  13969. break;
  13970. case WASM_OP_ATOMIC_I32_STORE:
  13971. case WASM_OP_ATOMIC_I32_STORE8:
  13972. case WASM_OP_ATOMIC_I32_STORE16:
  13973. POP_I32();
  13974. POP_MEM_OFFSET();
  13975. break;
  13976. case WASM_OP_ATOMIC_I64_LOAD:
  13977. case WASM_OP_ATOMIC_I64_LOAD8_U:
  13978. case WASM_OP_ATOMIC_I64_LOAD16_U:
  13979. case WASM_OP_ATOMIC_I64_LOAD32_U:
  13980. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  13981. break;
  13982. case WASM_OP_ATOMIC_I64_STORE:
  13983. case WASM_OP_ATOMIC_I64_STORE8:
  13984. case WASM_OP_ATOMIC_I64_STORE16:
  13985. case WASM_OP_ATOMIC_I64_STORE32:
  13986. POP_I64();
  13987. POP_MEM_OFFSET();
  13988. break;
  13989. case WASM_OP_ATOMIC_RMW_I32_ADD:
  13990. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  13991. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  13992. case WASM_OP_ATOMIC_RMW_I32_SUB:
  13993. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  13994. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  13995. case WASM_OP_ATOMIC_RMW_I32_AND:
  13996. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  13997. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  13998. case WASM_OP_ATOMIC_RMW_I32_OR:
  13999. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  14000. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  14001. case WASM_OP_ATOMIC_RMW_I32_XOR:
  14002. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  14003. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  14004. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  14005. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14006. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14007. POP_I32();
  14008. POP_MEM_OFFSET();
  14009. PUSH_I32();
  14010. break;
  14011. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14012. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14013. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14014. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14015. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14016. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14017. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14018. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14019. case WASM_OP_ATOMIC_RMW_I64_AND:
  14020. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14021. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14022. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14023. case WASM_OP_ATOMIC_RMW_I64_OR:
  14024. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14025. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14026. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14027. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14028. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14029. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14030. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14031. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14032. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14033. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14034. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14035. POP_I64();
  14036. POP_MEM_OFFSET();
  14037. PUSH_I64();
  14038. break;
  14039. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14040. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14041. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14042. POP_I32();
  14043. POP_I32();
  14044. POP_MEM_OFFSET();
  14045. PUSH_I32();
  14046. break;
  14047. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14048. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14049. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14050. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14051. POP_I64();
  14052. POP_I64();
  14053. POP_MEM_OFFSET();
  14054. PUSH_I64();
  14055. break;
  14056. default:
  14057. set_error_buf_v(error_buf, error_buf_size,
  14058. "%s %02x %02x", "unsupported opcode",
  14059. 0xfe, opcode1);
  14060. goto fail;
  14061. }
  14062. break;
  14063. }
  14064. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14065. default:
  14066. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14067. "unsupported opcode", opcode);
  14068. goto fail;
  14069. }
  14070. #if WASM_ENABLE_FAST_INTERP != 0
  14071. last_op = opcode;
  14072. #endif
  14073. }
  14074. if (loader_ctx->csp_num > 0) {
  14075. if (cur_func_idx < module->function_count - 1)
  14076. /* Function with missing end marker (between two functions) */
  14077. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14078. else
  14079. /* Function with missing end marker
  14080. (at EOF or end of code sections) */
  14081. set_error_buf(error_buf, error_buf_size,
  14082. "unexpected end of section or function, "
  14083. "or section size mismatch");
  14084. goto fail;
  14085. }
  14086. #if WASM_ENABLE_FAST_INTERP != 0
  14087. if (loader_ctx->p_code_compiled == NULL)
  14088. goto re_scan;
  14089. func->const_cell_num = loader_ctx->const_cell_num;
  14090. if (func->const_cell_num > 0) {
  14091. int32 j;
  14092. if (!(func->consts = func_const = loader_malloc(
  14093. func->const_cell_num * 4, error_buf, error_buf_size)))
  14094. goto fail;
  14095. func_const_end = func->consts + func->const_cell_num * 4;
  14096. /* reverse the const buf */
  14097. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14098. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14099. if (c->value_type == VALUE_TYPE_F64
  14100. || c->value_type == VALUE_TYPE_I64) {
  14101. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14102. &(c->value.f64), (uint32)sizeof(int64));
  14103. func_const += sizeof(int64);
  14104. }
  14105. else {
  14106. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14107. &(c->value.f32), (uint32)sizeof(int32));
  14108. func_const += sizeof(int32);
  14109. }
  14110. }
  14111. }
  14112. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14113. - loader_ctx->start_dynamic_offset + 1;
  14114. #else
  14115. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14116. #endif
  14117. func->max_block_num = loader_ctx->max_csp_num;
  14118. return_value = true;
  14119. fail:
  14120. wasm_loader_ctx_destroy(loader_ctx);
  14121. (void)table_idx;
  14122. (void)table_seg_idx;
  14123. (void)data_seg_idx;
  14124. (void)i64_const;
  14125. (void)local_offset;
  14126. (void)p_org;
  14127. (void)mem_offset;
  14128. (void)align;
  14129. return return_value;
  14130. }