wasm_loader.c 574 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643
  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 "../common/wasm_native.h"
  12. #include "../common/wasm_memory.h"
  13. #if WASM_ENABLE_GC != 0
  14. #include "../common/gc/gc_type.h"
  15. #include "../common/gc/gc_object.h"
  16. #endif
  17. #if WASM_ENABLE_DEBUG_INTERP != 0
  18. #include "../libraries/debug-engine/debug_engine.h"
  19. #endif
  20. #if WASM_ENABLE_FAST_JIT != 0
  21. #include "../fast-jit/jit_compiler.h"
  22. #include "../fast-jit/jit_codecache.h"
  23. #endif
  24. #if WASM_ENABLE_JIT != 0
  25. #include "../compilation/aot_llvm.h"
  26. #endif
  27. #ifndef TRACE_WASM_LOADER
  28. #define TRACE_WASM_LOADER 0
  29. #endif
  30. /* Read a value of given type from the address pointed to by the given
  31. pointer and increase the pointer to the position just after the
  32. value being read. */
  33. #define TEMPLATE_READ_VALUE(Type, p) \
  34. (p += sizeof(Type), *(Type *)(p - sizeof(Type)))
  35. #if WASM_ENABLE_MEMORY64 != 0
  36. static bool
  37. has_module_memory64(WASMModule *module)
  38. {
  39. /* TODO: multi-memories for now assuming the memory idx type is consistent
  40. * across multi-memories */
  41. if (module->import_memory_count > 0)
  42. return !!(module->import_memories[0].u.memory.flags & MEMORY64_FLAG);
  43. else if (module->memory_count > 0)
  44. return !!(module->memories[0].flags & MEMORY64_FLAG);
  45. return false;
  46. }
  47. #endif
  48. static void
  49. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  50. {
  51. if (error_buf != NULL) {
  52. snprintf(error_buf, error_buf_size, "WASM module load failed: %s",
  53. string);
  54. }
  55. }
  56. #if WASM_ENABLE_MEMORY64 != 0
  57. static void
  58. set_error_buf_mem_offset_out_of_range(char *error_buf, uint32 error_buf_size)
  59. {
  60. if (error_buf != NULL) {
  61. snprintf(error_buf, error_buf_size, "offset out of range");
  62. }
  63. }
  64. #endif
  65. static void
  66. set_error_buf_v(char *error_buf, uint32 error_buf_size, const char *format, ...)
  67. {
  68. va_list args;
  69. char buf[128];
  70. if (error_buf != NULL) {
  71. va_start(args, format);
  72. vsnprintf(buf, sizeof(buf), format, args);
  73. va_end(args);
  74. snprintf(error_buf, error_buf_size, "WASM module load failed: %s", buf);
  75. }
  76. }
  77. static bool
  78. check_buf(const uint8 *buf, const uint8 *buf_end, uint32 length,
  79. char *error_buf, uint32 error_buf_size)
  80. {
  81. if ((uintptr_t)buf + length < (uintptr_t)buf
  82. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  83. set_error_buf(error_buf, error_buf_size,
  84. "unexpected end of section or function");
  85. return false;
  86. }
  87. return true;
  88. }
  89. static bool
  90. check_buf1(const uint8 *buf, const uint8 *buf_end, uint32 length,
  91. char *error_buf, uint32 error_buf_size)
  92. {
  93. if ((uintptr_t)buf + length < (uintptr_t)buf
  94. || (uintptr_t)buf + length > (uintptr_t)buf_end) {
  95. set_error_buf(error_buf, error_buf_size, "unexpected end");
  96. return false;
  97. }
  98. return true;
  99. }
  100. #define CHECK_BUF(buf, buf_end, length) \
  101. do { \
  102. if (!check_buf(buf, buf_end, length, error_buf, error_buf_size)) { \
  103. goto fail; \
  104. } \
  105. } while (0)
  106. #define CHECK_BUF1(buf, buf_end, length) \
  107. do { \
  108. if (!check_buf1(buf, buf_end, length, error_buf, error_buf_size)) { \
  109. goto fail; \
  110. } \
  111. } while (0)
  112. #define skip_leb(p) while (*p++ & 0x80)
  113. #define skip_leb_int64(p, p_end) skip_leb(p)
  114. #define skip_leb_uint32(p, p_end) skip_leb(p)
  115. #define skip_leb_int32(p, p_end) skip_leb(p)
  116. #define skip_leb_mem_offset(p, p_end) skip_leb(p)
  117. static bool
  118. read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
  119. uint64 *p_result, char *error_buf, uint32 error_buf_size)
  120. {
  121. const uint8 *buf = *p_buf;
  122. uint64 result = 0;
  123. uint32 shift = 0;
  124. uint32 offset = 0, bcnt = 0;
  125. uint64 byte;
  126. while (true) {
  127. /* uN or SN must not exceed ceil(N/7) bytes */
  128. if (bcnt + 1 > (maxbits + 6) / 7) {
  129. set_error_buf(error_buf, error_buf_size,
  130. "integer representation too long");
  131. return false;
  132. }
  133. CHECK_BUF(buf, buf_end, offset + 1);
  134. byte = buf[offset];
  135. offset += 1;
  136. result |= ((byte & 0x7f) << shift);
  137. shift += 7;
  138. bcnt += 1;
  139. if ((byte & 0x80) == 0) {
  140. break;
  141. }
  142. }
  143. if (!sign && maxbits == 32 && shift >= maxbits) {
  144. /* The top bits set represent values > 32 bits */
  145. if (((uint8)byte) & 0xf0)
  146. goto fail_integer_too_large;
  147. }
  148. else if (sign && maxbits == 32) {
  149. if (shift < maxbits) {
  150. /* Sign extend, second-highest bit is the sign bit */
  151. if ((uint8)byte & 0x40)
  152. result |= (~((uint64)0)) << shift;
  153. }
  154. else {
  155. /* The top bits should be a sign-extension of the sign bit */
  156. bool sign_bit_set = ((uint8)byte) & 0x8;
  157. int top_bits = ((uint8)byte) & 0xf0;
  158. if ((sign_bit_set && top_bits != 0x70)
  159. || (!sign_bit_set && top_bits != 0))
  160. goto fail_integer_too_large;
  161. }
  162. }
  163. else if (sign && maxbits == 64) {
  164. if (shift < maxbits) {
  165. /* Sign extend, second-highest bit is the sign bit */
  166. if ((uint8)byte & 0x40)
  167. result |= (~((uint64)0)) << shift;
  168. }
  169. else {
  170. /* The top bits should be a sign-extension of the sign bit */
  171. bool sign_bit_set = ((uint8)byte) & 0x1;
  172. int top_bits = ((uint8)byte) & 0xfe;
  173. if ((sign_bit_set && top_bits != 0x7e)
  174. || (!sign_bit_set && top_bits != 0))
  175. goto fail_integer_too_large;
  176. }
  177. }
  178. *p_buf += offset;
  179. *p_result = result;
  180. return true;
  181. fail_integer_too_large:
  182. set_error_buf(error_buf, error_buf_size, "integer too large");
  183. fail:
  184. return false;
  185. }
  186. #define read_uint8(p) TEMPLATE_READ_VALUE(uint8, p)
  187. #define read_uint32(p) TEMPLATE_READ_VALUE(uint32, p)
  188. #define read_leb_int64(p, p_end, res) \
  189. do { \
  190. uint64 res64; \
  191. if (!read_leb((uint8 **)&p, p_end, 64, true, &res64, error_buf, \
  192. error_buf_size)) \
  193. goto fail; \
  194. res = (int64)res64; \
  195. } while (0)
  196. #if WASM_ENABLE_MEMORY64 != 0
  197. #define read_leb_mem_offset(p, p_end, res) \
  198. do { \
  199. uint64 res64; \
  200. if (!read_leb((uint8 **)&p, p_end, is_memory64 ? 64 : 32, false, \
  201. &res64, error_buf, error_buf_size)) { \
  202. set_error_buf_mem_offset_out_of_range(error_buf, error_buf_size); \
  203. goto fail; \
  204. } \
  205. res = (mem_offset_t)res64; \
  206. } while (0)
  207. #else
  208. #define read_leb_mem_offset(p, p_end, res) read_leb_uint32(p, p_end, res)
  209. #endif
  210. #define read_leb_uint32(p, p_end, res) \
  211. do { \
  212. uint64 res64; \
  213. if (!read_leb((uint8 **)&p, p_end, 32, false, &res64, error_buf, \
  214. error_buf_size)) \
  215. goto fail; \
  216. res = (uint32)res64; \
  217. } while (0)
  218. #define read_leb_int32(p, p_end, res) \
  219. do { \
  220. uint64 res64; \
  221. if (!read_leb((uint8 **)&p, p_end, 32, true, &res64, error_buf, \
  222. error_buf_size)) \
  223. goto fail; \
  224. res = (int32)res64; \
  225. } while (0)
  226. static char *
  227. type2str(uint8 type)
  228. {
  229. char *type_str[] = { "v128", "f64", "f32", "i64", "i32" };
  230. #if WASM_ENABLE_GC != 0
  231. char *type_str_ref[] = { "stringview_iter",
  232. "stringview_wtf16",
  233. "(ref null ht)",
  234. "(ref ht)",
  235. "", /* reserved */
  236. "stringview_wtf8",
  237. "stringref",
  238. "", /* reserved */
  239. "", /* reserved */
  240. "arrayref",
  241. "structref",
  242. "i32ref",
  243. "eqref",
  244. "anyref",
  245. "externref",
  246. "funcref",
  247. "nullref",
  248. "nullexternref",
  249. "nullfuncref" };
  250. #endif
  251. if (type >= VALUE_TYPE_V128 && type <= VALUE_TYPE_I32)
  252. return type_str[type - VALUE_TYPE_V128];
  253. #if WASM_ENABLE_GC != 0
  254. else if (wasm_is_type_reftype(type))
  255. return type_str_ref[type - REF_TYPE_STRINGVIEWITER];
  256. #endif
  257. else if (type == VALUE_TYPE_FUNCREF)
  258. return "funcref";
  259. else if (type == VALUE_TYPE_EXTERNREF)
  260. return "externref";
  261. else
  262. return "unknown type";
  263. }
  264. static bool
  265. is_32bit_type(uint8 type)
  266. {
  267. if (type == VALUE_TYPE_I32 || type == VALUE_TYPE_F32
  268. #if WASM_ENABLE_GC != 0
  269. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  270. #elif WASM_ENABLE_REF_TYPES != 0
  271. /* For reference types, we use uint32 index to represent
  272. the funcref and externref */
  273. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  274. #endif
  275. )
  276. return true;
  277. return false;
  278. }
  279. static bool
  280. is_64bit_type(uint8 type)
  281. {
  282. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  283. #if WASM_ENABLE_GC != 0
  284. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  285. #endif
  286. )
  287. return true;
  288. return false;
  289. }
  290. static bool
  291. is_value_type(uint8 type)
  292. {
  293. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  294. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  295. #if WASM_ENABLE_GC != 0
  296. /* reference types, 0x65 to 0x70 */
  297. || wasm_is_type_reftype(type)
  298. #elif WASM_ENABLE_REF_TYPES != 0
  299. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  300. #endif
  301. #if WASM_ENABLE_SIMD != 0
  302. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  303. || type == VALUE_TYPE_V128 /* 0x7B */
  304. #endif
  305. #endif
  306. )
  307. return true;
  308. return false;
  309. }
  310. #if WASM_ENABLE_GC != 0
  311. static bool
  312. is_packed_type(uint8 type)
  313. {
  314. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  315. }
  316. #endif
  317. static bool
  318. is_byte_a_type(uint8 type)
  319. {
  320. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  321. }
  322. #if WASM_ENABLE_SIMD != 0
  323. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  324. static V128
  325. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  326. {
  327. V128 result;
  328. uint8 i;
  329. for (i = 0; i != 16; ++i) {
  330. result.i8x16[i] = read_uint8(p_buf);
  331. }
  332. return result;
  333. }
  334. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  335. #endif /* end of WASM_ENABLE_SIMD */
  336. static void *
  337. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  338. {
  339. void *mem;
  340. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  341. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  342. return NULL;
  343. }
  344. memset(mem, 0, (uint32)size);
  345. return mem;
  346. }
  347. static void *
  348. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  349. uint32 error_buf_size)
  350. {
  351. uint8 *mem_new;
  352. bh_assert(size_new > size_old);
  353. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  354. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  355. memset(mem_new + size_old, 0, size_new - size_old);
  356. wasm_runtime_free(mem_old);
  357. }
  358. return mem_new;
  359. }
  360. #define MEM_REALLOC(mem, size_old, size_new) \
  361. do { \
  362. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  363. error_buf_size); \
  364. if (!mem_new) \
  365. goto fail; \
  366. mem = mem_new; \
  367. } while (0)
  368. #if WASM_ENABLE_GC != 0
  369. static bool
  370. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  371. char *error_buf, uint32 error_buf_size)
  372. {
  373. if (type_index >= type_count) {
  374. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  375. type_index);
  376. return false;
  377. }
  378. return true;
  379. }
  380. static bool
  381. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  382. uint32 error_buf_size)
  383. {
  384. if (!check_type_index(module, module->type_count, type_index, error_buf,
  385. error_buf_size)) {
  386. return false;
  387. }
  388. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  389. set_error_buf(error_buf, error_buf_size, "unkown array type");
  390. return false;
  391. }
  392. return true;
  393. }
  394. #endif
  395. static bool
  396. check_function_index(const WASMModule *module, uint32 function_index,
  397. char *error_buf, uint32 error_buf_size)
  398. {
  399. if (function_index
  400. >= module->import_function_count + module->function_count) {
  401. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  402. function_index);
  403. return false;
  404. }
  405. return true;
  406. }
  407. typedef struct InitValue {
  408. uint8 type;
  409. uint8 flag;
  410. #if WASM_ENABLE_GC != 0
  411. uint8 gc_opcode;
  412. WASMRefType ref_type;
  413. #endif
  414. WASMValue value;
  415. } InitValue;
  416. typedef struct ConstExprContext {
  417. uint32 sp;
  418. uint32 size;
  419. WASMModule *module;
  420. InitValue *stack;
  421. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  422. } ConstExprContext;
  423. static void
  424. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  425. {
  426. ctx->sp = 0;
  427. ctx->module = module;
  428. ctx->stack = ctx->data;
  429. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  430. }
  431. static bool
  432. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  433. #if WASM_ENABLE_GC != 0
  434. WASMRefType *ref_type, uint8 gc_opcode,
  435. #endif
  436. WASMValue *value, char *error_buf, uint32 error_buf_size)
  437. {
  438. InitValue *cur_value;
  439. if (ctx->sp >= ctx->size) {
  440. if (ctx->stack != ctx->data) {
  441. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  442. (ctx->size + 4) * sizeof(InitValue));
  443. }
  444. else {
  445. if (!(ctx->stack =
  446. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  447. error_buf, error_buf_size))) {
  448. goto fail;
  449. }
  450. }
  451. ctx->size += 4;
  452. }
  453. cur_value = &ctx->stack[ctx->sp++];
  454. cur_value->type = type;
  455. cur_value->flag = flag;
  456. cur_value->value = *value;
  457. #if WASM_ENABLE_GC != 0
  458. cur_value->gc_opcode = gc_opcode;
  459. if (wasm_is_type_multi_byte_type(type)) {
  460. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  461. ref_type, wasm_reftype_struct_size(ref_type));
  462. }
  463. #endif
  464. return true;
  465. fail:
  466. return false;
  467. }
  468. static bool
  469. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  470. #if WASM_ENABLE_GC != 0
  471. WASMRefType *ref_type, uint8 *p_gc_opcode,
  472. #endif
  473. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  474. {
  475. InitValue *cur_value;
  476. if (ctx->sp == 0) {
  477. set_error_buf(error_buf, error_buf_size,
  478. "type mismatch: const expr stack underflow");
  479. return false;
  480. }
  481. cur_value = &ctx->stack[--ctx->sp];
  482. #if WASM_ENABLE_GC == 0
  483. if (cur_value->type != type) {
  484. set_error_buf(error_buf, error_buf_size, "type mismatch");
  485. return false;
  486. }
  487. #else
  488. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  489. ref_type, ctx->module->types,
  490. ctx->module->type_count)) {
  491. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  492. "type mismatch: expect ", type2str(type),
  493. " but got other");
  494. goto fail;
  495. }
  496. if ((ctx->sp != 0) && (cur_value->flag == WASM_OP_GC_PREFIX)
  497. && (cur_value->gc_opcode != WASM_OP_REF_I31)) {
  498. /* To reduce complexity, we don't allow initialize struct fields/array
  499. * element with references, so struct/array must be at the bottom of the
  500. * init value stack */
  501. set_error_buf(
  502. error_buf, error_buf_size,
  503. "struct or array as field is not supported in constant expr");
  504. goto fail;
  505. }
  506. #endif
  507. if (p_flag)
  508. *p_flag = cur_value->flag;
  509. if (p_value)
  510. *p_value = cur_value->value;
  511. #if WASM_ENABLE_GC != 0
  512. if (p_gc_opcode)
  513. *p_gc_opcode = cur_value->gc_opcode;
  514. #endif
  515. return true;
  516. #if WASM_ENABLE_GC != 0
  517. fail:
  518. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  519. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  520. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  521. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  522. wasm_runtime_free(cur_value->value.data);
  523. }
  524. return false;
  525. #endif
  526. }
  527. static void
  528. destroy_const_expr_stack(ConstExprContext *ctx)
  529. {
  530. #if WASM_ENABLE_GC != 0
  531. uint32 i;
  532. for (i = 0; i < ctx->sp; i++) {
  533. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  534. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  535. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  536. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  537. wasm_runtime_free(ctx->stack[i].value.data);
  538. }
  539. }
  540. #endif
  541. if (ctx->stack != ctx->data) {
  542. wasm_runtime_free(ctx->stack);
  543. }
  544. }
  545. #if WASM_ENABLE_GC != 0
  546. static void
  547. destroy_init_expr(InitializerExpression *expr)
  548. {
  549. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  550. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  551. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  552. wasm_runtime_free(expr->u.data);
  553. }
  554. }
  555. #endif /* end of WASM_ENABLE_GC != 0 */
  556. static bool
  557. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  558. InitializerExpression *init_expr, uint8 type, void *ref_type,
  559. char *error_buf, uint32 error_buf_size)
  560. {
  561. const uint8 *p = *p_buf, *p_end = buf_end;
  562. uint8 flag, *p_float;
  563. uint32 i;
  564. ConstExprContext const_expr_ctx = { 0 };
  565. WASMValue cur_value;
  566. #if WASM_ENABLE_GC != 0
  567. uint32 opcode1, type_idx;
  568. uint8 opcode;
  569. WASMRefType cur_ref_type = { 0 };
  570. #endif
  571. init_const_expr_stack(&const_expr_ctx, module);
  572. CHECK_BUF(p, p_end, 1);
  573. flag = read_uint8(p);
  574. while (flag != WASM_OP_END) {
  575. switch (flag) {
  576. /* i32.const */
  577. case INIT_EXPR_TYPE_I32_CONST:
  578. read_leb_int32(p, p_end, cur_value.i32);
  579. if (!push_const_expr_stack(
  580. &const_expr_ctx, flag, VALUE_TYPE_I32,
  581. #if WASM_ENABLE_GC != 0
  582. NULL, 0,
  583. #endif
  584. &cur_value, error_buf, error_buf_size))
  585. goto fail;
  586. break;
  587. /* i64.const */
  588. case INIT_EXPR_TYPE_I64_CONST:
  589. read_leb_int64(p, p_end, cur_value.i64);
  590. if (!push_const_expr_stack(
  591. &const_expr_ctx, flag, VALUE_TYPE_I64,
  592. #if WASM_ENABLE_GC != 0
  593. NULL, 0,
  594. #endif
  595. &cur_value, error_buf, error_buf_size))
  596. goto fail;
  597. break;
  598. /* f32.const */
  599. case INIT_EXPR_TYPE_F32_CONST:
  600. CHECK_BUF(p, p_end, 4);
  601. p_float = (uint8 *)&cur_value.f32;
  602. for (i = 0; i < sizeof(float32); i++)
  603. *p_float++ = *p++;
  604. if (!push_const_expr_stack(
  605. &const_expr_ctx, flag, VALUE_TYPE_F32,
  606. #if WASM_ENABLE_GC != 0
  607. NULL, 0,
  608. #endif
  609. &cur_value, error_buf, error_buf_size))
  610. goto fail;
  611. break;
  612. /* f64.const */
  613. case INIT_EXPR_TYPE_F64_CONST:
  614. CHECK_BUF(p, p_end, 8);
  615. p_float = (uint8 *)&cur_value.f64;
  616. for (i = 0; i < sizeof(float64); i++)
  617. *p_float++ = *p++;
  618. if (!push_const_expr_stack(
  619. &const_expr_ctx, flag, VALUE_TYPE_F64,
  620. #if WASM_ENABLE_GC != 0
  621. NULL, 0,
  622. #endif
  623. &cur_value, error_buf, error_buf_size))
  624. goto fail;
  625. break;
  626. #if WASM_ENABLE_SIMD != 0
  627. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  628. /* v128.const */
  629. case INIT_EXPR_TYPE_V128_CONST:
  630. {
  631. uint64 high, low;
  632. CHECK_BUF(p, p_end, 1);
  633. (void)read_uint8(p);
  634. CHECK_BUF(p, p_end, 16);
  635. wasm_runtime_read_v128(p, &high, &low);
  636. p += 16;
  637. cur_value.v128.i64x2[0] = high;
  638. cur_value.v128.i64x2[1] = low;
  639. if (!push_const_expr_stack(
  640. &const_expr_ctx, flag, VALUE_TYPE_V128,
  641. #if WASM_ENABLE_GC != 0
  642. NULL, 0,
  643. #endif
  644. &cur_value, error_buf, error_buf_size))
  645. goto fail;
  646. #if WASM_ENABLE_WAMR_COMPILER != 0
  647. /* If any init_expr is v128.const, mark SIMD used */
  648. module->is_simd_used = true;
  649. #endif
  650. break;
  651. }
  652. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  653. #endif /* end of WASM_ENABLE_SIMD */
  654. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  655. /* ref.func */
  656. case INIT_EXPR_TYPE_FUNCREF_CONST:
  657. {
  658. uint32 func_idx;
  659. read_leb_uint32(p, p_end, func_idx);
  660. cur_value.ref_index = func_idx;
  661. if (!check_function_index(module, func_idx, error_buf,
  662. error_buf_size)) {
  663. goto fail;
  664. }
  665. #if WASM_ENABLE_GC == 0
  666. if (!push_const_expr_stack(&const_expr_ctx, flag,
  667. VALUE_TYPE_FUNCREF, &cur_value,
  668. error_buf, error_buf_size))
  669. goto fail;
  670. #else
  671. if (func_idx < module->import_function_count) {
  672. type_idx =
  673. module->import_functions[func_idx].u.function.type_idx;
  674. }
  675. else {
  676. type_idx = module
  677. ->functions[func_idx
  678. - module->import_function_count]
  679. ->type_idx;
  680. }
  681. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  682. false, type_idx);
  683. if (!push_const_expr_stack(&const_expr_ctx, flag,
  684. cur_ref_type.ref_type, &cur_ref_type,
  685. 0, &cur_value, error_buf,
  686. error_buf_size))
  687. goto fail;
  688. #endif
  689. #if WASM_ENABLE_WAMR_COMPILER != 0
  690. module->is_ref_types_used = true;
  691. #endif
  692. break;
  693. }
  694. /* ref.null */
  695. case INIT_EXPR_TYPE_REFNULL_CONST:
  696. {
  697. uint8 type1;
  698. CHECK_BUF(p, p_end, 1);
  699. type1 = read_uint8(p);
  700. #if WASM_ENABLE_GC == 0
  701. cur_value.ref_index = NULL_REF;
  702. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  703. &cur_value, error_buf,
  704. error_buf_size))
  705. goto fail;
  706. #else
  707. cur_value.gc_obj = NULL_REF;
  708. if (!is_byte_a_type(type1)) {
  709. p--;
  710. read_leb_uint32(p, p_end, type_idx);
  711. if (!check_type_index(module, module->type_count, type_idx,
  712. error_buf, error_buf_size))
  713. goto fail;
  714. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  715. true, type_idx);
  716. if (!push_const_expr_stack(&const_expr_ctx, flag,
  717. cur_ref_type.ref_type,
  718. &cur_ref_type, 0, &cur_value,
  719. error_buf, error_buf_size))
  720. goto fail;
  721. }
  722. else {
  723. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  724. NULL, 0, &cur_value, error_buf,
  725. error_buf_size))
  726. goto fail;
  727. }
  728. #endif
  729. #if WASM_ENABLE_WAMR_COMPILER != 0
  730. module->is_ref_types_used = true;
  731. #endif
  732. break;
  733. }
  734. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  735. /* get_global */
  736. case INIT_EXPR_TYPE_GET_GLOBAL:
  737. {
  738. uint32 global_idx;
  739. uint8 global_type;
  740. read_leb_uint32(p, p_end, cur_value.global_index);
  741. global_idx = cur_value.global_index;
  742. #if WASM_ENABLE_GC == 0
  743. if (global_idx >= module->import_global_count) {
  744. /**
  745. * Currently, constant expressions occurring as initializers
  746. * of globals are further constrained in that contained
  747. * global.get instructions are
  748. * only allowed to refer to imported globals.
  749. */
  750. set_error_buf_v(error_buf, error_buf_size,
  751. "unknown global %u", global_idx);
  752. goto fail;
  753. }
  754. if (module->import_globals[global_idx].u.global.is_mutable) {
  755. set_error_buf_v(error_buf, error_buf_size,
  756. "constant expression required");
  757. goto fail;
  758. }
  759. #else
  760. if (global_idx
  761. >= module->import_global_count + module->global_count) {
  762. set_error_buf_v(error_buf, error_buf_size,
  763. "unknown global %u", global_idx);
  764. goto fail;
  765. }
  766. if (global_idx < module->import_global_count
  767. && module->import_globals[global_idx].u.global.is_mutable) {
  768. set_error_buf_v(error_buf, error_buf_size,
  769. "constant expression required");
  770. goto fail;
  771. }
  772. #endif
  773. if (global_idx < module->import_global_count) {
  774. global_type =
  775. module->import_globals[global_idx].u.global.type;
  776. #if WASM_ENABLE_GC != 0
  777. if (wasm_is_type_multi_byte_type(global_type)) {
  778. WASMRefType *global_ref_type =
  779. module->import_globals[global_idx]
  780. .u.global.ref_type;
  781. bh_memcpy_s(&cur_ref_type,
  782. wasm_reftype_struct_size(global_ref_type),
  783. global_ref_type,
  784. wasm_reftype_struct_size(global_ref_type));
  785. }
  786. #endif
  787. }
  788. else {
  789. global_type =
  790. module
  791. ->globals[global_idx - module->import_global_count]
  792. .type;
  793. #if WASM_ENABLE_GC != 0
  794. if (wasm_is_type_multi_byte_type(global_type)) {
  795. WASMRefType *global_ref_type =
  796. module
  797. ->globals[global_idx
  798. - module->import_global_count]
  799. .ref_type;
  800. bh_memcpy_s(&cur_ref_type,
  801. wasm_reftype_struct_size(global_ref_type),
  802. global_ref_type,
  803. wasm_reftype_struct_size(global_ref_type));
  804. }
  805. #endif
  806. }
  807. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  808. #if WASM_ENABLE_GC != 0
  809. &cur_ref_type, 0,
  810. #endif
  811. &cur_value, error_buf,
  812. error_buf_size))
  813. goto fail;
  814. break;
  815. }
  816. #if WASM_ENABLE_GC != 0
  817. /* struct.new and array.new */
  818. case WASM_OP_GC_PREFIX:
  819. {
  820. read_leb_uint32(p, p_end, opcode1);
  821. switch (opcode1) {
  822. case WASM_OP_STRUCT_NEW:
  823. {
  824. WASMStructType *struct_type;
  825. WASMStructNewInitValues *struct_init_values = NULL;
  826. uint32 field_count;
  827. read_leb_uint32(p, p_end, type_idx);
  828. if (!check_type_index(module, module->type_count,
  829. type_idx, error_buf,
  830. error_buf_size)) {
  831. goto fail;
  832. }
  833. struct_type = (WASMStructType *)module->types[type_idx];
  834. if (struct_type->base_type.type_flag
  835. != WASM_TYPE_STRUCT) {
  836. set_error_buf(error_buf, error_buf_size,
  837. "unkown struct type");
  838. goto fail;
  839. }
  840. field_count = struct_type->field_count;
  841. if (!(struct_init_values = loader_malloc(
  842. offsetof(WASMStructNewInitValues, fields)
  843. + (uint64)field_count * sizeof(WASMValue),
  844. error_buf, error_buf_size))) {
  845. goto fail;
  846. }
  847. struct_init_values->count = field_count;
  848. for (i = field_count; i > 0; i--) {
  849. WASMRefType *field_ref_type = NULL;
  850. uint32 field_idx = i - 1;
  851. uint8 field_type =
  852. struct_type->fields[field_idx].field_type;
  853. if (wasm_is_type_multi_byte_type(field_type)) {
  854. field_ref_type = wasm_reftype_map_find(
  855. struct_type->ref_type_maps,
  856. struct_type->ref_type_map_count, field_idx);
  857. }
  858. if (is_packed_type(field_type)) {
  859. field_type = VALUE_TYPE_I32;
  860. }
  861. if (!pop_const_expr_stack(
  862. &const_expr_ctx, NULL, field_type,
  863. field_ref_type, NULL,
  864. &struct_init_values->fields[field_idx],
  865. error_buf, error_buf_size)) {
  866. wasm_runtime_free(struct_init_values);
  867. goto fail;
  868. }
  869. }
  870. cur_value.data = struct_init_values;
  871. wasm_set_refheaptype_typeidx(
  872. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  873. if (!push_const_expr_stack(
  874. &const_expr_ctx, flag, cur_ref_type.ref_type,
  875. &cur_ref_type, (uint8)opcode1, &cur_value,
  876. error_buf, error_buf_size)) {
  877. wasm_runtime_free(struct_init_values);
  878. goto fail;
  879. }
  880. break;
  881. }
  882. case WASM_OP_STRUCT_NEW_DEFAULT:
  883. {
  884. read_leb_uint32(p, p_end, cur_value.type_index);
  885. type_idx = cur_value.type_index;
  886. if (!check_type_index(module, module->type_count,
  887. type_idx, error_buf,
  888. error_buf_size)) {
  889. goto fail;
  890. }
  891. if (module->types[type_idx]->type_flag
  892. != WASM_TYPE_STRUCT) {
  893. set_error_buf(error_buf, error_buf_size,
  894. "unkown struct type");
  895. goto fail;
  896. }
  897. cur_value.type_index = type_idx;
  898. wasm_set_refheaptype_typeidx(
  899. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  900. if (!push_const_expr_stack(
  901. &const_expr_ctx, flag, cur_ref_type.ref_type,
  902. &cur_ref_type, (uint8)opcode1, &cur_value,
  903. error_buf, error_buf_size)) {
  904. goto fail;
  905. }
  906. break;
  907. }
  908. case WASM_OP_ARRAY_NEW:
  909. case WASM_OP_ARRAY_NEW_DEFAULT:
  910. case WASM_OP_ARRAY_NEW_FIXED:
  911. {
  912. WASMArrayNewInitValues *array_init_values = NULL;
  913. WASMArrayType *array_type = NULL;
  914. WASMRefType *elem_ref_type = NULL;
  915. uint64 total_size;
  916. uint8 elem_type;
  917. read_leb_uint32(p, p_end, cur_value.type_index);
  918. type_idx = cur_value.type_index;
  919. if (!check_type_index(module, module->type_count,
  920. type_idx, error_buf,
  921. error_buf_size)) {
  922. goto fail;
  923. }
  924. array_type = (WASMArrayType *)module->types[type_idx];
  925. if (array_type->base_type.type_flag
  926. != WASM_TYPE_ARRAY) {
  927. set_error_buf(error_buf, error_buf_size,
  928. "unkown array type");
  929. goto fail;
  930. }
  931. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  932. elem_type = array_type->elem_type;
  933. if (wasm_is_type_multi_byte_type(elem_type)) {
  934. elem_ref_type = array_type->elem_ref_type;
  935. }
  936. if (is_packed_type(elem_type)) {
  937. elem_type = VALUE_TYPE_I32;
  938. }
  939. if (opcode1 == WASM_OP_ARRAY_NEW) {
  940. WASMValue len_val;
  941. if (!(array_init_values = loader_malloc(
  942. sizeof(WASMArrayNewInitValues),
  943. error_buf, error_buf_size))) {
  944. goto fail;
  945. }
  946. array_init_values->type_idx = type_idx;
  947. if (!pop_const_expr_stack(
  948. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  949. NULL, NULL, &len_val, error_buf,
  950. error_buf_size)) {
  951. wasm_runtime_free(array_init_values);
  952. goto fail;
  953. }
  954. array_init_values->length = len_val.i32;
  955. if (!pop_const_expr_stack(
  956. &const_expr_ctx, NULL, elem_type,
  957. elem_ref_type, NULL,
  958. &array_init_values->elem_data[0],
  959. error_buf, error_buf_size)) {
  960. wasm_runtime_free(array_init_values);
  961. goto fail;
  962. }
  963. cur_value.data = array_init_values;
  964. }
  965. else {
  966. /* WASM_OP_ARRAY_NEW_FIXED */
  967. uint32 len;
  968. read_leb_uint32(p, p_end, len);
  969. total_size =
  970. (uint64)offsetof(WASMArrayNewInitValues,
  971. elem_data)
  972. + (uint64)sizeof(WASMValue) * len;
  973. if (!(array_init_values =
  974. loader_malloc(total_size, error_buf,
  975. error_buf_size))) {
  976. goto fail;
  977. }
  978. array_init_values->type_idx = type_idx;
  979. array_init_values->length = len;
  980. for (i = len; i > 0; i--) {
  981. if (!pop_const_expr_stack(
  982. &const_expr_ctx, NULL, elem_type,
  983. elem_ref_type, NULL,
  984. &array_init_values
  985. ->elem_data[i - 1],
  986. error_buf, error_buf_size)) {
  987. wasm_runtime_free(array_init_values);
  988. goto fail;
  989. }
  990. }
  991. cur_value.data = array_init_values;
  992. }
  993. }
  994. else {
  995. /* WASM_OP_ARRAY_NEW_DEFAULT */
  996. WASMValue len_val;
  997. uint32 len;
  998. /* POP(i32) */
  999. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1000. VALUE_TYPE_I32, NULL,
  1001. NULL, &len_val, error_buf,
  1002. error_buf_size)) {
  1003. goto fail;
  1004. }
  1005. len = len_val.i32;
  1006. cur_value.array_new_default.type_index = type_idx;
  1007. cur_value.array_new_default.length = len;
  1008. }
  1009. wasm_set_refheaptype_typeidx(
  1010. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1011. if (!push_const_expr_stack(
  1012. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1013. &cur_ref_type, (uint8)opcode1, &cur_value,
  1014. error_buf, error_buf_size)) {
  1015. if (array_init_values) {
  1016. wasm_runtime_free(array_init_values);
  1017. }
  1018. goto fail;
  1019. }
  1020. break;
  1021. }
  1022. case WASM_OP_ANY_CONVERT_EXTERN:
  1023. {
  1024. set_error_buf(error_buf, error_buf_size,
  1025. "unsuppoted constant expression of "
  1026. "extern.internalize");
  1027. goto fail;
  1028. }
  1029. case WASM_OP_EXTERN_CONVERT_ANY:
  1030. {
  1031. set_error_buf(error_buf, error_buf_size,
  1032. "unsuppoted constant expression of "
  1033. "extern.externalize");
  1034. goto fail;
  1035. }
  1036. case WASM_OP_REF_I31:
  1037. {
  1038. /* POP(i32) */
  1039. if (!pop_const_expr_stack(
  1040. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1041. NULL, &cur_value, error_buf, error_buf_size)) {
  1042. goto fail;
  1043. }
  1044. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1045. false, HEAP_TYPE_I31);
  1046. if (!push_const_expr_stack(
  1047. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1048. &cur_ref_type, (uint8)opcode1, &cur_value,
  1049. error_buf, error_buf_size)) {
  1050. goto fail;
  1051. }
  1052. break;
  1053. }
  1054. default:
  1055. set_error_buf(
  1056. error_buf, error_buf_size,
  1057. "type mismatch or constant expression required");
  1058. goto fail;
  1059. }
  1060. break;
  1061. }
  1062. #endif /* end of WASM_ENABLE_GC != 0 */
  1063. default:
  1064. {
  1065. set_error_buf(error_buf, error_buf_size,
  1066. "illegal opcode "
  1067. "or constant expression required "
  1068. "or type mismatch");
  1069. goto fail;
  1070. }
  1071. }
  1072. CHECK_BUF(p, p_end, 1);
  1073. flag = read_uint8(p);
  1074. }
  1075. /* There should be only one value left on the init value stack */
  1076. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1077. #if WASM_ENABLE_GC != 0
  1078. ref_type, &opcode,
  1079. #endif
  1080. &cur_value, error_buf, error_buf_size)) {
  1081. goto fail;
  1082. }
  1083. if (const_expr_ctx.sp != 0) {
  1084. set_error_buf(error_buf, error_buf_size,
  1085. "type mismatch: illegal constant opcode sequence");
  1086. goto fail;
  1087. }
  1088. init_expr->init_expr_type = flag;
  1089. init_expr->u = cur_value;
  1090. #if WASM_ENABLE_GC != 0
  1091. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1092. switch (opcode) {
  1093. case WASM_OP_STRUCT_NEW:
  1094. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1095. break;
  1096. case WASM_OP_STRUCT_NEW_DEFAULT:
  1097. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1098. break;
  1099. case WASM_OP_ARRAY_NEW:
  1100. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1101. break;
  1102. case WASM_OP_ARRAY_NEW_DEFAULT:
  1103. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1104. break;
  1105. case WASM_OP_ARRAY_NEW_FIXED:
  1106. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1107. break;
  1108. case WASM_OP_REF_I31:
  1109. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1110. break;
  1111. default:
  1112. bh_assert(0);
  1113. break;
  1114. }
  1115. }
  1116. #endif /* end of WASM_ENABLE_GC != 0 */
  1117. *p_buf = p;
  1118. destroy_const_expr_stack(&const_expr_ctx);
  1119. return true;
  1120. fail:
  1121. destroy_const_expr_stack(&const_expr_ctx);
  1122. return false;
  1123. }
  1124. static bool
  1125. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1126. {
  1127. if (mutable >= 2) {
  1128. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1129. return false;
  1130. }
  1131. return true;
  1132. }
  1133. #if WASM_ENABLE_GC != 0
  1134. static void
  1135. destroy_func_type(WASMFuncType *type)
  1136. {
  1137. /* Destroy the reference type hash set */
  1138. if (type->ref_type_maps)
  1139. wasm_runtime_free(type->ref_type_maps);
  1140. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1141. && WASM_ENABLE_LAZY_JIT != 0
  1142. if (type->call_to_llvm_jit_from_fast_jit)
  1143. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1144. #endif
  1145. /* Free the type */
  1146. wasm_runtime_free(type);
  1147. }
  1148. static void
  1149. destroy_struct_type(WASMStructType *type)
  1150. {
  1151. if (type->ref_type_maps)
  1152. wasm_runtime_free(type->ref_type_maps);
  1153. wasm_runtime_free(type);
  1154. }
  1155. static void
  1156. destroy_array_type(WASMArrayType *type)
  1157. {
  1158. wasm_runtime_free(type);
  1159. }
  1160. static void
  1161. destroy_wasm_type(WASMType *type)
  1162. {
  1163. if (type->ref_count > 1) {
  1164. /* The type is referenced by other types
  1165. of current wasm module */
  1166. type->ref_count--;
  1167. return;
  1168. }
  1169. if (type->type_flag == WASM_TYPE_FUNC)
  1170. destroy_func_type((WASMFuncType *)type);
  1171. else if (type->type_flag == WASM_TYPE_STRUCT)
  1172. destroy_struct_type((WASMStructType *)type);
  1173. else if (type->type_flag == WASM_TYPE_ARRAY)
  1174. destroy_array_type((WASMArrayType *)type);
  1175. else {
  1176. bh_assert(0);
  1177. }
  1178. }
  1179. /* Resolve (ref null ht) or (ref ht) */
  1180. static bool
  1181. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1182. WASMModule *module, uint32 type_count, bool nullable,
  1183. WASMRefType *ref_type, char *error_buf,
  1184. uint32 error_buf_size)
  1185. {
  1186. const uint8 *p = *p_buf, *p_end = buf_end;
  1187. ref_type->ref_type =
  1188. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1189. ref_type->ref_ht_common.nullable = nullable;
  1190. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1191. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1192. /* heap type is (type i), i : typeidx, >= 0 */
  1193. if (!check_type_index(module, type_count,
  1194. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1195. error_buf_size)) {
  1196. return false;
  1197. }
  1198. }
  1199. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1200. /* heap type is func, extern, any, eq, i31 or data */
  1201. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1202. return false;
  1203. }
  1204. *p_buf = p;
  1205. return true;
  1206. fail:
  1207. return false;
  1208. }
  1209. static bool
  1210. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1211. WASMModule *module, uint32 type_count,
  1212. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1213. bool allow_packed_type, char *error_buf,
  1214. uint32 error_buf_size)
  1215. {
  1216. const uint8 *p = *p_buf, *p_end = buf_end;
  1217. uint8 type;
  1218. memset(ref_type, 0, sizeof(WASMRefType));
  1219. CHECK_BUF(p, p_end, 1);
  1220. type = read_uint8(p);
  1221. if (wasm_is_reftype_htref_nullable(type)) {
  1222. /* (ref null ht) */
  1223. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1224. ref_type, error_buf, error_buf_size))
  1225. return false;
  1226. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1227. *p_need_ref_type_map = true;
  1228. else {
  1229. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1230. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1231. multi-byte type to one-byte type to reduce the footprint and
  1232. the complexity of type equal/subtype checking */
  1233. ref_type->ref_type =
  1234. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1235. *p_need_ref_type_map = false;
  1236. }
  1237. }
  1238. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1239. /* (ref ht) */
  1240. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1241. ref_type, error_buf, error_buf_size))
  1242. return false;
  1243. *p_need_ref_type_map = true;
  1244. #if WASM_ENABLE_STRINGREF != 0
  1245. /* covert (ref string) to stringref */
  1246. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1247. ref_type->ref_type =
  1248. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1249. *p_need_ref_type_map = false;
  1250. }
  1251. #endif
  1252. }
  1253. else {
  1254. /* type which can be represented by one byte */
  1255. if (!is_value_type(type)
  1256. && !(allow_packed_type && is_packed_type(type))) {
  1257. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1258. return false;
  1259. }
  1260. ref_type->ref_type = type;
  1261. *p_need_ref_type_map = false;
  1262. #if WASM_ENABLE_WAMR_COMPILER != 0
  1263. /* If any value's type is v128, mark the module as SIMD used */
  1264. if (type == VALUE_TYPE_V128)
  1265. module->is_simd_used = true;
  1266. #endif
  1267. }
  1268. *p_buf = p;
  1269. return true;
  1270. fail:
  1271. return false;
  1272. }
  1273. static WASMRefType *
  1274. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1275. char *error_buf, uint32 error_buf_size)
  1276. {
  1277. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1278. if (!ret) {
  1279. set_error_buf(error_buf, error_buf_size,
  1280. "insert ref type to hash set failed");
  1281. }
  1282. return ret;
  1283. }
  1284. static bool
  1285. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1286. uint32 type_count, uint32 type_idx, char *error_buf,
  1287. uint32 error_buf_size)
  1288. {
  1289. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1290. uint32 param_count, result_count, i, j = 0;
  1291. uint32 param_cell_num, ret_cell_num;
  1292. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1293. uint64 total_size;
  1294. bool need_ref_type_map;
  1295. WASMRefType ref_type;
  1296. WASMFuncType *type = NULL;
  1297. /* Parse first time to resolve param count, result count and
  1298. ref type map count */
  1299. read_leb_uint32(p, p_end, param_count);
  1300. p_org = p;
  1301. for (i = 0; i < param_count; i++) {
  1302. if (!resolve_value_type(&p, p_end, module, type_count,
  1303. &need_ref_type_map, &ref_type, false, error_buf,
  1304. error_buf_size)) {
  1305. return false;
  1306. }
  1307. if (need_ref_type_map)
  1308. ref_type_map_count++;
  1309. }
  1310. read_leb_uint32(p, p_end, result_count);
  1311. for (i = 0; i < result_count; i++) {
  1312. if (!resolve_value_type(&p, p_end, module, type_count,
  1313. &need_ref_type_map, &ref_type, false, error_buf,
  1314. error_buf_size)) {
  1315. return false;
  1316. }
  1317. if (need_ref_type_map) {
  1318. ref_type_map_count++;
  1319. result_ref_type_map_count++;
  1320. }
  1321. }
  1322. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1323. "ref type map count: %d",
  1324. type_idx, param_count, result_count, ref_type_map_count);
  1325. /* Parse second time to resolve param types, result types and
  1326. ref type map info */
  1327. p = p_org;
  1328. total_size = offsetof(WASMFuncType, types)
  1329. + sizeof(uint8) * (uint64)(param_count + result_count);
  1330. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1331. return false;
  1332. }
  1333. if (ref_type_map_count > 0) {
  1334. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1335. if (!(type->ref_type_maps =
  1336. loader_malloc(total_size, error_buf, error_buf_size))) {
  1337. goto fail;
  1338. }
  1339. }
  1340. type->base_type.type_flag = WASM_TYPE_FUNC;
  1341. type->param_count = param_count;
  1342. type->result_count = result_count;
  1343. type->ref_type_map_count = ref_type_map_count;
  1344. if (ref_type_map_count > 0) {
  1345. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1346. - result_ref_type_map_count;
  1347. }
  1348. for (i = 0; i < param_count; i++) {
  1349. if (!resolve_value_type(&p, p_end, module, type_count,
  1350. &need_ref_type_map, &ref_type, false, error_buf,
  1351. error_buf_size)) {
  1352. goto fail;
  1353. }
  1354. type->types[i] = ref_type.ref_type;
  1355. if (need_ref_type_map) {
  1356. type->ref_type_maps[j].index = i;
  1357. if (!(type->ref_type_maps[j++].ref_type =
  1358. reftype_set_insert(module->ref_type_set, &ref_type,
  1359. error_buf, error_buf_size))) {
  1360. goto fail;
  1361. }
  1362. }
  1363. }
  1364. read_leb_uint32(p, p_end, result_count);
  1365. for (i = 0; i < result_count; i++) {
  1366. if (!resolve_value_type(&p, p_end, module, type_count,
  1367. &need_ref_type_map, &ref_type, false, error_buf,
  1368. error_buf_size)) {
  1369. goto fail;
  1370. }
  1371. type->types[param_count + i] = ref_type.ref_type;
  1372. if (need_ref_type_map) {
  1373. type->ref_type_maps[j].index = param_count + i;
  1374. if (!(type->ref_type_maps[j++].ref_type =
  1375. reftype_set_insert(module->ref_type_set, &ref_type,
  1376. error_buf, error_buf_size))) {
  1377. goto fail;
  1378. }
  1379. }
  1380. }
  1381. bh_assert(j == type->ref_type_map_count);
  1382. #if TRACE_WASM_LOADER != 0
  1383. os_printf("type %d = ", type_idx);
  1384. wasm_dump_func_type(type);
  1385. #endif
  1386. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1387. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1388. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1389. set_error_buf(error_buf, error_buf_size,
  1390. "param count or result count too large");
  1391. goto fail;
  1392. }
  1393. type->param_cell_num = (uint16)param_cell_num;
  1394. type->ret_cell_num = (uint16)ret_cell_num;
  1395. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1396. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1397. #endif
  1398. #if WASM_ENABLE_WAMR_COMPILER != 0
  1399. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1400. if (type->types[i] == VALUE_TYPE_V128)
  1401. module->is_simd_used = true;
  1402. }
  1403. #endif
  1404. *p_buf = p;
  1405. module->types[type_idx] = (WASMType *)type;
  1406. return true;
  1407. fail:
  1408. if (type)
  1409. destroy_func_type(type);
  1410. return false;
  1411. }
  1412. static bool
  1413. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1414. WASMModule *module, uint32 type_count, uint32 type_idx,
  1415. char *error_buf, uint32 error_buf_size)
  1416. {
  1417. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1418. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1419. uint32 i, j = 0, offset;
  1420. uint16 *reference_table;
  1421. uint64 total_size;
  1422. uint8 mutable;
  1423. bool need_ref_type_map;
  1424. WASMRefType ref_type;
  1425. WASMStructType *type = NULL;
  1426. /* Parse first time to resolve field count and ref type map count */
  1427. read_leb_uint32(p, p_end, field_count);
  1428. p_org = p;
  1429. for (i = 0; i < field_count; i++) {
  1430. if (!resolve_value_type(&p, p_end, module, type_count,
  1431. &need_ref_type_map, &ref_type, true, error_buf,
  1432. error_buf_size)) {
  1433. return false;
  1434. }
  1435. if (need_ref_type_map)
  1436. ref_type_map_count++;
  1437. if (wasm_is_type_reftype(ref_type.ref_type))
  1438. ref_field_count++;
  1439. CHECK_BUF(p, p_end, 1);
  1440. mutable = read_uint8(p);
  1441. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1442. return false;
  1443. }
  1444. }
  1445. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1446. type_idx, field_count, ref_type_map_count);
  1447. /* Parse second time to resolve field types and ref type map info */
  1448. p = p_org;
  1449. total_size = offsetof(WASMStructType, fields)
  1450. + sizeof(WASMStructFieldType) * (uint64)field_count
  1451. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1452. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1453. return false;
  1454. }
  1455. if (ref_type_map_count > 0) {
  1456. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1457. if (!(type->ref_type_maps =
  1458. loader_malloc(total_size, error_buf, error_buf_size))) {
  1459. goto fail;
  1460. }
  1461. }
  1462. type->reference_table = reference_table =
  1463. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1464. + sizeof(WASMStructFieldType) * field_count);
  1465. *reference_table++ = ref_field_count;
  1466. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1467. type->field_count = field_count;
  1468. type->ref_type_map_count = ref_type_map_count;
  1469. offset = (uint32)offsetof(WASMStructObject, field_data);
  1470. for (i = 0; i < field_count; i++) {
  1471. if (!resolve_value_type(&p, p_end, module, type_count,
  1472. &need_ref_type_map, &ref_type, true, error_buf,
  1473. error_buf_size)) {
  1474. goto fail;
  1475. }
  1476. type->fields[i].field_type = ref_type.ref_type;
  1477. if (need_ref_type_map) {
  1478. type->ref_type_maps[j].index = i;
  1479. if (!(type->ref_type_maps[j++].ref_type =
  1480. reftype_set_insert(module->ref_type_set, &ref_type,
  1481. error_buf, error_buf_size))) {
  1482. goto fail;
  1483. }
  1484. }
  1485. CHECK_BUF(p, p_end, 1);
  1486. type->fields[i].field_flags = read_uint8(p);
  1487. type->fields[i].field_size =
  1488. (uint8)wasm_reftype_size(ref_type.ref_type);
  1489. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1490. || defined(BUILD_TARGET_X86_32))
  1491. if (type->fields[i].field_size == 2)
  1492. offset = align_uint(offset, 2);
  1493. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1494. offset = align_uint(offset, 4);
  1495. #endif
  1496. type->fields[i].field_offset = offset;
  1497. if (wasm_is_type_reftype(ref_type.ref_type))
  1498. *reference_table++ = offset;
  1499. offset += type->fields[i].field_size;
  1500. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1501. type->fields[i].field_flags, type->fields[i].field_type);
  1502. }
  1503. type->total_size = offset;
  1504. bh_assert(j == type->ref_type_map_count);
  1505. #if TRACE_WASM_LOADER != 0
  1506. os_printf("type %d = ", type_idx);
  1507. wasm_dump_struct_type(type);
  1508. #endif
  1509. *p_buf = p;
  1510. module->types[type_idx] = (WASMType *)type;
  1511. return true;
  1512. fail:
  1513. if (type)
  1514. destroy_struct_type(type);
  1515. return false;
  1516. }
  1517. static bool
  1518. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1519. WASMModule *module, uint32 type_count, uint32 type_idx,
  1520. char *error_buf, uint32 error_buf_size)
  1521. {
  1522. const uint8 *p = *p_buf, *p_end = buf_end;
  1523. uint8 mutable;
  1524. bool need_ref_type_map;
  1525. WASMRefType ref_type;
  1526. WASMArrayType *type = NULL;
  1527. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1528. &ref_type, true, error_buf, error_buf_size)) {
  1529. return false;
  1530. }
  1531. CHECK_BUF(p, p_end, 1);
  1532. mutable = read_uint8(p);
  1533. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1534. return false;
  1535. }
  1536. LOG_VERBOSE("type %u: array", type_idx);
  1537. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1538. error_buf_size))) {
  1539. return false;
  1540. }
  1541. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1542. type->elem_flags = mutable;
  1543. type->elem_type = ref_type.ref_type;
  1544. if (need_ref_type_map) {
  1545. if (!(type->elem_ref_type =
  1546. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1547. error_buf_size))) {
  1548. goto fail;
  1549. }
  1550. }
  1551. #if TRACE_WASM_LOADER != 0
  1552. os_printf("type %d = ", type_idx);
  1553. wasm_dump_array_type(type);
  1554. #endif
  1555. *p_buf = p;
  1556. module->types[type_idx] = (WASMType *)type;
  1557. return true;
  1558. fail:
  1559. if (type)
  1560. destroy_array_type(type);
  1561. return false;
  1562. }
  1563. static bool
  1564. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1565. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1566. {
  1567. if (heap_type >= 0) {
  1568. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1569. error_buf_size)) {
  1570. return false;
  1571. }
  1572. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1573. heap_type);
  1574. }
  1575. else {
  1576. if (!wasm_is_valid_heap_type(heap_type)) {
  1577. set_error_buf(error_buf, error_buf_size, "unknown type");
  1578. return false;
  1579. }
  1580. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1581. heap_type);
  1582. if (nullable) {
  1583. /* For (ref null func/extern/any/eq/i31/data),
  1584. they are same as
  1585. funcref/externref/anyref/eqref/i31ref/dataref,
  1586. we convert the multi-byte type to one-byte
  1587. type to reduce the footprint and the
  1588. complexity of type equal/subtype checking */
  1589. ref_type->ref_type =
  1590. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1591. }
  1592. }
  1593. return true;
  1594. }
  1595. static void
  1596. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1597. WASMRefType *ref_type2)
  1598. {
  1599. /**
  1600. * The difference rt1 ∖ rt2 between two reference types is defined as
  1601. * follows:
  1602. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1603. * (ref ht2) = (ref null?1 ht1)
  1604. */
  1605. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1606. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1607. ref_type1, wasm_reftype_struct_size(ref_type1));
  1608. }
  1609. else {
  1610. ref_type_diff->ref_type = ref_type1->ref_type;
  1611. }
  1612. if (ref_type2->ref_ht_common.nullable) {
  1613. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1614. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1615. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1616. (int32)ref_type_diff->ref_type - 0x80);
  1617. }
  1618. else {
  1619. ref_type_diff->ref_ht_typeidx.nullable = false;
  1620. }
  1621. }
  1622. }
  1623. #else /* else of WASM_ENABLE_GC != 0 */
  1624. static void
  1625. destroy_wasm_type(WASMType *type)
  1626. {
  1627. if (type->ref_count > 1) {
  1628. /* The type is referenced by other types
  1629. of current wasm module */
  1630. type->ref_count--;
  1631. return;
  1632. }
  1633. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1634. && WASM_ENABLE_LAZY_JIT != 0
  1635. if (type->call_to_llvm_jit_from_fast_jit)
  1636. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1637. #endif
  1638. wasm_runtime_free(type);
  1639. }
  1640. #endif /* end of WASM_ENABLE_GC != 0 */
  1641. static bool
  1642. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1643. char *error_buf, uint32 error_buf_size)
  1644. {
  1645. const uint8 *p = buf, *p_end = buf_end;
  1646. uint32 type_count, i;
  1647. uint64 total_size;
  1648. uint8 flag;
  1649. #if WASM_ENABLE_GC != 0
  1650. uint32 processed_type_count = 0;
  1651. #endif
  1652. read_leb_uint32(p, p_end, type_count);
  1653. if (type_count) {
  1654. module->type_count = type_count;
  1655. total_size = sizeof(WASMType *) * (uint64)type_count;
  1656. if (!(module->types =
  1657. loader_malloc(total_size, error_buf, error_buf_size))) {
  1658. return false;
  1659. }
  1660. #if WASM_ENABLE_GC == 0
  1661. for (i = 0; i < type_count; i++) {
  1662. WASMFuncType *type;
  1663. const uint8 *p_org;
  1664. uint32 param_count, result_count, j;
  1665. uint32 param_cell_num, ret_cell_num;
  1666. CHECK_BUF(p, p_end, 1);
  1667. flag = read_uint8(p);
  1668. if (flag != 0x60) {
  1669. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1670. return false;
  1671. }
  1672. read_leb_uint32(p, p_end, param_count);
  1673. /* Resolve param count and result count firstly */
  1674. p_org = p;
  1675. CHECK_BUF(p, p_end, param_count);
  1676. p += param_count;
  1677. read_leb_uint32(p, p_end, result_count);
  1678. CHECK_BUF(p, p_end, result_count);
  1679. p = p_org;
  1680. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1681. set_error_buf(error_buf, error_buf_size,
  1682. "param count or result count too large");
  1683. return false;
  1684. }
  1685. total_size = offsetof(WASMFuncType, types)
  1686. + sizeof(uint8) * (uint64)(param_count + result_count);
  1687. if (!(type = module->types[i] =
  1688. loader_malloc(total_size, error_buf, error_buf_size))) {
  1689. return false;
  1690. }
  1691. /* Resolve param types and result types */
  1692. type->ref_count = 1;
  1693. type->param_count = (uint16)param_count;
  1694. type->result_count = (uint16)result_count;
  1695. for (j = 0; j < param_count; j++) {
  1696. CHECK_BUF(p, p_end, 1);
  1697. type->types[j] = read_uint8(p);
  1698. }
  1699. read_leb_uint32(p, p_end, result_count);
  1700. for (j = 0; j < result_count; j++) {
  1701. CHECK_BUF(p, p_end, 1);
  1702. type->types[param_count + j] = read_uint8(p);
  1703. }
  1704. for (j = 0; j < param_count + result_count; j++) {
  1705. if (!is_value_type(type->types[j])) {
  1706. set_error_buf(error_buf, error_buf_size,
  1707. "unknown value type");
  1708. return false;
  1709. }
  1710. }
  1711. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1712. ret_cell_num =
  1713. wasm_get_cell_num(type->types + param_count, result_count);
  1714. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1715. set_error_buf(error_buf, error_buf_size,
  1716. "param count or result count too large");
  1717. return false;
  1718. }
  1719. type->param_cell_num = (uint16)param_cell_num;
  1720. type->ret_cell_num = (uint16)ret_cell_num;
  1721. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1722. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1723. #endif
  1724. #if WASM_ENABLE_WAMR_COMPILER != 0
  1725. for (j = 0; j < type->param_count + type->result_count; j++) {
  1726. if (type->types[j] == VALUE_TYPE_V128)
  1727. module->is_simd_used = true;
  1728. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1729. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1730. module->is_ref_types_used = true;
  1731. }
  1732. #endif
  1733. /* If there is already a same type created, use it instead */
  1734. for (j = 0; j < i; j++) {
  1735. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1736. if (module->types[j]->ref_count == UINT16_MAX) {
  1737. set_error_buf(error_buf, error_buf_size,
  1738. "wasm type's ref count too large");
  1739. return false;
  1740. }
  1741. destroy_wasm_type(type);
  1742. module->types[i] = module->types[j];
  1743. module->types[j]->ref_count++;
  1744. break;
  1745. }
  1746. }
  1747. }
  1748. #else /* else of WASM_ENABLE_GC == 0 */
  1749. for (i = 0; i < type_count; i++) {
  1750. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1751. uint32 rec_count = 1, j;
  1752. bool is_sub_final = true;
  1753. CHECK_BUF(p, p_end, 1);
  1754. flag = read_uint8(p);
  1755. if (flag == DEFINED_TYPE_REC) {
  1756. read_leb_uint32(p, p_end, rec_count);
  1757. if (rec_count > 1) {
  1758. uint64 new_total_size;
  1759. /* integer overflow */
  1760. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1761. set_error_buf(error_buf, error_buf_size,
  1762. "recursive type count too large");
  1763. return false;
  1764. }
  1765. module->type_count += rec_count - 1;
  1766. new_total_size =
  1767. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1768. if (new_total_size > UINT32_MAX) {
  1769. set_error_buf(error_buf, error_buf_size,
  1770. "allocate memory failed");
  1771. return false;
  1772. }
  1773. MEM_REALLOC(module->types, (uint32)total_size,
  1774. (uint32)new_total_size);
  1775. total_size = new_total_size;
  1776. }
  1777. LOG_VERBOSE("Processing rec group [%d-%d]",
  1778. processed_type_count,
  1779. processed_type_count + rec_count - 1);
  1780. }
  1781. else {
  1782. p--;
  1783. }
  1784. for (j = 0; j < rec_count; j++) {
  1785. WASMType *cur_type = NULL;
  1786. CHECK_BUF(p, p_end, 1);
  1787. flag = read_uint8(p);
  1788. parent_type_idx = -1;
  1789. if (flag == DEFINED_TYPE_SUB
  1790. || flag == DEFINED_TYPE_SUB_FINAL) {
  1791. read_leb_uint32(p, p_end, super_type_count);
  1792. if (super_type_count > 1) {
  1793. set_error_buf(error_buf, error_buf_size,
  1794. "super type count too large");
  1795. return false;
  1796. }
  1797. if (super_type_count > 0) {
  1798. read_leb_uint32(p, p_end, parent_type_idx);
  1799. if (parent_type_idx >= processed_type_count + j) {
  1800. set_error_buf_v(error_buf, error_buf_size,
  1801. "unknown type %d", parent_type_idx);
  1802. return false;
  1803. }
  1804. if (module->types[parent_type_idx]->is_sub_final) {
  1805. set_error_buf(error_buf, error_buf_size,
  1806. "sub type can not inherit from "
  1807. "a final super type");
  1808. return false;
  1809. }
  1810. }
  1811. if (flag == DEFINED_TYPE_SUB)
  1812. is_sub_final = false;
  1813. CHECK_BUF(p, p_end, 1);
  1814. flag = read_uint8(p);
  1815. }
  1816. if (flag == DEFINED_TYPE_FUNC) {
  1817. if (!resolve_func_type(&p, buf_end, module,
  1818. processed_type_count + rec_count,
  1819. processed_type_count + j, error_buf,
  1820. error_buf_size)) {
  1821. return false;
  1822. }
  1823. }
  1824. else if (flag == DEFINED_TYPE_STRUCT) {
  1825. if (!resolve_struct_type(&p, buf_end, module,
  1826. processed_type_count + rec_count,
  1827. processed_type_count + j,
  1828. error_buf, error_buf_size)) {
  1829. return false;
  1830. }
  1831. }
  1832. else if (flag == DEFINED_TYPE_ARRAY) {
  1833. if (!resolve_array_type(&p, buf_end, module,
  1834. processed_type_count + rec_count,
  1835. processed_type_count + j, error_buf,
  1836. error_buf_size)) {
  1837. return false;
  1838. }
  1839. }
  1840. else {
  1841. set_error_buf(error_buf, error_buf_size,
  1842. "invalid type flag");
  1843. return false;
  1844. }
  1845. cur_type = module->types[processed_type_count + j];
  1846. cur_type->ref_count = 1;
  1847. cur_type->parent_type_idx = parent_type_idx;
  1848. cur_type->is_sub_final = is_sub_final;
  1849. cur_type->rec_count = rec_count;
  1850. cur_type->rec_idx = j;
  1851. cur_type->rec_begin_type_idx = processed_type_count;
  1852. }
  1853. /* resolve subtyping relationship in current rec group */
  1854. for (j = 0; j < rec_count; j++) {
  1855. WASMType *cur_type = module->types[processed_type_count + j];
  1856. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1857. WASMType *parent_type =
  1858. module->types[cur_type->parent_type_idx];
  1859. cur_type->parent_type = parent_type;
  1860. cur_type->root_type = parent_type->root_type;
  1861. if (parent_type->inherit_depth == UINT16_MAX) {
  1862. set_error_buf(error_buf, error_buf_size,
  1863. "parent type's inherit depth too large");
  1864. return false;
  1865. }
  1866. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1867. }
  1868. else {
  1869. cur_type->parent_type = NULL;
  1870. cur_type->root_type = cur_type;
  1871. cur_type->inherit_depth = 0;
  1872. }
  1873. }
  1874. for (j = 0; j < rec_count; j++) {
  1875. WASMType *cur_type = module->types[processed_type_count + j];
  1876. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1877. WASMType *parent_type =
  1878. module->types[cur_type->parent_type_idx];
  1879. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1880. module->types,
  1881. module->type_count)) {
  1882. set_error_buf(error_buf, error_buf_size,
  1883. "sub type does not match super type");
  1884. return false;
  1885. }
  1886. }
  1887. }
  1888. /* If there is already an equivalence type or a group of equivalence
  1889. recursive types created, use it or them instead */
  1890. for (j = 0; j < processed_type_count;) {
  1891. WASMType *src_type = module->types[j];
  1892. WASMType *cur_type = module->types[processed_type_count];
  1893. uint32 k, src_rec_count;
  1894. src_rec_count = src_type->rec_count;
  1895. if (src_rec_count != rec_count) {
  1896. /* no type equivalence */
  1897. j += src_rec_count;
  1898. continue;
  1899. }
  1900. for (k = 0; k < rec_count; k++) {
  1901. src_type = module->types[j + k];
  1902. cur_type = module->types[processed_type_count + k];
  1903. if (!wasm_type_equal(src_type, cur_type, module->types,
  1904. module->type_count)) {
  1905. break;
  1906. }
  1907. }
  1908. if (k < rec_count) {
  1909. /* no type equivalence */
  1910. j += src_rec_count;
  1911. continue;
  1912. }
  1913. /* type equivalence */
  1914. for (k = 0; k < rec_count; k++) {
  1915. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1916. set_error_buf(error_buf, error_buf_size,
  1917. "wasm type's ref count too large");
  1918. return false;
  1919. }
  1920. destroy_wasm_type(module->types[processed_type_count + k]);
  1921. module->types[processed_type_count + k] =
  1922. module->types[j + k];
  1923. module->types[j + k]->ref_count++;
  1924. }
  1925. break;
  1926. }
  1927. if (rec_count > 1) {
  1928. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1929. processed_type_count,
  1930. processed_type_count + rec_count - 1);
  1931. }
  1932. processed_type_count += rec_count;
  1933. }
  1934. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1935. * module->type_count,
  1936. error_buf, error_buf_size))) {
  1937. return false;
  1938. }
  1939. #endif /* end of WASM_ENABLE_GC == 0 */
  1940. }
  1941. if (p != p_end) {
  1942. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1943. return false;
  1944. }
  1945. LOG_VERBOSE("Load type section success.\n");
  1946. return true;
  1947. fail:
  1948. return false;
  1949. }
  1950. static void
  1951. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1952. {
  1953. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  1954. ? init_size * 2
  1955. : WASM_TABLE_MAX_SIZE;
  1956. if (max_size_flag) {
  1957. /* module defines the table limitation */
  1958. bh_assert(init_size <= *max_size);
  1959. if (init_size < *max_size) {
  1960. *max_size =
  1961. *max_size < default_max_size ? *max_size : default_max_size;
  1962. }
  1963. }
  1964. else {
  1965. /* partial defined table limitation, gives a default value */
  1966. *max_size = default_max_size;
  1967. }
  1968. }
  1969. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1970. /**
  1971. * Find export item of a module with export info:
  1972. * module name, field name and export kind
  1973. */
  1974. static WASMExport *
  1975. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1976. const char *field_name, uint8 export_kind,
  1977. char *error_buf, uint32 error_buf_size)
  1978. {
  1979. WASMExport *export =
  1980. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1981. export_kind, error_buf, error_buf_size);
  1982. return export;
  1983. }
  1984. #endif
  1985. #if WASM_ENABLE_MULTI_MODULE != 0
  1986. static WASMFunction *
  1987. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1988. const WASMFuncType *expected_function_type,
  1989. char *error_buf, uint32 error_buf_size)
  1990. {
  1991. WASMModuleCommon *module_reg;
  1992. WASMFunction *function = NULL;
  1993. WASMExport *export = NULL;
  1994. WASMModule *module = NULL;
  1995. WASMFuncType *target_function_type = NULL;
  1996. module_reg = wasm_runtime_find_module_registered(module_name);
  1997. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  1998. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  1999. function_name);
  2000. set_error_buf(error_buf, error_buf_size, "unknown import");
  2001. return NULL;
  2002. }
  2003. module = (WASMModule *)module_reg;
  2004. export =
  2005. wasm_loader_find_export(module, module_name, function_name,
  2006. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2007. if (!export) {
  2008. return NULL;
  2009. }
  2010. /* resolve function type and function */
  2011. if (export->index < module->import_function_count) {
  2012. target_function_type =
  2013. module->import_functions[export->index].u.function.func_type;
  2014. function = module->import_functions[export->index]
  2015. .u.function.import_func_linked;
  2016. }
  2017. else {
  2018. target_function_type =
  2019. module->functions[export->index - module->import_function_count]
  2020. ->func_type;
  2021. function =
  2022. module->functions[export->index - module->import_function_count];
  2023. }
  2024. /* check function type */
  2025. if (!wasm_type_equal((WASMType *)expected_function_type,
  2026. (WASMType *)target_function_type, module->types,
  2027. module->type_count)) {
  2028. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2029. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2030. return NULL;
  2031. }
  2032. return function;
  2033. }
  2034. static WASMTable *
  2035. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2036. uint32 init_size, uint32 max_size, char *error_buf,
  2037. uint32 error_buf_size)
  2038. {
  2039. WASMModuleCommon *module_reg;
  2040. WASMTable *table = NULL;
  2041. WASMExport *export = NULL;
  2042. WASMModule *module = NULL;
  2043. module_reg = wasm_runtime_find_module_registered(module_name);
  2044. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2045. LOG_DEBUG("can not find a module named %s for table", module_name);
  2046. set_error_buf(error_buf, error_buf_size, "unknown import");
  2047. return NULL;
  2048. }
  2049. module = (WASMModule *)module_reg;
  2050. export =
  2051. wasm_loader_find_export(module, module_name, table_name,
  2052. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2053. if (!export) {
  2054. return NULL;
  2055. }
  2056. /* resolve table and check the init/max size */
  2057. if (export->index < module->import_table_count) {
  2058. table =
  2059. module->import_tables[export->index].u.table.import_table_linked;
  2060. }
  2061. else {
  2062. table = &(module->tables[export->index - module->import_table_count]);
  2063. }
  2064. if (table->init_size < init_size || table->max_size > max_size) {
  2065. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2066. module_name, table_name, table->init_size, table->max_size,
  2067. init_size, max_size);
  2068. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2069. return NULL;
  2070. }
  2071. return table;
  2072. }
  2073. static WASMMemory *
  2074. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2075. uint32 init_page_count, uint32 max_page_count,
  2076. char *error_buf, uint32 error_buf_size)
  2077. {
  2078. WASMModuleCommon *module_reg;
  2079. WASMMemory *memory = NULL;
  2080. WASMExport *export = NULL;
  2081. WASMModule *module = NULL;
  2082. module_reg = wasm_runtime_find_module_registered(module_name);
  2083. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2084. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2085. set_error_buf(error_buf, error_buf_size, "unknown import");
  2086. return NULL;
  2087. }
  2088. module = (WASMModule *)module_reg;
  2089. export =
  2090. wasm_loader_find_export(module, module_name, memory_name,
  2091. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2092. if (!export) {
  2093. return NULL;
  2094. }
  2095. /* resolve memory and check the init/max page count */
  2096. if (export->index < module->import_memory_count) {
  2097. memory = module->import_memories[export->index]
  2098. .u.memory.import_memory_linked;
  2099. }
  2100. else {
  2101. memory =
  2102. &(module->memories[export->index - module->import_memory_count]);
  2103. }
  2104. if (memory->init_page_count < init_page_count
  2105. || memory->max_page_count > max_page_count) {
  2106. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2107. module_name, memory_name, memory->init_page_count,
  2108. memory->max_page_count, init_page_count, max_page_count);
  2109. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2110. return NULL;
  2111. }
  2112. return memory;
  2113. }
  2114. static WASMGlobal *
  2115. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2116. uint8 type, bool is_mutable, char *error_buf,
  2117. uint32 error_buf_size)
  2118. {
  2119. WASMModuleCommon *module_reg;
  2120. WASMGlobal *global = NULL;
  2121. WASMExport *export = NULL;
  2122. WASMModule *module = NULL;
  2123. module_reg = wasm_runtime_find_module_registered(module_name);
  2124. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2125. LOG_DEBUG("can not find a module named %s for global", module_name);
  2126. set_error_buf(error_buf, error_buf_size, "unknown import");
  2127. return NULL;
  2128. }
  2129. module = (WASMModule *)module_reg;
  2130. export =
  2131. wasm_loader_find_export(module, module_name, global_name,
  2132. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2133. if (!export) {
  2134. return NULL;
  2135. }
  2136. /* resolve and check the global */
  2137. if (export->index < module->import_global_count) {
  2138. global =
  2139. module->import_globals[export->index].u.global.import_global_linked;
  2140. }
  2141. else {
  2142. global =
  2143. &(module->globals[export->index - module->import_global_count]);
  2144. }
  2145. if (global->type != type || global->is_mutable != is_mutable) {
  2146. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2147. module_name, global_name, global->type, global->is_mutable,
  2148. type, is_mutable);
  2149. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2150. return NULL;
  2151. }
  2152. return global;
  2153. }
  2154. #if WASM_ENABLE_TAGS != 0
  2155. static WASMTag *
  2156. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2157. const WASMType *expected_tag_type,
  2158. uint32 *linked_tag_index, char *error_buf,
  2159. uint32 error_buf_size)
  2160. {
  2161. WASMModuleCommon *module_reg;
  2162. WASMTag *tag = NULL;
  2163. WASMExport *export = NULL;
  2164. WASMModule *module = NULL;
  2165. module_reg = wasm_runtime_find_module_registered(module_name);
  2166. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2167. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2168. tag_name);
  2169. set_error_buf(error_buf, error_buf_size, "unknown import");
  2170. return NULL;
  2171. }
  2172. module = (WASMModule *)module_reg;
  2173. export =
  2174. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2175. error_buf, error_buf_size);
  2176. if (!export) {
  2177. return NULL;
  2178. }
  2179. /* resolve tag type and tag */
  2180. if (export->index < module->import_tag_count) {
  2181. /* importing an imported tag from the submodule */
  2182. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2183. }
  2184. else {
  2185. /* importing an section tag from the submodule */
  2186. tag = module->tags[export->index - module->import_tag_count];
  2187. }
  2188. /* check function type */
  2189. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2190. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2191. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2192. return NULL;
  2193. }
  2194. if (linked_tag_index != NULL) {
  2195. *linked_tag_index = export->index;
  2196. }
  2197. return tag;
  2198. }
  2199. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2200. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2201. static bool
  2202. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2203. const WASMModule *parent_module,
  2204. const char *sub_module_name, const char *function_name,
  2205. WASMFunctionImport *function, char *error_buf,
  2206. uint32 error_buf_size)
  2207. {
  2208. const uint8 *p = *p_buf, *p_end = buf_end;
  2209. uint32 declare_type_index = 0;
  2210. WASMFuncType *declare_func_type = NULL;
  2211. WASMFunction *linked_func = NULL;
  2212. #if WASM_ENABLE_MULTI_MODULE != 0
  2213. WASMModule *sub_module = NULL;
  2214. #endif
  2215. const char *linked_signature = NULL;
  2216. void *linked_attachment = NULL;
  2217. bool linked_call_conv_raw = false;
  2218. bool is_native_symbol = false;
  2219. read_leb_uint32(p, p_end, declare_type_index);
  2220. *p_buf = p;
  2221. if (declare_type_index >= parent_module->type_count) {
  2222. set_error_buf(error_buf, error_buf_size, "unknown type");
  2223. return false;
  2224. }
  2225. #if WASM_ENABLE_GC != 0
  2226. function->type_idx = declare_type_index;
  2227. #endif
  2228. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2229. declare_type_index = wasm_get_smallest_type_idx(
  2230. parent_module->types, parent_module->type_count, declare_type_index);
  2231. #endif
  2232. declare_func_type =
  2233. (WASMFuncType *)parent_module->types[declare_type_index];
  2234. /* lookup registered native symbols first */
  2235. linked_func = wasm_native_resolve_symbol(
  2236. sub_module_name, function_name, declare_func_type, &linked_signature,
  2237. &linked_attachment, &linked_call_conv_raw);
  2238. if (linked_func) {
  2239. is_native_symbol = true;
  2240. }
  2241. #if WASM_ENABLE_MULTI_MODULE != 0
  2242. else {
  2243. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2244. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2245. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2246. error_buf_size);
  2247. if (!sub_module) {
  2248. return false;
  2249. }
  2250. }
  2251. linked_func = wasm_loader_resolve_function(
  2252. sub_module_name, function_name, declare_func_type, error_buf,
  2253. error_buf_size);
  2254. }
  2255. #endif
  2256. function->module_name = (char *)sub_module_name;
  2257. function->field_name = (char *)function_name;
  2258. function->func_type = declare_func_type;
  2259. /* func_ptr_linked is for native registered symbol */
  2260. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2261. function->signature = linked_signature;
  2262. function->attachment = linked_attachment;
  2263. function->call_conv_raw = linked_call_conv_raw;
  2264. #if WASM_ENABLE_MULTI_MODULE != 0
  2265. function->import_module = is_native_symbol ? NULL : sub_module;
  2266. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2267. #endif
  2268. return true;
  2269. fail:
  2270. return false;
  2271. }
  2272. static bool
  2273. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2274. uint32 error_buf_size)
  2275. {
  2276. if (max_size < init_size) {
  2277. set_error_buf(error_buf, error_buf_size,
  2278. "size minimum must not be greater than maximum");
  2279. return false;
  2280. }
  2281. return true;
  2282. }
  2283. static bool
  2284. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2285. WASMModule *parent_module, const char *sub_module_name,
  2286. const char *table_name, WASMTableImport *table,
  2287. char *error_buf, uint32 error_buf_size)
  2288. {
  2289. const uint8 *p = *p_buf, *p_end = buf_end;
  2290. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2291. declare_init_size = 0, declare_max_size = 0;
  2292. #if WASM_ENABLE_MULTI_MODULE != 0
  2293. WASMModule *sub_module = NULL;
  2294. WASMTable *linked_table = NULL;
  2295. #endif
  2296. #if WASM_ENABLE_GC != 0
  2297. WASMRefType ref_type;
  2298. bool need_ref_type_map;
  2299. #endif
  2300. #if WASM_ENABLE_GC == 0
  2301. CHECK_BUF(p, p_end, 1);
  2302. /* 0x70 or 0x6F */
  2303. declare_elem_type = read_uint8(p);
  2304. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2305. #if WASM_ENABLE_REF_TYPES != 0
  2306. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2307. #endif
  2308. ) {
  2309. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2310. return false;
  2311. }
  2312. #else /* else of WASM_ENABLE_GC == 0 */
  2313. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2314. &need_ref_type_map, &ref_type, false, error_buf,
  2315. error_buf_size)) {
  2316. return false;
  2317. }
  2318. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2319. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2320. return false;
  2321. }
  2322. declare_elem_type = ref_type.ref_type;
  2323. if (need_ref_type_map) {
  2324. if (!(table->elem_ref_type =
  2325. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2326. error_buf, error_buf_size))) {
  2327. return false;
  2328. }
  2329. }
  2330. #if TRACE_WASM_LOADER != 0
  2331. os_printf("import table type: ");
  2332. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2333. os_printf("\n");
  2334. #endif
  2335. #endif /* end of WASM_ENABLE_GC == 0 */
  2336. read_leb_uint32(p, p_end, declare_max_size_flag);
  2337. if (declare_max_size_flag > 1) {
  2338. set_error_buf(error_buf, error_buf_size, "integer too large");
  2339. return false;
  2340. }
  2341. read_leb_uint32(p, p_end, declare_init_size);
  2342. if (declare_max_size_flag) {
  2343. read_leb_uint32(p, p_end, declare_max_size);
  2344. if (!check_table_max_size(declare_init_size, declare_max_size,
  2345. error_buf, error_buf_size))
  2346. return false;
  2347. }
  2348. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2349. &declare_max_size);
  2350. *p_buf = p;
  2351. #if WASM_ENABLE_MULTI_MODULE != 0
  2352. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2353. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2354. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2355. error_buf_size);
  2356. if (!sub_module) {
  2357. return false;
  2358. }
  2359. linked_table = wasm_loader_resolve_table(
  2360. sub_module_name, table_name, declare_init_size, declare_max_size,
  2361. error_buf, error_buf_size);
  2362. if (!linked_table) {
  2363. return false;
  2364. }
  2365. /* reset with linked table limit */
  2366. declare_elem_type = linked_table->elem_type;
  2367. declare_init_size = linked_table->init_size;
  2368. declare_max_size = linked_table->max_size;
  2369. declare_max_size_flag = linked_table->flags;
  2370. table->import_table_linked = linked_table;
  2371. table->import_module = sub_module;
  2372. }
  2373. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2374. /* (table (export "table") 10 20 funcref) */
  2375. /* we need this section working in wamrc */
  2376. if (!strcmp("spectest", sub_module_name)) {
  2377. const uint32 spectest_table_init_size = 10;
  2378. const uint32 spectest_table_max_size = 20;
  2379. if (strcmp("table", table_name)) {
  2380. set_error_buf(error_buf, error_buf_size,
  2381. "incompatible import type or unknown import");
  2382. return false;
  2383. }
  2384. if (declare_init_size > spectest_table_init_size
  2385. || declare_max_size < spectest_table_max_size) {
  2386. set_error_buf(error_buf, error_buf_size,
  2387. "incompatible import type");
  2388. return false;
  2389. }
  2390. declare_init_size = spectest_table_init_size;
  2391. declare_max_size = spectest_table_max_size;
  2392. }
  2393. /* now we believe all declaration are ok */
  2394. table->elem_type = declare_elem_type;
  2395. table->init_size = declare_init_size;
  2396. table->flags = declare_max_size_flag;
  2397. table->max_size = declare_max_size;
  2398. #if WASM_ENABLE_WAMR_COMPILER != 0
  2399. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2400. parent_module->is_ref_types_used = true;
  2401. #endif
  2402. (void)parent_module;
  2403. return true;
  2404. fail:
  2405. return false;
  2406. }
  2407. static bool
  2408. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2409. uint32 error_buf_size)
  2410. {
  2411. uint32 default_max_size =
  2412. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2413. if (!is_memory64 && init_size > default_max_size) {
  2414. set_error_buf(error_buf, error_buf_size,
  2415. "memory size must be at most 65536 pages (4GiB)");
  2416. return false;
  2417. }
  2418. #if WASM_ENABLE_MEMORY64 != 0
  2419. else if (is_memory64 && init_size > default_max_size) {
  2420. set_error_buf(
  2421. error_buf, error_buf_size,
  2422. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2423. return false;
  2424. }
  2425. #endif
  2426. return true;
  2427. }
  2428. static bool
  2429. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2430. char *error_buf, uint32 error_buf_size)
  2431. {
  2432. uint32 default_max_size =
  2433. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2434. if (max_size < init_size) {
  2435. set_error_buf(error_buf, error_buf_size,
  2436. "size minimum must not be greater than maximum");
  2437. return false;
  2438. }
  2439. if (!is_memory64 && max_size > default_max_size) {
  2440. set_error_buf(error_buf, error_buf_size,
  2441. "memory size must be at most 65536 pages (4GiB)");
  2442. return false;
  2443. }
  2444. #if WASM_ENABLE_MEMORY64 != 0
  2445. else if (is_memory64 && max_size > default_max_size) {
  2446. set_error_buf(
  2447. error_buf, error_buf_size,
  2448. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2449. return false;
  2450. }
  2451. #endif
  2452. return true;
  2453. }
  2454. static bool
  2455. check_memory_flag(const uint8 mem_flag, char *error_buf, uint32 error_buf_size)
  2456. {
  2457. /* Check whether certain features indicated by mem_flag are enabled in
  2458. * runtime */
  2459. if (mem_flag > MAX_PAGE_COUNT_FLAG) {
  2460. #if WASM_ENABLE_SHARED_MEMORY == 0
  2461. if (mem_flag & SHARED_MEMORY_FLAG) {
  2462. LOG_VERBOSE("shared memory flag was found, please enable shared "
  2463. "memory, lib-pthread or lib-wasi-threads");
  2464. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2465. return false;
  2466. }
  2467. #endif
  2468. #if WASM_ENABLE_MEMORY64 == 0
  2469. if (mem_flag & MEMORY64_FLAG) {
  2470. LOG_VERBOSE("memory64 flag was found, please enable memory64");
  2471. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2472. return false;
  2473. }
  2474. #endif
  2475. }
  2476. if (mem_flag > MAX_PAGE_COUNT_FLAG + SHARED_MEMORY_FLAG + MEMORY64_FLAG) {
  2477. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2478. return false;
  2479. }
  2480. else if ((mem_flag & SHARED_MEMORY_FLAG)
  2481. && !(mem_flag & MAX_PAGE_COUNT_FLAG)) {
  2482. set_error_buf(error_buf, error_buf_size,
  2483. "shared memory must have maximum");
  2484. return false;
  2485. }
  2486. return true;
  2487. }
  2488. static bool
  2489. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2490. WASMModule *parent_module, const char *sub_module_name,
  2491. const char *memory_name, WASMMemoryImport *memory,
  2492. char *error_buf, uint32 error_buf_size)
  2493. {
  2494. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2495. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2496. uint32 pool_size = wasm_runtime_memory_pool_size();
  2497. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2498. / DEFAULT_NUM_BYTES_PER_PAGE;
  2499. #else
  2500. uint32 max_page_count;
  2501. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2502. uint32 mem_flag = 0;
  2503. bool is_memory64 = false;
  2504. uint32 declare_init_page_count = 0;
  2505. uint32 declare_max_page_count = 0;
  2506. #if WASM_ENABLE_MULTI_MODULE != 0
  2507. WASMModule *sub_module = NULL;
  2508. WASMMemory *linked_memory = NULL;
  2509. #endif
  2510. p_org = p;
  2511. read_leb_uint32(p, p_end, mem_flag);
  2512. is_memory64 = mem_flag & MEMORY64_FLAG;
  2513. if (p - p_org > 1) {
  2514. LOG_VERBOSE("integer representation too long");
  2515. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2516. return false;
  2517. }
  2518. if (!check_memory_flag(mem_flag, error_buf, error_buf_size)) {
  2519. return false;
  2520. }
  2521. read_leb_uint32(p, p_end, declare_init_page_count);
  2522. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2523. error_buf_size)) {
  2524. return false;
  2525. }
  2526. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2527. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2528. #endif
  2529. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2530. read_leb_uint32(p, p_end, declare_max_page_count);
  2531. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2532. declare_max_page_count, error_buf,
  2533. error_buf_size)) {
  2534. return false;
  2535. }
  2536. if (declare_max_page_count > max_page_count) {
  2537. declare_max_page_count = max_page_count;
  2538. }
  2539. }
  2540. else {
  2541. /* Limit the maximum memory size to max_page_count */
  2542. declare_max_page_count = max_page_count;
  2543. }
  2544. #if WASM_ENABLE_MULTI_MODULE != 0
  2545. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2546. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2547. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2548. error_buf_size);
  2549. if (!sub_module) {
  2550. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2551. /* Avoid memory import failure when wasi-threads is enabled
  2552. and the memory is shared */
  2553. if (!(mem_flag & SHARED_MEMORY_FLAG))
  2554. return false;
  2555. #else
  2556. return false;
  2557. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2558. }
  2559. else {
  2560. linked_memory = wasm_loader_resolve_memory(
  2561. sub_module_name, memory_name, declare_init_page_count,
  2562. declare_max_page_count, error_buf, error_buf_size);
  2563. if (!linked_memory) {
  2564. return false;
  2565. }
  2566. /**
  2567. * reset with linked memory limit
  2568. */
  2569. memory->import_module = sub_module;
  2570. memory->import_memory_linked = linked_memory;
  2571. declare_init_page_count = linked_memory->init_page_count;
  2572. declare_max_page_count = linked_memory->max_page_count;
  2573. }
  2574. }
  2575. #endif
  2576. /* (memory (export "memory") 1 2) */
  2577. if (!strcmp("spectest", sub_module_name)) {
  2578. uint32 spectest_memory_init_page = 1;
  2579. uint32 spectest_memory_max_page = 2;
  2580. if (strcmp("memory", memory_name)) {
  2581. set_error_buf(error_buf, error_buf_size,
  2582. "incompatible import type or unknown import");
  2583. return false;
  2584. }
  2585. if (declare_init_page_count > spectest_memory_init_page
  2586. || declare_max_page_count < spectest_memory_max_page) {
  2587. set_error_buf(error_buf, error_buf_size,
  2588. "incompatible import type");
  2589. return false;
  2590. }
  2591. declare_init_page_count = spectest_memory_init_page;
  2592. declare_max_page_count = spectest_memory_max_page;
  2593. }
  2594. /* now we believe all declaration are ok */
  2595. memory->flags = mem_flag;
  2596. memory->init_page_count = declare_init_page_count;
  2597. memory->max_page_count = declare_max_page_count;
  2598. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2599. *p_buf = p;
  2600. (void)parent_module;
  2601. return true;
  2602. fail:
  2603. return false;
  2604. }
  2605. #if WASM_ENABLE_TAGS != 0
  2606. static bool
  2607. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2608. const WASMModule *parent_module, /* this module ! */
  2609. const char *sub_module_name, const char *tag_name,
  2610. WASMTagImport *tag, /* structure to fill */
  2611. char *error_buf, uint32 error_buf_size)
  2612. {
  2613. /* attribute and type of the import statement */
  2614. uint8 declare_tag_attribute;
  2615. uint32 declare_type_index;
  2616. const uint8 *p = *p_buf, *p_end = buf_end;
  2617. #if WASM_ENABLE_MULTI_MODULE != 0
  2618. WASMModule *sub_module = NULL;
  2619. #endif
  2620. /* get the one byte attribute */
  2621. CHECK_BUF(p, p_end, 1);
  2622. declare_tag_attribute = read_uint8(p);
  2623. if (declare_tag_attribute != 0) {
  2624. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2625. goto fail;
  2626. }
  2627. /* get type */
  2628. read_leb_uint32(p, p_end, declare_type_index);
  2629. /* compare against module->types */
  2630. if (declare_type_index >= parent_module->type_count) {
  2631. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2632. goto fail;
  2633. }
  2634. WASMType *declare_tag_type = parent_module->types[declare_type_index];
  2635. /* check, that the type of the declared tag returns void */
  2636. if (declare_tag_type->result_count != 0) {
  2637. set_error_buf(error_buf, error_buf_size,
  2638. "tag type signature does not return void");
  2639. goto fail;
  2640. }
  2641. #if WASM_ENABLE_MULTI_MODULE != 0
  2642. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2643. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2644. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2645. error_buf_size);
  2646. if (!sub_module) {
  2647. return false;
  2648. }
  2649. /* wasm_loader_resolve_tag checks, that the imported tag
  2650. * and the declared tag have the same type
  2651. */
  2652. uint32 linked_tag_index = 0;
  2653. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2654. sub_module_name, tag_name, declare_tag_type,
  2655. &linked_tag_index /* out */, error_buf, error_buf_size);
  2656. if (linked_tag) {
  2657. tag->import_module = sub_module;
  2658. tag->import_tag_linked = linked_tag;
  2659. tag->import_tag_index_linked = linked_tag_index;
  2660. }
  2661. }
  2662. #endif
  2663. /* store to module tag declarations */
  2664. tag->attribute = declare_tag_attribute;
  2665. tag->type = declare_type_index;
  2666. tag->module_name = (char *)sub_module_name;
  2667. tag->field_name = (char *)tag_name;
  2668. tag->tag_type = declare_tag_type;
  2669. *p_buf = p;
  2670. (void)parent_module;
  2671. LOG_VERBOSE("Load tag import success\n");
  2672. return true;
  2673. fail:
  2674. return false;
  2675. }
  2676. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2677. static bool
  2678. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2679. WASMModule *parent_module, char *sub_module_name,
  2680. char *global_name, WASMGlobalImport *global, char *error_buf,
  2681. uint32 error_buf_size)
  2682. {
  2683. const uint8 *p = *p_buf, *p_end = buf_end;
  2684. uint8 declare_type = 0;
  2685. uint8 declare_mutable = 0;
  2686. #if WASM_ENABLE_MULTI_MODULE != 0
  2687. WASMModule *sub_module = NULL;
  2688. WASMGlobal *linked_global = NULL;
  2689. #endif
  2690. #if WASM_ENABLE_GC != 0
  2691. WASMRefType ref_type;
  2692. bool need_ref_type_map;
  2693. #endif
  2694. bool ret = false;
  2695. #if WASM_ENABLE_GC == 0
  2696. CHECK_BUF(p, p_end, 2);
  2697. declare_type = read_uint8(p);
  2698. declare_mutable = read_uint8(p);
  2699. #else
  2700. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2701. &need_ref_type_map, &ref_type, false, error_buf,
  2702. error_buf_size)) {
  2703. return false;
  2704. }
  2705. declare_type = ref_type.ref_type;
  2706. if (need_ref_type_map) {
  2707. if (!(global->ref_type =
  2708. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2709. error_buf, error_buf_size))) {
  2710. return false;
  2711. }
  2712. }
  2713. #if TRACE_WASM_LOADER != 0
  2714. os_printf("import global type: ");
  2715. wasm_dump_value_type(declare_type, global->ref_type);
  2716. os_printf("\n");
  2717. #endif
  2718. CHECK_BUF(p, p_end, 1);
  2719. declare_mutable = read_uint8(p);
  2720. #endif /* end of WASM_ENABLE_GC == 0 */
  2721. *p_buf = p;
  2722. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2723. return false;
  2724. }
  2725. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2726. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2727. global);
  2728. if (ret) {
  2729. if (global->type != declare_type
  2730. || global->is_mutable != declare_mutable) {
  2731. set_error_buf(error_buf, error_buf_size,
  2732. "incompatible import type");
  2733. return false;
  2734. }
  2735. global->is_linked = true;
  2736. }
  2737. #endif
  2738. #if WASM_ENABLE_MULTI_MODULE != 0
  2739. if (!global->is_linked
  2740. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2741. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2742. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2743. error_buf_size);
  2744. if (!sub_module) {
  2745. return false;
  2746. }
  2747. /* check sub modules */
  2748. linked_global = wasm_loader_resolve_global(
  2749. sub_module_name, global_name, declare_type, declare_mutable,
  2750. error_buf, error_buf_size);
  2751. if (linked_global) {
  2752. global->import_module = sub_module;
  2753. global->import_global_linked = linked_global;
  2754. global->is_linked = true;
  2755. }
  2756. }
  2757. #endif
  2758. global->module_name = sub_module_name;
  2759. global->field_name = global_name;
  2760. global->type = declare_type;
  2761. global->is_mutable = (declare_mutable == 1);
  2762. #if WASM_ENABLE_WAMR_COMPILER != 0
  2763. if (global->type == VALUE_TYPE_V128)
  2764. parent_module->is_simd_used = true;
  2765. else if (global->type == VALUE_TYPE_EXTERNREF)
  2766. parent_module->is_ref_types_used = true;
  2767. #endif
  2768. (void)parent_module;
  2769. (void)ret;
  2770. return true;
  2771. fail:
  2772. return false;
  2773. }
  2774. static bool
  2775. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2776. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2777. {
  2778. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2779. #if WASM_ENABLE_GC != 0
  2780. WASMRefType ref_type;
  2781. bool need_ref_type_map;
  2782. #endif
  2783. #if WASM_ENABLE_GC == 0
  2784. CHECK_BUF(p, p_end, 1);
  2785. /* 0x70 or 0x6F */
  2786. table->elem_type = read_uint8(p);
  2787. if (VALUE_TYPE_FUNCREF != table->elem_type
  2788. #if WASM_ENABLE_REF_TYPES != 0
  2789. && VALUE_TYPE_EXTERNREF != table->elem_type
  2790. #endif
  2791. ) {
  2792. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2793. return false;
  2794. }
  2795. #else /* else of WASM_ENABLE_GC == 0 */
  2796. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2797. &need_ref_type_map, &ref_type, false, error_buf,
  2798. error_buf_size)) {
  2799. return false;
  2800. }
  2801. table->elem_type = ref_type.ref_type;
  2802. if (need_ref_type_map) {
  2803. if (!(table->elem_ref_type =
  2804. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2805. error_buf_size))) {
  2806. return false;
  2807. }
  2808. }
  2809. #if TRACE_WASM_LOADER != 0
  2810. os_printf("table type: ");
  2811. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2812. os_printf("\n");
  2813. #endif
  2814. #endif /* end of WASM_ENABLE_GC == 0 */
  2815. p_org = p;
  2816. read_leb_uint32(p, p_end, table->flags);
  2817. #if WASM_ENABLE_SHARED_MEMORY == 0
  2818. if (p - p_org > 1) {
  2819. set_error_buf(error_buf, error_buf_size,
  2820. "integer representation too long");
  2821. return false;
  2822. }
  2823. if (table->flags > 1) {
  2824. set_error_buf(error_buf, error_buf_size, "integer too large");
  2825. return false;
  2826. }
  2827. #else
  2828. if (p - p_org > 1) {
  2829. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2830. return false;
  2831. }
  2832. if (table->flags == 2) {
  2833. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2834. return false;
  2835. }
  2836. if (table->flags > 1) {
  2837. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2838. return false;
  2839. }
  2840. #endif
  2841. read_leb_uint32(p, p_end, table->init_size);
  2842. if (table->flags) {
  2843. read_leb_uint32(p, p_end, table->max_size);
  2844. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2845. error_buf_size))
  2846. return false;
  2847. }
  2848. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2849. #if WASM_ENABLE_WAMR_COMPILER != 0
  2850. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2851. module->is_ref_types_used = true;
  2852. #endif
  2853. *p_buf = p;
  2854. return true;
  2855. fail:
  2856. return false;
  2857. }
  2858. static bool
  2859. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2860. char *error_buf, uint32 error_buf_size)
  2861. {
  2862. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2863. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2864. uint32 pool_size = wasm_runtime_memory_pool_size();
  2865. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2866. / DEFAULT_NUM_BYTES_PER_PAGE;
  2867. #else
  2868. uint32 max_page_count;
  2869. #endif
  2870. bool is_memory64 = false;
  2871. p_org = p;
  2872. read_leb_uint32(p, p_end, memory->flags);
  2873. is_memory64 = memory->flags & MEMORY64_FLAG;
  2874. if (p - p_org > 1) {
  2875. LOG_VERBOSE("integer representation too long");
  2876. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2877. return false;
  2878. }
  2879. if (!check_memory_flag(memory->flags, error_buf, error_buf_size)) {
  2880. return false;
  2881. }
  2882. read_leb_uint32(p, p_end, memory->init_page_count);
  2883. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2884. error_buf_size))
  2885. return false;
  2886. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2887. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2888. #endif
  2889. if (memory->flags & 1) {
  2890. read_leb_uint32(p, p_end, memory->max_page_count);
  2891. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2892. memory->max_page_count, error_buf,
  2893. error_buf_size))
  2894. return false;
  2895. if (memory->max_page_count > max_page_count)
  2896. memory->max_page_count = max_page_count;
  2897. }
  2898. else {
  2899. /* Limit the maximum memory size to max_page_count */
  2900. memory->max_page_count = max_page_count;
  2901. }
  2902. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2903. *p_buf = p;
  2904. return true;
  2905. fail:
  2906. return false;
  2907. }
  2908. static bool
  2909. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2910. bool is_load_from_file_buf, char *error_buf,
  2911. uint32 error_buf_size)
  2912. {
  2913. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2914. uint32 import_count, name_len, type_index, i, u32, flags;
  2915. uint64 total_size;
  2916. WASMImport *import;
  2917. WASMImport *import_functions = NULL, *import_tables = NULL;
  2918. WASMImport *import_memories = NULL, *import_globals = NULL;
  2919. #if WASM_ENABLE_TAGS != 0
  2920. WASMImport *import_tags = NULL;
  2921. #endif
  2922. char *sub_module_name, *field_name;
  2923. uint8 u8, kind, global_type;
  2924. read_leb_uint32(p, p_end, import_count);
  2925. if (import_count) {
  2926. module->import_count = import_count;
  2927. total_size = sizeof(WASMImport) * (uint64)import_count;
  2928. if (!(module->imports =
  2929. loader_malloc(total_size, error_buf, error_buf_size))) {
  2930. return false;
  2931. }
  2932. p_old = p;
  2933. /* Scan firstly to get import count of each type */
  2934. for (i = 0; i < import_count; i++) {
  2935. /* module name */
  2936. read_leb_uint32(p, p_end, name_len);
  2937. CHECK_BUF(p, p_end, name_len);
  2938. p += name_len;
  2939. /* field name */
  2940. read_leb_uint32(p, p_end, name_len);
  2941. CHECK_BUF(p, p_end, name_len);
  2942. p += name_len;
  2943. CHECK_BUF(p, p_end, 1);
  2944. /* 0x00/0x01/0x02/0x03/0x04 */
  2945. kind = read_uint8(p);
  2946. switch (kind) {
  2947. case IMPORT_KIND_FUNC: /* import function */
  2948. read_leb_uint32(p, p_end, type_index);
  2949. module->import_function_count++;
  2950. break;
  2951. case IMPORT_KIND_TABLE: /* import table */
  2952. CHECK_BUF(p, p_end, 1);
  2953. /* 0x70 */
  2954. u8 = read_uint8(p);
  2955. read_leb_uint32(p, p_end, flags);
  2956. read_leb_uint32(p, p_end, u32);
  2957. if (flags & 1)
  2958. read_leb_uint32(p, p_end, u32);
  2959. module->import_table_count++;
  2960. if (module->import_table_count > 1) {
  2961. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2962. set_error_buf(error_buf, error_buf_size,
  2963. "multiple tables");
  2964. return false;
  2965. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2966. module->is_ref_types_used = true;
  2967. #endif
  2968. }
  2969. break;
  2970. case IMPORT_KIND_MEMORY: /* import memory */
  2971. read_leb_uint32(p, p_end, flags);
  2972. read_leb_uint32(p, p_end, u32);
  2973. if (flags & 1)
  2974. read_leb_uint32(p, p_end, u32);
  2975. module->import_memory_count++;
  2976. if (module->import_memory_count > 1) {
  2977. set_error_buf(error_buf, error_buf_size,
  2978. "multiple memories");
  2979. return false;
  2980. }
  2981. break;
  2982. #if WASM_ENABLE_TAGS != 0
  2983. case IMPORT_KIND_TAG: /* import tags */
  2984. /* it only counts the number of tags to import */
  2985. module->import_tag_count++;
  2986. CHECK_BUF(p, p_end, 1);
  2987. u8 = read_uint8(p);
  2988. read_leb_uint32(p, p_end, type_index);
  2989. break;
  2990. #endif
  2991. case IMPORT_KIND_GLOBAL: /* import global */
  2992. #if WASM_ENABLE_GC != 0
  2993. /* valtype */
  2994. CHECK_BUF(p, p_end, 1);
  2995. global_type = read_uint8(p);
  2996. if (wasm_is_type_multi_byte_type(global_type)) {
  2997. int32 heap_type;
  2998. read_leb_int32(p, p_end, heap_type);
  2999. (void)heap_type;
  3000. }
  3001. /* mutability */
  3002. CHECK_BUF(p, p_end, 1);
  3003. p += 1;
  3004. #else
  3005. CHECK_BUF(p, p_end, 2);
  3006. p += 2;
  3007. #endif
  3008. (void)global_type;
  3009. module->import_global_count++;
  3010. break;
  3011. default:
  3012. set_error_buf(error_buf, error_buf_size,
  3013. "invalid import kind");
  3014. return false;
  3015. }
  3016. }
  3017. if (module->import_function_count)
  3018. import_functions = module->import_functions = module->imports;
  3019. if (module->import_table_count)
  3020. import_tables = module->import_tables =
  3021. module->imports + module->import_function_count;
  3022. if (module->import_memory_count)
  3023. import_memories = module->import_memories =
  3024. module->imports + module->import_function_count
  3025. + module->import_table_count;
  3026. #if WASM_ENABLE_TAGS != 0
  3027. if (module->import_tag_count)
  3028. import_tags = module->import_tags =
  3029. module->imports + module->import_function_count
  3030. + module->import_table_count + module->import_memory_count;
  3031. if (module->import_global_count)
  3032. import_globals = module->import_globals =
  3033. module->imports + module->import_function_count
  3034. + module->import_table_count + module->import_memory_count
  3035. + module->import_tag_count;
  3036. #else
  3037. if (module->import_global_count)
  3038. import_globals = module->import_globals =
  3039. module->imports + module->import_function_count
  3040. + module->import_table_count + module->import_memory_count;
  3041. #endif
  3042. p = p_old;
  3043. /* Scan again to resolve the data */
  3044. for (i = 0; i < import_count; i++) {
  3045. /* load module name */
  3046. read_leb_uint32(p, p_end, name_len);
  3047. CHECK_BUF(p, p_end, name_len);
  3048. if (!(sub_module_name = wasm_const_str_list_insert(
  3049. p, name_len, module, is_load_from_file_buf, error_buf,
  3050. error_buf_size))) {
  3051. return false;
  3052. }
  3053. p += name_len;
  3054. /* load field name */
  3055. read_leb_uint32(p, p_end, name_len);
  3056. CHECK_BUF(p, p_end, name_len);
  3057. if (!(field_name = wasm_const_str_list_insert(
  3058. p, name_len, module, is_load_from_file_buf, error_buf,
  3059. error_buf_size))) {
  3060. return false;
  3061. }
  3062. p += name_len;
  3063. CHECK_BUF(p, p_end, 1);
  3064. /* 0x00/0x01/0x02/0x03/0x4 */
  3065. kind = read_uint8(p);
  3066. switch (kind) {
  3067. case IMPORT_KIND_FUNC: /* import function */
  3068. bh_assert(import_functions);
  3069. import = import_functions++;
  3070. if (!load_function_import(
  3071. &p, p_end, module, sub_module_name, field_name,
  3072. &import->u.function, error_buf, error_buf_size)) {
  3073. return false;
  3074. }
  3075. break;
  3076. case IMPORT_KIND_TABLE: /* import table */
  3077. bh_assert(import_tables);
  3078. import = import_tables++;
  3079. if (!load_table_import(&p, p_end, module, sub_module_name,
  3080. field_name, &import->u.table,
  3081. error_buf, error_buf_size)) {
  3082. LOG_DEBUG("can not import such a table (%s,%s)",
  3083. sub_module_name, field_name);
  3084. return false;
  3085. }
  3086. break;
  3087. case IMPORT_KIND_MEMORY: /* import memory */
  3088. bh_assert(import_memories);
  3089. import = import_memories++;
  3090. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3091. field_name, &import->u.memory,
  3092. error_buf, error_buf_size)) {
  3093. return false;
  3094. }
  3095. break;
  3096. #if WASM_ENABLE_TAGS != 0
  3097. case IMPORT_KIND_TAG:
  3098. bh_assert(import_tags);
  3099. import = import_tags++;
  3100. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3101. field_name, &import->u.tag, error_buf,
  3102. error_buf_size)) {
  3103. return false;
  3104. }
  3105. break;
  3106. #endif
  3107. case IMPORT_KIND_GLOBAL: /* import global */
  3108. bh_assert(import_globals);
  3109. import = import_globals++;
  3110. if (!load_global_import(&p, p_end, module, sub_module_name,
  3111. field_name, &import->u.global,
  3112. error_buf, error_buf_size)) {
  3113. return false;
  3114. }
  3115. break;
  3116. default:
  3117. set_error_buf(error_buf, error_buf_size,
  3118. "invalid import kind");
  3119. return false;
  3120. }
  3121. import->kind = kind;
  3122. import->u.names.module_name = sub_module_name;
  3123. import->u.names.field_name = field_name;
  3124. }
  3125. #if WASM_ENABLE_LIBC_WASI != 0
  3126. import = module->import_functions;
  3127. for (i = 0; i < module->import_function_count; i++, import++) {
  3128. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3129. || !strcmp(import->u.names.module_name,
  3130. "wasi_snapshot_preview1")) {
  3131. module->import_wasi_api = true;
  3132. break;
  3133. }
  3134. }
  3135. #endif
  3136. }
  3137. if (p != p_end) {
  3138. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3139. return false;
  3140. }
  3141. LOG_VERBOSE("Load import section success.\n");
  3142. (void)u8;
  3143. (void)u32;
  3144. (void)type_index;
  3145. return true;
  3146. fail:
  3147. return false;
  3148. }
  3149. static bool
  3150. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3151. uint32 error_buf_size)
  3152. {
  3153. WASMFuncType *param_type = func->func_type;
  3154. uint32 param_count = param_type->param_count;
  3155. uint8 *param_types = param_type->types;
  3156. uint32 local_count = func->local_count;
  3157. uint8 *local_types = func->local_types;
  3158. uint32 i, local_offset = 0;
  3159. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3160. /*
  3161. * Only allocate memory when total_size is not 0,
  3162. * or the return value of malloc(0) might be NULL on some platforms,
  3163. * which causes wasm loader return false.
  3164. */
  3165. if (total_size > 0
  3166. && !(func->local_offsets =
  3167. loader_malloc(total_size, error_buf, error_buf_size))) {
  3168. return false;
  3169. }
  3170. for (i = 0; i < param_count; i++) {
  3171. func->local_offsets[i] = (uint16)local_offset;
  3172. local_offset += wasm_value_type_cell_num(param_types[i]);
  3173. }
  3174. for (i = 0; i < local_count; i++) {
  3175. func->local_offsets[param_count + i] = (uint16)local_offset;
  3176. local_offset += wasm_value_type_cell_num(local_types[i]);
  3177. }
  3178. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3179. return true;
  3180. }
  3181. static bool
  3182. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3183. const uint8 *buf_code, const uint8 *buf_code_end,
  3184. WASMModule *module, char *error_buf,
  3185. uint32 error_buf_size)
  3186. {
  3187. const uint8 *p = buf, *p_end = buf_end;
  3188. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3189. uint32 func_count;
  3190. uint64 total_size;
  3191. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3192. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3193. uint8 type;
  3194. WASMFunction *func;
  3195. #if WASM_ENABLE_GC != 0
  3196. bool need_ref_type_map;
  3197. WASMRefType ref_type;
  3198. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3199. #endif
  3200. read_leb_uint32(p, p_end, func_count);
  3201. if (buf_code)
  3202. read_leb_uint32(p_code, buf_code_end, code_count);
  3203. if (func_count != code_count) {
  3204. set_error_buf(error_buf, error_buf_size,
  3205. "function and code section have inconsistent lengths or "
  3206. "unexpected end");
  3207. return false;
  3208. }
  3209. if (func_count) {
  3210. module->function_count = func_count;
  3211. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3212. if (!(module->functions =
  3213. loader_malloc(total_size, error_buf, error_buf_size))) {
  3214. return false;
  3215. }
  3216. for (i = 0; i < func_count; i++) {
  3217. /* Resolve function type */
  3218. read_leb_uint32(p, p_end, type_index);
  3219. if (type_index >= module->type_count) {
  3220. set_error_buf(error_buf, error_buf_size, "unknown type");
  3221. return false;
  3222. }
  3223. #if WASM_ENABLE_GC != 0
  3224. type_index_org = type_index;
  3225. #endif
  3226. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3227. && WASM_ENABLE_GC == 0
  3228. type_index = wasm_get_smallest_type_idx(
  3229. module->types, module->type_count, type_index);
  3230. #endif
  3231. read_leb_uint32(p_code, buf_code_end, code_size);
  3232. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3233. set_error_buf(error_buf, error_buf_size,
  3234. "invalid function code size");
  3235. return false;
  3236. }
  3237. /* Resolve local set count */
  3238. p_code_end = p_code + code_size;
  3239. local_count = 0;
  3240. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3241. p_code_save = p_code;
  3242. #if WASM_ENABLE_GC != 0
  3243. ref_type_map_count = 0;
  3244. #endif
  3245. /* Calculate total local count */
  3246. for (j = 0; j < local_set_count; j++) {
  3247. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3248. if (sub_local_count > UINT32_MAX - local_count) {
  3249. set_error_buf(error_buf, error_buf_size, "too many locals");
  3250. return false;
  3251. }
  3252. #if WASM_ENABLE_GC == 0
  3253. CHECK_BUF(p_code, buf_code_end, 1);
  3254. /* 0x7F/0x7E/0x7D/0x7C */
  3255. type = read_uint8(p_code);
  3256. local_count += sub_local_count;
  3257. #if WASM_ENABLE_WAMR_COMPILER != 0
  3258. /* If any value's type is v128, mark the module as SIMD used */
  3259. if (type == VALUE_TYPE_V128)
  3260. module->is_simd_used = true;
  3261. #endif
  3262. #else
  3263. if (!resolve_value_type(&p_code, buf_code_end, module,
  3264. module->type_count, &need_ref_type_map,
  3265. &ref_type, false, error_buf,
  3266. error_buf_size)) {
  3267. return false;
  3268. }
  3269. local_count += sub_local_count;
  3270. if (need_ref_type_map)
  3271. ref_type_map_count += sub_local_count;
  3272. #endif
  3273. }
  3274. /* Alloc memory, layout: function structure + local types */
  3275. code_size = (uint32)(p_code_end - p_code);
  3276. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3277. if (!(func = module->functions[i] =
  3278. loader_malloc(total_size, error_buf, error_buf_size))) {
  3279. return false;
  3280. }
  3281. #if WASM_ENABLE_GC != 0
  3282. if (ref_type_map_count > 0) {
  3283. total_size =
  3284. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3285. if (!(func->local_ref_type_maps = loader_malloc(
  3286. total_size, error_buf, error_buf_size))) {
  3287. return false;
  3288. }
  3289. func->local_ref_type_map_count = ref_type_map_count;
  3290. }
  3291. #endif
  3292. /* Set function type, local count, code size and code body */
  3293. func->func_type = (WASMFuncType *)module->types[type_index];
  3294. func->local_count = local_count;
  3295. if (local_count > 0)
  3296. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3297. func->code_size = code_size;
  3298. /*
  3299. * we shall make a copy of code body [p_code, p_code + code_size]
  3300. * when we are worrying about inappropriate releasing behaviour.
  3301. * all code bodies are actually in a buffer which user allocates in
  3302. * his embedding environment and we don't have power on them.
  3303. * it will be like:
  3304. * code_body_cp = malloc(code_size);
  3305. * memcpy(code_body_cp, p_code, code_size);
  3306. * func->code = code_body_cp;
  3307. */
  3308. func->code = (uint8 *)p_code;
  3309. #if WASM_ENABLE_GC != 0
  3310. func->type_idx = type_index_org;
  3311. #endif
  3312. #if WASM_ENABLE_GC != 0
  3313. t = 0;
  3314. #endif
  3315. /* Load each local type */
  3316. p_code = p_code_save;
  3317. local_type_index = 0;
  3318. for (j = 0; j < local_set_count; j++) {
  3319. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3320. /* Note: sub_local_count is allowed to be 0 */
  3321. if (local_type_index > UINT32_MAX - sub_local_count
  3322. || local_type_index + sub_local_count > local_count) {
  3323. set_error_buf(error_buf, error_buf_size,
  3324. "invalid local count");
  3325. return false;
  3326. }
  3327. #if WASM_ENABLE_GC == 0
  3328. CHECK_BUF(p_code, buf_code_end, 1);
  3329. /* 0x7F/0x7E/0x7D/0x7C */
  3330. type = read_uint8(p_code);
  3331. if (!is_value_type(type)) {
  3332. if (type == VALUE_TYPE_V128)
  3333. set_error_buf(error_buf, error_buf_size,
  3334. "v128 value type requires simd feature");
  3335. else if (type == VALUE_TYPE_FUNCREF
  3336. || type == VALUE_TYPE_EXTERNREF)
  3337. set_error_buf(error_buf, error_buf_size,
  3338. "ref value type requires "
  3339. "reference types feature");
  3340. else
  3341. set_error_buf_v(error_buf, error_buf_size,
  3342. "invalid local type 0x%02X", type);
  3343. return false;
  3344. }
  3345. #else
  3346. if (!resolve_value_type(&p_code, buf_code_end, module,
  3347. module->type_count, &need_ref_type_map,
  3348. &ref_type, false, error_buf,
  3349. error_buf_size)) {
  3350. return false;
  3351. }
  3352. if (need_ref_type_map) {
  3353. WASMRefType *ref_type_tmp;
  3354. if (!(ref_type_tmp = reftype_set_insert(
  3355. module->ref_type_set, &ref_type, error_buf,
  3356. error_buf_size))) {
  3357. return false;
  3358. }
  3359. for (k = 0; k < sub_local_count; k++) {
  3360. func->local_ref_type_maps[t + k].ref_type =
  3361. ref_type_tmp;
  3362. func->local_ref_type_maps[t + k].index =
  3363. local_type_index + k;
  3364. }
  3365. t += sub_local_count;
  3366. }
  3367. type = ref_type.ref_type;
  3368. #endif
  3369. for (k = 0; k < sub_local_count; k++) {
  3370. func->local_types[local_type_index++] = type;
  3371. }
  3372. #if WASM_ENABLE_WAMR_COMPILER != 0
  3373. if (type == VALUE_TYPE_V128)
  3374. module->is_simd_used = true;
  3375. else if (type == VALUE_TYPE_FUNCREF
  3376. || type == VALUE_TYPE_EXTERNREF)
  3377. module->is_ref_types_used = true;
  3378. #endif
  3379. }
  3380. bh_assert(local_type_index == func->local_count);
  3381. #if WASM_ENABLE_GC != 0
  3382. bh_assert(t == func->local_ref_type_map_count);
  3383. #if TRACE_WASM_LOADER != 0
  3384. os_printf("func %u, local types: [", i);
  3385. k = 0;
  3386. for (j = 0; j < func->local_count; j++) {
  3387. WASMRefType *ref_type_tmp = NULL;
  3388. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3389. bh_assert(j == func->local_ref_type_maps[k].index);
  3390. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3391. }
  3392. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3393. if (j < func->local_count - 1)
  3394. os_printf(" ");
  3395. }
  3396. os_printf("]\n");
  3397. #endif
  3398. #endif
  3399. func->param_cell_num = func->func_type->param_cell_num;
  3400. func->ret_cell_num = func->func_type->ret_cell_num;
  3401. local_cell_num =
  3402. wasm_get_cell_num(func->local_types, func->local_count);
  3403. if (local_cell_num > UINT16_MAX) {
  3404. set_error_buf(error_buf, error_buf_size,
  3405. "local count too large");
  3406. return false;
  3407. }
  3408. func->local_cell_num = (uint16)local_cell_num;
  3409. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3410. return false;
  3411. p_code = p_code_end;
  3412. }
  3413. }
  3414. if (p != p_end) {
  3415. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3416. return false;
  3417. }
  3418. LOG_VERBOSE("Load function section success.\n");
  3419. return true;
  3420. fail:
  3421. return false;
  3422. }
  3423. static bool
  3424. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3425. char *error_buf, uint32 error_buf_size)
  3426. {
  3427. const uint8 *p = buf, *p_end = buf_end;
  3428. uint32 table_count, i;
  3429. uint64 total_size;
  3430. WASMTable *table;
  3431. read_leb_uint32(p, p_end, table_count);
  3432. if (module->import_table_count + table_count > 1) {
  3433. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3434. /* a total of one table is allowed */
  3435. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3436. return false;
  3437. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3438. module->is_ref_types_used = true;
  3439. #endif
  3440. }
  3441. if (table_count) {
  3442. module->table_count = table_count;
  3443. total_size = sizeof(WASMTable) * (uint64)table_count;
  3444. if (!(module->tables =
  3445. loader_malloc(total_size, error_buf, error_buf_size))) {
  3446. return false;
  3447. }
  3448. /* load each table */
  3449. table = module->tables;
  3450. for (i = 0; i < table_count; i++, table++) {
  3451. #if WASM_ENABLE_GC != 0
  3452. uint8 flag;
  3453. bool has_init = false;
  3454. CHECK_BUF(buf, buf_end, 1);
  3455. flag = read_uint8(p);
  3456. if (flag == TABLE_INIT_EXPR_FLAG) {
  3457. CHECK_BUF(buf, buf_end, 1);
  3458. flag = read_uint8(p);
  3459. if (flag != 0x00) {
  3460. set_error_buf(error_buf, error_buf_size,
  3461. "invalid leading bytes for table");
  3462. return false;
  3463. }
  3464. has_init = true;
  3465. }
  3466. else {
  3467. p--;
  3468. }
  3469. #endif /* end of WASM_ENABLE_GC != 0 */
  3470. if (!load_table(&p, p_end, module, table, error_buf,
  3471. error_buf_size))
  3472. return false;
  3473. #if WASM_ENABLE_GC != 0
  3474. if (has_init) {
  3475. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3476. table->elem_type, table->elem_ref_type,
  3477. error_buf, error_buf_size))
  3478. return false;
  3479. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3480. && table->init_expr.init_expr_type
  3481. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3482. set_error_buf(
  3483. error_buf, error_buf_size,
  3484. "unsupported initializer expression for table");
  3485. return false;
  3486. }
  3487. }
  3488. else {
  3489. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3490. set_error_buf(
  3491. error_buf, error_buf_size,
  3492. "type mismatch: non-nullable table without init expr");
  3493. return false;
  3494. }
  3495. }
  3496. #endif /* end of WASM_ENABLE_GC != 0 */
  3497. #if WASM_ENABLE_WAMR_COMPILER != 0
  3498. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3499. module->is_ref_types_used = true;
  3500. #endif
  3501. }
  3502. }
  3503. if (p != p_end) {
  3504. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3505. return false;
  3506. }
  3507. LOG_VERBOSE("Load table section success.\n");
  3508. return true;
  3509. fail:
  3510. return false;
  3511. }
  3512. static bool
  3513. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3514. char *error_buf, uint32 error_buf_size)
  3515. {
  3516. const uint8 *p = buf, *p_end = buf_end;
  3517. uint32 memory_count, i;
  3518. uint64 total_size;
  3519. WASMMemory *memory;
  3520. read_leb_uint32(p, p_end, memory_count);
  3521. /* a total of one memory is allowed */
  3522. if (module->import_memory_count + memory_count > 1) {
  3523. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3524. return false;
  3525. }
  3526. if (memory_count) {
  3527. module->memory_count = memory_count;
  3528. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3529. if (!(module->memories =
  3530. loader_malloc(total_size, error_buf, error_buf_size))) {
  3531. return false;
  3532. }
  3533. /* load each memory */
  3534. memory = module->memories;
  3535. for (i = 0; i < memory_count; i++, memory++)
  3536. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3537. return false;
  3538. }
  3539. if (p != p_end) {
  3540. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3541. return false;
  3542. }
  3543. LOG_VERBOSE("Load memory section success.\n");
  3544. return true;
  3545. fail:
  3546. return false;
  3547. }
  3548. static bool
  3549. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3550. char *error_buf, uint32 error_buf_size)
  3551. {
  3552. const uint8 *p = buf, *p_end = buf_end;
  3553. uint32 global_count, i;
  3554. uint64 total_size;
  3555. WASMGlobal *global;
  3556. uint8 mutable;
  3557. #if WASM_ENABLE_GC != 0
  3558. bool need_ref_type_map;
  3559. WASMRefType ref_type;
  3560. #endif
  3561. read_leb_uint32(p, p_end, global_count);
  3562. module->global_count = 0;
  3563. if (global_count) {
  3564. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3565. if (!(module->globals =
  3566. loader_malloc(total_size, error_buf, error_buf_size))) {
  3567. return false;
  3568. }
  3569. global = module->globals;
  3570. for (i = 0; i < global_count; i++, global++) {
  3571. #if WASM_ENABLE_GC == 0
  3572. CHECK_BUF(p, p_end, 2);
  3573. global->type = read_uint8(p);
  3574. mutable = read_uint8(p);
  3575. #else
  3576. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3577. &need_ref_type_map, &ref_type, false,
  3578. error_buf, error_buf_size)) {
  3579. return false;
  3580. }
  3581. global->type = ref_type.ref_type;
  3582. CHECK_BUF(p, p_end, 1);
  3583. mutable = read_uint8(p);
  3584. #endif /* end of WASM_ENABLE_GC */
  3585. #if WASM_ENABLE_WAMR_COMPILER != 0
  3586. if (global->type == VALUE_TYPE_V128)
  3587. module->is_simd_used = true;
  3588. else if (global->type == VALUE_TYPE_FUNCREF
  3589. || global->type == VALUE_TYPE_EXTERNREF)
  3590. module->is_ref_types_used = true;
  3591. #endif
  3592. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3593. return false;
  3594. }
  3595. global->is_mutable = mutable ? true : false;
  3596. /* initialize expression */
  3597. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3598. global->type,
  3599. #if WASM_ENABLE_GC == 0
  3600. NULL,
  3601. #else
  3602. &ref_type,
  3603. #endif
  3604. error_buf, error_buf_size))
  3605. return false;
  3606. #if WASM_ENABLE_GC != 0
  3607. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3608. uint8 global_type;
  3609. WASMRefType *global_ref_type;
  3610. uint32 global_idx = global->init_expr.u.global_index;
  3611. if (global->init_expr.u.global_index
  3612. >= module->import_global_count + i) {
  3613. set_error_buf(error_buf, error_buf_size, "unknown global");
  3614. return false;
  3615. }
  3616. if (global_idx < module->import_global_count) {
  3617. global_type =
  3618. module->import_globals[global_idx].u.global.type;
  3619. global_ref_type =
  3620. module->import_globals[global_idx].u.global.ref_type;
  3621. }
  3622. else {
  3623. global_type =
  3624. module
  3625. ->globals[global_idx - module->import_global_count]
  3626. .type;
  3627. global_ref_type =
  3628. module
  3629. ->globals[global_idx - module->import_global_count]
  3630. .ref_type;
  3631. }
  3632. if (!wasm_reftype_is_subtype_of(
  3633. global_type, global_ref_type, global->type,
  3634. global->ref_type, module->types, module->type_count)) {
  3635. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3636. return false;
  3637. }
  3638. }
  3639. if (need_ref_type_map) {
  3640. if (!(global->ref_type =
  3641. reftype_set_insert(module->ref_type_set, &ref_type,
  3642. error_buf, error_buf_size))) {
  3643. return false;
  3644. }
  3645. }
  3646. #if TRACE_WASM_LOADER != 0
  3647. os_printf("global type: ");
  3648. wasm_dump_value_type(global->type, global->ref_type);
  3649. os_printf("\n");
  3650. #endif
  3651. #endif
  3652. module->global_count++;
  3653. }
  3654. bh_assert(module->global_count == global_count);
  3655. }
  3656. if (p != p_end) {
  3657. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3658. return false;
  3659. }
  3660. LOG_VERBOSE("Load global section success.\n");
  3661. return true;
  3662. fail:
  3663. return false;
  3664. }
  3665. static bool
  3666. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3667. bool is_load_from_file_buf, char *error_buf,
  3668. uint32 error_buf_size)
  3669. {
  3670. const uint8 *p = buf, *p_end = buf_end;
  3671. uint32 export_count, i, j, index;
  3672. uint64 total_size;
  3673. uint32 str_len;
  3674. WASMExport *export;
  3675. const char *name;
  3676. read_leb_uint32(p, p_end, export_count);
  3677. if (export_count) {
  3678. module->export_count = export_count;
  3679. total_size = sizeof(WASMExport) * (uint64)export_count;
  3680. if (!(module->exports =
  3681. loader_malloc(total_size, error_buf, error_buf_size))) {
  3682. return false;
  3683. }
  3684. export = module->exports;
  3685. for (i = 0; i < export_count; i++, export ++) {
  3686. #if WASM_ENABLE_THREAD_MGR == 0
  3687. if (p == p_end) {
  3688. /* export section with inconsistent count:
  3689. n export declared, but less than n given */
  3690. set_error_buf(error_buf, error_buf_size,
  3691. "length out of bounds");
  3692. return false;
  3693. }
  3694. #endif
  3695. read_leb_uint32(p, p_end, str_len);
  3696. CHECK_BUF(p, p_end, str_len);
  3697. for (j = 0; j < i; j++) {
  3698. name = module->exports[j].name;
  3699. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3700. set_error_buf(error_buf, error_buf_size,
  3701. "duplicate export name");
  3702. return false;
  3703. }
  3704. }
  3705. if (!(export->name = wasm_const_str_list_insert(
  3706. p, str_len, module, is_load_from_file_buf, error_buf,
  3707. error_buf_size))) {
  3708. return false;
  3709. }
  3710. p += str_len;
  3711. CHECK_BUF(p, p_end, 1);
  3712. export->kind = read_uint8(p);
  3713. read_leb_uint32(p, p_end, index);
  3714. export->index = index;
  3715. switch (export->kind) {
  3716. /* function index */
  3717. case EXPORT_KIND_FUNC:
  3718. if (index >= module->function_count
  3719. + module->import_function_count) {
  3720. set_error_buf(error_buf, error_buf_size,
  3721. "unknown function");
  3722. return false;
  3723. }
  3724. #if WASM_ENABLE_SIMD != 0
  3725. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3726. /* TODO: check func type, if it has v128 param or result,
  3727. report error */
  3728. #endif
  3729. #endif
  3730. break;
  3731. /* table index */
  3732. case EXPORT_KIND_TABLE:
  3733. if (index
  3734. >= module->table_count + module->import_table_count) {
  3735. set_error_buf(error_buf, error_buf_size,
  3736. "unknown table");
  3737. return false;
  3738. }
  3739. break;
  3740. /* memory index */
  3741. case EXPORT_KIND_MEMORY:
  3742. if (index
  3743. >= module->memory_count + module->import_memory_count) {
  3744. set_error_buf(error_buf, error_buf_size,
  3745. "unknown memory");
  3746. return false;
  3747. }
  3748. break;
  3749. #if WASM_ENABLE_TAGS != 0
  3750. /* export tag */
  3751. case EXPORT_KIND_TAG:
  3752. if (index >= module->tag_count + module->import_tag_count) {
  3753. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3754. return false;
  3755. }
  3756. break;
  3757. #endif
  3758. /* global index */
  3759. case EXPORT_KIND_GLOBAL:
  3760. if (index
  3761. >= module->global_count + module->import_global_count) {
  3762. set_error_buf(error_buf, error_buf_size,
  3763. "unknown global");
  3764. return false;
  3765. }
  3766. break;
  3767. default:
  3768. set_error_buf(error_buf, error_buf_size,
  3769. "invalid export kind");
  3770. return false;
  3771. }
  3772. }
  3773. }
  3774. if (p != p_end) {
  3775. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3776. return false;
  3777. }
  3778. LOG_VERBOSE("Load export section success.\n");
  3779. return true;
  3780. fail:
  3781. return false;
  3782. }
  3783. static bool
  3784. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3785. uint32 error_buf_size)
  3786. {
  3787. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3788. if (table_index != 0) {
  3789. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3790. return false;
  3791. }
  3792. #endif
  3793. if (table_index >= module->import_table_count + module->table_count) {
  3794. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3795. table_index);
  3796. return false;
  3797. }
  3798. return true;
  3799. }
  3800. static bool
  3801. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3802. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3803. {
  3804. const uint8 *p = *p_buf, *p_end = buf_end;
  3805. uint32 table_index;
  3806. read_leb_uint32(p, p_end, table_index);
  3807. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3808. return false;
  3809. }
  3810. *p_table_index = table_index;
  3811. *p_buf = p;
  3812. return true;
  3813. fail:
  3814. return false;
  3815. }
  3816. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3817. static bool
  3818. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3819. uint32 *p_elem_type,
  3820. #if WASM_ENABLE_GC != 0
  3821. WASMRefType **p_elem_ref_type,
  3822. #endif
  3823. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3824. {
  3825. const uint8 *p = *p_buf, *p_end = buf_end;
  3826. uint8 elem_type;
  3827. #if WASM_ENABLE_GC != 0
  3828. WASMRefType elem_ref_type;
  3829. bool need_ref_type_map;
  3830. #endif
  3831. CHECK_BUF(p, p_end, 1);
  3832. elem_type = read_uint8(p);
  3833. if (elemkind_zero) {
  3834. if (elem_type != 0) {
  3835. set_error_buf(error_buf, error_buf_size,
  3836. "invalid reference type or unknown type");
  3837. return false;
  3838. }
  3839. else {
  3840. *p_elem_type = VALUE_TYPE_FUNCREF;
  3841. *p_buf = p;
  3842. return true;
  3843. }
  3844. }
  3845. #if WASM_ENABLE_GC == 0
  3846. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3847. set_error_buf(error_buf, error_buf_size,
  3848. "invalid reference type or unknown type");
  3849. return false;
  3850. }
  3851. *p_elem_type = elem_type;
  3852. #else
  3853. p--;
  3854. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3855. module->type_count, &need_ref_type_map,
  3856. &elem_ref_type, false, error_buf, error_buf_size)) {
  3857. return false;
  3858. }
  3859. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3860. set_error_buf(error_buf, error_buf_size,
  3861. "invalid reference type or unknown type");
  3862. return false;
  3863. }
  3864. *p_elem_type = elem_ref_type.ref_type;
  3865. if (need_ref_type_map) {
  3866. if (!(*p_elem_ref_type =
  3867. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3868. error_buf, error_buf_size))) {
  3869. return false;
  3870. }
  3871. }
  3872. #endif
  3873. *p_buf = p;
  3874. return true;
  3875. fail:
  3876. return false;
  3877. }
  3878. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3879. static bool
  3880. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3881. WASMModule *module, WASMTableSeg *table_segment,
  3882. char *error_buf, uint32 error_buf_size)
  3883. {
  3884. const uint8 *p = *p_buf, *p_end = buf_end;
  3885. uint32 function_count, function_index = 0, i;
  3886. uint64 total_size;
  3887. read_leb_uint32(p, p_end, function_count);
  3888. table_segment->value_count = function_count;
  3889. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3890. if (total_size > 0
  3891. && !(table_segment->init_values =
  3892. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3893. error_buf_size))) {
  3894. return false;
  3895. }
  3896. for (i = 0; i < function_count; i++) {
  3897. InitializerExpression *init_expr = &table_segment->init_values[i];
  3898. read_leb_uint32(p, p_end, function_index);
  3899. if (!check_function_index(module, function_index, error_buf,
  3900. error_buf_size)) {
  3901. return false;
  3902. }
  3903. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3904. init_expr->u.ref_index = function_index;
  3905. }
  3906. *p_buf = p;
  3907. return true;
  3908. fail:
  3909. return false;
  3910. }
  3911. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3912. static bool
  3913. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3914. WASMModule *module, WASMTableSeg *table_segment,
  3915. char *error_buf, uint32 error_buf_size)
  3916. {
  3917. const uint8 *p = *p_buf, *p_end = buf_end;
  3918. uint32 ref_count, i;
  3919. uint64 total_size;
  3920. read_leb_uint32(p, p_end, ref_count);
  3921. table_segment->value_count = ref_count;
  3922. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3923. if (total_size > 0
  3924. && !(table_segment->init_values =
  3925. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3926. error_buf_size))) {
  3927. return false;
  3928. }
  3929. for (i = 0; i < ref_count; i++) {
  3930. InitializerExpression *init_expr = &table_segment->init_values[i];
  3931. if (!load_init_expr(module, &p, p_end, init_expr,
  3932. table_segment->elem_type,
  3933. #if WASM_ENABLE_GC == 0
  3934. NULL,
  3935. #else
  3936. table_segment->elem_ref_type,
  3937. #endif
  3938. error_buf, error_buf_size))
  3939. return false;
  3940. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3941. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3942. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3943. && init_expr->init_expr_type
  3944. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3945. }
  3946. *p_buf = p;
  3947. return true;
  3948. fail:
  3949. return false;
  3950. }
  3951. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3952. static bool
  3953. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3954. WASMModule *module, char *error_buf,
  3955. uint32 error_buf_size)
  3956. {
  3957. const uint8 *p = buf, *p_end = buf_end;
  3958. uint32 table_segment_count, i;
  3959. uint64 total_size;
  3960. WASMTableSeg *table_segment;
  3961. read_leb_uint32(p, p_end, table_segment_count);
  3962. if (table_segment_count) {
  3963. module->table_seg_count = table_segment_count;
  3964. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3965. if (!(module->table_segments =
  3966. loader_malloc(total_size, error_buf, error_buf_size))) {
  3967. return false;
  3968. }
  3969. table_segment = module->table_segments;
  3970. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3971. if (p >= p_end) {
  3972. set_error_buf(error_buf, error_buf_size,
  3973. "invalid value type or "
  3974. "invalid elements segment kind");
  3975. return false;
  3976. }
  3977. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3978. read_leb_uint32(p, p_end, table_segment->mode);
  3979. /* last three bits */
  3980. table_segment->mode = table_segment->mode & 0x07;
  3981. switch (table_segment->mode) {
  3982. /* elemkind/elemtype + active */
  3983. case 0:
  3984. case 4:
  3985. {
  3986. #if WASM_ENABLE_GC != 0
  3987. if (table_segment->mode == 0) {
  3988. /* vec(funcidx), set elem type to (ref func) */
  3989. WASMRefType elem_ref_type = { 0 };
  3990. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3991. wasm_set_refheaptype_common(
  3992. &elem_ref_type.ref_ht_common, false,
  3993. HEAP_TYPE_FUNC);
  3994. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3995. module->ref_type_set, &elem_ref_type,
  3996. error_buf, error_buf_size)))
  3997. return false;
  3998. }
  3999. else {
  4000. /* vec(expr), set elem type to funcref */
  4001. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4002. }
  4003. #else
  4004. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4005. #endif
  4006. table_segment->table_index = 0;
  4007. if (!check_table_index(module, table_segment->table_index,
  4008. error_buf, error_buf_size))
  4009. return false;
  4010. if (!load_init_expr(
  4011. module, &p, p_end, &table_segment->base_offset,
  4012. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4013. return false;
  4014. if (table_segment->mode == 0) {
  4015. /* vec(funcidx) */
  4016. if (!load_func_index_vec(&p, p_end, module,
  4017. table_segment, error_buf,
  4018. error_buf_size))
  4019. return false;
  4020. }
  4021. else {
  4022. /* vec(expr) */
  4023. if (!load_init_expr_vec(&p, p_end, module,
  4024. table_segment, error_buf,
  4025. error_buf_size))
  4026. return false;
  4027. }
  4028. break;
  4029. }
  4030. /* elemkind + passive/declarative */
  4031. case 1:
  4032. case 3:
  4033. if (!load_elem_type(module, &p, p_end,
  4034. &table_segment->elem_type,
  4035. #if WASM_ENABLE_GC != 0
  4036. &table_segment->elem_ref_type,
  4037. #endif
  4038. true, error_buf, error_buf_size))
  4039. return false;
  4040. /* vec(funcidx) */
  4041. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4042. error_buf, error_buf_size))
  4043. return false;
  4044. break;
  4045. /* elemkind/elemtype + table_idx + active */
  4046. case 2:
  4047. case 6:
  4048. if (!load_table_index(&p, p_end, module,
  4049. &table_segment->table_index,
  4050. error_buf, error_buf_size))
  4051. return false;
  4052. if (!load_init_expr(
  4053. module, &p, p_end, &table_segment->base_offset,
  4054. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4055. return false;
  4056. if (!load_elem_type(module, &p, p_end,
  4057. &table_segment->elem_type,
  4058. #if WASM_ENABLE_GC != 0
  4059. &table_segment->elem_ref_type,
  4060. #endif
  4061. table_segment->mode == 2 ? true : false,
  4062. error_buf, error_buf_size))
  4063. return false;
  4064. if (table_segment->mode == 2) {
  4065. /* vec(funcidx) */
  4066. if (!load_func_index_vec(&p, p_end, module,
  4067. table_segment, error_buf,
  4068. error_buf_size))
  4069. return false;
  4070. }
  4071. else {
  4072. /* vec(expr) */
  4073. if (!load_init_expr_vec(&p, p_end, module,
  4074. table_segment, error_buf,
  4075. error_buf_size))
  4076. return false;
  4077. }
  4078. break;
  4079. case 5:
  4080. case 7:
  4081. if (!load_elem_type(module, &p, p_end,
  4082. &table_segment->elem_type,
  4083. #if WASM_ENABLE_GC != 0
  4084. &table_segment->elem_ref_type,
  4085. #endif
  4086. false, error_buf, error_buf_size))
  4087. return false;
  4088. /* vec(expr) */
  4089. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4090. error_buf, error_buf_size))
  4091. return false;
  4092. break;
  4093. default:
  4094. set_error_buf(error_buf, error_buf_size,
  4095. "unknown element segment kind");
  4096. return false;
  4097. }
  4098. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4099. /*
  4100. * like: 00 41 05 0b 04 00 01 00 01
  4101. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4102. */
  4103. if (!load_table_index(&p, p_end, module,
  4104. &table_segment->table_index, error_buf,
  4105. error_buf_size))
  4106. return false;
  4107. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4108. VALUE_TYPE_I32, NULL, error_buf,
  4109. error_buf_size))
  4110. return false;
  4111. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4112. error_buf, error_buf_size))
  4113. return false;
  4114. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4115. #if WASM_ENABLE_WAMR_COMPILER != 0
  4116. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4117. module->is_ref_types_used = true;
  4118. #endif
  4119. }
  4120. }
  4121. if (p != p_end) {
  4122. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4123. return false;
  4124. }
  4125. LOG_VERBOSE("Load table segment section success.\n");
  4126. return true;
  4127. fail:
  4128. return false;
  4129. }
  4130. static bool
  4131. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4132. WASMModule *module, char *error_buf,
  4133. uint32 error_buf_size)
  4134. {
  4135. const uint8 *p = buf, *p_end = buf_end;
  4136. uint32 data_seg_count, i, mem_index, data_seg_len;
  4137. uint64 total_size;
  4138. WASMDataSeg *dataseg;
  4139. InitializerExpression init_expr;
  4140. #if WASM_ENABLE_BULK_MEMORY != 0
  4141. bool is_passive = false;
  4142. uint32 mem_flag;
  4143. #endif
  4144. uint8 mem_offset_type = VALUE_TYPE_I32;
  4145. read_leb_uint32(p, p_end, data_seg_count);
  4146. #if WASM_ENABLE_BULK_MEMORY != 0
  4147. if ((module->data_seg_count1 != 0)
  4148. && (data_seg_count != module->data_seg_count1)) {
  4149. set_error_buf(error_buf, error_buf_size,
  4150. "data count and data section have inconsistent lengths");
  4151. return false;
  4152. }
  4153. #endif
  4154. if (data_seg_count) {
  4155. module->data_seg_count = data_seg_count;
  4156. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4157. if (!(module->data_segments =
  4158. loader_malloc(total_size, error_buf, error_buf_size))) {
  4159. return false;
  4160. }
  4161. for (i = 0; i < data_seg_count; i++) {
  4162. read_leb_uint32(p, p_end, mem_index);
  4163. #if WASM_ENABLE_BULK_MEMORY != 0
  4164. is_passive = false;
  4165. mem_flag = mem_index & 0x03;
  4166. switch (mem_flag) {
  4167. case 0x01:
  4168. is_passive = true;
  4169. #if WASM_ENABLE_WAMR_COMPILER != 0
  4170. module->is_bulk_memory_used = true;
  4171. #endif
  4172. break;
  4173. case 0x00:
  4174. /* no memory index, treat index as 0 */
  4175. mem_index = 0;
  4176. goto check_mem_index;
  4177. case 0x02:
  4178. /* read following memory index */
  4179. read_leb_uint32(p, p_end, mem_index);
  4180. #if WASM_ENABLE_WAMR_COMPILER != 0
  4181. module->is_bulk_memory_used = true;
  4182. #endif
  4183. check_mem_index:
  4184. if (mem_index
  4185. >= module->import_memory_count + module->memory_count) {
  4186. set_error_buf_v(error_buf, error_buf_size,
  4187. "unknown memory %d", mem_index);
  4188. return false;
  4189. }
  4190. break;
  4191. case 0x03:
  4192. default:
  4193. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4194. return false;
  4195. break;
  4196. }
  4197. #else
  4198. if (mem_index
  4199. >= module->import_memory_count + module->memory_count) {
  4200. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4201. mem_index);
  4202. return false;
  4203. }
  4204. #endif /* WASM_ENABLE_BULK_MEMORY */
  4205. #if WASM_ENABLE_BULK_MEMORY != 0
  4206. if (!is_passive)
  4207. #endif
  4208. {
  4209. #if WASM_ENABLE_MEMORY64 != 0
  4210. /* This memory_flag is from memory instead of data segment */
  4211. uint8 memory_flag;
  4212. if (module->import_memory_count > 0) {
  4213. memory_flag =
  4214. module->import_memories[mem_index].u.memory.flags;
  4215. }
  4216. else {
  4217. memory_flag =
  4218. module
  4219. ->memories[mem_index - module->import_memory_count]
  4220. .flags;
  4221. }
  4222. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4223. : VALUE_TYPE_I32;
  4224. #else
  4225. mem_offset_type = VALUE_TYPE_I32;
  4226. #endif
  4227. }
  4228. #if WASM_ENABLE_BULK_MEMORY != 0
  4229. if (!is_passive)
  4230. #endif
  4231. if (!load_init_expr(module, &p, p_end, &init_expr,
  4232. mem_offset_type, NULL, error_buf,
  4233. error_buf_size))
  4234. return false;
  4235. read_leb_uint32(p, p_end, data_seg_len);
  4236. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4237. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4238. return false;
  4239. }
  4240. #if WASM_ENABLE_BULK_MEMORY != 0
  4241. dataseg->is_passive = is_passive;
  4242. if (!is_passive)
  4243. #endif
  4244. {
  4245. bh_memcpy_s(&dataseg->base_offset,
  4246. sizeof(InitializerExpression), &init_expr,
  4247. sizeof(InitializerExpression));
  4248. dataseg->memory_index = mem_index;
  4249. }
  4250. dataseg->data_length = data_seg_len;
  4251. CHECK_BUF(p, p_end, data_seg_len);
  4252. dataseg->data = (uint8 *)p;
  4253. p += data_seg_len;
  4254. }
  4255. }
  4256. if (p != p_end) {
  4257. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4258. return false;
  4259. }
  4260. LOG_VERBOSE("Load data segment section success.\n");
  4261. return true;
  4262. fail:
  4263. return false;
  4264. }
  4265. #if WASM_ENABLE_BULK_MEMORY != 0
  4266. static bool
  4267. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4268. WASMModule *module, char *error_buf,
  4269. uint32 error_buf_size)
  4270. {
  4271. const uint8 *p = buf, *p_end = buf_end;
  4272. uint32 data_seg_count1 = 0;
  4273. read_leb_uint32(p, p_end, data_seg_count1);
  4274. module->data_seg_count1 = data_seg_count1;
  4275. if (p != p_end) {
  4276. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4277. return false;
  4278. }
  4279. #if WASM_ENABLE_WAMR_COMPILER != 0
  4280. module->is_bulk_memory_used = true;
  4281. #endif
  4282. LOG_VERBOSE("Load datacount section success.\n");
  4283. return true;
  4284. fail:
  4285. return false;
  4286. }
  4287. #endif
  4288. #if WASM_ENABLE_TAGS != 0
  4289. static bool
  4290. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4291. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4292. uint32 error_buf_size)
  4293. {
  4294. (void)buf_code;
  4295. (void)buf_code_end;
  4296. const uint8 *p = buf, *p_end = buf_end;
  4297. size_t total_size = 0;
  4298. /* number of tags defined in the section */
  4299. uint32 section_tag_count = 0;
  4300. uint8 tag_attribute;
  4301. uint32 tag_type;
  4302. WASMTag *tag = NULL;
  4303. /* get tag count */
  4304. read_leb_uint32(p, p_end, section_tag_count);
  4305. module->tag_count = section_tag_count;
  4306. if (section_tag_count) {
  4307. total_size = sizeof(WASMTag *) * module->tag_count;
  4308. if (!(module->tags =
  4309. loader_malloc(total_size, error_buf, error_buf_size))) {
  4310. return false;
  4311. }
  4312. /* load each tag, imported tags precede the tags */
  4313. uint32 tag_index;
  4314. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4315. /* get the one byte attribute */
  4316. CHECK_BUF(p, p_end, 1);
  4317. tag_attribute = read_uint8(p);
  4318. /* get type */
  4319. read_leb_uint32(p, p_end, tag_type);
  4320. /* compare against module->types */
  4321. if (tag_type >= module->type_count) {
  4322. set_error_buf(error_buf, error_buf_size, "unknown type");
  4323. return false;
  4324. }
  4325. /* get return type (must be 0) */
  4326. /* check, that the type of the referred tag returns void */
  4327. WASMType *func_type = (WASMType *)module->types[tag_type];
  4328. if (func_type->result_count != 0) {
  4329. set_error_buf(error_buf, error_buf_size,
  4330. "non-empty tag result type");
  4331. goto fail;
  4332. }
  4333. if (!(tag = module->tags[tag_index] = loader_malloc(
  4334. sizeof(WASMTag), error_buf, error_buf_size))) {
  4335. return false;
  4336. }
  4337. /* store to module tag declarations */
  4338. tag->attribute = tag_attribute;
  4339. tag->type = tag_type;
  4340. tag->tag_type = func_type;
  4341. }
  4342. }
  4343. if (p != p_end) {
  4344. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4345. return false;
  4346. }
  4347. LOG_VERBOSE("Load tag section success.\n");
  4348. return true;
  4349. fail:
  4350. return false;
  4351. }
  4352. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4353. static bool
  4354. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4355. const uint8 *buf_func_end, WASMModule *module,
  4356. char *error_buf, uint32 error_buf_size)
  4357. {
  4358. const uint8 *p = buf, *p_end = buf_end;
  4359. const uint8 *p_func = buf_func;
  4360. uint32 func_count = 0, code_count;
  4361. /* code has been loaded in function section, so pass it here, just check
  4362. * whether function and code section have inconsistent lengths */
  4363. read_leb_uint32(p, p_end, code_count);
  4364. if (buf_func)
  4365. read_leb_uint32(p_func, buf_func_end, func_count);
  4366. if (func_count != code_count) {
  4367. set_error_buf(error_buf, error_buf_size,
  4368. "function and code section have inconsistent lengths");
  4369. return false;
  4370. }
  4371. LOG_VERBOSE("Load code segment section success.\n");
  4372. (void)module;
  4373. return true;
  4374. fail:
  4375. return false;
  4376. }
  4377. static bool
  4378. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4379. char *error_buf, uint32 error_buf_size)
  4380. {
  4381. const uint8 *p = buf, *p_end = buf_end;
  4382. WASMFuncType *type;
  4383. uint32 start_function;
  4384. read_leb_uint32(p, p_end, start_function);
  4385. if (start_function
  4386. >= module->function_count + module->import_function_count) {
  4387. set_error_buf(error_buf, error_buf_size, "unknown function");
  4388. return false;
  4389. }
  4390. if (start_function < module->import_function_count)
  4391. type = module->import_functions[start_function].u.function.func_type;
  4392. else
  4393. type = module->functions[start_function - module->import_function_count]
  4394. ->func_type;
  4395. if (type->param_count != 0 || type->result_count != 0) {
  4396. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4397. return false;
  4398. }
  4399. module->start_function = start_function;
  4400. if (p != p_end) {
  4401. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4402. return false;
  4403. }
  4404. LOG_VERBOSE("Load start section success.\n");
  4405. return true;
  4406. fail:
  4407. return false;
  4408. }
  4409. #if WASM_ENABLE_STRINGREF != 0
  4410. static bool
  4411. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4412. WASMModule *module, bool is_load_from_file_buf,
  4413. char *error_buf, uint32 error_buf_size)
  4414. {
  4415. const uint8 *p = buf, *p_end = buf_end;
  4416. int32 deferred_count, immediate_count, string_length, i;
  4417. uint64 total_size;
  4418. read_leb_uint32(p, p_end, deferred_count);
  4419. read_leb_uint32(p, p_end, immediate_count);
  4420. /* proposal set deferred_count for future extension */
  4421. if (deferred_count != 0) {
  4422. goto fail;
  4423. }
  4424. if (immediate_count > 0) {
  4425. total_size = sizeof(char *) * (uint64)immediate_count;
  4426. if (!(module->string_literal_ptrs =
  4427. loader_malloc(total_size, error_buf, error_buf_size))) {
  4428. goto fail;
  4429. }
  4430. module->string_literal_count = immediate_count;
  4431. total_size = sizeof(uint32) * (uint64)immediate_count;
  4432. if (!(module->string_literal_lengths =
  4433. loader_malloc(total_size, error_buf, error_buf_size))) {
  4434. goto fail;
  4435. }
  4436. for (i = 0; i < immediate_count; i++) {
  4437. read_leb_uint32(p, p_end, string_length);
  4438. CHECK_BUF(p, p_end, string_length);
  4439. module->string_literal_ptrs[i] = p;
  4440. module->string_literal_lengths[i] = string_length;
  4441. p += string_length;
  4442. }
  4443. }
  4444. if (p != p_end) {
  4445. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4446. goto fail;
  4447. }
  4448. LOG_VERBOSE("Load stringref section success.\n");
  4449. return true;
  4450. fail:
  4451. return false;
  4452. }
  4453. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4454. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4455. static bool
  4456. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4457. bool is_load_from_file_buf, char *error_buf,
  4458. uint32 error_buf_size)
  4459. {
  4460. const uint8 *p = buf, *p_end = buf_end;
  4461. uint32 name_type, subsection_size;
  4462. uint32 previous_name_type = 0;
  4463. uint32 num_func_name;
  4464. uint32 func_index;
  4465. uint32 previous_func_index = ~0U;
  4466. uint32 func_name_len;
  4467. uint32 name_index;
  4468. int i = 0;
  4469. if (p >= p_end) {
  4470. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4471. return false;
  4472. }
  4473. while (p < p_end) {
  4474. read_leb_uint32(p, p_end, name_type);
  4475. if (i != 0) {
  4476. if (name_type == previous_name_type) {
  4477. set_error_buf(error_buf, error_buf_size,
  4478. "duplicate sub-section");
  4479. return false;
  4480. }
  4481. if (name_type < previous_name_type) {
  4482. set_error_buf(error_buf, error_buf_size,
  4483. "out-of-order sub-section");
  4484. return false;
  4485. }
  4486. }
  4487. previous_name_type = name_type;
  4488. read_leb_uint32(p, p_end, subsection_size);
  4489. CHECK_BUF(p, p_end, subsection_size);
  4490. switch (name_type) {
  4491. case SUB_SECTION_TYPE_FUNC:
  4492. if (subsection_size) {
  4493. read_leb_uint32(p, p_end, num_func_name);
  4494. for (name_index = 0; name_index < num_func_name;
  4495. name_index++) {
  4496. read_leb_uint32(p, p_end, func_index);
  4497. if (func_index == previous_func_index) {
  4498. set_error_buf(error_buf, error_buf_size,
  4499. "duplicate function name");
  4500. return false;
  4501. }
  4502. if (func_index < previous_func_index
  4503. && previous_func_index != ~0U) {
  4504. set_error_buf(error_buf, error_buf_size,
  4505. "out-of-order function index ");
  4506. return false;
  4507. }
  4508. previous_func_index = func_index;
  4509. read_leb_uint32(p, p_end, func_name_len);
  4510. CHECK_BUF(p, p_end, func_name_len);
  4511. /* Skip the import functions */
  4512. if (func_index >= module->import_function_count) {
  4513. func_index -= module->import_function_count;
  4514. if (func_index >= module->function_count) {
  4515. set_error_buf(error_buf, error_buf_size,
  4516. "out-of-range function index");
  4517. return false;
  4518. }
  4519. if (!(module->functions[func_index]->field_name =
  4520. wasm_const_str_list_insert(
  4521. p, func_name_len, module,
  4522. #if WASM_ENABLE_WAMR_COMPILER != 0
  4523. false,
  4524. #else
  4525. is_load_from_file_buf,
  4526. #endif
  4527. error_buf, error_buf_size))) {
  4528. return false;
  4529. }
  4530. }
  4531. p += func_name_len;
  4532. }
  4533. }
  4534. break;
  4535. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4536. */
  4537. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4538. default:
  4539. p = p + subsection_size;
  4540. break;
  4541. }
  4542. i++;
  4543. }
  4544. return true;
  4545. fail:
  4546. return false;
  4547. }
  4548. #endif
  4549. static bool
  4550. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4551. bool is_load_from_file_buf, char *error_buf,
  4552. uint32 error_buf_size)
  4553. {
  4554. const uint8 *p = buf, *p_end = buf_end;
  4555. char section_name[32];
  4556. uint32 name_len, buffer_len;
  4557. if (p >= p_end) {
  4558. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4559. return false;
  4560. }
  4561. read_leb_uint32(p, p_end, name_len);
  4562. if (p + name_len > p_end) {
  4563. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4564. return false;
  4565. }
  4566. if (!wasm_check_utf8_str(p, name_len)) {
  4567. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4568. return false;
  4569. }
  4570. buffer_len = sizeof(section_name);
  4571. memset(section_name, 0, buffer_len);
  4572. if (name_len < buffer_len) {
  4573. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4574. }
  4575. else {
  4576. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4577. memset(section_name + buffer_len - 4, '.', 3);
  4578. }
  4579. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4580. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4581. module->name_section_buf = buf;
  4582. module->name_section_buf_end = buf_end;
  4583. p += name_len;
  4584. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  4585. error_buf_size);
  4586. LOG_VERBOSE("Load custom name section success.");
  4587. return true;
  4588. }
  4589. #endif
  4590. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4591. {
  4592. WASMCustomSection *section =
  4593. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4594. if (!section) {
  4595. return false;
  4596. }
  4597. section->name_addr = (char *)p;
  4598. section->name_len = name_len;
  4599. section->content_addr = (uint8 *)(p + name_len);
  4600. section->content_len = (uint32)(p_end - p - name_len);
  4601. section->next = module->custom_section_list;
  4602. module->custom_section_list = section;
  4603. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4604. return true;
  4605. }
  4606. #endif
  4607. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4608. (void)is_load_from_file_buf;
  4609. (void)module;
  4610. return true;
  4611. fail:
  4612. return false;
  4613. }
  4614. static void
  4615. calculate_global_data_offset(WASMModule *module)
  4616. {
  4617. uint32 i, data_offset;
  4618. data_offset = 0;
  4619. for (i = 0; i < module->import_global_count; i++) {
  4620. WASMGlobalImport *import_global =
  4621. &((module->import_globals + i)->u.global);
  4622. #if WASM_ENABLE_FAST_JIT != 0
  4623. import_global->data_offset = data_offset;
  4624. #endif
  4625. data_offset += wasm_value_type_size(import_global->type);
  4626. }
  4627. for (i = 0; i < module->global_count; i++) {
  4628. WASMGlobal *global = module->globals + i;
  4629. #if WASM_ENABLE_FAST_JIT != 0
  4630. global->data_offset = data_offset;
  4631. #endif
  4632. data_offset += wasm_value_type_size(global->type);
  4633. }
  4634. module->global_data_size = data_offset;
  4635. }
  4636. #if WASM_ENABLE_FAST_JIT != 0
  4637. static bool
  4638. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4639. uint32 error_buf_size)
  4640. {
  4641. #if WASM_ENABLE_LAZY_JIT != 0
  4642. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4643. #endif
  4644. uint32 i;
  4645. if (!module->function_count)
  4646. return true;
  4647. if (!(module->fast_jit_func_ptrs =
  4648. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4649. error_buf_size))) {
  4650. return false;
  4651. }
  4652. #if WASM_ENABLE_LAZY_JIT != 0
  4653. for (i = 0; i < module->function_count; i++) {
  4654. module->fast_jit_func_ptrs[i] =
  4655. jit_globals->compile_fast_jit_and_then_call;
  4656. }
  4657. #endif
  4658. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4659. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4660. set_error_buf(error_buf, error_buf_size,
  4661. "init fast jit thread lock failed");
  4662. return false;
  4663. }
  4664. module->fast_jit_thread_locks_inited[i] = true;
  4665. }
  4666. return true;
  4667. }
  4668. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4669. #if WASM_ENABLE_JIT != 0
  4670. static bool
  4671. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4672. uint32 error_buf_size)
  4673. {
  4674. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4675. AOTCompOption option = { 0 };
  4676. char *aot_last_error;
  4677. uint64 size;
  4678. #if WASM_ENABLE_GC != 0
  4679. bool gc_enabled = true;
  4680. #else
  4681. bool gc_enabled = false;
  4682. #endif
  4683. if (module->function_count == 0)
  4684. return true;
  4685. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4686. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4687. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4688. return false;
  4689. }
  4690. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4691. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4692. os_mutex_destroy(&module->tierup_wait_lock);
  4693. return false;
  4694. }
  4695. module->tierup_wait_lock_inited = true;
  4696. #endif
  4697. size = sizeof(void *) * (uint64)module->function_count
  4698. + sizeof(bool) * (uint64)module->function_count;
  4699. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4700. return false;
  4701. }
  4702. module->func_ptrs_compiled =
  4703. (bool *)((uint8 *)module->func_ptrs
  4704. + sizeof(void *) * module->function_count);
  4705. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4706. if (!module->comp_data) {
  4707. aot_last_error = aot_get_last_error();
  4708. bh_assert(aot_last_error != NULL);
  4709. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4710. return false;
  4711. }
  4712. option.is_jit_mode = true;
  4713. option.opt_level = llvm_jit_options->opt_level;
  4714. option.size_level = llvm_jit_options->size_level;
  4715. option.segue_flags = llvm_jit_options->segue_flags;
  4716. option.quick_invoke_c_api_import =
  4717. llvm_jit_options->quick_invoke_c_api_import;
  4718. #if WASM_ENABLE_BULK_MEMORY != 0
  4719. option.enable_bulk_memory = true;
  4720. #endif
  4721. #if WASM_ENABLE_THREAD_MGR != 0
  4722. option.enable_thread_mgr = true;
  4723. #endif
  4724. #if WASM_ENABLE_TAIL_CALL != 0
  4725. option.enable_tail_call = true;
  4726. #endif
  4727. #if WASM_ENABLE_SIMD != 0
  4728. option.enable_simd = true;
  4729. #endif
  4730. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4731. option.enable_ref_types = true;
  4732. #elif WASM_ENABLE_GC != 0
  4733. option.enable_gc = true;
  4734. #endif
  4735. option.enable_aux_stack_check = true;
  4736. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4737. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4738. option.enable_aux_stack_frame = true;
  4739. #endif
  4740. #if WASM_ENABLE_PERF_PROFILING != 0
  4741. option.enable_perf_profiling = true;
  4742. #endif
  4743. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4744. option.enable_memory_profiling = true;
  4745. option.enable_stack_estimation = true;
  4746. #endif
  4747. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4748. if (!module->comp_ctx) {
  4749. aot_last_error = aot_get_last_error();
  4750. bh_assert(aot_last_error != NULL);
  4751. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4752. return false;
  4753. }
  4754. return true;
  4755. }
  4756. static bool
  4757. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4758. uint32 error_buf_size)
  4759. {
  4760. char *aot_last_error;
  4761. uint32 i;
  4762. if (module->function_count == 0)
  4763. return true;
  4764. if (!aot_compile_wasm(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. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4771. if (module->orcjit_stop_compiling)
  4772. return false;
  4773. #endif
  4774. bh_print_time("Begin to lookup llvm jit functions");
  4775. for (i = 0; i < module->function_count; i++) {
  4776. LLVMOrcJITTargetAddress func_addr = 0;
  4777. LLVMErrorRef error;
  4778. char func_name[48];
  4779. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4780. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4781. func_name);
  4782. if (error != LLVMErrorSuccess) {
  4783. char *err_msg = LLVMGetErrorMessage(error);
  4784. set_error_buf_v(error_buf, error_buf_size,
  4785. "failed to compile llvm jit function: %s", err_msg);
  4786. LLVMDisposeErrorMessage(err_msg);
  4787. return false;
  4788. }
  4789. /**
  4790. * No need to lock the func_ptr[func_idx] here as it is basic
  4791. * data type, the load/store for it can be finished by one cpu
  4792. * instruction, and there can be only one cpu instruction
  4793. * loading/storing at the same time.
  4794. */
  4795. module->func_ptrs[i] = (void *)func_addr;
  4796. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4797. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4798. if (module->orcjit_stop_compiling)
  4799. return false;
  4800. #endif
  4801. }
  4802. bh_print_time("End lookup llvm jit functions");
  4803. return true;
  4804. }
  4805. #endif /* end of WASM_ENABLE_JIT != 0 */
  4806. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4807. && WASM_ENABLE_LAZY_JIT != 0
  4808. static void *
  4809. init_llvm_jit_functions_stage2_callback(void *arg)
  4810. {
  4811. WASMModule *module = (WASMModule *)arg;
  4812. char error_buf[128];
  4813. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4814. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4815. module->orcjit_stop_compiling = true;
  4816. return NULL;
  4817. }
  4818. os_mutex_lock(&module->tierup_wait_lock);
  4819. module->llvm_jit_inited = true;
  4820. os_cond_broadcast(&module->tierup_wait_cond);
  4821. os_mutex_unlock(&module->tierup_wait_lock);
  4822. return NULL;
  4823. }
  4824. #endif
  4825. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4826. /* The callback function to compile jit functions */
  4827. static void *
  4828. orcjit_thread_callback(void *arg)
  4829. {
  4830. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4831. #if WASM_ENABLE_JIT != 0
  4832. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4833. #endif
  4834. WASMModule *module = thread_arg->module;
  4835. uint32 group_idx = thread_arg->group_idx;
  4836. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4837. uint32 func_count = module->function_count;
  4838. uint32 i;
  4839. #if WASM_ENABLE_FAST_JIT != 0
  4840. /* Compile fast jit funcitons of this group */
  4841. for (i = group_idx; i < func_count; i += group_stride) {
  4842. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4843. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4844. break;
  4845. }
  4846. if (module->orcjit_stop_compiling) {
  4847. return NULL;
  4848. }
  4849. }
  4850. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4851. os_mutex_lock(&module->tierup_wait_lock);
  4852. module->fast_jit_ready_groups++;
  4853. os_mutex_unlock(&module->tierup_wait_lock);
  4854. #endif
  4855. #endif
  4856. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4857. && WASM_ENABLE_LAZY_JIT != 0
  4858. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4859. for (i = group_idx; i < func_count;
  4860. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4861. uint32 j;
  4862. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4863. if (i + j * group_stride < func_count) {
  4864. if (!jit_compiler_set_call_to_fast_jit(
  4865. module,
  4866. i + j * group_stride + module->import_function_count)) {
  4867. LOG_ERROR(
  4868. "failed to compile call_to_fast_jit for func %u\n",
  4869. i + j * group_stride + module->import_function_count);
  4870. module->orcjit_stop_compiling = true;
  4871. return NULL;
  4872. }
  4873. }
  4874. if (module->orcjit_stop_compiling) {
  4875. return NULL;
  4876. }
  4877. }
  4878. }
  4879. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4880. fast jit functions are compiled */
  4881. os_mutex_lock(&module->tierup_wait_lock);
  4882. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4883. && module->fast_jit_ready_groups >= group_stride)) {
  4884. os_cond_reltimedwait(&module->tierup_wait_cond,
  4885. &module->tierup_wait_lock, 10000);
  4886. if (module->orcjit_stop_compiling) {
  4887. /* init_llvm_jit_functions_stage2 failed */
  4888. os_mutex_unlock(&module->tierup_wait_lock);
  4889. return NULL;
  4890. }
  4891. }
  4892. os_mutex_unlock(&module->tierup_wait_lock);
  4893. #endif
  4894. #if WASM_ENABLE_JIT != 0
  4895. /* Compile llvm jit functions of this group */
  4896. for (i = group_idx; i < func_count;
  4897. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4898. LLVMOrcJITTargetAddress func_addr = 0;
  4899. LLVMErrorRef error;
  4900. char func_name[48];
  4901. typedef void (*F)(void);
  4902. union {
  4903. F f;
  4904. void *v;
  4905. } u;
  4906. uint32 j;
  4907. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4908. "_wrapper");
  4909. LOG_DEBUG("compile llvm jit func %s", func_name);
  4910. error =
  4911. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4912. if (error != LLVMErrorSuccess) {
  4913. char *err_msg = LLVMGetErrorMessage(error);
  4914. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4915. LLVMDisposeErrorMessage(err_msg);
  4916. break;
  4917. }
  4918. /* Call the jit wrapper function to trigger its compilation, so as
  4919. to compile the actual jit functions, since we add the latter to
  4920. function list in the PartitionFunction callback */
  4921. u.v = (void *)func_addr;
  4922. u.f();
  4923. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4924. if (i + j * group_stride < func_count) {
  4925. module->func_ptrs_compiled[i + j * group_stride] = true;
  4926. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4927. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4928. i + j * group_stride);
  4929. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4930. func_name);
  4931. if (error != LLVMErrorSuccess) {
  4932. char *err_msg = LLVMGetErrorMessage(error);
  4933. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4934. err_msg);
  4935. LLVMDisposeErrorMessage(err_msg);
  4936. /* Ignore current llvm jit func, as its func ptr is
  4937. previous set to call_to_fast_jit, which also works */
  4938. continue;
  4939. }
  4940. jit_compiler_set_llvm_jit_func_ptr(
  4941. module,
  4942. i + j * group_stride + module->import_function_count,
  4943. (void *)func_addr);
  4944. /* Try to switch to call this llvm jit funtion instead of
  4945. fast jit function from fast jit jitted code */
  4946. jit_compiler_set_call_to_llvm_jit(
  4947. module,
  4948. i + j * group_stride + module->import_function_count);
  4949. #endif
  4950. }
  4951. }
  4952. if (module->orcjit_stop_compiling) {
  4953. break;
  4954. }
  4955. }
  4956. #endif
  4957. return NULL;
  4958. }
  4959. static void
  4960. orcjit_stop_compile_threads(WASMModule *module)
  4961. {
  4962. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4963. / sizeof(OrcJitThreadArg));
  4964. module->orcjit_stop_compiling = true;
  4965. for (i = 0; i < thread_num; i++) {
  4966. if (module->orcjit_threads[i])
  4967. os_thread_join(module->orcjit_threads[i], NULL);
  4968. }
  4969. }
  4970. static bool
  4971. compile_jit_functions(WASMModule *module, char *error_buf,
  4972. uint32 error_buf_size)
  4973. {
  4974. uint32 thread_num =
  4975. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4976. uint32 i, j;
  4977. bh_print_time("Begin to compile jit functions");
  4978. /* Create threads to compile the jit functions */
  4979. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4980. #if WASM_ENABLE_JIT != 0
  4981. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4982. #endif
  4983. module->orcjit_thread_args[i].module = module;
  4984. module->orcjit_thread_args[i].group_idx = i;
  4985. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  4986. (void *)&module->orcjit_thread_args[i],
  4987. APP_THREAD_STACK_SIZE_DEFAULT)
  4988. != 0) {
  4989. set_error_buf(error_buf, error_buf_size,
  4990. "create orcjit compile thread failed");
  4991. /* Terminate the threads created */
  4992. module->orcjit_stop_compiling = true;
  4993. for (j = 0; j < i; j++) {
  4994. os_thread_join(module->orcjit_threads[j], NULL);
  4995. }
  4996. return false;
  4997. }
  4998. }
  4999. #if WASM_ENABLE_LAZY_JIT == 0
  5000. /* Wait until all jit functions are compiled for eager mode */
  5001. for (i = 0; i < thread_num; i++) {
  5002. if (module->orcjit_threads[i])
  5003. os_thread_join(module->orcjit_threads[i], NULL);
  5004. }
  5005. #if WASM_ENABLE_FAST_JIT != 0
  5006. /* Ensure all the fast-jit functions are compiled */
  5007. for (i = 0; i < module->function_count; i++) {
  5008. if (!jit_compiler_is_compiled(module,
  5009. i + module->import_function_count)) {
  5010. set_error_buf(error_buf, error_buf_size,
  5011. "failed to compile fast jit function");
  5012. return false;
  5013. }
  5014. }
  5015. #endif
  5016. #if WASM_ENABLE_JIT != 0
  5017. /* Ensure all the llvm-jit functions are compiled */
  5018. for (i = 0; i < module->function_count; i++) {
  5019. if (!module->func_ptrs_compiled[i]) {
  5020. set_error_buf(error_buf, error_buf_size,
  5021. "failed to compile llvm jit function");
  5022. return false;
  5023. }
  5024. }
  5025. #endif
  5026. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5027. bh_print_time("End compile jit functions");
  5028. return true;
  5029. }
  5030. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5031. static bool
  5032. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5033. uint32 cur_func_idx, char *error_buf,
  5034. uint32 error_buf_size);
  5035. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5036. void **
  5037. wasm_interp_get_handle_table();
  5038. static void **handle_table;
  5039. #endif
  5040. static bool
  5041. load_from_sections(WASMModule *module, WASMSection *sections,
  5042. bool is_load_from_file_buf, char *error_buf,
  5043. uint32 error_buf_size)
  5044. {
  5045. WASMExport *export;
  5046. WASMSection *section = sections;
  5047. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5048. *buf_func = NULL, *buf_func_end = NULL;
  5049. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5050. WASMGlobal *aux_stack_top_global = NULL, *global;
  5051. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5052. aux_stack_top = (uint64)-1LL;
  5053. uint32 global_index, func_index, i;
  5054. uint32 aux_data_end_global_index = (uint32)-1;
  5055. uint32 aux_heap_base_global_index = (uint32)-1;
  5056. WASMFuncType *func_type;
  5057. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5058. /* Find code and function sections if have */
  5059. while (section) {
  5060. if (section->section_type == SECTION_TYPE_CODE) {
  5061. buf_code = section->section_body;
  5062. buf_code_end = buf_code + section->section_body_size;
  5063. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5064. module->buf_code = (uint8 *)buf_code;
  5065. module->buf_code_size = section->section_body_size;
  5066. #endif
  5067. }
  5068. else if (section->section_type == SECTION_TYPE_FUNC) {
  5069. buf_func = section->section_body;
  5070. buf_func_end = buf_func + section->section_body_size;
  5071. }
  5072. section = section->next;
  5073. }
  5074. section = sections;
  5075. while (section) {
  5076. buf = section->section_body;
  5077. buf_end = buf + section->section_body_size;
  5078. switch (section->section_type) {
  5079. case SECTION_TYPE_USER:
  5080. /* unsupported user section, ignore it. */
  5081. if (!load_user_section(buf, buf_end, module,
  5082. is_load_from_file_buf, error_buf,
  5083. error_buf_size))
  5084. return false;
  5085. break;
  5086. case SECTION_TYPE_TYPE:
  5087. if (!load_type_section(buf, buf_end, module, error_buf,
  5088. error_buf_size))
  5089. return false;
  5090. break;
  5091. case SECTION_TYPE_IMPORT:
  5092. if (!load_import_section(buf, buf_end, module,
  5093. is_load_from_file_buf, error_buf,
  5094. error_buf_size))
  5095. return false;
  5096. break;
  5097. case SECTION_TYPE_FUNC:
  5098. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5099. module, error_buf, error_buf_size))
  5100. return false;
  5101. break;
  5102. case SECTION_TYPE_TABLE:
  5103. if (!load_table_section(buf, buf_end, module, error_buf,
  5104. error_buf_size))
  5105. return false;
  5106. break;
  5107. case SECTION_TYPE_MEMORY:
  5108. if (!load_memory_section(buf, buf_end, module, error_buf,
  5109. error_buf_size))
  5110. return false;
  5111. break;
  5112. #if WASM_ENABLE_TAGS != 0
  5113. case SECTION_TYPE_TAG:
  5114. /* load tag declaration section */
  5115. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5116. module, error_buf, error_buf_size))
  5117. return false;
  5118. break;
  5119. #endif
  5120. case SECTION_TYPE_GLOBAL:
  5121. if (!load_global_section(buf, buf_end, module, error_buf,
  5122. error_buf_size))
  5123. return false;
  5124. break;
  5125. case SECTION_TYPE_EXPORT:
  5126. if (!load_export_section(buf, buf_end, module,
  5127. is_load_from_file_buf, error_buf,
  5128. error_buf_size))
  5129. return false;
  5130. break;
  5131. case SECTION_TYPE_START:
  5132. if (!load_start_section(buf, buf_end, module, error_buf,
  5133. error_buf_size))
  5134. return false;
  5135. break;
  5136. case SECTION_TYPE_ELEM:
  5137. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5138. error_buf_size))
  5139. return false;
  5140. break;
  5141. case SECTION_TYPE_CODE:
  5142. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5143. module, error_buf, error_buf_size))
  5144. return false;
  5145. break;
  5146. case SECTION_TYPE_DATA:
  5147. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  5148. error_buf_size))
  5149. return false;
  5150. break;
  5151. #if WASM_ENABLE_BULK_MEMORY != 0
  5152. case SECTION_TYPE_DATACOUNT:
  5153. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5154. error_buf_size))
  5155. return false;
  5156. break;
  5157. #endif
  5158. #if WASM_ENABLE_STRINGREF != 0
  5159. case SECTION_TYPE_STRINGREF:
  5160. if (!load_stringref_section(buf, buf_end, module,
  5161. is_load_from_file_buf, error_buf,
  5162. error_buf_size))
  5163. return false;
  5164. break;
  5165. #endif
  5166. default:
  5167. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5168. return false;
  5169. }
  5170. section = section->next;
  5171. }
  5172. module->aux_data_end_global_index = (uint32)-1;
  5173. module->aux_heap_base_global_index = (uint32)-1;
  5174. module->aux_stack_top_global_index = (uint32)-1;
  5175. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5176. export = module->exports;
  5177. for (i = 0; i < module->export_count; i++, export ++) {
  5178. if (export->kind == EXPORT_KIND_GLOBAL) {
  5179. if (!strcmp(export->name, "__heap_base")) {
  5180. global_index = export->index - module->import_global_count;
  5181. global = module->globals + global_index;
  5182. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5183. && global->init_expr.init_expr_type
  5184. == INIT_EXPR_TYPE_I32_CONST) {
  5185. aux_heap_base_global = global;
  5186. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5187. aux_heap_base_global_index = export->index;
  5188. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5189. aux_heap_base);
  5190. }
  5191. }
  5192. else if (!strcmp(export->name, "__data_end")) {
  5193. global_index = export->index - module->import_global_count;
  5194. global = module->globals + global_index;
  5195. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5196. && global->init_expr.init_expr_type
  5197. == INIT_EXPR_TYPE_I32_CONST) {
  5198. aux_data_end_global = global;
  5199. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5200. aux_data_end_global_index = export->index;
  5201. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5202. aux_data_end);
  5203. aux_data_end = align_uint64(aux_data_end, 16);
  5204. }
  5205. }
  5206. /* For module compiled with -pthread option, the global is:
  5207. [0] stack_top <-- 0
  5208. [1] tls_pointer
  5209. [2] tls_size
  5210. [3] data_end <-- 3
  5211. [4] global_base
  5212. [5] heap_base <-- 5
  5213. [6] dso_handle
  5214. For module compiled without -pthread option:
  5215. [0] stack_top <-- 0
  5216. [1] data_end <-- 1
  5217. [2] global_base
  5218. [3] heap_base <-- 3
  5219. [4] dso_handle
  5220. */
  5221. if (aux_data_end_global && aux_heap_base_global
  5222. && aux_data_end <= aux_heap_base) {
  5223. module->aux_data_end_global_index = aux_data_end_global_index;
  5224. module->aux_data_end = aux_data_end;
  5225. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5226. module->aux_heap_base = aux_heap_base;
  5227. /* Resolve aux stack top global */
  5228. for (global_index = 0; global_index < module->global_count;
  5229. global_index++) {
  5230. global = module->globals + global_index;
  5231. if (global->is_mutable /* heap_base and data_end is
  5232. not mutable */
  5233. && global->type == VALUE_TYPE_I32
  5234. && global->init_expr.init_expr_type
  5235. == INIT_EXPR_TYPE_I32_CONST
  5236. && (uint64)(uint32)global->init_expr.u.i32
  5237. <= aux_heap_base) {
  5238. aux_stack_top_global = global;
  5239. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5240. module->aux_stack_top_global_index =
  5241. module->import_global_count + global_index;
  5242. module->aux_stack_bottom = aux_stack_top;
  5243. module->aux_stack_size =
  5244. aux_stack_top > aux_data_end
  5245. ? (uint32)(aux_stack_top - aux_data_end)
  5246. : (uint32)aux_stack_top;
  5247. LOG_VERBOSE(
  5248. "Found aux stack top global, value: %" PRIu64 ", "
  5249. "global index: %d, stack size: %d",
  5250. aux_stack_top, global_index,
  5251. module->aux_stack_size);
  5252. break;
  5253. }
  5254. }
  5255. if (!aux_stack_top_global) {
  5256. /* Auxiliary stack global isn't found, it must be unused
  5257. in the wasm app, as if it is used, the global must be
  5258. defined. Here we set it to __heap_base global and set
  5259. its size to 0. */
  5260. aux_stack_top_global = aux_heap_base_global;
  5261. aux_stack_top = aux_heap_base;
  5262. module->aux_stack_top_global_index =
  5263. module->aux_heap_base_global_index;
  5264. module->aux_stack_bottom = aux_stack_top;
  5265. module->aux_stack_size = 0;
  5266. }
  5267. break;
  5268. }
  5269. }
  5270. }
  5271. module->malloc_function = (uint32)-1;
  5272. module->free_function = (uint32)-1;
  5273. module->retain_function = (uint32)-1;
  5274. /* Resolve malloc/free function exported by wasm module */
  5275. #if WASM_ENABLE_MEMORY64 != 0
  5276. if (has_module_memory64(module))
  5277. malloc_free_io_type = VALUE_TYPE_I64;
  5278. #endif
  5279. export = module->exports;
  5280. for (i = 0; i < module->export_count; i++, export ++) {
  5281. if (export->kind == EXPORT_KIND_FUNC) {
  5282. if (!strcmp(export->name, "malloc")
  5283. && export->index >= module->import_function_count) {
  5284. func_index = export->index - module->import_function_count;
  5285. func_type = module->functions[func_index]->func_type;
  5286. if (func_type->param_count == 1 && func_type->result_count == 1
  5287. && func_type->types[0] == malloc_free_io_type
  5288. && func_type->types[1] == malloc_free_io_type) {
  5289. bh_assert(module->malloc_function == (uint32)-1);
  5290. module->malloc_function = export->index;
  5291. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5292. export->name, export->index);
  5293. }
  5294. }
  5295. else if (!strcmp(export->name, "__new")
  5296. && export->index >= module->import_function_count) {
  5297. /* __new && __pin for AssemblyScript */
  5298. func_index = export->index - module->import_function_count;
  5299. func_type = module->functions[func_index]->func_type;
  5300. if (func_type->param_count == 2 && func_type->result_count == 1
  5301. && func_type->types[0] == malloc_free_io_type
  5302. && func_type->types[1] == VALUE_TYPE_I32
  5303. && func_type->types[2] == malloc_free_io_type) {
  5304. uint32 j;
  5305. WASMExport *export_tmp;
  5306. bh_assert(module->malloc_function == (uint32)-1);
  5307. module->malloc_function = export->index;
  5308. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5309. export->name, export->index);
  5310. /* resolve retain function.
  5311. If not found, reset malloc function index */
  5312. export_tmp = module->exports;
  5313. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5314. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5315. && (!strcmp(export_tmp->name, "__retain")
  5316. || (!strcmp(export_tmp->name, "__pin")))
  5317. && (export_tmp->index
  5318. >= module->import_function_count)) {
  5319. func_index = export_tmp->index
  5320. - module->import_function_count;
  5321. func_type =
  5322. module->functions[func_index]->func_type;
  5323. if (func_type->param_count == 1
  5324. && func_type->result_count == 1
  5325. && func_type->types[0] == malloc_free_io_type
  5326. && func_type->types[1] == malloc_free_io_type) {
  5327. bh_assert(module->retain_function
  5328. == (uint32)-1);
  5329. module->retain_function = export_tmp->index;
  5330. LOG_VERBOSE("Found retain function, name: %s, "
  5331. "index: %u",
  5332. export_tmp->name,
  5333. export_tmp->index);
  5334. break;
  5335. }
  5336. }
  5337. }
  5338. if (j == module->export_count) {
  5339. module->malloc_function = (uint32)-1;
  5340. LOG_VERBOSE("Can't find retain function,"
  5341. "reset malloc function index to -1");
  5342. }
  5343. }
  5344. }
  5345. else if (((!strcmp(export->name, "free"))
  5346. || (!strcmp(export->name, "__release"))
  5347. || (!strcmp(export->name, "__unpin")))
  5348. && export->index >= module->import_function_count) {
  5349. func_index = export->index - module->import_function_count;
  5350. func_type = module->functions[func_index]->func_type;
  5351. if (func_type->param_count == 1 && func_type->result_count == 0
  5352. && func_type->types[0] == malloc_free_io_type) {
  5353. bh_assert(module->free_function == (uint32)-1);
  5354. module->free_function = export->index;
  5355. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5356. export->name, export->index);
  5357. }
  5358. }
  5359. }
  5360. }
  5361. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5362. handle_table = wasm_interp_get_handle_table();
  5363. #endif
  5364. for (i = 0; i < module->function_count; i++) {
  5365. WASMFunction *func = module->functions[i];
  5366. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5367. error_buf_size)) {
  5368. return false;
  5369. }
  5370. if (i == module->function_count - 1
  5371. && func->code + func->code_size != buf_code_end) {
  5372. set_error_buf(error_buf, error_buf_size,
  5373. "code section size mismatch");
  5374. return false;
  5375. }
  5376. }
  5377. if (!module->possible_memory_grow) {
  5378. WASMMemoryImport *memory_import;
  5379. WASMMemory *memory;
  5380. if (aux_data_end_global && aux_heap_base_global
  5381. && aux_stack_top_global) {
  5382. uint64 init_memory_size;
  5383. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5384. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5385. * valid range of uint32 */
  5386. if (shrunk_memory_size <= UINT32_MAX) {
  5387. if (module->import_memory_count) {
  5388. memory_import = &module->import_memories[0].u.memory;
  5389. init_memory_size = (uint64)memory_import->num_bytes_per_page
  5390. * memory_import->init_page_count;
  5391. if (shrunk_memory_size <= init_memory_size) {
  5392. /* Reset memory info to decrease memory usage */
  5393. memory_import->num_bytes_per_page =
  5394. (uint32)shrunk_memory_size;
  5395. memory_import->init_page_count = 1;
  5396. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5397. shrunk_memory_size);
  5398. }
  5399. }
  5400. if (module->memory_count) {
  5401. memory = &module->memories[0];
  5402. init_memory_size = (uint64)memory->num_bytes_per_page
  5403. * memory->init_page_count;
  5404. if (shrunk_memory_size <= init_memory_size) {
  5405. /* Reset memory info to decrease memory usage */
  5406. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5407. memory->init_page_count = 1;
  5408. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5409. shrunk_memory_size);
  5410. }
  5411. }
  5412. }
  5413. }
  5414. #if WASM_ENABLE_MULTI_MODULE == 0
  5415. if (module->import_memory_count) {
  5416. memory_import = &module->import_memories[0].u.memory;
  5417. /* Only resize the memory to one big page if num_bytes_per_page is
  5418. * in valid range of uint32 */
  5419. if (memory_import->init_page_count < DEFAULT_MAX_PAGES) {
  5420. memory_import->num_bytes_per_page *=
  5421. memory_import->init_page_count;
  5422. if (memory_import->init_page_count > 0)
  5423. memory_import->init_page_count =
  5424. memory_import->max_page_count = 1;
  5425. else
  5426. memory_import->init_page_count =
  5427. memory_import->max_page_count = 0;
  5428. }
  5429. }
  5430. if (module->memory_count) {
  5431. memory = &module->memories[0];
  5432. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5433. * valid range of uint32 */
  5434. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5435. memory->num_bytes_per_page *= memory->init_page_count;
  5436. if (memory->init_page_count > 0)
  5437. memory->init_page_count = memory->max_page_count = 1;
  5438. else
  5439. memory->init_page_count = memory->max_page_count = 0;
  5440. }
  5441. }
  5442. #endif
  5443. }
  5444. calculate_global_data_offset(module);
  5445. #if WASM_ENABLE_FAST_JIT != 0
  5446. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5447. return false;
  5448. }
  5449. #endif
  5450. #if WASM_ENABLE_JIT != 0
  5451. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5452. return false;
  5453. }
  5454. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5455. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5456. return false;
  5457. }
  5458. #else
  5459. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5460. thread fast jit execution, since applying llvm optimizations in
  5461. aot_compile_wasm may cost a lot of time.
  5462. Create thread with enough native stack to apply llvm optimizations */
  5463. if (os_thread_create(&module->llvm_jit_init_thread,
  5464. init_llvm_jit_functions_stage2_callback,
  5465. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5466. != 0) {
  5467. set_error_buf(error_buf, error_buf_size,
  5468. "create orcjit compile thread failed");
  5469. return false;
  5470. }
  5471. #endif
  5472. #endif
  5473. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5474. /* Create threads to compile the jit functions */
  5475. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5476. return false;
  5477. }
  5478. #endif
  5479. #if WASM_ENABLE_MEMORY_TRACING != 0
  5480. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5481. #endif
  5482. return true;
  5483. }
  5484. static WASMModule *
  5485. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5486. {
  5487. WASMModule *module =
  5488. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5489. bh_list_status ret;
  5490. if (!module) {
  5491. return NULL;
  5492. }
  5493. module->module_type = Wasm_Module_Bytecode;
  5494. /* Set start_function to -1, means no start function */
  5495. module->start_function = (uint32)-1;
  5496. module->name = name;
  5497. #if WASM_ENABLE_FAST_INTERP == 0
  5498. module->br_table_cache_list = &module->br_table_cache_list_head;
  5499. ret = bh_list_init(module->br_table_cache_list);
  5500. bh_assert(ret == BH_LIST_SUCCESS);
  5501. #endif
  5502. #if WASM_ENABLE_MULTI_MODULE != 0
  5503. module->import_module_list = &module->import_module_list_head;
  5504. ret = bh_list_init(module->import_module_list);
  5505. bh_assert(ret == BH_LIST_SUCCESS);
  5506. #endif
  5507. #if WASM_ENABLE_DEBUG_INTERP != 0
  5508. ret = bh_list_init(&module->fast_opcode_list);
  5509. bh_assert(ret == BH_LIST_SUCCESS);
  5510. #endif
  5511. #if WASM_ENABLE_GC != 0
  5512. if (!(module->ref_type_set =
  5513. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5514. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5515. goto fail1;
  5516. }
  5517. if (os_mutex_init(&module->rtt_type_lock)) {
  5518. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5519. goto fail2;
  5520. }
  5521. #endif
  5522. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5523. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5524. && WASM_ENABLE_LAZY_JIT != 0)
  5525. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5526. set_error_buf(error_buf, error_buf_size,
  5527. "init instance list lock failed");
  5528. goto fail3;
  5529. }
  5530. #endif
  5531. (void)ret;
  5532. return module;
  5533. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5534. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5535. && WASM_ENABLE_LAZY_JIT != 0)
  5536. fail3:
  5537. #endif
  5538. #if WASM_ENABLE_GC != 0
  5539. os_mutex_destroy(&module->rtt_type_lock);
  5540. fail2:
  5541. bh_hash_map_destroy(module->ref_type_set);
  5542. fail1:
  5543. #endif
  5544. wasm_runtime_free(module);
  5545. return NULL;
  5546. }
  5547. #if WASM_ENABLE_DEBUG_INTERP != 0
  5548. static bool
  5549. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5550. uint32 error_buf_size)
  5551. {
  5552. WASMFastOPCodeNode *fast_op =
  5553. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5554. if (fast_op) {
  5555. fast_op->offset = pos - module->load_addr;
  5556. fast_op->orig_op = orig_op;
  5557. bh_list_insert(&module->fast_opcode_list, fast_op);
  5558. }
  5559. return fast_op ? true : false;
  5560. }
  5561. #endif
  5562. WASMModule *
  5563. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5564. uint32 error_buf_size)
  5565. {
  5566. WASMModule *module = create_module("", error_buf, error_buf_size);
  5567. if (!module)
  5568. return NULL;
  5569. if (!load_from_sections(module, section_list, false, error_buf,
  5570. error_buf_size)) {
  5571. wasm_loader_unload(module);
  5572. return NULL;
  5573. }
  5574. LOG_VERBOSE("Load module from sections success.\n");
  5575. return module;
  5576. }
  5577. static void
  5578. destroy_sections(WASMSection *section_list)
  5579. {
  5580. WASMSection *section = section_list, *next;
  5581. while (section) {
  5582. next = section->next;
  5583. wasm_runtime_free(section);
  5584. section = next;
  5585. }
  5586. }
  5587. /* clang-format off */
  5588. static uint8 section_ids[] = {
  5589. SECTION_TYPE_USER,
  5590. SECTION_TYPE_TYPE,
  5591. SECTION_TYPE_IMPORT,
  5592. SECTION_TYPE_FUNC,
  5593. SECTION_TYPE_TABLE,
  5594. SECTION_TYPE_MEMORY,
  5595. #if WASM_ENABLE_TAGS != 0
  5596. SECTION_TYPE_TAG,
  5597. #endif
  5598. #if WASM_ENABLE_STRINGREF != 0
  5599. /* must immediately precede the global section,
  5600. or where the global section would be */
  5601. SECTION_TYPE_STRINGREF,
  5602. #endif
  5603. SECTION_TYPE_GLOBAL,
  5604. SECTION_TYPE_EXPORT,
  5605. SECTION_TYPE_START,
  5606. SECTION_TYPE_ELEM,
  5607. #if WASM_ENABLE_BULK_MEMORY != 0
  5608. SECTION_TYPE_DATACOUNT,
  5609. #endif
  5610. SECTION_TYPE_CODE,
  5611. SECTION_TYPE_DATA
  5612. };
  5613. /* clang-format on */
  5614. static uint8
  5615. get_section_index(uint8 section_type)
  5616. {
  5617. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5618. for (uint8 i = 0; i < max_id; i++) {
  5619. if (section_type == section_ids[i])
  5620. return i;
  5621. }
  5622. return (uint8)-1;
  5623. }
  5624. static bool
  5625. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5626. char *error_buf, uint32 error_buf_size)
  5627. {
  5628. WASMSection *section_list_end = NULL, *section;
  5629. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  5630. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5631. uint32 section_size;
  5632. bh_assert(!*p_section_list);
  5633. p += 8;
  5634. while (p < p_end) {
  5635. CHECK_BUF(p, p_end, 1);
  5636. section_type = read_uint8(p);
  5637. section_index = get_section_index(section_type);
  5638. if (section_index != (uint8)-1) {
  5639. if (section_type != SECTION_TYPE_USER) {
  5640. /* Custom sections may be inserted at any place,
  5641. while other sections must occur at most once
  5642. and in prescribed order. */
  5643. if (last_section_index != (uint8)-1
  5644. && (section_index <= last_section_index)) {
  5645. set_error_buf(error_buf, error_buf_size,
  5646. "unexpected content after last section or "
  5647. "junk after last section");
  5648. return false;
  5649. }
  5650. last_section_index = section_index;
  5651. }
  5652. read_leb_uint32(p, p_end, section_size);
  5653. CHECK_BUF1(p, p_end, section_size);
  5654. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5655. error_buf_size))) {
  5656. return false;
  5657. }
  5658. section->section_type = section_type;
  5659. section->section_body = (uint8 *)p;
  5660. section->section_body_size = section_size;
  5661. if (!section_list_end)
  5662. *p_section_list = section_list_end = section;
  5663. else {
  5664. section_list_end->next = section;
  5665. section_list_end = section;
  5666. }
  5667. p += section_size;
  5668. }
  5669. else {
  5670. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5671. return false;
  5672. }
  5673. }
  5674. return true;
  5675. fail:
  5676. return false;
  5677. }
  5678. static void
  5679. exchange32(uint8 *p_data)
  5680. {
  5681. uint8 value = *p_data;
  5682. *p_data = *(p_data + 3);
  5683. *(p_data + 3) = value;
  5684. value = *(p_data + 1);
  5685. *(p_data + 1) = *(p_data + 2);
  5686. *(p_data + 2) = value;
  5687. }
  5688. static union {
  5689. int a;
  5690. char b;
  5691. } __ue = { .a = 1 };
  5692. #define is_little_endian() (__ue.b == 1)
  5693. static bool
  5694. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  5695. uint32 error_buf_size)
  5696. {
  5697. const uint8 *buf_end = buf + size;
  5698. const uint8 *p = buf, *p_end = buf_end;
  5699. uint32 magic_number, version;
  5700. WASMSection *section_list = NULL;
  5701. CHECK_BUF1(p, p_end, sizeof(uint32));
  5702. magic_number = read_uint32(p);
  5703. if (!is_little_endian())
  5704. exchange32((uint8 *)&magic_number);
  5705. if (magic_number != WASM_MAGIC_NUMBER) {
  5706. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5707. return false;
  5708. }
  5709. CHECK_BUF1(p, p_end, sizeof(uint32));
  5710. version = read_uint32(p);
  5711. if (!is_little_endian())
  5712. exchange32((uint8 *)&version);
  5713. if (version != WASM_CURRENT_VERSION) {
  5714. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5715. return false;
  5716. }
  5717. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5718. || !load_from_sections(module, section_list, true, error_buf,
  5719. error_buf_size)) {
  5720. destroy_sections(section_list);
  5721. return false;
  5722. }
  5723. destroy_sections(section_list);
  5724. return true;
  5725. fail:
  5726. return false;
  5727. }
  5728. #if WASM_ENABLE_LIBC_WASI != 0
  5729. /**
  5730. * refer to
  5731. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5732. */
  5733. static bool
  5734. check_wasi_abi_compatibility(const WASMModule *module,
  5735. #if WASM_ENABLE_MULTI_MODULE != 0
  5736. bool main_module,
  5737. #endif
  5738. char *error_buf, uint32 error_buf_size)
  5739. {
  5740. /**
  5741. * be careful with:
  5742. * wasi compatiable modules(command/reactor) which don't import any wasi
  5743. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5744. * reactor can depend on nothing. At the same time, each has its own entry
  5745. * point.
  5746. *
  5747. * observations:
  5748. * - clang always injects `_start` into a command
  5749. * - clang always injects `_initialize` into a reactor
  5750. * - `iwasm -f` allows to run a function in the reactor
  5751. *
  5752. * strong assumptions:
  5753. * - no one will define either `_start` or `_initialize` on purpose
  5754. * - `_start` should always be `void _start(void)`
  5755. * - `_initialize` should always be `void _initialize(void)`
  5756. *
  5757. */
  5758. /* clang-format off */
  5759. /**
  5760. *
  5761. * | | import_wasi_api True | | import_wasi_api False | |
  5762. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5763. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5764. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5765. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5766. */
  5767. /* clang-format on */
  5768. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5769. uint32 import_function_count = module->import_function_count;
  5770. WASMFuncType *func_type;
  5771. /* (func (export "_start") (...) */
  5772. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5773. error_buf, error_buf_size);
  5774. if (start) {
  5775. if (start->index < import_function_count) {
  5776. set_error_buf(
  5777. error_buf, error_buf_size,
  5778. "the builtin _start function can not be an import function");
  5779. return false;
  5780. }
  5781. func_type =
  5782. module->functions[start->index - import_function_count]->func_type;
  5783. if (func_type->param_count || func_type->result_count) {
  5784. set_error_buf(error_buf, error_buf_size,
  5785. "the signature of builtin _start function is wrong");
  5786. return false;
  5787. }
  5788. }
  5789. else {
  5790. /* (func (export "_initialize") (...) */
  5791. initialize =
  5792. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5793. error_buf, error_buf_size);
  5794. if (initialize) {
  5795. if (initialize->index < import_function_count) {
  5796. set_error_buf(error_buf, error_buf_size,
  5797. "the builtin _initialize function can not be an "
  5798. "import function");
  5799. return false;
  5800. }
  5801. func_type =
  5802. module->functions[initialize->index - import_function_count]
  5803. ->func_type;
  5804. if (func_type->param_count || func_type->result_count) {
  5805. set_error_buf(
  5806. error_buf, error_buf_size,
  5807. "the signature of builtin _initialize function is wrong");
  5808. return false;
  5809. }
  5810. }
  5811. }
  5812. /* filter out non-wasi compatiable modules */
  5813. if (!module->import_wasi_api && !start && !initialize) {
  5814. return true;
  5815. }
  5816. /* should have one at least */
  5817. if (module->import_wasi_api && !start && !initialize) {
  5818. LOG_WARNING("warning: a module with WASI apis should be either "
  5819. "a command or a reactor");
  5820. }
  5821. /*
  5822. * there is at least one of `_start` and `_initialize` in below cases.
  5823. * according to the assumption, they should be all wasi compatiable
  5824. */
  5825. #if WASM_ENABLE_MULTI_MODULE != 0
  5826. /* filter out commands (with `_start`) cases */
  5827. if (start && !main_module) {
  5828. set_error_buf(
  5829. error_buf, error_buf_size,
  5830. "a command (with _start function) can not be a sub-module");
  5831. return false;
  5832. }
  5833. #endif
  5834. /*
  5835. * it is ok a reactor acts as a main module,
  5836. * so skip the check about (with `_initialize`)
  5837. */
  5838. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5839. error_buf, error_buf_size);
  5840. if (!memory
  5841. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5842. /*
  5843. * with wasi-threads, it's still an open question if a memory
  5844. * should be exported.
  5845. *
  5846. * https://github.com/WebAssembly/wasi-threads/issues/22
  5847. * https://github.com/WebAssembly/WASI/issues/502
  5848. *
  5849. * Note: this code assumes the number of memories is at most 1.
  5850. */
  5851. && module->import_memory_count == 0
  5852. #endif
  5853. ) {
  5854. set_error_buf(error_buf, error_buf_size,
  5855. "a module with WASI apis must export memory by default");
  5856. return false;
  5857. }
  5858. return true;
  5859. }
  5860. #endif
  5861. WASMModule *
  5862. wasm_loader_load(uint8 *buf, uint32 size,
  5863. #if WASM_ENABLE_MULTI_MODULE != 0
  5864. bool main_module,
  5865. #endif
  5866. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5867. {
  5868. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5869. if (!module) {
  5870. return NULL;
  5871. }
  5872. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5873. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5874. module->load_addr = (uint8 *)buf;
  5875. module->load_size = size;
  5876. #endif
  5877. if (!load(buf, size, module, error_buf, error_buf_size)) {
  5878. goto fail;
  5879. }
  5880. #if WASM_ENABLE_LIBC_WASI != 0
  5881. /* Check the WASI application ABI */
  5882. if (!check_wasi_abi_compatibility(module,
  5883. #if WASM_ENABLE_MULTI_MODULE != 0
  5884. main_module,
  5885. #endif
  5886. error_buf, error_buf_size)) {
  5887. goto fail;
  5888. }
  5889. #endif
  5890. LOG_VERBOSE("Load module success.\n");
  5891. return module;
  5892. fail:
  5893. wasm_loader_unload(module);
  5894. return NULL;
  5895. }
  5896. void
  5897. wasm_loader_unload(WASMModule *module)
  5898. {
  5899. uint32 i;
  5900. if (!module)
  5901. return;
  5902. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5903. && WASM_ENABLE_LAZY_JIT != 0
  5904. module->orcjit_stop_compiling = true;
  5905. if (module->llvm_jit_init_thread)
  5906. os_thread_join(module->llvm_jit_init_thread, NULL);
  5907. #endif
  5908. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5909. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5910. module internal data after they were freed */
  5911. orcjit_stop_compile_threads(module);
  5912. #endif
  5913. #if WASM_ENABLE_JIT != 0
  5914. if (module->func_ptrs)
  5915. wasm_runtime_free(module->func_ptrs);
  5916. if (module->comp_ctx)
  5917. aot_destroy_comp_context(module->comp_ctx);
  5918. if (module->comp_data)
  5919. aot_destroy_comp_data(module->comp_data);
  5920. #endif
  5921. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5922. && WASM_ENABLE_LAZY_JIT != 0
  5923. if (module->tierup_wait_lock_inited) {
  5924. os_mutex_destroy(&module->tierup_wait_lock);
  5925. os_cond_destroy(&module->tierup_wait_cond);
  5926. }
  5927. #endif
  5928. if (module->types) {
  5929. for (i = 0; i < module->type_count; i++) {
  5930. if (module->types[i])
  5931. destroy_wasm_type(module->types[i]);
  5932. }
  5933. wasm_runtime_free(module->types);
  5934. }
  5935. if (module->imports)
  5936. wasm_runtime_free(module->imports);
  5937. if (module->functions) {
  5938. for (i = 0; i < module->function_count; i++) {
  5939. if (module->functions[i]) {
  5940. if (module->functions[i]->local_offsets)
  5941. wasm_runtime_free(module->functions[i]->local_offsets);
  5942. #if WASM_ENABLE_FAST_INTERP != 0
  5943. if (module->functions[i]->code_compiled)
  5944. wasm_runtime_free(module->functions[i]->code_compiled);
  5945. if (module->functions[i]->consts)
  5946. wasm_runtime_free(module->functions[i]->consts);
  5947. #endif
  5948. #if WASM_ENABLE_FAST_JIT != 0
  5949. if (module->functions[i]->fast_jit_jitted_code) {
  5950. jit_code_cache_free(
  5951. module->functions[i]->fast_jit_jitted_code);
  5952. }
  5953. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5954. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5955. jit_code_cache_free(
  5956. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5957. }
  5958. #endif
  5959. #endif
  5960. #if WASM_ENABLE_GC != 0
  5961. if (module->functions[i]->local_ref_type_maps) {
  5962. wasm_runtime_free(
  5963. module->functions[i]->local_ref_type_maps);
  5964. }
  5965. #endif
  5966. wasm_runtime_free(module->functions[i]);
  5967. }
  5968. }
  5969. wasm_runtime_free(module->functions);
  5970. }
  5971. if (module->tables) {
  5972. #if WASM_ENABLE_GC != 0
  5973. for (i = 0; i < module->table_count; i++) {
  5974. destroy_init_expr(&module->tables[i].init_expr);
  5975. }
  5976. #endif
  5977. wasm_runtime_free(module->tables);
  5978. }
  5979. if (module->memories)
  5980. wasm_runtime_free(module->memories);
  5981. if (module->globals) {
  5982. #if WASM_ENABLE_GC != 0
  5983. for (i = 0; i < module->global_count; i++) {
  5984. destroy_init_expr(&module->globals[i].init_expr);
  5985. }
  5986. #endif
  5987. wasm_runtime_free(module->globals);
  5988. }
  5989. #if WASM_ENABLE_TAGS != 0
  5990. if (module->tags) {
  5991. for (i = 0; i < module->tag_count; i++) {
  5992. if (module->tags[i])
  5993. wasm_runtime_free(module->tags[i]);
  5994. }
  5995. wasm_runtime_free(module->tags);
  5996. }
  5997. #endif
  5998. if (module->exports)
  5999. wasm_runtime_free(module->exports);
  6000. if (module->table_segments) {
  6001. for (i = 0; i < module->table_seg_count; i++) {
  6002. if (module->table_segments[i].init_values) {
  6003. #if WASM_ENABLE_GC != 0
  6004. uint32 j;
  6005. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6006. destroy_init_expr(
  6007. &module->table_segments[i].init_values[j]);
  6008. }
  6009. #endif
  6010. wasm_runtime_free(module->table_segments[i].init_values);
  6011. }
  6012. }
  6013. wasm_runtime_free(module->table_segments);
  6014. }
  6015. if (module->data_segments) {
  6016. for (i = 0; i < module->data_seg_count; i++) {
  6017. if (module->data_segments[i])
  6018. wasm_runtime_free(module->data_segments[i]);
  6019. }
  6020. wasm_runtime_free(module->data_segments);
  6021. }
  6022. if (module->const_str_list) {
  6023. StringNode *node = module->const_str_list, *node_next;
  6024. while (node) {
  6025. node_next = node->next;
  6026. wasm_runtime_free(node);
  6027. node = node_next;
  6028. }
  6029. }
  6030. #if WASM_ENABLE_STRINGREF != 0
  6031. if (module->string_literal_ptrs) {
  6032. wasm_runtime_free((void *)module->string_literal_ptrs);
  6033. }
  6034. if (module->string_literal_lengths) {
  6035. wasm_runtime_free(module->string_literal_lengths);
  6036. }
  6037. #endif
  6038. #if WASM_ENABLE_FAST_INTERP == 0
  6039. if (module->br_table_cache_list) {
  6040. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6041. BrTableCache *node_next;
  6042. while (node) {
  6043. node_next = bh_list_elem_next(node);
  6044. wasm_runtime_free(node);
  6045. node = node_next;
  6046. }
  6047. }
  6048. #endif
  6049. #if WASM_ENABLE_MULTI_MODULE != 0
  6050. /* just release the sub module list */
  6051. if (module->import_module_list) {
  6052. WASMRegisteredModule *node =
  6053. bh_list_first_elem(module->import_module_list);
  6054. while (node) {
  6055. WASMRegisteredModule *next = bh_list_elem_next(node);
  6056. bh_list_remove(module->import_module_list, node);
  6057. /*
  6058. * unload(sub_module) will be trigged during runtime_destroy().
  6059. * every module in the global module list will be unloaded one by
  6060. * one. so don't worry.
  6061. */
  6062. wasm_runtime_free(node);
  6063. /*
  6064. * the module file reading buffer will be released
  6065. * in runtime_destroy()
  6066. */
  6067. node = next;
  6068. }
  6069. }
  6070. #endif
  6071. #if WASM_ENABLE_DEBUG_INTERP != 0
  6072. WASMFastOPCodeNode *fast_opcode =
  6073. bh_list_first_elem(&module->fast_opcode_list);
  6074. while (fast_opcode) {
  6075. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6076. wasm_runtime_free(fast_opcode);
  6077. fast_opcode = next;
  6078. }
  6079. #endif
  6080. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6081. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6082. && WASM_ENABLE_LAZY_JIT != 0)
  6083. os_mutex_destroy(&module->instance_list_lock);
  6084. #endif
  6085. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6086. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6087. #endif
  6088. #if WASM_ENABLE_FAST_JIT != 0
  6089. if (module->fast_jit_func_ptrs) {
  6090. wasm_runtime_free(module->fast_jit_func_ptrs);
  6091. }
  6092. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6093. if (module->fast_jit_thread_locks_inited[i]) {
  6094. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6095. }
  6096. }
  6097. #endif
  6098. #if WASM_ENABLE_GC != 0
  6099. os_mutex_destroy(&module->rtt_type_lock);
  6100. bh_hash_map_destroy(module->ref_type_set);
  6101. if (module->rtt_types) {
  6102. for (i = 0; i < module->type_count; i++) {
  6103. if (module->rtt_types[i])
  6104. wasm_runtime_free(module->rtt_types[i]);
  6105. }
  6106. wasm_runtime_free(module->rtt_types);
  6107. }
  6108. #if WASM_ENABLE_STRINGREF != 0
  6109. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6110. if (module->stringref_rtts[i])
  6111. wasm_runtime_free(module->stringref_rtts[i]);
  6112. }
  6113. #endif
  6114. #endif
  6115. wasm_runtime_free(module);
  6116. }
  6117. bool
  6118. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6119. const uint8 *start_addr, const uint8 *code_end_addr,
  6120. uint8 label_type, uint8 **p_else_addr,
  6121. uint8 **p_end_addr)
  6122. {
  6123. const uint8 *p = start_addr, *p_end = code_end_addr;
  6124. uint8 *else_addr = NULL;
  6125. char error_buf[128];
  6126. uint32 block_nested_depth = 1, count, i, j, t;
  6127. uint32 error_buf_size = sizeof(error_buf);
  6128. uint8 opcode, u8;
  6129. BlockAddr block_stack[16] = { { 0 } }, *block;
  6130. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6131. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6132. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6133. if (block[j].start_addr == start_addr) {
  6134. /* Cache hit */
  6135. *p_else_addr = block[j].else_addr;
  6136. *p_end_addr = block[j].end_addr;
  6137. return true;
  6138. }
  6139. }
  6140. /* Cache unhit */
  6141. block_stack[0].start_addr = start_addr;
  6142. while (p < code_end_addr) {
  6143. opcode = *p++;
  6144. #if WASM_ENABLE_DEBUG_INTERP != 0
  6145. op_break_retry:
  6146. #endif
  6147. switch (opcode) {
  6148. case WASM_OP_UNREACHABLE:
  6149. case WASM_OP_NOP:
  6150. break;
  6151. #if WASM_ENABLE_EXCE_HANDLING != 0
  6152. case WASM_OP_TRY:
  6153. u8 = read_uint8(p);
  6154. if (block_nested_depth
  6155. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6156. block_stack[block_nested_depth].start_addr = p;
  6157. block_stack[block_nested_depth].else_addr = NULL;
  6158. }
  6159. block_nested_depth++;
  6160. break;
  6161. case EXT_OP_TRY:
  6162. skip_leb_uint32(p, p_end);
  6163. if (block_nested_depth
  6164. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6165. block_stack[block_nested_depth].start_addr = p;
  6166. block_stack[block_nested_depth].else_addr = NULL;
  6167. }
  6168. block_nested_depth++;
  6169. break;
  6170. case WASM_OP_CATCH:
  6171. if (block_nested_depth == 1) {
  6172. *p_end_addr = (uint8 *)(p - 1);
  6173. /* stop search and return the address of the catch block */
  6174. return true;
  6175. }
  6176. break;
  6177. case WASM_OP_CATCH_ALL:
  6178. if (block_nested_depth == 1) {
  6179. *p_end_addr = (uint8 *)(p - 1);
  6180. /* stop search and return the address of the catch_all block
  6181. */
  6182. return true;
  6183. }
  6184. break;
  6185. case WASM_OP_THROW:
  6186. /* skip tag_index */
  6187. skip_leb(p);
  6188. break;
  6189. case WASM_OP_RETHROW:
  6190. /* skip depth */
  6191. skip_leb(p);
  6192. break;
  6193. case WASM_OP_DELEGATE:
  6194. if (block_nested_depth == 1) {
  6195. *p_end_addr = (uint8 *)(p - 1);
  6196. return true;
  6197. }
  6198. else {
  6199. skip_leb(p);
  6200. /* the DELEGATE opcode ends the tryblock, */
  6201. block_nested_depth--;
  6202. if (block_nested_depth
  6203. < sizeof(block_stack) / sizeof(BlockAddr))
  6204. block_stack[block_nested_depth].end_addr =
  6205. (uint8 *)(p - 1);
  6206. }
  6207. break;
  6208. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6209. case WASM_OP_BLOCK:
  6210. case WASM_OP_LOOP:
  6211. case WASM_OP_IF:
  6212. {
  6213. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6214. u8 = read_uint8(p);
  6215. if (is_byte_a_type(u8)) {
  6216. #if WASM_ENABLE_GC != 0
  6217. if (wasm_is_type_multi_byte_type(u8)) {
  6218. /* the possible extra bytes of GC ref type have been
  6219. modified to OP_NOP, no need to resolve them again */
  6220. }
  6221. #endif
  6222. }
  6223. else {
  6224. p--;
  6225. skip_leb_uint32(p, p_end);
  6226. }
  6227. if (block_nested_depth
  6228. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6229. block_stack[block_nested_depth].start_addr = p;
  6230. block_stack[block_nested_depth].else_addr = NULL;
  6231. }
  6232. block_nested_depth++;
  6233. break;
  6234. }
  6235. case EXT_OP_BLOCK:
  6236. case EXT_OP_LOOP:
  6237. case EXT_OP_IF:
  6238. /* block type */
  6239. skip_leb_uint32(p, p_end);
  6240. if (block_nested_depth
  6241. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6242. block_stack[block_nested_depth].start_addr = p;
  6243. block_stack[block_nested_depth].else_addr = NULL;
  6244. }
  6245. block_nested_depth++;
  6246. break;
  6247. case WASM_OP_ELSE:
  6248. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6249. else_addr = (uint8 *)(p - 1);
  6250. if (block_nested_depth - 1
  6251. < sizeof(block_stack) / sizeof(BlockAddr))
  6252. block_stack[block_nested_depth - 1].else_addr =
  6253. (uint8 *)(p - 1);
  6254. break;
  6255. case WASM_OP_END:
  6256. if (block_nested_depth == 1) {
  6257. if (label_type == LABEL_TYPE_IF)
  6258. *p_else_addr = else_addr;
  6259. *p_end_addr = (uint8 *)(p - 1);
  6260. block_stack[0].end_addr = (uint8 *)(p - 1);
  6261. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6262. t++) {
  6263. start_addr = block_stack[t].start_addr;
  6264. if (start_addr) {
  6265. i = ((uintptr_t)start_addr)
  6266. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6267. block =
  6268. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6269. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6270. if (!block[j].start_addr)
  6271. break;
  6272. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6273. memmove(block + 1, block,
  6274. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6275. * sizeof(BlockAddr));
  6276. j = 0;
  6277. }
  6278. block[j].start_addr = block_stack[t].start_addr;
  6279. block[j].else_addr = block_stack[t].else_addr;
  6280. block[j].end_addr = block_stack[t].end_addr;
  6281. }
  6282. else
  6283. break;
  6284. }
  6285. return true;
  6286. }
  6287. else {
  6288. block_nested_depth--;
  6289. if (block_nested_depth
  6290. < sizeof(block_stack) / sizeof(BlockAddr))
  6291. block_stack[block_nested_depth].end_addr =
  6292. (uint8 *)(p - 1);
  6293. }
  6294. break;
  6295. case WASM_OP_BR:
  6296. case WASM_OP_BR_IF:
  6297. skip_leb_uint32(p, p_end); /* labelidx */
  6298. break;
  6299. case WASM_OP_BR_TABLE:
  6300. read_leb_uint32(p, p_end, count); /* lable num */
  6301. #if WASM_ENABLE_FAST_INTERP != 0
  6302. for (i = 0; i <= count; i++) /* lableidxs */
  6303. skip_leb_uint32(p, p_end);
  6304. #else
  6305. p += count + 1;
  6306. while (*p == WASM_OP_NOP)
  6307. p++;
  6308. #endif
  6309. break;
  6310. #if WASM_ENABLE_FAST_INTERP == 0
  6311. case EXT_OP_BR_TABLE_CACHE:
  6312. read_leb_uint32(p, p_end, count); /* lable num */
  6313. while (*p == WASM_OP_NOP)
  6314. p++;
  6315. break;
  6316. #endif
  6317. case WASM_OP_RETURN:
  6318. break;
  6319. case WASM_OP_CALL:
  6320. #if WASM_ENABLE_TAIL_CALL != 0
  6321. case WASM_OP_RETURN_CALL:
  6322. #endif
  6323. skip_leb_uint32(p, p_end); /* funcidx */
  6324. break;
  6325. case WASM_OP_CALL_INDIRECT:
  6326. #if WASM_ENABLE_TAIL_CALL != 0
  6327. case WASM_OP_RETURN_CALL_INDIRECT:
  6328. #endif
  6329. skip_leb_uint32(p, p_end); /* typeidx */
  6330. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6331. u8 = read_uint8(p); /* 0x00 */
  6332. #else
  6333. skip_leb_uint32(p, p_end); /* talbeidx */
  6334. #endif
  6335. break;
  6336. #if WASM_ENABLE_GC != 0
  6337. case WASM_OP_CALL_REF:
  6338. case WASM_OP_RETURN_CALL_REF:
  6339. skip_leb_uint32(p, p_end); /* typeidx */
  6340. break;
  6341. #endif
  6342. case WASM_OP_DROP:
  6343. case WASM_OP_SELECT:
  6344. case WASM_OP_DROP_64:
  6345. case WASM_OP_SELECT_64:
  6346. break;
  6347. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6348. case WASM_OP_SELECT_T:
  6349. {
  6350. skip_leb_uint32(p, p_end); /* vec length */
  6351. u8 = read_uint8(p); /* typeidx */
  6352. /* the possible extra bytes of GC ref type have been
  6353. modified to OP_NOP, no need to resolve them again */
  6354. break;
  6355. }
  6356. case WASM_OP_TABLE_GET:
  6357. case WASM_OP_TABLE_SET:
  6358. skip_leb_uint32(p, p_end); /* table index */
  6359. break;
  6360. case WASM_OP_REF_NULL:
  6361. {
  6362. u8 = read_uint8(p); /* type */
  6363. if (is_byte_a_type(u8)) {
  6364. #if WASM_ENABLE_GC != 0
  6365. if (wasm_is_type_multi_byte_type(u8)) {
  6366. /* the possible extra bytes of GC ref type have been
  6367. modified to OP_NOP, no need to resolve them again */
  6368. }
  6369. #endif
  6370. }
  6371. else {
  6372. p--;
  6373. skip_leb_uint32(p, p_end);
  6374. }
  6375. break;
  6376. }
  6377. case WASM_OP_REF_IS_NULL:
  6378. break;
  6379. case WASM_OP_REF_FUNC:
  6380. skip_leb_uint32(p, p_end); /* func index */
  6381. break;
  6382. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6383. #if WASM_ENABLE_GC != 0
  6384. case WASM_OP_REF_AS_NON_NULL:
  6385. case WASM_OP_REF_EQ:
  6386. break;
  6387. case WASM_OP_BR_ON_NULL:
  6388. case WASM_OP_BR_ON_NON_NULL:
  6389. skip_leb_uint32(p, p_end); /* label index */
  6390. break;
  6391. #endif /* end of WASM_ENABLE_GC != 0 */
  6392. case WASM_OP_GET_LOCAL:
  6393. case WASM_OP_SET_LOCAL:
  6394. case WASM_OP_TEE_LOCAL:
  6395. case WASM_OP_GET_GLOBAL:
  6396. case WASM_OP_SET_GLOBAL:
  6397. case WASM_OP_GET_GLOBAL_64:
  6398. case WASM_OP_SET_GLOBAL_64:
  6399. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6400. skip_leb_uint32(p, p_end); /* local index */
  6401. break;
  6402. case EXT_OP_GET_LOCAL_FAST:
  6403. case EXT_OP_SET_LOCAL_FAST:
  6404. case EXT_OP_TEE_LOCAL_FAST:
  6405. CHECK_BUF(p, p_end, 1);
  6406. p++;
  6407. break;
  6408. case WASM_OP_I32_LOAD:
  6409. case WASM_OP_I64_LOAD:
  6410. case WASM_OP_F32_LOAD:
  6411. case WASM_OP_F64_LOAD:
  6412. case WASM_OP_I32_LOAD8_S:
  6413. case WASM_OP_I32_LOAD8_U:
  6414. case WASM_OP_I32_LOAD16_S:
  6415. case WASM_OP_I32_LOAD16_U:
  6416. case WASM_OP_I64_LOAD8_S:
  6417. case WASM_OP_I64_LOAD8_U:
  6418. case WASM_OP_I64_LOAD16_S:
  6419. case WASM_OP_I64_LOAD16_U:
  6420. case WASM_OP_I64_LOAD32_S:
  6421. case WASM_OP_I64_LOAD32_U:
  6422. case WASM_OP_I32_STORE:
  6423. case WASM_OP_I64_STORE:
  6424. case WASM_OP_F32_STORE:
  6425. case WASM_OP_F64_STORE:
  6426. case WASM_OP_I32_STORE8:
  6427. case WASM_OP_I32_STORE16:
  6428. case WASM_OP_I64_STORE8:
  6429. case WASM_OP_I64_STORE16:
  6430. case WASM_OP_I64_STORE32:
  6431. skip_leb_uint32(p, p_end); /* align */
  6432. skip_leb_mem_offset(p, p_end); /* offset */
  6433. break;
  6434. case WASM_OP_MEMORY_SIZE:
  6435. case WASM_OP_MEMORY_GROW:
  6436. skip_leb_uint32(p, p_end); /* 0x00 */
  6437. break;
  6438. case WASM_OP_I32_CONST:
  6439. skip_leb_int32(p, p_end);
  6440. break;
  6441. case WASM_OP_I64_CONST:
  6442. skip_leb_int64(p, p_end);
  6443. break;
  6444. case WASM_OP_F32_CONST:
  6445. p += sizeof(float32);
  6446. break;
  6447. case WASM_OP_F64_CONST:
  6448. p += sizeof(float64);
  6449. break;
  6450. case WASM_OP_I32_EQZ:
  6451. case WASM_OP_I32_EQ:
  6452. case WASM_OP_I32_NE:
  6453. case WASM_OP_I32_LT_S:
  6454. case WASM_OP_I32_LT_U:
  6455. case WASM_OP_I32_GT_S:
  6456. case WASM_OP_I32_GT_U:
  6457. case WASM_OP_I32_LE_S:
  6458. case WASM_OP_I32_LE_U:
  6459. case WASM_OP_I32_GE_S:
  6460. case WASM_OP_I32_GE_U:
  6461. case WASM_OP_I64_EQZ:
  6462. case WASM_OP_I64_EQ:
  6463. case WASM_OP_I64_NE:
  6464. case WASM_OP_I64_LT_S:
  6465. case WASM_OP_I64_LT_U:
  6466. case WASM_OP_I64_GT_S:
  6467. case WASM_OP_I64_GT_U:
  6468. case WASM_OP_I64_LE_S:
  6469. case WASM_OP_I64_LE_U:
  6470. case WASM_OP_I64_GE_S:
  6471. case WASM_OP_I64_GE_U:
  6472. case WASM_OP_F32_EQ:
  6473. case WASM_OP_F32_NE:
  6474. case WASM_OP_F32_LT:
  6475. case WASM_OP_F32_GT:
  6476. case WASM_OP_F32_LE:
  6477. case WASM_OP_F32_GE:
  6478. case WASM_OP_F64_EQ:
  6479. case WASM_OP_F64_NE:
  6480. case WASM_OP_F64_LT:
  6481. case WASM_OP_F64_GT:
  6482. case WASM_OP_F64_LE:
  6483. case WASM_OP_F64_GE:
  6484. case WASM_OP_I32_CLZ:
  6485. case WASM_OP_I32_CTZ:
  6486. case WASM_OP_I32_POPCNT:
  6487. case WASM_OP_I32_ADD:
  6488. case WASM_OP_I32_SUB:
  6489. case WASM_OP_I32_MUL:
  6490. case WASM_OP_I32_DIV_S:
  6491. case WASM_OP_I32_DIV_U:
  6492. case WASM_OP_I32_REM_S:
  6493. case WASM_OP_I32_REM_U:
  6494. case WASM_OP_I32_AND:
  6495. case WASM_OP_I32_OR:
  6496. case WASM_OP_I32_XOR:
  6497. case WASM_OP_I32_SHL:
  6498. case WASM_OP_I32_SHR_S:
  6499. case WASM_OP_I32_SHR_U:
  6500. case WASM_OP_I32_ROTL:
  6501. case WASM_OP_I32_ROTR:
  6502. case WASM_OP_I64_CLZ:
  6503. case WASM_OP_I64_CTZ:
  6504. case WASM_OP_I64_POPCNT:
  6505. case WASM_OP_I64_ADD:
  6506. case WASM_OP_I64_SUB:
  6507. case WASM_OP_I64_MUL:
  6508. case WASM_OP_I64_DIV_S:
  6509. case WASM_OP_I64_DIV_U:
  6510. case WASM_OP_I64_REM_S:
  6511. case WASM_OP_I64_REM_U:
  6512. case WASM_OP_I64_AND:
  6513. case WASM_OP_I64_OR:
  6514. case WASM_OP_I64_XOR:
  6515. case WASM_OP_I64_SHL:
  6516. case WASM_OP_I64_SHR_S:
  6517. case WASM_OP_I64_SHR_U:
  6518. case WASM_OP_I64_ROTL:
  6519. case WASM_OP_I64_ROTR:
  6520. case WASM_OP_F32_ABS:
  6521. case WASM_OP_F32_NEG:
  6522. case WASM_OP_F32_CEIL:
  6523. case WASM_OP_F32_FLOOR:
  6524. case WASM_OP_F32_TRUNC:
  6525. case WASM_OP_F32_NEAREST:
  6526. case WASM_OP_F32_SQRT:
  6527. case WASM_OP_F32_ADD:
  6528. case WASM_OP_F32_SUB:
  6529. case WASM_OP_F32_MUL:
  6530. case WASM_OP_F32_DIV:
  6531. case WASM_OP_F32_MIN:
  6532. case WASM_OP_F32_MAX:
  6533. case WASM_OP_F32_COPYSIGN:
  6534. case WASM_OP_F64_ABS:
  6535. case WASM_OP_F64_NEG:
  6536. case WASM_OP_F64_CEIL:
  6537. case WASM_OP_F64_FLOOR:
  6538. case WASM_OP_F64_TRUNC:
  6539. case WASM_OP_F64_NEAREST:
  6540. case WASM_OP_F64_SQRT:
  6541. case WASM_OP_F64_ADD:
  6542. case WASM_OP_F64_SUB:
  6543. case WASM_OP_F64_MUL:
  6544. case WASM_OP_F64_DIV:
  6545. case WASM_OP_F64_MIN:
  6546. case WASM_OP_F64_MAX:
  6547. case WASM_OP_F64_COPYSIGN:
  6548. case WASM_OP_I32_WRAP_I64:
  6549. case WASM_OP_I32_TRUNC_S_F32:
  6550. case WASM_OP_I32_TRUNC_U_F32:
  6551. case WASM_OP_I32_TRUNC_S_F64:
  6552. case WASM_OP_I32_TRUNC_U_F64:
  6553. case WASM_OP_I64_EXTEND_S_I32:
  6554. case WASM_OP_I64_EXTEND_U_I32:
  6555. case WASM_OP_I64_TRUNC_S_F32:
  6556. case WASM_OP_I64_TRUNC_U_F32:
  6557. case WASM_OP_I64_TRUNC_S_F64:
  6558. case WASM_OP_I64_TRUNC_U_F64:
  6559. case WASM_OP_F32_CONVERT_S_I32:
  6560. case WASM_OP_F32_CONVERT_U_I32:
  6561. case WASM_OP_F32_CONVERT_S_I64:
  6562. case WASM_OP_F32_CONVERT_U_I64:
  6563. case WASM_OP_F32_DEMOTE_F64:
  6564. case WASM_OP_F64_CONVERT_S_I32:
  6565. case WASM_OP_F64_CONVERT_U_I32:
  6566. case WASM_OP_F64_CONVERT_S_I64:
  6567. case WASM_OP_F64_CONVERT_U_I64:
  6568. case WASM_OP_F64_PROMOTE_F32:
  6569. case WASM_OP_I32_REINTERPRET_F32:
  6570. case WASM_OP_I64_REINTERPRET_F64:
  6571. case WASM_OP_F32_REINTERPRET_I32:
  6572. case WASM_OP_F64_REINTERPRET_I64:
  6573. case WASM_OP_I32_EXTEND8_S:
  6574. case WASM_OP_I32_EXTEND16_S:
  6575. case WASM_OP_I64_EXTEND8_S:
  6576. case WASM_OP_I64_EXTEND16_S:
  6577. case WASM_OP_I64_EXTEND32_S:
  6578. break;
  6579. #if WASM_ENABLE_GC != 0
  6580. case WASM_OP_GC_PREFIX:
  6581. {
  6582. uint32 opcode1;
  6583. read_leb_uint32(p, p_end, opcode1);
  6584. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6585. is no larger than UINT8_MAX */
  6586. opcode = (uint8)opcode1;
  6587. switch (opcode) {
  6588. case WASM_OP_STRUCT_NEW:
  6589. case WASM_OP_STRUCT_NEW_DEFAULT:
  6590. skip_leb_uint32(p, p_end); /* typeidx */
  6591. break;
  6592. case WASM_OP_STRUCT_GET:
  6593. case WASM_OP_STRUCT_GET_S:
  6594. case WASM_OP_STRUCT_GET_U:
  6595. case WASM_OP_STRUCT_SET:
  6596. skip_leb_uint32(p, p_end); /* typeidx */
  6597. skip_leb_uint32(p, p_end); /* fieldidx */
  6598. break;
  6599. case WASM_OP_ARRAY_NEW:
  6600. case WASM_OP_ARRAY_NEW_DEFAULT:
  6601. case WASM_OP_ARRAY_GET:
  6602. case WASM_OP_ARRAY_GET_S:
  6603. case WASM_OP_ARRAY_GET_U:
  6604. case WASM_OP_ARRAY_SET:
  6605. case WASM_OP_ARRAY_FILL:
  6606. skip_leb_uint32(p, p_end); /* typeidx */
  6607. break;
  6608. case WASM_OP_ARRAY_COPY:
  6609. skip_leb_uint32(p, p_end); /* typeidx1 */
  6610. skip_leb_uint32(p, p_end); /* typeidx2 */
  6611. break;
  6612. case WASM_OP_ARRAY_LEN:
  6613. break;
  6614. case WASM_OP_ARRAY_NEW_FIXED:
  6615. case WASM_OP_ARRAY_NEW_DATA:
  6616. case WASM_OP_ARRAY_NEW_ELEM:
  6617. skip_leb_uint32(p, p_end); /* typeidx */
  6618. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6619. break;
  6620. case WASM_OP_REF_I31:
  6621. case WASM_OP_I31_GET_S:
  6622. case WASM_OP_I31_GET_U:
  6623. break;
  6624. case WASM_OP_REF_TEST:
  6625. case WASM_OP_REF_CAST:
  6626. case WASM_OP_REF_TEST_NULLABLE:
  6627. case WASM_OP_REF_CAST_NULLABLE:
  6628. skip_leb_int32(p, p_end); /* heaptype */
  6629. break;
  6630. case WASM_OP_BR_ON_CAST:
  6631. case WASM_OP_BR_ON_CAST_FAIL:
  6632. p += sizeof(uint8); /* castflag */
  6633. skip_leb_uint32(p, p_end); /* labelidx */
  6634. skip_leb_int32(p, p_end); /* heaptype */
  6635. skip_leb_int32(p, p_end); /* heaptype2 */
  6636. break;
  6637. case WASM_OP_ANY_CONVERT_EXTERN:
  6638. case WASM_OP_EXTERN_CONVERT_ANY:
  6639. break;
  6640. #if WASM_ENABLE_STRINGREF != 0
  6641. case WASM_OP_STRING_NEW_UTF8:
  6642. case WASM_OP_STRING_NEW_WTF16:
  6643. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6644. case WASM_OP_STRING_NEW_WTF8:
  6645. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6646. break;
  6647. case WASM_OP_STRING_CONST:
  6648. skip_leb_int32(p, p_end); /* contents */
  6649. break;
  6650. case WASM_OP_STRING_MEASURE_UTF8:
  6651. case WASM_OP_STRING_MEASURE_WTF8:
  6652. case WASM_OP_STRING_MEASURE_WTF16:
  6653. break;
  6654. case WASM_OP_STRING_ENCODE_UTF8:
  6655. case WASM_OP_STRING_ENCODE_WTF16:
  6656. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6657. case WASM_OP_STRING_ENCODE_WTF8:
  6658. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6659. break;
  6660. case WASM_OP_STRING_CONCAT:
  6661. case WASM_OP_STRING_EQ:
  6662. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6663. case WASM_OP_STRING_AS_WTF8:
  6664. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6665. break;
  6666. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6667. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6668. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6669. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6670. break;
  6671. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6672. case WASM_OP_STRING_AS_WTF16:
  6673. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6674. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6675. break;
  6676. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6677. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6678. break;
  6679. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6680. case WASM_OP_STRING_AS_ITER:
  6681. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6682. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6683. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6684. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6685. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6686. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6687. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6688. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6689. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6690. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6691. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6692. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6693. break;
  6694. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6695. default:
  6696. return false;
  6697. }
  6698. break;
  6699. }
  6700. #endif /* end of WASM_ENABLE_GC != 0 */
  6701. case WASM_OP_MISC_PREFIX:
  6702. {
  6703. uint32 opcode1;
  6704. read_leb_uint32(p, p_end, opcode1);
  6705. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6706. is no larger than UINT8_MAX */
  6707. opcode = (uint8)opcode1;
  6708. switch (opcode) {
  6709. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6710. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6711. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6712. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6713. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6714. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6715. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6716. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6717. break;
  6718. #if WASM_ENABLE_BULK_MEMORY != 0
  6719. case WASM_OP_MEMORY_INIT:
  6720. skip_leb_uint32(p, p_end);
  6721. /* skip memory idx */
  6722. p++;
  6723. break;
  6724. case WASM_OP_DATA_DROP:
  6725. skip_leb_uint32(p, p_end);
  6726. break;
  6727. case WASM_OP_MEMORY_COPY:
  6728. /* skip two memory idx */
  6729. p += 2;
  6730. break;
  6731. case WASM_OP_MEMORY_FILL:
  6732. /* skip memory idx */
  6733. p++;
  6734. break;
  6735. #endif /* WASM_ENABLE_BULK_MEMORY */
  6736. #if WASM_ENABLE_REF_TYPES != 0
  6737. case WASM_OP_TABLE_INIT:
  6738. case WASM_OP_TABLE_COPY:
  6739. /* tableidx */
  6740. skip_leb_uint32(p, p_end);
  6741. /* elemidx */
  6742. skip_leb_uint32(p, p_end);
  6743. break;
  6744. case WASM_OP_ELEM_DROP:
  6745. /* elemidx */
  6746. skip_leb_uint32(p, p_end);
  6747. break;
  6748. case WASM_OP_TABLE_SIZE:
  6749. case WASM_OP_TABLE_GROW:
  6750. case WASM_OP_TABLE_FILL:
  6751. skip_leb_uint32(p, p_end); /* table idx */
  6752. break;
  6753. #endif /* WASM_ENABLE_REF_TYPES */
  6754. default:
  6755. return false;
  6756. }
  6757. break;
  6758. }
  6759. #if WASM_ENABLE_SIMD != 0
  6760. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6761. case WASM_OP_SIMD_PREFIX:
  6762. {
  6763. /* TODO: memory64 offset type changes */
  6764. uint32 opcode1;
  6765. read_leb_uint32(p, p_end, opcode1);
  6766. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6767. is no larger than UINT8_MAX */
  6768. opcode = (uint8)opcode1;
  6769. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6770. */
  6771. switch (opcode) {
  6772. case SIMD_v128_load:
  6773. case SIMD_v128_load8x8_s:
  6774. case SIMD_v128_load8x8_u:
  6775. case SIMD_v128_load16x4_s:
  6776. case SIMD_v128_load16x4_u:
  6777. case SIMD_v128_load32x2_s:
  6778. case SIMD_v128_load32x2_u:
  6779. case SIMD_v128_load8_splat:
  6780. case SIMD_v128_load16_splat:
  6781. case SIMD_v128_load32_splat:
  6782. case SIMD_v128_load64_splat:
  6783. case SIMD_v128_store:
  6784. /* memarg align */
  6785. skip_leb_uint32(p, p_end);
  6786. /* memarg offset*/
  6787. skip_leb_uint32(p, p_end);
  6788. break;
  6789. case SIMD_v128_const:
  6790. case SIMD_v8x16_shuffle:
  6791. /* immByte[16] immLaneId[16] */
  6792. CHECK_BUF1(p, p_end, 16);
  6793. p += 16;
  6794. break;
  6795. case SIMD_i8x16_extract_lane_s:
  6796. case SIMD_i8x16_extract_lane_u:
  6797. case SIMD_i8x16_replace_lane:
  6798. case SIMD_i16x8_extract_lane_s:
  6799. case SIMD_i16x8_extract_lane_u:
  6800. case SIMD_i16x8_replace_lane:
  6801. case SIMD_i32x4_extract_lane:
  6802. case SIMD_i32x4_replace_lane:
  6803. case SIMD_i64x2_extract_lane:
  6804. case SIMD_i64x2_replace_lane:
  6805. case SIMD_f32x4_extract_lane:
  6806. case SIMD_f32x4_replace_lane:
  6807. case SIMD_f64x2_extract_lane:
  6808. case SIMD_f64x2_replace_lane:
  6809. /* ImmLaneId */
  6810. CHECK_BUF(p, p_end, 1);
  6811. p++;
  6812. break;
  6813. case SIMD_v128_load8_lane:
  6814. case SIMD_v128_load16_lane:
  6815. case SIMD_v128_load32_lane:
  6816. case SIMD_v128_load64_lane:
  6817. case SIMD_v128_store8_lane:
  6818. case SIMD_v128_store16_lane:
  6819. case SIMD_v128_store32_lane:
  6820. case SIMD_v128_store64_lane:
  6821. /* memarg align */
  6822. skip_leb_uint32(p, p_end);
  6823. /* memarg offset*/
  6824. skip_leb_uint32(p, p_end);
  6825. /* ImmLaneId */
  6826. CHECK_BUF(p, p_end, 1);
  6827. p++;
  6828. break;
  6829. case SIMD_v128_load32_zero:
  6830. case SIMD_v128_load64_zero:
  6831. /* memarg align */
  6832. skip_leb_uint32(p, p_end);
  6833. /* memarg offset*/
  6834. skip_leb_uint32(p, p_end);
  6835. break;
  6836. default:
  6837. /*
  6838. * since latest SIMD specific used almost every value
  6839. * from 0x00 to 0xff, the default branch will present
  6840. * all opcodes without imm
  6841. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6842. */
  6843. break;
  6844. }
  6845. break;
  6846. }
  6847. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6848. #endif /* end of WASM_ENABLE_SIMD */
  6849. #if WASM_ENABLE_SHARED_MEMORY != 0
  6850. case WASM_OP_ATOMIC_PREFIX:
  6851. {
  6852. /* TODO: memory64 offset type changes */
  6853. uint32 opcode1;
  6854. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6855. read_leb_uint32(p, p_end, opcode1);
  6856. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6857. is no larger than UINT8_MAX */
  6858. opcode = (uint8)opcode1;
  6859. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6860. skip_leb_uint32(p, p_end); /* align */
  6861. skip_leb_mem_offset(p, p_end); /* offset */
  6862. }
  6863. else {
  6864. /* atomic.fence doesn't have memarg */
  6865. p++;
  6866. }
  6867. break;
  6868. }
  6869. #endif
  6870. #if WASM_ENABLE_DEBUG_INTERP != 0
  6871. case DEBUG_OP_BREAK:
  6872. {
  6873. WASMDebugInstance *debug_instance =
  6874. wasm_exec_env_get_instance(exec_env);
  6875. char orignal_opcode[1];
  6876. uint64 size = 1;
  6877. WASMModuleInstance *module_inst =
  6878. (WASMModuleInstance *)exec_env->module_inst;
  6879. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6880. ? (p - 1) - module_inst->module->load_addr
  6881. : ~0;
  6882. if (debug_instance) {
  6883. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6884. orignal_opcode, &size)
  6885. && size == 1) {
  6886. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6887. "with %02x: ",
  6888. orignal_opcode[0]);
  6889. opcode = orignal_opcode[0];
  6890. goto op_break_retry;
  6891. }
  6892. }
  6893. break;
  6894. }
  6895. #endif
  6896. default:
  6897. return false;
  6898. }
  6899. }
  6900. (void)u8;
  6901. (void)exec_env;
  6902. return false;
  6903. fail:
  6904. return false;
  6905. }
  6906. #if WASM_ENABLE_FAST_INTERP != 0
  6907. #if WASM_DEBUG_PREPROCESSOR != 0
  6908. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6909. #else
  6910. #define LOG_OP(...) (void)0
  6911. #endif
  6912. #define PATCH_ELSE 0
  6913. #define PATCH_END 1
  6914. typedef struct BranchBlockPatch {
  6915. struct BranchBlockPatch *next;
  6916. uint8 patch_type;
  6917. uint8 *code_compiled;
  6918. } BranchBlockPatch;
  6919. #endif
  6920. typedef struct BranchBlock {
  6921. uint8 label_type;
  6922. BlockType block_type;
  6923. uint8 *start_addr;
  6924. uint8 *else_addr;
  6925. uint8 *end_addr;
  6926. uint32 stack_cell_num;
  6927. #if WASM_ENABLE_GC != 0
  6928. uint32 reftype_map_num;
  6929. /* Indicate which local is used inside current block, used to validate
  6930. * local.get with non-nullable ref types */
  6931. uint8 *local_use_mask;
  6932. uint32 local_use_mask_size;
  6933. #endif
  6934. #if WASM_ENABLE_FAST_INTERP != 0
  6935. uint16 dynamic_offset;
  6936. uint8 *code_compiled;
  6937. BranchBlockPatch *patch_list;
  6938. /* This is used to save params frame_offset of of if block */
  6939. int16 *param_frame_offsets;
  6940. /* This is used to recover dynamic offset for else branch */
  6941. uint16 start_dynamic_offset;
  6942. #endif
  6943. /* Indicate the operand stack is in polymorphic state.
  6944. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6945. * to polymorphic state until the block's 'end' opcode is processed.
  6946. * If stack is in polymorphic state and stack is empty, instruction can
  6947. * pop any type of value directly without decreasing stack top pointer
  6948. * and stack cell num. */
  6949. bool is_stack_polymorphic;
  6950. } BranchBlock;
  6951. typedef struct WASMLoaderContext {
  6952. /* frame ref stack */
  6953. uint8 *frame_ref;
  6954. uint8 *frame_ref_bottom;
  6955. uint8 *frame_ref_boundary;
  6956. uint32 frame_ref_size;
  6957. uint32 stack_cell_num;
  6958. uint32 max_stack_cell_num;
  6959. #if WASM_ENABLE_GC != 0
  6960. /* frame reftype map stack */
  6961. WASMRefTypeMap *frame_reftype_map;
  6962. WASMRefTypeMap *frame_reftype_map_bottom;
  6963. WASMRefTypeMap *frame_reftype_map_boundary;
  6964. uint32 frame_reftype_map_size;
  6965. uint32 reftype_map_num;
  6966. uint32 max_reftype_map_num;
  6967. /* Current module */
  6968. WASMModule *module;
  6969. /* Current module's ref_type_set */
  6970. HashMap *ref_type_set;
  6971. /* Always point to local variable ref_type of
  6972. wasm_loader_prepare_bytecode */
  6973. WASMRefType *ref_type_tmp;
  6974. #endif
  6975. /* frame csp stack */
  6976. BranchBlock *frame_csp;
  6977. BranchBlock *frame_csp_bottom;
  6978. BranchBlock *frame_csp_boundary;
  6979. uint32 frame_csp_size;
  6980. uint32 csp_num;
  6981. uint32 max_csp_num;
  6982. #if WASM_ENABLE_FAST_INTERP != 0
  6983. /* frame offset stack */
  6984. int16 *frame_offset;
  6985. int16 *frame_offset_bottom;
  6986. int16 *frame_offset_boundary;
  6987. uint32 frame_offset_size;
  6988. int16 dynamic_offset;
  6989. int16 start_dynamic_offset;
  6990. int16 max_dynamic_offset;
  6991. /* preserved local offset */
  6992. int16 preserved_local_offset;
  6993. /* const buffer */
  6994. uint8 *const_buf;
  6995. uint16 num_const;
  6996. uint16 const_cell_num;
  6997. uint32 const_buf_size;
  6998. /* processed code */
  6999. uint8 *p_code_compiled;
  7000. uint8 *p_code_compiled_end;
  7001. uint32 code_compiled_size;
  7002. /* If the last opcode will be dropped, the peak memory usage will be larger
  7003. * than the final code_compiled_size, we record the peak size to ensure
  7004. * there will not be invalid memory access during second traverse */
  7005. uint32 code_compiled_peak_size;
  7006. #endif
  7007. } WASMLoaderContext;
  7008. typedef struct Const {
  7009. WASMValue value;
  7010. uint16 slot_index;
  7011. uint8 value_type;
  7012. } Const;
  7013. #define CHECK_CSP_PUSH() \
  7014. do { \
  7015. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7016. MEM_REALLOC( \
  7017. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7018. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7019. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7020. ctx->frame_csp_boundary = \
  7021. ctx->frame_csp_bottom \
  7022. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7023. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7024. } \
  7025. } while (0)
  7026. #define CHECK_CSP_POP() \
  7027. do { \
  7028. if (ctx->csp_num < 1) { \
  7029. set_error_buf(error_buf, error_buf_size, \
  7030. "type mismatch: " \
  7031. "expect data but block stack was empty"); \
  7032. goto fail; \
  7033. } \
  7034. } while (0)
  7035. #if WASM_ENABLE_FAST_INTERP != 0
  7036. static bool
  7037. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7038. uint32 error_buf_size)
  7039. {
  7040. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7041. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7042. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7043. ctx->frame_offset_size + 16);
  7044. ctx->frame_offset_size += 16;
  7045. ctx->frame_offset_boundary =
  7046. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7047. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7048. }
  7049. return true;
  7050. fail:
  7051. return false;
  7052. }
  7053. static bool
  7054. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7055. {
  7056. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7057. return false;
  7058. return true;
  7059. }
  7060. static void
  7061. free_label_patch_list(BranchBlock *frame_csp)
  7062. {
  7063. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7064. BranchBlockPatch *next;
  7065. while (label_patch != NULL) {
  7066. next = label_patch->next;
  7067. wasm_runtime_free(label_patch);
  7068. label_patch = next;
  7069. }
  7070. frame_csp->patch_list = NULL;
  7071. }
  7072. static void
  7073. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7074. {
  7075. BranchBlock *tmp_csp = frame_csp;
  7076. for (uint32 i = 0; i < csp_num; i++) {
  7077. free_label_patch_list(tmp_csp);
  7078. tmp_csp++;
  7079. }
  7080. }
  7081. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7082. #if WASM_ENABLE_GC != 0
  7083. static bool
  7084. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7085. char *error_buf, uint32 error_buf_size)
  7086. {
  7087. BranchBlock *current_csp = ctx->frame_csp - 1;
  7088. uint32 local_mask_size;
  7089. if (local_count == 0) {
  7090. current_csp->local_use_mask_size = 0;
  7091. return true;
  7092. }
  7093. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7094. * else branch, we don't need to allocate memory again */
  7095. if (!current_csp->local_use_mask) {
  7096. local_mask_size = (local_count + 7) / sizeof(uint8);
  7097. if (!(current_csp->local_use_mask =
  7098. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7099. return false;
  7100. }
  7101. current_csp->local_use_mask_size = local_mask_size;
  7102. }
  7103. else {
  7104. local_mask_size = current_csp->local_use_mask_size;
  7105. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7106. }
  7107. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7108. /* For non-function blocks, inherit the use status from parent block */
  7109. BranchBlock *parent_csp = current_csp - 1;
  7110. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7111. bh_assert(parent_csp->local_use_mask);
  7112. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7113. parent_csp->local_use_mask, local_mask_size);
  7114. }
  7115. return true;
  7116. }
  7117. static void
  7118. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7119. {
  7120. BranchBlock *current_csp = ctx->frame_csp - 1;
  7121. bh_assert(current_csp->local_use_mask
  7122. || current_csp->local_use_mask_size == 0);
  7123. if (current_csp->local_use_mask) {
  7124. wasm_runtime_free(current_csp->local_use_mask);
  7125. }
  7126. current_csp->local_use_mask = NULL;
  7127. current_csp->local_use_mask_size = 0;
  7128. }
  7129. static void
  7130. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7131. {
  7132. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7133. uint32 i;
  7134. for (i = 0; i < ctx->csp_num; i++) {
  7135. if (tmp_csp->local_use_mask) {
  7136. wasm_runtime_free(tmp_csp->local_use_mask);
  7137. tmp_csp->local_use_mask = NULL;
  7138. tmp_csp->local_use_mask_size = 0;
  7139. }
  7140. tmp_csp++;
  7141. }
  7142. }
  7143. static void
  7144. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7145. {
  7146. BranchBlock *current_csp = ctx->frame_csp - 1;
  7147. uint32 byte_offset = index / sizeof(uint8);
  7148. uint32 bit_offset = index % sizeof(uint8);
  7149. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7150. bh_assert(current_csp->local_use_mask);
  7151. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7152. }
  7153. static bool
  7154. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7155. {
  7156. BranchBlock *current_csp = ctx->frame_csp - 1;
  7157. uint32 byte_offset = index / sizeof(uint8);
  7158. uint32 bit_offset = index % sizeof(uint8);
  7159. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7160. bh_assert(current_csp->local_use_mask);
  7161. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7162. ? true
  7163. : false;
  7164. }
  7165. #endif /* end of WASM_ENABLE_GC != 0 */
  7166. static void
  7167. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7168. {
  7169. if (ctx) {
  7170. if (ctx->frame_ref_bottom)
  7171. wasm_runtime_free(ctx->frame_ref_bottom);
  7172. #if WASM_ENABLE_GC != 0
  7173. if (ctx->frame_reftype_map_bottom)
  7174. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7175. #endif
  7176. if (ctx->frame_csp_bottom) {
  7177. #if WASM_ENABLE_FAST_INTERP != 0
  7178. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7179. #endif
  7180. #if WASM_ENABLE_GC != 0
  7181. wasm_loader_clean_all_local_use_masks(ctx);
  7182. #endif
  7183. wasm_runtime_free(ctx->frame_csp_bottom);
  7184. }
  7185. #if WASM_ENABLE_FAST_INTERP != 0
  7186. if (ctx->frame_offset_bottom)
  7187. wasm_runtime_free(ctx->frame_offset_bottom);
  7188. if (ctx->const_buf)
  7189. wasm_runtime_free(ctx->const_buf);
  7190. #endif
  7191. wasm_runtime_free(ctx);
  7192. }
  7193. }
  7194. static WASMLoaderContext *
  7195. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7196. {
  7197. WASMLoaderContext *loader_ctx =
  7198. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7199. if (!loader_ctx)
  7200. return NULL;
  7201. loader_ctx->frame_ref_size = 32;
  7202. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7203. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7204. goto fail;
  7205. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7206. #if WASM_ENABLE_GC != 0
  7207. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7208. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7209. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7210. error_buf_size)))
  7211. goto fail;
  7212. loader_ctx->frame_reftype_map_boundary =
  7213. loader_ctx->frame_reftype_map_bottom + 16;
  7214. #endif
  7215. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7216. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7217. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7218. goto fail;
  7219. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7220. #if WASM_ENABLE_EXCE_HANDLING != 0
  7221. func->exception_handler_count = 0;
  7222. #endif
  7223. #if WASM_ENABLE_FAST_INTERP != 0
  7224. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7225. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7226. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7227. error_buf_size)))
  7228. goto fail;
  7229. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7230. loader_ctx->num_const = 0;
  7231. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7232. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7233. error_buf, error_buf_size)))
  7234. goto fail;
  7235. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7236. set_error_buf(error_buf, error_buf_size,
  7237. "fast interpreter offset overflow");
  7238. goto fail;
  7239. }
  7240. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7241. loader_ctx->max_dynamic_offset =
  7242. func->param_cell_num + func->local_cell_num;
  7243. #endif
  7244. return loader_ctx;
  7245. fail:
  7246. wasm_loader_ctx_destroy(loader_ctx);
  7247. return NULL;
  7248. }
  7249. static bool
  7250. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7251. uint32 error_buf_size)
  7252. {
  7253. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7254. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7255. /* Increase the frame ref stack */
  7256. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7257. ctx->frame_ref_size + 16);
  7258. ctx->frame_ref_size += 16;
  7259. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7260. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7261. }
  7262. #if WASM_ENABLE_GC != 0
  7263. if (wasm_is_type_multi_byte_type(type)
  7264. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7265. /* Increase the frame reftype map stack */
  7266. bh_assert(
  7267. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7268. * sizeof(WASMRefTypeMap))
  7269. == ctx->frame_reftype_map_size);
  7270. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7271. ctx->frame_reftype_map_size
  7272. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7273. ctx->frame_reftype_map =
  7274. ctx->frame_reftype_map_bottom
  7275. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7276. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7277. ctx->frame_reftype_map_boundary =
  7278. ctx->frame_reftype_map_bottom
  7279. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7280. }
  7281. #endif
  7282. return true;
  7283. fail:
  7284. return false;
  7285. }
  7286. static bool
  7287. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7288. uint32 error_buf_size)
  7289. {
  7290. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7291. uint32 i;
  7292. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7293. return false;
  7294. #if WASM_ENABLE_GC != 0
  7295. if (wasm_is_type_multi_byte_type(type)) {
  7296. WASMRefType *ref_type;
  7297. if (!(ref_type =
  7298. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7299. error_buf, error_buf_size))) {
  7300. return false;
  7301. }
  7302. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7303. /* Increase the frame reftype map stack */
  7304. bh_assert((uint32)((ctx->frame_reftype_map
  7305. - ctx->frame_reftype_map_bottom)
  7306. * sizeof(WASMRefTypeMap))
  7307. == ctx->frame_reftype_map_size);
  7308. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7309. ctx->frame_reftype_map_size,
  7310. ctx->frame_reftype_map_size
  7311. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7312. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7313. + ctx->frame_reftype_map_size
  7314. / ((uint32)sizeof(WASMRefTypeMap));
  7315. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7316. ctx->frame_reftype_map_boundary =
  7317. ctx->frame_reftype_map_bottom
  7318. + ctx->frame_reftype_map_size
  7319. / ((uint32)sizeof(WASMRefTypeMap));
  7320. }
  7321. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7322. ctx->frame_reftype_map->ref_type = ref_type;
  7323. ctx->frame_reftype_map++;
  7324. ctx->reftype_map_num++;
  7325. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7326. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7327. }
  7328. #endif
  7329. for (i = 0; i < type_cell_num; i++)
  7330. *ctx->frame_ref++ = type;
  7331. ctx->stack_cell_num += type_cell_num;
  7332. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7333. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7334. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7335. set_error_buf(error_buf, error_buf_size,
  7336. "operand stack depth limit exceeded");
  7337. return false;
  7338. }
  7339. }
  7340. return true;
  7341. #if WASM_ENABLE_GC != 0
  7342. fail:
  7343. return false;
  7344. #endif
  7345. }
  7346. static bool
  7347. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7348. int32 stack_cell_num,
  7349. #if WASM_ENABLE_GC != 0
  7350. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7351. #endif
  7352. uint8 type,
  7353. #if WASM_ENABLE_GC != 0
  7354. WASMRefType *ref_type,
  7355. #endif
  7356. char *error_buf, uint32 error_buf_size)
  7357. {
  7358. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7359. #if WASM_ENABLE_GC != 0
  7360. WASMRefType *frame_reftype = NULL;
  7361. #endif
  7362. if (stack_cell_num < type_cell_num) {
  7363. set_error_buf(error_buf, error_buf_size,
  7364. "type mismatch: expect data but stack was empty");
  7365. return false;
  7366. }
  7367. #if WASM_ENABLE_GC == 0
  7368. for (i = 0; i < type_cell_num; i++) {
  7369. if (*(frame_ref - 1 - i) != type) {
  7370. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7371. "type mismatch: expect ", type2str(type),
  7372. " but got other");
  7373. return false;
  7374. }
  7375. }
  7376. #else
  7377. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7378. bh_assert(reftype_map_num > 0);
  7379. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7380. }
  7381. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7382. ref_type, ctx->module->types,
  7383. ctx->module->type_count)) {
  7384. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7385. "type mismatch: expect ", type2str(type),
  7386. " but got other");
  7387. return false;
  7388. }
  7389. for (i = 0; i < type_cell_num - 1; i++) {
  7390. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7391. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7392. "type mismatch: expect ", type2str(type),
  7393. " but got other");
  7394. return false;
  7395. }
  7396. }
  7397. #endif
  7398. return true;
  7399. }
  7400. static bool
  7401. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7402. uint32 error_buf_size)
  7403. {
  7404. int32 block_stack_cell_num =
  7405. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7406. #if WASM_ENABLE_GC != 0
  7407. int32 reftype_map_num =
  7408. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7409. #endif
  7410. if (block_stack_cell_num > 0) {
  7411. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7412. /* the stack top is a value of any type, return success */
  7413. return true;
  7414. }
  7415. #if WASM_ENABLE_GC != 0
  7416. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7417. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7418. WASMRefType *stack_top_ref_type = NULL;
  7419. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7420. bh_assert(reftype_map_num > 0);
  7421. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7422. }
  7423. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7424. ctx->ref_type_tmp, ctx->module->types,
  7425. ctx->module->type_count)) {
  7426. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7427. uint32 ref_type_struct_size =
  7428. wasm_reftype_struct_size(stack_top_ref_type);
  7429. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7430. stack_top_ref_type, ref_type_struct_size);
  7431. }
  7432. return true;
  7433. }
  7434. }
  7435. #endif
  7436. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7437. #if WASM_ENABLE_GC != 0
  7438. ctx->frame_reftype_map, reftype_map_num,
  7439. #endif
  7440. type,
  7441. #if WASM_ENABLE_GC != 0
  7442. ctx->ref_type_tmp,
  7443. #endif
  7444. error_buf, error_buf_size)) {
  7445. return false;
  7446. }
  7447. return true;
  7448. }
  7449. static bool
  7450. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7451. uint32 error_buf_size)
  7452. {
  7453. BranchBlock *cur_block = ctx->frame_csp - 1;
  7454. int32 available_stack_cell =
  7455. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7456. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7457. /* Directly return success if current block is in stack
  7458. polymorphic state while stack is empty. */
  7459. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7460. return true;
  7461. if (type == VALUE_TYPE_VOID)
  7462. return true;
  7463. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7464. return false;
  7465. bh_assert(available_stack_cell > 0);
  7466. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7467. type = VALUE_TYPE_ANY;
  7468. cell_num_to_pop = 1;
  7469. }
  7470. ctx->frame_ref -= cell_num_to_pop;
  7471. ctx->stack_cell_num -= cell_num_to_pop;
  7472. #if WASM_ENABLE_GC != 0
  7473. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7474. ctx->frame_reftype_map--;
  7475. ctx->reftype_map_num--;
  7476. }
  7477. #endif
  7478. return true;
  7479. }
  7480. #if WASM_ENABLE_GC != 0
  7481. /* Get the stack top element of current block */
  7482. static bool
  7483. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7484. WASMRefType **p_ref_type, char *error_buf,
  7485. uint32 error_buf_size)
  7486. {
  7487. BranchBlock *cur_block = ctx->frame_csp - 1;
  7488. int32 available_stack_cell =
  7489. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7490. if (available_stack_cell <= 0) {
  7491. /* Directly return success if current block is in stack
  7492. polymorphic state while stack is empty. */
  7493. if (cur_block->is_stack_polymorphic) {
  7494. *p_type = VALUE_TYPE_ANY;
  7495. return true;
  7496. }
  7497. else {
  7498. set_error_buf(
  7499. error_buf, error_buf_size,
  7500. "type mismatch: expect data but block stack was empty");
  7501. return false;
  7502. }
  7503. }
  7504. *p_type = *(ctx->frame_ref - 1);
  7505. if (wasm_is_type_multi_byte_type(*p_type)) {
  7506. int32 available_reftype_map =
  7507. (int32)(ctx->reftype_map_num
  7508. - (ctx->frame_csp - 1)->reftype_map_num);
  7509. bh_assert(available_reftype_map > 0);
  7510. (void)available_reftype_map;
  7511. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7512. }
  7513. return true;
  7514. }
  7515. #if WASM_ENABLE_FAST_INTERP != 0
  7516. static bool
  7517. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7518. char *error_buf, uint32 error_buf_size);
  7519. #endif
  7520. /* Check whether the stack top elem is a heap object, and if yes,
  7521. pop and return it */
  7522. static bool
  7523. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7524. WASMRefType *ref_ht_ret, char *error_buf,
  7525. uint32 error_buf_size)
  7526. {
  7527. uint8 type = 0;
  7528. WASMRefType *ref_type = NULL;
  7529. /* Get stack top element */
  7530. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7531. error_buf_size)) {
  7532. return false;
  7533. }
  7534. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7535. /* stack top isn't a ref type */
  7536. && !wasm_is_type_reftype(type)) {
  7537. set_error_buf(error_buf, error_buf_size,
  7538. "type mismatch: expect heap object but got others");
  7539. return false;
  7540. }
  7541. /* POP stack top */
  7542. if (wasm_is_type_multi_byte_type(type)) {
  7543. bh_assert(ref_type);
  7544. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7545. wasm_reftype_struct_size(ref_type));
  7546. }
  7547. #if WASM_ENABLE_FAST_INTERP != 0
  7548. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7549. error_buf_size)) {
  7550. return false;
  7551. }
  7552. #else
  7553. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7554. return false;
  7555. }
  7556. #endif
  7557. if (p_type)
  7558. *p_type = type;
  7559. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7560. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7561. wasm_reftype_struct_size(ref_type));
  7562. }
  7563. return true;
  7564. }
  7565. /* Check whether the stack top elem is subtype of (ref null ht),
  7566. and if yes, pop it and return the converted (ref ht) */
  7567. static bool
  7568. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7569. WASMRefType *ref_ht_ret, char *error_buf,
  7570. uint32 error_buf_size)
  7571. {
  7572. uint8 type = 0;
  7573. WASMRefType ref_type = { 0 };
  7574. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7575. error_buf_size)) {
  7576. return false;
  7577. }
  7578. /* Convert to related (ref ht) and return */
  7579. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7580. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7581. */
  7582. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7583. HEAP_TYPE_ARRAY
  7584. + (type - REF_TYPE_ARRAYREF));
  7585. type = ref_ht_ret->ref_type;
  7586. }
  7587. else if (wasm_is_reftype_htref_nullable(type)
  7588. || wasm_is_reftype_htref_non_nullable(type)) {
  7589. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7590. wasm_reftype_struct_size(&ref_type));
  7591. /* Convert to (ref ht) */
  7592. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7593. ref_ht_ret->ref_ht_common.nullable = false;
  7594. type = ref_ht_ret->ref_type;
  7595. }
  7596. *p_type = type;
  7597. return true;
  7598. }
  7599. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7600. and if yes, pop it and return the type_idx */
  7601. static bool
  7602. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7603. uint32 *p_type_idx, char *error_buf,
  7604. uint32 error_buf_size)
  7605. {
  7606. uint8 type = 0;
  7607. int32 type_idx = -1;
  7608. WASMRefType *ref_type = NULL;
  7609. /* Get stack top element */
  7610. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7611. error_buf_size)) {
  7612. return false;
  7613. }
  7614. if (type != VALUE_TYPE_ANY) {
  7615. /* stack top isn't (ref null $t) */
  7616. if (!((wasm_is_reftype_htref_nullable(type)
  7617. || wasm_is_reftype_htref_non_nullable(type))
  7618. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7619. set_error_buf(error_buf, error_buf_size,
  7620. "type mismatch: expect (ref null $t) but got others");
  7621. return false;
  7622. }
  7623. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7624. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7625. wasm_reftype_struct_size(ref_type));
  7626. }
  7627. /* POP stack top */
  7628. #if WASM_ENABLE_FAST_INTERP != 0
  7629. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7630. error_buf_size)) {
  7631. return false;
  7632. }
  7633. #else
  7634. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7635. return false;
  7636. }
  7637. #endif
  7638. /* Convert to type_idx and return */
  7639. *p_type = type;
  7640. if (type != VALUE_TYPE_ANY)
  7641. *p_type_idx = (uint32)type_idx;
  7642. return true;
  7643. }
  7644. #endif /* WASM_ENABLE_GC != 0 */
  7645. #if WASM_ENABLE_FAST_INTERP == 0
  7646. static bool
  7647. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7648. uint8 type_push, uint8 type_pop, char *error_buf,
  7649. uint32 error_buf_size)
  7650. {
  7651. for (int i = 0; i < pop_cnt; i++) {
  7652. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7653. error_buf_size))
  7654. return false;
  7655. }
  7656. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7657. return false;
  7658. return true;
  7659. }
  7660. #endif
  7661. static bool
  7662. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7663. BlockType block_type, uint8 *start_addr,
  7664. char *error_buf, uint32 error_buf_size)
  7665. {
  7666. CHECK_CSP_PUSH();
  7667. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7668. ctx->frame_csp->label_type = label_type;
  7669. ctx->frame_csp->block_type = block_type;
  7670. ctx->frame_csp->start_addr = start_addr;
  7671. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7672. #if WASM_ENABLE_GC != 0
  7673. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7674. #endif
  7675. #if WASM_ENABLE_FAST_INTERP != 0
  7676. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7677. ctx->frame_csp->patch_list = NULL;
  7678. #endif
  7679. ctx->frame_csp++;
  7680. ctx->csp_num++;
  7681. if (ctx->csp_num > ctx->max_csp_num) {
  7682. ctx->max_csp_num = ctx->csp_num;
  7683. if (ctx->max_csp_num > UINT16_MAX) {
  7684. set_error_buf(error_buf, error_buf_size,
  7685. "label stack depth limit exceeded");
  7686. return false;
  7687. }
  7688. }
  7689. return true;
  7690. fail:
  7691. return false;
  7692. }
  7693. static bool
  7694. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7695. uint32 error_buf_size)
  7696. {
  7697. CHECK_CSP_POP();
  7698. #if WASM_ENABLE_FAST_INTERP != 0
  7699. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7700. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7701. #endif
  7702. ctx->frame_csp--;
  7703. ctx->csp_num--;
  7704. return true;
  7705. fail:
  7706. return false;
  7707. }
  7708. #if WASM_ENABLE_FAST_INTERP != 0
  7709. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7710. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7711. #define emit_label(opcode) \
  7712. do { \
  7713. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7714. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7715. } while (0)
  7716. #define skip_label() \
  7717. do { \
  7718. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7719. LOG_OP("\ndelete last op\n"); \
  7720. } while (0)
  7721. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7722. #if UINTPTR_MAX == UINT64_MAX
  7723. #define emit_label(opcode) \
  7724. do { \
  7725. int32 offset = \
  7726. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7727. /* emit int32 relative offset in 64-bit target */ \
  7728. wasm_loader_emit_uint32(loader_ctx, offset); \
  7729. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7730. } while (0)
  7731. #else
  7732. #define emit_label(opcode) \
  7733. do { \
  7734. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7735. /* emit uint32 label address in 32-bit target */ \
  7736. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7737. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7738. } while (0)
  7739. #endif
  7740. #define skip_label() \
  7741. do { \
  7742. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7743. LOG_OP("\ndelete last op\n"); \
  7744. } while (0)
  7745. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7746. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7747. #define emit_label(opcode) \
  7748. do { \
  7749. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7750. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7751. } while (0)
  7752. #define skip_label() \
  7753. do { \
  7754. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7755. LOG_OP("\ndelete last op\n"); \
  7756. } while (0)
  7757. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7758. #define emit_empty_label_addr_and_frame_ip(type) \
  7759. do { \
  7760. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7761. loader_ctx->p_code_compiled, error_buf, \
  7762. error_buf_size)) \
  7763. goto fail; \
  7764. /* label address, to be patched */ \
  7765. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7766. } while (0)
  7767. #define emit_br_info(frame_csp, is_br) \
  7768. do { \
  7769. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7770. error_buf_size)) \
  7771. goto fail; \
  7772. } while (0)
  7773. #define LAST_OP_OUTPUT_I32() \
  7774. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7775. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7776. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7777. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7778. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7779. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7780. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7781. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7782. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7783. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7784. || (last_op == EXT_OP_COPY_STACK_TOP)
  7785. #define LAST_OP_OUTPUT_I64() \
  7786. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7787. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7788. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7789. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7790. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7791. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7792. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7793. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7794. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7795. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7796. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7797. #define GET_CONST_OFFSET(type, val) \
  7798. do { \
  7799. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7800. &operand_offset, error_buf, \
  7801. error_buf_size))) \
  7802. goto fail; \
  7803. } while (0)
  7804. #define GET_CONST_F32_OFFSET(type, fval) \
  7805. do { \
  7806. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7807. &operand_offset, error_buf, \
  7808. error_buf_size))) \
  7809. goto fail; \
  7810. } while (0)
  7811. #define GET_CONST_F64_OFFSET(type, fval) \
  7812. do { \
  7813. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7814. &operand_offset, error_buf, \
  7815. error_buf_size))) \
  7816. goto fail; \
  7817. } while (0)
  7818. #define emit_operand(ctx, offset) \
  7819. do { \
  7820. wasm_loader_emit_int16(ctx, offset); \
  7821. LOG_OP("%d\t", offset); \
  7822. } while (0)
  7823. #define emit_byte(ctx, byte) \
  7824. do { \
  7825. wasm_loader_emit_uint8(ctx, byte); \
  7826. LOG_OP("%d\t", byte); \
  7827. } while (0)
  7828. #define emit_uint32(ctx, value) \
  7829. do { \
  7830. wasm_loader_emit_uint32(ctx, value); \
  7831. LOG_OP("%d\t", value); \
  7832. } while (0)
  7833. #define emit_uint64(ctx, value) \
  7834. do { \
  7835. wasm_loader_emit_const(ctx, &value, false); \
  7836. LOG_OP("%lld\t", value); \
  7837. } while (0)
  7838. #define emit_float32(ctx, value) \
  7839. do { \
  7840. wasm_loader_emit_const(ctx, &value, true); \
  7841. LOG_OP("%f\t", value); \
  7842. } while (0)
  7843. #define emit_float64(ctx, value) \
  7844. do { \
  7845. wasm_loader_emit_const(ctx, &value, false); \
  7846. LOG_OP("%f\t", value); \
  7847. } while (0)
  7848. static bool
  7849. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7850. {
  7851. if (!(ctx->p_code_compiled =
  7852. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7853. return false;
  7854. ctx->p_code_compiled_end =
  7855. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7856. /* clean up frame ref */
  7857. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7858. ctx->frame_ref = ctx->frame_ref_bottom;
  7859. ctx->stack_cell_num = 0;
  7860. #if WASM_ENABLE_GC != 0
  7861. /* clean up reftype map */
  7862. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7863. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7864. ctx->reftype_map_num = 0;
  7865. #endif
  7866. /* clean up frame csp */
  7867. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7868. ctx->frame_csp = ctx->frame_csp_bottom;
  7869. ctx->csp_num = 0;
  7870. ctx->max_csp_num = 0;
  7871. /* clean up frame offset */
  7872. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7873. ctx->frame_offset = ctx->frame_offset_bottom;
  7874. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7875. /* init preserved local offsets */
  7876. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7877. /* const buf is reserved */
  7878. return true;
  7879. }
  7880. static void
  7881. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7882. {
  7883. ctx->code_compiled_size += size;
  7884. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7885. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7886. }
  7887. }
  7888. static void
  7889. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7890. {
  7891. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7892. if (ctx->p_code_compiled) {
  7893. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7894. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7895. #endif
  7896. bh_memcpy_s(ctx->p_code_compiled,
  7897. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7898. value, size);
  7899. ctx->p_code_compiled += size;
  7900. }
  7901. else {
  7902. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7903. bh_assert((ctx->code_compiled_size & 1) == 0);
  7904. #endif
  7905. increase_compiled_code_space(ctx, size);
  7906. }
  7907. }
  7908. static void
  7909. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7910. {
  7911. if (ctx->p_code_compiled) {
  7912. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7913. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7914. #endif
  7915. STORE_U32(ctx->p_code_compiled, value);
  7916. ctx->p_code_compiled += sizeof(uint32);
  7917. }
  7918. else {
  7919. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7920. bh_assert((ctx->code_compiled_size & 1) == 0);
  7921. #endif
  7922. increase_compiled_code_space(ctx, sizeof(uint32));
  7923. }
  7924. }
  7925. static void
  7926. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7927. {
  7928. if (ctx->p_code_compiled) {
  7929. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7930. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7931. #endif
  7932. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7933. ctx->p_code_compiled += sizeof(int16);
  7934. }
  7935. else {
  7936. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7937. bh_assert((ctx->code_compiled_size & 1) == 0);
  7938. #endif
  7939. increase_compiled_code_space(ctx, sizeof(uint16));
  7940. }
  7941. }
  7942. static void
  7943. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7944. {
  7945. if (ctx->p_code_compiled) {
  7946. *(ctx->p_code_compiled) = value;
  7947. ctx->p_code_compiled += sizeof(uint8);
  7948. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7949. ctx->p_code_compiled++;
  7950. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7951. #endif
  7952. }
  7953. else {
  7954. increase_compiled_code_space(ctx, sizeof(uint8));
  7955. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7956. increase_compiled_code_space(ctx, sizeof(uint8));
  7957. bh_assert((ctx->code_compiled_size & 1) == 0);
  7958. #endif
  7959. }
  7960. }
  7961. static void
  7962. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  7963. {
  7964. if (ctx->p_code_compiled) {
  7965. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7966. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7967. #endif
  7968. STORE_PTR(ctx->p_code_compiled, value);
  7969. ctx->p_code_compiled += sizeof(void *);
  7970. }
  7971. else {
  7972. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7973. bh_assert((ctx->code_compiled_size & 1) == 0);
  7974. #endif
  7975. increase_compiled_code_space(ctx, sizeof(void *));
  7976. }
  7977. }
  7978. static void
  7979. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  7980. {
  7981. if (ctx->p_code_compiled) {
  7982. ctx->p_code_compiled -= size;
  7983. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7984. if (size == sizeof(uint8)) {
  7985. ctx->p_code_compiled--;
  7986. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7987. }
  7988. #endif
  7989. }
  7990. else {
  7991. ctx->code_compiled_size -= size;
  7992. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7993. if (size == sizeof(uint8)) {
  7994. ctx->code_compiled_size--;
  7995. bh_assert((ctx->code_compiled_size & 1) == 0);
  7996. }
  7997. #endif
  7998. }
  7999. }
  8000. static bool
  8001. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8002. uint32 local_index, uint32 local_type,
  8003. bool *preserved, char *error_buf,
  8004. uint32 error_buf_size)
  8005. {
  8006. uint32 i = 0;
  8007. int16 preserved_offset = (int16)local_index;
  8008. *preserved = false;
  8009. while (i < loader_ctx->stack_cell_num) {
  8010. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8011. /* move previous local into dynamic space before a set/tee_local opcode
  8012. */
  8013. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8014. if (!(*preserved)) {
  8015. *preserved = true;
  8016. skip_label();
  8017. preserved_offset = loader_ctx->preserved_local_offset;
  8018. if (loader_ctx->p_code_compiled) {
  8019. bh_assert(preserved_offset != (int16)local_index);
  8020. }
  8021. if (is_32bit_type(local_type)) {
  8022. /* Only increase preserve offset in the second traversal */
  8023. if (loader_ctx->p_code_compiled)
  8024. loader_ctx->preserved_local_offset++;
  8025. emit_label(EXT_OP_COPY_STACK_TOP);
  8026. }
  8027. else {
  8028. if (loader_ctx->p_code_compiled)
  8029. loader_ctx->preserved_local_offset += 2;
  8030. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8031. }
  8032. emit_operand(loader_ctx, local_index);
  8033. emit_operand(loader_ctx, preserved_offset);
  8034. emit_label(opcode);
  8035. }
  8036. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8037. }
  8038. if (is_32bit_type(cur_type))
  8039. i++;
  8040. else
  8041. i += 2;
  8042. }
  8043. (void)error_buf;
  8044. (void)error_buf_size;
  8045. return true;
  8046. }
  8047. static bool
  8048. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8049. char *error_buf, uint32 error_buf_size)
  8050. {
  8051. uint32 i = 0;
  8052. bool preserve_local;
  8053. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8054. blocks will not influence the value of these locals */
  8055. while (i < loader_ctx->stack_cell_num) {
  8056. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8057. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8058. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8059. && (cur_offset >= 0)) {
  8060. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8061. cur_type, &preserve_local,
  8062. error_buf, error_buf_size)))
  8063. return false;
  8064. }
  8065. if (is_32bit_type(cur_type)) {
  8066. i++;
  8067. }
  8068. else {
  8069. i += 2;
  8070. }
  8071. }
  8072. return true;
  8073. }
  8074. static bool
  8075. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8076. uint8 *p_code_compiled, char *error_buf,
  8077. uint32 error_buf_size)
  8078. {
  8079. BranchBlockPatch *patch =
  8080. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8081. if (!patch) {
  8082. return false;
  8083. }
  8084. patch->patch_type = patch_type;
  8085. patch->code_compiled = p_code_compiled;
  8086. if (!frame_csp->patch_list) {
  8087. frame_csp->patch_list = patch;
  8088. patch->next = NULL;
  8089. }
  8090. else {
  8091. patch->next = frame_csp->patch_list;
  8092. frame_csp->patch_list = patch;
  8093. }
  8094. return true;
  8095. }
  8096. static void
  8097. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8098. {
  8099. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8100. BranchBlockPatch *node = frame_csp->patch_list;
  8101. BranchBlockPatch *node_prev = NULL, *node_next;
  8102. if (!ctx->p_code_compiled)
  8103. return;
  8104. while (node) {
  8105. node_next = node->next;
  8106. if (node->patch_type == patch_type) {
  8107. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8108. if (node_prev == NULL) {
  8109. frame_csp->patch_list = node_next;
  8110. }
  8111. else {
  8112. node_prev->next = node_next;
  8113. }
  8114. wasm_runtime_free(node);
  8115. }
  8116. else {
  8117. node_prev = node;
  8118. }
  8119. node = node_next;
  8120. }
  8121. }
  8122. static bool
  8123. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8124. bool is_br, char *error_buf, uint32 error_buf_size)
  8125. {
  8126. /* br info layout:
  8127. * a) arity of target block
  8128. * b) total cell num of arity values
  8129. * c) each arity value's cell num
  8130. * d) each arity value's src frame offset
  8131. * e) each arity values's dst dynamic offset
  8132. * f) branch target address
  8133. *
  8134. * Note: b-e are omitted when arity is 0 so that
  8135. * interpreter can recover the br info quickly.
  8136. */
  8137. BlockType *block_type = &frame_csp->block_type;
  8138. uint8 *types = NULL, cell;
  8139. #if WASM_ENABLE_GC != 0
  8140. WASMRefTypeMap *reftype_maps;
  8141. uint32 reftype_map_count;
  8142. #endif
  8143. uint32 arity = 0;
  8144. int32 i;
  8145. int16 *frame_offset = ctx->frame_offset;
  8146. uint16 dynamic_offset;
  8147. /* Note: loop's arity is different from if and block. loop's arity is
  8148. * its parameter count while if and block arity is result count.
  8149. */
  8150. #if WASM_ENABLE_GC == 0
  8151. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8152. arity = block_type_get_param_types(block_type, &types);
  8153. else
  8154. arity = block_type_get_result_types(block_type, &types);
  8155. #else
  8156. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8157. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8158. &reftype_map_count);
  8159. else
  8160. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8161. &reftype_map_count);
  8162. #endif
  8163. /* Part a */
  8164. emit_uint32(ctx, arity);
  8165. if (arity) {
  8166. /* Part b */
  8167. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8168. /* Part c */
  8169. for (i = (int32)arity - 1; i >= 0; i--) {
  8170. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8171. emit_byte(ctx, cell);
  8172. }
  8173. /* Part d */
  8174. for (i = (int32)arity - 1; i >= 0; i--) {
  8175. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8176. frame_offset -= cell;
  8177. emit_operand(ctx, *(int16 *)(frame_offset));
  8178. }
  8179. /* Part e */
  8180. dynamic_offset =
  8181. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8182. if (is_br)
  8183. ctx->dynamic_offset = dynamic_offset;
  8184. for (i = (int32)arity - 1; i >= 0; i--) {
  8185. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8186. dynamic_offset -= cell;
  8187. emit_operand(ctx, dynamic_offset);
  8188. }
  8189. }
  8190. /* Part f */
  8191. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8192. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8193. }
  8194. else {
  8195. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8196. error_buf, error_buf_size))
  8197. return false;
  8198. /* label address, to be patched */
  8199. wasm_loader_emit_ptr(ctx, NULL);
  8200. }
  8201. return true;
  8202. }
  8203. static bool
  8204. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8205. bool disable_emit, int16 operand_offset,
  8206. char *error_buf, uint32 error_buf_size)
  8207. {
  8208. if (type == VALUE_TYPE_VOID)
  8209. return true;
  8210. /* only check memory overflow in first traverse */
  8211. if (ctx->p_code_compiled == NULL) {
  8212. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8213. return false;
  8214. }
  8215. if (disable_emit)
  8216. *(ctx->frame_offset)++ = operand_offset;
  8217. else {
  8218. emit_operand(ctx, ctx->dynamic_offset);
  8219. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8220. ctx->dynamic_offset++;
  8221. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8222. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8223. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8224. goto fail;
  8225. }
  8226. }
  8227. }
  8228. if (is_32bit_type(type))
  8229. return true;
  8230. if (ctx->p_code_compiled == NULL) {
  8231. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8232. return false;
  8233. }
  8234. ctx->frame_offset++;
  8235. if (!disable_emit) {
  8236. ctx->dynamic_offset++;
  8237. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8238. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8239. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8240. goto fail;
  8241. }
  8242. }
  8243. }
  8244. return true;
  8245. fail:
  8246. set_error_buf(error_buf, error_buf_size,
  8247. "fast interpreter offset overflow");
  8248. return false;
  8249. }
  8250. /* This function should be in front of wasm_loader_pop_frame_ref
  8251. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8252. will be modified by wasm_loader_pop_frame_ref */
  8253. static bool
  8254. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8255. char *error_buf, uint32 error_buf_size)
  8256. {
  8257. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8258. then current block is the function block */
  8259. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8260. BranchBlock *cur_block = ctx->frame_csp - depth;
  8261. int32 available_stack_cell =
  8262. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8263. /* Directly return success if current block is in stack
  8264. * polymorphic state while stack is empty. */
  8265. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8266. return true;
  8267. if (type == VALUE_TYPE_VOID)
  8268. return true;
  8269. if (is_32bit_type(type)) {
  8270. /* Check the offset stack bottom to ensure the frame offset
  8271. stack will not go underflow. But we don't thrown error
  8272. and return true here, because the error msg should be
  8273. given in wasm_loader_pop_frame_ref */
  8274. if (!check_offset_pop(ctx, 1))
  8275. return true;
  8276. ctx->frame_offset -= 1;
  8277. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8278. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8279. ctx->dynamic_offset -= 1;
  8280. }
  8281. else {
  8282. if (!check_offset_pop(ctx, 2))
  8283. return true;
  8284. ctx->frame_offset -= 2;
  8285. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8286. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8287. ctx->dynamic_offset -= 2;
  8288. }
  8289. emit_operand(ctx, *(ctx->frame_offset));
  8290. (void)error_buf;
  8291. (void)error_buf_size;
  8292. return true;
  8293. }
  8294. static bool
  8295. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8296. bool disable_emit, int16 operand_offset,
  8297. char *error_buf, uint32 error_buf_size)
  8298. {
  8299. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8300. error_buf, error_buf_size)))
  8301. return false;
  8302. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8303. return false;
  8304. return true;
  8305. }
  8306. static bool
  8307. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8308. char *error_buf, uint32 error_buf_size)
  8309. {
  8310. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8311. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8312. return false;
  8313. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8314. return false;
  8315. return true;
  8316. }
  8317. static bool
  8318. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8319. uint8 type_push, uint8 type_pop,
  8320. bool disable_emit, int16 operand_offset,
  8321. char *error_buf, uint32 error_buf_size)
  8322. {
  8323. uint8 i;
  8324. for (i = 0; i < pop_cnt; i++) {
  8325. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8326. error_buf_size))
  8327. return false;
  8328. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8329. error_buf_size))
  8330. return false;
  8331. }
  8332. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8333. operand_offset, error_buf,
  8334. error_buf_size))
  8335. return false;
  8336. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8337. return false;
  8338. return true;
  8339. }
  8340. static bool
  8341. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8342. int16 *offset, char *error_buf,
  8343. uint32 error_buf_size)
  8344. {
  8345. int8 bytes_to_increase;
  8346. int16 operand_offset = 0;
  8347. Const *c;
  8348. /* Search existing constant */
  8349. for (c = (Const *)ctx->const_buf;
  8350. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8351. /* TODO: handle v128 type? */
  8352. if ((type == c->value_type)
  8353. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8354. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8355. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8356. || (type == VALUE_TYPE_FUNCREF
  8357. && *(int32 *)value == c->value.i32)
  8358. || (type == VALUE_TYPE_EXTERNREF
  8359. && *(int32 *)value == c->value.i32)
  8360. #endif
  8361. || (type == VALUE_TYPE_F64
  8362. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8363. || (type == VALUE_TYPE_F32
  8364. && (0
  8365. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8366. operand_offset = c->slot_index;
  8367. break;
  8368. }
  8369. if (is_32bit_type(c->value_type))
  8370. operand_offset += 1;
  8371. else
  8372. operand_offset += 2;
  8373. }
  8374. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8375. /* New constant, append to the const buffer */
  8376. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8377. bytes_to_increase = 2;
  8378. }
  8379. else {
  8380. bytes_to_increase = 1;
  8381. }
  8382. /* The max cell num of const buffer is 32768 since the valid index range
  8383. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8384. * full */
  8385. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8386. *offset = 0;
  8387. return true;
  8388. }
  8389. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8390. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8391. ctx->const_buf_size + 4 * sizeof(Const));
  8392. ctx->const_buf_size += 4 * sizeof(Const);
  8393. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8394. }
  8395. c->value_type = type;
  8396. switch (type) {
  8397. case VALUE_TYPE_F64:
  8398. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8399. sizeof(float64));
  8400. ctx->const_cell_num += 2;
  8401. /* The const buf will be reversed, we use the second cell */
  8402. /* of the i64/f64 const so the finnal offset is corrent */
  8403. operand_offset++;
  8404. break;
  8405. case VALUE_TYPE_I64:
  8406. c->value.i64 = *(int64 *)value;
  8407. ctx->const_cell_num += 2;
  8408. operand_offset++;
  8409. break;
  8410. case VALUE_TYPE_F32:
  8411. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8412. sizeof(float32));
  8413. ctx->const_cell_num++;
  8414. break;
  8415. case VALUE_TYPE_I32:
  8416. c->value.i32 = *(int32 *)value;
  8417. ctx->const_cell_num++;
  8418. break;
  8419. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8420. case VALUE_TYPE_EXTERNREF:
  8421. case VALUE_TYPE_FUNCREF:
  8422. c->value.i32 = *(int32 *)value;
  8423. ctx->const_cell_num++;
  8424. break;
  8425. #endif
  8426. default:
  8427. break;
  8428. }
  8429. c->slot_index = operand_offset;
  8430. ctx->num_const++;
  8431. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8432. (int64)c->value.i64);
  8433. }
  8434. /* use negetive index for const */
  8435. operand_offset = -(operand_offset + 1);
  8436. *offset = operand_offset;
  8437. return true;
  8438. fail:
  8439. return false;
  8440. }
  8441. /*
  8442. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8443. -- Mostly used for the binary / compare operation
  8444. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8445. -- Mostly used in block / control instructions
  8446. The POP will always emit the offset on the top of the frame_offset stack
  8447. PUSH can be used in two ways:
  8448. 1. directly PUSH:
  8449. PUSH_XXX();
  8450. will allocate a dynamic space and emit
  8451. 2. silent PUSH:
  8452. operand_offset = xxx; disable_emit = true;
  8453. PUSH_XXX();
  8454. only push the frame_offset stack, no emit
  8455. */
  8456. #define TEMPLATE_PUSH(Type) \
  8457. do { \
  8458. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8459. disable_emit, operand_offset, \
  8460. error_buf, error_buf_size)) \
  8461. goto fail; \
  8462. } while (0)
  8463. #define TEMPLATE_PUSH_REF(Type) \
  8464. do { \
  8465. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8466. operand_offset, error_buf, \
  8467. error_buf_size)) \
  8468. goto fail; \
  8469. } while (0)
  8470. #define TEMPLATE_POP(Type) \
  8471. do { \
  8472. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8473. error_buf, error_buf_size)) \
  8474. goto fail; \
  8475. } while (0)
  8476. #define TEMPLATE_POP_REF(Type) \
  8477. do { \
  8478. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8479. error_buf_size)) \
  8480. goto fail; \
  8481. } while (0)
  8482. #define PUSH_OFFSET_TYPE(type) \
  8483. do { \
  8484. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8485. operand_offset, error_buf, \
  8486. error_buf_size))) \
  8487. goto fail; \
  8488. } while (0)
  8489. #define POP_OFFSET_TYPE(type) \
  8490. do { \
  8491. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8492. error_buf_size))) \
  8493. goto fail; \
  8494. } while (0)
  8495. #define POP_AND_PUSH(type_pop, type_push) \
  8496. do { \
  8497. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8498. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8499. operand_offset, error_buf, error_buf_size))) \
  8500. goto fail; \
  8501. } while (0)
  8502. /* type of POPs should be the same */
  8503. #define POP2_AND_PUSH(type_pop, type_push) \
  8504. do { \
  8505. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8506. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8507. operand_offset, error_buf, error_buf_size))) \
  8508. goto fail; \
  8509. } while (0)
  8510. #else /* WASM_ENABLE_FAST_INTERP */
  8511. #define TEMPLATE_PUSH(Type) \
  8512. do { \
  8513. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8514. error_buf, error_buf_size))) \
  8515. goto fail; \
  8516. } while (0)
  8517. #define TEMPLATE_PUSH_REF(Type) \
  8518. do { \
  8519. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8520. error_buf_size))) \
  8521. goto fail; \
  8522. } while (0)
  8523. #define TEMPLATE_POP(Type) \
  8524. do { \
  8525. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8526. error_buf, error_buf_size))) \
  8527. goto fail; \
  8528. } while (0)
  8529. #define TEMPLATE_POP_REF(Type) \
  8530. do { \
  8531. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8532. error_buf_size))) \
  8533. goto fail; \
  8534. } while (0)
  8535. #define POP_AND_PUSH(type_pop, type_push) \
  8536. do { \
  8537. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8538. type_pop, error_buf, \
  8539. error_buf_size))) \
  8540. goto fail; \
  8541. } while (0)
  8542. /* type of POPs should be the same */
  8543. #define POP2_AND_PUSH(type_pop, type_push) \
  8544. do { \
  8545. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8546. type_pop, error_buf, \
  8547. error_buf_size))) \
  8548. goto fail; \
  8549. } while (0)
  8550. #endif /* WASM_ENABLE_FAST_INTERP */
  8551. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8552. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8553. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8554. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8555. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8556. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8557. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8558. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8559. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8560. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8561. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8562. #define POP_I32() TEMPLATE_POP(I32)
  8563. #define POP_F32() TEMPLATE_POP(F32)
  8564. #define POP_I64() TEMPLATE_POP(I64)
  8565. #define POP_F64() TEMPLATE_POP(F64)
  8566. #define POP_V128() TEMPLATE_POP(V128)
  8567. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8568. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8569. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8570. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8571. #if WASM_ENABLE_FAST_INTERP != 0
  8572. static bool
  8573. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8574. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8575. {
  8576. int16 operand_offset = 0;
  8577. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8578. : loader_ctx->frame_csp;
  8579. BlockType *block_type = &block->block_type;
  8580. uint8 *return_types = NULL;
  8581. #if WASM_ENABLE_GC != 0
  8582. WASMRefTypeMap *reftype_maps = NULL;
  8583. uint32 reftype_map_count;
  8584. #endif
  8585. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8586. int32 i = 0;
  8587. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8588. *frame_offset_org = NULL;
  8589. #if WASM_ENABLE_GC == 0
  8590. return_count = block_type_get_result_types(block_type, &return_types);
  8591. #else
  8592. return_count = block_type_get_result_types(
  8593. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8594. #endif
  8595. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8596. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8597. if (return_count == 1) {
  8598. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8599. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8600. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8601. /* insert op_copy before else opcode */
  8602. if (opcode == WASM_OP_ELSE)
  8603. skip_label();
  8604. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8605. : EXT_OP_COPY_STACK_TOP_I64);
  8606. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8607. emit_operand(loader_ctx, block->dynamic_offset);
  8608. if (opcode == WASM_OP_ELSE) {
  8609. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8610. }
  8611. else {
  8612. loader_ctx->frame_offset -= cell;
  8613. loader_ctx->dynamic_offset = block->dynamic_offset;
  8614. PUSH_OFFSET_TYPE(return_types[0]);
  8615. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8616. }
  8617. if (opcode == WASM_OP_ELSE)
  8618. emit_label(opcode);
  8619. }
  8620. return true;
  8621. }
  8622. /* Copy stack top values to block's results which are in dynamic space.
  8623. * The instruction format:
  8624. * Part a: values count
  8625. * Part b: all values total cell num
  8626. * Part c: each value's cell_num, src offset and dst offset
  8627. * Part d: each value's src offset and dst offset
  8628. * Part e: each value's dst offset
  8629. */
  8630. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8631. dynamic_offset = dynamic_offset_org =
  8632. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8633. /* First traversal to get the count of values needed to be copied. */
  8634. for (i = (int32)return_count - 1; i >= 0; i--) {
  8635. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8636. frame_offset -= cells;
  8637. dynamic_offset -= cells;
  8638. if (dynamic_offset != *frame_offset) {
  8639. value_count++;
  8640. total_cel_num += cells;
  8641. }
  8642. }
  8643. if (value_count) {
  8644. uint32 j = 0;
  8645. uint8 *emit_data = NULL, *cells = NULL;
  8646. int16 *src_offsets = NULL;
  8647. uint16 *dst_offsets = NULL;
  8648. uint64 size =
  8649. (uint64)value_count
  8650. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8651. /* Allocate memory for the emit data */
  8652. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8653. return false;
  8654. cells = emit_data;
  8655. src_offsets = (int16 *)(cells + value_count);
  8656. dst_offsets = (uint16 *)(src_offsets + value_count);
  8657. /* insert op_copy before else opcode */
  8658. if (opcode == WASM_OP_ELSE)
  8659. skip_label();
  8660. emit_label(EXT_OP_COPY_STACK_VALUES);
  8661. /* Part a) */
  8662. emit_uint32(loader_ctx, value_count);
  8663. /* Part b) */
  8664. emit_uint32(loader_ctx, total_cel_num);
  8665. /* Second traversal to get each value's cell num, src offset and dst
  8666. * offset. */
  8667. frame_offset = frame_offset_org;
  8668. dynamic_offset = dynamic_offset_org;
  8669. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8670. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8671. frame_offset -= cell;
  8672. dynamic_offset -= cell;
  8673. if (dynamic_offset != *frame_offset) {
  8674. /* cell num */
  8675. cells[j] = cell;
  8676. /* src offset */
  8677. src_offsets[j] = *frame_offset;
  8678. /* dst offset */
  8679. dst_offsets[j] = dynamic_offset;
  8680. j++;
  8681. }
  8682. if (opcode == WASM_OP_ELSE) {
  8683. *frame_offset = dynamic_offset;
  8684. }
  8685. else {
  8686. loader_ctx->frame_offset = frame_offset;
  8687. loader_ctx->dynamic_offset = dynamic_offset;
  8688. PUSH_OFFSET_TYPE(return_types[i]);
  8689. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8690. loader_ctx->frame_offset = frame_offset_org;
  8691. loader_ctx->dynamic_offset = dynamic_offset_org;
  8692. }
  8693. }
  8694. bh_assert(j == value_count);
  8695. /* Emit the cells, src_offsets and dst_offsets */
  8696. for (j = 0; j < value_count; j++)
  8697. emit_byte(loader_ctx, cells[j]);
  8698. for (j = 0; j < value_count; j++)
  8699. emit_operand(loader_ctx, src_offsets[j]);
  8700. for (j = 0; j < value_count; j++)
  8701. emit_operand(loader_ctx, dst_offsets[j]);
  8702. if (opcode == WASM_OP_ELSE)
  8703. emit_label(opcode);
  8704. wasm_runtime_free(emit_data);
  8705. }
  8706. return true;
  8707. fail:
  8708. return false;
  8709. }
  8710. #endif /* WASM_ENABLE_FAST_INTERP */
  8711. #define RESERVE_BLOCK_RET() \
  8712. do { \
  8713. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8714. error_buf_size)) \
  8715. goto fail; \
  8716. } while (0)
  8717. #define PUSH_TYPE(type) \
  8718. do { \
  8719. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8720. error_buf_size))) \
  8721. goto fail; \
  8722. } while (0)
  8723. #define POP_TYPE(type) \
  8724. do { \
  8725. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8726. error_buf_size))) \
  8727. goto fail; \
  8728. } while (0)
  8729. #if WASM_ENABLE_GC == 0
  8730. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8731. do { \
  8732. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8733. _start_addr, error_buf, \
  8734. error_buf_size)) \
  8735. goto fail; \
  8736. } while (0)
  8737. #define POP_CSP() \
  8738. do { \
  8739. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8740. goto fail; \
  8741. } while (0)
  8742. #else
  8743. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8744. do { \
  8745. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8746. _start_addr, error_buf, \
  8747. error_buf_size)) \
  8748. goto fail; \
  8749. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8750. error_buf, error_buf_size)) { \
  8751. goto fail; \
  8752. } \
  8753. } while (0)
  8754. #define POP_CSP() \
  8755. do { \
  8756. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8757. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8758. goto fail; \
  8759. } while (0)
  8760. #endif /* end of WASM_ENABLE_GC == 0 */
  8761. #if WASM_ENABLE_GC == 0
  8762. #define GET_LOCAL_REFTYPE() (void)0
  8763. #else
  8764. #define GET_LOCAL_REFTYPE() \
  8765. do { \
  8766. if (wasm_is_type_multi_byte_type(local_type)) { \
  8767. WASMRefType *_ref_type; \
  8768. if (local_idx < param_count) \
  8769. _ref_type = wasm_reftype_map_find( \
  8770. param_reftype_maps, param_reftype_map_count, local_idx); \
  8771. else \
  8772. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8773. local_reftype_map_count, \
  8774. local_idx - param_count); \
  8775. bh_assert(_ref_type); \
  8776. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8777. wasm_reftype_struct_size(_ref_type)); \
  8778. } \
  8779. } while (0)
  8780. #endif /* end of WASM_ENABLE_GC == 0 */
  8781. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8782. do { \
  8783. read_leb_uint32(p, p_end, local_idx); \
  8784. if (local_idx >= param_count + local_count) { \
  8785. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8786. goto fail; \
  8787. } \
  8788. local_type = local_idx < param_count \
  8789. ? param_types[local_idx] \
  8790. : local_types[local_idx - param_count]; \
  8791. local_offset = local_offsets[local_idx]; \
  8792. GET_LOCAL_REFTYPE(); \
  8793. } while (0)
  8794. static bool
  8795. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8796. {
  8797. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8798. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8799. return false;
  8800. }
  8801. return true;
  8802. }
  8803. #define CHECK_MEMORY() \
  8804. do { \
  8805. if (!check_memory(module, error_buf, error_buf_size)) \
  8806. goto fail; \
  8807. } while (0)
  8808. static bool
  8809. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8810. uint32 error_buf_size)
  8811. {
  8812. uint8 mem_access_aligns[] = {
  8813. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8814. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8815. };
  8816. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8817. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8818. set_error_buf(error_buf, error_buf_size,
  8819. "alignment must not be larger than natural");
  8820. return false;
  8821. }
  8822. return true;
  8823. }
  8824. #if WASM_ENABLE_SIMD != 0
  8825. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8826. static bool
  8827. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8828. uint32 error_buf_size)
  8829. {
  8830. uint8 mem_access_aligns[] = {
  8831. 4, /* load */
  8832. 3, 3, 3, 3, 3, 3, /* load and extend */
  8833. 0, 1, 2, 3, /* load and splat */
  8834. 4, /* store */
  8835. };
  8836. uint8 mem_access_aligns_load_lane[] = {
  8837. 0, 1, 2, 3, /* load lane */
  8838. 0, 1, 2, 3, /* store lane */
  8839. 2, 3 /* store zero */
  8840. };
  8841. if (!((opcode <= SIMD_v128_store)
  8842. || (SIMD_v128_load8_lane <= opcode
  8843. && opcode <= SIMD_v128_load64_zero))) {
  8844. set_error_buf(error_buf, error_buf_size,
  8845. "the opcode doesn't include memarg");
  8846. return false;
  8847. }
  8848. if ((opcode <= SIMD_v128_store
  8849. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8850. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8851. && align > mem_access_aligns_load_lane[opcode
  8852. - SIMD_v128_load8_lane])) {
  8853. set_error_buf(error_buf, error_buf_size,
  8854. "alignment must not be larger than natural");
  8855. return false;
  8856. }
  8857. return true;
  8858. }
  8859. static bool
  8860. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8861. uint32 error_buf_size)
  8862. {
  8863. switch (opcode) {
  8864. case SIMD_i8x16_extract_lane_s:
  8865. case SIMD_i8x16_extract_lane_u:
  8866. case SIMD_i8x16_replace_lane:
  8867. if (lane >= 16) {
  8868. goto fail;
  8869. }
  8870. break;
  8871. case SIMD_i16x8_extract_lane_s:
  8872. case SIMD_i16x8_extract_lane_u:
  8873. case SIMD_i16x8_replace_lane:
  8874. if (lane >= 8) {
  8875. goto fail;
  8876. }
  8877. break;
  8878. case SIMD_i32x4_extract_lane:
  8879. case SIMD_i32x4_replace_lane:
  8880. case SIMD_f32x4_extract_lane:
  8881. case SIMD_f32x4_replace_lane:
  8882. if (lane >= 4) {
  8883. goto fail;
  8884. }
  8885. break;
  8886. case SIMD_i64x2_extract_lane:
  8887. case SIMD_i64x2_replace_lane:
  8888. case SIMD_f64x2_extract_lane:
  8889. case SIMD_f64x2_replace_lane:
  8890. if (lane >= 2) {
  8891. goto fail;
  8892. }
  8893. break;
  8894. case SIMD_v128_load8_lane:
  8895. case SIMD_v128_load16_lane:
  8896. case SIMD_v128_load32_lane:
  8897. case SIMD_v128_load64_lane:
  8898. case SIMD_v128_store8_lane:
  8899. case SIMD_v128_store16_lane:
  8900. case SIMD_v128_store32_lane:
  8901. case SIMD_v128_store64_lane:
  8902. case SIMD_v128_load32_zero:
  8903. case SIMD_v128_load64_zero:
  8904. {
  8905. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8906. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8907. goto fail;
  8908. }
  8909. break;
  8910. }
  8911. default:
  8912. goto fail;
  8913. }
  8914. return true;
  8915. fail:
  8916. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8917. return false;
  8918. }
  8919. static bool
  8920. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8921. {
  8922. uint8 i;
  8923. for (i = 0; i != 16; ++i) {
  8924. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8925. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8926. return false;
  8927. }
  8928. }
  8929. return true;
  8930. }
  8931. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8932. #endif /* end of WASM_ENABLE_SIMD */
  8933. #if WASM_ENABLE_SHARED_MEMORY != 0
  8934. static bool
  8935. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8936. uint32 error_buf_size)
  8937. {
  8938. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8939. uint8 mem_access_aligns[] = {
  8940. 2, 3, 0, 1, 0, 1, 2,
  8941. };
  8942. uint8 expect;
  8943. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8944. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8945. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8946. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8947. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8948. }
  8949. else {
  8950. /* 7 opcodes in every group */
  8951. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8952. }
  8953. if (align != expect) {
  8954. set_error_buf(error_buf, error_buf_size,
  8955. "alignment isn't equal to natural");
  8956. return false;
  8957. }
  8958. return true;
  8959. }
  8960. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8961. static bool
  8962. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  8963. char *error_buf, uint32 error_buf_size)
  8964. {
  8965. BranchBlock *target_block, *cur_block;
  8966. BlockType *target_block_type;
  8967. uint8 type, *types = NULL, *frame_ref;
  8968. uint32 arity = 0;
  8969. int32 i, available_stack_cell;
  8970. uint16 cell_num;
  8971. #if WASM_ENABLE_GC != 0
  8972. WASMRefTypeMap *frame_reftype_map;
  8973. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  8974. WASMRefType *ref_type;
  8975. uint32 reftype_map_count = 0;
  8976. int32 available_reftype_map;
  8977. bool is_type_multi_byte;
  8978. #endif
  8979. uint8 *frame_ref_old = loader_ctx->frame_ref;
  8980. uint8 *frame_ref_after_popped = NULL;
  8981. uint8 frame_ref_tmp[4] = { 0 };
  8982. uint8 *frame_ref_buf = frame_ref_tmp;
  8983. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  8984. #if WASM_ENABLE_GC != 0
  8985. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  8986. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  8987. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  8988. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  8989. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  8990. #endif
  8991. #if WASM_ENABLE_FAST_INTERP != 0
  8992. int16 *frame_offset_old = loader_ctx->frame_offset;
  8993. int16 *frame_offset_after_popped = NULL;
  8994. int16 frame_offset_tmp[4] = { 0 };
  8995. int16 *frame_offset_buf = frame_offset_tmp;
  8996. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  8997. #endif
  8998. bool ret = false;
  8999. bh_assert(loader_ctx->csp_num > 0);
  9000. if (loader_ctx->csp_num - 1 < depth) {
  9001. set_error_buf(error_buf, error_buf_size,
  9002. "unknown label, "
  9003. "unexpected end of section or function");
  9004. return false;
  9005. }
  9006. cur_block = loader_ctx->frame_csp - 1;
  9007. target_block = loader_ctx->frame_csp - (depth + 1);
  9008. target_block_type = &target_block->block_type;
  9009. frame_ref = loader_ctx->frame_ref;
  9010. #if WASM_ENABLE_GC != 0
  9011. frame_reftype_map = loader_ctx->frame_reftype_map;
  9012. #endif
  9013. /* Note: loop's arity is different from if and block. loop's arity is
  9014. * its parameter count while if and block arity is result count.
  9015. */
  9016. #if WASM_ENABLE_GC == 0
  9017. if (target_block->label_type == LABEL_TYPE_LOOP)
  9018. arity = block_type_get_param_types(target_block_type, &types);
  9019. else
  9020. arity = block_type_get_result_types(target_block_type, &types);
  9021. #else
  9022. if (target_block->label_type == LABEL_TYPE_LOOP)
  9023. arity = block_type_get_param_types(target_block_type, &types,
  9024. &reftype_maps, &reftype_map_count);
  9025. else
  9026. arity = block_type_get_result_types(target_block_type, &types,
  9027. &reftype_maps, &reftype_map_count);
  9028. #endif
  9029. /* If the stack is in polymorphic state, just clear the stack
  9030. * and then re-push the values to make the stack top values
  9031. * match block type. */
  9032. if (cur_block->is_stack_polymorphic) {
  9033. #if WASM_ENABLE_GC != 0
  9034. int32 j = reftype_map_count - 1;
  9035. #endif
  9036. for (i = (int32)arity - 1; i >= 0; i--) {
  9037. #if WASM_ENABLE_GC != 0
  9038. if (wasm_is_type_multi_byte_type(types[i])) {
  9039. bh_assert(reftype_maps[j].index == i);
  9040. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9041. reftype_maps[j].ref_type,
  9042. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9043. j--;
  9044. }
  9045. #endif
  9046. #if WASM_ENABLE_FAST_INTERP != 0
  9047. POP_OFFSET_TYPE(types[i]);
  9048. #endif
  9049. POP_TYPE(types[i]);
  9050. }
  9051. /* Backup stack data since it may be changed in the below
  9052. push operations, and the stack data may be used when
  9053. checking other target blocks of opcode br_table */
  9054. if (opcode == WASM_OP_BR_TABLE) {
  9055. uint64 total_size;
  9056. frame_ref_after_popped = loader_ctx->frame_ref;
  9057. total_size = (uint64)sizeof(uint8)
  9058. * (frame_ref_old - frame_ref_after_popped);
  9059. if (total_size > sizeof(frame_ref_tmp)
  9060. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9061. error_buf_size))) {
  9062. goto fail;
  9063. }
  9064. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9065. frame_ref_after_popped, (uint32)total_size);
  9066. #if WASM_ENABLE_GC != 0
  9067. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9068. total_size =
  9069. (uint64)sizeof(WASMRefTypeMap)
  9070. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9071. if (total_size > sizeof(frame_reftype_map_tmp)
  9072. && !(frame_reftype_map_buf = loader_malloc(
  9073. total_size, error_buf, error_buf_size))) {
  9074. goto fail;
  9075. }
  9076. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9077. frame_reftype_map_after_popped, (uint32)total_size);
  9078. #endif
  9079. #if WASM_ENABLE_FAST_INTERP != 0
  9080. frame_offset_after_popped = loader_ctx->frame_offset;
  9081. total_size = (uint64)sizeof(int16)
  9082. * (frame_offset_old - frame_offset_after_popped);
  9083. if (total_size > sizeof(frame_offset_tmp)
  9084. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9085. error_buf_size))) {
  9086. goto fail;
  9087. }
  9088. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9089. frame_offset_after_popped, (uint32)total_size);
  9090. #endif
  9091. }
  9092. #if WASM_ENABLE_GC != 0
  9093. j = 0;
  9094. #endif
  9095. for (i = 0; i < (int32)arity; i++) {
  9096. #if WASM_ENABLE_GC != 0
  9097. if (wasm_is_type_multi_byte_type(types[i])) {
  9098. bh_assert(reftype_maps[j].index == i);
  9099. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9100. reftype_maps[j].ref_type,
  9101. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9102. j++;
  9103. }
  9104. #endif
  9105. #if WASM_ENABLE_FAST_INTERP != 0
  9106. bool disable_emit = true;
  9107. int16 operand_offset = 0;
  9108. PUSH_OFFSET_TYPE(types[i]);
  9109. #endif
  9110. PUSH_TYPE(types[i]);
  9111. }
  9112. #if WASM_ENABLE_FAST_INTERP != 0
  9113. emit_br_info(target_block, opcode == WASM_OP_BR);
  9114. #endif
  9115. /* Restore the stack data, note that frame_ref_bottom,
  9116. frame_reftype_map_bottom, frame_offset_bottom may be
  9117. re-allocated in the above push operations */
  9118. if (opcode == WASM_OP_BR_TABLE) {
  9119. uint32 total_size;
  9120. /* The stack operand num should not be smaller than before
  9121. after pop and push operations */
  9122. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9123. loader_ctx->stack_cell_num = stack_cell_num_old;
  9124. loader_ctx->frame_ref =
  9125. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9126. total_size = (uint32)(sizeof(uint8)
  9127. * (frame_ref_old - frame_ref_after_popped));
  9128. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9129. frame_ref_buf, total_size);
  9130. #if WASM_ENABLE_GC != 0
  9131. /* The stack operand num should not be smaller than before
  9132. after pop and push operations */
  9133. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9134. loader_ctx->reftype_map_num = reftype_map_num_old;
  9135. loader_ctx->frame_reftype_map =
  9136. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9137. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9138. * (frame_reftype_map_old
  9139. - frame_reftype_map_after_popped));
  9140. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9141. total_size, frame_reftype_map_buf, total_size);
  9142. #endif
  9143. #if WASM_ENABLE_FAST_INTERP != 0
  9144. loader_ctx->frame_offset =
  9145. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9146. total_size =
  9147. (uint32)(sizeof(int16)
  9148. * (frame_offset_old - frame_offset_after_popped));
  9149. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9150. total_size, frame_offset_buf, total_size);
  9151. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9152. #endif
  9153. }
  9154. ret = true;
  9155. goto cleanup_and_return;
  9156. }
  9157. available_stack_cell =
  9158. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9159. #if WASM_ENABLE_GC != 0
  9160. available_reftype_map =
  9161. (int32)(loader_ctx->reftype_map_num
  9162. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9163. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9164. #endif
  9165. /* Check stack top values match target block type */
  9166. for (i = (int32)arity - 1; i >= 0; i--) {
  9167. type = types[i];
  9168. #if WASM_ENABLE_GC != 0
  9169. ref_type = NULL;
  9170. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9171. if (is_type_multi_byte) {
  9172. bh_assert(reftype_map);
  9173. ref_type = reftype_map->ref_type;
  9174. }
  9175. #endif
  9176. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9177. break;
  9178. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9179. #if WASM_ENABLE_GC != 0
  9180. frame_reftype_map, available_reftype_map,
  9181. #endif
  9182. type,
  9183. #if WASM_ENABLE_GC != 0
  9184. ref_type,
  9185. #endif
  9186. error_buf, error_buf_size)) {
  9187. goto fail;
  9188. }
  9189. cell_num = wasm_value_type_cell_num(types[i]);
  9190. frame_ref -= cell_num;
  9191. available_stack_cell -= cell_num;
  9192. #if WASM_ENABLE_GC != 0
  9193. if (is_type_multi_byte) {
  9194. frame_reftype_map--;
  9195. available_reftype_map--;
  9196. reftype_map--;
  9197. }
  9198. #endif
  9199. }
  9200. #if WASM_ENABLE_FAST_INTERP != 0
  9201. emit_br_info(target_block, opcode == WASM_OP_BR);
  9202. #endif
  9203. ret = true;
  9204. cleanup_and_return:
  9205. fail:
  9206. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9207. wasm_runtime_free(frame_ref_buf);
  9208. #if WASM_ENABLE_GC != 0
  9209. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9210. wasm_runtime_free(frame_reftype_map_buf);
  9211. #endif
  9212. #if WASM_ENABLE_FAST_INTERP != 0
  9213. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9214. wasm_runtime_free(frame_offset_buf);
  9215. #endif
  9216. return ret;
  9217. }
  9218. static BranchBlock *
  9219. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9220. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9221. {
  9222. uint8 *p = *p_buf, *p_end = buf_end;
  9223. BranchBlock *frame_csp_tmp;
  9224. uint32 depth;
  9225. read_leb_uint32(p, p_end, depth);
  9226. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9227. error_buf_size)) {
  9228. goto fail;
  9229. }
  9230. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9231. *p_buf = p;
  9232. return frame_csp_tmp;
  9233. fail:
  9234. return NULL;
  9235. }
  9236. #if WASM_ENABLE_EXCE_HANDLING != 0
  9237. static BranchBlock *
  9238. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9239. uint8 *buf_end, char *error_buf,
  9240. 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. /*
  9247. * Note: "delegate 0" means the surrounding block, not the
  9248. * try-delegate block itself.
  9249. *
  9250. * Note: the caller hasn't popped the try-delegate frame yet.
  9251. */
  9252. bh_assert(loader_ctx->csp_num > 0);
  9253. if (loader_ctx->csp_num - 1 <= depth) {
  9254. #if WASM_ENABLE_SPEC_TEST == 0
  9255. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9256. #else
  9257. set_error_buf(error_buf, error_buf_size, "unknown label");
  9258. #endif
  9259. goto fail;
  9260. }
  9261. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9262. #if WASM_ENABLE_FAST_INTERP != 0
  9263. emit_br_info(frame_csp_tmp, false);
  9264. #endif
  9265. *p_buf = p;
  9266. return frame_csp_tmp;
  9267. fail:
  9268. return NULL;
  9269. }
  9270. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9271. static bool
  9272. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9273. char *error_buf, uint32 error_buf_size)
  9274. {
  9275. BlockType *block_type = &block->block_type;
  9276. uint8 *return_types = NULL;
  9277. uint32 return_count = 0;
  9278. int32 available_stack_cell, return_cell_num, i;
  9279. uint8 *frame_ref = NULL;
  9280. #if WASM_ENABLE_GC != 0
  9281. WASMRefTypeMap *frame_reftype_map;
  9282. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9283. WASMRefType *ref_type;
  9284. uint32 param_count, return_reftype_map_count = 0;
  9285. int32 available_reftype_map =
  9286. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9287. #endif
  9288. available_stack_cell =
  9289. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9290. #if WASM_ENABLE_GC == 0
  9291. return_count = block_type_get_result_types(block_type, &return_types);
  9292. #else
  9293. return_count = block_type_get_result_types(block_type, &return_types,
  9294. &return_reftype_maps,
  9295. &return_reftype_map_count);
  9296. param_count =
  9297. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9298. (void)param_count;
  9299. #endif
  9300. return_cell_num =
  9301. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9302. /* If the stack is in polymorphic state, just clear the stack
  9303. * and then re-push the values to make the stack top values
  9304. * match block type. */
  9305. if (block->is_stack_polymorphic) {
  9306. #if WASM_ENABLE_GC != 0
  9307. int32 j = return_reftype_map_count - 1;
  9308. #endif
  9309. for (i = (int32)return_count - 1; i >= 0; i--) {
  9310. #if WASM_ENABLE_GC != 0
  9311. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9312. bh_assert(return_reftype_maps[j].index == i + param_count);
  9313. bh_memcpy_s(
  9314. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9315. return_reftype_maps[j].ref_type,
  9316. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9317. j--;
  9318. }
  9319. #endif
  9320. #if WASM_ENABLE_FAST_INTERP != 0
  9321. POP_OFFSET_TYPE(return_types[i]);
  9322. #endif
  9323. POP_TYPE(return_types[i]);
  9324. }
  9325. /* Check stack is empty */
  9326. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9327. set_error_buf(
  9328. error_buf, error_buf_size,
  9329. "type mismatch: stack size does not match block type");
  9330. goto fail;
  9331. }
  9332. #if WASM_ENABLE_GC != 0
  9333. j = 0;
  9334. #endif
  9335. for (i = 0; i < (int32)return_count; i++) {
  9336. #if WASM_ENABLE_GC != 0
  9337. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9338. bh_assert(return_reftype_maps[j].index == i + param_count);
  9339. bh_memcpy_s(
  9340. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9341. return_reftype_maps[j].ref_type,
  9342. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9343. j++;
  9344. }
  9345. #endif
  9346. #if WASM_ENABLE_FAST_INTERP != 0
  9347. bool disable_emit = true;
  9348. int16 operand_offset = 0;
  9349. PUSH_OFFSET_TYPE(return_types[i]);
  9350. #endif
  9351. PUSH_TYPE(return_types[i]);
  9352. }
  9353. return true;
  9354. }
  9355. if (available_stack_cell != return_cell_num) {
  9356. #if WASM_ENABLE_EXCE_HANDLING != 0
  9357. /* testspec: this error message format is expected by try_catch.wast */
  9358. snprintf(
  9359. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9360. block->label_type == LABEL_TYPE_TRY
  9361. || (block->label_type == LABEL_TYPE_CATCH
  9362. && return_cell_num > 0)
  9363. ? "instruction"
  9364. : "block",
  9365. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9366. " but stack has ",
  9367. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9368. : "");
  9369. goto fail;
  9370. #else
  9371. set_error_buf(error_buf, error_buf_size,
  9372. "type mismatch: stack size does not match block type");
  9373. goto fail;
  9374. #endif
  9375. }
  9376. /* Check stack values match return types */
  9377. frame_ref = loader_ctx->frame_ref;
  9378. #if WASM_ENABLE_GC != 0
  9379. frame_reftype_map = loader_ctx->frame_reftype_map;
  9380. return_reftype_map =
  9381. return_reftype_map_count
  9382. ? return_reftype_maps + return_reftype_map_count - 1
  9383. : NULL;
  9384. #endif
  9385. for (i = (int32)return_count - 1; i >= 0; i--) {
  9386. uint8 type = return_types[i];
  9387. #if WASM_ENABLE_GC != 0
  9388. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9389. ref_type = NULL;
  9390. if (is_type_multi_byte) {
  9391. bh_assert(return_reftype_map);
  9392. ref_type = return_reftype_map->ref_type;
  9393. }
  9394. #endif
  9395. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9396. #if WASM_ENABLE_GC != 0
  9397. frame_reftype_map, available_reftype_map,
  9398. #endif
  9399. type,
  9400. #if WASM_ENABLE_GC != 0
  9401. ref_type,
  9402. #endif
  9403. error_buf, error_buf_size))
  9404. return false;
  9405. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9406. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9407. #if WASM_ENABLE_GC != 0
  9408. if (is_type_multi_byte) {
  9409. frame_reftype_map--;
  9410. available_reftype_map--;
  9411. return_reftype_map--;
  9412. }
  9413. #endif
  9414. }
  9415. return true;
  9416. fail:
  9417. return false;
  9418. }
  9419. #if WASM_ENABLE_FAST_INTERP != 0
  9420. /* Copy parameters to dynamic space.
  9421. * 1) POP original parameter out;
  9422. * 2) Push and copy original values to dynamic space.
  9423. * The copy instruction format:
  9424. * Part a: param count
  9425. * Part b: all param total cell num
  9426. * Part c: each param's cell_num, src offset and dst offset
  9427. * Part d: each param's src offset
  9428. * Part e: each param's dst offset
  9429. */
  9430. static bool
  9431. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  9432. char *error_buf, uint32 error_buf_size)
  9433. {
  9434. bool ret = false;
  9435. int16 *frame_offset = NULL;
  9436. uint8 *cells = NULL, cell;
  9437. int16 *src_offsets = NULL;
  9438. uint8 *emit_data = NULL;
  9439. uint32 i;
  9440. BranchBlock *block = loader_ctx->frame_csp - 1;
  9441. BlockType *block_type = &block->block_type;
  9442. WASMFuncType *wasm_type = block_type->u.type;
  9443. uint32 param_count = block_type->u.type->param_count;
  9444. int16 condition_offset = 0;
  9445. bool disable_emit = false;
  9446. int16 operand_offset = 0;
  9447. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9448. bh_assert(size > 0);
  9449. /* For if block, we also need copy the condition operand offset. */
  9450. if (is_if_block)
  9451. size += sizeof(*cells) + sizeof(*src_offsets);
  9452. /* Allocate memory for the emit data */
  9453. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9454. return false;
  9455. cells = emit_data;
  9456. src_offsets = (int16 *)(cells + param_count);
  9457. if (is_if_block)
  9458. condition_offset = *loader_ctx->frame_offset;
  9459. /* POP original parameter out */
  9460. for (i = 0; i < param_count; i++) {
  9461. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9462. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9463. }
  9464. frame_offset = loader_ctx->frame_offset;
  9465. /* Get each param's cell num and src offset */
  9466. for (i = 0; i < param_count; i++) {
  9467. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9468. cells[i] = cell;
  9469. src_offsets[i] = *frame_offset;
  9470. frame_offset += cell;
  9471. }
  9472. /* emit copy instruction */
  9473. emit_label(EXT_OP_COPY_STACK_VALUES);
  9474. /* Part a) */
  9475. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9476. /* Part b) */
  9477. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9478. : wasm_type->param_cell_num);
  9479. /* Part c) */
  9480. for (i = 0; i < param_count; i++)
  9481. emit_byte(loader_ctx, cells[i]);
  9482. if (is_if_block)
  9483. emit_byte(loader_ctx, 1);
  9484. /* Part d) */
  9485. for (i = 0; i < param_count; i++)
  9486. emit_operand(loader_ctx, src_offsets[i]);
  9487. if (is_if_block)
  9488. emit_operand(loader_ctx, condition_offset);
  9489. /* Part e) */
  9490. /* Push to dynamic space. The push will emit the dst offset. */
  9491. for (i = 0; i < param_count; i++)
  9492. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9493. if (is_if_block)
  9494. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9495. ret = true;
  9496. fail:
  9497. /* Free the emit data */
  9498. wasm_runtime_free(emit_data);
  9499. return ret;
  9500. }
  9501. #endif
  9502. #if WASM_ENABLE_GC == 0
  9503. #define RESET_REFTYPE_MAP_STACK() (void)0
  9504. #else
  9505. #define RESET_REFTYPE_MAP_STACK() \
  9506. do { \
  9507. loader_ctx->reftype_map_num = \
  9508. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9509. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9510. + loader_ctx->reftype_map_num; \
  9511. } while (0)
  9512. #endif
  9513. /* reset the stack to the state of before entering the last block */
  9514. #if WASM_ENABLE_FAST_INTERP != 0
  9515. #define RESET_STACK() \
  9516. do { \
  9517. loader_ctx->stack_cell_num = \
  9518. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9519. loader_ctx->frame_ref = \
  9520. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9521. loader_ctx->frame_offset = \
  9522. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9523. RESET_REFTYPE_MAP_STACK(); \
  9524. } while (0)
  9525. #else
  9526. #define RESET_STACK() \
  9527. do { \
  9528. loader_ctx->stack_cell_num = \
  9529. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9530. loader_ctx->frame_ref = \
  9531. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9532. RESET_REFTYPE_MAP_STACK(); \
  9533. } while (0)
  9534. #endif
  9535. /* set current block's stack polymorphic state */
  9536. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9537. do { \
  9538. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9539. _cur_block->is_stack_polymorphic = flag; \
  9540. } while (0)
  9541. #define BLOCK_HAS_PARAM(block_type) \
  9542. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9543. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9544. do { \
  9545. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9546. error_buf_size))) { \
  9547. goto fail; \
  9548. } \
  9549. } while (0)
  9550. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9551. static bool
  9552. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9553. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9554. uint32 error_buf_size)
  9555. {
  9556. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9557. return false;
  9558. }
  9559. if (table_idx < module->import_table_count) {
  9560. if (p_elem_type)
  9561. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9562. #if WASM_ENABLE_GC != 0
  9563. if (p_ref_type)
  9564. *((WASMRefType **)p_ref_type) =
  9565. module->import_tables[table_idx].u.table.elem_ref_type;
  9566. #endif
  9567. }
  9568. else {
  9569. if (p_elem_type)
  9570. *p_elem_type =
  9571. module->tables[module->import_table_count + table_idx]
  9572. .elem_type;
  9573. #if WASM_ENABLE_GC != 0
  9574. if (p_ref_type)
  9575. *((WASMRefType **)p_ref_type) =
  9576. module->tables[module->import_table_count + table_idx]
  9577. .elem_ref_type;
  9578. #endif
  9579. }
  9580. return true;
  9581. }
  9582. static bool
  9583. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9584. uint8 *p_elem_type, void **p_elem_ref_type,
  9585. char *error_buf, uint32 error_buf_size)
  9586. {
  9587. if (table_seg_idx >= module->table_seg_count) {
  9588. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9589. table_seg_idx);
  9590. return false;
  9591. }
  9592. if (p_elem_type) {
  9593. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9594. }
  9595. #if WASM_ENABLE_GC != 0
  9596. if (p_elem_ref_type)
  9597. *((WASMRefType **)p_elem_ref_type) =
  9598. module->table_segments[table_seg_idx].elem_ref_type;
  9599. #endif
  9600. return true;
  9601. }
  9602. #endif
  9603. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9604. const uint8 *
  9605. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9606. uint32 *len)
  9607. {
  9608. WASMCustomSection *section = module->custom_section_list;
  9609. while (section) {
  9610. if ((section->name_len == strlen(name))
  9611. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9612. if (len) {
  9613. *len = section->content_len;
  9614. }
  9615. return section->content_addr;
  9616. }
  9617. section = section->next;
  9618. }
  9619. return NULL;
  9620. }
  9621. #endif
  9622. static bool
  9623. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9624. uint32 cur_func_idx, char *error_buf,
  9625. uint32 error_buf_size)
  9626. {
  9627. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9628. uint32 param_count, local_count, global_count;
  9629. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9630. BlockType func_block_type;
  9631. uint16 *local_offsets, local_offset;
  9632. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9633. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9634. mem_offset_t mem_offset;
  9635. int32 i32_const = 0;
  9636. int64 i64_const;
  9637. uint8 opcode;
  9638. bool return_value = false;
  9639. WASMLoaderContext *loader_ctx;
  9640. BranchBlock *frame_csp_tmp;
  9641. #if WASM_ENABLE_GC != 0
  9642. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9643. uint32 param_reftype_map_count, local_reftype_map_count;
  9644. int32 heap_type;
  9645. WASMRefType wasm_ref_type = { 0 };
  9646. bool need_ref_type_map;
  9647. #endif
  9648. #if WASM_ENABLE_FAST_INTERP != 0
  9649. uint8 *func_const_end, *func_const = NULL;
  9650. int16 operand_offset = 0;
  9651. uint8 last_op = 0;
  9652. bool disable_emit, preserve_local = false, if_condition_available = true;
  9653. float32 f32_const;
  9654. float64 f64_const;
  9655. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9656. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9657. #endif
  9658. #if WASM_ENABLE_MEMORY64 != 0
  9659. bool is_memory64 = has_module_memory64(module);
  9660. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9661. #else
  9662. mem_offset_type = VALUE_TYPE_I32;
  9663. #endif
  9664. global_count = module->import_global_count + module->global_count;
  9665. param_count = func->func_type->param_count;
  9666. param_types = func->func_type->types;
  9667. func_block_type.is_value_type = false;
  9668. func_block_type.u.type = func->func_type;
  9669. local_count = func->local_count;
  9670. local_types = func->local_types;
  9671. local_offsets = func->local_offsets;
  9672. #if WASM_ENABLE_GC != 0
  9673. param_reftype_maps = func->func_type->ref_type_maps;
  9674. param_reftype_map_count = func->func_type->ref_type_map_count;
  9675. local_reftype_maps = func->local_ref_type_maps;
  9676. local_reftype_map_count = func->local_ref_type_map_count;
  9677. #endif
  9678. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9679. goto fail;
  9680. }
  9681. #if WASM_ENABLE_GC != 0
  9682. loader_ctx->module = module;
  9683. loader_ctx->ref_type_set = module->ref_type_set;
  9684. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9685. #endif
  9686. #if WASM_ENABLE_FAST_INTERP != 0
  9687. /* For the first traverse, the initial value of preserved_local_offset has
  9688. * not been determined, we use the INT16_MAX to represent that a slot has
  9689. * been copied to preserve space. For second traverse, this field will be
  9690. * set to the appropriate value in wasm_loader_ctx_reinit.
  9691. * This is for Issue #1230,
  9692. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9693. * drop opcodes need to know which slots are preserved, so those slots will
  9694. * not be treated as dynamically allocated slots */
  9695. loader_ctx->preserved_local_offset = INT16_MAX;
  9696. re_scan:
  9697. if (loader_ctx->code_compiled_size > 0) {
  9698. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9699. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9700. goto fail;
  9701. }
  9702. p = func->code;
  9703. func->code_compiled = loader_ctx->p_code_compiled;
  9704. func->code_compiled_size = loader_ctx->code_compiled_size;
  9705. }
  9706. #endif
  9707. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9708. while (p < p_end) {
  9709. opcode = *p++;
  9710. #if WASM_ENABLE_FAST_INTERP != 0
  9711. p_org = p;
  9712. disable_emit = false;
  9713. emit_label(opcode);
  9714. #endif
  9715. switch (opcode) {
  9716. case WASM_OP_UNREACHABLE:
  9717. RESET_STACK();
  9718. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9719. break;
  9720. case WASM_OP_NOP:
  9721. #if WASM_ENABLE_FAST_INTERP != 0
  9722. skip_label();
  9723. #endif
  9724. break;
  9725. case WASM_OP_IF:
  9726. {
  9727. #if WASM_ENABLE_FAST_INTERP != 0
  9728. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9729. int32 available_stack_cell =
  9730. (int32)(loader_ctx->stack_cell_num
  9731. - parent_block->stack_cell_num);
  9732. if (available_stack_cell <= 0
  9733. && parent_block->is_stack_polymorphic)
  9734. if_condition_available = false;
  9735. else
  9736. if_condition_available = true;
  9737. PRESERVE_LOCAL_FOR_BLOCK();
  9738. #endif
  9739. #if WASM_ENABLE_GC == 0
  9740. POP_I32();
  9741. #endif
  9742. goto handle_op_block_and_loop;
  9743. }
  9744. case WASM_OP_BLOCK:
  9745. case WASM_OP_LOOP:
  9746. #if WASM_ENABLE_EXCE_HANDLING != 0
  9747. case WASM_OP_TRY:
  9748. if (opcode == WASM_OP_TRY) {
  9749. /*
  9750. * keep track of exception handlers to account for
  9751. * memory allocation
  9752. */
  9753. func->exception_handler_count++;
  9754. /*
  9755. * try is a block
  9756. * do nothing special, but execution continues to
  9757. * to handle_op_block_and_loop,
  9758. * and that be pushes the csp
  9759. */
  9760. }
  9761. #endif
  9762. #if WASM_ENABLE_FAST_INTERP != 0
  9763. PRESERVE_LOCAL_FOR_BLOCK();
  9764. #endif
  9765. handle_op_block_and_loop:
  9766. {
  9767. uint8 value_type;
  9768. BlockType block_type;
  9769. #if WASM_ENABLE_FAST_INTERP != 0
  9770. uint32 available_params = 0;
  9771. #endif
  9772. CHECK_BUF(p, p_end, 1);
  9773. value_type = read_uint8(p);
  9774. if (is_byte_a_type(value_type)) {
  9775. /* If the first byte is one of these special values:
  9776. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9777. * the single return value. */
  9778. block_type.is_value_type = true;
  9779. block_type.u.value_type.type = value_type;
  9780. #if WASM_ENABLE_WAMR_COMPILER != 0
  9781. if (value_type == VALUE_TYPE_V128)
  9782. module->is_simd_used = true;
  9783. else if (value_type == VALUE_TYPE_FUNCREF
  9784. || value_type == VALUE_TYPE_EXTERNREF)
  9785. module->is_ref_types_used = true;
  9786. #endif
  9787. #if WASM_ENABLE_GC != 0
  9788. if (value_type != VALUE_TYPE_VOID) {
  9789. p_org = p;
  9790. p--;
  9791. if (!resolve_value_type((const uint8 **)&p, p_end,
  9792. module, module->type_count,
  9793. &need_ref_type_map,
  9794. &wasm_ref_type, false,
  9795. error_buf, error_buf_size)) {
  9796. goto fail;
  9797. }
  9798. if (need_ref_type_map) {
  9799. block_type.u.value_type.ref_type_map.index = 0;
  9800. if (!(block_type.u.value_type.ref_type_map
  9801. .ref_type = reftype_set_insert(
  9802. module->ref_type_set, &wasm_ref_type,
  9803. error_buf, error_buf_size))) {
  9804. goto fail;
  9805. }
  9806. }
  9807. /* Set again as the type might be changed, e.g.
  9808. (ref null any) to anyref */
  9809. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9810. #if WASM_ENABLE_FAST_INTERP == 0
  9811. while (p_org < p) {
  9812. #if WASM_ENABLE_DEBUG_INTERP != 0
  9813. if (!record_fast_op(module, p_org, *p_org,
  9814. error_buf, error_buf_size)) {
  9815. goto fail;
  9816. }
  9817. #endif
  9818. /* Ignore extra bytes for interpreter */
  9819. *p_org++ = WASM_OP_NOP;
  9820. }
  9821. #endif
  9822. }
  9823. #endif /* end of WASM_ENABLE_GC != 0 */
  9824. }
  9825. else {
  9826. uint32 type_index;
  9827. /* Resolve the leb128 encoded type index as block type */
  9828. p--;
  9829. p_org = p - 1;
  9830. read_leb_uint32(p, p_end, type_index);
  9831. if (type_index >= module->type_count) {
  9832. set_error_buf(error_buf, error_buf_size,
  9833. "unknown type");
  9834. goto fail;
  9835. }
  9836. block_type.is_value_type = false;
  9837. block_type.u.type =
  9838. (WASMFuncType *)module->types[type_index];
  9839. #if WASM_ENABLE_FAST_INTERP == 0
  9840. /* If block use type index as block type, change the opcode
  9841. * to new extended opcode so that interpreter can resolve
  9842. * the block quickly.
  9843. */
  9844. #if WASM_ENABLE_DEBUG_INTERP != 0
  9845. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9846. error_buf_size)) {
  9847. goto fail;
  9848. }
  9849. #endif
  9850. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9851. #endif
  9852. }
  9853. #if WASM_ENABLE_GC != 0
  9854. if (opcode == WASM_OP_IF) {
  9855. POP_I32();
  9856. }
  9857. #endif
  9858. /* Pop block parameters from stack */
  9859. if (BLOCK_HAS_PARAM(block_type)) {
  9860. WASMFuncType *wasm_type = block_type.u.type;
  9861. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9862. #if WASM_ENABLE_FAST_INTERP != 0
  9863. uint32 cell_num;
  9864. available_params = block_type.u.type->param_count;
  9865. #endif
  9866. for (i = 0; i < block_type.u.type->param_count; i++) {
  9867. int32 available_stack_cell =
  9868. (int32)(loader_ctx->stack_cell_num
  9869. - cur_block->stack_cell_num);
  9870. if (available_stack_cell <= 0
  9871. && cur_block->is_stack_polymorphic) {
  9872. #if WASM_ENABLE_FAST_INTERP != 0
  9873. available_params = i;
  9874. #endif
  9875. break;
  9876. }
  9877. POP_TYPE(
  9878. wasm_type->types[wasm_type->param_count - i - 1]);
  9879. #if WASM_ENABLE_FAST_INTERP != 0
  9880. /* decrease the frame_offset pointer accordingly to keep
  9881. * consistent with frame_ref stack */
  9882. cell_num = wasm_value_type_cell_num(
  9883. wasm_type->types[wasm_type->param_count - i - 1]);
  9884. loader_ctx->frame_offset -= cell_num;
  9885. #endif
  9886. }
  9887. }
  9888. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9889. block_type, p);
  9890. /* Pass parameters to block */
  9891. if (BLOCK_HAS_PARAM(block_type)) {
  9892. for (i = 0; i < block_type.u.type->param_count; i++) {
  9893. #if WASM_ENABLE_FAST_INTERP != 0
  9894. uint32 cell_num = wasm_value_type_cell_num(
  9895. block_type.u.type->types[i]);
  9896. if (i >= available_params) {
  9897. /* If there isn't enough data on stack, push a dummy
  9898. * offset to keep the stack consistent with
  9899. * frame_ref.
  9900. * Since the stack is already in polymorphic state,
  9901. * the opcode will not be executed, so the dummy
  9902. * offset won't cause any error */
  9903. uint32 n;
  9904. for (n = 0; n < cell_num; n++) {
  9905. if (loader_ctx->p_code_compiled == NULL) {
  9906. if (!check_offset_push(loader_ctx,
  9907. error_buf,
  9908. error_buf_size))
  9909. goto fail;
  9910. }
  9911. *loader_ctx->frame_offset++ = 0;
  9912. }
  9913. }
  9914. else {
  9915. loader_ctx->frame_offset += cell_num;
  9916. }
  9917. #endif
  9918. PUSH_TYPE(block_type.u.type->types[i]);
  9919. }
  9920. }
  9921. #if WASM_ENABLE_FAST_INTERP != 0
  9922. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  9923. skip_label();
  9924. if (BLOCK_HAS_PARAM(block_type)) {
  9925. /* Make sure params are in dynamic space */
  9926. if (!copy_params_to_dynamic_space(
  9927. loader_ctx, false, error_buf, error_buf_size))
  9928. goto fail;
  9929. }
  9930. if (opcode == WASM_OP_LOOP) {
  9931. (loader_ctx->frame_csp - 1)->code_compiled =
  9932. loader_ctx->p_code_compiled;
  9933. }
  9934. }
  9935. #if WASM_ENABLE_EXCE_HANDLING != 0
  9936. else if (opcode == WASM_OP_TRY) {
  9937. skip_label();
  9938. }
  9939. #endif
  9940. else if (opcode == WASM_OP_IF) {
  9941. BranchBlock *block = loader_ctx->frame_csp - 1;
  9942. /* If block has parameters, we should make sure they are in
  9943. * dynamic space. Otherwise, when else branch is missing,
  9944. * the later opcode may consume incorrect operand offset.
  9945. * Spec case:
  9946. * (func (export "params-id") (param i32) (result i32)
  9947. * (i32.const 1)
  9948. * (i32.const 2)
  9949. * (if (param i32 i32) (result i32 i32) (local.get 0)
  9950. * (then)) (i32.add)
  9951. * )
  9952. *
  9953. * So we should emit a copy instruction before the if.
  9954. *
  9955. * And we also need to save the parameter offsets and
  9956. * recover them before entering else branch.
  9957. *
  9958. */
  9959. if (BLOCK_HAS_PARAM(block_type)) {
  9960. uint64 size;
  9961. /* In polymorphic state, there may be no if condition on
  9962. * the stack, so the offset may not emitted */
  9963. if (if_condition_available) {
  9964. /* skip the if condition operand offset */
  9965. wasm_loader_emit_backspace(loader_ctx,
  9966. sizeof(int16));
  9967. }
  9968. /* skip the if label */
  9969. skip_label();
  9970. /* Emit a copy instruction */
  9971. if (!copy_params_to_dynamic_space(
  9972. loader_ctx, true, error_buf, error_buf_size))
  9973. goto fail;
  9974. /* Emit the if instruction */
  9975. emit_label(opcode);
  9976. /* Emit the new condition operand offset */
  9977. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  9978. /* Save top param_count values of frame_offset stack, so
  9979. * that we can recover it before executing else branch
  9980. */
  9981. size = sizeof(int16)
  9982. * (uint64)block_type.u.type->param_cell_num;
  9983. if (!(block->param_frame_offsets = loader_malloc(
  9984. size, error_buf, error_buf_size)))
  9985. goto fail;
  9986. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  9987. loader_ctx->frame_offset
  9988. - size / sizeof(int16),
  9989. (uint32)size);
  9990. }
  9991. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9992. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  9993. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9994. }
  9995. #endif
  9996. break;
  9997. }
  9998. #if WASM_ENABLE_EXCE_HANDLING != 0
  9999. case WASM_OP_THROW:
  10000. {
  10001. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10002. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10003. uint8 label_type = cur_block->label_type;
  10004. uint32 tag_index = 0;
  10005. read_leb_int32(p, p_end, tag_index);
  10006. /* check validity of tag_index against module->tag_count */
  10007. /* check tag index is within the tag index space */
  10008. if (tag_index >= module->import_tag_count + module->tag_count) {
  10009. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10010. tag_index);
  10011. goto fail;
  10012. }
  10013. /* the tag_type is stored in either the WASMTag (section tags)
  10014. * or WASMTagImport (import tag) */
  10015. WASMType *tag_type = NULL;
  10016. if (tag_index < module->import_tag_count) {
  10017. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10018. }
  10019. else {
  10020. tag_type =
  10021. module->tags[tag_index - module->import_tag_count]
  10022. ->tag_type;
  10023. }
  10024. if (tag_type->result_count != 0) {
  10025. set_error_buf(error_buf, error_buf_size,
  10026. "tag type signature does not return void");
  10027. goto fail;
  10028. }
  10029. int32 available_stack_cell =
  10030. (int32)(loader_ctx->stack_cell_num
  10031. - cur_block->stack_cell_num);
  10032. /* Check stack values match return types by comparing tag param
  10033. * types with stack cells */
  10034. uint8 *frame_ref = loader_ctx->frame_ref;
  10035. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  10036. tti--) {
  10037. if (!check_stack_top_values(
  10038. loader_ctx, frame_ref, available_stack_cell,
  10039. tag_type->types[tti], error_buf, error_buf_size)) {
  10040. snprintf(error_buf, error_buf_size,
  10041. "type mismatch: instruction requires [%s] but "
  10042. "stack has [%s]",
  10043. tag_type->param_count > 0
  10044. ? type2str(tag_type->types[tti])
  10045. : "",
  10046. available_stack_cell > 0
  10047. ? type2str(*(loader_ctx->frame_ref - 1))
  10048. : "");
  10049. goto fail;
  10050. }
  10051. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10052. available_stack_cell -=
  10053. wasm_value_type_cell_num(tag_type->types[tti]);
  10054. }
  10055. /* throw is stack polymorphic */
  10056. (void)label_type;
  10057. RESET_STACK();
  10058. break;
  10059. }
  10060. case WASM_OP_RETHROW:
  10061. {
  10062. /* must be done before checking branch block */
  10063. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10064. /* check the target catching block: LABEL_TYPE_CATCH */
  10065. if (!(frame_csp_tmp =
  10066. check_branch_block(loader_ctx, &p, p_end, opcode,
  10067. error_buf, error_buf_size)))
  10068. goto fail;
  10069. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10070. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10071. /* trap according to spectest (rethrow.wast) */
  10072. set_error_buf(error_buf, error_buf_size,
  10073. "invalid rethrow label");
  10074. goto fail;
  10075. }
  10076. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10077. uint8 label_type = cur_block->label_type;
  10078. (void)label_type;
  10079. /* rethrow is stack polymorphic */
  10080. RESET_STACK();
  10081. break;
  10082. }
  10083. case WASM_OP_DELEGATE:
  10084. {
  10085. /* check target block is valid */
  10086. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10087. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10088. goto fail;
  10089. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10090. uint8 label_type = cur_block->label_type;
  10091. (void)label_type;
  10092. /* DELEGATE ends the block */
  10093. POP_CSP();
  10094. break;
  10095. }
  10096. case WASM_OP_CATCH:
  10097. {
  10098. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10099. uint8 label_type = cur_block->label_type;
  10100. uint32 tag_index = 0;
  10101. read_leb_int32(p, p_end, tag_index);
  10102. /* check validity of tag_index against module->tag_count */
  10103. /* check tag index is within the tag index space */
  10104. if (tag_index >= module->import_tag_count + module->tag_count) {
  10105. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10106. __FUNCTION__);
  10107. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10108. goto fail;
  10109. }
  10110. /* the tag_type is stored in either the WASMTag (section tags)
  10111. * or WASMTagImport (import tag) */
  10112. WASMType *func_type = NULL;
  10113. if (tag_index < module->import_tag_count) {
  10114. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10115. }
  10116. else {
  10117. func_type =
  10118. module->tags[tag_index - module->import_tag_count]
  10119. ->tag_type;
  10120. }
  10121. if (func_type->result_count != 0) {
  10122. set_error_buf(error_buf, error_buf_size,
  10123. "tag type signature does not return void");
  10124. goto fail;
  10125. }
  10126. /* check validity of current label (expect LABEL_TYPE_TRY or
  10127. * LABEL_TYPE_CATCH) */
  10128. if ((LABEL_TYPE_CATCH != label_type)
  10129. && (LABEL_TYPE_TRY != label_type)) {
  10130. set_error_buf(error_buf, error_buf_size,
  10131. "Unexpected block sequence encountered.");
  10132. goto fail;
  10133. }
  10134. BlockType new_block_type;
  10135. new_block_type.is_value_type = false;
  10136. new_block_type.u.type = func_type;
  10137. /*
  10138. * replace frame_csp by LABEL_TYPE_CATCH
  10139. */
  10140. cur_block->label_type = LABEL_TYPE_CATCH;
  10141. /* RESET_STACK removes the values pushed in TRY or pervious
  10142. * CATCH Blocks */
  10143. RESET_STACK();
  10144. /* push types on the stack according to catched type */
  10145. if (BLOCK_HAS_PARAM(new_block_type)) {
  10146. for (i = 0; i < new_block_type.u.type->param_count; i++)
  10147. PUSH_TYPE(new_block_type.u.type->types[i]);
  10148. }
  10149. break;
  10150. }
  10151. case WASM_OP_CATCH_ALL:
  10152. {
  10153. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10154. /* expecting a TRY or CATCH, anything else will be considered an
  10155. * error */
  10156. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10157. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10158. set_error_buf(error_buf, error_buf_size,
  10159. "Unexpected block sequence encountered.");
  10160. goto fail;
  10161. }
  10162. /* no immediates */
  10163. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10164. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10165. /* RESET_STACK removes the values pushed in TRY or pervious
  10166. * CATCH Blocks */
  10167. RESET_STACK();
  10168. /* catch_all has no tagtype and therefore no parameters */
  10169. break;
  10170. }
  10171. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10172. case WASM_OP_ELSE:
  10173. {
  10174. BranchBlock *block = NULL;
  10175. BlockType block_type;
  10176. if (loader_ctx->csp_num < 2
  10177. /* the matched if isn't found */
  10178. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10179. /* duplicated else is found */
  10180. || (loader_ctx->frame_csp - 1)->else_addr) {
  10181. set_error_buf(
  10182. error_buf, error_buf_size,
  10183. "opcode else found without matched opcode if");
  10184. goto fail;
  10185. }
  10186. block = loader_ctx->frame_csp - 1;
  10187. /* check whether if branch's stack matches its result type */
  10188. if (!check_block_stack(loader_ctx, block, error_buf,
  10189. error_buf_size))
  10190. goto fail;
  10191. block->else_addr = p - 1;
  10192. block_type = block->block_type;
  10193. #if WASM_ENABLE_GC != 0
  10194. if (!wasm_loader_init_local_use_masks(
  10195. loader_ctx, local_count, error_buf, error_buf_size)) {
  10196. goto fail;
  10197. }
  10198. #endif
  10199. #if WASM_ENABLE_FAST_INTERP != 0
  10200. /* if the result of if branch is in local or const area, add a
  10201. * copy op */
  10202. RESERVE_BLOCK_RET();
  10203. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10204. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10205. #endif
  10206. RESET_STACK();
  10207. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10208. /* Pass parameters to if-false branch */
  10209. if (BLOCK_HAS_PARAM(block_type)) {
  10210. for (i = 0; i < block_type.u.type->param_count; i++)
  10211. PUSH_TYPE(block_type.u.type->types[i]);
  10212. }
  10213. #if WASM_ENABLE_FAST_INTERP != 0
  10214. /* Recover top param_count values of frame_offset stack */
  10215. if (BLOCK_HAS_PARAM((block_type))) {
  10216. uint32 size;
  10217. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10218. bh_memcpy_s(loader_ctx->frame_offset, size,
  10219. block->param_frame_offsets, size);
  10220. loader_ctx->frame_offset += (size / sizeof(int16));
  10221. }
  10222. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10223. #endif
  10224. break;
  10225. }
  10226. case WASM_OP_END:
  10227. {
  10228. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10229. /* check whether block stack matches its result type */
  10230. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10231. error_buf_size))
  10232. goto fail;
  10233. /* if no else branch, and return types do not match param types,
  10234. report failure */
  10235. if (cur_block->label_type == LABEL_TYPE_IF
  10236. && !cur_block->else_addr) {
  10237. uint32 block_param_count = 0, block_ret_count = 0;
  10238. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  10239. BlockType *cur_block_type = &cur_block->block_type;
  10240. #if WASM_ENABLE_GC != 0
  10241. uint32 block_param_reftype_map_count;
  10242. uint32 block_ret_reftype_map_count;
  10243. WASMRefTypeMap *block_param_reftype_maps;
  10244. WASMRefTypeMap *block_ret_reftype_maps;
  10245. #endif
  10246. block_param_count = block_type_get_param_types(
  10247. cur_block_type, &block_param_types
  10248. #if WASM_ENABLE_GC != 0
  10249. ,
  10250. &block_param_reftype_maps,
  10251. &block_param_reftype_map_count
  10252. #endif
  10253. );
  10254. block_ret_count = block_type_get_result_types(
  10255. cur_block_type, &block_ret_types
  10256. #if WASM_ENABLE_GC != 0
  10257. ,
  10258. &block_ret_reftype_maps, &block_ret_reftype_map_count
  10259. #endif
  10260. );
  10261. if (block_param_count != block_ret_count
  10262. || (block_param_count
  10263. && memcmp(block_param_types, block_ret_types,
  10264. block_param_count))) {
  10265. set_error_buf(error_buf, error_buf_size,
  10266. "type mismatch: else branch missing");
  10267. goto fail;
  10268. }
  10269. #if WASM_ENABLE_GC != 0
  10270. if (block_param_reftype_map_count
  10271. != block_ret_reftype_map_count
  10272. || (block_param_reftype_map_count
  10273. && memcmp(block_param_reftype_maps,
  10274. block_ret_reftype_maps,
  10275. sizeof(WASMRefTypeMap)
  10276. * block_param_reftype_map_count))) {
  10277. set_error_buf(error_buf, error_buf_size,
  10278. "type mismatch: else branch missing");
  10279. goto fail;
  10280. }
  10281. #endif
  10282. }
  10283. POP_CSP();
  10284. #if WASM_ENABLE_FAST_INTERP != 0
  10285. skip_label();
  10286. /* copy the result to the block return address */
  10287. RESERVE_BLOCK_RET();
  10288. apply_label_patch(loader_ctx, 0, PATCH_END);
  10289. free_label_patch_list(loader_ctx->frame_csp);
  10290. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10291. int32 idx;
  10292. uint8 ret_type;
  10293. emit_label(WASM_OP_RETURN);
  10294. for (idx = (int32)func->func_type->result_count - 1;
  10295. idx >= 0; idx--) {
  10296. ret_type = *(func->func_type->types
  10297. + func->func_type->param_count + idx);
  10298. POP_OFFSET_TYPE(ret_type);
  10299. }
  10300. }
  10301. #endif
  10302. if (loader_ctx->csp_num > 0) {
  10303. loader_ctx->frame_csp->end_addr = p - 1;
  10304. }
  10305. else {
  10306. /* end of function block, function will return */
  10307. if (p < p_end) {
  10308. set_error_buf(error_buf, error_buf_size,
  10309. "section size mismatch");
  10310. goto fail;
  10311. }
  10312. }
  10313. break;
  10314. }
  10315. case WASM_OP_BR:
  10316. {
  10317. if (!(frame_csp_tmp =
  10318. check_branch_block(loader_ctx, &p, p_end, opcode,
  10319. error_buf, error_buf_size)))
  10320. goto fail;
  10321. RESET_STACK();
  10322. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10323. break;
  10324. }
  10325. case WASM_OP_BR_IF:
  10326. {
  10327. POP_I32();
  10328. if (!(frame_csp_tmp =
  10329. check_branch_block(loader_ctx, &p, p_end, opcode,
  10330. error_buf, error_buf_size)))
  10331. goto fail;
  10332. break;
  10333. }
  10334. case WASM_OP_BR_TABLE:
  10335. {
  10336. uint32 depth = 0, default_arity, arity = 0;
  10337. BranchBlock *target_block;
  10338. BlockType *target_block_type;
  10339. #if WASM_ENABLE_FAST_INTERP == 0
  10340. BrTableCache *br_table_cache = NULL;
  10341. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10342. uint32 j;
  10343. #endif
  10344. read_leb_uint32(p, p_end, count);
  10345. #if WASM_ENABLE_FAST_INTERP != 0
  10346. emit_uint32(loader_ctx, count);
  10347. #endif
  10348. POP_I32();
  10349. /* Get the default depth and check it */
  10350. p_org = p;
  10351. for (i = 0; i <= count; i++) {
  10352. read_leb_uint32(p, p_end, depth);
  10353. }
  10354. if (loader_ctx->csp_num < depth + 1) {
  10355. set_error_buf(error_buf, error_buf_size,
  10356. "unknown label, "
  10357. "unexpected end of section or function");
  10358. goto fail;
  10359. }
  10360. p = p_org;
  10361. /* Get the default block's arity */
  10362. target_block = loader_ctx->frame_csp - (depth + 1);
  10363. target_block_type = &target_block->block_type;
  10364. default_arity = block_type_get_arity(target_block_type,
  10365. target_block->label_type);
  10366. #if WASM_ENABLE_FAST_INTERP == 0
  10367. p_depth_begin = p_depth = p;
  10368. #endif
  10369. for (i = 0; i <= count; i++) {
  10370. p_org = p;
  10371. read_leb_uint32(p, p_end, depth);
  10372. if (loader_ctx->csp_num < depth + 1) {
  10373. set_error_buf(error_buf, error_buf_size,
  10374. "unknown label, "
  10375. "unexpected end of section or function");
  10376. goto fail;
  10377. }
  10378. p = p_org;
  10379. /* Get the target block's arity and check it */
  10380. target_block = loader_ctx->frame_csp - (depth + 1);
  10381. target_block_type = &target_block->block_type;
  10382. arity = block_type_get_arity(target_block_type,
  10383. target_block->label_type);
  10384. if (arity != default_arity) {
  10385. set_error_buf(error_buf, error_buf_size,
  10386. "type mismatch: br_table targets must "
  10387. "all use same result type");
  10388. goto fail;
  10389. }
  10390. if (!(frame_csp_tmp =
  10391. check_branch_block(loader_ctx, &p, p_end, opcode,
  10392. error_buf, error_buf_size))) {
  10393. goto fail;
  10394. }
  10395. #if WASM_ENABLE_FAST_INTERP == 0
  10396. if (br_table_cache) {
  10397. br_table_cache->br_depths[i] = depth;
  10398. }
  10399. else {
  10400. if (depth > 255) {
  10401. /* The depth cannot be stored in one byte,
  10402. create br_table cache to store each depth */
  10403. #if WASM_ENABLE_DEBUG_INTERP != 0
  10404. if (!record_fast_op(module, p_opcode, *p_opcode,
  10405. error_buf, error_buf_size)) {
  10406. goto fail;
  10407. }
  10408. #endif
  10409. if (!(br_table_cache = loader_malloc(
  10410. offsetof(BrTableCache, br_depths)
  10411. + sizeof(uint32)
  10412. * (uint64)(count + 1),
  10413. error_buf, error_buf_size))) {
  10414. goto fail;
  10415. }
  10416. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10417. br_table_cache->br_table_op_addr = p_opcode;
  10418. br_table_cache->br_count = count;
  10419. /* Copy previous depths which are one byte */
  10420. for (j = 0; j < i; j++) {
  10421. br_table_cache->br_depths[j] = p_depth_begin[j];
  10422. }
  10423. br_table_cache->br_depths[i] = depth;
  10424. bh_list_insert(module->br_table_cache_list,
  10425. br_table_cache);
  10426. }
  10427. else {
  10428. /* The depth can be stored in one byte, use the
  10429. byte of the leb to store it */
  10430. *p_depth++ = (uint8)depth;
  10431. }
  10432. }
  10433. #endif
  10434. }
  10435. #if WASM_ENABLE_FAST_INTERP == 0
  10436. /* Set the tailing bytes to nop */
  10437. if (br_table_cache)
  10438. p_depth = p_depth_begin;
  10439. while (p_depth < p)
  10440. *p_depth++ = WASM_OP_NOP;
  10441. #endif
  10442. RESET_STACK();
  10443. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10444. break;
  10445. }
  10446. case WASM_OP_RETURN:
  10447. {
  10448. int32 idx;
  10449. uint8 ret_type;
  10450. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  10451. idx--) {
  10452. ret_type = *(func->func_type->types
  10453. + func->func_type->param_count + idx);
  10454. #if WASM_ENABLE_FAST_INTERP != 0
  10455. /* emit the offset after return opcode */
  10456. POP_OFFSET_TYPE(ret_type);
  10457. #endif
  10458. POP_TYPE(ret_type);
  10459. }
  10460. RESET_STACK();
  10461. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10462. break;
  10463. }
  10464. case WASM_OP_CALL:
  10465. #if WASM_ENABLE_TAIL_CALL != 0
  10466. case WASM_OP_RETURN_CALL:
  10467. #endif
  10468. #if WASM_ENABLE_GC != 0
  10469. case WASM_OP_CALL_REF:
  10470. case WASM_OP_RETURN_CALL_REF:
  10471. #endif
  10472. {
  10473. WASMFuncType *func_type;
  10474. uint8 type;
  10475. int32 idx;
  10476. #if WASM_ENABLE_GC != 0
  10477. WASMRefType *ref_type;
  10478. uint32 type_idx1;
  10479. int32 j;
  10480. #endif
  10481. #if WASM_ENABLE_GC != 0
  10482. if (opcode == WASM_OP_CALL_REF
  10483. || opcode == WASM_OP_RETURN_CALL_REF) {
  10484. read_leb_uint32(p, p_end, type_idx1);
  10485. if (!check_type_index(module, module->type_count, type_idx1,
  10486. error_buf, error_buf_size)) {
  10487. goto fail;
  10488. }
  10489. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10490. set_error_buf(error_buf, error_buf_size,
  10491. "unkown function type");
  10492. goto fail;
  10493. }
  10494. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10495. &type_idx, error_buf,
  10496. error_buf_size)) {
  10497. goto fail;
  10498. }
  10499. if (type == VALUE_TYPE_ANY) {
  10500. type_idx = type_idx1;
  10501. }
  10502. if (!check_type_index(module, module->type_count, type_idx,
  10503. error_buf, error_buf_size)) {
  10504. goto fail;
  10505. }
  10506. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10507. set_error_buf(error_buf, error_buf_size,
  10508. "unkown function type");
  10509. goto fail;
  10510. }
  10511. if (!wasm_func_type_is_super_of(
  10512. (WASMFuncType *)module->types[type_idx1],
  10513. (WASMFuncType *)module->types[type_idx])) {
  10514. set_error_buf(error_buf, error_buf_size,
  10515. "function type mismatch");
  10516. goto fail;
  10517. }
  10518. func_type = (WASMFuncType *)module->types[type_idx];
  10519. }
  10520. else
  10521. #endif
  10522. {
  10523. read_leb_uint32(p, p_end, func_idx);
  10524. #if WASM_ENABLE_FAST_INTERP != 0
  10525. /* we need to emit func_idx before arguments */
  10526. emit_uint32(loader_ctx, func_idx);
  10527. #endif
  10528. if (!check_function_index(module, func_idx, error_buf,
  10529. error_buf_size)) {
  10530. goto fail;
  10531. }
  10532. if (func_idx < module->import_function_count)
  10533. func_type = module->import_functions[func_idx]
  10534. .u.function.func_type;
  10535. else
  10536. func_type =
  10537. module
  10538. ->functions[func_idx
  10539. - module->import_function_count]
  10540. ->func_type;
  10541. }
  10542. if (func_type->param_count > 0) {
  10543. #if WASM_ENABLE_GC != 0
  10544. j = (int32)(func_type->result_ref_type_maps
  10545. - func_type->ref_type_maps - 1);
  10546. #endif
  10547. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10548. idx--) {
  10549. #if WASM_ENABLE_GC != 0
  10550. if (wasm_is_type_multi_byte_type(
  10551. func_type->types[idx])) {
  10552. ref_type = func_type->ref_type_maps[j].ref_type;
  10553. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10554. ref_type,
  10555. wasm_reftype_struct_size(ref_type));
  10556. j--;
  10557. }
  10558. #endif
  10559. #if WASM_ENABLE_FAST_INTERP != 0
  10560. POP_OFFSET_TYPE(func_type->types[idx]);
  10561. #endif
  10562. POP_TYPE(func_type->types[idx]);
  10563. }
  10564. }
  10565. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10566. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10567. #endif
  10568. #if WASM_ENABLE_GC != 0
  10569. j = (int32)(func_type->result_ref_type_maps
  10570. - func_type->ref_type_maps);
  10571. #endif
  10572. for (i = 0; i < func_type->result_count; i++) {
  10573. #if WASM_ENABLE_GC != 0
  10574. if (wasm_is_type_multi_byte_type(
  10575. func_type->types[func_type->param_count + i])) {
  10576. ref_type = func_type->ref_type_maps[j].ref_type;
  10577. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10578. ref_type,
  10579. wasm_reftype_struct_size(ref_type));
  10580. j++;
  10581. }
  10582. #endif
  10583. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10584. #if WASM_ENABLE_FAST_INTERP != 0
  10585. /* Here we emit each return value's dynamic_offset. But
  10586. * in fact these offsets are continuous, so interpreter
  10587. * only need to get the first return value's offset.
  10588. */
  10589. PUSH_OFFSET_TYPE(
  10590. func_type->types[func_type->param_count + i]);
  10591. #endif
  10592. }
  10593. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10594. }
  10595. else {
  10596. #if WASM_ENABLE_GC == 0
  10597. if (func_type->result_count
  10598. != func->func_type->result_count) {
  10599. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10600. "type mismatch: expect ",
  10601. func->func_type->result_count,
  10602. " return values but got other");
  10603. goto fail;
  10604. }
  10605. for (i = 0; i < func_type->result_count; i++) {
  10606. type = func->func_type
  10607. ->types[func->func_type->param_count + i];
  10608. if (func_type->types[func_type->param_count + i]
  10609. != type) {
  10610. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10611. "type mismatch: expect ",
  10612. type2str(type), " but got other");
  10613. goto fail;
  10614. }
  10615. }
  10616. #else
  10617. if (!wasm_func_type_result_is_subtype_of(
  10618. func_type, func->func_type, module->types,
  10619. module->type_count)) {
  10620. set_error_buf(
  10621. error_buf, error_buf_size,
  10622. "type mismatch: invalid func result types");
  10623. goto fail;
  10624. }
  10625. #endif
  10626. RESET_STACK();
  10627. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10628. }
  10629. #endif
  10630. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10631. || WASM_ENABLE_WAMR_COMPILER != 0
  10632. func->has_op_func_call = true;
  10633. #endif
  10634. (void)type;
  10635. break;
  10636. }
  10637. /*
  10638. * if disable reference type: call_indirect typeidx, 0x00
  10639. * if enable reference type: call_indirect typeidx, tableidx
  10640. */
  10641. case WASM_OP_CALL_INDIRECT:
  10642. #if WASM_ENABLE_TAIL_CALL != 0
  10643. case WASM_OP_RETURN_CALL_INDIRECT:
  10644. #endif
  10645. {
  10646. int32 idx;
  10647. WASMFuncType *func_type;
  10648. read_leb_uint32(p, p_end, type_idx);
  10649. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10650. read_leb_uint32(p, p_end, table_idx);
  10651. #else
  10652. CHECK_BUF(p, p_end, 1);
  10653. table_idx = read_uint8(p);
  10654. #endif
  10655. if (!check_table_index(module, table_idx, error_buf,
  10656. error_buf_size)) {
  10657. goto fail;
  10658. }
  10659. #if WASM_ENABLE_FAST_INTERP != 0
  10660. /* we need to emit before arguments */
  10661. #if WASM_ENABLE_TAIL_CALL != 0
  10662. emit_byte(loader_ctx, opcode);
  10663. #endif
  10664. emit_uint32(loader_ctx, type_idx);
  10665. emit_uint32(loader_ctx, table_idx);
  10666. #endif
  10667. /* skip elem idx */
  10668. POP_I32();
  10669. if (type_idx >= module->type_count) {
  10670. set_error_buf(error_buf, error_buf_size, "unknown type");
  10671. goto fail;
  10672. }
  10673. func_type = (WASMFuncType *)module->types[type_idx];
  10674. if (func_type->param_count > 0) {
  10675. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10676. idx--) {
  10677. #if WASM_ENABLE_FAST_INTERP != 0
  10678. POP_OFFSET_TYPE(func_type->types[idx]);
  10679. #endif
  10680. POP_TYPE(func_type->types[idx]);
  10681. }
  10682. }
  10683. #if WASM_ENABLE_TAIL_CALL != 0
  10684. if (opcode == WASM_OP_CALL_INDIRECT) {
  10685. #endif
  10686. for (i = 0; i < func_type->result_count; i++) {
  10687. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10688. #if WASM_ENABLE_FAST_INTERP != 0
  10689. PUSH_OFFSET_TYPE(
  10690. func_type->types[func_type->param_count + i]);
  10691. #endif
  10692. }
  10693. #if WASM_ENABLE_TAIL_CALL != 0
  10694. }
  10695. else {
  10696. uint8 type;
  10697. if (func_type->result_count
  10698. != func->func_type->result_count) {
  10699. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10700. "type mismatch: expect ",
  10701. func->func_type->result_count,
  10702. " return values but got other");
  10703. goto fail;
  10704. }
  10705. for (i = 0; i < func_type->result_count; i++) {
  10706. type = func->func_type
  10707. ->types[func->func_type->param_count + i];
  10708. if (func_type->types[func_type->param_count + i]
  10709. != type) {
  10710. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10711. "type mismatch: expect ",
  10712. type2str(type), " but got other");
  10713. goto fail;
  10714. }
  10715. }
  10716. RESET_STACK();
  10717. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10718. }
  10719. #endif
  10720. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10721. || WASM_ENABLE_WAMR_COMPILER != 0
  10722. func->has_op_func_call = true;
  10723. #endif
  10724. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10725. func->has_op_call_indirect = true;
  10726. #endif
  10727. break;
  10728. }
  10729. case WASM_OP_DROP:
  10730. {
  10731. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10732. int32 available_stack_cell =
  10733. (int32)(loader_ctx->stack_cell_num
  10734. - cur_block->stack_cell_num);
  10735. if (available_stack_cell <= 0
  10736. && !cur_block->is_stack_polymorphic) {
  10737. set_error_buf(error_buf, error_buf_size,
  10738. "type mismatch, opcode drop was found "
  10739. "but stack was empty");
  10740. goto fail;
  10741. }
  10742. if (available_stack_cell > 0) {
  10743. #if WASM_ENABLE_GC != 0
  10744. if (wasm_is_type_multi_byte_type(
  10745. *(loader_ctx->frame_ref - 1))) {
  10746. bh_assert((int32)(loader_ctx->reftype_map_num
  10747. - cur_block->reftype_map_num)
  10748. > 0);
  10749. loader_ctx->frame_reftype_map--;
  10750. loader_ctx->reftype_map_num--;
  10751. }
  10752. #endif
  10753. if (is_32bit_type(*(loader_ctx->frame_ref - 1))
  10754. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  10755. loader_ctx->frame_ref--;
  10756. loader_ctx->stack_cell_num--;
  10757. #if WASM_ENABLE_FAST_INTERP != 0
  10758. skip_label();
  10759. loader_ctx->frame_offset--;
  10760. if ((*(loader_ctx->frame_offset)
  10761. > loader_ctx->start_dynamic_offset)
  10762. && (*(loader_ctx->frame_offset)
  10763. < loader_ctx->max_dynamic_offset))
  10764. loader_ctx->dynamic_offset--;
  10765. #endif
  10766. }
  10767. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10768. loader_ctx->frame_ref -= 2;
  10769. loader_ctx->stack_cell_num -= 2;
  10770. #if WASM_ENABLE_FAST_INTERP == 0
  10771. *(p - 1) = WASM_OP_DROP_64;
  10772. #endif
  10773. #if WASM_ENABLE_FAST_INTERP != 0
  10774. skip_label();
  10775. loader_ctx->frame_offset -= 2;
  10776. if ((*(loader_ctx->frame_offset)
  10777. > loader_ctx->start_dynamic_offset)
  10778. && (*(loader_ctx->frame_offset)
  10779. < loader_ctx->max_dynamic_offset))
  10780. loader_ctx->dynamic_offset -= 2;
  10781. #endif
  10782. }
  10783. #if WASM_ENABLE_SIMD != 0
  10784. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10785. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10786. loader_ctx->frame_ref -= 4;
  10787. loader_ctx->stack_cell_num -= 4;
  10788. }
  10789. #endif
  10790. #endif
  10791. else {
  10792. set_error_buf(error_buf, error_buf_size,
  10793. "type mismatch");
  10794. goto fail;
  10795. }
  10796. }
  10797. else {
  10798. #if WASM_ENABLE_FAST_INTERP != 0
  10799. skip_label();
  10800. #endif
  10801. }
  10802. break;
  10803. }
  10804. case WASM_OP_SELECT:
  10805. {
  10806. uint8 ref_type;
  10807. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10808. int32 available_stack_cell;
  10809. #if WASM_ENABLE_FAST_INTERP != 0
  10810. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10811. #endif
  10812. POP_I32();
  10813. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10814. - cur_block->stack_cell_num);
  10815. if (available_stack_cell <= 0
  10816. && !cur_block->is_stack_polymorphic) {
  10817. set_error_buf(error_buf, error_buf_size,
  10818. "type mismatch or invalid result arity, "
  10819. "opcode select was found "
  10820. "but stack was empty");
  10821. goto fail;
  10822. }
  10823. if (available_stack_cell > 0) {
  10824. switch (*(loader_ctx->frame_ref - 1)) {
  10825. case VALUE_TYPE_I32:
  10826. case VALUE_TYPE_F32:
  10827. break;
  10828. case VALUE_TYPE_I64:
  10829. case VALUE_TYPE_F64:
  10830. #if WASM_ENABLE_FAST_INTERP == 0
  10831. *(p - 1) = WASM_OP_SELECT_64;
  10832. #endif
  10833. #if WASM_ENABLE_FAST_INTERP != 0
  10834. if (loader_ctx->p_code_compiled) {
  10835. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10836. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10837. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10838. *(void **)(p_code_compiled_tmp
  10839. - sizeof(void *)) =
  10840. handle_table[opcode_tmp];
  10841. #else
  10842. #if UINTPTR_MAX == UINT64_MAX
  10843. /* emit int32 relative offset in 64-bit target
  10844. */
  10845. int32 offset =
  10846. (int32)((uint8 *)handle_table[opcode_tmp]
  10847. - (uint8 *)handle_table[0]);
  10848. *(int32 *)(p_code_compiled_tmp
  10849. - sizeof(int32)) = offset;
  10850. #else
  10851. /* emit uint32 label address in 32-bit target */
  10852. *(uint32 *)(p_code_compiled_tmp
  10853. - sizeof(uint32)) =
  10854. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10855. #endif
  10856. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10857. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10858. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10859. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10860. #else
  10861. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10862. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10863. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10864. }
  10865. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10866. break;
  10867. #if WASM_ENABLE_SIMD != 0
  10868. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10869. case VALUE_TYPE_V128:
  10870. break;
  10871. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10872. #endif /* WASM_ENABLE_SIMD != 0 */
  10873. default:
  10874. {
  10875. set_error_buf(error_buf, error_buf_size,
  10876. "type mismatch");
  10877. goto fail;
  10878. }
  10879. }
  10880. ref_type = *(loader_ctx->frame_ref - 1);
  10881. #if WASM_ENABLE_FAST_INTERP != 0
  10882. POP_OFFSET_TYPE(ref_type);
  10883. POP_TYPE(ref_type);
  10884. POP_OFFSET_TYPE(ref_type);
  10885. POP_TYPE(ref_type);
  10886. PUSH_OFFSET_TYPE(ref_type);
  10887. PUSH_TYPE(ref_type);
  10888. #else
  10889. POP2_AND_PUSH(ref_type, ref_type);
  10890. #endif
  10891. }
  10892. else {
  10893. #if WASM_ENABLE_FAST_INTERP != 0
  10894. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  10895. #endif
  10896. PUSH_TYPE(VALUE_TYPE_ANY);
  10897. }
  10898. break;
  10899. }
  10900. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10901. case WASM_OP_SELECT_T:
  10902. {
  10903. uint8 vec_len, type;
  10904. #if WASM_ENABLE_GC != 0
  10905. WASMRefType *ref_type = NULL;
  10906. #endif
  10907. #if WASM_ENABLE_FAST_INTERP != 0
  10908. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10909. #endif
  10910. read_leb_uint32(p, p_end, vec_len);
  10911. if (vec_len != 1) {
  10912. /* typed select must have exactly one result */
  10913. set_error_buf(error_buf, error_buf_size,
  10914. "invalid result arity");
  10915. goto fail;
  10916. }
  10917. #if WASM_ENABLE_GC == 0
  10918. CHECK_BUF(p, p_end, 1);
  10919. type = read_uint8(p);
  10920. if (!is_value_type(type)) {
  10921. set_error_buf(error_buf, error_buf_size,
  10922. "unknown value type");
  10923. goto fail;
  10924. }
  10925. #else
  10926. p_org = p + 1;
  10927. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  10928. module->type_count, &need_ref_type_map,
  10929. &wasm_ref_type, false, error_buf,
  10930. error_buf_size)) {
  10931. goto fail;
  10932. }
  10933. type = wasm_ref_type.ref_type;
  10934. if (need_ref_type_map) {
  10935. if (!(ref_type = reftype_set_insert(
  10936. module->ref_type_set, &wasm_ref_type, error_buf,
  10937. error_buf_size))) {
  10938. goto fail;
  10939. }
  10940. }
  10941. #if WASM_ENABLE_FAST_INTERP == 0
  10942. while (p_org < p) {
  10943. #if WASM_ENABLE_DEBUG_INTERP != 0
  10944. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10945. error_buf_size)) {
  10946. goto fail;
  10947. }
  10948. #endif
  10949. /* Ignore extra bytes for interpreter */
  10950. *p_org++ = WASM_OP_NOP;
  10951. }
  10952. #endif
  10953. #endif /* end of WASM_ENABLE_GC == 0 */
  10954. POP_I32();
  10955. #if WASM_ENABLE_FAST_INTERP != 0
  10956. if (loader_ctx->p_code_compiled) {
  10957. uint8 opcode_tmp = WASM_OP_SELECT;
  10958. if (type == VALUE_TYPE_V128) {
  10959. #if (WASM_ENABLE_SIMD == 0) \
  10960. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  10961. set_error_buf(error_buf, error_buf_size,
  10962. "SIMD v128 type isn't supported");
  10963. goto fail;
  10964. #endif
  10965. }
  10966. else {
  10967. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  10968. opcode_tmp = WASM_OP_SELECT_64;
  10969. #if WASM_ENABLE_GC != 0
  10970. if (wasm_is_type_reftype(type))
  10971. opcode_tmp = WASM_OP_SELECT_T;
  10972. #endif
  10973. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10974. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10975. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  10976. handle_table[opcode_tmp];
  10977. #else
  10978. #if UINTPTR_MAX == UINT64_MAX
  10979. /* emit int32 relative offset in 64-bit target */
  10980. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  10981. - (uint8 *)handle_table[0]);
  10982. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  10983. offset;
  10984. #else
  10985. /* emit uint32 label address in 32-bit target */
  10986. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  10987. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10988. #endif
  10989. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10990. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10991. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10992. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10993. #else
  10994. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10995. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10996. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10997. }
  10998. }
  10999. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  11000. POP_REF(type);
  11001. #if WASM_ENABLE_GC != 0
  11002. if (need_ref_type_map) {
  11003. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11004. wasm_reftype_struct_size(ref_type));
  11005. }
  11006. #endif
  11007. POP_REF(type);
  11008. #if WASM_ENABLE_GC != 0
  11009. if (need_ref_type_map) {
  11010. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11011. wasm_reftype_struct_size(ref_type));
  11012. }
  11013. #endif
  11014. PUSH_REF(type);
  11015. #if WASM_ENABLE_WAMR_COMPILER != 0
  11016. module->is_ref_types_used = true;
  11017. #endif
  11018. (void)vec_len;
  11019. break;
  11020. }
  11021. /* table.get x. tables[x]. [i32] -> [t] */
  11022. /* table.set x. tables[x]. [i32 t] -> [] */
  11023. case WASM_OP_TABLE_GET:
  11024. case WASM_OP_TABLE_SET:
  11025. {
  11026. uint8 decl_ref_type;
  11027. #if WASM_ENABLE_GC != 0
  11028. WASMRefType *ref_type;
  11029. #endif
  11030. read_leb_uint32(p, p_end, table_idx);
  11031. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11032. #if WASM_ENABLE_GC != 0
  11033. (void **)&ref_type,
  11034. #else
  11035. NULL,
  11036. #endif
  11037. error_buf, error_buf_size))
  11038. goto fail;
  11039. #if WASM_ENABLE_GC != 0
  11040. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11041. bh_assert(ref_type);
  11042. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11043. ref_type, wasm_reftype_struct_size(ref_type));
  11044. }
  11045. #endif
  11046. #if WASM_ENABLE_FAST_INTERP != 0
  11047. emit_uint32(loader_ctx, table_idx);
  11048. #endif
  11049. if (opcode == WASM_OP_TABLE_GET) {
  11050. POP_I32();
  11051. #if WASM_ENABLE_FAST_INTERP != 0
  11052. PUSH_OFFSET_TYPE(decl_ref_type);
  11053. #endif
  11054. PUSH_TYPE(decl_ref_type);
  11055. }
  11056. else {
  11057. #if WASM_ENABLE_FAST_INTERP != 0
  11058. POP_OFFSET_TYPE(decl_ref_type);
  11059. #endif
  11060. POP_TYPE(decl_ref_type);
  11061. POP_I32();
  11062. }
  11063. #if WASM_ENABLE_WAMR_COMPILER != 0
  11064. module->is_ref_types_used = true;
  11065. #endif
  11066. break;
  11067. }
  11068. case WASM_OP_REF_NULL:
  11069. {
  11070. uint8 ref_type;
  11071. #if WASM_ENABLE_GC == 0
  11072. CHECK_BUF(p, p_end, 1);
  11073. ref_type = read_uint8(p);
  11074. if (ref_type != VALUE_TYPE_FUNCREF
  11075. && ref_type != VALUE_TYPE_EXTERNREF) {
  11076. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11077. goto fail;
  11078. }
  11079. #else
  11080. read_leb_int32(p, p_end, heap_type);
  11081. if (heap_type >= 0) {
  11082. if (!check_type_index(module, module->type_count, heap_type,
  11083. error_buf, error_buf_size)) {
  11084. goto fail;
  11085. }
  11086. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11087. true, heap_type);
  11088. ref_type = wasm_ref_type.ref_type;
  11089. }
  11090. else {
  11091. if (!wasm_is_valid_heap_type(heap_type)) {
  11092. set_error_buf(error_buf, error_buf_size,
  11093. "unknown type");
  11094. goto fail;
  11095. }
  11096. ref_type = (uint8)((int32)0x80 + heap_type);
  11097. }
  11098. #endif /* end of WASM_ENABLE_GC == 0 */
  11099. #if WASM_ENABLE_FAST_INTERP != 0
  11100. PUSH_OFFSET_TYPE(ref_type);
  11101. #endif
  11102. PUSH_TYPE(ref_type);
  11103. #if WASM_ENABLE_WAMR_COMPILER != 0
  11104. module->is_ref_types_used = true;
  11105. #endif
  11106. break;
  11107. }
  11108. case WASM_OP_REF_IS_NULL:
  11109. {
  11110. #if WASM_ENABLE_GC == 0
  11111. #if WASM_ENABLE_FAST_INTERP != 0
  11112. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11113. int32 block_stack_cell_num =
  11114. (int32)(loader_ctx->stack_cell_num
  11115. - cur_block->stack_cell_num);
  11116. if (block_stack_cell_num <= 0) {
  11117. if (!cur_block->is_stack_polymorphic) {
  11118. set_error_buf(
  11119. error_buf, error_buf_size,
  11120. "type mismatch: expect data but stack was empty");
  11121. goto fail;
  11122. }
  11123. }
  11124. else {
  11125. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11126. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11127. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11128. if (!wasm_loader_pop_frame_ref_offset(
  11129. loader_ctx, *(loader_ctx->frame_ref - 1),
  11130. error_buf, error_buf_size)) {
  11131. goto fail;
  11132. }
  11133. }
  11134. else {
  11135. set_error_buf(error_buf, error_buf_size,
  11136. "type mismatch");
  11137. goto fail;
  11138. }
  11139. }
  11140. #else
  11141. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11142. error_buf, error_buf_size)
  11143. && !wasm_loader_pop_frame_ref(loader_ctx,
  11144. VALUE_TYPE_EXTERNREF,
  11145. error_buf, error_buf_size)) {
  11146. goto fail;
  11147. }
  11148. #endif
  11149. #else /* else of WASM_ENABLE_GC == 0 */
  11150. uint8 type;
  11151. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11152. error_buf, error_buf_size)) {
  11153. goto fail;
  11154. }
  11155. #endif
  11156. PUSH_I32();
  11157. #if WASM_ENABLE_WAMR_COMPILER != 0
  11158. module->is_ref_types_used = true;
  11159. #endif
  11160. break;
  11161. }
  11162. case WASM_OP_REF_FUNC:
  11163. {
  11164. read_leb_uint32(p, p_end, func_idx);
  11165. if (!check_function_index(module, func_idx, error_buf,
  11166. error_buf_size)) {
  11167. goto fail;
  11168. }
  11169. /* Refer to a forward-declared function:
  11170. the function must be an import, exported, or present in
  11171. a table elem segment or global initializer to be used as
  11172. the operand to ref.func */
  11173. if (func_idx >= module->import_function_count) {
  11174. WASMTableSeg *table_seg = module->table_segments;
  11175. bool func_declared = false;
  11176. uint32 j;
  11177. for (i = 0; i < module->global_count; i++) {
  11178. if (module->globals[i].type == VALUE_TYPE_FUNCREF
  11179. && module->globals[i].init_expr.init_expr_type
  11180. == INIT_EXPR_TYPE_FUNCREF_CONST
  11181. && module->globals[i].init_expr.u.u32 == func_idx) {
  11182. func_declared = true;
  11183. break;
  11184. }
  11185. }
  11186. if (!func_declared) {
  11187. /* Check whether the function is declared in table segs,
  11188. note that it doesn't matter whether the table seg's
  11189. mode is passive, active or declarative. */
  11190. for (i = 0; i < module->table_seg_count;
  11191. i++, table_seg++) {
  11192. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11193. #if WASM_ENABLE_GC != 0
  11194. /* elem type is (ref null? func) or
  11195. (ref null? $t) */
  11196. || ((table_seg->elem_type
  11197. == REF_TYPE_HT_NON_NULLABLE
  11198. || table_seg->elem_type
  11199. == REF_TYPE_HT_NULLABLE)
  11200. && (table_seg->elem_ref_type->ref_ht_common
  11201. .heap_type
  11202. == HEAP_TYPE_FUNC
  11203. || table_seg->elem_ref_type
  11204. ->ref_ht_common.heap_type
  11205. > 0))
  11206. #endif
  11207. ) {
  11208. for (j = 0; j < table_seg->value_count; j++) {
  11209. if (table_seg->init_values[j].u.ref_index
  11210. == func_idx) {
  11211. func_declared = true;
  11212. break;
  11213. }
  11214. }
  11215. }
  11216. }
  11217. }
  11218. if (!func_declared) {
  11219. /* Check whether the function is exported */
  11220. for (i = 0; i < module->export_count; i++) {
  11221. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11222. && module->exports[i].index == func_idx) {
  11223. func_declared = true;
  11224. break;
  11225. }
  11226. }
  11227. }
  11228. if (!func_declared) {
  11229. set_error_buf(error_buf, error_buf_size,
  11230. "undeclared function reference");
  11231. goto fail;
  11232. }
  11233. }
  11234. #if WASM_ENABLE_FAST_INTERP != 0
  11235. emit_uint32(loader_ctx, func_idx);
  11236. #endif
  11237. #if WASM_ENABLE_GC == 0
  11238. PUSH_FUNCREF();
  11239. #else
  11240. if (func_idx < module->import_function_count)
  11241. type_idx =
  11242. module->import_functions[func_idx].u.function.type_idx;
  11243. else
  11244. type_idx = module
  11245. ->functions[func_idx
  11246. - module->import_function_count]
  11247. ->type_idx;
  11248. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11249. false, type_idx);
  11250. PUSH_REF(wasm_ref_type.ref_type);
  11251. #endif
  11252. #if WASM_ENABLE_WAMR_COMPILER != 0
  11253. module->is_ref_types_used = true;
  11254. #endif
  11255. break;
  11256. }
  11257. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11258. #if WASM_ENABLE_GC != 0
  11259. case WASM_OP_REF_AS_NON_NULL:
  11260. case WASM_OP_BR_ON_NULL:
  11261. {
  11262. uint8 type;
  11263. WASMRefType ref_type;
  11264. /* POP (ref null ht) and get the converted (ref ht) */
  11265. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11266. error_buf, error_buf_size)) {
  11267. goto fail;
  11268. }
  11269. if (opcode == WASM_OP_BR_ON_NULL) {
  11270. if (!(frame_csp_tmp =
  11271. check_branch_block(loader_ctx, &p, p_end, opcode,
  11272. error_buf, error_buf_size))) {
  11273. goto fail;
  11274. }
  11275. #if WASM_ENABLE_FAST_INTERP != 0
  11276. disable_emit = true;
  11277. #endif
  11278. }
  11279. /* PUSH the converted (ref ht) */
  11280. if (type != VALUE_TYPE_ANY) {
  11281. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11282. sizeof(WASMRefType));
  11283. }
  11284. PUSH_REF(type);
  11285. break;
  11286. }
  11287. case WASM_OP_BR_ON_NON_NULL:
  11288. {
  11289. uint8 type;
  11290. WASMRefType ref_type;
  11291. uint32 available_stack_cell =
  11292. loader_ctx->stack_cell_num
  11293. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11294. /* POP (ref null ht) and get the converted (ref ht) */
  11295. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11296. error_buf, error_buf_size)) {
  11297. goto fail;
  11298. }
  11299. #if WASM_ENABLE_FAST_INTERP != 0
  11300. disable_emit = true;
  11301. #endif
  11302. /* Temporarily PUSH back (ref ht), check brach block and
  11303. then POP it */
  11304. if (available_stack_cell
  11305. > 0) { /* stack isn't in polymorphic state */
  11306. if (type != VALUE_TYPE_ANY) {
  11307. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11308. &ref_type, sizeof(WASMRefType));
  11309. }
  11310. PUSH_REF(type);
  11311. }
  11312. if (!(frame_csp_tmp =
  11313. check_branch_block(loader_ctx, &p, p_end, opcode,
  11314. error_buf, error_buf_size))) {
  11315. goto fail;
  11316. }
  11317. if (available_stack_cell
  11318. > 0) { /* stack isn't in polymorphic state */
  11319. POP_REF(type);
  11320. #if WASM_ENABLE_FAST_INTERP != 0
  11321. /* Erase the opnd offset emitted by POP_REF() */
  11322. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11323. #endif
  11324. }
  11325. break;
  11326. }
  11327. case WASM_OP_REF_EQ:
  11328. POP_REF(REF_TYPE_EQREF);
  11329. POP_REF(REF_TYPE_EQREF);
  11330. PUSH_I32();
  11331. break;
  11332. #endif /* end of WASM_ENABLE_GC != 0 */
  11333. case WASM_OP_GET_LOCAL:
  11334. {
  11335. p_org = p - 1;
  11336. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11337. PUSH_TYPE(local_type);
  11338. #if WASM_ENABLE_GC != 0
  11339. /* Cannot get a non-nullable and unset local */
  11340. if (local_idx >= param_count
  11341. && wasm_is_reftype_htref_non_nullable(local_type)
  11342. && !wasm_loader_get_local_status(loader_ctx,
  11343. local_idx - param_count)) {
  11344. set_error_buf(error_buf, error_buf_size,
  11345. "uninitialized local");
  11346. goto fail;
  11347. }
  11348. #endif
  11349. #if WASM_ENABLE_FAST_INTERP != 0
  11350. /* Get Local is optimized out */
  11351. skip_label();
  11352. disable_emit = true;
  11353. operand_offset = local_offset;
  11354. PUSH_OFFSET_TYPE(local_type);
  11355. #else
  11356. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11357. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11358. if (local_offset < 0x80
  11359. #if WASM_ENABLE_GC != 0
  11360. && !wasm_is_type_reftype(local_type)
  11361. #endif
  11362. ) {
  11363. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11364. if (is_32bit_type(local_type)) {
  11365. *p_org++ = (uint8)local_offset;
  11366. }
  11367. else {
  11368. *p_org++ = (uint8)(local_offset | 0x80);
  11369. }
  11370. while (p_org < p) {
  11371. *p_org++ = WASM_OP_NOP;
  11372. }
  11373. }
  11374. #endif
  11375. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11376. break;
  11377. }
  11378. case WASM_OP_SET_LOCAL:
  11379. {
  11380. p_org = p - 1;
  11381. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11382. #if WASM_ENABLE_FAST_INTERP != 0
  11383. if (!(preserve_referenced_local(
  11384. loader_ctx, opcode, local_offset, local_type,
  11385. &preserve_local, error_buf, error_buf_size)))
  11386. goto fail;
  11387. if (local_offset < 256
  11388. #if WASM_ENABLE_GC != 0
  11389. && !wasm_is_type_reftype(local_type)
  11390. #endif
  11391. ) {
  11392. skip_label();
  11393. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11394. if (loader_ctx->p_code_compiled)
  11395. STORE_U16(loader_ctx->p_code_compiled - 2,
  11396. local_offset);
  11397. loader_ctx->frame_offset--;
  11398. loader_ctx->dynamic_offset--;
  11399. }
  11400. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11401. if (loader_ctx->p_code_compiled)
  11402. STORE_U16(loader_ctx->p_code_compiled - 2,
  11403. local_offset);
  11404. loader_ctx->frame_offset -= 2;
  11405. loader_ctx->dynamic_offset -= 2;
  11406. }
  11407. else {
  11408. if (is_32bit_type(local_type)) {
  11409. emit_label(EXT_OP_SET_LOCAL_FAST);
  11410. emit_byte(loader_ctx, (uint8)local_offset);
  11411. }
  11412. else {
  11413. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11414. emit_byte(loader_ctx, (uint8)local_offset);
  11415. }
  11416. POP_OFFSET_TYPE(local_type);
  11417. }
  11418. }
  11419. else { /* local index larger than 255, reserve leb */
  11420. emit_uint32(loader_ctx, local_idx);
  11421. POP_OFFSET_TYPE(local_type);
  11422. }
  11423. #else
  11424. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11425. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11426. if (local_offset < 0x80
  11427. #if WASM_ENABLE_GC != 0
  11428. && !wasm_is_type_reftype(local_type)
  11429. #endif
  11430. ) {
  11431. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11432. if (is_32bit_type(local_type)) {
  11433. *p_org++ = (uint8)local_offset;
  11434. }
  11435. else {
  11436. *p_org++ = (uint8)(local_offset | 0x80);
  11437. }
  11438. while (p_org < p) {
  11439. *p_org++ = WASM_OP_NOP;
  11440. }
  11441. }
  11442. #endif
  11443. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11444. #if WASM_ENABLE_GC != 0
  11445. if (local_idx >= param_count) {
  11446. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11447. }
  11448. #endif
  11449. POP_TYPE(local_type);
  11450. break;
  11451. }
  11452. case WASM_OP_TEE_LOCAL:
  11453. {
  11454. p_org = p - 1;
  11455. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11456. #if WASM_ENABLE_FAST_INTERP != 0
  11457. /* If the stack is in polymorphic state, do fake pop and push on
  11458. offset stack to keep the depth of offset stack to be the
  11459. same with ref stack */
  11460. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11461. if (cur_block->is_stack_polymorphic) {
  11462. POP_OFFSET_TYPE(local_type);
  11463. PUSH_OFFSET_TYPE(local_type);
  11464. }
  11465. #endif
  11466. POP_TYPE(local_type);
  11467. PUSH_TYPE(local_type);
  11468. #if WASM_ENABLE_FAST_INTERP != 0
  11469. if (!(preserve_referenced_local(
  11470. loader_ctx, opcode, local_offset, local_type,
  11471. &preserve_local, error_buf, error_buf_size)))
  11472. goto fail;
  11473. if (local_offset < 256
  11474. #if WASM_ENABLE_GC != 0
  11475. && !wasm_is_type_reftype(local_type)
  11476. #endif
  11477. ) {
  11478. skip_label();
  11479. if (is_32bit_type(local_type)) {
  11480. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11481. emit_byte(loader_ctx, (uint8)local_offset);
  11482. }
  11483. else {
  11484. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11485. emit_byte(loader_ctx, (uint8)local_offset);
  11486. }
  11487. }
  11488. else { /* local index larger than 255, reserve leb */
  11489. emit_uint32(loader_ctx, local_idx);
  11490. }
  11491. emit_operand(loader_ctx,
  11492. *(loader_ctx->frame_offset
  11493. - wasm_value_type_cell_num(local_type)));
  11494. #else
  11495. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11496. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11497. if (local_offset < 0x80
  11498. #if WASM_ENABLE_GC != 0
  11499. && !wasm_is_type_reftype(local_type)
  11500. #endif
  11501. ) {
  11502. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11503. if (is_32bit_type(local_type)) {
  11504. *p_org++ = (uint8)local_offset;
  11505. }
  11506. else {
  11507. *p_org++ = (uint8)(local_offset | 0x80);
  11508. }
  11509. while (p_org < p) {
  11510. *p_org++ = WASM_OP_NOP;
  11511. }
  11512. }
  11513. #endif
  11514. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11515. #if WASM_ENABLE_GC != 0
  11516. if (local_idx >= param_count) {
  11517. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11518. }
  11519. #endif
  11520. break;
  11521. }
  11522. case WASM_OP_GET_GLOBAL:
  11523. {
  11524. #if WASM_ENABLE_GC != 0
  11525. WASMRefType *ref_type;
  11526. #endif
  11527. p_org = p - 1;
  11528. read_leb_uint32(p, p_end, global_idx);
  11529. if (global_idx >= global_count) {
  11530. set_error_buf(error_buf, error_buf_size, "unknown global");
  11531. goto fail;
  11532. }
  11533. global_type =
  11534. global_idx < module->import_global_count
  11535. ? module->import_globals[global_idx].u.global.type
  11536. : module
  11537. ->globals[global_idx
  11538. - module->import_global_count]
  11539. .type;
  11540. #if WASM_ENABLE_GC != 0
  11541. ref_type =
  11542. global_idx < module->import_global_count
  11543. ? module->import_globals[global_idx].u.global.ref_type
  11544. : module
  11545. ->globals[global_idx
  11546. - module->import_global_count]
  11547. .ref_type;
  11548. if (wasm_is_type_multi_byte_type(global_type)) {
  11549. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11550. wasm_reftype_struct_size(ref_type));
  11551. }
  11552. #endif
  11553. PUSH_TYPE(global_type);
  11554. #if WASM_ENABLE_FAST_INTERP == 0
  11555. if (global_type == VALUE_TYPE_I64
  11556. || global_type == VALUE_TYPE_F64) {
  11557. #if WASM_ENABLE_DEBUG_INTERP != 0
  11558. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11559. error_buf_size)) {
  11560. goto fail;
  11561. }
  11562. #endif
  11563. *p_org = WASM_OP_GET_GLOBAL_64;
  11564. }
  11565. #else /* else of WASM_ENABLE_FAST_INTERP */
  11566. if (global_type == VALUE_TYPE_I64
  11567. || global_type == VALUE_TYPE_F64) {
  11568. skip_label();
  11569. emit_label(WASM_OP_GET_GLOBAL_64);
  11570. }
  11571. emit_uint32(loader_ctx, global_idx);
  11572. PUSH_OFFSET_TYPE(global_type);
  11573. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11574. break;
  11575. }
  11576. case WASM_OP_SET_GLOBAL:
  11577. {
  11578. bool is_mutable = false;
  11579. #if WASM_ENABLE_GC != 0
  11580. WASMRefType *ref_type;
  11581. #endif
  11582. p_org = p - 1;
  11583. read_leb_uint32(p, p_end, global_idx);
  11584. if (global_idx >= global_count) {
  11585. set_error_buf(error_buf, error_buf_size, "unknown global");
  11586. goto fail;
  11587. }
  11588. is_mutable =
  11589. global_idx < module->import_global_count
  11590. ? module->import_globals[global_idx].u.global.is_mutable
  11591. : module
  11592. ->globals[global_idx
  11593. - module->import_global_count]
  11594. .is_mutable;
  11595. if (!is_mutable) {
  11596. #if WASM_ENABLE_GC == 0
  11597. set_error_buf(error_buf, error_buf_size,
  11598. "global is immutable");
  11599. #else
  11600. set_error_buf(error_buf, error_buf_size,
  11601. "immutable global");
  11602. #endif
  11603. goto fail;
  11604. }
  11605. global_type =
  11606. global_idx < module->import_global_count
  11607. ? module->import_globals[global_idx].u.global.type
  11608. : module
  11609. ->globals[global_idx
  11610. - module->import_global_count]
  11611. .type;
  11612. #if WASM_ENABLE_GC != 0
  11613. ref_type =
  11614. global_idx < module->import_global_count
  11615. ? module->import_globals[global_idx].u.global.ref_type
  11616. : module
  11617. ->globals[global_idx
  11618. - module->import_global_count]
  11619. .ref_type;
  11620. if (wasm_is_type_multi_byte_type(global_type)) {
  11621. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11622. wasm_reftype_struct_size(ref_type));
  11623. }
  11624. #endif
  11625. #if WASM_ENABLE_FAST_INTERP == 0
  11626. if (global_type == VALUE_TYPE_I64
  11627. || global_type == VALUE_TYPE_F64) {
  11628. #if WASM_ENABLE_DEBUG_INTERP != 0
  11629. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11630. error_buf_size)) {
  11631. goto fail;
  11632. }
  11633. #endif
  11634. *p_org = WASM_OP_SET_GLOBAL_64;
  11635. }
  11636. else if (module->aux_stack_size > 0
  11637. && global_idx == module->aux_stack_top_global_index) {
  11638. #if WASM_ENABLE_DEBUG_INTERP != 0
  11639. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11640. error_buf_size)) {
  11641. goto fail;
  11642. }
  11643. #endif
  11644. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11645. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11646. func->has_op_set_global_aux_stack = true;
  11647. #endif
  11648. }
  11649. #else /* else of WASM_ENABLE_FAST_INTERP */
  11650. if (global_type == VALUE_TYPE_I64
  11651. || global_type == VALUE_TYPE_F64) {
  11652. skip_label();
  11653. emit_label(WASM_OP_SET_GLOBAL_64);
  11654. }
  11655. else if (module->aux_stack_size > 0
  11656. && global_idx == module->aux_stack_top_global_index) {
  11657. skip_label();
  11658. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11659. }
  11660. emit_uint32(loader_ctx, global_idx);
  11661. POP_OFFSET_TYPE(global_type);
  11662. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11663. POP_TYPE(global_type);
  11664. break;
  11665. }
  11666. /* load */
  11667. case WASM_OP_I32_LOAD:
  11668. case WASM_OP_I32_LOAD8_S:
  11669. case WASM_OP_I32_LOAD8_U:
  11670. case WASM_OP_I32_LOAD16_S:
  11671. case WASM_OP_I32_LOAD16_U:
  11672. case WASM_OP_I64_LOAD:
  11673. case WASM_OP_I64_LOAD8_S:
  11674. case WASM_OP_I64_LOAD8_U:
  11675. case WASM_OP_I64_LOAD16_S:
  11676. case WASM_OP_I64_LOAD16_U:
  11677. case WASM_OP_I64_LOAD32_S:
  11678. case WASM_OP_I64_LOAD32_U:
  11679. case WASM_OP_F32_LOAD:
  11680. case WASM_OP_F64_LOAD:
  11681. /* store */
  11682. case WASM_OP_I32_STORE:
  11683. case WASM_OP_I32_STORE8:
  11684. case WASM_OP_I32_STORE16:
  11685. case WASM_OP_I64_STORE:
  11686. case WASM_OP_I64_STORE8:
  11687. case WASM_OP_I64_STORE16:
  11688. case WASM_OP_I64_STORE32:
  11689. case WASM_OP_F32_STORE:
  11690. case WASM_OP_F64_STORE:
  11691. {
  11692. #if WASM_ENABLE_FAST_INTERP != 0
  11693. /* change F32/F64 into I32/I64 */
  11694. if (opcode == WASM_OP_F32_LOAD) {
  11695. skip_label();
  11696. emit_label(WASM_OP_I32_LOAD);
  11697. }
  11698. else if (opcode == WASM_OP_F64_LOAD) {
  11699. skip_label();
  11700. emit_label(WASM_OP_I64_LOAD);
  11701. }
  11702. else if (opcode == WASM_OP_F32_STORE) {
  11703. skip_label();
  11704. emit_label(WASM_OP_I32_STORE);
  11705. }
  11706. else if (opcode == WASM_OP_F64_STORE) {
  11707. skip_label();
  11708. emit_label(WASM_OP_I64_STORE);
  11709. }
  11710. #endif
  11711. CHECK_MEMORY();
  11712. read_leb_uint32(p, p_end, align); /* align */
  11713. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11714. if (!check_memory_access_align(opcode, align, error_buf,
  11715. error_buf_size)) {
  11716. goto fail;
  11717. }
  11718. #if WASM_ENABLE_FAST_INTERP != 0
  11719. emit_uint32(loader_ctx, mem_offset);
  11720. #endif
  11721. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11722. func->has_memory_operations = true;
  11723. #endif
  11724. switch (opcode) {
  11725. /* load */
  11726. case WASM_OP_I32_LOAD:
  11727. case WASM_OP_I32_LOAD8_S:
  11728. case WASM_OP_I32_LOAD8_U:
  11729. case WASM_OP_I32_LOAD16_S:
  11730. case WASM_OP_I32_LOAD16_U:
  11731. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11732. break;
  11733. case WASM_OP_I64_LOAD:
  11734. case WASM_OP_I64_LOAD8_S:
  11735. case WASM_OP_I64_LOAD8_U:
  11736. case WASM_OP_I64_LOAD16_S:
  11737. case WASM_OP_I64_LOAD16_U:
  11738. case WASM_OP_I64_LOAD32_S:
  11739. case WASM_OP_I64_LOAD32_U:
  11740. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11741. break;
  11742. case WASM_OP_F32_LOAD:
  11743. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11744. break;
  11745. case WASM_OP_F64_LOAD:
  11746. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11747. break;
  11748. /* store */
  11749. case WASM_OP_I32_STORE:
  11750. case WASM_OP_I32_STORE8:
  11751. case WASM_OP_I32_STORE16:
  11752. POP_I32();
  11753. POP_MEM_OFFSET();
  11754. break;
  11755. case WASM_OP_I64_STORE:
  11756. case WASM_OP_I64_STORE8:
  11757. case WASM_OP_I64_STORE16:
  11758. case WASM_OP_I64_STORE32:
  11759. POP_I64();
  11760. POP_MEM_OFFSET();
  11761. break;
  11762. case WASM_OP_F32_STORE:
  11763. POP_F32();
  11764. POP_MEM_OFFSET();
  11765. break;
  11766. case WASM_OP_F64_STORE:
  11767. POP_F64();
  11768. POP_MEM_OFFSET();
  11769. break;
  11770. default:
  11771. break;
  11772. }
  11773. break;
  11774. }
  11775. case WASM_OP_MEMORY_SIZE:
  11776. CHECK_MEMORY();
  11777. /* reserved byte 0x00 */
  11778. if (*p++ != 0x00) {
  11779. set_error_buf(error_buf, error_buf_size,
  11780. "zero byte expected");
  11781. goto fail;
  11782. }
  11783. PUSH_PAGE_COUNT();
  11784. module->possible_memory_grow = true;
  11785. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11786. func->has_memory_operations = true;
  11787. #endif
  11788. break;
  11789. case WASM_OP_MEMORY_GROW:
  11790. CHECK_MEMORY();
  11791. /* reserved byte 0x00 */
  11792. if (*p++ != 0x00) {
  11793. set_error_buf(error_buf, error_buf_size,
  11794. "zero byte expected");
  11795. goto fail;
  11796. }
  11797. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11798. module->possible_memory_grow = true;
  11799. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11800. || WASM_ENABLE_WAMR_COMPILER != 0
  11801. func->has_op_memory_grow = true;
  11802. #endif
  11803. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11804. func->has_memory_operations = true;
  11805. #endif
  11806. break;
  11807. case WASM_OP_I32_CONST:
  11808. read_leb_int32(p, p_end, i32_const);
  11809. #if WASM_ENABLE_FAST_INTERP != 0
  11810. skip_label();
  11811. disable_emit = true;
  11812. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11813. if (operand_offset == 0) {
  11814. disable_emit = false;
  11815. emit_label(WASM_OP_I32_CONST);
  11816. emit_uint32(loader_ctx, i32_const);
  11817. }
  11818. #else
  11819. (void)i32_const;
  11820. #endif
  11821. PUSH_I32();
  11822. break;
  11823. case WASM_OP_I64_CONST:
  11824. read_leb_int64(p, p_end, i64_const);
  11825. #if WASM_ENABLE_FAST_INTERP != 0
  11826. skip_label();
  11827. disable_emit = true;
  11828. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11829. if (operand_offset == 0) {
  11830. disable_emit = false;
  11831. emit_label(WASM_OP_I64_CONST);
  11832. emit_uint64(loader_ctx, i64_const);
  11833. }
  11834. #endif
  11835. PUSH_I64();
  11836. break;
  11837. case WASM_OP_F32_CONST:
  11838. CHECK_BUF(p, p_end, sizeof(float32));
  11839. p += sizeof(float32);
  11840. #if WASM_ENABLE_FAST_INTERP != 0
  11841. skip_label();
  11842. disable_emit = true;
  11843. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11844. sizeof(float32));
  11845. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11846. if (operand_offset == 0) {
  11847. disable_emit = false;
  11848. emit_label(WASM_OP_F32_CONST);
  11849. emit_float32(loader_ctx, f32_const);
  11850. }
  11851. #endif
  11852. PUSH_F32();
  11853. break;
  11854. case WASM_OP_F64_CONST:
  11855. CHECK_BUF(p, p_end, sizeof(float64));
  11856. p += sizeof(float64);
  11857. #if WASM_ENABLE_FAST_INTERP != 0
  11858. skip_label();
  11859. disable_emit = true;
  11860. /* Some MCU may require 8-byte align */
  11861. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11862. sizeof(float64));
  11863. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11864. if (operand_offset == 0) {
  11865. disable_emit = false;
  11866. emit_label(WASM_OP_F64_CONST);
  11867. emit_float64(loader_ctx, f64_const);
  11868. }
  11869. #endif
  11870. PUSH_F64();
  11871. break;
  11872. case WASM_OP_I32_EQZ:
  11873. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11874. break;
  11875. case WASM_OP_I32_EQ:
  11876. case WASM_OP_I32_NE:
  11877. case WASM_OP_I32_LT_S:
  11878. case WASM_OP_I32_LT_U:
  11879. case WASM_OP_I32_GT_S:
  11880. case WASM_OP_I32_GT_U:
  11881. case WASM_OP_I32_LE_S:
  11882. case WASM_OP_I32_LE_U:
  11883. case WASM_OP_I32_GE_S:
  11884. case WASM_OP_I32_GE_U:
  11885. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11886. break;
  11887. case WASM_OP_I64_EQZ:
  11888. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11889. break;
  11890. case WASM_OP_I64_EQ:
  11891. case WASM_OP_I64_NE:
  11892. case WASM_OP_I64_LT_S:
  11893. case WASM_OP_I64_LT_U:
  11894. case WASM_OP_I64_GT_S:
  11895. case WASM_OP_I64_GT_U:
  11896. case WASM_OP_I64_LE_S:
  11897. case WASM_OP_I64_LE_U:
  11898. case WASM_OP_I64_GE_S:
  11899. case WASM_OP_I64_GE_U:
  11900. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11901. break;
  11902. case WASM_OP_F32_EQ:
  11903. case WASM_OP_F32_NE:
  11904. case WASM_OP_F32_LT:
  11905. case WASM_OP_F32_GT:
  11906. case WASM_OP_F32_LE:
  11907. case WASM_OP_F32_GE:
  11908. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11909. break;
  11910. case WASM_OP_F64_EQ:
  11911. case WASM_OP_F64_NE:
  11912. case WASM_OP_F64_LT:
  11913. case WASM_OP_F64_GT:
  11914. case WASM_OP_F64_LE:
  11915. case WASM_OP_F64_GE:
  11916. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11917. break;
  11918. case WASM_OP_I32_CLZ:
  11919. case WASM_OP_I32_CTZ:
  11920. case WASM_OP_I32_POPCNT:
  11921. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11922. break;
  11923. case WASM_OP_I32_ADD:
  11924. case WASM_OP_I32_SUB:
  11925. case WASM_OP_I32_MUL:
  11926. case WASM_OP_I32_DIV_S:
  11927. case WASM_OP_I32_DIV_U:
  11928. case WASM_OP_I32_REM_S:
  11929. case WASM_OP_I32_REM_U:
  11930. case WASM_OP_I32_AND:
  11931. case WASM_OP_I32_OR:
  11932. case WASM_OP_I32_XOR:
  11933. case WASM_OP_I32_SHL:
  11934. case WASM_OP_I32_SHR_S:
  11935. case WASM_OP_I32_SHR_U:
  11936. case WASM_OP_I32_ROTL:
  11937. case WASM_OP_I32_ROTR:
  11938. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11939. break;
  11940. case WASM_OP_I64_CLZ:
  11941. case WASM_OP_I64_CTZ:
  11942. case WASM_OP_I64_POPCNT:
  11943. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11944. break;
  11945. case WASM_OP_I64_ADD:
  11946. case WASM_OP_I64_SUB:
  11947. case WASM_OP_I64_MUL:
  11948. case WASM_OP_I64_DIV_S:
  11949. case WASM_OP_I64_DIV_U:
  11950. case WASM_OP_I64_REM_S:
  11951. case WASM_OP_I64_REM_U:
  11952. case WASM_OP_I64_AND:
  11953. case WASM_OP_I64_OR:
  11954. case WASM_OP_I64_XOR:
  11955. case WASM_OP_I64_SHL:
  11956. case WASM_OP_I64_SHR_S:
  11957. case WASM_OP_I64_SHR_U:
  11958. case WASM_OP_I64_ROTL:
  11959. case WASM_OP_I64_ROTR:
  11960. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11961. break;
  11962. case WASM_OP_F32_ABS:
  11963. case WASM_OP_F32_NEG:
  11964. case WASM_OP_F32_CEIL:
  11965. case WASM_OP_F32_FLOOR:
  11966. case WASM_OP_F32_TRUNC:
  11967. case WASM_OP_F32_NEAREST:
  11968. case WASM_OP_F32_SQRT:
  11969. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11970. break;
  11971. case WASM_OP_F32_ADD:
  11972. case WASM_OP_F32_SUB:
  11973. case WASM_OP_F32_MUL:
  11974. case WASM_OP_F32_DIV:
  11975. case WASM_OP_F32_MIN:
  11976. case WASM_OP_F32_MAX:
  11977. case WASM_OP_F32_COPYSIGN:
  11978. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11979. break;
  11980. case WASM_OP_F64_ABS:
  11981. case WASM_OP_F64_NEG:
  11982. case WASM_OP_F64_CEIL:
  11983. case WASM_OP_F64_FLOOR:
  11984. case WASM_OP_F64_TRUNC:
  11985. case WASM_OP_F64_NEAREST:
  11986. case WASM_OP_F64_SQRT:
  11987. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11988. break;
  11989. case WASM_OP_F64_ADD:
  11990. case WASM_OP_F64_SUB:
  11991. case WASM_OP_F64_MUL:
  11992. case WASM_OP_F64_DIV:
  11993. case WASM_OP_F64_MIN:
  11994. case WASM_OP_F64_MAX:
  11995. case WASM_OP_F64_COPYSIGN:
  11996. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11997. break;
  11998. case WASM_OP_I32_WRAP_I64:
  11999. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  12000. break;
  12001. case WASM_OP_I32_TRUNC_S_F32:
  12002. case WASM_OP_I32_TRUNC_U_F32:
  12003. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12004. break;
  12005. case WASM_OP_I32_TRUNC_S_F64:
  12006. case WASM_OP_I32_TRUNC_U_F64:
  12007. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12008. break;
  12009. case WASM_OP_I64_EXTEND_S_I32:
  12010. case WASM_OP_I64_EXTEND_U_I32:
  12011. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12012. break;
  12013. case WASM_OP_I64_TRUNC_S_F32:
  12014. case WASM_OP_I64_TRUNC_U_F32:
  12015. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12016. break;
  12017. case WASM_OP_I64_TRUNC_S_F64:
  12018. case WASM_OP_I64_TRUNC_U_F64:
  12019. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12020. break;
  12021. case WASM_OP_F32_CONVERT_S_I32:
  12022. case WASM_OP_F32_CONVERT_U_I32:
  12023. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12024. break;
  12025. case WASM_OP_F32_CONVERT_S_I64:
  12026. case WASM_OP_F32_CONVERT_U_I64:
  12027. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12028. break;
  12029. case WASM_OP_F32_DEMOTE_F64:
  12030. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12031. break;
  12032. case WASM_OP_F64_CONVERT_S_I32:
  12033. case WASM_OP_F64_CONVERT_U_I32:
  12034. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12035. break;
  12036. case WASM_OP_F64_CONVERT_S_I64:
  12037. case WASM_OP_F64_CONVERT_U_I64:
  12038. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12039. break;
  12040. case WASM_OP_F64_PROMOTE_F32:
  12041. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12042. break;
  12043. case WASM_OP_I32_REINTERPRET_F32:
  12044. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12045. break;
  12046. case WASM_OP_I64_REINTERPRET_F64:
  12047. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12048. break;
  12049. case WASM_OP_F32_REINTERPRET_I32:
  12050. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12051. break;
  12052. case WASM_OP_F64_REINTERPRET_I64:
  12053. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12054. break;
  12055. case WASM_OP_I32_EXTEND8_S:
  12056. case WASM_OP_I32_EXTEND16_S:
  12057. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12058. break;
  12059. case WASM_OP_I64_EXTEND8_S:
  12060. case WASM_OP_I64_EXTEND16_S:
  12061. case WASM_OP_I64_EXTEND32_S:
  12062. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12063. break;
  12064. #if WASM_ENABLE_GC != 0
  12065. case WASM_OP_GC_PREFIX:
  12066. {
  12067. uint32 opcode1;
  12068. read_leb_uint32(p, p_end, opcode1);
  12069. #if WASM_ENABLE_FAST_INTERP != 0
  12070. emit_byte(loader_ctx, ((uint8)opcode1));
  12071. #endif
  12072. switch (opcode1) {
  12073. case WASM_OP_STRUCT_NEW:
  12074. case WASM_OP_STRUCT_NEW_DEFAULT:
  12075. {
  12076. read_leb_uint32(p, p_end, type_idx);
  12077. #if WASM_ENABLE_FAST_INTERP != 0
  12078. emit_uint32(loader_ctx, type_idx);
  12079. #endif
  12080. if (!check_type_index(module, module->type_count,
  12081. type_idx, error_buf,
  12082. error_buf_size)) {
  12083. goto fail;
  12084. }
  12085. if (module->types[type_idx]->type_flag
  12086. != WASM_TYPE_STRUCT) {
  12087. set_error_buf(error_buf, error_buf_size,
  12088. "unkown struct type");
  12089. goto fail;
  12090. }
  12091. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12092. int32 j, k;
  12093. uint8 value_type;
  12094. uint32 ref_type_struct_size;
  12095. WASMStructType *struct_type =
  12096. (WASMStructType *)module->types[type_idx];
  12097. k = struct_type->ref_type_map_count - 1;
  12098. for (j = struct_type->field_count - 1; j >= 0;
  12099. j--) {
  12100. value_type = struct_type->fields[j].field_type;
  12101. if (wasm_is_type_reftype(value_type)) {
  12102. if (wasm_is_type_multi_byte_type(
  12103. value_type)) {
  12104. ref_type_struct_size =
  12105. wasm_reftype_struct_size(
  12106. struct_type->ref_type_maps[k]
  12107. .ref_type);
  12108. bh_memcpy_s(
  12109. &wasm_ref_type,
  12110. (uint32)sizeof(WASMRefType),
  12111. struct_type->ref_type_maps[k]
  12112. .ref_type,
  12113. ref_type_struct_size);
  12114. k--;
  12115. }
  12116. POP_REF(value_type);
  12117. }
  12118. else {
  12119. switch (value_type) {
  12120. case VALUE_TYPE_I32:
  12121. case PACKED_TYPE_I8:
  12122. case PACKED_TYPE_I16:
  12123. POP_I32();
  12124. break;
  12125. case VALUE_TYPE_I64:
  12126. POP_I64();
  12127. break;
  12128. case VALUE_TYPE_F32:
  12129. POP_F32();
  12130. break;
  12131. case VALUE_TYPE_F64:
  12132. POP_F64();
  12133. break;
  12134. default:
  12135. set_error_buf(error_buf,
  12136. error_buf_size,
  12137. "unknown type");
  12138. goto fail;
  12139. }
  12140. }
  12141. }
  12142. }
  12143. /* PUSH struct obj, (ref $t) */
  12144. wasm_set_refheaptype_typeidx(
  12145. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12146. PUSH_REF(wasm_ref_type.ref_type);
  12147. break;
  12148. }
  12149. case WASM_OP_STRUCT_GET:
  12150. case WASM_OP_STRUCT_GET_S:
  12151. case WASM_OP_STRUCT_GET_U:
  12152. case WASM_OP_STRUCT_SET:
  12153. {
  12154. WASMStructType *struct_type;
  12155. WASMRefType *ref_type = NULL;
  12156. uint32 field_idx;
  12157. uint8 field_type;
  12158. read_leb_uint32(p, p_end, type_idx);
  12159. #if WASM_ENABLE_FAST_INTERP != 0
  12160. emit_uint32(loader_ctx, type_idx);
  12161. #endif
  12162. if (!check_type_index(module, module->type_count,
  12163. type_idx, error_buf,
  12164. error_buf_size)) {
  12165. goto fail;
  12166. }
  12167. if (module->types[type_idx]->type_flag
  12168. != WASM_TYPE_STRUCT) {
  12169. set_error_buf(error_buf, error_buf_size,
  12170. "unknown struct type");
  12171. goto fail;
  12172. }
  12173. struct_type = (WASMStructType *)module->types[type_idx];
  12174. read_leb_uint32(p, p_end, field_idx);
  12175. #if WASM_ENABLE_FAST_INTERP != 0
  12176. emit_uint32(loader_ctx, field_idx);
  12177. #endif
  12178. if (field_idx >= struct_type->field_count) {
  12179. set_error_buf(error_buf, error_buf_size,
  12180. "unknown struct field");
  12181. goto fail;
  12182. }
  12183. if (opcode1 == WASM_OP_STRUCT_SET
  12184. && !(struct_type->fields[field_idx].field_flags
  12185. & 1)) {
  12186. set_error_buf(error_buf, error_buf_size,
  12187. "field is immutable");
  12188. goto fail;
  12189. }
  12190. field_type = struct_type->fields[field_idx].field_type;
  12191. if (is_packed_type(field_type)) {
  12192. if (opcode1 == WASM_OP_STRUCT_GET) {
  12193. set_error_buf(error_buf, error_buf_size,
  12194. "type mismatch");
  12195. goto fail;
  12196. }
  12197. else {
  12198. field_type = VALUE_TYPE_I32;
  12199. }
  12200. }
  12201. if (wasm_is_type_multi_byte_type(field_type)) {
  12202. ref_type = wasm_reftype_map_find(
  12203. struct_type->ref_type_maps,
  12204. struct_type->ref_type_map_count, field_idx);
  12205. bh_assert(ref_type);
  12206. }
  12207. if (opcode1 == WASM_OP_STRUCT_SET) {
  12208. /* POP field */
  12209. if (wasm_is_type_multi_byte_type(field_type)) {
  12210. bh_memcpy_s(&wasm_ref_type,
  12211. (uint32)sizeof(WASMRefType),
  12212. ref_type,
  12213. wasm_reftype_struct_size(ref_type));
  12214. }
  12215. POP_REF(field_type);
  12216. /* POP struct obj, (ref null $t) */
  12217. wasm_set_refheaptype_typeidx(
  12218. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12219. POP_REF(wasm_ref_type.ref_type);
  12220. }
  12221. else {
  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. /* PUSH field */
  12227. if (wasm_is_type_multi_byte_type(field_type)) {
  12228. bh_memcpy_s(&wasm_ref_type,
  12229. (uint32)sizeof(WASMRefType),
  12230. ref_type,
  12231. wasm_reftype_struct_size(ref_type));
  12232. }
  12233. PUSH_REF(field_type);
  12234. }
  12235. break;
  12236. }
  12237. case WASM_OP_ARRAY_NEW:
  12238. case WASM_OP_ARRAY_NEW_DEFAULT:
  12239. case WASM_OP_ARRAY_NEW_FIXED:
  12240. case WASM_OP_ARRAY_NEW_DATA:
  12241. case WASM_OP_ARRAY_NEW_ELEM:
  12242. {
  12243. WASMArrayType *array_type;
  12244. uint8 elem_type;
  12245. uint32 u32 = 0;
  12246. read_leb_uint32(p, p_end, type_idx);
  12247. #if WASM_ENABLE_FAST_INTERP != 0
  12248. emit_uint32(loader_ctx, type_idx);
  12249. #endif
  12250. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12251. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12252. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12253. read_leb_uint32(p, p_end, u32);
  12254. #if WASM_ENABLE_FAST_INTERP != 0
  12255. emit_uint32(loader_ctx, u32);
  12256. #endif
  12257. }
  12258. if (!check_array_type(module, type_idx, error_buf,
  12259. error_buf_size)) {
  12260. goto fail;
  12261. }
  12262. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12263. /* length */
  12264. POP_I32();
  12265. }
  12266. array_type = (WASMArrayType *)module->types[type_idx];
  12267. elem_type = array_type->elem_type;
  12268. if (opcode1 == WASM_OP_ARRAY_NEW
  12269. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12270. if (wasm_is_type_multi_byte_type(elem_type)) {
  12271. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12272. array_type->elem_ref_type,
  12273. wasm_reftype_struct_size(
  12274. array_type->elem_ref_type));
  12275. }
  12276. if (is_packed_type(elem_type)) {
  12277. elem_type = VALUE_TYPE_I32;
  12278. }
  12279. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12280. uint32 N = u32;
  12281. for (i = 0; i < N; i++) {
  12282. if (wasm_is_type_multi_byte_type(
  12283. elem_type)) {
  12284. bh_memcpy_s(
  12285. &wasm_ref_type, sizeof(WASMRefType),
  12286. array_type->elem_ref_type,
  12287. wasm_reftype_struct_size(
  12288. array_type->elem_ref_type));
  12289. }
  12290. POP_REF(elem_type);
  12291. }
  12292. }
  12293. else
  12294. POP_REF(elem_type);
  12295. }
  12296. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12297. /* offset of data segment */
  12298. POP_I32();
  12299. if (u32 >= module->data_seg_count) {
  12300. set_error_buf(error_buf, error_buf_size,
  12301. "unknown data segement");
  12302. goto fail;
  12303. }
  12304. if (wasm_is_type_reftype(elem_type)) {
  12305. set_error_buf(error_buf, error_buf_size,
  12306. "array elem type mismatch");
  12307. goto fail;
  12308. }
  12309. }
  12310. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12311. WASMTableSeg *table_seg =
  12312. module->table_segments + u32;
  12313. /* offset of element segment */
  12314. POP_I32();
  12315. if (u32 >= module->table_seg_count) {
  12316. set_error_buf(error_buf, error_buf_size,
  12317. "unknown element segement");
  12318. goto fail;
  12319. }
  12320. if (!wasm_reftype_is_subtype_of(
  12321. table_seg->elem_type,
  12322. table_seg->elem_ref_type, elem_type,
  12323. array_type->elem_ref_type, module->types,
  12324. module->type_count)) {
  12325. set_error_buf(error_buf, error_buf_size,
  12326. "array elem type mismatch");
  12327. goto fail;
  12328. }
  12329. }
  12330. /* PUSH array obj, (ref $t) */
  12331. wasm_set_refheaptype_typeidx(
  12332. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12333. PUSH_REF(wasm_ref_type.ref_type);
  12334. break;
  12335. }
  12336. case WASM_OP_ARRAY_GET:
  12337. case WASM_OP_ARRAY_GET_S:
  12338. case WASM_OP_ARRAY_GET_U:
  12339. case WASM_OP_ARRAY_SET:
  12340. {
  12341. uint8 elem_type;
  12342. WASMArrayType *array_type;
  12343. WASMRefType *ref_type = NULL;
  12344. read_leb_uint32(p, p_end, type_idx);
  12345. #if WASM_ENABLE_FAST_INTERP != 0
  12346. emit_uint32(loader_ctx, type_idx);
  12347. #endif
  12348. if (!check_array_type(module, type_idx, error_buf,
  12349. error_buf_size)) {
  12350. goto fail;
  12351. }
  12352. array_type = (WASMArrayType *)module->types[type_idx];
  12353. if (opcode1 == WASM_OP_ARRAY_SET
  12354. && !(array_type->elem_flags & 1)) {
  12355. set_error_buf(error_buf, error_buf_size,
  12356. "array is immutable");
  12357. goto fail;
  12358. }
  12359. elem_type = array_type->elem_type;
  12360. if (is_packed_type(elem_type)) {
  12361. if (opcode1 != WASM_OP_ARRAY_GET_S
  12362. && opcode1 != WASM_OP_ARRAY_GET_U
  12363. && opcode1 != WASM_OP_ARRAY_SET) {
  12364. set_error_buf(error_buf, error_buf_size,
  12365. "type mismatch");
  12366. goto fail;
  12367. }
  12368. else {
  12369. elem_type = VALUE_TYPE_I32;
  12370. }
  12371. }
  12372. ref_type = array_type->elem_ref_type;
  12373. if (opcode1 == WASM_OP_ARRAY_SET) {
  12374. /* POP elem to set */
  12375. if (wasm_is_type_multi_byte_type(elem_type)) {
  12376. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12377. ref_type,
  12378. wasm_reftype_struct_size(ref_type));
  12379. }
  12380. POP_REF(elem_type);
  12381. }
  12382. /* elem idx */
  12383. POP_I32();
  12384. /* POP array obj, (ref null $t) */
  12385. wasm_set_refheaptype_typeidx(
  12386. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12387. POP_REF(wasm_ref_type.ref_type);
  12388. if (opcode1 != WASM_OP_ARRAY_SET) {
  12389. /* PUSH elem */
  12390. if (wasm_is_type_multi_byte_type(elem_type)) {
  12391. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12392. ref_type,
  12393. wasm_reftype_struct_size(ref_type));
  12394. }
  12395. PUSH_REF(elem_type);
  12396. }
  12397. break;
  12398. }
  12399. case WASM_OP_ARRAY_LEN:
  12400. {
  12401. POP_REF(REF_TYPE_ARRAYREF);
  12402. /* length */
  12403. PUSH_I32();
  12404. break;
  12405. }
  12406. case WASM_OP_ARRAY_FILL:
  12407. {
  12408. WASMArrayType *array_type;
  12409. uint8 elem_type;
  12410. /* typeidx */
  12411. read_leb_uint32(p, p_end, type_idx);
  12412. #if WASM_ENABLE_FAST_INTERP != 0
  12413. emit_uint32(loader_ctx, type_idx);
  12414. #endif
  12415. if (!check_array_type(module, type_idx, error_buf,
  12416. error_buf_size)) {
  12417. goto fail;
  12418. }
  12419. array_type = (WASMArrayType *)module->types[type_idx];
  12420. if (!(array_type->elem_flags & 1)) {
  12421. set_error_buf(error_buf, error_buf_size,
  12422. "array is immutable");
  12423. goto fail;
  12424. }
  12425. elem_type = array_type->elem_type;
  12426. if (is_packed_type(elem_type)) {
  12427. elem_type = VALUE_TYPE_I32;
  12428. }
  12429. POP_I32(); /* length */
  12430. #if WASM_ENABLE_FAST_INTERP != 0
  12431. POP_OFFSET_TYPE(elem_type);
  12432. #endif
  12433. POP_TYPE(elem_type);
  12434. POP_I32(); /* start */
  12435. /* POP array obj, (ref null $t) */
  12436. wasm_set_refheaptype_typeidx(
  12437. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12438. POP_REF(wasm_ref_type.ref_type);
  12439. break;
  12440. }
  12441. case WASM_OP_ARRAY_COPY:
  12442. {
  12443. uint32 src_type_idx;
  12444. uint8 src_elem_type, dst_elem_type;
  12445. WASMRefType src_ref_type = { 0 },
  12446. *src_elem_ref_type = NULL;
  12447. WASMRefType dst_ref_type = { 0 },
  12448. *dst_elem_ref_type = NULL;
  12449. WASMArrayType *array_type;
  12450. /* typeidx1 */
  12451. read_leb_uint32(p, p_end, type_idx);
  12452. #if WASM_ENABLE_FAST_INTERP != 0
  12453. emit_uint32(loader_ctx, type_idx);
  12454. #endif
  12455. /* typeidx2 */
  12456. read_leb_uint32(p, p_end, src_type_idx);
  12457. #if WASM_ENABLE_FAST_INTERP != 0
  12458. emit_uint32(loader_ctx, src_type_idx);
  12459. #endif
  12460. if (!check_array_type(module, type_idx, error_buf,
  12461. error_buf_size)) {
  12462. goto fail;
  12463. }
  12464. if (!check_array_type(module, src_type_idx, error_buf,
  12465. error_buf_size)) {
  12466. goto fail;
  12467. }
  12468. POP_I32();
  12469. POP_I32();
  12470. /* POP array obj, (ref null $t) */
  12471. wasm_set_refheaptype_typeidx(
  12472. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12473. POP_REF(wasm_ref_type.ref_type);
  12474. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12475. &wasm_ref_type,
  12476. wasm_reftype_struct_size(&wasm_ref_type));
  12477. POP_I32();
  12478. /* POP array obj, (ref null $t) */
  12479. wasm_set_refheaptype_typeidx(
  12480. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12481. POP_REF(wasm_ref_type.ref_type);
  12482. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12483. &wasm_ref_type,
  12484. wasm_reftype_struct_size(&wasm_ref_type));
  12485. array_type = (WASMArrayType *)module->types[type_idx];
  12486. if (!(array_type->elem_flags & 1)) {
  12487. set_error_buf(error_buf, error_buf_size,
  12488. "destination array is immutable");
  12489. goto fail;
  12490. }
  12491. dst_elem_type = array_type->elem_type;
  12492. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12493. dst_elem_ref_type = array_type->elem_ref_type;
  12494. }
  12495. array_type =
  12496. (WASMArrayType *)module->types[src_type_idx];
  12497. src_elem_type = array_type->elem_type;
  12498. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12499. src_elem_ref_type = array_type->elem_ref_type;
  12500. }
  12501. if (!wasm_reftype_is_subtype_of(
  12502. src_elem_type, src_elem_ref_type, dst_elem_type,
  12503. dst_elem_ref_type, module->types,
  12504. module->type_count)) {
  12505. set_error_buf(error_buf, error_buf_size,
  12506. "array types do not match");
  12507. goto fail;
  12508. }
  12509. break;
  12510. }
  12511. case WASM_OP_REF_I31:
  12512. {
  12513. POP_I32();
  12514. wasm_set_refheaptype_common(
  12515. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12516. PUSH_REF(wasm_ref_type.ref_type);
  12517. break;
  12518. }
  12519. case WASM_OP_I31_GET_S:
  12520. case WASM_OP_I31_GET_U:
  12521. {
  12522. POP_REF(REF_TYPE_I31REF);
  12523. PUSH_I32();
  12524. break;
  12525. }
  12526. case WASM_OP_REF_TEST:
  12527. case WASM_OP_REF_CAST:
  12528. case WASM_OP_REF_TEST_NULLABLE:
  12529. case WASM_OP_REF_CAST_NULLABLE:
  12530. {
  12531. uint8 type;
  12532. read_leb_int32(p, p_end, heap_type);
  12533. #if WASM_ENABLE_FAST_INTERP != 0
  12534. emit_uint32(loader_ctx, (uint32)heap_type);
  12535. #endif
  12536. if (heap_type >= 0) {
  12537. if (!check_type_index(module, module->type_count,
  12538. heap_type, error_buf,
  12539. error_buf_size)) {
  12540. goto fail;
  12541. }
  12542. }
  12543. else {
  12544. if (!wasm_is_valid_heap_type(heap_type)) {
  12545. set_error_buf(error_buf, error_buf_size,
  12546. "unknown type");
  12547. goto fail;
  12548. }
  12549. }
  12550. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12551. &wasm_ref_type, error_buf,
  12552. error_buf_size)) {
  12553. goto fail;
  12554. }
  12555. if (opcode1 == WASM_OP_REF_TEST
  12556. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12557. PUSH_I32();
  12558. else {
  12559. bool nullable =
  12560. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12561. : false;
  12562. if (heap_type >= 0 || !nullable) {
  12563. wasm_set_refheaptype_typeidx(
  12564. &wasm_ref_type.ref_ht_typeidx, nullable,
  12565. heap_type);
  12566. PUSH_REF(wasm_ref_type.ref_type);
  12567. }
  12568. else {
  12569. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12570. }
  12571. }
  12572. break;
  12573. }
  12574. case WASM_OP_BR_ON_CAST:
  12575. case WASM_OP_BR_ON_CAST_FAIL:
  12576. {
  12577. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12578. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12579. uint8 type_tmp, castflags;
  12580. uint32 depth;
  12581. int32 heap_type_dst;
  12582. bool src_nullable, dst_nullable;
  12583. CHECK_BUF(p, p_end, 1);
  12584. castflags = read_uint8(p);
  12585. #if WASM_ENABLE_FAST_INTERP != 0
  12586. /* Emit heap_type firstly */
  12587. emit_byte(loader_ctx, castflags);
  12588. #endif
  12589. p_org = p;
  12590. read_leb_uint32(p, p_end, depth);
  12591. read_leb_int32(p, p_end, heap_type);
  12592. #if WASM_ENABLE_FAST_INTERP != 0
  12593. /* Emit heap_type firstly */
  12594. emit_uint32(loader_ctx, (uint32)heap_type);
  12595. #endif
  12596. read_leb_int32(p, p_end, heap_type_dst);
  12597. #if WASM_ENABLE_FAST_INTERP != 0
  12598. /* Emit heap_type firstly */
  12599. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12600. #endif
  12601. (void)depth;
  12602. /*
  12603. * castflags should be 0~3:
  12604. * 0: (non-null, non-null)
  12605. * 1: (null, non-null)
  12606. * 2: (non-null, null)
  12607. * 3: (null, null)
  12608. */
  12609. if (castflags > 3) {
  12610. set_error_buf(error_buf, error_buf_size,
  12611. "invalid castflags");
  12612. break;
  12613. }
  12614. src_nullable =
  12615. (castflags == 1) || (castflags == 3) ? true : false;
  12616. dst_nullable =
  12617. (castflags == 2) || (castflags == 3) ? true : false;
  12618. /* Pop and backup the stack top's ref type */
  12619. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12620. &ref_type_tmp, error_buf,
  12621. error_buf_size)) {
  12622. goto fail;
  12623. }
  12624. /* The reference type rt1 must be valid */
  12625. if (!init_ref_type(module, &ref_type1, src_nullable,
  12626. heap_type, error_buf,
  12627. error_buf_size)) {
  12628. goto fail;
  12629. }
  12630. /* The reference type rt2 must be valid. */
  12631. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12632. heap_type_dst, error_buf,
  12633. error_buf_size)) {
  12634. goto fail;
  12635. }
  12636. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12637. &ref_type2);
  12638. /* The reference type rt2 must match rt1. */
  12639. if (!wasm_reftype_is_subtype_of(
  12640. ref_type2.ref_type, &ref_type2,
  12641. ref_type1.ref_type, &ref_type1, module->types,
  12642. module->type_count)) {
  12643. set_error_buf(error_buf, error_buf_size,
  12644. "type mismatch");
  12645. goto fail;
  12646. }
  12647. p = p_org;
  12648. /* Push ref type casted for branch block check */
  12649. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12650. /* The reference type rt2 must match rt′. */
  12651. type_tmp = ref_type2.ref_type;
  12652. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12653. bh_memcpy_s(
  12654. &wasm_ref_type,
  12655. wasm_reftype_struct_size(&ref_type2),
  12656. &ref_type2,
  12657. wasm_reftype_struct_size(&ref_type2));
  12658. }
  12659. }
  12660. else {
  12661. /* The reference type rt′1 must match rt′. */
  12662. type_tmp = ref_type_diff.ref_type;
  12663. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12664. bh_memcpy_s(
  12665. &wasm_ref_type,
  12666. wasm_reftype_struct_size(&ref_type_diff),
  12667. &ref_type_diff,
  12668. wasm_reftype_struct_size(&ref_type_diff));
  12669. }
  12670. }
  12671. PUSH_REF(type_tmp);
  12672. if (!(frame_csp_tmp = check_branch_block(
  12673. loader_ctx, &p, p_end, opcode, error_buf,
  12674. error_buf_size))) {
  12675. goto fail;
  12676. }
  12677. /* Ignore heap_types */
  12678. skip_leb_uint32(p, p_end);
  12679. skip_leb_uint32(p, p_end);
  12680. /* Restore the original stack top's ref type */
  12681. POP_REF(type_tmp);
  12682. #if WASM_ENABLE_FAST_INTERP != 0
  12683. /* Erase the opnd offset emitted by POP_REF() */
  12684. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12685. #endif
  12686. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12687. type_tmp = ref_type_diff.ref_type;
  12688. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12689. bh_memcpy_s(
  12690. &wasm_ref_type,
  12691. wasm_reftype_struct_size(&ref_type_diff),
  12692. &ref_type_diff,
  12693. wasm_reftype_struct_size(&ref_type_diff));
  12694. }
  12695. }
  12696. else {
  12697. type_tmp = ref_type2.ref_type;
  12698. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12699. bh_memcpy_s(
  12700. &wasm_ref_type,
  12701. wasm_reftype_struct_size(&ref_type2),
  12702. &ref_type2,
  12703. wasm_reftype_struct_size(&ref_type2));
  12704. }
  12705. }
  12706. PUSH_REF(type_tmp);
  12707. #if WASM_ENABLE_FAST_INTERP != 0
  12708. /* Erase the opnd offset emitted by PUSH_REF() */
  12709. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12710. #endif
  12711. break;
  12712. }
  12713. case WASM_OP_ANY_CONVERT_EXTERN:
  12714. {
  12715. uint8 type;
  12716. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12717. &wasm_ref_type, error_buf,
  12718. error_buf_size)) {
  12719. goto fail;
  12720. }
  12721. if (!(type == REF_TYPE_EXTERNREF
  12722. || (type == REF_TYPE_HT_NON_NULLABLE
  12723. && wasm_ref_type.ref_ht_common.heap_type
  12724. == HEAP_TYPE_EXTERN)
  12725. || type == VALUE_TYPE_ANY)) {
  12726. set_error_buf(error_buf, error_buf_size,
  12727. "type mismatch");
  12728. goto fail;
  12729. }
  12730. if (type == REF_TYPE_EXTERNREF)
  12731. type = REF_TYPE_ANYREF;
  12732. else {
  12733. wasm_ref_type.ref_ht_common.heap_type =
  12734. HEAP_TYPE_ANY;
  12735. }
  12736. PUSH_REF(type);
  12737. break;
  12738. }
  12739. case WASM_OP_EXTERN_CONVERT_ANY:
  12740. {
  12741. uint8 type;
  12742. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12743. &wasm_ref_type, error_buf,
  12744. error_buf_size)) {
  12745. goto fail;
  12746. }
  12747. if (type == REF_TYPE_EXTERNREF
  12748. || ((type == REF_TYPE_HT_NULLABLE
  12749. || type == REF_TYPE_HT_NON_NULLABLE)
  12750. && wasm_ref_type.ref_ht_common.heap_type
  12751. == HEAP_TYPE_EXTERN)) {
  12752. set_error_buf(error_buf, error_buf_size,
  12753. "type mismatch");
  12754. goto fail;
  12755. }
  12756. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12757. /* push (ref null extern) */
  12758. type = REF_TYPE_EXTERNREF;
  12759. }
  12760. else {
  12761. /* push (ref extern) */
  12762. type = REF_TYPE_HT_NON_NULLABLE;
  12763. wasm_set_refheaptype_common(
  12764. &wasm_ref_type.ref_ht_common, false,
  12765. HEAP_TYPE_EXTERN);
  12766. }
  12767. PUSH_REF(type);
  12768. break;
  12769. }
  12770. #if WASM_ENABLE_STRINGREF != 0
  12771. case WASM_OP_STRING_NEW_UTF8:
  12772. case WASM_OP_STRING_NEW_WTF16:
  12773. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12774. case WASM_OP_STRING_NEW_WTF8:
  12775. {
  12776. uint32 memidx;
  12777. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12778. func->has_memory_operations = true;
  12779. #endif
  12780. read_leb_uint32(p, p_end, memidx);
  12781. #if WASM_ENABLE_FAST_INTERP != 0
  12782. emit_uint32(loader_ctx, (uint32)memidx);
  12783. #endif
  12784. POP_I32();
  12785. POP_I32();
  12786. PUSH_REF(REF_TYPE_STRINGREF);
  12787. (void)memidx;
  12788. break;
  12789. }
  12790. case WASM_OP_STRING_CONST:
  12791. {
  12792. uint32 contents;
  12793. read_leb_uint32(p, p_end, contents);
  12794. #if WASM_ENABLE_FAST_INTERP != 0
  12795. emit_uint32(loader_ctx, (uint32)contents);
  12796. #endif
  12797. PUSH_REF(REF_TYPE_STRINGREF);
  12798. (void)contents;
  12799. break;
  12800. }
  12801. case WASM_OP_STRING_MEASURE_UTF8:
  12802. case WASM_OP_STRING_MEASURE_WTF8:
  12803. case WASM_OP_STRING_MEASURE_WTF16:
  12804. {
  12805. POP_STRINGREF();
  12806. PUSH_I32();
  12807. break;
  12808. }
  12809. case WASM_OP_STRING_ENCODE_UTF8:
  12810. case WASM_OP_STRING_ENCODE_WTF16:
  12811. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12812. case WASM_OP_STRING_ENCODE_WTF8:
  12813. {
  12814. uint32 memidx;
  12815. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12816. func->has_memory_operations = true;
  12817. #endif
  12818. read_leb_uint32(p, p_end, memidx);
  12819. #if WASM_ENABLE_FAST_INTERP != 0
  12820. emit_uint32(loader_ctx, (uint32)memidx);
  12821. #endif
  12822. POP_I32();
  12823. POP_STRINGREF();
  12824. PUSH_I32();
  12825. (void)memidx;
  12826. break;
  12827. }
  12828. case WASM_OP_STRING_CONCAT:
  12829. {
  12830. POP_STRINGREF();
  12831. POP_STRINGREF();
  12832. PUSH_REF(REF_TYPE_STRINGREF);
  12833. break;
  12834. }
  12835. case WASM_OP_STRING_EQ:
  12836. {
  12837. POP_STRINGREF();
  12838. POP_STRINGREF();
  12839. PUSH_I32();
  12840. break;
  12841. }
  12842. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12843. {
  12844. POP_STRINGREF();
  12845. PUSH_I32();
  12846. break;
  12847. }
  12848. case WASM_OP_STRING_AS_WTF8:
  12849. {
  12850. POP_STRINGREF();
  12851. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12852. break;
  12853. }
  12854. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12855. {
  12856. POP_I32();
  12857. POP_I32();
  12858. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12859. PUSH_I32();
  12860. break;
  12861. }
  12862. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12863. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12864. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12865. {
  12866. uint32 memidx;
  12867. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12868. func->has_memory_operations = true;
  12869. #endif
  12870. read_leb_uint32(p, p_end, memidx);
  12871. #if WASM_ENABLE_FAST_INTERP != 0
  12872. emit_uint32(loader_ctx, (uint32)memidx);
  12873. #endif
  12874. POP_I32();
  12875. POP_I32();
  12876. POP_I32();
  12877. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12878. PUSH_I32();
  12879. PUSH_I32();
  12880. (void)memidx;
  12881. break;
  12882. }
  12883. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12884. {
  12885. POP_I32();
  12886. POP_I32();
  12887. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12888. PUSH_REF(REF_TYPE_STRINGREF);
  12889. break;
  12890. }
  12891. case WASM_OP_STRING_AS_WTF16:
  12892. {
  12893. POP_STRINGREF();
  12894. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12895. break;
  12896. }
  12897. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12898. {
  12899. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12900. PUSH_I32();
  12901. break;
  12902. }
  12903. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  12904. {
  12905. POP_I32();
  12906. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12907. PUSH_I32();
  12908. break;
  12909. }
  12910. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  12911. {
  12912. uint32 memidx;
  12913. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12914. func->has_memory_operations = true;
  12915. #endif
  12916. read_leb_uint32(p, p_end, memidx);
  12917. #if WASM_ENABLE_FAST_INTERP != 0
  12918. emit_uint32(loader_ctx, (uint32)memidx);
  12919. #endif
  12920. POP_I32();
  12921. POP_I32();
  12922. POP_I32();
  12923. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12924. PUSH_I32();
  12925. (void)memidx;
  12926. break;
  12927. }
  12928. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  12929. {
  12930. POP_I32();
  12931. POP_I32();
  12932. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12933. PUSH_REF(REF_TYPE_STRINGREF);
  12934. break;
  12935. }
  12936. case WASM_OP_STRING_AS_ITER:
  12937. {
  12938. POP_STRINGREF();
  12939. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  12940. break;
  12941. }
  12942. case WASM_OP_STRINGVIEW_ITER_NEXT:
  12943. {
  12944. POP_REF(REF_TYPE_STRINGVIEWITER);
  12945. PUSH_I32();
  12946. break;
  12947. }
  12948. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  12949. case WASM_OP_STRINGVIEW_ITER_REWIND:
  12950. {
  12951. POP_I32();
  12952. POP_REF(REF_TYPE_STRINGVIEWITER);
  12953. PUSH_I32();
  12954. break;
  12955. }
  12956. case WASM_OP_STRINGVIEW_ITER_SLICE:
  12957. {
  12958. POP_I32();
  12959. POP_REF(REF_TYPE_STRINGVIEWITER);
  12960. PUSH_REF(REF_TYPE_STRINGREF);
  12961. break;
  12962. }
  12963. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  12964. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  12965. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  12966. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  12967. {
  12968. POP_I32();
  12969. POP_I32();
  12970. POP_REF(REF_TYPE_ARRAYREF);
  12971. PUSH_REF(REF_TYPE_STRINGREF);
  12972. break;
  12973. }
  12974. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  12975. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  12976. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  12977. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  12978. {
  12979. POP_I32();
  12980. POP_REF(REF_TYPE_ARRAYREF);
  12981. POP_STRINGREF();
  12982. PUSH_I32();
  12983. break;
  12984. }
  12985. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  12986. default:
  12987. set_error_buf_v(error_buf, error_buf_size,
  12988. "%s %02x %02x", "unsupported opcode",
  12989. 0xfb, opcode1);
  12990. goto fail;
  12991. }
  12992. break;
  12993. }
  12994. #endif /* end of WASM_ENABLE_GC != 0 */
  12995. case WASM_OP_MISC_PREFIX:
  12996. {
  12997. uint32 opcode1;
  12998. read_leb_uint32(p, p_end, opcode1);
  12999. #if WASM_ENABLE_FAST_INTERP != 0
  13000. emit_byte(loader_ctx, ((uint8)opcode1));
  13001. #endif
  13002. switch (opcode1) {
  13003. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13004. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13005. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13006. break;
  13007. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13008. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13009. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13010. break;
  13011. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13012. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13013. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13014. break;
  13015. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13016. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13017. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13018. break;
  13019. #if WASM_ENABLE_BULK_MEMORY != 0
  13020. case WASM_OP_MEMORY_INIT:
  13021. {
  13022. read_leb_uint32(p, p_end, data_seg_idx);
  13023. #if WASM_ENABLE_FAST_INTERP != 0
  13024. emit_uint32(loader_ctx, data_seg_idx);
  13025. #endif
  13026. if (module->import_memory_count == 0
  13027. && module->memory_count == 0)
  13028. goto fail_unknown_memory;
  13029. if (*p++ != 0x00)
  13030. goto fail_zero_byte_expected;
  13031. if (data_seg_idx >= module->data_seg_count) {
  13032. set_error_buf_v(error_buf, error_buf_size,
  13033. "unknown data segment %d",
  13034. data_seg_idx);
  13035. goto fail;
  13036. }
  13037. if (module->data_seg_count1 == 0)
  13038. goto fail_data_cnt_sec_require;
  13039. POP_I32();
  13040. POP_I32();
  13041. POP_MEM_OFFSET();
  13042. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13043. func->has_memory_operations = true;
  13044. #endif
  13045. #if WASM_ENABLE_WAMR_COMPILER != 0
  13046. module->is_bulk_memory_used = true;
  13047. #endif
  13048. break;
  13049. }
  13050. case WASM_OP_DATA_DROP:
  13051. {
  13052. read_leb_uint32(p, p_end, data_seg_idx);
  13053. #if WASM_ENABLE_FAST_INTERP != 0
  13054. emit_uint32(loader_ctx, data_seg_idx);
  13055. #endif
  13056. if (data_seg_idx >= module->data_seg_count) {
  13057. set_error_buf(error_buf, error_buf_size,
  13058. "unknown data segment");
  13059. goto fail;
  13060. }
  13061. if (module->data_seg_count1 == 0)
  13062. goto fail_data_cnt_sec_require;
  13063. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13064. func->has_memory_operations = true;
  13065. #endif
  13066. #if WASM_ENABLE_WAMR_COMPILER != 0
  13067. module->is_bulk_memory_used = true;
  13068. #endif
  13069. break;
  13070. }
  13071. case WASM_OP_MEMORY_COPY:
  13072. {
  13073. CHECK_BUF(p, p_end, sizeof(int16));
  13074. /* both src and dst memory index should be 0 */
  13075. if (*(int16 *)p != 0x0000)
  13076. goto fail_zero_byte_expected;
  13077. p += 2;
  13078. if (module->import_memory_count == 0
  13079. && module->memory_count == 0)
  13080. goto fail_unknown_memory;
  13081. POP_MEM_OFFSET();
  13082. POP_MEM_OFFSET();
  13083. POP_MEM_OFFSET();
  13084. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13085. func->has_memory_operations = true;
  13086. #endif
  13087. #if WASM_ENABLE_WAMR_COMPILER != 0
  13088. module->is_bulk_memory_used = true;
  13089. #endif
  13090. break;
  13091. }
  13092. case WASM_OP_MEMORY_FILL:
  13093. {
  13094. if (*p++ != 0x00) {
  13095. goto fail_zero_byte_expected;
  13096. }
  13097. if (module->import_memory_count == 0
  13098. && module->memory_count == 0) {
  13099. goto fail_unknown_memory;
  13100. }
  13101. POP_MEM_OFFSET();
  13102. POP_I32();
  13103. POP_MEM_OFFSET();
  13104. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13105. func->has_memory_operations = true;
  13106. #endif
  13107. #if WASM_ENABLE_WAMR_COMPILER != 0
  13108. module->is_bulk_memory_used = true;
  13109. #endif
  13110. break;
  13111. }
  13112. fail_zero_byte_expected:
  13113. set_error_buf(error_buf, error_buf_size,
  13114. "zero byte expected");
  13115. goto fail;
  13116. fail_unknown_memory:
  13117. set_error_buf(error_buf, error_buf_size,
  13118. "unknown memory 0");
  13119. goto fail;
  13120. fail_data_cnt_sec_require:
  13121. set_error_buf(error_buf, error_buf_size,
  13122. "data count section required");
  13123. goto fail;
  13124. #endif /* WASM_ENABLE_BULK_MEMORY */
  13125. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13126. case WASM_OP_TABLE_INIT:
  13127. {
  13128. uint8 seg_type = 0, tbl_type = 0;
  13129. #if WASM_ENABLE_GC != 0
  13130. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13131. #endif
  13132. read_leb_uint32(p, p_end, table_seg_idx);
  13133. read_leb_uint32(p, p_end, table_idx);
  13134. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13135. #if WASM_ENABLE_GC != 0
  13136. (void **)&tbl_ref_type,
  13137. #else
  13138. NULL,
  13139. #endif
  13140. error_buf, error_buf_size))
  13141. goto fail;
  13142. if (!get_table_seg_elem_type(module, table_seg_idx,
  13143. &seg_type,
  13144. #if WASM_ENABLE_GC != 0
  13145. (void **)&seg_ref_type,
  13146. #else
  13147. NULL,
  13148. #endif
  13149. error_buf, error_buf_size))
  13150. goto fail;
  13151. #if WASM_ENABLE_GC == 0
  13152. if (seg_type != tbl_type) {
  13153. set_error_buf(error_buf, error_buf_size,
  13154. "type mismatch");
  13155. goto fail;
  13156. }
  13157. #else
  13158. if (!wasm_reftype_is_subtype_of(
  13159. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13160. module->types, module->type_count)) {
  13161. set_error_buf(error_buf, error_buf_size,
  13162. "type mismatch");
  13163. goto fail;
  13164. }
  13165. #endif
  13166. #if WASM_ENABLE_FAST_INTERP != 0
  13167. emit_uint32(loader_ctx, table_seg_idx);
  13168. emit_uint32(loader_ctx, table_idx);
  13169. #endif
  13170. POP_I32();
  13171. POP_I32();
  13172. POP_I32();
  13173. #if WASM_ENABLE_WAMR_COMPILER != 0
  13174. module->is_ref_types_used = true;
  13175. #endif
  13176. break;
  13177. }
  13178. case WASM_OP_ELEM_DROP:
  13179. {
  13180. read_leb_uint32(p, p_end, table_seg_idx);
  13181. if (!get_table_seg_elem_type(module, table_seg_idx,
  13182. NULL, NULL, error_buf,
  13183. error_buf_size))
  13184. goto fail;
  13185. #if WASM_ENABLE_FAST_INTERP != 0
  13186. emit_uint32(loader_ctx, table_seg_idx);
  13187. #endif
  13188. #if WASM_ENABLE_WAMR_COMPILER != 0
  13189. module->is_ref_types_used = true;
  13190. #endif
  13191. break;
  13192. }
  13193. case WASM_OP_TABLE_COPY:
  13194. {
  13195. uint8 src_type, dst_type;
  13196. #if WASM_ENABLE_GC != 0
  13197. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13198. #endif
  13199. uint32 src_tbl_idx, dst_tbl_idx;
  13200. read_leb_uint32(p, p_end, dst_tbl_idx);
  13201. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13202. #if WASM_ENABLE_GC != 0
  13203. (void **)&dst_ref_type,
  13204. #else
  13205. NULL,
  13206. #endif
  13207. error_buf, error_buf_size))
  13208. goto fail;
  13209. read_leb_uint32(p, p_end, src_tbl_idx);
  13210. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13211. #if WASM_ENABLE_GC != 0
  13212. (void **)&src_ref_type,
  13213. #else
  13214. NULL,
  13215. #endif
  13216. error_buf, error_buf_size))
  13217. goto fail;
  13218. #if WASM_ENABLE_GC == 0
  13219. if (src_type != dst_type) {
  13220. set_error_buf(error_buf, error_buf_size,
  13221. "type mismatch");
  13222. goto fail;
  13223. }
  13224. #else
  13225. if (!wasm_reftype_is_subtype_of(
  13226. src_type, src_ref_type, dst_type, dst_ref_type,
  13227. module->types, module->type_count)) {
  13228. set_error_buf(error_buf, error_buf_size,
  13229. "type mismatch");
  13230. goto fail;
  13231. }
  13232. #endif
  13233. #if WASM_ENABLE_FAST_INTERP != 0
  13234. emit_uint32(loader_ctx, dst_tbl_idx);
  13235. emit_uint32(loader_ctx, src_tbl_idx);
  13236. #endif
  13237. POP_I32();
  13238. POP_I32();
  13239. POP_I32();
  13240. #if WASM_ENABLE_WAMR_COMPILER != 0
  13241. module->is_ref_types_used = true;
  13242. #endif
  13243. break;
  13244. }
  13245. case WASM_OP_TABLE_SIZE:
  13246. {
  13247. read_leb_uint32(p, p_end, table_idx);
  13248. /* TODO: shall we create a new function to check
  13249. table idx instead of using below function? */
  13250. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13251. error_buf, error_buf_size))
  13252. goto fail;
  13253. #if WASM_ENABLE_FAST_INTERP != 0
  13254. emit_uint32(loader_ctx, table_idx);
  13255. #endif
  13256. PUSH_I32();
  13257. #if WASM_ENABLE_WAMR_COMPILER != 0
  13258. module->is_ref_types_used = true;
  13259. #endif
  13260. break;
  13261. }
  13262. case WASM_OP_TABLE_GROW:
  13263. case WASM_OP_TABLE_FILL:
  13264. {
  13265. uint8 decl_type;
  13266. #if WASM_ENABLE_GC != 0
  13267. WASMRefType *ref_type = NULL;
  13268. #endif
  13269. read_leb_uint32(p, p_end, table_idx);
  13270. if (!get_table_elem_type(module, table_idx, &decl_type,
  13271. #if WASM_ENABLE_GC != 0
  13272. (void **)&ref_type,
  13273. #else
  13274. NULL,
  13275. #endif
  13276. error_buf, error_buf_size))
  13277. goto fail;
  13278. #if WASM_ENABLE_GC != 0
  13279. if (wasm_is_type_multi_byte_type(decl_type)) {
  13280. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13281. ref_type,
  13282. wasm_reftype_struct_size(ref_type));
  13283. }
  13284. #endif
  13285. if (opcode1 == WASM_OP_TABLE_GROW) {
  13286. if (table_idx < module->import_table_count) {
  13287. module->import_tables[table_idx]
  13288. .u.table.possible_grow = true;
  13289. }
  13290. else {
  13291. module
  13292. ->tables[table_idx
  13293. - module->import_table_count]
  13294. .possible_grow = true;
  13295. }
  13296. }
  13297. #if WASM_ENABLE_FAST_INTERP != 0
  13298. emit_uint32(loader_ctx, table_idx);
  13299. #endif
  13300. POP_I32();
  13301. #if WASM_ENABLE_FAST_INTERP != 0
  13302. POP_OFFSET_TYPE(decl_type);
  13303. #endif
  13304. POP_TYPE(decl_type);
  13305. if (opcode1 == WASM_OP_TABLE_GROW)
  13306. PUSH_I32();
  13307. else
  13308. POP_I32();
  13309. #if WASM_ENABLE_WAMR_COMPILER != 0
  13310. module->is_ref_types_used = true;
  13311. #endif
  13312. break;
  13313. }
  13314. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13315. default:
  13316. set_error_buf_v(error_buf, error_buf_size,
  13317. "%s %02x %02x", "unsupported opcode",
  13318. 0xfc, opcode1);
  13319. goto fail;
  13320. }
  13321. break;
  13322. }
  13323. #if WASM_ENABLE_SIMD != 0
  13324. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13325. case WASM_OP_SIMD_PREFIX:
  13326. {
  13327. /* TODO: memory64 offset type changes */
  13328. uint32 opcode1;
  13329. #if WASM_ENABLE_WAMR_COMPILER != 0
  13330. /* Mark the SIMD instruction is used in this module */
  13331. module->is_simd_used = true;
  13332. #endif
  13333. read_leb_uint32(p, p_end, opcode1);
  13334. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13335. */
  13336. switch (opcode1) {
  13337. /* memory instruction */
  13338. case SIMD_v128_load:
  13339. case SIMD_v128_load8x8_s:
  13340. case SIMD_v128_load8x8_u:
  13341. case SIMD_v128_load16x4_s:
  13342. case SIMD_v128_load16x4_u:
  13343. case SIMD_v128_load32x2_s:
  13344. case SIMD_v128_load32x2_u:
  13345. case SIMD_v128_load8_splat:
  13346. case SIMD_v128_load16_splat:
  13347. case SIMD_v128_load32_splat:
  13348. case SIMD_v128_load64_splat:
  13349. {
  13350. CHECK_MEMORY();
  13351. read_leb_uint32(p, p_end, align); /* align */
  13352. if (!check_simd_memory_access_align(
  13353. opcode1, align, error_buf, error_buf_size)) {
  13354. goto fail;
  13355. }
  13356. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13357. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13358. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13359. func->has_memory_operations = true;
  13360. #endif
  13361. break;
  13362. }
  13363. case SIMD_v128_store:
  13364. {
  13365. CHECK_MEMORY();
  13366. read_leb_uint32(p, p_end, align); /* align */
  13367. if (!check_simd_memory_access_align(
  13368. opcode1, align, error_buf, error_buf_size)) {
  13369. goto fail;
  13370. }
  13371. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13372. POP_V128();
  13373. POP_I32();
  13374. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13375. func->has_memory_operations = true;
  13376. #endif
  13377. break;
  13378. }
  13379. /* basic operation */
  13380. case SIMD_v128_const:
  13381. {
  13382. CHECK_BUF1(p, p_end, 16);
  13383. p += 16;
  13384. PUSH_V128();
  13385. break;
  13386. }
  13387. case SIMD_v8x16_shuffle:
  13388. {
  13389. V128 mask;
  13390. CHECK_BUF1(p, p_end, 16);
  13391. mask = read_i8x16(p, error_buf, error_buf_size);
  13392. p += 16;
  13393. if (!check_simd_shuffle_mask(mask, error_buf,
  13394. error_buf_size)) {
  13395. goto fail;
  13396. }
  13397. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13398. break;
  13399. }
  13400. case SIMD_v8x16_swizzle:
  13401. {
  13402. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13403. break;
  13404. }
  13405. /* splat operation */
  13406. case SIMD_i8x16_splat:
  13407. case SIMD_i16x8_splat:
  13408. case SIMD_i32x4_splat:
  13409. case SIMD_i64x2_splat:
  13410. case SIMD_f32x4_splat:
  13411. case SIMD_f64x2_splat:
  13412. {
  13413. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13414. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13415. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13416. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13417. VALUE_TYPE_V128);
  13418. break;
  13419. }
  13420. /* lane operation */
  13421. case SIMD_i8x16_extract_lane_s:
  13422. case SIMD_i8x16_extract_lane_u:
  13423. case SIMD_i8x16_replace_lane:
  13424. case SIMD_i16x8_extract_lane_s:
  13425. case SIMD_i16x8_extract_lane_u:
  13426. case SIMD_i16x8_replace_lane:
  13427. case SIMD_i32x4_extract_lane:
  13428. case SIMD_i32x4_replace_lane:
  13429. case SIMD_i64x2_extract_lane:
  13430. case SIMD_i64x2_replace_lane:
  13431. case SIMD_f32x4_extract_lane:
  13432. case SIMD_f32x4_replace_lane:
  13433. case SIMD_f64x2_extract_lane:
  13434. case SIMD_f64x2_replace_lane:
  13435. {
  13436. uint8 lane;
  13437. /* clang-format off */
  13438. uint8 replace[] = {
  13439. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13440. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13441. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13442. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13443. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13444. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13445. };
  13446. uint8 push_type[] = {
  13447. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13448. VALUE_TYPE_V128,
  13449. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13450. VALUE_TYPE_V128,
  13451. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13452. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13453. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13454. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13455. };
  13456. /* clang-format on */
  13457. CHECK_BUF(p, p_end, 1);
  13458. lane = read_uint8(p);
  13459. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13460. error_buf_size)) {
  13461. goto fail;
  13462. }
  13463. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13464. if (!(wasm_loader_pop_frame_ref(
  13465. loader_ctx,
  13466. replace[opcode1
  13467. - SIMD_i8x16_extract_lane_s],
  13468. error_buf, error_buf_size)))
  13469. goto fail;
  13470. }
  13471. POP_AND_PUSH(
  13472. VALUE_TYPE_V128,
  13473. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13474. break;
  13475. }
  13476. /* i8x16 compare operation */
  13477. case SIMD_i8x16_eq:
  13478. case SIMD_i8x16_ne:
  13479. case SIMD_i8x16_lt_s:
  13480. case SIMD_i8x16_lt_u:
  13481. case SIMD_i8x16_gt_s:
  13482. case SIMD_i8x16_gt_u:
  13483. case SIMD_i8x16_le_s:
  13484. case SIMD_i8x16_le_u:
  13485. case SIMD_i8x16_ge_s:
  13486. case SIMD_i8x16_ge_u:
  13487. /* i16x8 compare operation */
  13488. case SIMD_i16x8_eq:
  13489. case SIMD_i16x8_ne:
  13490. case SIMD_i16x8_lt_s:
  13491. case SIMD_i16x8_lt_u:
  13492. case SIMD_i16x8_gt_s:
  13493. case SIMD_i16x8_gt_u:
  13494. case SIMD_i16x8_le_s:
  13495. case SIMD_i16x8_le_u:
  13496. case SIMD_i16x8_ge_s:
  13497. case SIMD_i16x8_ge_u:
  13498. /* i32x4 compare operation */
  13499. case SIMD_i32x4_eq:
  13500. case SIMD_i32x4_ne:
  13501. case SIMD_i32x4_lt_s:
  13502. case SIMD_i32x4_lt_u:
  13503. case SIMD_i32x4_gt_s:
  13504. case SIMD_i32x4_gt_u:
  13505. case SIMD_i32x4_le_s:
  13506. case SIMD_i32x4_le_u:
  13507. case SIMD_i32x4_ge_s:
  13508. case SIMD_i32x4_ge_u:
  13509. /* f32x4 compare operation */
  13510. case SIMD_f32x4_eq:
  13511. case SIMD_f32x4_ne:
  13512. case SIMD_f32x4_lt:
  13513. case SIMD_f32x4_gt:
  13514. case SIMD_f32x4_le:
  13515. case SIMD_f32x4_ge:
  13516. /* f64x2 compare operation */
  13517. case SIMD_f64x2_eq:
  13518. case SIMD_f64x2_ne:
  13519. case SIMD_f64x2_lt:
  13520. case SIMD_f64x2_gt:
  13521. case SIMD_f64x2_le:
  13522. case SIMD_f64x2_ge:
  13523. {
  13524. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13525. break;
  13526. }
  13527. /* v128 operation */
  13528. case SIMD_v128_not:
  13529. {
  13530. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13531. break;
  13532. }
  13533. case SIMD_v128_and:
  13534. case SIMD_v128_andnot:
  13535. case SIMD_v128_or:
  13536. case SIMD_v128_xor:
  13537. {
  13538. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13539. break;
  13540. }
  13541. case SIMD_v128_bitselect:
  13542. {
  13543. POP_V128();
  13544. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13545. break;
  13546. }
  13547. case SIMD_v128_any_true:
  13548. {
  13549. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13550. break;
  13551. }
  13552. /* Load Lane Operation */
  13553. case SIMD_v128_load8_lane:
  13554. case SIMD_v128_load16_lane:
  13555. case SIMD_v128_load32_lane:
  13556. case SIMD_v128_load64_lane:
  13557. case SIMD_v128_store8_lane:
  13558. case SIMD_v128_store16_lane:
  13559. case SIMD_v128_store32_lane:
  13560. case SIMD_v128_store64_lane:
  13561. {
  13562. uint8 lane;
  13563. CHECK_MEMORY();
  13564. read_leb_uint32(p, p_end, align); /* align */
  13565. if (!check_simd_memory_access_align(
  13566. opcode1, align, error_buf, error_buf_size)) {
  13567. goto fail;
  13568. }
  13569. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13570. CHECK_BUF(p, p_end, 1);
  13571. lane = read_uint8(p);
  13572. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13573. error_buf_size)) {
  13574. goto fail;
  13575. }
  13576. POP_V128();
  13577. POP_I32();
  13578. if (opcode1 < SIMD_v128_store8_lane) {
  13579. PUSH_V128();
  13580. }
  13581. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13582. func->has_memory_operations = true;
  13583. #endif
  13584. break;
  13585. }
  13586. case SIMD_v128_load32_zero:
  13587. case SIMD_v128_load64_zero:
  13588. {
  13589. CHECK_MEMORY();
  13590. read_leb_uint32(p, p_end, align); /* align */
  13591. if (!check_simd_memory_access_align(
  13592. opcode1, align, error_buf, error_buf_size)) {
  13593. goto fail;
  13594. }
  13595. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13596. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13597. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13598. func->has_memory_operations = true;
  13599. #endif
  13600. break;
  13601. }
  13602. /* Float conversion */
  13603. case SIMD_f32x4_demote_f64x2_zero:
  13604. case SIMD_f64x2_promote_low_f32x4_zero:
  13605. {
  13606. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13607. break;
  13608. }
  13609. /* i8x16 Operation */
  13610. case SIMD_i8x16_abs:
  13611. case SIMD_i8x16_neg:
  13612. case SIMD_i8x16_popcnt:
  13613. {
  13614. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13615. break;
  13616. }
  13617. case SIMD_i8x16_all_true:
  13618. case SIMD_i8x16_bitmask:
  13619. {
  13620. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13621. break;
  13622. }
  13623. case SIMD_i8x16_narrow_i16x8_s:
  13624. case SIMD_i8x16_narrow_i16x8_u:
  13625. {
  13626. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13627. break;
  13628. }
  13629. case SIMD_f32x4_ceil:
  13630. case SIMD_f32x4_floor:
  13631. case SIMD_f32x4_trunc:
  13632. case SIMD_f32x4_nearest:
  13633. {
  13634. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13635. break;
  13636. }
  13637. case SIMD_i8x16_shl:
  13638. case SIMD_i8x16_shr_s:
  13639. case SIMD_i8x16_shr_u:
  13640. {
  13641. POP_I32();
  13642. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13643. break;
  13644. }
  13645. case SIMD_i8x16_add:
  13646. case SIMD_i8x16_add_sat_s:
  13647. case SIMD_i8x16_add_sat_u:
  13648. case SIMD_i8x16_sub:
  13649. case SIMD_i8x16_sub_sat_s:
  13650. case SIMD_i8x16_sub_sat_u:
  13651. {
  13652. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13653. break;
  13654. }
  13655. case SIMD_f64x2_ceil:
  13656. case SIMD_f64x2_floor:
  13657. {
  13658. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13659. break;
  13660. }
  13661. case SIMD_i8x16_min_s:
  13662. case SIMD_i8x16_min_u:
  13663. case SIMD_i8x16_max_s:
  13664. case SIMD_i8x16_max_u:
  13665. {
  13666. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13667. break;
  13668. }
  13669. case SIMD_f64x2_trunc:
  13670. {
  13671. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13672. break;
  13673. }
  13674. case SIMD_i8x16_avgr_u:
  13675. {
  13676. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13677. break;
  13678. }
  13679. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13680. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13681. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13682. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13683. /* i16x8 operation */
  13684. case SIMD_i16x8_abs:
  13685. case SIMD_i16x8_neg:
  13686. {
  13687. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13688. break;
  13689. }
  13690. case SIMD_i16x8_q15mulr_sat_s:
  13691. {
  13692. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13693. break;
  13694. }
  13695. case SIMD_i16x8_all_true:
  13696. case SIMD_i16x8_bitmask:
  13697. {
  13698. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13699. break;
  13700. }
  13701. case SIMD_i16x8_narrow_i32x4_s:
  13702. case SIMD_i16x8_narrow_i32x4_u:
  13703. {
  13704. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13705. break;
  13706. }
  13707. case SIMD_i16x8_extend_low_i8x16_s:
  13708. case SIMD_i16x8_extend_high_i8x16_s:
  13709. case SIMD_i16x8_extend_low_i8x16_u:
  13710. case SIMD_i16x8_extend_high_i8x16_u:
  13711. {
  13712. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13713. break;
  13714. }
  13715. case SIMD_i16x8_shl:
  13716. case SIMD_i16x8_shr_s:
  13717. case SIMD_i16x8_shr_u:
  13718. {
  13719. POP_I32();
  13720. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13721. break;
  13722. }
  13723. case SIMD_i16x8_add:
  13724. case SIMD_i16x8_add_sat_s:
  13725. case SIMD_i16x8_add_sat_u:
  13726. case SIMD_i16x8_sub:
  13727. case SIMD_i16x8_sub_sat_s:
  13728. case SIMD_i16x8_sub_sat_u:
  13729. {
  13730. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13731. break;
  13732. }
  13733. case SIMD_f64x2_nearest:
  13734. {
  13735. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13736. break;
  13737. }
  13738. case SIMD_i16x8_mul:
  13739. case SIMD_i16x8_min_s:
  13740. case SIMD_i16x8_min_u:
  13741. case SIMD_i16x8_max_s:
  13742. case SIMD_i16x8_max_u:
  13743. case SIMD_i16x8_avgr_u:
  13744. case SIMD_i16x8_extmul_low_i8x16_s:
  13745. case SIMD_i16x8_extmul_high_i8x16_s:
  13746. case SIMD_i16x8_extmul_low_i8x16_u:
  13747. case SIMD_i16x8_extmul_high_i8x16_u:
  13748. {
  13749. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13750. break;
  13751. }
  13752. /* i32x4 operation */
  13753. case SIMD_i32x4_abs:
  13754. case SIMD_i32x4_neg:
  13755. {
  13756. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13757. break;
  13758. }
  13759. case SIMD_i32x4_all_true:
  13760. case SIMD_i32x4_bitmask:
  13761. {
  13762. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13763. break;
  13764. }
  13765. case SIMD_i32x4_extend_low_i16x8_s:
  13766. case SIMD_i32x4_extend_high_i16x8_s:
  13767. case SIMD_i32x4_extend_low_i16x8_u:
  13768. case SIMD_i32x4_extend_high_i16x8_u:
  13769. {
  13770. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13771. break;
  13772. }
  13773. case SIMD_i32x4_shl:
  13774. case SIMD_i32x4_shr_s:
  13775. case SIMD_i32x4_shr_u:
  13776. {
  13777. POP_I32();
  13778. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13779. break;
  13780. }
  13781. case SIMD_i32x4_add:
  13782. case SIMD_i32x4_sub:
  13783. case SIMD_i32x4_mul:
  13784. case SIMD_i32x4_min_s:
  13785. case SIMD_i32x4_min_u:
  13786. case SIMD_i32x4_max_s:
  13787. case SIMD_i32x4_max_u:
  13788. case SIMD_i32x4_dot_i16x8_s:
  13789. case SIMD_i32x4_extmul_low_i16x8_s:
  13790. case SIMD_i32x4_extmul_high_i16x8_s:
  13791. case SIMD_i32x4_extmul_low_i16x8_u:
  13792. case SIMD_i32x4_extmul_high_i16x8_u:
  13793. {
  13794. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13795. break;
  13796. }
  13797. /* i64x2 operation */
  13798. case SIMD_i64x2_abs:
  13799. case SIMD_i64x2_neg:
  13800. {
  13801. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13802. break;
  13803. }
  13804. case SIMD_i64x2_all_true:
  13805. case SIMD_i64x2_bitmask:
  13806. {
  13807. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13808. break;
  13809. }
  13810. case SIMD_i64x2_extend_low_i32x4_s:
  13811. case SIMD_i64x2_extend_high_i32x4_s:
  13812. case SIMD_i64x2_extend_low_i32x4_u:
  13813. case SIMD_i64x2_extend_high_i32x4_u:
  13814. {
  13815. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13816. break;
  13817. }
  13818. case SIMD_i64x2_shl:
  13819. case SIMD_i64x2_shr_s:
  13820. case SIMD_i64x2_shr_u:
  13821. {
  13822. POP_I32();
  13823. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13824. break;
  13825. }
  13826. case SIMD_i64x2_add:
  13827. case SIMD_i64x2_sub:
  13828. case SIMD_i64x2_mul:
  13829. case SIMD_i64x2_eq:
  13830. case SIMD_i64x2_ne:
  13831. case SIMD_i64x2_lt_s:
  13832. case SIMD_i64x2_gt_s:
  13833. case SIMD_i64x2_le_s:
  13834. case SIMD_i64x2_ge_s:
  13835. case SIMD_i64x2_extmul_low_i32x4_s:
  13836. case SIMD_i64x2_extmul_high_i32x4_s:
  13837. case SIMD_i64x2_extmul_low_i32x4_u:
  13838. case SIMD_i64x2_extmul_high_i32x4_u:
  13839. {
  13840. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13841. break;
  13842. }
  13843. /* f32x4 operation */
  13844. case SIMD_f32x4_abs:
  13845. case SIMD_f32x4_neg:
  13846. case SIMD_f32x4_sqrt:
  13847. {
  13848. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13849. break;
  13850. }
  13851. case SIMD_f32x4_add:
  13852. case SIMD_f32x4_sub:
  13853. case SIMD_f32x4_mul:
  13854. case SIMD_f32x4_div:
  13855. case SIMD_f32x4_min:
  13856. case SIMD_f32x4_max:
  13857. case SIMD_f32x4_pmin:
  13858. case SIMD_f32x4_pmax:
  13859. {
  13860. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13861. break;
  13862. }
  13863. /* f64x2 operation */
  13864. case SIMD_f64x2_abs:
  13865. case SIMD_f64x2_neg:
  13866. case SIMD_f64x2_sqrt:
  13867. {
  13868. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13869. break;
  13870. }
  13871. case SIMD_f64x2_add:
  13872. case SIMD_f64x2_sub:
  13873. case SIMD_f64x2_mul:
  13874. case SIMD_f64x2_div:
  13875. case SIMD_f64x2_min:
  13876. case SIMD_f64x2_max:
  13877. case SIMD_f64x2_pmin:
  13878. case SIMD_f64x2_pmax:
  13879. {
  13880. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13881. break;
  13882. }
  13883. case SIMD_i32x4_trunc_sat_f32x4_s:
  13884. case SIMD_i32x4_trunc_sat_f32x4_u:
  13885. case SIMD_f32x4_convert_i32x4_s:
  13886. case SIMD_f32x4_convert_i32x4_u:
  13887. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13888. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13889. case SIMD_f64x2_convert_low_i32x4_s:
  13890. case SIMD_f64x2_convert_low_i32x4_u:
  13891. {
  13892. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13893. break;
  13894. }
  13895. default:
  13896. {
  13897. if (error_buf != NULL) {
  13898. snprintf(error_buf, error_buf_size,
  13899. "WASM module load failed: "
  13900. "invalid opcode 0xfd %02x.",
  13901. opcode1);
  13902. }
  13903. goto fail;
  13904. }
  13905. }
  13906. break;
  13907. }
  13908. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  13909. #endif /* end of WASM_ENABLE_SIMD */
  13910. #if WASM_ENABLE_SHARED_MEMORY != 0
  13911. case WASM_OP_ATOMIC_PREFIX:
  13912. {
  13913. uint32 opcode1;
  13914. read_leb_uint32(p, p_end, opcode1);
  13915. #if WASM_ENABLE_FAST_INTERP != 0
  13916. emit_byte(loader_ctx, opcode1);
  13917. #endif
  13918. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  13919. CHECK_MEMORY();
  13920. read_leb_uint32(p, p_end, align); /* align */
  13921. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13922. if (!check_memory_align_equal(opcode1, align, error_buf,
  13923. error_buf_size)) {
  13924. goto fail;
  13925. }
  13926. #if WASM_ENABLE_FAST_INTERP != 0
  13927. emit_uint32(loader_ctx, mem_offset);
  13928. #endif
  13929. }
  13930. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13931. func->has_memory_operations = true;
  13932. #endif
  13933. switch (opcode1) {
  13934. case WASM_OP_ATOMIC_NOTIFY:
  13935. POP_I32();
  13936. POP_MEM_OFFSET();
  13937. PUSH_I32();
  13938. break;
  13939. case WASM_OP_ATOMIC_WAIT32:
  13940. POP_I64();
  13941. POP_I32();
  13942. POP_MEM_OFFSET();
  13943. PUSH_I32();
  13944. break;
  13945. case WASM_OP_ATOMIC_WAIT64:
  13946. POP_I64();
  13947. POP_I64();
  13948. POP_MEM_OFFSET();
  13949. PUSH_I32();
  13950. break;
  13951. case WASM_OP_ATOMIC_FENCE:
  13952. /* reserved byte 0x00 */
  13953. if (*p++ != 0x00) {
  13954. set_error_buf(error_buf, error_buf_size,
  13955. "zero byte expected");
  13956. goto fail;
  13957. }
  13958. break;
  13959. case WASM_OP_ATOMIC_I32_LOAD:
  13960. case WASM_OP_ATOMIC_I32_LOAD8_U:
  13961. case WASM_OP_ATOMIC_I32_LOAD16_U:
  13962. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  13963. break;
  13964. case WASM_OP_ATOMIC_I32_STORE:
  13965. case WASM_OP_ATOMIC_I32_STORE8:
  13966. case WASM_OP_ATOMIC_I32_STORE16:
  13967. POP_I32();
  13968. POP_MEM_OFFSET();
  13969. break;
  13970. case WASM_OP_ATOMIC_I64_LOAD:
  13971. case WASM_OP_ATOMIC_I64_LOAD8_U:
  13972. case WASM_OP_ATOMIC_I64_LOAD16_U:
  13973. case WASM_OP_ATOMIC_I64_LOAD32_U:
  13974. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  13975. break;
  13976. case WASM_OP_ATOMIC_I64_STORE:
  13977. case WASM_OP_ATOMIC_I64_STORE8:
  13978. case WASM_OP_ATOMIC_I64_STORE16:
  13979. case WASM_OP_ATOMIC_I64_STORE32:
  13980. POP_I64();
  13981. POP_MEM_OFFSET();
  13982. break;
  13983. case WASM_OP_ATOMIC_RMW_I32_ADD:
  13984. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  13985. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  13986. case WASM_OP_ATOMIC_RMW_I32_SUB:
  13987. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  13988. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  13989. case WASM_OP_ATOMIC_RMW_I32_AND:
  13990. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  13991. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  13992. case WASM_OP_ATOMIC_RMW_I32_OR:
  13993. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  13994. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  13995. case WASM_OP_ATOMIC_RMW_I32_XOR:
  13996. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  13997. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  13998. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  13999. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  14000. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  14001. POP_I32();
  14002. POP_MEM_OFFSET();
  14003. PUSH_I32();
  14004. break;
  14005. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14006. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14007. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14008. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14009. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14010. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14011. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14012. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14013. case WASM_OP_ATOMIC_RMW_I64_AND:
  14014. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14015. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14016. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14017. case WASM_OP_ATOMIC_RMW_I64_OR:
  14018. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14019. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14020. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14021. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14022. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14023. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14024. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14025. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14026. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14027. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14028. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14029. POP_I64();
  14030. POP_MEM_OFFSET();
  14031. PUSH_I64();
  14032. break;
  14033. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14034. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14035. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14036. POP_I32();
  14037. POP_I32();
  14038. POP_MEM_OFFSET();
  14039. PUSH_I32();
  14040. break;
  14041. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14042. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14043. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14044. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14045. POP_I64();
  14046. POP_I64();
  14047. POP_MEM_OFFSET();
  14048. PUSH_I64();
  14049. break;
  14050. default:
  14051. set_error_buf_v(error_buf, error_buf_size,
  14052. "%s %02x %02x", "unsupported opcode",
  14053. 0xfe, opcode1);
  14054. goto fail;
  14055. }
  14056. break;
  14057. }
  14058. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14059. default:
  14060. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14061. "unsupported opcode", opcode);
  14062. goto fail;
  14063. }
  14064. #if WASM_ENABLE_FAST_INTERP != 0
  14065. last_op = opcode;
  14066. #endif
  14067. }
  14068. if (loader_ctx->csp_num > 0) {
  14069. if (cur_func_idx < module->function_count - 1)
  14070. /* Function with missing end marker (between two functions) */
  14071. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14072. else
  14073. /* Function with missing end marker
  14074. (at EOF or end of code sections) */
  14075. set_error_buf(error_buf, error_buf_size,
  14076. "unexpected end of section or function, "
  14077. "or section size mismatch");
  14078. goto fail;
  14079. }
  14080. #if WASM_ENABLE_FAST_INTERP != 0
  14081. if (loader_ctx->p_code_compiled == NULL)
  14082. goto re_scan;
  14083. func->const_cell_num = loader_ctx->const_cell_num;
  14084. if (func->const_cell_num > 0) {
  14085. int32 j;
  14086. if (!(func->consts = func_const = loader_malloc(
  14087. func->const_cell_num * 4, error_buf, error_buf_size)))
  14088. goto fail;
  14089. func_const_end = func->consts + func->const_cell_num * 4;
  14090. /* reverse the const buf */
  14091. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14092. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14093. if (c->value_type == VALUE_TYPE_F64
  14094. || c->value_type == VALUE_TYPE_I64) {
  14095. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14096. &(c->value.f64), (uint32)sizeof(int64));
  14097. func_const += sizeof(int64);
  14098. }
  14099. else {
  14100. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14101. &(c->value.f32), (uint32)sizeof(int32));
  14102. func_const += sizeof(int32);
  14103. }
  14104. }
  14105. }
  14106. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14107. - loader_ctx->start_dynamic_offset + 1;
  14108. #else
  14109. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14110. #endif
  14111. func->max_block_num = loader_ctx->max_csp_num;
  14112. return_value = true;
  14113. fail:
  14114. wasm_loader_ctx_destroy(loader_ctx);
  14115. (void)table_idx;
  14116. (void)table_seg_idx;
  14117. (void)data_seg_idx;
  14118. (void)i64_const;
  14119. (void)local_offset;
  14120. (void)p_org;
  14121. (void)mem_offset;
  14122. (void)align;
  14123. return return_value;
  14124. }