wasm_loader.c 574 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641
  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
  268. || type == VALUE_TYPE_F32
  269. /* the operand stack is in polymorphic state */
  270. || type == VALUE_TYPE_ANY
  271. #if WASM_ENABLE_GC != 0
  272. || (sizeof(uintptr_t) == 4 && wasm_is_type_reftype(type))
  273. #elif WASM_ENABLE_REF_TYPES != 0
  274. /* For reference types, we use uint32 index to represent
  275. the funcref and externref */
  276. || type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF
  277. #endif
  278. )
  279. return true;
  280. return false;
  281. }
  282. static bool
  283. is_64bit_type(uint8 type)
  284. {
  285. if (type == VALUE_TYPE_I64 || type == VALUE_TYPE_F64
  286. #if WASM_ENABLE_GC != 0
  287. || (sizeof(uintptr_t) == 8 && wasm_is_type_reftype(type))
  288. #endif
  289. )
  290. return true;
  291. return false;
  292. }
  293. static bool
  294. is_value_type(uint8 type)
  295. {
  296. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  297. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  298. #if WASM_ENABLE_GC != 0
  299. /* reference types, 0x65 to 0x70 */
  300. || wasm_is_type_reftype(type)
  301. #elif WASM_ENABLE_REF_TYPES != 0
  302. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  303. #endif
  304. #if WASM_ENABLE_SIMD != 0
  305. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  306. || type == VALUE_TYPE_V128 /* 0x7B */
  307. #endif
  308. #endif
  309. )
  310. return true;
  311. return false;
  312. }
  313. #if WASM_ENABLE_GC != 0
  314. static bool
  315. is_packed_type(uint8 type)
  316. {
  317. return (type == PACKED_TYPE_I8 || type == PACKED_TYPE_I16) ? true : false;
  318. }
  319. #endif
  320. static bool
  321. is_byte_a_type(uint8 type)
  322. {
  323. return (is_value_type(type) || (type == VALUE_TYPE_VOID)) ? true : false;
  324. }
  325. #if WASM_ENABLE_SIMD != 0
  326. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  327. static V128
  328. read_i8x16(uint8 *p_buf, char *error_buf, uint32 error_buf_size)
  329. {
  330. V128 result;
  331. uint8 i;
  332. for (i = 0; i != 16; ++i) {
  333. result.i8x16[i] = read_uint8(p_buf);
  334. }
  335. return result;
  336. }
  337. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  338. #endif /* end of WASM_ENABLE_SIMD */
  339. static void *
  340. loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
  341. {
  342. void *mem;
  343. if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
  344. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  345. return NULL;
  346. }
  347. memset(mem, 0, (uint32)size);
  348. return mem;
  349. }
  350. static void *
  351. memory_realloc(void *mem_old, uint32 size_old, uint32 size_new, char *error_buf,
  352. uint32 error_buf_size)
  353. {
  354. uint8 *mem_new;
  355. bh_assert(size_new > size_old);
  356. if ((mem_new = loader_malloc(size_new, error_buf, error_buf_size))) {
  357. bh_memcpy_s(mem_new, size_new, mem_old, size_old);
  358. memset(mem_new + size_old, 0, size_new - size_old);
  359. wasm_runtime_free(mem_old);
  360. }
  361. return mem_new;
  362. }
  363. #define MEM_REALLOC(mem, size_old, size_new) \
  364. do { \
  365. void *mem_new = memory_realloc(mem, size_old, size_new, error_buf, \
  366. error_buf_size); \
  367. if (!mem_new) \
  368. goto fail; \
  369. mem = mem_new; \
  370. } while (0)
  371. #if WASM_ENABLE_GC != 0
  372. static bool
  373. check_type_index(const WASMModule *module, uint32 type_count, uint32 type_index,
  374. char *error_buf, uint32 error_buf_size)
  375. {
  376. if (type_index >= type_count) {
  377. set_error_buf_v(error_buf, error_buf_size, "unknown type %d",
  378. type_index);
  379. return false;
  380. }
  381. return true;
  382. }
  383. static bool
  384. check_array_type(const WASMModule *module, uint32 type_index, char *error_buf,
  385. uint32 error_buf_size)
  386. {
  387. if (!check_type_index(module, module->type_count, type_index, error_buf,
  388. error_buf_size)) {
  389. return false;
  390. }
  391. if (module->types[type_index]->type_flag != WASM_TYPE_ARRAY) {
  392. set_error_buf(error_buf, error_buf_size, "unkown array type");
  393. return false;
  394. }
  395. return true;
  396. }
  397. #endif
  398. static bool
  399. check_function_index(const WASMModule *module, uint32 function_index,
  400. char *error_buf, uint32 error_buf_size)
  401. {
  402. if (function_index
  403. >= module->import_function_count + module->function_count) {
  404. set_error_buf_v(error_buf, error_buf_size, "unknown function %u",
  405. function_index);
  406. return false;
  407. }
  408. return true;
  409. }
  410. typedef struct InitValue {
  411. uint8 type;
  412. uint8 flag;
  413. #if WASM_ENABLE_GC != 0
  414. uint8 gc_opcode;
  415. WASMRefType ref_type;
  416. #endif
  417. WASMValue value;
  418. } InitValue;
  419. typedef struct ConstExprContext {
  420. uint32 sp;
  421. uint32 size;
  422. WASMModule *module;
  423. InitValue *stack;
  424. InitValue data[WASM_CONST_EXPR_STACK_SIZE];
  425. } ConstExprContext;
  426. static void
  427. init_const_expr_stack(ConstExprContext *ctx, WASMModule *module)
  428. {
  429. ctx->sp = 0;
  430. ctx->module = module;
  431. ctx->stack = ctx->data;
  432. ctx->size = WASM_CONST_EXPR_STACK_SIZE;
  433. }
  434. static bool
  435. push_const_expr_stack(ConstExprContext *ctx, uint8 flag, uint8 type,
  436. #if WASM_ENABLE_GC != 0
  437. WASMRefType *ref_type, uint8 gc_opcode,
  438. #endif
  439. WASMValue *value, char *error_buf, uint32 error_buf_size)
  440. {
  441. InitValue *cur_value;
  442. if (ctx->sp >= ctx->size) {
  443. if (ctx->stack != ctx->data) {
  444. MEM_REALLOC(ctx->stack, ctx->size * sizeof(InitValue),
  445. (ctx->size + 4) * sizeof(InitValue));
  446. }
  447. else {
  448. if (!(ctx->stack =
  449. loader_malloc((ctx->size + 4) * (uint64)sizeof(InitValue),
  450. error_buf, error_buf_size))) {
  451. goto fail;
  452. }
  453. }
  454. ctx->size += 4;
  455. }
  456. cur_value = &ctx->stack[ctx->sp++];
  457. cur_value->type = type;
  458. cur_value->flag = flag;
  459. cur_value->value = *value;
  460. #if WASM_ENABLE_GC != 0
  461. cur_value->gc_opcode = gc_opcode;
  462. if (wasm_is_type_multi_byte_type(type)) {
  463. bh_memcpy_s(&cur_value->ref_type, wasm_reftype_struct_size(ref_type),
  464. ref_type, wasm_reftype_struct_size(ref_type));
  465. }
  466. #endif
  467. return true;
  468. fail:
  469. return false;
  470. }
  471. static bool
  472. pop_const_expr_stack(ConstExprContext *ctx, uint8 *p_flag, uint8 type,
  473. #if WASM_ENABLE_GC != 0
  474. WASMRefType *ref_type, uint8 *p_gc_opcode,
  475. #endif
  476. WASMValue *p_value, char *error_buf, uint32 error_buf_size)
  477. {
  478. InitValue *cur_value;
  479. if (ctx->sp == 0) {
  480. set_error_buf(error_buf, error_buf_size,
  481. "type mismatch: const expr stack underflow");
  482. return false;
  483. }
  484. cur_value = &ctx->stack[--ctx->sp];
  485. #if WASM_ENABLE_GC == 0
  486. if (cur_value->type != type) {
  487. set_error_buf(error_buf, error_buf_size, "type mismatch");
  488. return false;
  489. }
  490. #else
  491. if (!wasm_reftype_is_subtype_of(cur_value->type, &cur_value->ref_type, type,
  492. ref_type, ctx->module->types,
  493. ctx->module->type_count)) {
  494. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  495. "type mismatch: expect ", type2str(type),
  496. " but got other");
  497. goto fail;
  498. }
  499. if ((ctx->sp != 0) && (cur_value->flag == WASM_OP_GC_PREFIX)
  500. && (cur_value->gc_opcode != WASM_OP_REF_I31)) {
  501. /* To reduce complexity, we don't allow initialize struct fields/array
  502. * element with references, so struct/array must be at the bottom of the
  503. * init value stack */
  504. set_error_buf(
  505. error_buf, error_buf_size,
  506. "struct or array as field is not supported in constant expr");
  507. goto fail;
  508. }
  509. #endif
  510. if (p_flag)
  511. *p_flag = cur_value->flag;
  512. if (p_value)
  513. *p_value = cur_value->value;
  514. #if WASM_ENABLE_GC != 0
  515. if (p_gc_opcode)
  516. *p_gc_opcode = cur_value->gc_opcode;
  517. #endif
  518. return true;
  519. #if WASM_ENABLE_GC != 0
  520. fail:
  521. if ((cur_value->flag == WASM_OP_GC_PREFIX)
  522. && (cur_value->gc_opcode == WASM_OP_STRUCT_NEW
  523. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW
  524. || cur_value->gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  525. wasm_runtime_free(cur_value->value.data);
  526. }
  527. return false;
  528. #endif
  529. }
  530. static void
  531. destroy_const_expr_stack(ConstExprContext *ctx)
  532. {
  533. #if WASM_ENABLE_GC != 0
  534. uint32 i;
  535. for (i = 0; i < ctx->sp; i++) {
  536. if ((ctx->stack[i].flag == WASM_OP_GC_PREFIX)
  537. && (ctx->stack[i].gc_opcode == WASM_OP_STRUCT_NEW
  538. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW
  539. || ctx->stack[i].gc_opcode == WASM_OP_ARRAY_NEW_FIXED)) {
  540. wasm_runtime_free(ctx->stack[i].value.data);
  541. }
  542. }
  543. #endif
  544. if (ctx->stack != ctx->data) {
  545. wasm_runtime_free(ctx->stack);
  546. }
  547. }
  548. #if WASM_ENABLE_GC != 0
  549. static void
  550. destroy_init_expr(InitializerExpression *expr)
  551. {
  552. if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
  553. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
  554. || expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  555. wasm_runtime_free(expr->u.data);
  556. }
  557. }
  558. #endif /* end of WASM_ENABLE_GC != 0 */
  559. static bool
  560. load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  561. InitializerExpression *init_expr, uint8 type, void *ref_type,
  562. char *error_buf, uint32 error_buf_size)
  563. {
  564. const uint8 *p = *p_buf, *p_end = buf_end;
  565. uint8 flag, *p_float;
  566. uint32 i;
  567. ConstExprContext const_expr_ctx = { 0 };
  568. WASMValue cur_value;
  569. #if WASM_ENABLE_GC != 0
  570. uint32 opcode1, type_idx;
  571. uint8 opcode;
  572. WASMRefType cur_ref_type = { 0 };
  573. #endif
  574. init_const_expr_stack(&const_expr_ctx, module);
  575. CHECK_BUF(p, p_end, 1);
  576. flag = read_uint8(p);
  577. while (flag != WASM_OP_END) {
  578. switch (flag) {
  579. /* i32.const */
  580. case INIT_EXPR_TYPE_I32_CONST:
  581. read_leb_int32(p, p_end, cur_value.i32);
  582. if (!push_const_expr_stack(
  583. &const_expr_ctx, flag, VALUE_TYPE_I32,
  584. #if WASM_ENABLE_GC != 0
  585. NULL, 0,
  586. #endif
  587. &cur_value, error_buf, error_buf_size))
  588. goto fail;
  589. break;
  590. /* i64.const */
  591. case INIT_EXPR_TYPE_I64_CONST:
  592. read_leb_int64(p, p_end, cur_value.i64);
  593. if (!push_const_expr_stack(
  594. &const_expr_ctx, flag, VALUE_TYPE_I64,
  595. #if WASM_ENABLE_GC != 0
  596. NULL, 0,
  597. #endif
  598. &cur_value, error_buf, error_buf_size))
  599. goto fail;
  600. break;
  601. /* f32.const */
  602. case INIT_EXPR_TYPE_F32_CONST:
  603. CHECK_BUF(p, p_end, 4);
  604. p_float = (uint8 *)&cur_value.f32;
  605. for (i = 0; i < sizeof(float32); i++)
  606. *p_float++ = *p++;
  607. if (!push_const_expr_stack(
  608. &const_expr_ctx, flag, VALUE_TYPE_F32,
  609. #if WASM_ENABLE_GC != 0
  610. NULL, 0,
  611. #endif
  612. &cur_value, error_buf, error_buf_size))
  613. goto fail;
  614. break;
  615. /* f64.const */
  616. case INIT_EXPR_TYPE_F64_CONST:
  617. CHECK_BUF(p, p_end, 8);
  618. p_float = (uint8 *)&cur_value.f64;
  619. for (i = 0; i < sizeof(float64); i++)
  620. *p_float++ = *p++;
  621. if (!push_const_expr_stack(
  622. &const_expr_ctx, flag, VALUE_TYPE_F64,
  623. #if WASM_ENABLE_GC != 0
  624. NULL, 0,
  625. #endif
  626. &cur_value, error_buf, error_buf_size))
  627. goto fail;
  628. break;
  629. #if WASM_ENABLE_SIMD != 0
  630. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  631. /* v128.const */
  632. case INIT_EXPR_TYPE_V128_CONST:
  633. {
  634. uint64 high, low;
  635. CHECK_BUF(p, p_end, 1);
  636. (void)read_uint8(p);
  637. CHECK_BUF(p, p_end, 16);
  638. wasm_runtime_read_v128(p, &high, &low);
  639. p += 16;
  640. cur_value.v128.i64x2[0] = high;
  641. cur_value.v128.i64x2[1] = low;
  642. if (!push_const_expr_stack(
  643. &const_expr_ctx, flag, VALUE_TYPE_V128,
  644. #if WASM_ENABLE_GC != 0
  645. NULL, 0,
  646. #endif
  647. &cur_value, error_buf, error_buf_size))
  648. goto fail;
  649. #if WASM_ENABLE_WAMR_COMPILER != 0
  650. /* If any init_expr is v128.const, mark SIMD used */
  651. module->is_simd_used = true;
  652. #endif
  653. break;
  654. }
  655. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  656. #endif /* end of WASM_ENABLE_SIMD */
  657. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  658. /* ref.func */
  659. case INIT_EXPR_TYPE_FUNCREF_CONST:
  660. {
  661. uint32 func_idx;
  662. read_leb_uint32(p, p_end, func_idx);
  663. cur_value.ref_index = func_idx;
  664. if (!check_function_index(module, func_idx, error_buf,
  665. error_buf_size)) {
  666. goto fail;
  667. }
  668. #if WASM_ENABLE_GC == 0
  669. if (!push_const_expr_stack(&const_expr_ctx, flag,
  670. VALUE_TYPE_FUNCREF, &cur_value,
  671. error_buf, error_buf_size))
  672. goto fail;
  673. #else
  674. if (func_idx < module->import_function_count) {
  675. type_idx =
  676. module->import_functions[func_idx].u.function.type_idx;
  677. }
  678. else {
  679. type_idx = module
  680. ->functions[func_idx
  681. - module->import_function_count]
  682. ->type_idx;
  683. }
  684. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  685. false, type_idx);
  686. if (!push_const_expr_stack(&const_expr_ctx, flag,
  687. cur_ref_type.ref_type, &cur_ref_type,
  688. 0, &cur_value, error_buf,
  689. error_buf_size))
  690. goto fail;
  691. #endif
  692. #if WASM_ENABLE_WAMR_COMPILER != 0
  693. module->is_ref_types_used = true;
  694. #endif
  695. break;
  696. }
  697. /* ref.null */
  698. case INIT_EXPR_TYPE_REFNULL_CONST:
  699. {
  700. uint8 type1;
  701. CHECK_BUF(p, p_end, 1);
  702. type1 = read_uint8(p);
  703. #if WASM_ENABLE_GC == 0
  704. cur_value.ref_index = NULL_REF;
  705. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  706. &cur_value, error_buf,
  707. error_buf_size))
  708. goto fail;
  709. #else
  710. cur_value.gc_obj = NULL_REF;
  711. if (!is_byte_a_type(type1)) {
  712. p--;
  713. read_leb_uint32(p, p_end, type_idx);
  714. if (!check_type_index(module, module->type_count, type_idx,
  715. error_buf, error_buf_size))
  716. goto fail;
  717. wasm_set_refheaptype_typeidx(&cur_ref_type.ref_ht_typeidx,
  718. true, type_idx);
  719. if (!push_const_expr_stack(&const_expr_ctx, flag,
  720. cur_ref_type.ref_type,
  721. &cur_ref_type, 0, &cur_value,
  722. error_buf, error_buf_size))
  723. goto fail;
  724. }
  725. else {
  726. if (!push_const_expr_stack(&const_expr_ctx, flag, type1,
  727. NULL, 0, &cur_value, error_buf,
  728. error_buf_size))
  729. goto fail;
  730. }
  731. #endif
  732. #if WASM_ENABLE_WAMR_COMPILER != 0
  733. module->is_ref_types_used = true;
  734. #endif
  735. break;
  736. }
  737. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  738. /* get_global */
  739. case INIT_EXPR_TYPE_GET_GLOBAL:
  740. {
  741. uint32 global_idx;
  742. uint8 global_type;
  743. read_leb_uint32(p, p_end, cur_value.global_index);
  744. global_idx = cur_value.global_index;
  745. #if WASM_ENABLE_GC == 0
  746. if (global_idx >= module->import_global_count) {
  747. /**
  748. * Currently, constant expressions occurring as initializers
  749. * of globals are further constrained in that contained
  750. * global.get instructions are
  751. * only allowed to refer to imported globals.
  752. */
  753. set_error_buf_v(error_buf, error_buf_size,
  754. "unknown global %u", global_idx);
  755. goto fail;
  756. }
  757. if (module->import_globals[global_idx].u.global.is_mutable) {
  758. set_error_buf_v(error_buf, error_buf_size,
  759. "constant expression required");
  760. goto fail;
  761. }
  762. #else
  763. if (global_idx
  764. >= module->import_global_count + module->global_count) {
  765. set_error_buf_v(error_buf, error_buf_size,
  766. "unknown global %u", global_idx);
  767. goto fail;
  768. }
  769. if (global_idx < module->import_global_count
  770. && module->import_globals[global_idx].u.global.is_mutable) {
  771. set_error_buf_v(error_buf, error_buf_size,
  772. "constant expression required");
  773. goto fail;
  774. }
  775. #endif
  776. if (global_idx < module->import_global_count) {
  777. global_type =
  778. module->import_globals[global_idx].u.global.type;
  779. #if WASM_ENABLE_GC != 0
  780. if (wasm_is_type_multi_byte_type(global_type)) {
  781. WASMRefType *global_ref_type =
  782. module->import_globals[global_idx]
  783. .u.global.ref_type;
  784. bh_memcpy_s(&cur_ref_type,
  785. wasm_reftype_struct_size(global_ref_type),
  786. global_ref_type,
  787. wasm_reftype_struct_size(global_ref_type));
  788. }
  789. #endif
  790. }
  791. else {
  792. global_type =
  793. module
  794. ->globals[global_idx - module->import_global_count]
  795. .type;
  796. #if WASM_ENABLE_GC != 0
  797. if (wasm_is_type_multi_byte_type(global_type)) {
  798. WASMRefType *global_ref_type =
  799. module
  800. ->globals[global_idx
  801. - module->import_global_count]
  802. .ref_type;
  803. bh_memcpy_s(&cur_ref_type,
  804. wasm_reftype_struct_size(global_ref_type),
  805. global_ref_type,
  806. wasm_reftype_struct_size(global_ref_type));
  807. }
  808. #endif
  809. }
  810. if (!push_const_expr_stack(&const_expr_ctx, flag, global_type,
  811. #if WASM_ENABLE_GC != 0
  812. &cur_ref_type, 0,
  813. #endif
  814. &cur_value, error_buf,
  815. error_buf_size))
  816. goto fail;
  817. break;
  818. }
  819. #if WASM_ENABLE_GC != 0
  820. /* struct.new and array.new */
  821. case WASM_OP_GC_PREFIX:
  822. {
  823. read_leb_uint32(p, p_end, opcode1);
  824. switch (opcode1) {
  825. case WASM_OP_STRUCT_NEW:
  826. {
  827. WASMStructType *struct_type;
  828. WASMStructNewInitValues *struct_init_values = NULL;
  829. uint32 field_count;
  830. read_leb_uint32(p, p_end, type_idx);
  831. if (!check_type_index(module, module->type_count,
  832. type_idx, error_buf,
  833. error_buf_size)) {
  834. goto fail;
  835. }
  836. struct_type = (WASMStructType *)module->types[type_idx];
  837. if (struct_type->base_type.type_flag
  838. != WASM_TYPE_STRUCT) {
  839. set_error_buf(error_buf, error_buf_size,
  840. "unkown struct type");
  841. goto fail;
  842. }
  843. field_count = struct_type->field_count;
  844. if (!(struct_init_values = loader_malloc(
  845. offsetof(WASMStructNewInitValues, fields)
  846. + (uint64)field_count * sizeof(WASMValue),
  847. error_buf, error_buf_size))) {
  848. goto fail;
  849. }
  850. struct_init_values->count = field_count;
  851. for (i = field_count; i > 0; i--) {
  852. WASMRefType *field_ref_type = NULL;
  853. uint32 field_idx = i - 1;
  854. uint8 field_type =
  855. struct_type->fields[field_idx].field_type;
  856. if (wasm_is_type_multi_byte_type(field_type)) {
  857. field_ref_type = wasm_reftype_map_find(
  858. struct_type->ref_type_maps,
  859. struct_type->ref_type_map_count, field_idx);
  860. }
  861. if (is_packed_type(field_type)) {
  862. field_type = VALUE_TYPE_I32;
  863. }
  864. if (!pop_const_expr_stack(
  865. &const_expr_ctx, NULL, field_type,
  866. field_ref_type, NULL,
  867. &struct_init_values->fields[field_idx],
  868. error_buf, error_buf_size)) {
  869. wasm_runtime_free(struct_init_values);
  870. goto fail;
  871. }
  872. }
  873. cur_value.data = struct_init_values;
  874. wasm_set_refheaptype_typeidx(
  875. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  876. if (!push_const_expr_stack(
  877. &const_expr_ctx, flag, cur_ref_type.ref_type,
  878. &cur_ref_type, (uint8)opcode1, &cur_value,
  879. error_buf, error_buf_size)) {
  880. wasm_runtime_free(struct_init_values);
  881. goto fail;
  882. }
  883. break;
  884. }
  885. case WASM_OP_STRUCT_NEW_DEFAULT:
  886. {
  887. read_leb_uint32(p, p_end, cur_value.type_index);
  888. type_idx = cur_value.type_index;
  889. if (!check_type_index(module, module->type_count,
  890. type_idx, error_buf,
  891. error_buf_size)) {
  892. goto fail;
  893. }
  894. if (module->types[type_idx]->type_flag
  895. != WASM_TYPE_STRUCT) {
  896. set_error_buf(error_buf, error_buf_size,
  897. "unkown struct type");
  898. goto fail;
  899. }
  900. cur_value.type_index = type_idx;
  901. wasm_set_refheaptype_typeidx(
  902. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  903. if (!push_const_expr_stack(
  904. &const_expr_ctx, flag, cur_ref_type.ref_type,
  905. &cur_ref_type, (uint8)opcode1, &cur_value,
  906. error_buf, error_buf_size)) {
  907. goto fail;
  908. }
  909. break;
  910. }
  911. case WASM_OP_ARRAY_NEW:
  912. case WASM_OP_ARRAY_NEW_DEFAULT:
  913. case WASM_OP_ARRAY_NEW_FIXED:
  914. {
  915. WASMArrayNewInitValues *array_init_values = NULL;
  916. WASMArrayType *array_type = NULL;
  917. WASMRefType *elem_ref_type = NULL;
  918. uint64 total_size;
  919. uint8 elem_type;
  920. read_leb_uint32(p, p_end, cur_value.type_index);
  921. type_idx = cur_value.type_index;
  922. if (!check_type_index(module, module->type_count,
  923. type_idx, error_buf,
  924. error_buf_size)) {
  925. goto fail;
  926. }
  927. array_type = (WASMArrayType *)module->types[type_idx];
  928. if (array_type->base_type.type_flag
  929. != WASM_TYPE_ARRAY) {
  930. set_error_buf(error_buf, error_buf_size,
  931. "unkown array type");
  932. goto fail;
  933. }
  934. if (opcode1 != WASM_OP_ARRAY_NEW_DEFAULT) {
  935. elem_type = array_type->elem_type;
  936. if (wasm_is_type_multi_byte_type(elem_type)) {
  937. elem_ref_type = array_type->elem_ref_type;
  938. }
  939. if (is_packed_type(elem_type)) {
  940. elem_type = VALUE_TYPE_I32;
  941. }
  942. if (opcode1 == WASM_OP_ARRAY_NEW) {
  943. WASMValue len_val;
  944. if (!(array_init_values = loader_malloc(
  945. sizeof(WASMArrayNewInitValues),
  946. error_buf, error_buf_size))) {
  947. goto fail;
  948. }
  949. array_init_values->type_idx = type_idx;
  950. if (!pop_const_expr_stack(
  951. &const_expr_ctx, NULL, VALUE_TYPE_I32,
  952. NULL, NULL, &len_val, error_buf,
  953. error_buf_size)) {
  954. wasm_runtime_free(array_init_values);
  955. goto fail;
  956. }
  957. array_init_values->length = len_val.i32;
  958. if (!pop_const_expr_stack(
  959. &const_expr_ctx, NULL, elem_type,
  960. elem_ref_type, NULL,
  961. &array_init_values->elem_data[0],
  962. error_buf, error_buf_size)) {
  963. wasm_runtime_free(array_init_values);
  964. goto fail;
  965. }
  966. cur_value.data = array_init_values;
  967. }
  968. else {
  969. /* WASM_OP_ARRAY_NEW_FIXED */
  970. uint32 len;
  971. read_leb_uint32(p, p_end, len);
  972. total_size =
  973. (uint64)offsetof(WASMArrayNewInitValues,
  974. elem_data)
  975. + (uint64)sizeof(WASMValue) * len;
  976. if (!(array_init_values =
  977. loader_malloc(total_size, error_buf,
  978. error_buf_size))) {
  979. goto fail;
  980. }
  981. array_init_values->type_idx = type_idx;
  982. array_init_values->length = len;
  983. for (i = len; i > 0; i--) {
  984. if (!pop_const_expr_stack(
  985. &const_expr_ctx, NULL, elem_type,
  986. elem_ref_type, NULL,
  987. &array_init_values
  988. ->elem_data[i - 1],
  989. error_buf, error_buf_size)) {
  990. wasm_runtime_free(array_init_values);
  991. goto fail;
  992. }
  993. }
  994. cur_value.data = array_init_values;
  995. }
  996. }
  997. else {
  998. /* WASM_OP_ARRAY_NEW_DEFAULT */
  999. WASMValue len_val;
  1000. uint32 len;
  1001. /* POP(i32) */
  1002. if (!pop_const_expr_stack(&const_expr_ctx, NULL,
  1003. VALUE_TYPE_I32, NULL,
  1004. NULL, &len_val, error_buf,
  1005. error_buf_size)) {
  1006. goto fail;
  1007. }
  1008. len = len_val.i32;
  1009. cur_value.array_new_default.type_index = type_idx;
  1010. cur_value.array_new_default.length = len;
  1011. }
  1012. wasm_set_refheaptype_typeidx(
  1013. &cur_ref_type.ref_ht_typeidx, false, type_idx);
  1014. if (!push_const_expr_stack(
  1015. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1016. &cur_ref_type, (uint8)opcode1, &cur_value,
  1017. error_buf, error_buf_size)) {
  1018. if (array_init_values) {
  1019. wasm_runtime_free(array_init_values);
  1020. }
  1021. goto fail;
  1022. }
  1023. break;
  1024. }
  1025. case WASM_OP_ANY_CONVERT_EXTERN:
  1026. {
  1027. set_error_buf(error_buf, error_buf_size,
  1028. "unsupported constant expression of "
  1029. "extern.internalize");
  1030. goto fail;
  1031. }
  1032. case WASM_OP_EXTERN_CONVERT_ANY:
  1033. {
  1034. set_error_buf(error_buf, error_buf_size,
  1035. "unsupported constant expression of "
  1036. "extern.externalize");
  1037. goto fail;
  1038. }
  1039. case WASM_OP_REF_I31:
  1040. {
  1041. /* POP(i32) */
  1042. if (!pop_const_expr_stack(
  1043. &const_expr_ctx, NULL, VALUE_TYPE_I32, NULL,
  1044. NULL, &cur_value, error_buf, error_buf_size)) {
  1045. goto fail;
  1046. }
  1047. wasm_set_refheaptype_common(&cur_ref_type.ref_ht_common,
  1048. false, HEAP_TYPE_I31);
  1049. if (!push_const_expr_stack(
  1050. &const_expr_ctx, flag, cur_ref_type.ref_type,
  1051. &cur_ref_type, (uint8)opcode1, &cur_value,
  1052. error_buf, error_buf_size)) {
  1053. goto fail;
  1054. }
  1055. break;
  1056. }
  1057. default:
  1058. set_error_buf(
  1059. error_buf, error_buf_size,
  1060. "type mismatch or constant expression required");
  1061. goto fail;
  1062. }
  1063. break;
  1064. }
  1065. #endif /* end of WASM_ENABLE_GC != 0 */
  1066. default:
  1067. {
  1068. set_error_buf(error_buf, error_buf_size,
  1069. "illegal opcode "
  1070. "or constant expression required "
  1071. "or type mismatch");
  1072. goto fail;
  1073. }
  1074. }
  1075. CHECK_BUF(p, p_end, 1);
  1076. flag = read_uint8(p);
  1077. }
  1078. /* There should be only one value left on the init value stack */
  1079. if (!pop_const_expr_stack(&const_expr_ctx, &flag, type,
  1080. #if WASM_ENABLE_GC != 0
  1081. ref_type, &opcode,
  1082. #endif
  1083. &cur_value, error_buf, error_buf_size)) {
  1084. goto fail;
  1085. }
  1086. if (const_expr_ctx.sp != 0) {
  1087. set_error_buf(error_buf, error_buf_size,
  1088. "type mismatch: illegal constant opcode sequence");
  1089. goto fail;
  1090. }
  1091. init_expr->init_expr_type = flag;
  1092. init_expr->u = cur_value;
  1093. #if WASM_ENABLE_GC != 0
  1094. if (init_expr->init_expr_type == WASM_OP_GC_PREFIX) {
  1095. switch (opcode) {
  1096. case WASM_OP_STRUCT_NEW:
  1097. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW;
  1098. break;
  1099. case WASM_OP_STRUCT_NEW_DEFAULT:
  1100. init_expr->init_expr_type = INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT;
  1101. break;
  1102. case WASM_OP_ARRAY_NEW:
  1103. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW;
  1104. break;
  1105. case WASM_OP_ARRAY_NEW_DEFAULT:
  1106. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT;
  1107. break;
  1108. case WASM_OP_ARRAY_NEW_FIXED:
  1109. init_expr->init_expr_type = INIT_EXPR_TYPE_ARRAY_NEW_FIXED;
  1110. break;
  1111. case WASM_OP_REF_I31:
  1112. init_expr->init_expr_type = INIT_EXPR_TYPE_I31_NEW;
  1113. break;
  1114. default:
  1115. bh_assert(0);
  1116. break;
  1117. }
  1118. }
  1119. #endif /* end of WASM_ENABLE_GC != 0 */
  1120. *p_buf = p;
  1121. destroy_const_expr_stack(&const_expr_ctx);
  1122. return true;
  1123. fail:
  1124. destroy_const_expr_stack(&const_expr_ctx);
  1125. return false;
  1126. }
  1127. static bool
  1128. check_mutability(uint8 mutable, char *error_buf, uint32 error_buf_size)
  1129. {
  1130. if (mutable >= 2) {
  1131. set_error_buf(error_buf, error_buf_size, "invalid mutability");
  1132. return false;
  1133. }
  1134. return true;
  1135. }
  1136. #if WASM_ENABLE_GC != 0
  1137. static void
  1138. destroy_func_type(WASMFuncType *type)
  1139. {
  1140. /* Destroy the reference type hash set */
  1141. if (type->ref_type_maps)
  1142. wasm_runtime_free(type->ref_type_maps);
  1143. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1144. && WASM_ENABLE_LAZY_JIT != 0
  1145. if (type->call_to_llvm_jit_from_fast_jit)
  1146. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1147. #endif
  1148. /* Free the type */
  1149. wasm_runtime_free(type);
  1150. }
  1151. static void
  1152. destroy_struct_type(WASMStructType *type)
  1153. {
  1154. if (type->ref_type_maps)
  1155. wasm_runtime_free(type->ref_type_maps);
  1156. wasm_runtime_free(type);
  1157. }
  1158. static void
  1159. destroy_array_type(WASMArrayType *type)
  1160. {
  1161. wasm_runtime_free(type);
  1162. }
  1163. static void
  1164. destroy_wasm_type(WASMType *type)
  1165. {
  1166. if (type->ref_count > 1) {
  1167. /* The type is referenced by other types
  1168. of current wasm module */
  1169. type->ref_count--;
  1170. return;
  1171. }
  1172. if (type->type_flag == WASM_TYPE_FUNC)
  1173. destroy_func_type((WASMFuncType *)type);
  1174. else if (type->type_flag == WASM_TYPE_STRUCT)
  1175. destroy_struct_type((WASMStructType *)type);
  1176. else if (type->type_flag == WASM_TYPE_ARRAY)
  1177. destroy_array_type((WASMArrayType *)type);
  1178. else {
  1179. bh_assert(0);
  1180. }
  1181. }
  1182. /* Resolve (ref null ht) or (ref ht) */
  1183. static bool
  1184. resolve_reftype_htref(const uint8 **p_buf, const uint8 *buf_end,
  1185. WASMModule *module, uint32 type_count, bool nullable,
  1186. WASMRefType *ref_type, char *error_buf,
  1187. uint32 error_buf_size)
  1188. {
  1189. const uint8 *p = *p_buf, *p_end = buf_end;
  1190. ref_type->ref_type =
  1191. nullable ? REF_TYPE_HT_NULLABLE : REF_TYPE_HT_NON_NULLABLE;
  1192. ref_type->ref_ht_common.nullable = nullable;
  1193. read_leb_int32(p, p_end, ref_type->ref_ht_common.heap_type);
  1194. if (wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common)) {
  1195. /* heap type is (type i), i : typeidx, >= 0 */
  1196. if (!check_type_index(module, type_count,
  1197. ref_type->ref_ht_typeidx.type_idx, error_buf,
  1198. error_buf_size)) {
  1199. return false;
  1200. }
  1201. }
  1202. else if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common)) {
  1203. /* heap type is func, extern, any, eq, i31 or data */
  1204. set_error_buf(error_buf, error_buf_size, "unknown heap type");
  1205. return false;
  1206. }
  1207. *p_buf = p;
  1208. return true;
  1209. fail:
  1210. return false;
  1211. }
  1212. static bool
  1213. resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
  1214. WASMModule *module, uint32 type_count,
  1215. bool *p_need_ref_type_map, WASMRefType *ref_type,
  1216. bool allow_packed_type, char *error_buf,
  1217. uint32 error_buf_size)
  1218. {
  1219. const uint8 *p = *p_buf, *p_end = buf_end;
  1220. uint8 type;
  1221. memset(ref_type, 0, sizeof(WASMRefType));
  1222. CHECK_BUF(p, p_end, 1);
  1223. type = read_uint8(p);
  1224. if (wasm_is_reftype_htref_nullable(type)) {
  1225. /* (ref null ht) */
  1226. if (!resolve_reftype_htref(&p, p_end, module, type_count, true,
  1227. ref_type, error_buf, error_buf_size))
  1228. return false;
  1229. if (!wasm_is_refheaptype_common(&ref_type->ref_ht_common))
  1230. *p_need_ref_type_map = true;
  1231. else {
  1232. /* For (ref null func/extern/any/eq/i31/data), they are same as
  1233. funcref/externref/anyref/eqref/i31ref/dataref, we convert the
  1234. multi-byte type to one-byte type to reduce the footprint and
  1235. the complexity of type equal/subtype checking */
  1236. ref_type->ref_type =
  1237. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1238. *p_need_ref_type_map = false;
  1239. }
  1240. }
  1241. else if (wasm_is_reftype_htref_non_nullable(type)) {
  1242. /* (ref ht) */
  1243. if (!resolve_reftype_htref(&p, p_end, module, type_count, false,
  1244. ref_type, error_buf, error_buf_size))
  1245. return false;
  1246. *p_need_ref_type_map = true;
  1247. #if WASM_ENABLE_STRINGREF != 0
  1248. /* covert (ref string) to stringref */
  1249. if (wasm_is_refheaptype_stringrefs(&ref_type->ref_ht_common)) {
  1250. ref_type->ref_type =
  1251. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1252. *p_need_ref_type_map = false;
  1253. }
  1254. #endif
  1255. }
  1256. else {
  1257. /* type which can be represented by one byte */
  1258. if (!is_value_type(type)
  1259. && !(allow_packed_type && is_packed_type(type))) {
  1260. set_error_buf(error_buf, error_buf_size, "type mismatch");
  1261. return false;
  1262. }
  1263. ref_type->ref_type = type;
  1264. *p_need_ref_type_map = false;
  1265. #if WASM_ENABLE_WAMR_COMPILER != 0
  1266. /* If any value's type is v128, mark the module as SIMD used */
  1267. if (type == VALUE_TYPE_V128)
  1268. module->is_simd_used = true;
  1269. #endif
  1270. }
  1271. *p_buf = p;
  1272. return true;
  1273. fail:
  1274. return false;
  1275. }
  1276. static WASMRefType *
  1277. reftype_set_insert(HashMap *ref_type_set, const WASMRefType *ref_type,
  1278. char *error_buf, uint32 error_buf_size)
  1279. {
  1280. WASMRefType *ret = wasm_reftype_set_insert(ref_type_set, ref_type);
  1281. if (!ret) {
  1282. set_error_buf(error_buf, error_buf_size,
  1283. "insert ref type to hash set failed");
  1284. }
  1285. return ret;
  1286. }
  1287. static bool
  1288. resolve_func_type(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  1289. uint32 type_count, uint32 type_idx, char *error_buf,
  1290. uint32 error_buf_size)
  1291. {
  1292. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1293. uint32 param_count, result_count, i, j = 0;
  1294. uint32 param_cell_num, ret_cell_num;
  1295. uint32 ref_type_map_count = 0, result_ref_type_map_count = 0;
  1296. uint64 total_size;
  1297. bool need_ref_type_map;
  1298. WASMRefType ref_type;
  1299. WASMFuncType *type = NULL;
  1300. /* Parse first time to resolve param count, result count and
  1301. ref type map count */
  1302. read_leb_uint32(p, p_end, param_count);
  1303. p_org = p;
  1304. for (i = 0; i < param_count; i++) {
  1305. if (!resolve_value_type(&p, p_end, module, type_count,
  1306. &need_ref_type_map, &ref_type, false, error_buf,
  1307. error_buf_size)) {
  1308. return false;
  1309. }
  1310. if (need_ref_type_map)
  1311. ref_type_map_count++;
  1312. }
  1313. read_leb_uint32(p, p_end, result_count);
  1314. for (i = 0; i < result_count; i++) {
  1315. if (!resolve_value_type(&p, p_end, module, type_count,
  1316. &need_ref_type_map, &ref_type, false, error_buf,
  1317. error_buf_size)) {
  1318. return false;
  1319. }
  1320. if (need_ref_type_map) {
  1321. ref_type_map_count++;
  1322. result_ref_type_map_count++;
  1323. }
  1324. }
  1325. LOG_VERBOSE("type %u: func, param count: %d, result count: %d, "
  1326. "ref type map count: %d",
  1327. type_idx, param_count, result_count, ref_type_map_count);
  1328. /* Parse second time to resolve param types, result types and
  1329. ref type map info */
  1330. p = p_org;
  1331. total_size = offsetof(WASMFuncType, types)
  1332. + sizeof(uint8) * (uint64)(param_count + result_count);
  1333. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1334. return false;
  1335. }
  1336. if (ref_type_map_count > 0) {
  1337. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1338. if (!(type->ref_type_maps =
  1339. loader_malloc(total_size, error_buf, error_buf_size))) {
  1340. goto fail;
  1341. }
  1342. }
  1343. type->base_type.type_flag = WASM_TYPE_FUNC;
  1344. type->param_count = param_count;
  1345. type->result_count = result_count;
  1346. type->ref_type_map_count = ref_type_map_count;
  1347. if (ref_type_map_count > 0) {
  1348. type->result_ref_type_maps = type->ref_type_maps + ref_type_map_count
  1349. - result_ref_type_map_count;
  1350. }
  1351. for (i = 0; i < param_count; i++) {
  1352. if (!resolve_value_type(&p, p_end, module, type_count,
  1353. &need_ref_type_map, &ref_type, false, error_buf,
  1354. error_buf_size)) {
  1355. goto fail;
  1356. }
  1357. type->types[i] = ref_type.ref_type;
  1358. if (need_ref_type_map) {
  1359. type->ref_type_maps[j].index = i;
  1360. if (!(type->ref_type_maps[j++].ref_type =
  1361. reftype_set_insert(module->ref_type_set, &ref_type,
  1362. error_buf, error_buf_size))) {
  1363. goto fail;
  1364. }
  1365. }
  1366. }
  1367. read_leb_uint32(p, p_end, result_count);
  1368. for (i = 0; i < result_count; i++) {
  1369. if (!resolve_value_type(&p, p_end, module, type_count,
  1370. &need_ref_type_map, &ref_type, false, error_buf,
  1371. error_buf_size)) {
  1372. goto fail;
  1373. }
  1374. type->types[param_count + i] = ref_type.ref_type;
  1375. if (need_ref_type_map) {
  1376. type->ref_type_maps[j].index = param_count + i;
  1377. if (!(type->ref_type_maps[j++].ref_type =
  1378. reftype_set_insert(module->ref_type_set, &ref_type,
  1379. error_buf, error_buf_size))) {
  1380. goto fail;
  1381. }
  1382. }
  1383. }
  1384. bh_assert(j == type->ref_type_map_count);
  1385. #if TRACE_WASM_LOADER != 0
  1386. os_printf("type %d = ", type_idx);
  1387. wasm_dump_func_type(type);
  1388. #endif
  1389. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1390. ret_cell_num = wasm_get_cell_num(type->types + param_count, result_count);
  1391. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1392. set_error_buf(error_buf, error_buf_size,
  1393. "param count or result count too large");
  1394. goto fail;
  1395. }
  1396. type->param_cell_num = (uint16)param_cell_num;
  1397. type->ret_cell_num = (uint16)ret_cell_num;
  1398. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1399. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1400. #endif
  1401. #if WASM_ENABLE_WAMR_COMPILER != 0
  1402. for (i = 0; i < (uint32)(type->param_count + type->result_count); i++) {
  1403. if (type->types[i] == VALUE_TYPE_V128)
  1404. module->is_simd_used = true;
  1405. }
  1406. #endif
  1407. *p_buf = p;
  1408. module->types[type_idx] = (WASMType *)type;
  1409. return true;
  1410. fail:
  1411. if (type)
  1412. destroy_func_type(type);
  1413. return false;
  1414. }
  1415. static bool
  1416. resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
  1417. WASMModule *module, uint32 type_count, uint32 type_idx,
  1418. char *error_buf, uint32 error_buf_size)
  1419. {
  1420. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  1421. uint32 field_count, ref_type_map_count = 0, ref_field_count = 0;
  1422. uint32 i, j = 0, offset;
  1423. uint16 *reference_table;
  1424. uint64 total_size;
  1425. uint8 mutable;
  1426. bool need_ref_type_map;
  1427. WASMRefType ref_type;
  1428. WASMStructType *type = NULL;
  1429. /* Parse first time to resolve field count and ref type map count */
  1430. read_leb_uint32(p, p_end, field_count);
  1431. p_org = p;
  1432. for (i = 0; i < field_count; i++) {
  1433. if (!resolve_value_type(&p, p_end, module, type_count,
  1434. &need_ref_type_map, &ref_type, true, error_buf,
  1435. error_buf_size)) {
  1436. return false;
  1437. }
  1438. if (need_ref_type_map)
  1439. ref_type_map_count++;
  1440. if (wasm_is_type_reftype(ref_type.ref_type))
  1441. ref_field_count++;
  1442. CHECK_BUF(p, p_end, 1);
  1443. mutable = read_uint8(p);
  1444. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1445. return false;
  1446. }
  1447. }
  1448. LOG_VERBOSE("type %u: struct, field count: %d, ref type map count: %d",
  1449. type_idx, field_count, ref_type_map_count);
  1450. /* Parse second time to resolve field types and ref type map info */
  1451. p = p_org;
  1452. total_size = offsetof(WASMStructType, fields)
  1453. + sizeof(WASMStructFieldType) * (uint64)field_count
  1454. + sizeof(uint16) * (uint64)(ref_field_count + 1);
  1455. if (!(type = loader_malloc(total_size, error_buf, error_buf_size))) {
  1456. return false;
  1457. }
  1458. if (ref_type_map_count > 0) {
  1459. total_size = sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  1460. if (!(type->ref_type_maps =
  1461. loader_malloc(total_size, error_buf, error_buf_size))) {
  1462. goto fail;
  1463. }
  1464. }
  1465. type->reference_table = reference_table =
  1466. (uint16 *)((uint8 *)type + offsetof(WASMStructType, fields)
  1467. + sizeof(WASMStructFieldType) * field_count);
  1468. *reference_table++ = ref_field_count;
  1469. type->base_type.type_flag = WASM_TYPE_STRUCT;
  1470. type->field_count = field_count;
  1471. type->ref_type_map_count = ref_type_map_count;
  1472. offset = (uint32)offsetof(WASMStructObject, field_data);
  1473. for (i = 0; i < field_count; i++) {
  1474. if (!resolve_value_type(&p, p_end, module, type_count,
  1475. &need_ref_type_map, &ref_type, true, error_buf,
  1476. error_buf_size)) {
  1477. goto fail;
  1478. }
  1479. type->fields[i].field_type = ref_type.ref_type;
  1480. if (need_ref_type_map) {
  1481. type->ref_type_maps[j].index = i;
  1482. if (!(type->ref_type_maps[j++].ref_type =
  1483. reftype_set_insert(module->ref_type_set, &ref_type,
  1484. error_buf, error_buf_size))) {
  1485. goto fail;
  1486. }
  1487. }
  1488. CHECK_BUF(p, p_end, 1);
  1489. type->fields[i].field_flags = read_uint8(p);
  1490. type->fields[i].field_size =
  1491. (uint8)wasm_reftype_size(ref_type.ref_type);
  1492. #if !(defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \
  1493. || defined(BUILD_TARGET_X86_32))
  1494. if (type->fields[i].field_size == 2)
  1495. offset = align_uint(offset, 2);
  1496. else if (type->fields[i].field_size >= 4) /* field size is 4 or 8 */
  1497. offset = align_uint(offset, 4);
  1498. #endif
  1499. type->fields[i].field_offset = offset;
  1500. if (wasm_is_type_reftype(ref_type.ref_type))
  1501. *reference_table++ = offset;
  1502. offset += type->fields[i].field_size;
  1503. LOG_VERBOSE(" field: %d, flags: %d, type: %d", i,
  1504. type->fields[i].field_flags, type->fields[i].field_type);
  1505. }
  1506. type->total_size = offset;
  1507. bh_assert(j == type->ref_type_map_count);
  1508. #if TRACE_WASM_LOADER != 0
  1509. os_printf("type %d = ", type_idx);
  1510. wasm_dump_struct_type(type);
  1511. #endif
  1512. *p_buf = p;
  1513. module->types[type_idx] = (WASMType *)type;
  1514. return true;
  1515. fail:
  1516. if (type)
  1517. destroy_struct_type(type);
  1518. return false;
  1519. }
  1520. static bool
  1521. resolve_array_type(const uint8 **p_buf, const uint8 *buf_end,
  1522. WASMModule *module, uint32 type_count, uint32 type_idx,
  1523. char *error_buf, uint32 error_buf_size)
  1524. {
  1525. const uint8 *p = *p_buf, *p_end = buf_end;
  1526. uint8 mutable;
  1527. bool need_ref_type_map;
  1528. WASMRefType ref_type;
  1529. WASMArrayType *type = NULL;
  1530. if (!resolve_value_type(&p, p_end, module, type_count, &need_ref_type_map,
  1531. &ref_type, true, error_buf, error_buf_size)) {
  1532. return false;
  1533. }
  1534. CHECK_BUF(p, p_end, 1);
  1535. mutable = read_uint8(p);
  1536. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  1537. return false;
  1538. }
  1539. LOG_VERBOSE("type %u: array", type_idx);
  1540. if (!(type = loader_malloc(sizeof(WASMArrayType), error_buf,
  1541. error_buf_size))) {
  1542. return false;
  1543. }
  1544. type->base_type.type_flag = WASM_TYPE_ARRAY;
  1545. type->elem_flags = mutable;
  1546. type->elem_type = ref_type.ref_type;
  1547. if (need_ref_type_map) {
  1548. if (!(type->elem_ref_type =
  1549. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  1550. error_buf_size))) {
  1551. goto fail;
  1552. }
  1553. }
  1554. #if TRACE_WASM_LOADER != 0
  1555. os_printf("type %d = ", type_idx);
  1556. wasm_dump_array_type(type);
  1557. #endif
  1558. *p_buf = p;
  1559. module->types[type_idx] = (WASMType *)type;
  1560. return true;
  1561. fail:
  1562. if (type)
  1563. destroy_array_type(type);
  1564. return false;
  1565. }
  1566. static bool
  1567. init_ref_type(WASMModule *module, WASMRefType *ref_type, bool nullable,
  1568. int32 heap_type, char *error_buf, uint32 error_buf_size)
  1569. {
  1570. if (heap_type >= 0) {
  1571. if (!check_type_index(module, module->type_count, heap_type, error_buf,
  1572. error_buf_size)) {
  1573. return false;
  1574. }
  1575. wasm_set_refheaptype_typeidx(&ref_type->ref_ht_typeidx, nullable,
  1576. heap_type);
  1577. }
  1578. else {
  1579. if (!wasm_is_valid_heap_type(heap_type)) {
  1580. set_error_buf(error_buf, error_buf_size, "unknown type");
  1581. return false;
  1582. }
  1583. wasm_set_refheaptype_common(&ref_type->ref_ht_common, nullable,
  1584. heap_type);
  1585. if (nullable) {
  1586. /* For (ref null func/extern/any/eq/i31/data),
  1587. they are same as
  1588. funcref/externref/anyref/eqref/i31ref/dataref,
  1589. we convert the multi-byte type to one-byte
  1590. type to reduce the footprint and the
  1591. complexity of type equal/subtype checking */
  1592. ref_type->ref_type =
  1593. (uint8)((int32)0x80 + ref_type->ref_ht_common.heap_type);
  1594. }
  1595. }
  1596. return true;
  1597. }
  1598. static void
  1599. calculate_reftype_diff(WASMRefType *ref_type_diff, WASMRefType *ref_type1,
  1600. WASMRefType *ref_type2)
  1601. {
  1602. /**
  1603. * The difference rt1 ∖ rt2 between two reference types is defined as
  1604. * follows:
  1605. * (ref null?1 ht1) ∖ (ref null ht2) = (ref ht1) (ref null?1 ht1) ∖
  1606. * (ref ht2) = (ref null?1 ht1)
  1607. */
  1608. if (wasm_is_type_multi_byte_type(ref_type1->ref_type)) {
  1609. bh_memcpy_s(ref_type_diff, wasm_reftype_struct_size(ref_type1),
  1610. ref_type1, wasm_reftype_struct_size(ref_type1));
  1611. }
  1612. else {
  1613. ref_type_diff->ref_type = ref_type1->ref_type;
  1614. }
  1615. if (ref_type2->ref_ht_common.nullable) {
  1616. if (wasm_is_type_reftype(ref_type_diff->ref_type)
  1617. && !(wasm_is_type_multi_byte_type(ref_type_diff->ref_type))) {
  1618. wasm_set_refheaptype_typeidx(&ref_type_diff->ref_ht_typeidx, false,
  1619. (int32)ref_type_diff->ref_type - 0x80);
  1620. }
  1621. else {
  1622. ref_type_diff->ref_ht_typeidx.nullable = false;
  1623. }
  1624. }
  1625. }
  1626. #else /* else of WASM_ENABLE_GC != 0 */
  1627. static void
  1628. destroy_wasm_type(WASMType *type)
  1629. {
  1630. if (type->ref_count > 1) {
  1631. /* The type is referenced by other types
  1632. of current wasm module */
  1633. type->ref_count--;
  1634. return;
  1635. }
  1636. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  1637. && WASM_ENABLE_LAZY_JIT != 0
  1638. if (type->call_to_llvm_jit_from_fast_jit)
  1639. jit_code_cache_free(type->call_to_llvm_jit_from_fast_jit);
  1640. #endif
  1641. wasm_runtime_free(type);
  1642. }
  1643. #endif /* end of WASM_ENABLE_GC != 0 */
  1644. static bool
  1645. load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  1646. char *error_buf, uint32 error_buf_size)
  1647. {
  1648. const uint8 *p = buf, *p_end = buf_end;
  1649. uint32 type_count, i;
  1650. uint64 total_size;
  1651. uint8 flag;
  1652. #if WASM_ENABLE_GC != 0
  1653. uint32 processed_type_count = 0;
  1654. #endif
  1655. read_leb_uint32(p, p_end, type_count);
  1656. if (type_count) {
  1657. module->type_count = type_count;
  1658. total_size = sizeof(WASMType *) * (uint64)type_count;
  1659. if (!(module->types =
  1660. loader_malloc(total_size, error_buf, error_buf_size))) {
  1661. return false;
  1662. }
  1663. #if WASM_ENABLE_GC == 0
  1664. for (i = 0; i < type_count; i++) {
  1665. WASMFuncType *type;
  1666. const uint8 *p_org;
  1667. uint32 param_count, result_count, j;
  1668. uint32 param_cell_num, ret_cell_num;
  1669. CHECK_BUF(p, p_end, 1);
  1670. flag = read_uint8(p);
  1671. if (flag != 0x60) {
  1672. set_error_buf(error_buf, error_buf_size, "invalid type flag");
  1673. return false;
  1674. }
  1675. read_leb_uint32(p, p_end, param_count);
  1676. /* Resolve param count and result count firstly */
  1677. p_org = p;
  1678. CHECK_BUF(p, p_end, param_count);
  1679. p += param_count;
  1680. read_leb_uint32(p, p_end, result_count);
  1681. CHECK_BUF(p, p_end, result_count);
  1682. p = p_org;
  1683. if (param_count > UINT16_MAX || result_count > UINT16_MAX) {
  1684. set_error_buf(error_buf, error_buf_size,
  1685. "param count or result count too large");
  1686. return false;
  1687. }
  1688. total_size = offsetof(WASMFuncType, types)
  1689. + sizeof(uint8) * (uint64)(param_count + result_count);
  1690. if (!(type = module->types[i] =
  1691. loader_malloc(total_size, error_buf, error_buf_size))) {
  1692. return false;
  1693. }
  1694. /* Resolve param types and result types */
  1695. type->ref_count = 1;
  1696. type->param_count = (uint16)param_count;
  1697. type->result_count = (uint16)result_count;
  1698. for (j = 0; j < param_count; j++) {
  1699. CHECK_BUF(p, p_end, 1);
  1700. type->types[j] = read_uint8(p);
  1701. }
  1702. read_leb_uint32(p, p_end, result_count);
  1703. for (j = 0; j < result_count; j++) {
  1704. CHECK_BUF(p, p_end, 1);
  1705. type->types[param_count + j] = read_uint8(p);
  1706. }
  1707. for (j = 0; j < param_count + result_count; j++) {
  1708. if (!is_value_type(type->types[j])) {
  1709. set_error_buf(error_buf, error_buf_size,
  1710. "unknown value type");
  1711. return false;
  1712. }
  1713. }
  1714. param_cell_num = wasm_get_cell_num(type->types, param_count);
  1715. ret_cell_num =
  1716. wasm_get_cell_num(type->types + param_count, result_count);
  1717. if (param_cell_num > UINT16_MAX || ret_cell_num > UINT16_MAX) {
  1718. set_error_buf(error_buf, error_buf_size,
  1719. "param count or result count too large");
  1720. return false;
  1721. }
  1722. type->param_cell_num = (uint16)param_cell_num;
  1723. type->ret_cell_num = (uint16)ret_cell_num;
  1724. #if WASM_ENABLE_QUICK_AOT_ENTRY != 0
  1725. type->quick_aot_entry = wasm_native_lookup_quick_aot_entry(type);
  1726. #endif
  1727. #if WASM_ENABLE_WAMR_COMPILER != 0
  1728. for (j = 0; j < type->param_count + type->result_count; j++) {
  1729. if (type->types[j] == VALUE_TYPE_V128)
  1730. module->is_simd_used = true;
  1731. else if (type->types[j] == VALUE_TYPE_FUNCREF
  1732. || type->types[j] == VALUE_TYPE_EXTERNREF)
  1733. module->is_ref_types_used = true;
  1734. }
  1735. #endif
  1736. /* If there is already a same type created, use it instead */
  1737. for (j = 0; j < i; j++) {
  1738. if (wasm_type_equal(type, module->types[j], module->types, i)) {
  1739. if (module->types[j]->ref_count == UINT16_MAX) {
  1740. set_error_buf(error_buf, error_buf_size,
  1741. "wasm type's ref count too large");
  1742. return false;
  1743. }
  1744. destroy_wasm_type(type);
  1745. module->types[i] = module->types[j];
  1746. module->types[j]->ref_count++;
  1747. break;
  1748. }
  1749. }
  1750. }
  1751. #else /* else of WASM_ENABLE_GC == 0 */
  1752. for (i = 0; i < type_count; i++) {
  1753. uint32 super_type_count = 0, parent_type_idx = (uint32)-1;
  1754. uint32 rec_count = 1, j;
  1755. bool is_sub_final = true;
  1756. CHECK_BUF(p, p_end, 1);
  1757. flag = read_uint8(p);
  1758. if (flag == DEFINED_TYPE_REC) {
  1759. read_leb_uint32(p, p_end, rec_count);
  1760. if (rec_count > 1) {
  1761. uint64 new_total_size;
  1762. /* integer overflow */
  1763. if (rec_count - 1 > UINT32_MAX - module->type_count) {
  1764. set_error_buf(error_buf, error_buf_size,
  1765. "recursive type count too large");
  1766. return false;
  1767. }
  1768. module->type_count += rec_count - 1;
  1769. new_total_size =
  1770. sizeof(WASMFuncType *) * (uint64)module->type_count;
  1771. if (new_total_size > UINT32_MAX) {
  1772. set_error_buf(error_buf, error_buf_size,
  1773. "allocate memory failed");
  1774. return false;
  1775. }
  1776. MEM_REALLOC(module->types, (uint32)total_size,
  1777. (uint32)new_total_size);
  1778. total_size = new_total_size;
  1779. }
  1780. LOG_VERBOSE("Processing rec group [%d-%d]",
  1781. processed_type_count,
  1782. processed_type_count + rec_count - 1);
  1783. }
  1784. else {
  1785. p--;
  1786. }
  1787. for (j = 0; j < rec_count; j++) {
  1788. WASMType *cur_type = NULL;
  1789. CHECK_BUF(p, p_end, 1);
  1790. flag = read_uint8(p);
  1791. parent_type_idx = -1;
  1792. if (flag == DEFINED_TYPE_SUB
  1793. || flag == DEFINED_TYPE_SUB_FINAL) {
  1794. read_leb_uint32(p, p_end, super_type_count);
  1795. if (super_type_count > 1) {
  1796. set_error_buf(error_buf, error_buf_size,
  1797. "super type count too large");
  1798. return false;
  1799. }
  1800. if (super_type_count > 0) {
  1801. read_leb_uint32(p, p_end, parent_type_idx);
  1802. if (parent_type_idx >= processed_type_count + j) {
  1803. set_error_buf_v(error_buf, error_buf_size,
  1804. "unknown type %d", parent_type_idx);
  1805. return false;
  1806. }
  1807. if (module->types[parent_type_idx]->is_sub_final) {
  1808. set_error_buf(error_buf, error_buf_size,
  1809. "sub type can not inherit from "
  1810. "a final super type");
  1811. return false;
  1812. }
  1813. }
  1814. if (flag == DEFINED_TYPE_SUB)
  1815. is_sub_final = false;
  1816. CHECK_BUF(p, p_end, 1);
  1817. flag = read_uint8(p);
  1818. }
  1819. if (flag == DEFINED_TYPE_FUNC) {
  1820. if (!resolve_func_type(&p, buf_end, module,
  1821. processed_type_count + rec_count,
  1822. processed_type_count + j, error_buf,
  1823. error_buf_size)) {
  1824. return false;
  1825. }
  1826. }
  1827. else if (flag == DEFINED_TYPE_STRUCT) {
  1828. if (!resolve_struct_type(&p, buf_end, module,
  1829. processed_type_count + rec_count,
  1830. processed_type_count + j,
  1831. error_buf, error_buf_size)) {
  1832. return false;
  1833. }
  1834. }
  1835. else if (flag == DEFINED_TYPE_ARRAY) {
  1836. if (!resolve_array_type(&p, buf_end, module,
  1837. processed_type_count + rec_count,
  1838. processed_type_count + j, error_buf,
  1839. error_buf_size)) {
  1840. return false;
  1841. }
  1842. }
  1843. else {
  1844. set_error_buf(error_buf, error_buf_size,
  1845. "invalid type flag");
  1846. return false;
  1847. }
  1848. cur_type = module->types[processed_type_count + j];
  1849. cur_type->ref_count = 1;
  1850. cur_type->parent_type_idx = parent_type_idx;
  1851. cur_type->is_sub_final = is_sub_final;
  1852. cur_type->rec_count = rec_count;
  1853. cur_type->rec_idx = j;
  1854. cur_type->rec_begin_type_idx = processed_type_count;
  1855. }
  1856. /* resolve subtyping relationship in current rec group */
  1857. for (j = 0; j < rec_count; j++) {
  1858. WASMType *cur_type = module->types[processed_type_count + j];
  1859. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1860. WASMType *parent_type =
  1861. module->types[cur_type->parent_type_idx];
  1862. cur_type->parent_type = parent_type;
  1863. cur_type->root_type = parent_type->root_type;
  1864. if (parent_type->inherit_depth == UINT16_MAX) {
  1865. set_error_buf(error_buf, error_buf_size,
  1866. "parent type's inherit depth too large");
  1867. return false;
  1868. }
  1869. cur_type->inherit_depth = parent_type->inherit_depth + 1;
  1870. }
  1871. else {
  1872. cur_type->parent_type = NULL;
  1873. cur_type->root_type = cur_type;
  1874. cur_type->inherit_depth = 0;
  1875. }
  1876. }
  1877. for (j = 0; j < rec_count; j++) {
  1878. WASMType *cur_type = module->types[processed_type_count + j];
  1879. if (cur_type->parent_type_idx != (uint32)-1) { /* has parent */
  1880. WASMType *parent_type =
  1881. module->types[cur_type->parent_type_idx];
  1882. if (!wasm_type_is_subtype_of(cur_type, parent_type,
  1883. module->types,
  1884. module->type_count)) {
  1885. set_error_buf(error_buf, error_buf_size,
  1886. "sub type does not match super type");
  1887. return false;
  1888. }
  1889. }
  1890. }
  1891. /* If there is already an equivalence type or a group of equivalence
  1892. recursive types created, use it or them instead */
  1893. for (j = 0; j < processed_type_count;) {
  1894. WASMType *src_type = module->types[j];
  1895. WASMType *cur_type = module->types[processed_type_count];
  1896. uint32 k, src_rec_count;
  1897. src_rec_count = src_type->rec_count;
  1898. if (src_rec_count != rec_count) {
  1899. /* no type equivalence */
  1900. j += src_rec_count;
  1901. continue;
  1902. }
  1903. for (k = 0; k < rec_count; k++) {
  1904. src_type = module->types[j + k];
  1905. cur_type = module->types[processed_type_count + k];
  1906. if (!wasm_type_equal(src_type, cur_type, module->types,
  1907. module->type_count)) {
  1908. break;
  1909. }
  1910. }
  1911. if (k < rec_count) {
  1912. /* no type equivalence */
  1913. j += src_rec_count;
  1914. continue;
  1915. }
  1916. /* type equivalence */
  1917. for (k = 0; k < rec_count; k++) {
  1918. if (module->types[j + k]->ref_count == UINT16_MAX) {
  1919. set_error_buf(error_buf, error_buf_size,
  1920. "wasm type's ref count too large");
  1921. return false;
  1922. }
  1923. destroy_wasm_type(module->types[processed_type_count + k]);
  1924. module->types[processed_type_count + k] =
  1925. module->types[j + k];
  1926. module->types[j + k]->ref_count++;
  1927. }
  1928. break;
  1929. }
  1930. if (rec_count > 1) {
  1931. LOG_VERBOSE("Finished processing rec group [%d-%d]",
  1932. processed_type_count,
  1933. processed_type_count + rec_count - 1);
  1934. }
  1935. processed_type_count += rec_count;
  1936. }
  1937. if (!(module->rtt_types = loader_malloc((uint64)sizeof(WASMRttType *)
  1938. * module->type_count,
  1939. error_buf, error_buf_size))) {
  1940. return false;
  1941. }
  1942. #endif /* end of WASM_ENABLE_GC == 0 */
  1943. }
  1944. if (p != p_end) {
  1945. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  1946. return false;
  1947. }
  1948. LOG_VERBOSE("Load type section success.\n");
  1949. return true;
  1950. fail:
  1951. return false;
  1952. }
  1953. static void
  1954. adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
  1955. {
  1956. uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
  1957. ? init_size * 2
  1958. : WASM_TABLE_MAX_SIZE;
  1959. if (max_size_flag) {
  1960. /* module defines the table limitation */
  1961. bh_assert(init_size <= *max_size);
  1962. if (init_size < *max_size) {
  1963. *max_size =
  1964. *max_size < default_max_size ? *max_size : default_max_size;
  1965. }
  1966. }
  1967. else {
  1968. /* partial defined table limitation, gives a default value */
  1969. *max_size = default_max_size;
  1970. }
  1971. }
  1972. #if WASM_ENABLE_LIBC_WASI != 0 || WASM_ENABLE_MULTI_MODULE != 0
  1973. /**
  1974. * Find export item of a module with export info:
  1975. * module name, field name and export kind
  1976. */
  1977. static WASMExport *
  1978. wasm_loader_find_export(const WASMModule *module, const char *module_name,
  1979. const char *field_name, uint8 export_kind,
  1980. char *error_buf, uint32 error_buf_size)
  1981. {
  1982. WASMExport *export =
  1983. loader_find_export((WASMModuleCommon *)module, module_name, field_name,
  1984. export_kind, error_buf, error_buf_size);
  1985. return export;
  1986. }
  1987. #endif
  1988. #if WASM_ENABLE_MULTI_MODULE != 0
  1989. static WASMFunction *
  1990. wasm_loader_resolve_function(const char *module_name, const char *function_name,
  1991. const WASMFuncType *expected_function_type,
  1992. char *error_buf, uint32 error_buf_size)
  1993. {
  1994. WASMModuleCommon *module_reg;
  1995. WASMFunction *function = NULL;
  1996. WASMExport *export = NULL;
  1997. WASMModule *module = NULL;
  1998. WASMFuncType *target_function_type = NULL;
  1999. module_reg = wasm_runtime_find_module_registered(module_name);
  2000. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2001. LOG_DEBUG("can not find a module named %s for function %s", module_name,
  2002. function_name);
  2003. set_error_buf(error_buf, error_buf_size, "unknown import");
  2004. return NULL;
  2005. }
  2006. module = (WASMModule *)module_reg;
  2007. export =
  2008. wasm_loader_find_export(module, module_name, function_name,
  2009. EXPORT_KIND_FUNC, error_buf, error_buf_size);
  2010. if (!export) {
  2011. return NULL;
  2012. }
  2013. /* resolve function type and function */
  2014. if (export->index < module->import_function_count) {
  2015. target_function_type =
  2016. module->import_functions[export->index].u.function.func_type;
  2017. function = module->import_functions[export->index]
  2018. .u.function.import_func_linked;
  2019. }
  2020. else {
  2021. target_function_type =
  2022. module->functions[export->index - module->import_function_count]
  2023. ->func_type;
  2024. function =
  2025. module->functions[export->index - module->import_function_count];
  2026. }
  2027. /* check function type */
  2028. if (!wasm_type_equal((WASMType *)expected_function_type,
  2029. (WASMType *)target_function_type, module->types,
  2030. module->type_count)) {
  2031. LOG_DEBUG("%s.%s failed the type check", module_name, function_name);
  2032. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2033. return NULL;
  2034. }
  2035. return function;
  2036. }
  2037. static WASMTable *
  2038. wasm_loader_resolve_table(const char *module_name, const char *table_name,
  2039. uint32 init_size, uint32 max_size, char *error_buf,
  2040. uint32 error_buf_size)
  2041. {
  2042. WASMModuleCommon *module_reg;
  2043. WASMTable *table = NULL;
  2044. WASMExport *export = NULL;
  2045. WASMModule *module = NULL;
  2046. module_reg = wasm_runtime_find_module_registered(module_name);
  2047. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2048. LOG_DEBUG("can not find a module named %s for table", module_name);
  2049. set_error_buf(error_buf, error_buf_size, "unknown import");
  2050. return NULL;
  2051. }
  2052. module = (WASMModule *)module_reg;
  2053. export =
  2054. wasm_loader_find_export(module, module_name, table_name,
  2055. EXPORT_KIND_TABLE, error_buf, error_buf_size);
  2056. if (!export) {
  2057. return NULL;
  2058. }
  2059. /* resolve table and check the init/max size */
  2060. if (export->index < module->import_table_count) {
  2061. table =
  2062. module->import_tables[export->index].u.table.import_table_linked;
  2063. }
  2064. else {
  2065. table = &(module->tables[export->index - module->import_table_count]);
  2066. }
  2067. if (table->init_size < init_size || table->max_size > max_size) {
  2068. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2069. module_name, table_name, table->init_size, table->max_size,
  2070. init_size, max_size);
  2071. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2072. return NULL;
  2073. }
  2074. return table;
  2075. }
  2076. static WASMMemory *
  2077. wasm_loader_resolve_memory(const char *module_name, const char *memory_name,
  2078. uint32 init_page_count, uint32 max_page_count,
  2079. char *error_buf, uint32 error_buf_size)
  2080. {
  2081. WASMModuleCommon *module_reg;
  2082. WASMMemory *memory = NULL;
  2083. WASMExport *export = NULL;
  2084. WASMModule *module = NULL;
  2085. module_reg = wasm_runtime_find_module_registered(module_name);
  2086. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2087. LOG_DEBUG("can not find a module named %s for memory", module_name);
  2088. set_error_buf(error_buf, error_buf_size, "unknown import");
  2089. return NULL;
  2090. }
  2091. module = (WASMModule *)module_reg;
  2092. export =
  2093. wasm_loader_find_export(module, module_name, memory_name,
  2094. EXPORT_KIND_MEMORY, error_buf, error_buf_size);
  2095. if (!export) {
  2096. return NULL;
  2097. }
  2098. /* resolve memory and check the init/max page count */
  2099. if (export->index < module->import_memory_count) {
  2100. memory = module->import_memories[export->index]
  2101. .u.memory.import_memory_linked;
  2102. }
  2103. else {
  2104. memory =
  2105. &(module->memories[export->index - module->import_memory_count]);
  2106. }
  2107. if (memory->init_page_count < init_page_count
  2108. || memory->max_page_count > max_page_count) {
  2109. LOG_DEBUG("%s,%s failed type check(%d-%d), expected(%d-%d)",
  2110. module_name, memory_name, memory->init_page_count,
  2111. memory->max_page_count, init_page_count, max_page_count);
  2112. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2113. return NULL;
  2114. }
  2115. return memory;
  2116. }
  2117. static WASMGlobal *
  2118. wasm_loader_resolve_global(const char *module_name, const char *global_name,
  2119. uint8 type, bool is_mutable, char *error_buf,
  2120. uint32 error_buf_size)
  2121. {
  2122. WASMModuleCommon *module_reg;
  2123. WASMGlobal *global = NULL;
  2124. WASMExport *export = NULL;
  2125. WASMModule *module = NULL;
  2126. module_reg = wasm_runtime_find_module_registered(module_name);
  2127. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2128. LOG_DEBUG("can not find a module named %s for global", module_name);
  2129. set_error_buf(error_buf, error_buf_size, "unknown import");
  2130. return NULL;
  2131. }
  2132. module = (WASMModule *)module_reg;
  2133. export =
  2134. wasm_loader_find_export(module, module_name, global_name,
  2135. EXPORT_KIND_GLOBAL, error_buf, error_buf_size);
  2136. if (!export) {
  2137. return NULL;
  2138. }
  2139. /* resolve and check the global */
  2140. if (export->index < module->import_global_count) {
  2141. global =
  2142. module->import_globals[export->index].u.global.import_global_linked;
  2143. }
  2144. else {
  2145. global =
  2146. &(module->globals[export->index - module->import_global_count]);
  2147. }
  2148. if (global->type != type || global->is_mutable != is_mutable) {
  2149. LOG_DEBUG("%s,%s failed type check(%d, %d), expected(%d, %d)",
  2150. module_name, global_name, global->type, global->is_mutable,
  2151. type, is_mutable);
  2152. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2153. return NULL;
  2154. }
  2155. return global;
  2156. }
  2157. #if WASM_ENABLE_TAGS != 0
  2158. static WASMTag *
  2159. wasm_loader_resolve_tag(const char *module_name, const char *tag_name,
  2160. const WASMType *expected_tag_type,
  2161. uint32 *linked_tag_index, char *error_buf,
  2162. uint32 error_buf_size)
  2163. {
  2164. WASMModuleCommon *module_reg;
  2165. WASMTag *tag = NULL;
  2166. WASMExport *export = NULL;
  2167. WASMModule *module = NULL;
  2168. module_reg = wasm_runtime_find_module_registered(module_name);
  2169. if (!module_reg || module_reg->module_type != Wasm_Module_Bytecode) {
  2170. LOG_DEBUG("can not find a module named %s for tag %s", module_name,
  2171. tag_name);
  2172. set_error_buf(error_buf, error_buf_size, "unknown import");
  2173. return NULL;
  2174. }
  2175. module = (WASMModule *)module_reg;
  2176. export =
  2177. wasm_loader_find_export(module, module_name, tag_name, EXPORT_KIND_TAG,
  2178. error_buf, error_buf_size);
  2179. if (!export) {
  2180. return NULL;
  2181. }
  2182. /* resolve tag type and tag */
  2183. if (export->index < module->import_tag_count) {
  2184. /* importing an imported tag from the submodule */
  2185. tag = module->import_tags[export->index].u.tag.import_tag_linked;
  2186. }
  2187. else {
  2188. /* importing an section tag from the submodule */
  2189. tag = module->tags[export->index - module->import_tag_count];
  2190. }
  2191. /* check function type */
  2192. if (!wasm_type_equal(expected_tag_type, tag->tag_type)) {
  2193. LOG_DEBUG("%s.%s failed the type check", module_name, tag_name);
  2194. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2195. return NULL;
  2196. }
  2197. if (linked_tag_index != NULL) {
  2198. *linked_tag_index = export->index;
  2199. }
  2200. return tag;
  2201. }
  2202. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2203. #endif /* end of WASM_ENABLE_MULTI_MODULE */
  2204. static bool
  2205. load_function_import(const uint8 **p_buf, const uint8 *buf_end,
  2206. const WASMModule *parent_module,
  2207. const char *sub_module_name, const char *function_name,
  2208. WASMFunctionImport *function, char *error_buf,
  2209. uint32 error_buf_size)
  2210. {
  2211. const uint8 *p = *p_buf, *p_end = buf_end;
  2212. uint32 declare_type_index = 0;
  2213. WASMFuncType *declare_func_type = NULL;
  2214. WASMFunction *linked_func = NULL;
  2215. #if WASM_ENABLE_MULTI_MODULE != 0
  2216. WASMModule *sub_module = NULL;
  2217. #endif
  2218. const char *linked_signature = NULL;
  2219. void *linked_attachment = NULL;
  2220. bool linked_call_conv_raw = false;
  2221. bool is_native_symbol = false;
  2222. read_leb_uint32(p, p_end, declare_type_index);
  2223. *p_buf = p;
  2224. if (declare_type_index >= parent_module->type_count) {
  2225. set_error_buf(error_buf, error_buf_size, "unknown type");
  2226. return false;
  2227. }
  2228. #if WASM_ENABLE_GC != 0
  2229. function->type_idx = declare_type_index;
  2230. #endif
  2231. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  2232. declare_type_index = wasm_get_smallest_type_idx(
  2233. parent_module->types, parent_module->type_count, declare_type_index);
  2234. #endif
  2235. declare_func_type =
  2236. (WASMFuncType *)parent_module->types[declare_type_index];
  2237. /* lookup registered native symbols first */
  2238. linked_func = wasm_native_resolve_symbol(
  2239. sub_module_name, function_name, declare_func_type, &linked_signature,
  2240. &linked_attachment, &linked_call_conv_raw);
  2241. if (linked_func) {
  2242. is_native_symbol = true;
  2243. }
  2244. #if WASM_ENABLE_MULTI_MODULE != 0
  2245. else {
  2246. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2247. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2248. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2249. error_buf_size);
  2250. if (!sub_module) {
  2251. return false;
  2252. }
  2253. }
  2254. linked_func = wasm_loader_resolve_function(
  2255. sub_module_name, function_name, declare_func_type, error_buf,
  2256. error_buf_size);
  2257. }
  2258. #endif
  2259. function->module_name = (char *)sub_module_name;
  2260. function->field_name = (char *)function_name;
  2261. function->func_type = declare_func_type;
  2262. /* func_ptr_linked is for native registered symbol */
  2263. function->func_ptr_linked = is_native_symbol ? linked_func : NULL;
  2264. function->signature = linked_signature;
  2265. function->attachment = linked_attachment;
  2266. function->call_conv_raw = linked_call_conv_raw;
  2267. #if WASM_ENABLE_MULTI_MODULE != 0
  2268. function->import_module = is_native_symbol ? NULL : sub_module;
  2269. function->import_func_linked = is_native_symbol ? NULL : linked_func;
  2270. #endif
  2271. return true;
  2272. fail:
  2273. return false;
  2274. }
  2275. static bool
  2276. check_table_max_size(uint32 init_size, uint32 max_size, char *error_buf,
  2277. uint32 error_buf_size)
  2278. {
  2279. if (max_size < init_size) {
  2280. set_error_buf(error_buf, error_buf_size,
  2281. "size minimum must not be greater than maximum");
  2282. return false;
  2283. }
  2284. return true;
  2285. }
  2286. static bool
  2287. load_table_import(const uint8 **p_buf, const uint8 *buf_end,
  2288. WASMModule *parent_module, const char *sub_module_name,
  2289. const char *table_name, WASMTableImport *table,
  2290. char *error_buf, uint32 error_buf_size)
  2291. {
  2292. const uint8 *p = *p_buf, *p_end = buf_end;
  2293. uint32 declare_elem_type = 0, declare_max_size_flag = 0,
  2294. declare_init_size = 0, declare_max_size = 0;
  2295. #if WASM_ENABLE_MULTI_MODULE != 0
  2296. WASMModule *sub_module = NULL;
  2297. WASMTable *linked_table = NULL;
  2298. #endif
  2299. #if WASM_ENABLE_GC != 0
  2300. WASMRefType ref_type;
  2301. bool need_ref_type_map;
  2302. #endif
  2303. #if WASM_ENABLE_GC == 0
  2304. CHECK_BUF(p, p_end, 1);
  2305. /* 0x70 or 0x6F */
  2306. declare_elem_type = read_uint8(p);
  2307. if (VALUE_TYPE_FUNCREF != declare_elem_type
  2308. #if WASM_ENABLE_REF_TYPES != 0
  2309. && VALUE_TYPE_EXTERNREF != declare_elem_type
  2310. #endif
  2311. ) {
  2312. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2313. return false;
  2314. }
  2315. #else /* else of WASM_ENABLE_GC == 0 */
  2316. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2317. &need_ref_type_map, &ref_type, false, error_buf,
  2318. error_buf_size)) {
  2319. return false;
  2320. }
  2321. if (wasm_is_reftype_htref_non_nullable(ref_type.ref_type)) {
  2322. set_error_buf(error_buf, error_buf_size, "type mismatch");
  2323. return false;
  2324. }
  2325. declare_elem_type = ref_type.ref_type;
  2326. if (need_ref_type_map) {
  2327. if (!(table->elem_ref_type =
  2328. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2329. error_buf, error_buf_size))) {
  2330. return false;
  2331. }
  2332. }
  2333. #if TRACE_WASM_LOADER != 0
  2334. os_printf("import table type: ");
  2335. wasm_dump_value_type(declare_elem_type, table->elem_ref_type);
  2336. os_printf("\n");
  2337. #endif
  2338. #endif /* end of WASM_ENABLE_GC == 0 */
  2339. read_leb_uint32(p, p_end, declare_max_size_flag);
  2340. if (declare_max_size_flag > 1) {
  2341. set_error_buf(error_buf, error_buf_size, "integer too large");
  2342. return false;
  2343. }
  2344. read_leb_uint32(p, p_end, declare_init_size);
  2345. if (declare_max_size_flag) {
  2346. read_leb_uint32(p, p_end, declare_max_size);
  2347. if (!check_table_max_size(declare_init_size, declare_max_size,
  2348. error_buf, error_buf_size))
  2349. return false;
  2350. }
  2351. adjust_table_max_size(declare_init_size, declare_max_size_flag,
  2352. &declare_max_size);
  2353. *p_buf = p;
  2354. #if WASM_ENABLE_MULTI_MODULE != 0
  2355. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2356. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2357. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2358. error_buf_size);
  2359. if (!sub_module) {
  2360. return false;
  2361. }
  2362. linked_table = wasm_loader_resolve_table(
  2363. sub_module_name, table_name, declare_init_size, declare_max_size,
  2364. error_buf, error_buf_size);
  2365. if (!linked_table) {
  2366. return false;
  2367. }
  2368. /* reset with linked table limit */
  2369. declare_elem_type = linked_table->elem_type;
  2370. declare_init_size = linked_table->init_size;
  2371. declare_max_size = linked_table->max_size;
  2372. declare_max_size_flag = linked_table->flags;
  2373. table->import_table_linked = linked_table;
  2374. table->import_module = sub_module;
  2375. }
  2376. #endif /* WASM_ENABLE_MULTI_MODULE != 0 */
  2377. /* (table (export "table") 10 20 funcref) */
  2378. /* we need this section working in wamrc */
  2379. if (!strcmp("spectest", sub_module_name)) {
  2380. const uint32 spectest_table_init_size = 10;
  2381. const uint32 spectest_table_max_size = 20;
  2382. if (strcmp("table", table_name)) {
  2383. set_error_buf(error_buf, error_buf_size,
  2384. "incompatible import type or unknown import");
  2385. return false;
  2386. }
  2387. if (declare_init_size > spectest_table_init_size
  2388. || declare_max_size < spectest_table_max_size) {
  2389. set_error_buf(error_buf, error_buf_size,
  2390. "incompatible import type");
  2391. return false;
  2392. }
  2393. declare_init_size = spectest_table_init_size;
  2394. declare_max_size = spectest_table_max_size;
  2395. }
  2396. /* now we believe all declaration are ok */
  2397. table->elem_type = declare_elem_type;
  2398. table->init_size = declare_init_size;
  2399. table->flags = declare_max_size_flag;
  2400. table->max_size = declare_max_size;
  2401. #if WASM_ENABLE_WAMR_COMPILER != 0
  2402. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2403. parent_module->is_ref_types_used = true;
  2404. #endif
  2405. (void)parent_module;
  2406. return true;
  2407. fail:
  2408. return false;
  2409. }
  2410. static bool
  2411. check_memory_init_size(bool is_memory64, uint32 init_size, char *error_buf,
  2412. uint32 error_buf_size)
  2413. {
  2414. uint32 default_max_size =
  2415. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2416. if (!is_memory64 && init_size > default_max_size) {
  2417. set_error_buf(error_buf, error_buf_size,
  2418. "memory size must be at most 65536 pages (4GiB)");
  2419. return false;
  2420. }
  2421. #if WASM_ENABLE_MEMORY64 != 0
  2422. else if (is_memory64 && init_size > default_max_size) {
  2423. set_error_buf(
  2424. error_buf, error_buf_size,
  2425. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2426. return false;
  2427. }
  2428. #endif
  2429. return true;
  2430. }
  2431. static bool
  2432. check_memory_max_size(bool is_memory64, uint32 init_size, uint32 max_size,
  2433. char *error_buf, uint32 error_buf_size)
  2434. {
  2435. uint32 default_max_size =
  2436. is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2437. if (max_size < init_size) {
  2438. set_error_buf(error_buf, error_buf_size,
  2439. "size minimum must not be greater than maximum");
  2440. return false;
  2441. }
  2442. if (!is_memory64 && max_size > default_max_size) {
  2443. set_error_buf(error_buf, error_buf_size,
  2444. "memory size must be at most 65536 pages (4GiB)");
  2445. return false;
  2446. }
  2447. #if WASM_ENABLE_MEMORY64 != 0
  2448. else if (is_memory64 && max_size > default_max_size) {
  2449. set_error_buf(
  2450. error_buf, error_buf_size,
  2451. "memory size must be at most 4,294,967,295 pages (274 Terabyte)");
  2452. return false;
  2453. }
  2454. #endif
  2455. return true;
  2456. }
  2457. static bool
  2458. check_memory_flag(const uint8 mem_flag, char *error_buf, uint32 error_buf_size)
  2459. {
  2460. /* Check whether certain features indicated by mem_flag are enabled in
  2461. * runtime */
  2462. if (mem_flag > MAX_PAGE_COUNT_FLAG) {
  2463. #if WASM_ENABLE_SHARED_MEMORY == 0
  2464. if (mem_flag & SHARED_MEMORY_FLAG) {
  2465. LOG_VERBOSE("shared memory flag was found, please enable shared "
  2466. "memory, lib-pthread or lib-wasi-threads");
  2467. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2468. return false;
  2469. }
  2470. #endif
  2471. #if WASM_ENABLE_MEMORY64 == 0
  2472. if (mem_flag & MEMORY64_FLAG) {
  2473. LOG_VERBOSE("memory64 flag was found, please enable memory64");
  2474. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2475. return false;
  2476. }
  2477. #endif
  2478. }
  2479. if (mem_flag > MAX_PAGE_COUNT_FLAG + SHARED_MEMORY_FLAG + MEMORY64_FLAG) {
  2480. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2481. return false;
  2482. }
  2483. else if ((mem_flag & SHARED_MEMORY_FLAG)
  2484. && !(mem_flag & MAX_PAGE_COUNT_FLAG)) {
  2485. set_error_buf(error_buf, error_buf_size,
  2486. "shared memory must have maximum");
  2487. return false;
  2488. }
  2489. return true;
  2490. }
  2491. static bool
  2492. load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
  2493. WASMModule *parent_module, const char *sub_module_name,
  2494. const char *memory_name, WASMMemoryImport *memory,
  2495. char *error_buf, uint32 error_buf_size)
  2496. {
  2497. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2498. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2499. uint32 pool_size = wasm_runtime_memory_pool_size();
  2500. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2501. / DEFAULT_NUM_BYTES_PER_PAGE;
  2502. #else
  2503. uint32 max_page_count;
  2504. #endif /* WASM_ENABLE_APP_FRAMEWORK */
  2505. uint32 mem_flag = 0;
  2506. bool is_memory64 = false;
  2507. uint32 declare_init_page_count = 0;
  2508. uint32 declare_max_page_count = 0;
  2509. #if WASM_ENABLE_MULTI_MODULE != 0
  2510. WASMModule *sub_module = NULL;
  2511. WASMMemory *linked_memory = NULL;
  2512. #endif
  2513. p_org = p;
  2514. read_leb_uint32(p, p_end, mem_flag);
  2515. is_memory64 = mem_flag & MEMORY64_FLAG;
  2516. if (p - p_org > 1) {
  2517. LOG_VERBOSE("integer representation too long");
  2518. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2519. return false;
  2520. }
  2521. if (!check_memory_flag(mem_flag, error_buf, error_buf_size)) {
  2522. return false;
  2523. }
  2524. read_leb_uint32(p, p_end, declare_init_page_count);
  2525. if (!check_memory_init_size(is_memory64, declare_init_page_count, error_buf,
  2526. error_buf_size)) {
  2527. return false;
  2528. }
  2529. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2530. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2531. #endif
  2532. if (mem_flag & MAX_PAGE_COUNT_FLAG) {
  2533. read_leb_uint32(p, p_end, declare_max_page_count);
  2534. if (!check_memory_max_size(is_memory64, declare_init_page_count,
  2535. declare_max_page_count, error_buf,
  2536. error_buf_size)) {
  2537. return false;
  2538. }
  2539. if (declare_max_page_count > max_page_count) {
  2540. declare_max_page_count = max_page_count;
  2541. }
  2542. }
  2543. else {
  2544. /* Limit the maximum memory size to max_page_count */
  2545. declare_max_page_count = max_page_count;
  2546. }
  2547. #if WASM_ENABLE_MULTI_MODULE != 0
  2548. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2549. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2550. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2551. error_buf_size);
  2552. if (!sub_module) {
  2553. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  2554. /* Avoid memory import failure when wasi-threads is enabled
  2555. and the memory is shared */
  2556. if (!(mem_flag & SHARED_MEMORY_FLAG))
  2557. return false;
  2558. #else
  2559. return false;
  2560. #endif /* WASM_ENABLE_LIB_WASI_THREADS */
  2561. }
  2562. else {
  2563. linked_memory = wasm_loader_resolve_memory(
  2564. sub_module_name, memory_name, declare_init_page_count,
  2565. declare_max_page_count, error_buf, error_buf_size);
  2566. if (!linked_memory) {
  2567. return false;
  2568. }
  2569. /**
  2570. * reset with linked memory limit
  2571. */
  2572. memory->import_module = sub_module;
  2573. memory->import_memory_linked = linked_memory;
  2574. declare_init_page_count = linked_memory->init_page_count;
  2575. declare_max_page_count = linked_memory->max_page_count;
  2576. }
  2577. }
  2578. #endif
  2579. /* (memory (export "memory") 1 2) */
  2580. if (!strcmp("spectest", sub_module_name)) {
  2581. uint32 spectest_memory_init_page = 1;
  2582. uint32 spectest_memory_max_page = 2;
  2583. if (strcmp("memory", memory_name)) {
  2584. set_error_buf(error_buf, error_buf_size,
  2585. "incompatible import type or unknown import");
  2586. return false;
  2587. }
  2588. if (declare_init_page_count > spectest_memory_init_page
  2589. || declare_max_page_count < spectest_memory_max_page) {
  2590. set_error_buf(error_buf, error_buf_size,
  2591. "incompatible import type");
  2592. return false;
  2593. }
  2594. declare_init_page_count = spectest_memory_init_page;
  2595. declare_max_page_count = spectest_memory_max_page;
  2596. }
  2597. /* now we believe all declaration are ok */
  2598. memory->flags = mem_flag;
  2599. memory->init_page_count = declare_init_page_count;
  2600. memory->max_page_count = declare_max_page_count;
  2601. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2602. *p_buf = p;
  2603. (void)parent_module;
  2604. return true;
  2605. fail:
  2606. return false;
  2607. }
  2608. #if WASM_ENABLE_TAGS != 0
  2609. static bool
  2610. load_tag_import(const uint8 **p_buf, const uint8 *buf_end,
  2611. const WASMModule *parent_module, /* this module ! */
  2612. const char *sub_module_name, const char *tag_name,
  2613. WASMTagImport *tag, /* structure to fill */
  2614. char *error_buf, uint32 error_buf_size)
  2615. {
  2616. /* attribute and type of the import statement */
  2617. uint8 declare_tag_attribute;
  2618. uint32 declare_type_index;
  2619. const uint8 *p = *p_buf, *p_end = buf_end;
  2620. #if WASM_ENABLE_MULTI_MODULE != 0
  2621. WASMModule *sub_module = NULL;
  2622. #endif
  2623. /* get the one byte attribute */
  2624. CHECK_BUF(p, p_end, 1);
  2625. declare_tag_attribute = read_uint8(p);
  2626. if (declare_tag_attribute != 0) {
  2627. set_error_buf(error_buf, error_buf_size, "unknown tag attribute");
  2628. goto fail;
  2629. }
  2630. /* get type */
  2631. read_leb_uint32(p, p_end, declare_type_index);
  2632. /* compare against module->types */
  2633. if (declare_type_index >= parent_module->type_count) {
  2634. set_error_buf(error_buf, error_buf_size, "unknown tag type");
  2635. goto fail;
  2636. }
  2637. WASMType *declare_tag_type = parent_module->types[declare_type_index];
  2638. /* check, that the type of the declared tag returns void */
  2639. if (declare_tag_type->result_count != 0) {
  2640. set_error_buf(error_buf, error_buf_size,
  2641. "tag type signature does not return void");
  2642. goto fail;
  2643. }
  2644. #if WASM_ENABLE_MULTI_MODULE != 0
  2645. if (!wasm_runtime_is_built_in_module(sub_module_name)) {
  2646. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2647. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2648. error_buf_size);
  2649. if (!sub_module) {
  2650. return false;
  2651. }
  2652. /* wasm_loader_resolve_tag checks, that the imported tag
  2653. * and the declared tag have the same type
  2654. */
  2655. uint32 linked_tag_index = 0;
  2656. WASMTag *linked_tag = wasm_loader_resolve_tag(
  2657. sub_module_name, tag_name, declare_tag_type,
  2658. &linked_tag_index /* out */, error_buf, error_buf_size);
  2659. if (linked_tag) {
  2660. tag->import_module = sub_module;
  2661. tag->import_tag_linked = linked_tag;
  2662. tag->import_tag_index_linked = linked_tag_index;
  2663. }
  2664. }
  2665. #endif
  2666. /* store to module tag declarations */
  2667. tag->attribute = declare_tag_attribute;
  2668. tag->type = declare_type_index;
  2669. tag->module_name = (char *)sub_module_name;
  2670. tag->field_name = (char *)tag_name;
  2671. tag->tag_type = declare_tag_type;
  2672. *p_buf = p;
  2673. (void)parent_module;
  2674. LOG_VERBOSE("Load tag import success\n");
  2675. return true;
  2676. fail:
  2677. return false;
  2678. }
  2679. #endif /* end of WASM_ENABLE_TAGS != 0 */
  2680. static bool
  2681. load_global_import(const uint8 **p_buf, const uint8 *buf_end,
  2682. WASMModule *parent_module, char *sub_module_name,
  2683. char *global_name, WASMGlobalImport *global, char *error_buf,
  2684. uint32 error_buf_size)
  2685. {
  2686. const uint8 *p = *p_buf, *p_end = buf_end;
  2687. uint8 declare_type = 0;
  2688. uint8 declare_mutable = 0;
  2689. #if WASM_ENABLE_MULTI_MODULE != 0
  2690. WASMModule *sub_module = NULL;
  2691. WASMGlobal *linked_global = NULL;
  2692. #endif
  2693. #if WASM_ENABLE_GC != 0
  2694. WASMRefType ref_type;
  2695. bool need_ref_type_map;
  2696. #endif
  2697. bool ret = false;
  2698. #if WASM_ENABLE_GC == 0
  2699. CHECK_BUF(p, p_end, 2);
  2700. declare_type = read_uint8(p);
  2701. declare_mutable = read_uint8(p);
  2702. #else
  2703. if (!resolve_value_type(&p, p_end, parent_module, parent_module->type_count,
  2704. &need_ref_type_map, &ref_type, false, error_buf,
  2705. error_buf_size)) {
  2706. return false;
  2707. }
  2708. declare_type = ref_type.ref_type;
  2709. if (need_ref_type_map) {
  2710. if (!(global->ref_type =
  2711. reftype_set_insert(parent_module->ref_type_set, &ref_type,
  2712. error_buf, error_buf_size))) {
  2713. return false;
  2714. }
  2715. }
  2716. #if TRACE_WASM_LOADER != 0
  2717. os_printf("import global type: ");
  2718. wasm_dump_value_type(declare_type, global->ref_type);
  2719. os_printf("\n");
  2720. #endif
  2721. CHECK_BUF(p, p_end, 1);
  2722. declare_mutable = read_uint8(p);
  2723. #endif /* end of WASM_ENABLE_GC == 0 */
  2724. *p_buf = p;
  2725. if (!check_mutability(declare_mutable, error_buf, error_buf_size)) {
  2726. return false;
  2727. }
  2728. #if WASM_ENABLE_LIBC_BUILTIN != 0
  2729. ret = wasm_native_lookup_libc_builtin_global(sub_module_name, global_name,
  2730. global);
  2731. if (ret) {
  2732. if (global->type != declare_type
  2733. || global->is_mutable != declare_mutable) {
  2734. set_error_buf(error_buf, error_buf_size,
  2735. "incompatible import type");
  2736. return false;
  2737. }
  2738. global->is_linked = true;
  2739. }
  2740. #endif
  2741. #if WASM_ENABLE_MULTI_MODULE != 0
  2742. if (!global->is_linked
  2743. && !wasm_runtime_is_built_in_module(sub_module_name)) {
  2744. sub_module = (WASMModule *)wasm_runtime_load_depended_module(
  2745. (WASMModuleCommon *)parent_module, sub_module_name, error_buf,
  2746. error_buf_size);
  2747. if (!sub_module) {
  2748. return false;
  2749. }
  2750. /* check sub modules */
  2751. linked_global = wasm_loader_resolve_global(
  2752. sub_module_name, global_name, declare_type, declare_mutable,
  2753. error_buf, error_buf_size);
  2754. if (linked_global) {
  2755. global->import_module = sub_module;
  2756. global->import_global_linked = linked_global;
  2757. global->is_linked = true;
  2758. }
  2759. }
  2760. #endif
  2761. global->module_name = sub_module_name;
  2762. global->field_name = global_name;
  2763. global->type = declare_type;
  2764. global->is_mutable = (declare_mutable == 1);
  2765. #if WASM_ENABLE_WAMR_COMPILER != 0
  2766. if (global->type == VALUE_TYPE_V128)
  2767. parent_module->is_simd_used = true;
  2768. else if (global->type == VALUE_TYPE_EXTERNREF)
  2769. parent_module->is_ref_types_used = true;
  2770. #endif
  2771. (void)parent_module;
  2772. (void)ret;
  2773. return true;
  2774. fail:
  2775. return false;
  2776. }
  2777. static bool
  2778. load_table(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  2779. WASMTable *table, char *error_buf, uint32 error_buf_size)
  2780. {
  2781. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2782. #if WASM_ENABLE_GC != 0
  2783. WASMRefType ref_type;
  2784. bool need_ref_type_map;
  2785. #endif
  2786. #if WASM_ENABLE_GC == 0
  2787. CHECK_BUF(p, p_end, 1);
  2788. /* 0x70 or 0x6F */
  2789. table->elem_type = read_uint8(p);
  2790. if (VALUE_TYPE_FUNCREF != table->elem_type
  2791. #if WASM_ENABLE_REF_TYPES != 0
  2792. && VALUE_TYPE_EXTERNREF != table->elem_type
  2793. #endif
  2794. ) {
  2795. set_error_buf(error_buf, error_buf_size, "incompatible import type");
  2796. return false;
  2797. }
  2798. #else /* else of WASM_ENABLE_GC == 0 */
  2799. if (!resolve_value_type(&p, p_end, module, module->type_count,
  2800. &need_ref_type_map, &ref_type, false, error_buf,
  2801. error_buf_size)) {
  2802. return false;
  2803. }
  2804. table->elem_type = ref_type.ref_type;
  2805. if (need_ref_type_map) {
  2806. if (!(table->elem_ref_type =
  2807. reftype_set_insert(module->ref_type_set, &ref_type, error_buf,
  2808. error_buf_size))) {
  2809. return false;
  2810. }
  2811. }
  2812. #if TRACE_WASM_LOADER != 0
  2813. os_printf("table type: ");
  2814. wasm_dump_value_type(table->elem_type, table->elem_ref_type);
  2815. os_printf("\n");
  2816. #endif
  2817. #endif /* end of WASM_ENABLE_GC == 0 */
  2818. p_org = p;
  2819. read_leb_uint32(p, p_end, table->flags);
  2820. #if WASM_ENABLE_SHARED_MEMORY == 0
  2821. if (p - p_org > 1) {
  2822. set_error_buf(error_buf, error_buf_size,
  2823. "integer representation too long");
  2824. return false;
  2825. }
  2826. if (table->flags > 1) {
  2827. set_error_buf(error_buf, error_buf_size, "integer too large");
  2828. return false;
  2829. }
  2830. #else
  2831. if (p - p_org > 1) {
  2832. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2833. return false;
  2834. }
  2835. if (table->flags == 2) {
  2836. set_error_buf(error_buf, error_buf_size, "tables cannot be shared");
  2837. return false;
  2838. }
  2839. if (table->flags > 1) {
  2840. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2841. return false;
  2842. }
  2843. #endif
  2844. read_leb_uint32(p, p_end, table->init_size);
  2845. if (table->flags) {
  2846. read_leb_uint32(p, p_end, table->max_size);
  2847. if (!check_table_max_size(table->init_size, table->max_size, error_buf,
  2848. error_buf_size))
  2849. return false;
  2850. }
  2851. adjust_table_max_size(table->init_size, table->flags, &table->max_size);
  2852. #if WASM_ENABLE_WAMR_COMPILER != 0
  2853. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  2854. module->is_ref_types_used = true;
  2855. #endif
  2856. *p_buf = p;
  2857. return true;
  2858. fail:
  2859. return false;
  2860. }
  2861. static bool
  2862. load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
  2863. char *error_buf, uint32 error_buf_size)
  2864. {
  2865. const uint8 *p = *p_buf, *p_end = buf_end, *p_org;
  2866. #if WASM_ENABLE_APP_FRAMEWORK != 0
  2867. uint32 pool_size = wasm_runtime_memory_pool_size();
  2868. uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
  2869. / DEFAULT_NUM_BYTES_PER_PAGE;
  2870. #else
  2871. uint32 max_page_count;
  2872. #endif
  2873. bool is_memory64 = false;
  2874. p_org = p;
  2875. read_leb_uint32(p, p_end, memory->flags);
  2876. is_memory64 = memory->flags & MEMORY64_FLAG;
  2877. if (p - p_org > 1) {
  2878. LOG_VERBOSE("integer representation too long");
  2879. set_error_buf(error_buf, error_buf_size, "invalid limits flags");
  2880. return false;
  2881. }
  2882. if (!check_memory_flag(memory->flags, error_buf, error_buf_size)) {
  2883. return false;
  2884. }
  2885. read_leb_uint32(p, p_end, memory->init_page_count);
  2886. if (!check_memory_init_size(is_memory64, memory->init_page_count, error_buf,
  2887. error_buf_size))
  2888. return false;
  2889. #if WASM_ENABLE_APP_FRAMEWORK == 0
  2890. max_page_count = is_memory64 ? DEFAULT_MEM64_MAX_PAGES : DEFAULT_MAX_PAGES;
  2891. #endif
  2892. if (memory->flags & 1) {
  2893. read_leb_uint32(p, p_end, memory->max_page_count);
  2894. if (!check_memory_max_size(is_memory64, memory->init_page_count,
  2895. memory->max_page_count, error_buf,
  2896. error_buf_size))
  2897. return false;
  2898. if (memory->max_page_count > max_page_count)
  2899. memory->max_page_count = max_page_count;
  2900. }
  2901. else {
  2902. /* Limit the maximum memory size to max_page_count */
  2903. memory->max_page_count = max_page_count;
  2904. }
  2905. memory->num_bytes_per_page = DEFAULT_NUM_BYTES_PER_PAGE;
  2906. *p_buf = p;
  2907. return true;
  2908. fail:
  2909. return false;
  2910. }
  2911. static bool
  2912. load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  2913. bool is_load_from_file_buf, char *error_buf,
  2914. uint32 error_buf_size)
  2915. {
  2916. const uint8 *p = buf, *p_end = buf_end, *p_old;
  2917. uint32 import_count, name_len, type_index, i, u32, flags;
  2918. uint64 total_size;
  2919. WASMImport *import;
  2920. WASMImport *import_functions = NULL, *import_tables = NULL;
  2921. WASMImport *import_memories = NULL, *import_globals = NULL;
  2922. #if WASM_ENABLE_TAGS != 0
  2923. WASMImport *import_tags = NULL;
  2924. #endif
  2925. char *sub_module_name, *field_name;
  2926. uint8 u8, kind, global_type;
  2927. read_leb_uint32(p, p_end, import_count);
  2928. if (import_count) {
  2929. module->import_count = import_count;
  2930. total_size = sizeof(WASMImport) * (uint64)import_count;
  2931. if (!(module->imports =
  2932. loader_malloc(total_size, error_buf, error_buf_size))) {
  2933. return false;
  2934. }
  2935. p_old = p;
  2936. /* Scan firstly to get import count of each type */
  2937. for (i = 0; i < import_count; i++) {
  2938. /* module name */
  2939. read_leb_uint32(p, p_end, name_len);
  2940. CHECK_BUF(p, p_end, name_len);
  2941. p += name_len;
  2942. /* field name */
  2943. read_leb_uint32(p, p_end, name_len);
  2944. CHECK_BUF(p, p_end, name_len);
  2945. p += name_len;
  2946. CHECK_BUF(p, p_end, 1);
  2947. /* 0x00/0x01/0x02/0x03/0x04 */
  2948. kind = read_uint8(p);
  2949. switch (kind) {
  2950. case IMPORT_KIND_FUNC: /* import function */
  2951. read_leb_uint32(p, p_end, type_index);
  2952. module->import_function_count++;
  2953. break;
  2954. case IMPORT_KIND_TABLE: /* import table */
  2955. CHECK_BUF(p, p_end, 1);
  2956. /* 0x70 */
  2957. u8 = read_uint8(p);
  2958. read_leb_uint32(p, p_end, flags);
  2959. read_leb_uint32(p, p_end, u32);
  2960. if (flags & 1)
  2961. read_leb_uint32(p, p_end, u32);
  2962. module->import_table_count++;
  2963. if (module->import_table_count > 1) {
  2964. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  2965. set_error_buf(error_buf, error_buf_size,
  2966. "multiple tables");
  2967. return false;
  2968. #elif WASM_ENABLE_WAMR_COMPILER != 0
  2969. module->is_ref_types_used = true;
  2970. #endif
  2971. }
  2972. break;
  2973. case IMPORT_KIND_MEMORY: /* import memory */
  2974. read_leb_uint32(p, p_end, flags);
  2975. read_leb_uint32(p, p_end, u32);
  2976. if (flags & 1)
  2977. read_leb_uint32(p, p_end, u32);
  2978. module->import_memory_count++;
  2979. if (module->import_memory_count > 1) {
  2980. set_error_buf(error_buf, error_buf_size,
  2981. "multiple memories");
  2982. return false;
  2983. }
  2984. break;
  2985. #if WASM_ENABLE_TAGS != 0
  2986. case IMPORT_KIND_TAG: /* import tags */
  2987. /* it only counts the number of tags to import */
  2988. module->import_tag_count++;
  2989. CHECK_BUF(p, p_end, 1);
  2990. u8 = read_uint8(p);
  2991. read_leb_uint32(p, p_end, type_index);
  2992. break;
  2993. #endif
  2994. case IMPORT_KIND_GLOBAL: /* import global */
  2995. #if WASM_ENABLE_GC != 0
  2996. /* valtype */
  2997. CHECK_BUF(p, p_end, 1);
  2998. global_type = read_uint8(p);
  2999. if (wasm_is_type_multi_byte_type(global_type)) {
  3000. int32 heap_type;
  3001. read_leb_int32(p, p_end, heap_type);
  3002. (void)heap_type;
  3003. }
  3004. /* mutability */
  3005. CHECK_BUF(p, p_end, 1);
  3006. p += 1;
  3007. #else
  3008. CHECK_BUF(p, p_end, 2);
  3009. p += 2;
  3010. #endif
  3011. (void)global_type;
  3012. module->import_global_count++;
  3013. break;
  3014. default:
  3015. set_error_buf(error_buf, error_buf_size,
  3016. "invalid import kind");
  3017. return false;
  3018. }
  3019. }
  3020. if (module->import_function_count)
  3021. import_functions = module->import_functions = module->imports;
  3022. if (module->import_table_count)
  3023. import_tables = module->import_tables =
  3024. module->imports + module->import_function_count;
  3025. if (module->import_memory_count)
  3026. import_memories = module->import_memories =
  3027. module->imports + module->import_function_count
  3028. + module->import_table_count;
  3029. #if WASM_ENABLE_TAGS != 0
  3030. if (module->import_tag_count)
  3031. import_tags = module->import_tags =
  3032. module->imports + module->import_function_count
  3033. + module->import_table_count + module->import_memory_count;
  3034. if (module->import_global_count)
  3035. import_globals = module->import_globals =
  3036. module->imports + module->import_function_count
  3037. + module->import_table_count + module->import_memory_count
  3038. + module->import_tag_count;
  3039. #else
  3040. if (module->import_global_count)
  3041. import_globals = module->import_globals =
  3042. module->imports + module->import_function_count
  3043. + module->import_table_count + module->import_memory_count;
  3044. #endif
  3045. p = p_old;
  3046. /* Scan again to resolve the data */
  3047. for (i = 0; i < import_count; i++) {
  3048. /* load module name */
  3049. read_leb_uint32(p, p_end, name_len);
  3050. CHECK_BUF(p, p_end, name_len);
  3051. if (!(sub_module_name = wasm_const_str_list_insert(
  3052. p, name_len, module, is_load_from_file_buf, error_buf,
  3053. error_buf_size))) {
  3054. return false;
  3055. }
  3056. p += name_len;
  3057. /* load field name */
  3058. read_leb_uint32(p, p_end, name_len);
  3059. CHECK_BUF(p, p_end, name_len);
  3060. if (!(field_name = wasm_const_str_list_insert(
  3061. p, name_len, module, is_load_from_file_buf, error_buf,
  3062. error_buf_size))) {
  3063. return false;
  3064. }
  3065. p += name_len;
  3066. CHECK_BUF(p, p_end, 1);
  3067. /* 0x00/0x01/0x02/0x03/0x4 */
  3068. kind = read_uint8(p);
  3069. switch (kind) {
  3070. case IMPORT_KIND_FUNC: /* import function */
  3071. bh_assert(import_functions);
  3072. import = import_functions++;
  3073. if (!load_function_import(
  3074. &p, p_end, module, sub_module_name, field_name,
  3075. &import->u.function, error_buf, error_buf_size)) {
  3076. return false;
  3077. }
  3078. break;
  3079. case IMPORT_KIND_TABLE: /* import table */
  3080. bh_assert(import_tables);
  3081. import = import_tables++;
  3082. if (!load_table_import(&p, p_end, module, sub_module_name,
  3083. field_name, &import->u.table,
  3084. error_buf, error_buf_size)) {
  3085. LOG_DEBUG("can not import such a table (%s,%s)",
  3086. sub_module_name, field_name);
  3087. return false;
  3088. }
  3089. break;
  3090. case IMPORT_KIND_MEMORY: /* import memory */
  3091. bh_assert(import_memories);
  3092. import = import_memories++;
  3093. if (!load_memory_import(&p, p_end, module, sub_module_name,
  3094. field_name, &import->u.memory,
  3095. error_buf, error_buf_size)) {
  3096. return false;
  3097. }
  3098. break;
  3099. #if WASM_ENABLE_TAGS != 0
  3100. case IMPORT_KIND_TAG:
  3101. bh_assert(import_tags);
  3102. import = import_tags++;
  3103. if (!load_tag_import(&p, p_end, module, sub_module_name,
  3104. field_name, &import->u.tag, error_buf,
  3105. error_buf_size)) {
  3106. return false;
  3107. }
  3108. break;
  3109. #endif
  3110. case IMPORT_KIND_GLOBAL: /* import global */
  3111. bh_assert(import_globals);
  3112. import = import_globals++;
  3113. if (!load_global_import(&p, p_end, module, sub_module_name,
  3114. field_name, &import->u.global,
  3115. error_buf, error_buf_size)) {
  3116. return false;
  3117. }
  3118. break;
  3119. default:
  3120. set_error_buf(error_buf, error_buf_size,
  3121. "invalid import kind");
  3122. return false;
  3123. }
  3124. import->kind = kind;
  3125. import->u.names.module_name = sub_module_name;
  3126. import->u.names.field_name = field_name;
  3127. }
  3128. #if WASM_ENABLE_LIBC_WASI != 0
  3129. import = module->import_functions;
  3130. for (i = 0; i < module->import_function_count; i++, import++) {
  3131. if (!strcmp(import->u.names.module_name, "wasi_unstable")
  3132. || !strcmp(import->u.names.module_name,
  3133. "wasi_snapshot_preview1")) {
  3134. module->import_wasi_api = true;
  3135. break;
  3136. }
  3137. }
  3138. #endif
  3139. }
  3140. if (p != p_end) {
  3141. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3142. return false;
  3143. }
  3144. LOG_VERBOSE("Load import section success.\n");
  3145. (void)u8;
  3146. (void)u32;
  3147. (void)type_index;
  3148. return true;
  3149. fail:
  3150. return false;
  3151. }
  3152. static bool
  3153. init_function_local_offsets(WASMFunction *func, char *error_buf,
  3154. uint32 error_buf_size)
  3155. {
  3156. WASMFuncType *param_type = func->func_type;
  3157. uint32 param_count = param_type->param_count;
  3158. uint8 *param_types = param_type->types;
  3159. uint32 local_count = func->local_count;
  3160. uint8 *local_types = func->local_types;
  3161. uint32 i, local_offset = 0;
  3162. uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
  3163. /*
  3164. * Only allocate memory when total_size is not 0,
  3165. * or the return value of malloc(0) might be NULL on some platforms,
  3166. * which causes wasm loader return false.
  3167. */
  3168. if (total_size > 0
  3169. && !(func->local_offsets =
  3170. loader_malloc(total_size, error_buf, error_buf_size))) {
  3171. return false;
  3172. }
  3173. for (i = 0; i < param_count; i++) {
  3174. func->local_offsets[i] = (uint16)local_offset;
  3175. local_offset += wasm_value_type_cell_num(param_types[i]);
  3176. }
  3177. for (i = 0; i < local_count; i++) {
  3178. func->local_offsets[param_count + i] = (uint16)local_offset;
  3179. local_offset += wasm_value_type_cell_num(local_types[i]);
  3180. }
  3181. bh_assert(local_offset == func->param_cell_num + func->local_cell_num);
  3182. return true;
  3183. }
  3184. static bool
  3185. load_function_section(const uint8 *buf, const uint8 *buf_end,
  3186. const uint8 *buf_code, const uint8 *buf_code_end,
  3187. WASMModule *module, char *error_buf,
  3188. uint32 error_buf_size)
  3189. {
  3190. const uint8 *p = buf, *p_end = buf_end;
  3191. const uint8 *p_code = buf_code, *p_code_end, *p_code_save;
  3192. uint32 func_count;
  3193. uint64 total_size;
  3194. uint32 code_count = 0, code_size, type_index, i, j, k, local_type_index;
  3195. uint32 local_count, local_set_count, sub_local_count, local_cell_num;
  3196. uint8 type;
  3197. WASMFunction *func;
  3198. #if WASM_ENABLE_GC != 0
  3199. bool need_ref_type_map;
  3200. WASMRefType ref_type;
  3201. uint32 ref_type_map_count = 0, t = 0, type_index_org;
  3202. #endif
  3203. read_leb_uint32(p, p_end, func_count);
  3204. if (buf_code)
  3205. read_leb_uint32(p_code, buf_code_end, code_count);
  3206. if (func_count != code_count) {
  3207. set_error_buf(error_buf, error_buf_size,
  3208. "function and code section have inconsistent lengths or "
  3209. "unexpected end");
  3210. return false;
  3211. }
  3212. if (func_count) {
  3213. module->function_count = func_count;
  3214. total_size = sizeof(WASMFunction *) * (uint64)func_count;
  3215. if (!(module->functions =
  3216. loader_malloc(total_size, error_buf, error_buf_size))) {
  3217. return false;
  3218. }
  3219. for (i = 0; i < func_count; i++) {
  3220. /* Resolve function type */
  3221. read_leb_uint32(p, p_end, type_index);
  3222. if (type_index >= module->type_count) {
  3223. set_error_buf(error_buf, error_buf_size, "unknown type");
  3224. return false;
  3225. }
  3226. #if WASM_ENABLE_GC != 0
  3227. type_index_org = type_index;
  3228. #endif
  3229. #if (WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0) \
  3230. && WASM_ENABLE_GC == 0
  3231. type_index = wasm_get_smallest_type_idx(
  3232. module->types, module->type_count, type_index);
  3233. #endif
  3234. read_leb_uint32(p_code, buf_code_end, code_size);
  3235. if (code_size == 0 || p_code + code_size > buf_code_end) {
  3236. set_error_buf(error_buf, error_buf_size,
  3237. "invalid function code size");
  3238. return false;
  3239. }
  3240. /* Resolve local set count */
  3241. p_code_end = p_code + code_size;
  3242. local_count = 0;
  3243. read_leb_uint32(p_code, buf_code_end, local_set_count);
  3244. p_code_save = p_code;
  3245. #if WASM_ENABLE_GC != 0
  3246. ref_type_map_count = 0;
  3247. #endif
  3248. /* Calculate total local count */
  3249. for (j = 0; j < local_set_count; j++) {
  3250. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3251. if (sub_local_count > UINT32_MAX - local_count) {
  3252. set_error_buf(error_buf, error_buf_size, "too many locals");
  3253. return false;
  3254. }
  3255. #if WASM_ENABLE_GC == 0
  3256. CHECK_BUF(p_code, buf_code_end, 1);
  3257. /* 0x7F/0x7E/0x7D/0x7C */
  3258. type = read_uint8(p_code);
  3259. local_count += sub_local_count;
  3260. #if WASM_ENABLE_WAMR_COMPILER != 0
  3261. /* If any value's type is v128, mark the module as SIMD used */
  3262. if (type == VALUE_TYPE_V128)
  3263. module->is_simd_used = true;
  3264. #endif
  3265. #else
  3266. if (!resolve_value_type(&p_code, buf_code_end, module,
  3267. module->type_count, &need_ref_type_map,
  3268. &ref_type, false, error_buf,
  3269. error_buf_size)) {
  3270. return false;
  3271. }
  3272. local_count += sub_local_count;
  3273. if (need_ref_type_map)
  3274. ref_type_map_count += sub_local_count;
  3275. #endif
  3276. }
  3277. /* Alloc memory, layout: function structure + local types */
  3278. code_size = (uint32)(p_code_end - p_code);
  3279. total_size = sizeof(WASMFunction) + (uint64)local_count;
  3280. if (!(func = module->functions[i] =
  3281. loader_malloc(total_size, error_buf, error_buf_size))) {
  3282. return false;
  3283. }
  3284. #if WASM_ENABLE_GC != 0
  3285. if (ref_type_map_count > 0) {
  3286. total_size =
  3287. sizeof(WASMRefTypeMap) * (uint64)ref_type_map_count;
  3288. if (!(func->local_ref_type_maps = loader_malloc(
  3289. total_size, error_buf, error_buf_size))) {
  3290. return false;
  3291. }
  3292. func->local_ref_type_map_count = ref_type_map_count;
  3293. }
  3294. #endif
  3295. /* Set function type, local count, code size and code body */
  3296. func->func_type = (WASMFuncType *)module->types[type_index];
  3297. func->local_count = local_count;
  3298. if (local_count > 0)
  3299. func->local_types = (uint8 *)func + sizeof(WASMFunction);
  3300. func->code_size = code_size;
  3301. /*
  3302. * we shall make a copy of code body [p_code, p_code + code_size]
  3303. * when we are worrying about inappropriate releasing behaviour.
  3304. * all code bodies are actually in a buffer which user allocates in
  3305. * his embedding environment and we don't have power on them.
  3306. * it will be like:
  3307. * code_body_cp = malloc(code_size);
  3308. * memcpy(code_body_cp, p_code, code_size);
  3309. * func->code = code_body_cp;
  3310. */
  3311. func->code = (uint8 *)p_code;
  3312. #if WASM_ENABLE_GC != 0
  3313. func->type_idx = type_index_org;
  3314. #endif
  3315. #if WASM_ENABLE_GC != 0
  3316. t = 0;
  3317. #endif
  3318. /* Load each local type */
  3319. p_code = p_code_save;
  3320. local_type_index = 0;
  3321. for (j = 0; j < local_set_count; j++) {
  3322. read_leb_uint32(p_code, buf_code_end, sub_local_count);
  3323. /* Note: sub_local_count is allowed to be 0 */
  3324. if (local_type_index > UINT32_MAX - sub_local_count
  3325. || local_type_index + sub_local_count > local_count) {
  3326. set_error_buf(error_buf, error_buf_size,
  3327. "invalid local count");
  3328. return false;
  3329. }
  3330. #if WASM_ENABLE_GC == 0
  3331. CHECK_BUF(p_code, buf_code_end, 1);
  3332. /* 0x7F/0x7E/0x7D/0x7C */
  3333. type = read_uint8(p_code);
  3334. if (!is_value_type(type)) {
  3335. if (type == VALUE_TYPE_V128)
  3336. set_error_buf(error_buf, error_buf_size,
  3337. "v128 value type requires simd feature");
  3338. else if (type == VALUE_TYPE_FUNCREF
  3339. || type == VALUE_TYPE_EXTERNREF)
  3340. set_error_buf(error_buf, error_buf_size,
  3341. "ref value type requires "
  3342. "reference types feature");
  3343. else
  3344. set_error_buf_v(error_buf, error_buf_size,
  3345. "invalid local type 0x%02X", type);
  3346. return false;
  3347. }
  3348. #else
  3349. if (!resolve_value_type(&p_code, buf_code_end, module,
  3350. module->type_count, &need_ref_type_map,
  3351. &ref_type, false, error_buf,
  3352. error_buf_size)) {
  3353. return false;
  3354. }
  3355. if (need_ref_type_map) {
  3356. WASMRefType *ref_type_tmp;
  3357. if (!(ref_type_tmp = reftype_set_insert(
  3358. module->ref_type_set, &ref_type, error_buf,
  3359. error_buf_size))) {
  3360. return false;
  3361. }
  3362. for (k = 0; k < sub_local_count; k++) {
  3363. func->local_ref_type_maps[t + k].ref_type =
  3364. ref_type_tmp;
  3365. func->local_ref_type_maps[t + k].index =
  3366. local_type_index + k;
  3367. }
  3368. t += sub_local_count;
  3369. }
  3370. type = ref_type.ref_type;
  3371. #endif
  3372. for (k = 0; k < sub_local_count; k++) {
  3373. func->local_types[local_type_index++] = type;
  3374. }
  3375. #if WASM_ENABLE_WAMR_COMPILER != 0
  3376. if (type == VALUE_TYPE_V128)
  3377. module->is_simd_used = true;
  3378. else if (type == VALUE_TYPE_FUNCREF
  3379. || type == VALUE_TYPE_EXTERNREF)
  3380. module->is_ref_types_used = true;
  3381. #endif
  3382. }
  3383. bh_assert(local_type_index == func->local_count);
  3384. #if WASM_ENABLE_GC != 0
  3385. bh_assert(t == func->local_ref_type_map_count);
  3386. #if TRACE_WASM_LOADER != 0
  3387. os_printf("func %u, local types: [", i);
  3388. k = 0;
  3389. for (j = 0; j < func->local_count; j++) {
  3390. WASMRefType *ref_type_tmp = NULL;
  3391. if (wasm_is_type_multi_byte_type(func->local_types[j])) {
  3392. bh_assert(j == func->local_ref_type_maps[k].index);
  3393. ref_type_tmp = func->local_ref_type_maps[k++].ref_type;
  3394. }
  3395. wasm_dump_value_type(func->local_types[j], ref_type_tmp);
  3396. if (j < func->local_count - 1)
  3397. os_printf(" ");
  3398. }
  3399. os_printf("]\n");
  3400. #endif
  3401. #endif
  3402. func->param_cell_num = func->func_type->param_cell_num;
  3403. func->ret_cell_num = func->func_type->ret_cell_num;
  3404. local_cell_num =
  3405. wasm_get_cell_num(func->local_types, func->local_count);
  3406. if (local_cell_num > UINT16_MAX) {
  3407. set_error_buf(error_buf, error_buf_size,
  3408. "local count too large");
  3409. return false;
  3410. }
  3411. func->local_cell_num = (uint16)local_cell_num;
  3412. if (!init_function_local_offsets(func, error_buf, error_buf_size))
  3413. return false;
  3414. p_code = p_code_end;
  3415. }
  3416. }
  3417. if (p != p_end) {
  3418. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3419. return false;
  3420. }
  3421. LOG_VERBOSE("Load function section success.\n");
  3422. return true;
  3423. fail:
  3424. return false;
  3425. }
  3426. static bool
  3427. load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3428. char *error_buf, uint32 error_buf_size)
  3429. {
  3430. const uint8 *p = buf, *p_end = buf_end;
  3431. uint32 table_count, i;
  3432. uint64 total_size;
  3433. WASMTable *table;
  3434. read_leb_uint32(p, p_end, table_count);
  3435. if (module->import_table_count + table_count > 1) {
  3436. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3437. /* a total of one table is allowed */
  3438. set_error_buf(error_buf, error_buf_size, "multiple tables");
  3439. return false;
  3440. #elif WASM_ENABLE_WAMR_COMPILER != 0
  3441. module->is_ref_types_used = true;
  3442. #endif
  3443. }
  3444. if (table_count) {
  3445. module->table_count = table_count;
  3446. total_size = sizeof(WASMTable) * (uint64)table_count;
  3447. if (!(module->tables =
  3448. loader_malloc(total_size, error_buf, error_buf_size))) {
  3449. return false;
  3450. }
  3451. /* load each table */
  3452. table = module->tables;
  3453. for (i = 0; i < table_count; i++, table++) {
  3454. #if WASM_ENABLE_GC != 0
  3455. uint8 flag;
  3456. bool has_init = false;
  3457. CHECK_BUF(buf, buf_end, 1);
  3458. flag = read_uint8(p);
  3459. if (flag == TABLE_INIT_EXPR_FLAG) {
  3460. CHECK_BUF(buf, buf_end, 1);
  3461. flag = read_uint8(p);
  3462. if (flag != 0x00) {
  3463. set_error_buf(error_buf, error_buf_size,
  3464. "invalid leading bytes for table");
  3465. return false;
  3466. }
  3467. has_init = true;
  3468. }
  3469. else {
  3470. p--;
  3471. }
  3472. #endif /* end of WASM_ENABLE_GC != 0 */
  3473. if (!load_table(&p, p_end, module, table, error_buf,
  3474. error_buf_size))
  3475. return false;
  3476. #if WASM_ENABLE_GC != 0
  3477. if (has_init) {
  3478. if (!load_init_expr(module, &p, p_end, &table->init_expr,
  3479. table->elem_type, table->elem_ref_type,
  3480. error_buf, error_buf_size))
  3481. return false;
  3482. if (table->init_expr.init_expr_type >= INIT_EXPR_TYPE_STRUCT_NEW
  3483. && table->init_expr.init_expr_type
  3484. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
  3485. set_error_buf(
  3486. error_buf, error_buf_size,
  3487. "unsupported initializer expression for table");
  3488. return false;
  3489. }
  3490. }
  3491. else {
  3492. if (wasm_is_reftype_htref_non_nullable(table->elem_type)) {
  3493. set_error_buf(
  3494. error_buf, error_buf_size,
  3495. "type mismatch: non-nullable table without init expr");
  3496. return false;
  3497. }
  3498. }
  3499. #endif /* end of WASM_ENABLE_GC != 0 */
  3500. #if WASM_ENABLE_WAMR_COMPILER != 0
  3501. if (table->elem_type == VALUE_TYPE_EXTERNREF)
  3502. module->is_ref_types_used = true;
  3503. #endif
  3504. }
  3505. }
  3506. if (p != p_end) {
  3507. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3508. return false;
  3509. }
  3510. LOG_VERBOSE("Load table section success.\n");
  3511. return true;
  3512. fail:
  3513. return false;
  3514. }
  3515. static bool
  3516. load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3517. char *error_buf, uint32 error_buf_size)
  3518. {
  3519. const uint8 *p = buf, *p_end = buf_end;
  3520. uint32 memory_count, i;
  3521. uint64 total_size;
  3522. WASMMemory *memory;
  3523. read_leb_uint32(p, p_end, memory_count);
  3524. /* a total of one memory is allowed */
  3525. if (module->import_memory_count + memory_count > 1) {
  3526. set_error_buf(error_buf, error_buf_size, "multiple memories");
  3527. return false;
  3528. }
  3529. if (memory_count) {
  3530. module->memory_count = memory_count;
  3531. total_size = sizeof(WASMMemory) * (uint64)memory_count;
  3532. if (!(module->memories =
  3533. loader_malloc(total_size, error_buf, error_buf_size))) {
  3534. return false;
  3535. }
  3536. /* load each memory */
  3537. memory = module->memories;
  3538. for (i = 0; i < memory_count; i++, memory++)
  3539. if (!load_memory(&p, p_end, memory, error_buf, error_buf_size))
  3540. return false;
  3541. }
  3542. if (p != p_end) {
  3543. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3544. return false;
  3545. }
  3546. LOG_VERBOSE("Load memory section success.\n");
  3547. return true;
  3548. fail:
  3549. return false;
  3550. }
  3551. static bool
  3552. load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3553. char *error_buf, uint32 error_buf_size)
  3554. {
  3555. const uint8 *p = buf, *p_end = buf_end;
  3556. uint32 global_count, i;
  3557. uint64 total_size;
  3558. WASMGlobal *global;
  3559. uint8 mutable;
  3560. #if WASM_ENABLE_GC != 0
  3561. bool need_ref_type_map;
  3562. WASMRefType ref_type;
  3563. #endif
  3564. read_leb_uint32(p, p_end, global_count);
  3565. module->global_count = 0;
  3566. if (global_count) {
  3567. total_size = sizeof(WASMGlobal) * (uint64)global_count;
  3568. if (!(module->globals =
  3569. loader_malloc(total_size, error_buf, error_buf_size))) {
  3570. return false;
  3571. }
  3572. global = module->globals;
  3573. for (i = 0; i < global_count; i++, global++) {
  3574. #if WASM_ENABLE_GC == 0
  3575. CHECK_BUF(p, p_end, 2);
  3576. global->type = read_uint8(p);
  3577. mutable = read_uint8(p);
  3578. #else
  3579. if (!resolve_value_type(&p, p_end, module, module->type_count,
  3580. &need_ref_type_map, &ref_type, false,
  3581. error_buf, error_buf_size)) {
  3582. return false;
  3583. }
  3584. global->type = ref_type.ref_type;
  3585. CHECK_BUF(p, p_end, 1);
  3586. mutable = read_uint8(p);
  3587. #endif /* end of WASM_ENABLE_GC */
  3588. #if WASM_ENABLE_WAMR_COMPILER != 0
  3589. if (global->type == VALUE_TYPE_V128)
  3590. module->is_simd_used = true;
  3591. else if (global->type == VALUE_TYPE_FUNCREF
  3592. || global->type == VALUE_TYPE_EXTERNREF)
  3593. module->is_ref_types_used = true;
  3594. #endif
  3595. if (!check_mutability(mutable, error_buf, error_buf_size)) {
  3596. return false;
  3597. }
  3598. global->is_mutable = mutable ? true : false;
  3599. /* initialize expression */
  3600. if (!load_init_expr(module, &p, p_end, &(global->init_expr),
  3601. global->type,
  3602. #if WASM_ENABLE_GC == 0
  3603. NULL,
  3604. #else
  3605. &ref_type,
  3606. #endif
  3607. error_buf, error_buf_size))
  3608. return false;
  3609. #if WASM_ENABLE_GC != 0
  3610. if (global->init_expr.init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL) {
  3611. uint8 global_type;
  3612. WASMRefType *global_ref_type;
  3613. uint32 global_idx = global->init_expr.u.global_index;
  3614. if (global->init_expr.u.global_index
  3615. >= module->import_global_count + i) {
  3616. set_error_buf(error_buf, error_buf_size, "unknown global");
  3617. return false;
  3618. }
  3619. if (global_idx < module->import_global_count) {
  3620. global_type =
  3621. module->import_globals[global_idx].u.global.type;
  3622. global_ref_type =
  3623. module->import_globals[global_idx].u.global.ref_type;
  3624. }
  3625. else {
  3626. global_type =
  3627. module
  3628. ->globals[global_idx - module->import_global_count]
  3629. .type;
  3630. global_ref_type =
  3631. module
  3632. ->globals[global_idx - module->import_global_count]
  3633. .ref_type;
  3634. }
  3635. if (!wasm_reftype_is_subtype_of(
  3636. global_type, global_ref_type, global->type,
  3637. global->ref_type, module->types, module->type_count)) {
  3638. set_error_buf(error_buf, error_buf_size, "type mismatch");
  3639. return false;
  3640. }
  3641. }
  3642. if (need_ref_type_map) {
  3643. if (!(global->ref_type =
  3644. reftype_set_insert(module->ref_type_set, &ref_type,
  3645. error_buf, error_buf_size))) {
  3646. return false;
  3647. }
  3648. }
  3649. #if TRACE_WASM_LOADER != 0
  3650. os_printf("global type: ");
  3651. wasm_dump_value_type(global->type, global->ref_type);
  3652. os_printf("\n");
  3653. #endif
  3654. #endif
  3655. module->global_count++;
  3656. }
  3657. bh_assert(module->global_count == global_count);
  3658. }
  3659. if (p != p_end) {
  3660. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3661. return false;
  3662. }
  3663. LOG_VERBOSE("Load global section success.\n");
  3664. return true;
  3665. fail:
  3666. return false;
  3667. }
  3668. static bool
  3669. load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  3670. bool is_load_from_file_buf, char *error_buf,
  3671. uint32 error_buf_size)
  3672. {
  3673. const uint8 *p = buf, *p_end = buf_end;
  3674. uint32 export_count, i, j, index;
  3675. uint64 total_size;
  3676. uint32 str_len;
  3677. WASMExport *export;
  3678. const char *name;
  3679. read_leb_uint32(p, p_end, export_count);
  3680. if (export_count) {
  3681. module->export_count = export_count;
  3682. total_size = sizeof(WASMExport) * (uint64)export_count;
  3683. if (!(module->exports =
  3684. loader_malloc(total_size, error_buf, error_buf_size))) {
  3685. return false;
  3686. }
  3687. export = module->exports;
  3688. for (i = 0; i < export_count; i++, export ++) {
  3689. #if WASM_ENABLE_THREAD_MGR == 0
  3690. if (p == p_end) {
  3691. /* export section with inconsistent count:
  3692. n export declared, but less than n given */
  3693. set_error_buf(error_buf, error_buf_size,
  3694. "length out of bounds");
  3695. return false;
  3696. }
  3697. #endif
  3698. read_leb_uint32(p, p_end, str_len);
  3699. CHECK_BUF(p, p_end, str_len);
  3700. for (j = 0; j < i; j++) {
  3701. name = module->exports[j].name;
  3702. if (strlen(name) == str_len && memcmp(name, p, str_len) == 0) {
  3703. set_error_buf(error_buf, error_buf_size,
  3704. "duplicate export name");
  3705. return false;
  3706. }
  3707. }
  3708. if (!(export->name = wasm_const_str_list_insert(
  3709. p, str_len, module, is_load_from_file_buf, error_buf,
  3710. error_buf_size))) {
  3711. return false;
  3712. }
  3713. p += str_len;
  3714. CHECK_BUF(p, p_end, 1);
  3715. export->kind = read_uint8(p);
  3716. read_leb_uint32(p, p_end, index);
  3717. export->index = index;
  3718. switch (export->kind) {
  3719. /* function index */
  3720. case EXPORT_KIND_FUNC:
  3721. if (index >= module->function_count
  3722. + module->import_function_count) {
  3723. set_error_buf(error_buf, error_buf_size,
  3724. "unknown function");
  3725. return false;
  3726. }
  3727. #if WASM_ENABLE_SIMD != 0
  3728. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  3729. /* TODO: check func type, if it has v128 param or result,
  3730. report error */
  3731. #endif
  3732. #endif
  3733. break;
  3734. /* table index */
  3735. case EXPORT_KIND_TABLE:
  3736. if (index
  3737. >= module->table_count + module->import_table_count) {
  3738. set_error_buf(error_buf, error_buf_size,
  3739. "unknown table");
  3740. return false;
  3741. }
  3742. break;
  3743. /* memory index */
  3744. case EXPORT_KIND_MEMORY:
  3745. if (index
  3746. >= module->memory_count + module->import_memory_count) {
  3747. set_error_buf(error_buf, error_buf_size,
  3748. "unknown memory");
  3749. return false;
  3750. }
  3751. break;
  3752. #if WASM_ENABLE_TAGS != 0
  3753. /* export tag */
  3754. case EXPORT_KIND_TAG:
  3755. if (index >= module->tag_count + module->import_tag_count) {
  3756. set_error_buf(error_buf, error_buf_size, "unknown tag");
  3757. return false;
  3758. }
  3759. break;
  3760. #endif
  3761. /* global index */
  3762. case EXPORT_KIND_GLOBAL:
  3763. if (index
  3764. >= module->global_count + module->import_global_count) {
  3765. set_error_buf(error_buf, error_buf_size,
  3766. "unknown global");
  3767. return false;
  3768. }
  3769. break;
  3770. default:
  3771. set_error_buf(error_buf, error_buf_size,
  3772. "invalid export kind");
  3773. return false;
  3774. }
  3775. }
  3776. }
  3777. if (p != p_end) {
  3778. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  3779. return false;
  3780. }
  3781. LOG_VERBOSE("Load export section success.\n");
  3782. return true;
  3783. fail:
  3784. return false;
  3785. }
  3786. static bool
  3787. check_table_index(const WASMModule *module, uint32 table_index, char *error_buf,
  3788. uint32 error_buf_size)
  3789. {
  3790. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  3791. if (table_index != 0) {
  3792. set_error_buf(error_buf, error_buf_size, "zero byte expected");
  3793. return false;
  3794. }
  3795. #endif
  3796. if (table_index >= module->import_table_count + module->table_count) {
  3797. set_error_buf_v(error_buf, error_buf_size, "unknown table %d",
  3798. table_index);
  3799. return false;
  3800. }
  3801. return true;
  3802. }
  3803. static bool
  3804. load_table_index(const uint8 **p_buf, const uint8 *buf_end, WASMModule *module,
  3805. uint32 *p_table_index, char *error_buf, uint32 error_buf_size)
  3806. {
  3807. const uint8 *p = *p_buf, *p_end = buf_end;
  3808. uint32 table_index;
  3809. read_leb_uint32(p, p_end, table_index);
  3810. if (!check_table_index(module, table_index, error_buf, error_buf_size)) {
  3811. return false;
  3812. }
  3813. *p_table_index = table_index;
  3814. *p_buf = p;
  3815. return true;
  3816. fail:
  3817. return false;
  3818. }
  3819. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3820. static bool
  3821. load_elem_type(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
  3822. uint32 *p_elem_type,
  3823. #if WASM_ENABLE_GC != 0
  3824. WASMRefType **p_elem_ref_type,
  3825. #endif
  3826. bool elemkind_zero, char *error_buf, uint32 error_buf_size)
  3827. {
  3828. const uint8 *p = *p_buf, *p_end = buf_end;
  3829. uint8 elem_type;
  3830. #if WASM_ENABLE_GC != 0
  3831. WASMRefType elem_ref_type;
  3832. bool need_ref_type_map;
  3833. #endif
  3834. CHECK_BUF(p, p_end, 1);
  3835. elem_type = read_uint8(p);
  3836. if (elemkind_zero) {
  3837. if (elem_type != 0) {
  3838. set_error_buf(error_buf, error_buf_size,
  3839. "invalid reference type or unknown type");
  3840. return false;
  3841. }
  3842. else {
  3843. *p_elem_type = VALUE_TYPE_FUNCREF;
  3844. *p_buf = p;
  3845. return true;
  3846. }
  3847. }
  3848. #if WASM_ENABLE_GC == 0
  3849. if (elem_type != VALUE_TYPE_FUNCREF && elem_type != VALUE_TYPE_EXTERNREF) {
  3850. set_error_buf(error_buf, error_buf_size,
  3851. "invalid reference type or unknown type");
  3852. return false;
  3853. }
  3854. *p_elem_type = elem_type;
  3855. #else
  3856. p--;
  3857. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  3858. module->type_count, &need_ref_type_map,
  3859. &elem_ref_type, false, error_buf, error_buf_size)) {
  3860. return false;
  3861. }
  3862. if (!wasm_is_type_reftype(elem_ref_type.ref_type)) {
  3863. set_error_buf(error_buf, error_buf_size,
  3864. "invalid reference type or unknown type");
  3865. return false;
  3866. }
  3867. *p_elem_type = elem_ref_type.ref_type;
  3868. if (need_ref_type_map) {
  3869. if (!(*p_elem_ref_type =
  3870. reftype_set_insert(module->ref_type_set, &elem_ref_type,
  3871. error_buf, error_buf_size))) {
  3872. return false;
  3873. }
  3874. }
  3875. #endif
  3876. *p_buf = p;
  3877. return true;
  3878. fail:
  3879. return false;
  3880. }
  3881. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  3882. static bool
  3883. load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
  3884. WASMModule *module, WASMTableSeg *table_segment,
  3885. char *error_buf, uint32 error_buf_size)
  3886. {
  3887. const uint8 *p = *p_buf, *p_end = buf_end;
  3888. uint32 function_count, function_index = 0, i;
  3889. uint64 total_size;
  3890. read_leb_uint32(p, p_end, function_count);
  3891. table_segment->value_count = function_count;
  3892. total_size = sizeof(InitializerExpression) * (uint64)function_count;
  3893. if (total_size > 0
  3894. && !(table_segment->init_values =
  3895. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3896. error_buf_size))) {
  3897. return false;
  3898. }
  3899. for (i = 0; i < function_count; i++) {
  3900. InitializerExpression *init_expr = &table_segment->init_values[i];
  3901. read_leb_uint32(p, p_end, function_index);
  3902. if (!check_function_index(module, function_index, error_buf,
  3903. error_buf_size)) {
  3904. return false;
  3905. }
  3906. init_expr->init_expr_type = INIT_EXPR_TYPE_FUNCREF_CONST;
  3907. init_expr->u.ref_index = function_index;
  3908. }
  3909. *p_buf = p;
  3910. return true;
  3911. fail:
  3912. return false;
  3913. }
  3914. #if (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0)
  3915. static bool
  3916. load_init_expr_vec(const uint8 **p_buf, const uint8 *buf_end,
  3917. WASMModule *module, WASMTableSeg *table_segment,
  3918. char *error_buf, uint32 error_buf_size)
  3919. {
  3920. const uint8 *p = *p_buf, *p_end = buf_end;
  3921. uint32 ref_count, i;
  3922. uint64 total_size;
  3923. read_leb_uint32(p, p_end, ref_count);
  3924. table_segment->value_count = ref_count;
  3925. total_size = sizeof(InitializerExpression) * (uint64)ref_count;
  3926. if (total_size > 0
  3927. && !(table_segment->init_values =
  3928. (InitializerExpression *)loader_malloc(total_size, error_buf,
  3929. error_buf_size))) {
  3930. return false;
  3931. }
  3932. for (i = 0; i < ref_count; i++) {
  3933. InitializerExpression *init_expr = &table_segment->init_values[i];
  3934. if (!load_init_expr(module, &p, p_end, init_expr,
  3935. table_segment->elem_type,
  3936. #if WASM_ENABLE_GC == 0
  3937. NULL,
  3938. #else
  3939. table_segment->elem_ref_type,
  3940. #endif
  3941. error_buf, error_buf_size))
  3942. return false;
  3943. bh_assert((init_expr->init_expr_type == INIT_EXPR_TYPE_GET_GLOBAL)
  3944. || (init_expr->init_expr_type == INIT_EXPR_TYPE_REFNULL_CONST)
  3945. || (init_expr->init_expr_type >= INIT_EXPR_TYPE_FUNCREF_CONST
  3946. && init_expr->init_expr_type
  3947. <= INIT_EXPR_TYPE_ARRAY_NEW_FIXED));
  3948. }
  3949. *p_buf = p;
  3950. return true;
  3951. fail:
  3952. return false;
  3953. }
  3954. #endif /* end of (WASM_ENABLE_GC != 0) || (WASM_ENABLE_REF_TYPES != 0) */
  3955. static bool
  3956. load_table_segment_section(const uint8 *buf, const uint8 *buf_end,
  3957. WASMModule *module, char *error_buf,
  3958. uint32 error_buf_size)
  3959. {
  3960. const uint8 *p = buf, *p_end = buf_end;
  3961. uint32 table_segment_count, i;
  3962. uint64 total_size;
  3963. WASMTableSeg *table_segment;
  3964. read_leb_uint32(p, p_end, table_segment_count);
  3965. if (table_segment_count) {
  3966. module->table_seg_count = table_segment_count;
  3967. total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
  3968. if (!(module->table_segments =
  3969. loader_malloc(total_size, error_buf, error_buf_size))) {
  3970. return false;
  3971. }
  3972. table_segment = module->table_segments;
  3973. for (i = 0; i < table_segment_count; i++, table_segment++) {
  3974. if (p >= p_end) {
  3975. set_error_buf(error_buf, error_buf_size,
  3976. "invalid value type or "
  3977. "invalid elements segment kind");
  3978. return false;
  3979. }
  3980. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  3981. read_leb_uint32(p, p_end, table_segment->mode);
  3982. /* last three bits */
  3983. table_segment->mode = table_segment->mode & 0x07;
  3984. switch (table_segment->mode) {
  3985. /* elemkind/elemtype + active */
  3986. case 0:
  3987. case 4:
  3988. {
  3989. #if WASM_ENABLE_GC != 0
  3990. if (table_segment->mode == 0) {
  3991. /* vec(funcidx), set elem type to (ref func) */
  3992. WASMRefType elem_ref_type = { 0 };
  3993. table_segment->elem_type = REF_TYPE_HT_NON_NULLABLE;
  3994. wasm_set_refheaptype_common(
  3995. &elem_ref_type.ref_ht_common, false,
  3996. HEAP_TYPE_FUNC);
  3997. if (!(table_segment->elem_ref_type = reftype_set_insert(
  3998. module->ref_type_set, &elem_ref_type,
  3999. error_buf, error_buf_size)))
  4000. return false;
  4001. }
  4002. else {
  4003. /* vec(expr), set elem type to funcref */
  4004. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4005. }
  4006. #else
  4007. table_segment->elem_type = VALUE_TYPE_FUNCREF;
  4008. #endif
  4009. table_segment->table_index = 0;
  4010. if (!check_table_index(module, table_segment->table_index,
  4011. error_buf, error_buf_size))
  4012. return false;
  4013. if (!load_init_expr(
  4014. module, &p, p_end, &table_segment->base_offset,
  4015. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4016. return false;
  4017. if (table_segment->mode == 0) {
  4018. /* vec(funcidx) */
  4019. if (!load_func_index_vec(&p, p_end, module,
  4020. table_segment, error_buf,
  4021. error_buf_size))
  4022. return false;
  4023. }
  4024. else {
  4025. /* vec(expr) */
  4026. if (!load_init_expr_vec(&p, p_end, module,
  4027. table_segment, error_buf,
  4028. error_buf_size))
  4029. return false;
  4030. }
  4031. break;
  4032. }
  4033. /* elemkind + passive/declarative */
  4034. case 1:
  4035. case 3:
  4036. if (!load_elem_type(module, &p, p_end,
  4037. &table_segment->elem_type,
  4038. #if WASM_ENABLE_GC != 0
  4039. &table_segment->elem_ref_type,
  4040. #endif
  4041. true, error_buf, error_buf_size))
  4042. return false;
  4043. /* vec(funcidx) */
  4044. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4045. error_buf, error_buf_size))
  4046. return false;
  4047. break;
  4048. /* elemkind/elemtype + table_idx + active */
  4049. case 2:
  4050. case 6:
  4051. if (!load_table_index(&p, p_end, module,
  4052. &table_segment->table_index,
  4053. error_buf, error_buf_size))
  4054. return false;
  4055. if (!load_init_expr(
  4056. module, &p, p_end, &table_segment->base_offset,
  4057. VALUE_TYPE_I32, NULL, error_buf, error_buf_size))
  4058. return false;
  4059. if (!load_elem_type(module, &p, p_end,
  4060. &table_segment->elem_type,
  4061. #if WASM_ENABLE_GC != 0
  4062. &table_segment->elem_ref_type,
  4063. #endif
  4064. table_segment->mode == 2 ? true : false,
  4065. error_buf, error_buf_size))
  4066. return false;
  4067. if (table_segment->mode == 2) {
  4068. /* vec(funcidx) */
  4069. if (!load_func_index_vec(&p, p_end, module,
  4070. table_segment, error_buf,
  4071. error_buf_size))
  4072. return false;
  4073. }
  4074. else {
  4075. /* vec(expr) */
  4076. if (!load_init_expr_vec(&p, p_end, module,
  4077. table_segment, error_buf,
  4078. error_buf_size))
  4079. return false;
  4080. }
  4081. break;
  4082. case 5:
  4083. case 7:
  4084. if (!load_elem_type(module, &p, p_end,
  4085. &table_segment->elem_type,
  4086. #if WASM_ENABLE_GC != 0
  4087. &table_segment->elem_ref_type,
  4088. #endif
  4089. false, error_buf, error_buf_size))
  4090. return false;
  4091. /* vec(expr) */
  4092. if (!load_init_expr_vec(&p, p_end, module, table_segment,
  4093. error_buf, error_buf_size))
  4094. return false;
  4095. break;
  4096. default:
  4097. set_error_buf(error_buf, error_buf_size,
  4098. "unknown element segment kind");
  4099. return false;
  4100. }
  4101. #else /* else of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4102. /*
  4103. * like: 00 41 05 0b 04 00 01 00 01
  4104. * for: (elem 0 (offset (i32.const 5)) $f1 $f2 $f1 $f2)
  4105. */
  4106. if (!load_table_index(&p, p_end, module,
  4107. &table_segment->table_index, error_buf,
  4108. error_buf_size))
  4109. return false;
  4110. if (!load_init_expr(module, &p, p_end, &table_segment->base_offset,
  4111. VALUE_TYPE_I32, NULL, error_buf,
  4112. error_buf_size))
  4113. return false;
  4114. if (!load_func_index_vec(&p, p_end, module, table_segment,
  4115. error_buf, error_buf_size))
  4116. return false;
  4117. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  4118. #if WASM_ENABLE_WAMR_COMPILER != 0
  4119. if (table_segment->elem_type == VALUE_TYPE_EXTERNREF)
  4120. module->is_ref_types_used = true;
  4121. #endif
  4122. }
  4123. }
  4124. if (p != p_end) {
  4125. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4126. return false;
  4127. }
  4128. LOG_VERBOSE("Load table segment section success.\n");
  4129. return true;
  4130. fail:
  4131. return false;
  4132. }
  4133. static bool
  4134. load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
  4135. WASMModule *module, char *error_buf,
  4136. uint32 error_buf_size)
  4137. {
  4138. const uint8 *p = buf, *p_end = buf_end;
  4139. uint32 data_seg_count, i, mem_index, data_seg_len;
  4140. uint64 total_size;
  4141. WASMDataSeg *dataseg;
  4142. InitializerExpression init_expr;
  4143. #if WASM_ENABLE_BULK_MEMORY != 0
  4144. bool is_passive = false;
  4145. uint32 mem_flag;
  4146. #endif
  4147. uint8 mem_offset_type = VALUE_TYPE_I32;
  4148. read_leb_uint32(p, p_end, data_seg_count);
  4149. #if WASM_ENABLE_BULK_MEMORY != 0
  4150. if ((module->data_seg_count1 != 0)
  4151. && (data_seg_count != module->data_seg_count1)) {
  4152. set_error_buf(error_buf, error_buf_size,
  4153. "data count and data section have inconsistent lengths");
  4154. return false;
  4155. }
  4156. #endif
  4157. if (data_seg_count) {
  4158. module->data_seg_count = data_seg_count;
  4159. total_size = sizeof(WASMDataSeg *) * (uint64)data_seg_count;
  4160. if (!(module->data_segments =
  4161. loader_malloc(total_size, error_buf, error_buf_size))) {
  4162. return false;
  4163. }
  4164. for (i = 0; i < data_seg_count; i++) {
  4165. read_leb_uint32(p, p_end, mem_index);
  4166. #if WASM_ENABLE_BULK_MEMORY != 0
  4167. is_passive = false;
  4168. mem_flag = mem_index & 0x03;
  4169. switch (mem_flag) {
  4170. case 0x01:
  4171. is_passive = true;
  4172. #if WASM_ENABLE_WAMR_COMPILER != 0
  4173. module->is_bulk_memory_used = true;
  4174. #endif
  4175. break;
  4176. case 0x00:
  4177. /* no memory index, treat index as 0 */
  4178. mem_index = 0;
  4179. goto check_mem_index;
  4180. case 0x02:
  4181. /* read following memory index */
  4182. read_leb_uint32(p, p_end, mem_index);
  4183. #if WASM_ENABLE_WAMR_COMPILER != 0
  4184. module->is_bulk_memory_used = true;
  4185. #endif
  4186. check_mem_index:
  4187. if (mem_index
  4188. >= module->import_memory_count + module->memory_count) {
  4189. set_error_buf_v(error_buf, error_buf_size,
  4190. "unknown memory %d", mem_index);
  4191. return false;
  4192. }
  4193. break;
  4194. case 0x03:
  4195. default:
  4196. set_error_buf(error_buf, error_buf_size, "unknown memory");
  4197. return false;
  4198. break;
  4199. }
  4200. #else
  4201. if (mem_index
  4202. >= module->import_memory_count + module->memory_count) {
  4203. set_error_buf_v(error_buf, error_buf_size, "unknown memory %d",
  4204. mem_index);
  4205. return false;
  4206. }
  4207. #endif /* WASM_ENABLE_BULK_MEMORY */
  4208. #if WASM_ENABLE_BULK_MEMORY != 0
  4209. if (!is_passive)
  4210. #endif
  4211. {
  4212. #if WASM_ENABLE_MEMORY64 != 0
  4213. /* This memory_flag is from memory instead of data segment */
  4214. uint8 memory_flag;
  4215. if (module->import_memory_count > 0) {
  4216. memory_flag =
  4217. module->import_memories[mem_index].u.memory.flags;
  4218. }
  4219. else {
  4220. memory_flag =
  4221. module
  4222. ->memories[mem_index - module->import_memory_count]
  4223. .flags;
  4224. }
  4225. mem_offset_type = memory_flag & MEMORY64_FLAG ? VALUE_TYPE_I64
  4226. : VALUE_TYPE_I32;
  4227. #else
  4228. mem_offset_type = VALUE_TYPE_I32;
  4229. #endif
  4230. }
  4231. #if WASM_ENABLE_BULK_MEMORY != 0
  4232. if (!is_passive)
  4233. #endif
  4234. if (!load_init_expr(module, &p, p_end, &init_expr,
  4235. mem_offset_type, NULL, error_buf,
  4236. error_buf_size))
  4237. return false;
  4238. read_leb_uint32(p, p_end, data_seg_len);
  4239. if (!(dataseg = module->data_segments[i] = loader_malloc(
  4240. sizeof(WASMDataSeg), error_buf, error_buf_size))) {
  4241. return false;
  4242. }
  4243. #if WASM_ENABLE_BULK_MEMORY != 0
  4244. dataseg->is_passive = is_passive;
  4245. if (!is_passive)
  4246. #endif
  4247. {
  4248. bh_memcpy_s(&dataseg->base_offset,
  4249. sizeof(InitializerExpression), &init_expr,
  4250. sizeof(InitializerExpression));
  4251. dataseg->memory_index = mem_index;
  4252. }
  4253. dataseg->data_length = data_seg_len;
  4254. CHECK_BUF(p, p_end, data_seg_len);
  4255. dataseg->data = (uint8 *)p;
  4256. p += data_seg_len;
  4257. }
  4258. }
  4259. if (p != p_end) {
  4260. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4261. return false;
  4262. }
  4263. LOG_VERBOSE("Load data segment section success.\n");
  4264. return true;
  4265. fail:
  4266. return false;
  4267. }
  4268. #if WASM_ENABLE_BULK_MEMORY != 0
  4269. static bool
  4270. load_datacount_section(const uint8 *buf, const uint8 *buf_end,
  4271. WASMModule *module, char *error_buf,
  4272. uint32 error_buf_size)
  4273. {
  4274. const uint8 *p = buf, *p_end = buf_end;
  4275. uint32 data_seg_count1 = 0;
  4276. read_leb_uint32(p, p_end, data_seg_count1);
  4277. module->data_seg_count1 = data_seg_count1;
  4278. if (p != p_end) {
  4279. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4280. return false;
  4281. }
  4282. #if WASM_ENABLE_WAMR_COMPILER != 0
  4283. module->is_bulk_memory_used = true;
  4284. #endif
  4285. LOG_VERBOSE("Load datacount section success.\n");
  4286. return true;
  4287. fail:
  4288. return false;
  4289. }
  4290. #endif
  4291. #if WASM_ENABLE_TAGS != 0
  4292. static bool
  4293. load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
  4294. const uint8 *buf_code_end, WASMModule *module, char *error_buf,
  4295. uint32 error_buf_size)
  4296. {
  4297. (void)buf_code;
  4298. (void)buf_code_end;
  4299. const uint8 *p = buf, *p_end = buf_end;
  4300. size_t total_size = 0;
  4301. /* number of tags defined in the section */
  4302. uint32 section_tag_count = 0;
  4303. uint8 tag_attribute;
  4304. uint32 tag_type;
  4305. WASMTag *tag = NULL;
  4306. /* get tag count */
  4307. read_leb_uint32(p, p_end, section_tag_count);
  4308. module->tag_count = section_tag_count;
  4309. if (section_tag_count) {
  4310. total_size = sizeof(WASMTag *) * module->tag_count;
  4311. if (!(module->tags =
  4312. loader_malloc(total_size, error_buf, error_buf_size))) {
  4313. return false;
  4314. }
  4315. /* load each tag, imported tags precede the tags */
  4316. uint32 tag_index;
  4317. for (tag_index = 0; tag_index < section_tag_count; tag_index++) {
  4318. /* get the one byte attribute */
  4319. CHECK_BUF(p, p_end, 1);
  4320. tag_attribute = read_uint8(p);
  4321. /* get type */
  4322. read_leb_uint32(p, p_end, tag_type);
  4323. /* compare against module->types */
  4324. if (tag_type >= module->type_count) {
  4325. set_error_buf(error_buf, error_buf_size, "unknown type");
  4326. return false;
  4327. }
  4328. /* get return type (must be 0) */
  4329. /* check, that the type of the referred tag returns void */
  4330. WASMType *func_type = (WASMType *)module->types[tag_type];
  4331. if (func_type->result_count != 0) {
  4332. set_error_buf(error_buf, error_buf_size,
  4333. "non-empty tag result type");
  4334. goto fail;
  4335. }
  4336. if (!(tag = module->tags[tag_index] = loader_malloc(
  4337. sizeof(WASMTag), error_buf, error_buf_size))) {
  4338. return false;
  4339. }
  4340. /* store to module tag declarations */
  4341. tag->attribute = tag_attribute;
  4342. tag->type = tag_type;
  4343. tag->tag_type = func_type;
  4344. }
  4345. }
  4346. if (p != p_end) {
  4347. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4348. return false;
  4349. }
  4350. LOG_VERBOSE("Load tag section success.\n");
  4351. return true;
  4352. fail:
  4353. return false;
  4354. }
  4355. #endif /* end of WASM_ENABLE_TAGS != 0 */
  4356. static bool
  4357. load_code_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_func,
  4358. const uint8 *buf_func_end, WASMModule *module,
  4359. char *error_buf, uint32 error_buf_size)
  4360. {
  4361. const uint8 *p = buf, *p_end = buf_end;
  4362. const uint8 *p_func = buf_func;
  4363. uint32 func_count = 0, code_count;
  4364. /* code has been loaded in function section, so pass it here, just check
  4365. * whether function and code section have inconsistent lengths */
  4366. read_leb_uint32(p, p_end, code_count);
  4367. if (buf_func)
  4368. read_leb_uint32(p_func, buf_func_end, func_count);
  4369. if (func_count != code_count) {
  4370. set_error_buf(error_buf, error_buf_size,
  4371. "function and code section have inconsistent lengths");
  4372. return false;
  4373. }
  4374. LOG_VERBOSE("Load code segment section success.\n");
  4375. (void)module;
  4376. return true;
  4377. fail:
  4378. return false;
  4379. }
  4380. static bool
  4381. load_start_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4382. char *error_buf, uint32 error_buf_size)
  4383. {
  4384. const uint8 *p = buf, *p_end = buf_end;
  4385. WASMFuncType *type;
  4386. uint32 start_function;
  4387. read_leb_uint32(p, p_end, start_function);
  4388. if (start_function
  4389. >= module->function_count + module->import_function_count) {
  4390. set_error_buf(error_buf, error_buf_size, "unknown function");
  4391. return false;
  4392. }
  4393. if (start_function < module->import_function_count)
  4394. type = module->import_functions[start_function].u.function.func_type;
  4395. else
  4396. type = module->functions[start_function - module->import_function_count]
  4397. ->func_type;
  4398. if (type->param_count != 0 || type->result_count != 0) {
  4399. set_error_buf(error_buf, error_buf_size, "invalid start function");
  4400. return false;
  4401. }
  4402. module->start_function = start_function;
  4403. if (p != p_end) {
  4404. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4405. return false;
  4406. }
  4407. LOG_VERBOSE("Load start section success.\n");
  4408. return true;
  4409. fail:
  4410. return false;
  4411. }
  4412. #if WASM_ENABLE_STRINGREF != 0
  4413. static bool
  4414. load_stringref_section(const uint8 *buf, const uint8 *buf_end,
  4415. WASMModule *module, bool is_load_from_file_buf,
  4416. char *error_buf, uint32 error_buf_size)
  4417. {
  4418. const uint8 *p = buf, *p_end = buf_end;
  4419. int32 deferred_count, immediate_count, string_length, i;
  4420. uint64 total_size;
  4421. read_leb_uint32(p, p_end, deferred_count);
  4422. read_leb_uint32(p, p_end, immediate_count);
  4423. /* proposal set deferred_count for future extension */
  4424. if (deferred_count != 0) {
  4425. goto fail;
  4426. }
  4427. if (immediate_count > 0) {
  4428. total_size = sizeof(char *) * (uint64)immediate_count;
  4429. if (!(module->string_literal_ptrs =
  4430. loader_malloc(total_size, error_buf, error_buf_size))) {
  4431. goto fail;
  4432. }
  4433. module->string_literal_count = immediate_count;
  4434. total_size = sizeof(uint32) * (uint64)immediate_count;
  4435. if (!(module->string_literal_lengths =
  4436. loader_malloc(total_size, error_buf, error_buf_size))) {
  4437. goto fail;
  4438. }
  4439. for (i = 0; i < immediate_count; i++) {
  4440. read_leb_uint32(p, p_end, string_length);
  4441. CHECK_BUF(p, p_end, string_length);
  4442. module->string_literal_ptrs[i] = p;
  4443. module->string_literal_lengths[i] = string_length;
  4444. p += string_length;
  4445. }
  4446. }
  4447. if (p != p_end) {
  4448. set_error_buf(error_buf, error_buf_size, "section size mismatch");
  4449. goto fail;
  4450. }
  4451. LOG_VERBOSE("Load stringref section success.\n");
  4452. return true;
  4453. fail:
  4454. return false;
  4455. }
  4456. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  4457. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4458. static bool
  4459. handle_name_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4460. bool is_load_from_file_buf, char *error_buf,
  4461. uint32 error_buf_size)
  4462. {
  4463. const uint8 *p = buf, *p_end = buf_end;
  4464. uint32 name_type, subsection_size;
  4465. uint32 previous_name_type = 0;
  4466. uint32 num_func_name;
  4467. uint32 func_index;
  4468. uint32 previous_func_index = ~0U;
  4469. uint32 func_name_len;
  4470. uint32 name_index;
  4471. int i = 0;
  4472. if (p >= p_end) {
  4473. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4474. return false;
  4475. }
  4476. while (p < p_end) {
  4477. read_leb_uint32(p, p_end, name_type);
  4478. if (i != 0) {
  4479. if (name_type == previous_name_type) {
  4480. set_error_buf(error_buf, error_buf_size,
  4481. "duplicate sub-section");
  4482. return false;
  4483. }
  4484. if (name_type < previous_name_type) {
  4485. set_error_buf(error_buf, error_buf_size,
  4486. "out-of-order sub-section");
  4487. return false;
  4488. }
  4489. }
  4490. previous_name_type = name_type;
  4491. read_leb_uint32(p, p_end, subsection_size);
  4492. CHECK_BUF(p, p_end, subsection_size);
  4493. switch (name_type) {
  4494. case SUB_SECTION_TYPE_FUNC:
  4495. if (subsection_size) {
  4496. read_leb_uint32(p, p_end, num_func_name);
  4497. for (name_index = 0; name_index < num_func_name;
  4498. name_index++) {
  4499. read_leb_uint32(p, p_end, func_index);
  4500. if (func_index == previous_func_index) {
  4501. set_error_buf(error_buf, error_buf_size,
  4502. "duplicate function name");
  4503. return false;
  4504. }
  4505. if (func_index < previous_func_index
  4506. && previous_func_index != ~0U) {
  4507. set_error_buf(error_buf, error_buf_size,
  4508. "out-of-order function index ");
  4509. return false;
  4510. }
  4511. previous_func_index = func_index;
  4512. read_leb_uint32(p, p_end, func_name_len);
  4513. CHECK_BUF(p, p_end, func_name_len);
  4514. /* Skip the import functions */
  4515. if (func_index >= module->import_function_count) {
  4516. func_index -= module->import_function_count;
  4517. if (func_index >= module->function_count) {
  4518. set_error_buf(error_buf, error_buf_size,
  4519. "out-of-range function index");
  4520. return false;
  4521. }
  4522. if (!(module->functions[func_index]->field_name =
  4523. wasm_const_str_list_insert(
  4524. p, func_name_len, module,
  4525. #if WASM_ENABLE_WAMR_COMPILER != 0
  4526. false,
  4527. #else
  4528. is_load_from_file_buf,
  4529. #endif
  4530. error_buf, error_buf_size))) {
  4531. return false;
  4532. }
  4533. }
  4534. p += func_name_len;
  4535. }
  4536. }
  4537. break;
  4538. case SUB_SECTION_TYPE_MODULE: /* TODO: Parse for module subsection
  4539. */
  4540. case SUB_SECTION_TYPE_LOCAL: /* TODO: Parse for local subsection */
  4541. default:
  4542. p = p + subsection_size;
  4543. break;
  4544. }
  4545. i++;
  4546. }
  4547. return true;
  4548. fail:
  4549. return false;
  4550. }
  4551. #endif
  4552. static bool
  4553. load_user_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
  4554. bool is_load_from_file_buf, char *error_buf,
  4555. uint32 error_buf_size)
  4556. {
  4557. const uint8 *p = buf, *p_end = buf_end;
  4558. char section_name[32];
  4559. uint32 name_len, buffer_len;
  4560. if (p >= p_end) {
  4561. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4562. return false;
  4563. }
  4564. read_leb_uint32(p, p_end, name_len);
  4565. if (p + name_len > p_end) {
  4566. set_error_buf(error_buf, error_buf_size, "unexpected end");
  4567. return false;
  4568. }
  4569. if (!wasm_check_utf8_str(p, name_len)) {
  4570. set_error_buf(error_buf, error_buf_size, "invalid UTF-8 encoding");
  4571. return false;
  4572. }
  4573. buffer_len = sizeof(section_name);
  4574. memset(section_name, 0, buffer_len);
  4575. if (name_len < buffer_len) {
  4576. bh_memcpy_s(section_name, buffer_len, p, name_len);
  4577. }
  4578. else {
  4579. bh_memcpy_s(section_name, buffer_len, p, buffer_len - 4);
  4580. memset(section_name + buffer_len - 4, '.', 3);
  4581. }
  4582. #if WASM_ENABLE_CUSTOM_NAME_SECTION != 0
  4583. if (name_len == 4 && memcmp(p, "name", 4) == 0) {
  4584. module->name_section_buf = buf;
  4585. module->name_section_buf_end = buf_end;
  4586. p += name_len;
  4587. handle_name_section(p, p_end, module, is_load_from_file_buf, error_buf,
  4588. error_buf_size);
  4589. LOG_VERBOSE("Load custom name section success.");
  4590. return true;
  4591. }
  4592. #endif
  4593. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  4594. {
  4595. WASMCustomSection *section =
  4596. loader_malloc(sizeof(WASMCustomSection), error_buf, error_buf_size);
  4597. if (!section) {
  4598. return false;
  4599. }
  4600. section->name_addr = (char *)p;
  4601. section->name_len = name_len;
  4602. section->content_addr = (uint8 *)(p + name_len);
  4603. section->content_len = (uint32)(p_end - p - name_len);
  4604. section->next = module->custom_section_list;
  4605. module->custom_section_list = section;
  4606. LOG_VERBOSE("Load custom section [%s] success.", section_name);
  4607. return true;
  4608. }
  4609. #endif
  4610. LOG_VERBOSE("Ignore custom section [%s].", section_name);
  4611. (void)is_load_from_file_buf;
  4612. (void)module;
  4613. return true;
  4614. fail:
  4615. return false;
  4616. }
  4617. static void
  4618. calculate_global_data_offset(WASMModule *module)
  4619. {
  4620. uint32 i, data_offset;
  4621. data_offset = 0;
  4622. for (i = 0; i < module->import_global_count; i++) {
  4623. WASMGlobalImport *import_global =
  4624. &((module->import_globals + i)->u.global);
  4625. #if WASM_ENABLE_FAST_JIT != 0
  4626. import_global->data_offset = data_offset;
  4627. #endif
  4628. data_offset += wasm_value_type_size(import_global->type);
  4629. }
  4630. for (i = 0; i < module->global_count; i++) {
  4631. WASMGlobal *global = module->globals + i;
  4632. #if WASM_ENABLE_FAST_JIT != 0
  4633. global->data_offset = data_offset;
  4634. #endif
  4635. data_offset += wasm_value_type_size(global->type);
  4636. }
  4637. module->global_data_size = data_offset;
  4638. }
  4639. #if WASM_ENABLE_FAST_JIT != 0
  4640. static bool
  4641. init_fast_jit_functions(WASMModule *module, char *error_buf,
  4642. uint32 error_buf_size)
  4643. {
  4644. #if WASM_ENABLE_LAZY_JIT != 0
  4645. JitGlobals *jit_globals = jit_compiler_get_jit_globals();
  4646. #endif
  4647. uint32 i;
  4648. if (!module->function_count)
  4649. return true;
  4650. if (!(module->fast_jit_func_ptrs =
  4651. loader_malloc(sizeof(void *) * module->function_count, error_buf,
  4652. error_buf_size))) {
  4653. return false;
  4654. }
  4655. #if WASM_ENABLE_LAZY_JIT != 0
  4656. for (i = 0; i < module->function_count; i++) {
  4657. module->fast_jit_func_ptrs[i] =
  4658. jit_globals->compile_fast_jit_and_then_call;
  4659. }
  4660. #endif
  4661. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  4662. if (os_mutex_init(&module->fast_jit_thread_locks[i]) != 0) {
  4663. set_error_buf(error_buf, error_buf_size,
  4664. "init fast jit thread lock failed");
  4665. return false;
  4666. }
  4667. module->fast_jit_thread_locks_inited[i] = true;
  4668. }
  4669. return true;
  4670. }
  4671. #endif /* end of WASM_ENABLE_FAST_JIT != 0 */
  4672. #if WASM_ENABLE_JIT != 0
  4673. static bool
  4674. init_llvm_jit_functions_stage1(WASMModule *module, char *error_buf,
  4675. uint32 error_buf_size)
  4676. {
  4677. LLVMJITOptions *llvm_jit_options = wasm_runtime_get_llvm_jit_options();
  4678. AOTCompOption option = { 0 };
  4679. char *aot_last_error;
  4680. uint64 size;
  4681. #if WASM_ENABLE_GC != 0
  4682. bool gc_enabled = true;
  4683. #else
  4684. bool gc_enabled = false;
  4685. #endif
  4686. if (module->function_count == 0)
  4687. return true;
  4688. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4689. if (os_mutex_init(&module->tierup_wait_lock) != 0) {
  4690. set_error_buf(error_buf, error_buf_size, "init jit tierup lock failed");
  4691. return false;
  4692. }
  4693. if (os_cond_init(&module->tierup_wait_cond) != 0) {
  4694. set_error_buf(error_buf, error_buf_size, "init jit tierup cond failed");
  4695. os_mutex_destroy(&module->tierup_wait_lock);
  4696. return false;
  4697. }
  4698. module->tierup_wait_lock_inited = true;
  4699. #endif
  4700. size = sizeof(void *) * (uint64)module->function_count
  4701. + sizeof(bool) * (uint64)module->function_count;
  4702. if (!(module->func_ptrs = loader_malloc(size, error_buf, error_buf_size))) {
  4703. return false;
  4704. }
  4705. module->func_ptrs_compiled =
  4706. (bool *)((uint8 *)module->func_ptrs
  4707. + sizeof(void *) * module->function_count);
  4708. module->comp_data = aot_create_comp_data(module, NULL, gc_enabled);
  4709. if (!module->comp_data) {
  4710. aot_last_error = aot_get_last_error();
  4711. bh_assert(aot_last_error != NULL);
  4712. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4713. return false;
  4714. }
  4715. option.is_jit_mode = true;
  4716. option.opt_level = llvm_jit_options->opt_level;
  4717. option.size_level = llvm_jit_options->size_level;
  4718. option.segue_flags = llvm_jit_options->segue_flags;
  4719. option.quick_invoke_c_api_import =
  4720. llvm_jit_options->quick_invoke_c_api_import;
  4721. #if WASM_ENABLE_BULK_MEMORY != 0
  4722. option.enable_bulk_memory = true;
  4723. #endif
  4724. #if WASM_ENABLE_THREAD_MGR != 0
  4725. option.enable_thread_mgr = true;
  4726. #endif
  4727. #if WASM_ENABLE_TAIL_CALL != 0
  4728. option.enable_tail_call = true;
  4729. #endif
  4730. #if WASM_ENABLE_SIMD != 0
  4731. option.enable_simd = true;
  4732. #endif
  4733. #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
  4734. option.enable_ref_types = true;
  4735. #elif WASM_ENABLE_GC != 0
  4736. option.enable_gc = true;
  4737. #endif
  4738. option.enable_aux_stack_check = true;
  4739. #if WASM_ENABLE_PERF_PROFILING != 0 || WASM_ENABLE_DUMP_CALL_STACK != 0 \
  4740. || WASM_ENABLE_AOT_STACK_FRAME != 0
  4741. option.enable_aux_stack_frame = true;
  4742. #endif
  4743. #if WASM_ENABLE_PERF_PROFILING != 0
  4744. option.enable_perf_profiling = true;
  4745. #endif
  4746. #if WASM_ENABLE_MEMORY_PROFILING != 0
  4747. option.enable_memory_profiling = true;
  4748. option.enable_stack_estimation = true;
  4749. #endif
  4750. module->comp_ctx = aot_create_comp_context(module->comp_data, &option);
  4751. if (!module->comp_ctx) {
  4752. aot_last_error = aot_get_last_error();
  4753. bh_assert(aot_last_error != NULL);
  4754. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4755. return false;
  4756. }
  4757. return true;
  4758. }
  4759. static bool
  4760. init_llvm_jit_functions_stage2(WASMModule *module, char *error_buf,
  4761. uint32 error_buf_size)
  4762. {
  4763. char *aot_last_error;
  4764. uint32 i;
  4765. if (module->function_count == 0)
  4766. return true;
  4767. if (!aot_compile_wasm(module->comp_ctx)) {
  4768. aot_last_error = aot_get_last_error();
  4769. bh_assert(aot_last_error != NULL);
  4770. set_error_buf(error_buf, error_buf_size, aot_last_error);
  4771. return false;
  4772. }
  4773. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4774. if (module->orcjit_stop_compiling)
  4775. return false;
  4776. #endif
  4777. bh_print_time("Begin to lookup llvm jit functions");
  4778. for (i = 0; i < module->function_count; i++) {
  4779. LLVMOrcJITTargetAddress func_addr = 0;
  4780. LLVMErrorRef error;
  4781. char func_name[48];
  4782. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX, i);
  4783. error = LLVMOrcLLLazyJITLookup(module->comp_ctx->orc_jit, &func_addr,
  4784. func_name);
  4785. if (error != LLVMErrorSuccess) {
  4786. char *err_msg = LLVMGetErrorMessage(error);
  4787. set_error_buf_v(error_buf, error_buf_size,
  4788. "failed to compile llvm jit function: %s", err_msg);
  4789. LLVMDisposeErrorMessage(err_msg);
  4790. return false;
  4791. }
  4792. /**
  4793. * No need to lock the func_ptr[func_idx] here as it is basic
  4794. * data type, the load/store for it can be finished by one cpu
  4795. * instruction, and there can be only one cpu instruction
  4796. * loading/storing at the same time.
  4797. */
  4798. module->func_ptrs[i] = (void *)func_addr;
  4799. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4800. module->functions[i]->llvm_jit_func_ptr = (void *)func_addr;
  4801. if (module->orcjit_stop_compiling)
  4802. return false;
  4803. #endif
  4804. }
  4805. bh_print_time("End lookup llvm jit functions");
  4806. return true;
  4807. }
  4808. #endif /* end of WASM_ENABLE_JIT != 0 */
  4809. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4810. && WASM_ENABLE_LAZY_JIT != 0
  4811. static void *
  4812. init_llvm_jit_functions_stage2_callback(void *arg)
  4813. {
  4814. WASMModule *module = (WASMModule *)arg;
  4815. char error_buf[128];
  4816. uint32 error_buf_size = (uint32)sizeof(error_buf);
  4817. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  4818. module->orcjit_stop_compiling = true;
  4819. return NULL;
  4820. }
  4821. os_mutex_lock(&module->tierup_wait_lock);
  4822. module->llvm_jit_inited = true;
  4823. os_cond_broadcast(&module->tierup_wait_cond);
  4824. os_mutex_unlock(&module->tierup_wait_lock);
  4825. return NULL;
  4826. }
  4827. #endif
  4828. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  4829. /* The callback function to compile jit functions */
  4830. static void *
  4831. orcjit_thread_callback(void *arg)
  4832. {
  4833. OrcJitThreadArg *thread_arg = (OrcJitThreadArg *)arg;
  4834. #if WASM_ENABLE_JIT != 0
  4835. AOTCompContext *comp_ctx = thread_arg->comp_ctx;
  4836. #endif
  4837. WASMModule *module = thread_arg->module;
  4838. uint32 group_idx = thread_arg->group_idx;
  4839. uint32 group_stride = WASM_ORC_JIT_BACKEND_THREAD_NUM;
  4840. uint32 func_count = module->function_count;
  4841. uint32 i;
  4842. #if WASM_ENABLE_FAST_JIT != 0
  4843. /* Compile fast jit functions of this group */
  4844. for (i = group_idx; i < func_count; i += group_stride) {
  4845. if (!jit_compiler_compile(module, i + module->import_function_count)) {
  4846. LOG_ERROR("failed to compile fast jit function %u\n", i);
  4847. break;
  4848. }
  4849. if (module->orcjit_stop_compiling) {
  4850. return NULL;
  4851. }
  4852. }
  4853. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4854. os_mutex_lock(&module->tierup_wait_lock);
  4855. module->fast_jit_ready_groups++;
  4856. os_mutex_unlock(&module->tierup_wait_lock);
  4857. #endif
  4858. #endif
  4859. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  4860. && WASM_ENABLE_LAZY_JIT != 0
  4861. /* For JIT tier-up, set each llvm jit func to call_to_fast_jit */
  4862. for (i = group_idx; i < func_count;
  4863. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4864. uint32 j;
  4865. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4866. if (i + j * group_stride < func_count) {
  4867. if (!jit_compiler_set_call_to_fast_jit(
  4868. module,
  4869. i + j * group_stride + module->import_function_count)) {
  4870. LOG_ERROR(
  4871. "failed to compile call_to_fast_jit for func %u\n",
  4872. i + j * group_stride + module->import_function_count);
  4873. module->orcjit_stop_compiling = true;
  4874. return NULL;
  4875. }
  4876. }
  4877. if (module->orcjit_stop_compiling) {
  4878. return NULL;
  4879. }
  4880. }
  4881. }
  4882. /* Wait until init_llvm_jit_functions_stage2 finishes and all
  4883. fast jit functions are compiled */
  4884. os_mutex_lock(&module->tierup_wait_lock);
  4885. while (!(module->llvm_jit_inited && module->enable_llvm_jit_compilation
  4886. && module->fast_jit_ready_groups >= group_stride)) {
  4887. os_cond_reltimedwait(&module->tierup_wait_cond,
  4888. &module->tierup_wait_lock, 10000);
  4889. if (module->orcjit_stop_compiling) {
  4890. /* init_llvm_jit_functions_stage2 failed */
  4891. os_mutex_unlock(&module->tierup_wait_lock);
  4892. return NULL;
  4893. }
  4894. }
  4895. os_mutex_unlock(&module->tierup_wait_lock);
  4896. #endif
  4897. #if WASM_ENABLE_JIT != 0
  4898. /* Compile llvm jit functions of this group */
  4899. for (i = group_idx; i < func_count;
  4900. i += group_stride * WASM_ORC_JIT_COMPILE_THREAD_NUM) {
  4901. LLVMOrcJITTargetAddress func_addr = 0;
  4902. LLVMErrorRef error;
  4903. char func_name[48];
  4904. typedef void (*F)(void);
  4905. union {
  4906. F f;
  4907. void *v;
  4908. } u;
  4909. uint32 j;
  4910. snprintf(func_name, sizeof(func_name), "%s%d%s", AOT_FUNC_PREFIX, i,
  4911. "_wrapper");
  4912. LOG_DEBUG("compile llvm jit func %s", func_name);
  4913. error =
  4914. LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr, func_name);
  4915. if (error != LLVMErrorSuccess) {
  4916. char *err_msg = LLVMGetErrorMessage(error);
  4917. LOG_ERROR("failed to compile llvm jit function %u: %s", i, err_msg);
  4918. LLVMDisposeErrorMessage(err_msg);
  4919. break;
  4920. }
  4921. /* Call the jit wrapper function to trigger its compilation, so as
  4922. to compile the actual jit functions, since we add the latter to
  4923. function list in the PartitionFunction callback */
  4924. u.v = (void *)func_addr;
  4925. u.f();
  4926. for (j = 0; j < WASM_ORC_JIT_COMPILE_THREAD_NUM; j++) {
  4927. if (i + j * group_stride < func_count) {
  4928. module->func_ptrs_compiled[i + j * group_stride] = true;
  4929. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  4930. snprintf(func_name, sizeof(func_name), "%s%d", AOT_FUNC_PREFIX,
  4931. i + j * group_stride);
  4932. error = LLVMOrcLLLazyJITLookup(comp_ctx->orc_jit, &func_addr,
  4933. func_name);
  4934. if (error != LLVMErrorSuccess) {
  4935. char *err_msg = LLVMGetErrorMessage(error);
  4936. LOG_ERROR("failed to compile llvm jit function %u: %s", i,
  4937. err_msg);
  4938. LLVMDisposeErrorMessage(err_msg);
  4939. /* Ignore current llvm jit func, as its func ptr is
  4940. previous set to call_to_fast_jit, which also works */
  4941. continue;
  4942. }
  4943. jit_compiler_set_llvm_jit_func_ptr(
  4944. module,
  4945. i + j * group_stride + module->import_function_count,
  4946. (void *)func_addr);
  4947. /* Try to switch to call this llvm jit function instead of
  4948. fast jit function from fast jit jitted code */
  4949. jit_compiler_set_call_to_llvm_jit(
  4950. module,
  4951. i + j * group_stride + module->import_function_count);
  4952. #endif
  4953. }
  4954. }
  4955. if (module->orcjit_stop_compiling) {
  4956. break;
  4957. }
  4958. }
  4959. #endif
  4960. return NULL;
  4961. }
  4962. static void
  4963. orcjit_stop_compile_threads(WASMModule *module)
  4964. {
  4965. uint32 i, thread_num = (uint32)(sizeof(module->orcjit_thread_args)
  4966. / sizeof(OrcJitThreadArg));
  4967. module->orcjit_stop_compiling = true;
  4968. for (i = 0; i < thread_num; i++) {
  4969. if (module->orcjit_threads[i])
  4970. os_thread_join(module->orcjit_threads[i], NULL);
  4971. }
  4972. }
  4973. static bool
  4974. compile_jit_functions(WASMModule *module, char *error_buf,
  4975. uint32 error_buf_size)
  4976. {
  4977. uint32 thread_num =
  4978. (uint32)(sizeof(module->orcjit_thread_args) / sizeof(OrcJitThreadArg));
  4979. uint32 i, j;
  4980. bh_print_time("Begin to compile jit functions");
  4981. /* Create threads to compile the jit functions */
  4982. for (i = 0; i < thread_num && i < module->function_count; i++) {
  4983. #if WASM_ENABLE_JIT != 0
  4984. module->orcjit_thread_args[i].comp_ctx = module->comp_ctx;
  4985. #endif
  4986. module->orcjit_thread_args[i].module = module;
  4987. module->orcjit_thread_args[i].group_idx = i;
  4988. if (os_thread_create(&module->orcjit_threads[i], orcjit_thread_callback,
  4989. (void *)&module->orcjit_thread_args[i],
  4990. APP_THREAD_STACK_SIZE_DEFAULT)
  4991. != 0) {
  4992. set_error_buf(error_buf, error_buf_size,
  4993. "create orcjit compile thread failed");
  4994. /* Terminate the threads created */
  4995. module->orcjit_stop_compiling = true;
  4996. for (j = 0; j < i; j++) {
  4997. os_thread_join(module->orcjit_threads[j], NULL);
  4998. }
  4999. return false;
  5000. }
  5001. }
  5002. #if WASM_ENABLE_LAZY_JIT == 0
  5003. /* Wait until all jit functions are compiled for eager mode */
  5004. for (i = 0; i < thread_num; i++) {
  5005. if (module->orcjit_threads[i])
  5006. os_thread_join(module->orcjit_threads[i], NULL);
  5007. }
  5008. #if WASM_ENABLE_FAST_JIT != 0
  5009. /* Ensure all the fast-jit functions are compiled */
  5010. for (i = 0; i < module->function_count; i++) {
  5011. if (!jit_compiler_is_compiled(module,
  5012. i + module->import_function_count)) {
  5013. set_error_buf(error_buf, error_buf_size,
  5014. "failed to compile fast jit function");
  5015. return false;
  5016. }
  5017. }
  5018. #endif
  5019. #if WASM_ENABLE_JIT != 0
  5020. /* Ensure all the llvm-jit functions are compiled */
  5021. for (i = 0; i < module->function_count; i++) {
  5022. if (!module->func_ptrs_compiled[i]) {
  5023. set_error_buf(error_buf, error_buf_size,
  5024. "failed to compile llvm jit function");
  5025. return false;
  5026. }
  5027. }
  5028. #endif
  5029. #endif /* end of WASM_ENABLE_LAZY_JIT == 0 */
  5030. bh_print_time("End compile jit functions");
  5031. return true;
  5032. }
  5033. #endif /* end of WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 */
  5034. static bool
  5035. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  5036. uint32 cur_func_idx, char *error_buf,
  5037. uint32 error_buf_size);
  5038. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5039. void **
  5040. wasm_interp_get_handle_table();
  5041. static void **handle_table;
  5042. #endif
  5043. static bool
  5044. load_from_sections(WASMModule *module, WASMSection *sections,
  5045. bool is_load_from_file_buf, char *error_buf,
  5046. uint32 error_buf_size)
  5047. {
  5048. WASMExport *export;
  5049. WASMSection *section = sections;
  5050. const uint8 *buf, *buf_end, *buf_code = NULL, *buf_code_end = NULL,
  5051. *buf_func = NULL, *buf_func_end = NULL;
  5052. WASMGlobal *aux_data_end_global = NULL, *aux_heap_base_global = NULL;
  5053. WASMGlobal *aux_stack_top_global = NULL, *global;
  5054. uint64 aux_data_end = (uint64)-1LL, aux_heap_base = (uint64)-1LL,
  5055. aux_stack_top = (uint64)-1LL;
  5056. uint32 global_index, func_index, i;
  5057. uint32 aux_data_end_global_index = (uint32)-1;
  5058. uint32 aux_heap_base_global_index = (uint32)-1;
  5059. WASMFuncType *func_type;
  5060. uint8 malloc_free_io_type = VALUE_TYPE_I32;
  5061. /* Find code and function sections if have */
  5062. while (section) {
  5063. if (section->section_type == SECTION_TYPE_CODE) {
  5064. buf_code = section->section_body;
  5065. buf_code_end = buf_code + section->section_body_size;
  5066. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_DEBUG_AOT != 0
  5067. module->buf_code = (uint8 *)buf_code;
  5068. module->buf_code_size = section->section_body_size;
  5069. #endif
  5070. }
  5071. else if (section->section_type == SECTION_TYPE_FUNC) {
  5072. buf_func = section->section_body;
  5073. buf_func_end = buf_func + section->section_body_size;
  5074. }
  5075. section = section->next;
  5076. }
  5077. section = sections;
  5078. while (section) {
  5079. buf = section->section_body;
  5080. buf_end = buf + section->section_body_size;
  5081. switch (section->section_type) {
  5082. case SECTION_TYPE_USER:
  5083. /* unsupported user section, ignore it. */
  5084. if (!load_user_section(buf, buf_end, module,
  5085. is_load_from_file_buf, error_buf,
  5086. error_buf_size))
  5087. return false;
  5088. break;
  5089. case SECTION_TYPE_TYPE:
  5090. if (!load_type_section(buf, buf_end, module, error_buf,
  5091. error_buf_size))
  5092. return false;
  5093. break;
  5094. case SECTION_TYPE_IMPORT:
  5095. if (!load_import_section(buf, buf_end, module,
  5096. is_load_from_file_buf, error_buf,
  5097. error_buf_size))
  5098. return false;
  5099. break;
  5100. case SECTION_TYPE_FUNC:
  5101. if (!load_function_section(buf, buf_end, buf_code, buf_code_end,
  5102. module, error_buf, error_buf_size))
  5103. return false;
  5104. break;
  5105. case SECTION_TYPE_TABLE:
  5106. if (!load_table_section(buf, buf_end, module, error_buf,
  5107. error_buf_size))
  5108. return false;
  5109. break;
  5110. case SECTION_TYPE_MEMORY:
  5111. if (!load_memory_section(buf, buf_end, module, error_buf,
  5112. error_buf_size))
  5113. return false;
  5114. break;
  5115. #if WASM_ENABLE_TAGS != 0
  5116. case SECTION_TYPE_TAG:
  5117. /* load tag declaration section */
  5118. if (!load_tag_section(buf, buf_end, buf_code, buf_code_end,
  5119. module, error_buf, error_buf_size))
  5120. return false;
  5121. break;
  5122. #endif
  5123. case SECTION_TYPE_GLOBAL:
  5124. if (!load_global_section(buf, buf_end, module, error_buf,
  5125. error_buf_size))
  5126. return false;
  5127. break;
  5128. case SECTION_TYPE_EXPORT:
  5129. if (!load_export_section(buf, buf_end, module,
  5130. is_load_from_file_buf, error_buf,
  5131. error_buf_size))
  5132. return false;
  5133. break;
  5134. case SECTION_TYPE_START:
  5135. if (!load_start_section(buf, buf_end, module, error_buf,
  5136. error_buf_size))
  5137. return false;
  5138. break;
  5139. case SECTION_TYPE_ELEM:
  5140. if (!load_table_segment_section(buf, buf_end, module, error_buf,
  5141. error_buf_size))
  5142. return false;
  5143. break;
  5144. case SECTION_TYPE_CODE:
  5145. if (!load_code_section(buf, buf_end, buf_func, buf_func_end,
  5146. module, error_buf, error_buf_size))
  5147. return false;
  5148. break;
  5149. case SECTION_TYPE_DATA:
  5150. if (!load_data_segment_section(buf, buf_end, module, error_buf,
  5151. error_buf_size))
  5152. return false;
  5153. break;
  5154. #if WASM_ENABLE_BULK_MEMORY != 0
  5155. case SECTION_TYPE_DATACOUNT:
  5156. if (!load_datacount_section(buf, buf_end, module, error_buf,
  5157. error_buf_size))
  5158. return false;
  5159. break;
  5160. #endif
  5161. #if WASM_ENABLE_STRINGREF != 0
  5162. case SECTION_TYPE_STRINGREF:
  5163. if (!load_stringref_section(buf, buf_end, module,
  5164. is_load_from_file_buf, error_buf,
  5165. error_buf_size))
  5166. return false;
  5167. break;
  5168. #endif
  5169. default:
  5170. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5171. return false;
  5172. }
  5173. section = section->next;
  5174. }
  5175. module->aux_data_end_global_index = (uint32)-1;
  5176. module->aux_heap_base_global_index = (uint32)-1;
  5177. module->aux_stack_top_global_index = (uint32)-1;
  5178. /* Resolve auxiliary data/stack/heap info and reset memory info */
  5179. export = module->exports;
  5180. for (i = 0; i < module->export_count; i++, export ++) {
  5181. if (export->kind == EXPORT_KIND_GLOBAL) {
  5182. if (!strcmp(export->name, "__heap_base")) {
  5183. global_index = export->index - module->import_global_count;
  5184. global = module->globals + global_index;
  5185. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5186. && global->init_expr.init_expr_type
  5187. == INIT_EXPR_TYPE_I32_CONST) {
  5188. aux_heap_base_global = global;
  5189. aux_heap_base = (uint64)(uint32)global->init_expr.u.i32;
  5190. aux_heap_base_global_index = export->index;
  5191. LOG_VERBOSE("Found aux __heap_base global, value: %" PRIu64,
  5192. aux_heap_base);
  5193. }
  5194. }
  5195. else if (!strcmp(export->name, "__data_end")) {
  5196. global_index = export->index - module->import_global_count;
  5197. global = module->globals + global_index;
  5198. if (global->type == VALUE_TYPE_I32 && !global->is_mutable
  5199. && global->init_expr.init_expr_type
  5200. == INIT_EXPR_TYPE_I32_CONST) {
  5201. aux_data_end_global = global;
  5202. aux_data_end = (uint64)(uint32)global->init_expr.u.i32;
  5203. aux_data_end_global_index = export->index;
  5204. LOG_VERBOSE("Found aux __data_end global, value: %" PRIu64,
  5205. aux_data_end);
  5206. aux_data_end = align_uint64(aux_data_end, 16);
  5207. }
  5208. }
  5209. /* For module compiled with -pthread option, the global is:
  5210. [0] stack_top <-- 0
  5211. [1] tls_pointer
  5212. [2] tls_size
  5213. [3] data_end <-- 3
  5214. [4] global_base
  5215. [5] heap_base <-- 5
  5216. [6] dso_handle
  5217. For module compiled without -pthread option:
  5218. [0] stack_top <-- 0
  5219. [1] data_end <-- 1
  5220. [2] global_base
  5221. [3] heap_base <-- 3
  5222. [4] dso_handle
  5223. */
  5224. if (aux_data_end_global && aux_heap_base_global
  5225. && aux_data_end <= aux_heap_base) {
  5226. module->aux_data_end_global_index = aux_data_end_global_index;
  5227. module->aux_data_end = aux_data_end;
  5228. module->aux_heap_base_global_index = aux_heap_base_global_index;
  5229. module->aux_heap_base = aux_heap_base;
  5230. /* Resolve aux stack top global */
  5231. for (global_index = 0; global_index < module->global_count;
  5232. global_index++) {
  5233. global = module->globals + global_index;
  5234. if (global->is_mutable /* heap_base and data_end is
  5235. not mutable */
  5236. && global->type == VALUE_TYPE_I32
  5237. && global->init_expr.init_expr_type
  5238. == INIT_EXPR_TYPE_I32_CONST
  5239. && (uint64)(uint32)global->init_expr.u.i32
  5240. <= aux_heap_base) {
  5241. aux_stack_top_global = global;
  5242. aux_stack_top = (uint64)(uint32)global->init_expr.u.i32;
  5243. module->aux_stack_top_global_index =
  5244. module->import_global_count + global_index;
  5245. module->aux_stack_bottom = aux_stack_top;
  5246. module->aux_stack_size =
  5247. aux_stack_top > aux_data_end
  5248. ? (uint32)(aux_stack_top - aux_data_end)
  5249. : (uint32)aux_stack_top;
  5250. LOG_VERBOSE(
  5251. "Found aux stack top global, value: %" PRIu64 ", "
  5252. "global index: %d, stack size: %d",
  5253. aux_stack_top, global_index,
  5254. module->aux_stack_size);
  5255. break;
  5256. }
  5257. }
  5258. if (!aux_stack_top_global) {
  5259. /* Auxiliary stack global isn't found, it must be unused
  5260. in the wasm app, as if it is used, the global must be
  5261. defined. Here we set it to __heap_base global and set
  5262. its size to 0. */
  5263. aux_stack_top_global = aux_heap_base_global;
  5264. aux_stack_top = aux_heap_base;
  5265. module->aux_stack_top_global_index =
  5266. module->aux_heap_base_global_index;
  5267. module->aux_stack_bottom = aux_stack_top;
  5268. module->aux_stack_size = 0;
  5269. }
  5270. break;
  5271. }
  5272. }
  5273. }
  5274. module->malloc_function = (uint32)-1;
  5275. module->free_function = (uint32)-1;
  5276. module->retain_function = (uint32)-1;
  5277. /* Resolve malloc/free function exported by wasm module */
  5278. #if WASM_ENABLE_MEMORY64 != 0
  5279. if (has_module_memory64(module))
  5280. malloc_free_io_type = VALUE_TYPE_I64;
  5281. #endif
  5282. export = module->exports;
  5283. for (i = 0; i < module->export_count; i++, export ++) {
  5284. if (export->kind == EXPORT_KIND_FUNC) {
  5285. if (!strcmp(export->name, "malloc")
  5286. && export->index >= module->import_function_count) {
  5287. func_index = export->index - module->import_function_count;
  5288. func_type = module->functions[func_index]->func_type;
  5289. if (func_type->param_count == 1 && func_type->result_count == 1
  5290. && func_type->types[0] == malloc_free_io_type
  5291. && func_type->types[1] == malloc_free_io_type) {
  5292. bh_assert(module->malloc_function == (uint32)-1);
  5293. module->malloc_function = export->index;
  5294. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5295. export->name, export->index);
  5296. }
  5297. }
  5298. else if (!strcmp(export->name, "__new")
  5299. && export->index >= module->import_function_count) {
  5300. /* __new && __pin for AssemblyScript */
  5301. func_index = export->index - module->import_function_count;
  5302. func_type = module->functions[func_index]->func_type;
  5303. if (func_type->param_count == 2 && func_type->result_count == 1
  5304. && func_type->types[0] == malloc_free_io_type
  5305. && func_type->types[1] == VALUE_TYPE_I32
  5306. && func_type->types[2] == malloc_free_io_type) {
  5307. uint32 j;
  5308. WASMExport *export_tmp;
  5309. bh_assert(module->malloc_function == (uint32)-1);
  5310. module->malloc_function = export->index;
  5311. LOG_VERBOSE("Found malloc function, name: %s, index: %u",
  5312. export->name, export->index);
  5313. /* resolve retain function.
  5314. If not found, reset malloc function index */
  5315. export_tmp = module->exports;
  5316. for (j = 0; j < module->export_count; j++, export_tmp++) {
  5317. if ((export_tmp->kind == EXPORT_KIND_FUNC)
  5318. && (!strcmp(export_tmp->name, "__retain")
  5319. || (!strcmp(export_tmp->name, "__pin")))
  5320. && (export_tmp->index
  5321. >= module->import_function_count)) {
  5322. func_index = export_tmp->index
  5323. - module->import_function_count;
  5324. func_type =
  5325. module->functions[func_index]->func_type;
  5326. if (func_type->param_count == 1
  5327. && func_type->result_count == 1
  5328. && func_type->types[0] == malloc_free_io_type
  5329. && func_type->types[1] == malloc_free_io_type) {
  5330. bh_assert(module->retain_function
  5331. == (uint32)-1);
  5332. module->retain_function = export_tmp->index;
  5333. LOG_VERBOSE("Found retain function, name: %s, "
  5334. "index: %u",
  5335. export_tmp->name,
  5336. export_tmp->index);
  5337. break;
  5338. }
  5339. }
  5340. }
  5341. if (j == module->export_count) {
  5342. module->malloc_function = (uint32)-1;
  5343. LOG_VERBOSE("Can't find retain function,"
  5344. "reset malloc function index to -1");
  5345. }
  5346. }
  5347. }
  5348. else if (((!strcmp(export->name, "free"))
  5349. || (!strcmp(export->name, "__release"))
  5350. || (!strcmp(export->name, "__unpin")))
  5351. && export->index >= module->import_function_count) {
  5352. func_index = export->index - module->import_function_count;
  5353. func_type = module->functions[func_index]->func_type;
  5354. if (func_type->param_count == 1 && func_type->result_count == 0
  5355. && func_type->types[0] == malloc_free_io_type) {
  5356. bh_assert(module->free_function == (uint32)-1);
  5357. module->free_function = export->index;
  5358. LOG_VERBOSE("Found free function, name: %s, index: %u",
  5359. export->name, export->index);
  5360. }
  5361. }
  5362. }
  5363. }
  5364. #if WASM_ENABLE_FAST_INTERP != 0 && WASM_ENABLE_LABELS_AS_VALUES != 0
  5365. handle_table = wasm_interp_get_handle_table();
  5366. #endif
  5367. for (i = 0; i < module->function_count; i++) {
  5368. WASMFunction *func = module->functions[i];
  5369. if (!wasm_loader_prepare_bytecode(module, func, i, error_buf,
  5370. error_buf_size)) {
  5371. return false;
  5372. }
  5373. if (i == module->function_count - 1
  5374. && func->code + func->code_size != buf_code_end) {
  5375. set_error_buf(error_buf, error_buf_size,
  5376. "code section size mismatch");
  5377. return false;
  5378. }
  5379. }
  5380. if (!module->possible_memory_grow) {
  5381. WASMMemoryImport *memory_import;
  5382. WASMMemory *memory;
  5383. if (aux_data_end_global && aux_heap_base_global
  5384. && aux_stack_top_global) {
  5385. uint64 init_memory_size;
  5386. uint64 shrunk_memory_size = align_uint64(aux_heap_base, 8);
  5387. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5388. * valid range of uint32 */
  5389. if (shrunk_memory_size <= UINT32_MAX) {
  5390. if (module->import_memory_count) {
  5391. memory_import = &module->import_memories[0].u.memory;
  5392. init_memory_size = (uint64)memory_import->num_bytes_per_page
  5393. * memory_import->init_page_count;
  5394. if (shrunk_memory_size <= init_memory_size) {
  5395. /* Reset memory info to decrease memory usage */
  5396. memory_import->num_bytes_per_page =
  5397. (uint32)shrunk_memory_size;
  5398. memory_import->init_page_count = 1;
  5399. LOG_VERBOSE("Shrink import memory size to %" PRIu64,
  5400. shrunk_memory_size);
  5401. }
  5402. }
  5403. if (module->memory_count) {
  5404. memory = &module->memories[0];
  5405. init_memory_size = (uint64)memory->num_bytes_per_page
  5406. * memory->init_page_count;
  5407. if (shrunk_memory_size <= init_memory_size) {
  5408. /* Reset memory info to decrease memory usage */
  5409. memory->num_bytes_per_page = (uint32)shrunk_memory_size;
  5410. memory->init_page_count = 1;
  5411. LOG_VERBOSE("Shrink memory size to %" PRIu64,
  5412. shrunk_memory_size);
  5413. }
  5414. }
  5415. }
  5416. }
  5417. #if WASM_ENABLE_MULTI_MODULE == 0
  5418. if (module->import_memory_count) {
  5419. memory_import = &module->import_memories[0].u.memory;
  5420. /* Only resize the memory to one big page if num_bytes_per_page is
  5421. * in valid range of uint32 */
  5422. if (memory_import->init_page_count < DEFAULT_MAX_PAGES) {
  5423. memory_import->num_bytes_per_page *=
  5424. memory_import->init_page_count;
  5425. if (memory_import->init_page_count > 0)
  5426. memory_import->init_page_count =
  5427. memory_import->max_page_count = 1;
  5428. else
  5429. memory_import->init_page_count =
  5430. memory_import->max_page_count = 0;
  5431. }
  5432. }
  5433. if (module->memory_count) {
  5434. memory = &module->memories[0];
  5435. /* Only resize(shrunk) the memory size if num_bytes_per_page is in
  5436. * valid range of uint32 */
  5437. if (memory->init_page_count < DEFAULT_MAX_PAGES) {
  5438. memory->num_bytes_per_page *= memory->init_page_count;
  5439. if (memory->init_page_count > 0)
  5440. memory->init_page_count = memory->max_page_count = 1;
  5441. else
  5442. memory->init_page_count = memory->max_page_count = 0;
  5443. }
  5444. }
  5445. #endif
  5446. }
  5447. calculate_global_data_offset(module);
  5448. #if WASM_ENABLE_FAST_JIT != 0
  5449. if (!init_fast_jit_functions(module, error_buf, error_buf_size)) {
  5450. return false;
  5451. }
  5452. #endif
  5453. #if WASM_ENABLE_JIT != 0
  5454. if (!init_llvm_jit_functions_stage1(module, error_buf, error_buf_size)) {
  5455. return false;
  5456. }
  5457. #if !(WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0)
  5458. if (!init_llvm_jit_functions_stage2(module, error_buf, error_buf_size)) {
  5459. return false;
  5460. }
  5461. #else
  5462. /* Run aot_compile_wasm in a backend thread, so as not to block the main
  5463. thread fast jit execution, since applying llvm optimizations in
  5464. aot_compile_wasm may cost a lot of time.
  5465. Create thread with enough native stack to apply llvm optimizations */
  5466. if (os_thread_create(&module->llvm_jit_init_thread,
  5467. init_llvm_jit_functions_stage2_callback,
  5468. (void *)module, APP_THREAD_STACK_SIZE_DEFAULT * 8)
  5469. != 0) {
  5470. set_error_buf(error_buf, error_buf_size,
  5471. "create orcjit compile thread failed");
  5472. return false;
  5473. }
  5474. #endif
  5475. #endif
  5476. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5477. /* Create threads to compile the jit functions */
  5478. if (!compile_jit_functions(module, error_buf, error_buf_size)) {
  5479. return false;
  5480. }
  5481. #endif
  5482. #if WASM_ENABLE_MEMORY_TRACING != 0
  5483. wasm_runtime_dump_module_mem_consumption((WASMModuleCommon *)module);
  5484. #endif
  5485. return true;
  5486. }
  5487. static WASMModule *
  5488. create_module(char *name, char *error_buf, uint32 error_buf_size)
  5489. {
  5490. WASMModule *module =
  5491. loader_malloc(sizeof(WASMModule), error_buf, error_buf_size);
  5492. bh_list_status ret;
  5493. if (!module) {
  5494. return NULL;
  5495. }
  5496. module->module_type = Wasm_Module_Bytecode;
  5497. /* Set start_function to -1, means no start function */
  5498. module->start_function = (uint32)-1;
  5499. module->name = name;
  5500. #if WASM_ENABLE_FAST_INTERP == 0
  5501. module->br_table_cache_list = &module->br_table_cache_list_head;
  5502. ret = bh_list_init(module->br_table_cache_list);
  5503. bh_assert(ret == BH_LIST_SUCCESS);
  5504. #endif
  5505. #if WASM_ENABLE_MULTI_MODULE != 0
  5506. module->import_module_list = &module->import_module_list_head;
  5507. ret = bh_list_init(module->import_module_list);
  5508. bh_assert(ret == BH_LIST_SUCCESS);
  5509. #endif
  5510. #if WASM_ENABLE_DEBUG_INTERP != 0
  5511. ret = bh_list_init(&module->fast_opcode_list);
  5512. bh_assert(ret == BH_LIST_SUCCESS);
  5513. #endif
  5514. #if WASM_ENABLE_GC != 0
  5515. if (!(module->ref_type_set =
  5516. wasm_reftype_set_create(GC_REFTYPE_MAP_SIZE_DEFAULT))) {
  5517. set_error_buf(error_buf, error_buf_size, "create reftype map failed");
  5518. goto fail1;
  5519. }
  5520. if (os_mutex_init(&module->rtt_type_lock)) {
  5521. set_error_buf(error_buf, error_buf_size, "init rtt type lock failed");
  5522. goto fail2;
  5523. }
  5524. #endif
  5525. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5526. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5527. && WASM_ENABLE_LAZY_JIT != 0)
  5528. if (os_mutex_init(&module->instance_list_lock) != 0) {
  5529. set_error_buf(error_buf, error_buf_size,
  5530. "init instance list lock failed");
  5531. goto fail3;
  5532. }
  5533. #endif
  5534. (void)ret;
  5535. return module;
  5536. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  5537. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT \
  5538. && WASM_ENABLE_LAZY_JIT != 0)
  5539. fail3:
  5540. #endif
  5541. #if WASM_ENABLE_GC != 0
  5542. os_mutex_destroy(&module->rtt_type_lock);
  5543. fail2:
  5544. bh_hash_map_destroy(module->ref_type_set);
  5545. fail1:
  5546. #endif
  5547. wasm_runtime_free(module);
  5548. return NULL;
  5549. }
  5550. #if WASM_ENABLE_DEBUG_INTERP != 0
  5551. static bool
  5552. record_fast_op(WASMModule *module, uint8 *pos, uint8 orig_op, char *error_buf,
  5553. uint32 error_buf_size)
  5554. {
  5555. WASMFastOPCodeNode *fast_op =
  5556. loader_malloc(sizeof(WASMFastOPCodeNode), error_buf, error_buf_size);
  5557. if (fast_op) {
  5558. fast_op->offset = pos - module->load_addr;
  5559. fast_op->orig_op = orig_op;
  5560. bh_list_insert(&module->fast_opcode_list, fast_op);
  5561. }
  5562. return fast_op ? true : false;
  5563. }
  5564. #endif
  5565. WASMModule *
  5566. wasm_loader_load_from_sections(WASMSection *section_list, char *error_buf,
  5567. uint32 error_buf_size)
  5568. {
  5569. WASMModule *module = create_module("", error_buf, error_buf_size);
  5570. if (!module)
  5571. return NULL;
  5572. if (!load_from_sections(module, section_list, false, error_buf,
  5573. error_buf_size)) {
  5574. wasm_loader_unload(module);
  5575. return NULL;
  5576. }
  5577. LOG_VERBOSE("Load module from sections success.\n");
  5578. return module;
  5579. }
  5580. static void
  5581. destroy_sections(WASMSection *section_list)
  5582. {
  5583. WASMSection *section = section_list, *next;
  5584. while (section) {
  5585. next = section->next;
  5586. wasm_runtime_free(section);
  5587. section = next;
  5588. }
  5589. }
  5590. /* clang-format off */
  5591. static uint8 section_ids[] = {
  5592. SECTION_TYPE_USER,
  5593. SECTION_TYPE_TYPE,
  5594. SECTION_TYPE_IMPORT,
  5595. SECTION_TYPE_FUNC,
  5596. SECTION_TYPE_TABLE,
  5597. SECTION_TYPE_MEMORY,
  5598. #if WASM_ENABLE_TAGS != 0
  5599. SECTION_TYPE_TAG,
  5600. #endif
  5601. #if WASM_ENABLE_STRINGREF != 0
  5602. /* must immediately precede the global section,
  5603. or where the global section would be */
  5604. SECTION_TYPE_STRINGREF,
  5605. #endif
  5606. SECTION_TYPE_GLOBAL,
  5607. SECTION_TYPE_EXPORT,
  5608. SECTION_TYPE_START,
  5609. SECTION_TYPE_ELEM,
  5610. #if WASM_ENABLE_BULK_MEMORY != 0
  5611. SECTION_TYPE_DATACOUNT,
  5612. #endif
  5613. SECTION_TYPE_CODE,
  5614. SECTION_TYPE_DATA
  5615. };
  5616. /* clang-format on */
  5617. static uint8
  5618. get_section_index(uint8 section_type)
  5619. {
  5620. uint8 max_id = sizeof(section_ids) / sizeof(uint8);
  5621. for (uint8 i = 0; i < max_id; i++) {
  5622. if (section_type == section_ids[i])
  5623. return i;
  5624. }
  5625. return (uint8)-1;
  5626. }
  5627. static bool
  5628. create_sections(const uint8 *buf, uint32 size, WASMSection **p_section_list,
  5629. char *error_buf, uint32 error_buf_size)
  5630. {
  5631. WASMSection *section_list_end = NULL, *section;
  5632. const uint8 *p = buf, *p_end = buf + size /*, *section_body*/;
  5633. uint8 section_type, section_index, last_section_index = (uint8)-1;
  5634. uint32 section_size;
  5635. bh_assert(!*p_section_list);
  5636. p += 8;
  5637. while (p < p_end) {
  5638. CHECK_BUF(p, p_end, 1);
  5639. section_type = read_uint8(p);
  5640. section_index = get_section_index(section_type);
  5641. if (section_index != (uint8)-1) {
  5642. if (section_type != SECTION_TYPE_USER) {
  5643. /* Custom sections may be inserted at any place,
  5644. while other sections must occur at most once
  5645. and in prescribed order. */
  5646. if (last_section_index != (uint8)-1
  5647. && (section_index <= last_section_index)) {
  5648. set_error_buf(error_buf, error_buf_size,
  5649. "unexpected content after last section or "
  5650. "junk after last section");
  5651. return false;
  5652. }
  5653. last_section_index = section_index;
  5654. }
  5655. read_leb_uint32(p, p_end, section_size);
  5656. CHECK_BUF1(p, p_end, section_size);
  5657. if (!(section = loader_malloc(sizeof(WASMSection), error_buf,
  5658. error_buf_size))) {
  5659. return false;
  5660. }
  5661. section->section_type = section_type;
  5662. section->section_body = (uint8 *)p;
  5663. section->section_body_size = section_size;
  5664. if (!section_list_end)
  5665. *p_section_list = section_list_end = section;
  5666. else {
  5667. section_list_end->next = section;
  5668. section_list_end = section;
  5669. }
  5670. p += section_size;
  5671. }
  5672. else {
  5673. set_error_buf(error_buf, error_buf_size, "invalid section id");
  5674. return false;
  5675. }
  5676. }
  5677. return true;
  5678. fail:
  5679. return false;
  5680. }
  5681. static void
  5682. exchange32(uint8 *p_data)
  5683. {
  5684. uint8 value = *p_data;
  5685. *p_data = *(p_data + 3);
  5686. *(p_data + 3) = value;
  5687. value = *(p_data + 1);
  5688. *(p_data + 1) = *(p_data + 2);
  5689. *(p_data + 2) = value;
  5690. }
  5691. static union {
  5692. int a;
  5693. char b;
  5694. } __ue = { .a = 1 };
  5695. #define is_little_endian() (__ue.b == 1)
  5696. static bool
  5697. load(const uint8 *buf, uint32 size, WASMModule *module, char *error_buf,
  5698. uint32 error_buf_size)
  5699. {
  5700. const uint8 *buf_end = buf + size;
  5701. const uint8 *p = buf, *p_end = buf_end;
  5702. uint32 magic_number, version;
  5703. WASMSection *section_list = NULL;
  5704. CHECK_BUF1(p, p_end, sizeof(uint32));
  5705. magic_number = read_uint32(p);
  5706. if (!is_little_endian())
  5707. exchange32((uint8 *)&magic_number);
  5708. if (magic_number != WASM_MAGIC_NUMBER) {
  5709. set_error_buf(error_buf, error_buf_size, "magic header not detected");
  5710. return false;
  5711. }
  5712. CHECK_BUF1(p, p_end, sizeof(uint32));
  5713. version = read_uint32(p);
  5714. if (!is_little_endian())
  5715. exchange32((uint8 *)&version);
  5716. if (version != WASM_CURRENT_VERSION) {
  5717. set_error_buf(error_buf, error_buf_size, "unknown binary version");
  5718. return false;
  5719. }
  5720. if (!create_sections(buf, size, &section_list, error_buf, error_buf_size)
  5721. || !load_from_sections(module, section_list, true, error_buf,
  5722. error_buf_size)) {
  5723. destroy_sections(section_list);
  5724. return false;
  5725. }
  5726. destroy_sections(section_list);
  5727. return true;
  5728. fail:
  5729. return false;
  5730. }
  5731. #if WASM_ENABLE_LIBC_WASI != 0
  5732. /**
  5733. * refer to
  5734. * https://github.com/WebAssembly/WASI/blob/main/design/application-abi.md
  5735. */
  5736. static bool
  5737. check_wasi_abi_compatibility(const WASMModule *module,
  5738. #if WASM_ENABLE_MULTI_MODULE != 0
  5739. bool main_module,
  5740. #endif
  5741. char *error_buf, uint32 error_buf_size)
  5742. {
  5743. /**
  5744. * be careful with:
  5745. * wasi compatible modules(command/reactor) which don't import any wasi
  5746. * APIs. Usually, a command has to import a "prox_exit" at least, but a
  5747. * reactor can depend on nothing. At the same time, each has its own entry
  5748. * point.
  5749. *
  5750. * observations:
  5751. * - clang always injects `_start` into a command
  5752. * - clang always injects `_initialize` into a reactor
  5753. * - `iwasm -f` allows to run a function in the reactor
  5754. *
  5755. * strong assumptions:
  5756. * - no one will define either `_start` or `_initialize` on purpose
  5757. * - `_start` should always be `void _start(void)`
  5758. * - `_initialize` should always be `void _initialize(void)`
  5759. *
  5760. */
  5761. /* clang-format off */
  5762. /**
  5763. *
  5764. * | | import_wasi_api True | | import_wasi_api False | |
  5765. * | ----------- | -------------------- | ---------------- | --------------------- | ---------------- |
  5766. * | | \_initialize() Y | \_initialize() N | \_initialize() Y | \_initialize() N |
  5767. * | \_start() Y | N | COMMANDER | N | COMMANDER |
  5768. * | \_start() N | REACTOR | N | REACTOR | OTHERS |
  5769. */
  5770. /* clang-format on */
  5771. WASMExport *initialize = NULL, *memory = NULL, *start = NULL;
  5772. uint32 import_function_count = module->import_function_count;
  5773. WASMFuncType *func_type;
  5774. /* (func (export "_start") (...) */
  5775. start = wasm_loader_find_export(module, "", "_start", EXPORT_KIND_FUNC,
  5776. error_buf, error_buf_size);
  5777. if (start) {
  5778. if (start->index < import_function_count) {
  5779. set_error_buf(
  5780. error_buf, error_buf_size,
  5781. "the builtin _start function can not be an import function");
  5782. return false;
  5783. }
  5784. func_type =
  5785. module->functions[start->index - import_function_count]->func_type;
  5786. if (func_type->param_count || func_type->result_count) {
  5787. set_error_buf(error_buf, error_buf_size,
  5788. "the signature of builtin _start function is wrong");
  5789. return false;
  5790. }
  5791. }
  5792. else {
  5793. /* (func (export "_initialize") (...) */
  5794. initialize =
  5795. wasm_loader_find_export(module, "", "_initialize", EXPORT_KIND_FUNC,
  5796. error_buf, error_buf_size);
  5797. if (initialize) {
  5798. if (initialize->index < import_function_count) {
  5799. set_error_buf(error_buf, error_buf_size,
  5800. "the builtin _initialize function can not be an "
  5801. "import function");
  5802. return false;
  5803. }
  5804. func_type =
  5805. module->functions[initialize->index - import_function_count]
  5806. ->func_type;
  5807. if (func_type->param_count || func_type->result_count) {
  5808. set_error_buf(
  5809. error_buf, error_buf_size,
  5810. "the signature of builtin _initialize function is wrong");
  5811. return false;
  5812. }
  5813. }
  5814. }
  5815. /* filter out non-wasi compatible modules */
  5816. if (!module->import_wasi_api && !start && !initialize) {
  5817. return true;
  5818. }
  5819. /* should have one at least */
  5820. if (module->import_wasi_api && !start && !initialize) {
  5821. LOG_WARNING("warning: a module with WASI apis should be either "
  5822. "a command or a reactor");
  5823. }
  5824. /*
  5825. * there is at least one of `_start` and `_initialize` in below cases.
  5826. * according to the assumption, they should be all wasi compatible
  5827. */
  5828. #if WASM_ENABLE_MULTI_MODULE != 0
  5829. /* filter out commands (with `_start`) cases */
  5830. if (start && !main_module) {
  5831. set_error_buf(
  5832. error_buf, error_buf_size,
  5833. "a command (with _start function) can not be a sub-module");
  5834. return false;
  5835. }
  5836. #endif
  5837. /*
  5838. * it is ok a reactor acts as a main module,
  5839. * so skip the check about (with `_initialize`)
  5840. */
  5841. memory = wasm_loader_find_export(module, "", "memory", EXPORT_KIND_MEMORY,
  5842. error_buf, error_buf_size);
  5843. if (!memory
  5844. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  5845. /*
  5846. * with wasi-threads, it's still an open question if a memory
  5847. * should be exported.
  5848. *
  5849. * https://github.com/WebAssembly/wasi-threads/issues/22
  5850. * https://github.com/WebAssembly/WASI/issues/502
  5851. *
  5852. * Note: this code assumes the number of memories is at most 1.
  5853. */
  5854. && module->import_memory_count == 0
  5855. #endif
  5856. ) {
  5857. set_error_buf(error_buf, error_buf_size,
  5858. "a module with WASI apis must export memory by default");
  5859. return false;
  5860. }
  5861. return true;
  5862. }
  5863. #endif
  5864. WASMModule *
  5865. wasm_loader_load(uint8 *buf, uint32 size,
  5866. #if WASM_ENABLE_MULTI_MODULE != 0
  5867. bool main_module,
  5868. #endif
  5869. const LoadArgs *args, char *error_buf, uint32 error_buf_size)
  5870. {
  5871. WASMModule *module = create_module(args->name, error_buf, error_buf_size);
  5872. if (!module) {
  5873. return NULL;
  5874. }
  5875. #if WASM_ENABLE_DEBUG_INTERP != 0 || WASM_ENABLE_FAST_JIT != 0 \
  5876. || WASM_ENABLE_DUMP_CALL_STACK != 0 || WASM_ENABLE_JIT != 0
  5877. module->load_addr = (uint8 *)buf;
  5878. module->load_size = size;
  5879. #endif
  5880. if (!load(buf, size, module, error_buf, error_buf_size)) {
  5881. goto fail;
  5882. }
  5883. #if WASM_ENABLE_LIBC_WASI != 0
  5884. /* Check the WASI application ABI */
  5885. if (!check_wasi_abi_compatibility(module,
  5886. #if WASM_ENABLE_MULTI_MODULE != 0
  5887. main_module,
  5888. #endif
  5889. error_buf, error_buf_size)) {
  5890. goto fail;
  5891. }
  5892. #endif
  5893. LOG_VERBOSE("Load module success.\n");
  5894. return module;
  5895. fail:
  5896. wasm_loader_unload(module);
  5897. return NULL;
  5898. }
  5899. void
  5900. wasm_loader_unload(WASMModule *module)
  5901. {
  5902. uint32 i;
  5903. if (!module)
  5904. return;
  5905. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5906. && WASM_ENABLE_LAZY_JIT != 0
  5907. module->orcjit_stop_compiling = true;
  5908. if (module->llvm_jit_init_thread)
  5909. os_thread_join(module->llvm_jit_init_thread, NULL);
  5910. #endif
  5911. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0
  5912. /* Stop Fast/LLVM JIT compilation firstly to avoid accessing
  5913. module internal data after they were freed */
  5914. orcjit_stop_compile_threads(module);
  5915. #endif
  5916. #if WASM_ENABLE_JIT != 0
  5917. if (module->func_ptrs)
  5918. wasm_runtime_free(module->func_ptrs);
  5919. if (module->comp_ctx)
  5920. aot_destroy_comp_context(module->comp_ctx);
  5921. if (module->comp_data)
  5922. aot_destroy_comp_data(module->comp_data);
  5923. #endif
  5924. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  5925. && WASM_ENABLE_LAZY_JIT != 0
  5926. if (module->tierup_wait_lock_inited) {
  5927. os_mutex_destroy(&module->tierup_wait_lock);
  5928. os_cond_destroy(&module->tierup_wait_cond);
  5929. }
  5930. #endif
  5931. if (module->types) {
  5932. for (i = 0; i < module->type_count; i++) {
  5933. if (module->types[i])
  5934. destroy_wasm_type(module->types[i]);
  5935. }
  5936. wasm_runtime_free(module->types);
  5937. }
  5938. if (module->imports)
  5939. wasm_runtime_free(module->imports);
  5940. if (module->functions) {
  5941. for (i = 0; i < module->function_count; i++) {
  5942. if (module->functions[i]) {
  5943. if (module->functions[i]->local_offsets)
  5944. wasm_runtime_free(module->functions[i]->local_offsets);
  5945. #if WASM_ENABLE_FAST_INTERP != 0
  5946. if (module->functions[i]->code_compiled)
  5947. wasm_runtime_free(module->functions[i]->code_compiled);
  5948. if (module->functions[i]->consts)
  5949. wasm_runtime_free(module->functions[i]->consts);
  5950. #endif
  5951. #if WASM_ENABLE_FAST_JIT != 0
  5952. if (module->functions[i]->fast_jit_jitted_code) {
  5953. jit_code_cache_free(
  5954. module->functions[i]->fast_jit_jitted_code);
  5955. }
  5956. #if WASM_ENABLE_JIT != 0 && WASM_ENABLE_LAZY_JIT != 0
  5957. if (module->functions[i]->call_to_fast_jit_from_llvm_jit) {
  5958. jit_code_cache_free(
  5959. module->functions[i]->call_to_fast_jit_from_llvm_jit);
  5960. }
  5961. #endif
  5962. #endif
  5963. #if WASM_ENABLE_GC != 0
  5964. if (module->functions[i]->local_ref_type_maps) {
  5965. wasm_runtime_free(
  5966. module->functions[i]->local_ref_type_maps);
  5967. }
  5968. #endif
  5969. wasm_runtime_free(module->functions[i]);
  5970. }
  5971. }
  5972. wasm_runtime_free(module->functions);
  5973. }
  5974. if (module->tables) {
  5975. #if WASM_ENABLE_GC != 0
  5976. for (i = 0; i < module->table_count; i++) {
  5977. destroy_init_expr(&module->tables[i].init_expr);
  5978. }
  5979. #endif
  5980. wasm_runtime_free(module->tables);
  5981. }
  5982. if (module->memories)
  5983. wasm_runtime_free(module->memories);
  5984. if (module->globals) {
  5985. #if WASM_ENABLE_GC != 0
  5986. for (i = 0; i < module->global_count; i++) {
  5987. destroy_init_expr(&module->globals[i].init_expr);
  5988. }
  5989. #endif
  5990. wasm_runtime_free(module->globals);
  5991. }
  5992. #if WASM_ENABLE_TAGS != 0
  5993. if (module->tags) {
  5994. for (i = 0; i < module->tag_count; i++) {
  5995. if (module->tags[i])
  5996. wasm_runtime_free(module->tags[i]);
  5997. }
  5998. wasm_runtime_free(module->tags);
  5999. }
  6000. #endif
  6001. if (module->exports)
  6002. wasm_runtime_free(module->exports);
  6003. if (module->table_segments) {
  6004. for (i = 0; i < module->table_seg_count; i++) {
  6005. if (module->table_segments[i].init_values) {
  6006. #if WASM_ENABLE_GC != 0
  6007. uint32 j;
  6008. for (j = 0; j < module->table_segments[i].value_count; j++) {
  6009. destroy_init_expr(
  6010. &module->table_segments[i].init_values[j]);
  6011. }
  6012. #endif
  6013. wasm_runtime_free(module->table_segments[i].init_values);
  6014. }
  6015. }
  6016. wasm_runtime_free(module->table_segments);
  6017. }
  6018. if (module->data_segments) {
  6019. for (i = 0; i < module->data_seg_count; i++) {
  6020. if (module->data_segments[i])
  6021. wasm_runtime_free(module->data_segments[i]);
  6022. }
  6023. wasm_runtime_free(module->data_segments);
  6024. }
  6025. if (module->const_str_list) {
  6026. StringNode *node = module->const_str_list, *node_next;
  6027. while (node) {
  6028. node_next = node->next;
  6029. wasm_runtime_free(node);
  6030. node = node_next;
  6031. }
  6032. }
  6033. #if WASM_ENABLE_STRINGREF != 0
  6034. if (module->string_literal_ptrs) {
  6035. wasm_runtime_free((void *)module->string_literal_ptrs);
  6036. }
  6037. if (module->string_literal_lengths) {
  6038. wasm_runtime_free(module->string_literal_lengths);
  6039. }
  6040. #endif
  6041. #if WASM_ENABLE_FAST_INTERP == 0
  6042. if (module->br_table_cache_list) {
  6043. BrTableCache *node = bh_list_first_elem(module->br_table_cache_list);
  6044. BrTableCache *node_next;
  6045. while (node) {
  6046. node_next = bh_list_elem_next(node);
  6047. wasm_runtime_free(node);
  6048. node = node_next;
  6049. }
  6050. }
  6051. #endif
  6052. #if WASM_ENABLE_MULTI_MODULE != 0
  6053. /* just release the sub module list */
  6054. if (module->import_module_list) {
  6055. WASMRegisteredModule *node =
  6056. bh_list_first_elem(module->import_module_list);
  6057. while (node) {
  6058. WASMRegisteredModule *next = bh_list_elem_next(node);
  6059. bh_list_remove(module->import_module_list, node);
  6060. /*
  6061. * unload(sub_module) will be triggered during runtime_destroy().
  6062. * every module in the global module list will be unloaded one by
  6063. * one. so don't worry.
  6064. */
  6065. wasm_runtime_free(node);
  6066. /*
  6067. * the module file reading buffer will be released
  6068. * in runtime_destroy()
  6069. */
  6070. node = next;
  6071. }
  6072. }
  6073. #endif
  6074. #if WASM_ENABLE_DEBUG_INTERP != 0
  6075. WASMFastOPCodeNode *fast_opcode =
  6076. bh_list_first_elem(&module->fast_opcode_list);
  6077. while (fast_opcode) {
  6078. WASMFastOPCodeNode *next = bh_list_elem_next(fast_opcode);
  6079. wasm_runtime_free(fast_opcode);
  6080. fast_opcode = next;
  6081. }
  6082. #endif
  6083. #if WASM_ENABLE_DEBUG_INTERP != 0 \
  6084. || (WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  6085. && WASM_ENABLE_LAZY_JIT != 0)
  6086. os_mutex_destroy(&module->instance_list_lock);
  6087. #endif
  6088. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  6089. wasm_runtime_destroy_custom_sections(module->custom_section_list);
  6090. #endif
  6091. #if WASM_ENABLE_FAST_JIT != 0
  6092. if (module->fast_jit_func_ptrs) {
  6093. wasm_runtime_free(module->fast_jit_func_ptrs);
  6094. }
  6095. for (i = 0; i < WASM_ORC_JIT_BACKEND_THREAD_NUM; i++) {
  6096. if (module->fast_jit_thread_locks_inited[i]) {
  6097. os_mutex_destroy(&module->fast_jit_thread_locks[i]);
  6098. }
  6099. }
  6100. #endif
  6101. #if WASM_ENABLE_GC != 0
  6102. os_mutex_destroy(&module->rtt_type_lock);
  6103. bh_hash_map_destroy(module->ref_type_set);
  6104. if (module->rtt_types) {
  6105. for (i = 0; i < module->type_count; i++) {
  6106. if (module->rtt_types[i])
  6107. wasm_runtime_free(module->rtt_types[i]);
  6108. }
  6109. wasm_runtime_free(module->rtt_types);
  6110. }
  6111. #if WASM_ENABLE_STRINGREF != 0
  6112. for (i = 0; i < WASM_TYPE_STRINGVIEWITER - WASM_TYPE_STRINGREF + 1; i++) {
  6113. if (module->stringref_rtts[i])
  6114. wasm_runtime_free(module->stringref_rtts[i]);
  6115. }
  6116. #endif
  6117. #endif
  6118. wasm_runtime_free(module);
  6119. }
  6120. bool
  6121. wasm_loader_find_block_addr(WASMExecEnv *exec_env, BlockAddr *block_addr_cache,
  6122. const uint8 *start_addr, const uint8 *code_end_addr,
  6123. uint8 label_type, uint8 **p_else_addr,
  6124. uint8 **p_end_addr)
  6125. {
  6126. const uint8 *p = start_addr, *p_end = code_end_addr;
  6127. uint8 *else_addr = NULL;
  6128. char error_buf[128];
  6129. uint32 block_nested_depth = 1, count, i, j, t;
  6130. uint32 error_buf_size = sizeof(error_buf);
  6131. uint8 opcode, u8;
  6132. BlockAddr block_stack[16] = { { 0 } }, *block;
  6133. i = ((uintptr_t)start_addr) & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6134. block = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6135. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++) {
  6136. if (block[j].start_addr == start_addr) {
  6137. /* Cache hit */
  6138. *p_else_addr = block[j].else_addr;
  6139. *p_end_addr = block[j].end_addr;
  6140. return true;
  6141. }
  6142. }
  6143. /* Cache unhit */
  6144. block_stack[0].start_addr = start_addr;
  6145. while (p < code_end_addr) {
  6146. opcode = *p++;
  6147. #if WASM_ENABLE_DEBUG_INTERP != 0
  6148. op_break_retry:
  6149. #endif
  6150. switch (opcode) {
  6151. case WASM_OP_UNREACHABLE:
  6152. case WASM_OP_NOP:
  6153. break;
  6154. #if WASM_ENABLE_EXCE_HANDLING != 0
  6155. case WASM_OP_TRY:
  6156. u8 = read_uint8(p);
  6157. if (block_nested_depth
  6158. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6159. block_stack[block_nested_depth].start_addr = p;
  6160. block_stack[block_nested_depth].else_addr = NULL;
  6161. }
  6162. block_nested_depth++;
  6163. break;
  6164. case EXT_OP_TRY:
  6165. skip_leb_uint32(p, p_end);
  6166. if (block_nested_depth
  6167. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6168. block_stack[block_nested_depth].start_addr = p;
  6169. block_stack[block_nested_depth].else_addr = NULL;
  6170. }
  6171. block_nested_depth++;
  6172. break;
  6173. case WASM_OP_CATCH:
  6174. if (block_nested_depth == 1) {
  6175. *p_end_addr = (uint8 *)(p - 1);
  6176. /* stop search and return the address of the catch block */
  6177. return true;
  6178. }
  6179. break;
  6180. case WASM_OP_CATCH_ALL:
  6181. if (block_nested_depth == 1) {
  6182. *p_end_addr = (uint8 *)(p - 1);
  6183. /* stop search and return the address of the catch_all block
  6184. */
  6185. return true;
  6186. }
  6187. break;
  6188. case WASM_OP_THROW:
  6189. /* skip tag_index */
  6190. skip_leb(p);
  6191. break;
  6192. case WASM_OP_RETHROW:
  6193. /* skip depth */
  6194. skip_leb(p);
  6195. break;
  6196. case WASM_OP_DELEGATE:
  6197. if (block_nested_depth == 1) {
  6198. *p_end_addr = (uint8 *)(p - 1);
  6199. return true;
  6200. }
  6201. else {
  6202. skip_leb(p);
  6203. /* the DELEGATE opcode ends the tryblock, */
  6204. block_nested_depth--;
  6205. if (block_nested_depth
  6206. < sizeof(block_stack) / sizeof(BlockAddr))
  6207. block_stack[block_nested_depth].end_addr =
  6208. (uint8 *)(p - 1);
  6209. }
  6210. break;
  6211. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  6212. case WASM_OP_BLOCK:
  6213. case WASM_OP_LOOP:
  6214. case WASM_OP_IF:
  6215. {
  6216. /* block result type: 0x40/0x7F/0x7E/0x7D/0x7C */
  6217. u8 = read_uint8(p);
  6218. if (is_byte_a_type(u8)) {
  6219. #if WASM_ENABLE_GC != 0
  6220. if (wasm_is_type_multi_byte_type(u8)) {
  6221. /* the possible extra bytes of GC ref type have been
  6222. modified to OP_NOP, no need to resolve them again */
  6223. }
  6224. #endif
  6225. }
  6226. else {
  6227. p--;
  6228. skip_leb_uint32(p, p_end);
  6229. }
  6230. if (block_nested_depth
  6231. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6232. block_stack[block_nested_depth].start_addr = p;
  6233. block_stack[block_nested_depth].else_addr = NULL;
  6234. }
  6235. block_nested_depth++;
  6236. break;
  6237. }
  6238. case EXT_OP_BLOCK:
  6239. case EXT_OP_LOOP:
  6240. case EXT_OP_IF:
  6241. /* block type */
  6242. skip_leb_uint32(p, p_end);
  6243. if (block_nested_depth
  6244. < sizeof(block_stack) / sizeof(BlockAddr)) {
  6245. block_stack[block_nested_depth].start_addr = p;
  6246. block_stack[block_nested_depth].else_addr = NULL;
  6247. }
  6248. block_nested_depth++;
  6249. break;
  6250. case WASM_OP_ELSE:
  6251. if (label_type == LABEL_TYPE_IF && block_nested_depth == 1)
  6252. else_addr = (uint8 *)(p - 1);
  6253. if (block_nested_depth - 1
  6254. < sizeof(block_stack) / sizeof(BlockAddr))
  6255. block_stack[block_nested_depth - 1].else_addr =
  6256. (uint8 *)(p - 1);
  6257. break;
  6258. case WASM_OP_END:
  6259. if (block_nested_depth == 1) {
  6260. if (label_type == LABEL_TYPE_IF)
  6261. *p_else_addr = else_addr;
  6262. *p_end_addr = (uint8 *)(p - 1);
  6263. block_stack[0].end_addr = (uint8 *)(p - 1);
  6264. for (t = 0; t < sizeof(block_stack) / sizeof(BlockAddr);
  6265. t++) {
  6266. start_addr = block_stack[t].start_addr;
  6267. if (start_addr) {
  6268. i = ((uintptr_t)start_addr)
  6269. & (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
  6270. block =
  6271. block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * i;
  6272. for (j = 0; j < BLOCK_ADDR_CONFLICT_SIZE; j++)
  6273. if (!block[j].start_addr)
  6274. break;
  6275. if (j == BLOCK_ADDR_CONFLICT_SIZE) {
  6276. memmove(block + 1, block,
  6277. (BLOCK_ADDR_CONFLICT_SIZE - 1)
  6278. * sizeof(BlockAddr));
  6279. j = 0;
  6280. }
  6281. block[j].start_addr = block_stack[t].start_addr;
  6282. block[j].else_addr = block_stack[t].else_addr;
  6283. block[j].end_addr = block_stack[t].end_addr;
  6284. }
  6285. else
  6286. break;
  6287. }
  6288. return true;
  6289. }
  6290. else {
  6291. block_nested_depth--;
  6292. if (block_nested_depth
  6293. < sizeof(block_stack) / sizeof(BlockAddr))
  6294. block_stack[block_nested_depth].end_addr =
  6295. (uint8 *)(p - 1);
  6296. }
  6297. break;
  6298. case WASM_OP_BR:
  6299. case WASM_OP_BR_IF:
  6300. skip_leb_uint32(p, p_end); /* labelidx */
  6301. break;
  6302. case WASM_OP_BR_TABLE:
  6303. read_leb_uint32(p, p_end, count); /* label num */
  6304. #if WASM_ENABLE_FAST_INTERP != 0
  6305. for (i = 0; i <= count; i++) /* labelidxs */
  6306. skip_leb_uint32(p, p_end);
  6307. #else
  6308. p += count + 1;
  6309. while (*p == WASM_OP_NOP)
  6310. p++;
  6311. #endif
  6312. break;
  6313. #if WASM_ENABLE_FAST_INTERP == 0
  6314. case EXT_OP_BR_TABLE_CACHE:
  6315. read_leb_uint32(p, p_end, count); /* label num */
  6316. while (*p == WASM_OP_NOP)
  6317. p++;
  6318. break;
  6319. #endif
  6320. case WASM_OP_RETURN:
  6321. break;
  6322. case WASM_OP_CALL:
  6323. #if WASM_ENABLE_TAIL_CALL != 0
  6324. case WASM_OP_RETURN_CALL:
  6325. #endif
  6326. skip_leb_uint32(p, p_end); /* funcidx */
  6327. break;
  6328. case WASM_OP_CALL_INDIRECT:
  6329. #if WASM_ENABLE_TAIL_CALL != 0
  6330. case WASM_OP_RETURN_CALL_INDIRECT:
  6331. #endif
  6332. skip_leb_uint32(p, p_end); /* typeidx */
  6333. #if WASM_ENABLE_REF_TYPES == 0 && WASM_ENABLE_GC == 0
  6334. u8 = read_uint8(p); /* 0x00 */
  6335. #else
  6336. skip_leb_uint32(p, p_end); /* tableidx */
  6337. #endif
  6338. break;
  6339. #if WASM_ENABLE_GC != 0
  6340. case WASM_OP_CALL_REF:
  6341. case WASM_OP_RETURN_CALL_REF:
  6342. skip_leb_uint32(p, p_end); /* typeidx */
  6343. break;
  6344. #endif
  6345. case WASM_OP_DROP:
  6346. case WASM_OP_SELECT:
  6347. case WASM_OP_DROP_64:
  6348. case WASM_OP_SELECT_64:
  6349. break;
  6350. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  6351. case WASM_OP_SELECT_T:
  6352. {
  6353. skip_leb_uint32(p, p_end); /* vec length */
  6354. u8 = read_uint8(p); /* typeidx */
  6355. /* the possible extra bytes of GC ref type have been
  6356. modified to OP_NOP, no need to resolve them again */
  6357. break;
  6358. }
  6359. case WASM_OP_TABLE_GET:
  6360. case WASM_OP_TABLE_SET:
  6361. skip_leb_uint32(p, p_end); /* table index */
  6362. break;
  6363. case WASM_OP_REF_NULL:
  6364. {
  6365. u8 = read_uint8(p); /* type */
  6366. if (is_byte_a_type(u8)) {
  6367. #if WASM_ENABLE_GC != 0
  6368. if (wasm_is_type_multi_byte_type(u8)) {
  6369. /* the possible extra bytes of GC ref type have been
  6370. modified to OP_NOP, no need to resolve them again */
  6371. }
  6372. #endif
  6373. }
  6374. else {
  6375. p--;
  6376. skip_leb_uint32(p, p_end);
  6377. }
  6378. break;
  6379. }
  6380. case WASM_OP_REF_IS_NULL:
  6381. break;
  6382. case WASM_OP_REF_FUNC:
  6383. skip_leb_uint32(p, p_end); /* func index */
  6384. break;
  6385. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  6386. #if WASM_ENABLE_GC != 0
  6387. case WASM_OP_REF_AS_NON_NULL:
  6388. case WASM_OP_REF_EQ:
  6389. break;
  6390. case WASM_OP_BR_ON_NULL:
  6391. case WASM_OP_BR_ON_NON_NULL:
  6392. skip_leb_uint32(p, p_end); /* label index */
  6393. break;
  6394. #endif /* end of WASM_ENABLE_GC != 0 */
  6395. case WASM_OP_GET_LOCAL:
  6396. case WASM_OP_SET_LOCAL:
  6397. case WASM_OP_TEE_LOCAL:
  6398. case WASM_OP_GET_GLOBAL:
  6399. case WASM_OP_SET_GLOBAL:
  6400. case WASM_OP_GET_GLOBAL_64:
  6401. case WASM_OP_SET_GLOBAL_64:
  6402. case WASM_OP_SET_GLOBAL_AUX_STACK:
  6403. skip_leb_uint32(p, p_end); /* local index */
  6404. break;
  6405. case EXT_OP_GET_LOCAL_FAST:
  6406. case EXT_OP_SET_LOCAL_FAST:
  6407. case EXT_OP_TEE_LOCAL_FAST:
  6408. CHECK_BUF(p, p_end, 1);
  6409. p++;
  6410. break;
  6411. case WASM_OP_I32_LOAD:
  6412. case WASM_OP_I64_LOAD:
  6413. case WASM_OP_F32_LOAD:
  6414. case WASM_OP_F64_LOAD:
  6415. case WASM_OP_I32_LOAD8_S:
  6416. case WASM_OP_I32_LOAD8_U:
  6417. case WASM_OP_I32_LOAD16_S:
  6418. case WASM_OP_I32_LOAD16_U:
  6419. case WASM_OP_I64_LOAD8_S:
  6420. case WASM_OP_I64_LOAD8_U:
  6421. case WASM_OP_I64_LOAD16_S:
  6422. case WASM_OP_I64_LOAD16_U:
  6423. case WASM_OP_I64_LOAD32_S:
  6424. case WASM_OP_I64_LOAD32_U:
  6425. case WASM_OP_I32_STORE:
  6426. case WASM_OP_I64_STORE:
  6427. case WASM_OP_F32_STORE:
  6428. case WASM_OP_F64_STORE:
  6429. case WASM_OP_I32_STORE8:
  6430. case WASM_OP_I32_STORE16:
  6431. case WASM_OP_I64_STORE8:
  6432. case WASM_OP_I64_STORE16:
  6433. case WASM_OP_I64_STORE32:
  6434. skip_leb_uint32(p, p_end); /* align */
  6435. skip_leb_mem_offset(p, p_end); /* offset */
  6436. break;
  6437. case WASM_OP_MEMORY_SIZE:
  6438. case WASM_OP_MEMORY_GROW:
  6439. skip_leb_uint32(p, p_end); /* 0x00 */
  6440. break;
  6441. case WASM_OP_I32_CONST:
  6442. skip_leb_int32(p, p_end);
  6443. break;
  6444. case WASM_OP_I64_CONST:
  6445. skip_leb_int64(p, p_end);
  6446. break;
  6447. case WASM_OP_F32_CONST:
  6448. p += sizeof(float32);
  6449. break;
  6450. case WASM_OP_F64_CONST:
  6451. p += sizeof(float64);
  6452. break;
  6453. case WASM_OP_I32_EQZ:
  6454. case WASM_OP_I32_EQ:
  6455. case WASM_OP_I32_NE:
  6456. case WASM_OP_I32_LT_S:
  6457. case WASM_OP_I32_LT_U:
  6458. case WASM_OP_I32_GT_S:
  6459. case WASM_OP_I32_GT_U:
  6460. case WASM_OP_I32_LE_S:
  6461. case WASM_OP_I32_LE_U:
  6462. case WASM_OP_I32_GE_S:
  6463. case WASM_OP_I32_GE_U:
  6464. case WASM_OP_I64_EQZ:
  6465. case WASM_OP_I64_EQ:
  6466. case WASM_OP_I64_NE:
  6467. case WASM_OP_I64_LT_S:
  6468. case WASM_OP_I64_LT_U:
  6469. case WASM_OP_I64_GT_S:
  6470. case WASM_OP_I64_GT_U:
  6471. case WASM_OP_I64_LE_S:
  6472. case WASM_OP_I64_LE_U:
  6473. case WASM_OP_I64_GE_S:
  6474. case WASM_OP_I64_GE_U:
  6475. case WASM_OP_F32_EQ:
  6476. case WASM_OP_F32_NE:
  6477. case WASM_OP_F32_LT:
  6478. case WASM_OP_F32_GT:
  6479. case WASM_OP_F32_LE:
  6480. case WASM_OP_F32_GE:
  6481. case WASM_OP_F64_EQ:
  6482. case WASM_OP_F64_NE:
  6483. case WASM_OP_F64_LT:
  6484. case WASM_OP_F64_GT:
  6485. case WASM_OP_F64_LE:
  6486. case WASM_OP_F64_GE:
  6487. case WASM_OP_I32_CLZ:
  6488. case WASM_OP_I32_CTZ:
  6489. case WASM_OP_I32_POPCNT:
  6490. case WASM_OP_I32_ADD:
  6491. case WASM_OP_I32_SUB:
  6492. case WASM_OP_I32_MUL:
  6493. case WASM_OP_I32_DIV_S:
  6494. case WASM_OP_I32_DIV_U:
  6495. case WASM_OP_I32_REM_S:
  6496. case WASM_OP_I32_REM_U:
  6497. case WASM_OP_I32_AND:
  6498. case WASM_OP_I32_OR:
  6499. case WASM_OP_I32_XOR:
  6500. case WASM_OP_I32_SHL:
  6501. case WASM_OP_I32_SHR_S:
  6502. case WASM_OP_I32_SHR_U:
  6503. case WASM_OP_I32_ROTL:
  6504. case WASM_OP_I32_ROTR:
  6505. case WASM_OP_I64_CLZ:
  6506. case WASM_OP_I64_CTZ:
  6507. case WASM_OP_I64_POPCNT:
  6508. case WASM_OP_I64_ADD:
  6509. case WASM_OP_I64_SUB:
  6510. case WASM_OP_I64_MUL:
  6511. case WASM_OP_I64_DIV_S:
  6512. case WASM_OP_I64_DIV_U:
  6513. case WASM_OP_I64_REM_S:
  6514. case WASM_OP_I64_REM_U:
  6515. case WASM_OP_I64_AND:
  6516. case WASM_OP_I64_OR:
  6517. case WASM_OP_I64_XOR:
  6518. case WASM_OP_I64_SHL:
  6519. case WASM_OP_I64_SHR_S:
  6520. case WASM_OP_I64_SHR_U:
  6521. case WASM_OP_I64_ROTL:
  6522. case WASM_OP_I64_ROTR:
  6523. case WASM_OP_F32_ABS:
  6524. case WASM_OP_F32_NEG:
  6525. case WASM_OP_F32_CEIL:
  6526. case WASM_OP_F32_FLOOR:
  6527. case WASM_OP_F32_TRUNC:
  6528. case WASM_OP_F32_NEAREST:
  6529. case WASM_OP_F32_SQRT:
  6530. case WASM_OP_F32_ADD:
  6531. case WASM_OP_F32_SUB:
  6532. case WASM_OP_F32_MUL:
  6533. case WASM_OP_F32_DIV:
  6534. case WASM_OP_F32_MIN:
  6535. case WASM_OP_F32_MAX:
  6536. case WASM_OP_F32_COPYSIGN:
  6537. case WASM_OP_F64_ABS:
  6538. case WASM_OP_F64_NEG:
  6539. case WASM_OP_F64_CEIL:
  6540. case WASM_OP_F64_FLOOR:
  6541. case WASM_OP_F64_TRUNC:
  6542. case WASM_OP_F64_NEAREST:
  6543. case WASM_OP_F64_SQRT:
  6544. case WASM_OP_F64_ADD:
  6545. case WASM_OP_F64_SUB:
  6546. case WASM_OP_F64_MUL:
  6547. case WASM_OP_F64_DIV:
  6548. case WASM_OP_F64_MIN:
  6549. case WASM_OP_F64_MAX:
  6550. case WASM_OP_F64_COPYSIGN:
  6551. case WASM_OP_I32_WRAP_I64:
  6552. case WASM_OP_I32_TRUNC_S_F32:
  6553. case WASM_OP_I32_TRUNC_U_F32:
  6554. case WASM_OP_I32_TRUNC_S_F64:
  6555. case WASM_OP_I32_TRUNC_U_F64:
  6556. case WASM_OP_I64_EXTEND_S_I32:
  6557. case WASM_OP_I64_EXTEND_U_I32:
  6558. case WASM_OP_I64_TRUNC_S_F32:
  6559. case WASM_OP_I64_TRUNC_U_F32:
  6560. case WASM_OP_I64_TRUNC_S_F64:
  6561. case WASM_OP_I64_TRUNC_U_F64:
  6562. case WASM_OP_F32_CONVERT_S_I32:
  6563. case WASM_OP_F32_CONVERT_U_I32:
  6564. case WASM_OP_F32_CONVERT_S_I64:
  6565. case WASM_OP_F32_CONVERT_U_I64:
  6566. case WASM_OP_F32_DEMOTE_F64:
  6567. case WASM_OP_F64_CONVERT_S_I32:
  6568. case WASM_OP_F64_CONVERT_U_I32:
  6569. case WASM_OP_F64_CONVERT_S_I64:
  6570. case WASM_OP_F64_CONVERT_U_I64:
  6571. case WASM_OP_F64_PROMOTE_F32:
  6572. case WASM_OP_I32_REINTERPRET_F32:
  6573. case WASM_OP_I64_REINTERPRET_F64:
  6574. case WASM_OP_F32_REINTERPRET_I32:
  6575. case WASM_OP_F64_REINTERPRET_I64:
  6576. case WASM_OP_I32_EXTEND8_S:
  6577. case WASM_OP_I32_EXTEND16_S:
  6578. case WASM_OP_I64_EXTEND8_S:
  6579. case WASM_OP_I64_EXTEND16_S:
  6580. case WASM_OP_I64_EXTEND32_S:
  6581. break;
  6582. #if WASM_ENABLE_GC != 0
  6583. case WASM_OP_GC_PREFIX:
  6584. {
  6585. uint32 opcode1;
  6586. read_leb_uint32(p, p_end, opcode1);
  6587. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6588. is no larger than UINT8_MAX */
  6589. opcode = (uint8)opcode1;
  6590. switch (opcode) {
  6591. case WASM_OP_STRUCT_NEW:
  6592. case WASM_OP_STRUCT_NEW_DEFAULT:
  6593. skip_leb_uint32(p, p_end); /* typeidx */
  6594. break;
  6595. case WASM_OP_STRUCT_GET:
  6596. case WASM_OP_STRUCT_GET_S:
  6597. case WASM_OP_STRUCT_GET_U:
  6598. case WASM_OP_STRUCT_SET:
  6599. skip_leb_uint32(p, p_end); /* typeidx */
  6600. skip_leb_uint32(p, p_end); /* fieldidx */
  6601. break;
  6602. case WASM_OP_ARRAY_NEW:
  6603. case WASM_OP_ARRAY_NEW_DEFAULT:
  6604. case WASM_OP_ARRAY_GET:
  6605. case WASM_OP_ARRAY_GET_S:
  6606. case WASM_OP_ARRAY_GET_U:
  6607. case WASM_OP_ARRAY_SET:
  6608. case WASM_OP_ARRAY_FILL:
  6609. skip_leb_uint32(p, p_end); /* typeidx */
  6610. break;
  6611. case WASM_OP_ARRAY_COPY:
  6612. skip_leb_uint32(p, p_end); /* typeidx1 */
  6613. skip_leb_uint32(p, p_end); /* typeidx2 */
  6614. break;
  6615. case WASM_OP_ARRAY_LEN:
  6616. break;
  6617. case WASM_OP_ARRAY_NEW_FIXED:
  6618. case WASM_OP_ARRAY_NEW_DATA:
  6619. case WASM_OP_ARRAY_NEW_ELEM:
  6620. skip_leb_uint32(p, p_end); /* typeidx */
  6621. skip_leb_uint32(p, p_end); /* N/dataidx/elemidx */
  6622. break;
  6623. case WASM_OP_REF_I31:
  6624. case WASM_OP_I31_GET_S:
  6625. case WASM_OP_I31_GET_U:
  6626. break;
  6627. case WASM_OP_REF_TEST:
  6628. case WASM_OP_REF_CAST:
  6629. case WASM_OP_REF_TEST_NULLABLE:
  6630. case WASM_OP_REF_CAST_NULLABLE:
  6631. skip_leb_int32(p, p_end); /* heaptype */
  6632. break;
  6633. case WASM_OP_BR_ON_CAST:
  6634. case WASM_OP_BR_ON_CAST_FAIL:
  6635. p += sizeof(uint8); /* castflag */
  6636. skip_leb_uint32(p, p_end); /* labelidx */
  6637. skip_leb_int32(p, p_end); /* heaptype */
  6638. skip_leb_int32(p, p_end); /* heaptype2 */
  6639. break;
  6640. case WASM_OP_ANY_CONVERT_EXTERN:
  6641. case WASM_OP_EXTERN_CONVERT_ANY:
  6642. break;
  6643. #if WASM_ENABLE_STRINGREF != 0
  6644. case WASM_OP_STRING_NEW_UTF8:
  6645. case WASM_OP_STRING_NEW_WTF16:
  6646. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  6647. case WASM_OP_STRING_NEW_WTF8:
  6648. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6649. break;
  6650. case WASM_OP_STRING_CONST:
  6651. skip_leb_int32(p, p_end); /* contents */
  6652. break;
  6653. case WASM_OP_STRING_MEASURE_UTF8:
  6654. case WASM_OP_STRING_MEASURE_WTF8:
  6655. case WASM_OP_STRING_MEASURE_WTF16:
  6656. break;
  6657. case WASM_OP_STRING_ENCODE_UTF8:
  6658. case WASM_OP_STRING_ENCODE_WTF16:
  6659. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  6660. case WASM_OP_STRING_ENCODE_WTF8:
  6661. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6662. break;
  6663. case WASM_OP_STRING_CONCAT:
  6664. case WASM_OP_STRING_EQ:
  6665. case WASM_OP_STRING_IS_USV_SEQUENCE:
  6666. case WASM_OP_STRING_AS_WTF8:
  6667. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  6668. break;
  6669. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  6670. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  6671. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  6672. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6673. break;
  6674. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  6675. case WASM_OP_STRING_AS_WTF16:
  6676. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  6677. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  6678. break;
  6679. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  6680. skip_leb_uint32(p, p_end); /* memory index 0x00 */
  6681. break;
  6682. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  6683. case WASM_OP_STRING_AS_ITER:
  6684. case WASM_OP_STRINGVIEW_ITER_NEXT:
  6685. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  6686. case WASM_OP_STRINGVIEW_ITER_REWIND:
  6687. case WASM_OP_STRINGVIEW_ITER_SLICE:
  6688. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  6689. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  6690. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  6691. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  6692. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  6693. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  6694. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  6695. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  6696. break;
  6697. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  6698. default:
  6699. return false;
  6700. }
  6701. break;
  6702. }
  6703. #endif /* end of WASM_ENABLE_GC != 0 */
  6704. case WASM_OP_MISC_PREFIX:
  6705. {
  6706. uint32 opcode1;
  6707. read_leb_uint32(p, p_end, opcode1);
  6708. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6709. is no larger than UINT8_MAX */
  6710. opcode = (uint8)opcode1;
  6711. switch (opcode) {
  6712. case WASM_OP_I32_TRUNC_SAT_S_F32:
  6713. case WASM_OP_I32_TRUNC_SAT_U_F32:
  6714. case WASM_OP_I32_TRUNC_SAT_S_F64:
  6715. case WASM_OP_I32_TRUNC_SAT_U_F64:
  6716. case WASM_OP_I64_TRUNC_SAT_S_F32:
  6717. case WASM_OP_I64_TRUNC_SAT_U_F32:
  6718. case WASM_OP_I64_TRUNC_SAT_S_F64:
  6719. case WASM_OP_I64_TRUNC_SAT_U_F64:
  6720. break;
  6721. #if WASM_ENABLE_BULK_MEMORY != 0
  6722. case WASM_OP_MEMORY_INIT:
  6723. skip_leb_uint32(p, p_end);
  6724. /* skip memory idx */
  6725. p++;
  6726. break;
  6727. case WASM_OP_DATA_DROP:
  6728. skip_leb_uint32(p, p_end);
  6729. break;
  6730. case WASM_OP_MEMORY_COPY:
  6731. /* skip two memory idx */
  6732. p += 2;
  6733. break;
  6734. case WASM_OP_MEMORY_FILL:
  6735. /* skip memory idx */
  6736. p++;
  6737. break;
  6738. #endif /* WASM_ENABLE_BULK_MEMORY */
  6739. #if WASM_ENABLE_REF_TYPES != 0
  6740. case WASM_OP_TABLE_INIT:
  6741. case WASM_OP_TABLE_COPY:
  6742. /* tableidx */
  6743. skip_leb_uint32(p, p_end);
  6744. /* elemidx */
  6745. skip_leb_uint32(p, p_end);
  6746. break;
  6747. case WASM_OP_ELEM_DROP:
  6748. /* elemidx */
  6749. skip_leb_uint32(p, p_end);
  6750. break;
  6751. case WASM_OP_TABLE_SIZE:
  6752. case WASM_OP_TABLE_GROW:
  6753. case WASM_OP_TABLE_FILL:
  6754. skip_leb_uint32(p, p_end); /* table idx */
  6755. break;
  6756. #endif /* WASM_ENABLE_REF_TYPES */
  6757. default:
  6758. return false;
  6759. }
  6760. break;
  6761. }
  6762. #if WASM_ENABLE_SIMD != 0
  6763. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  6764. case WASM_OP_SIMD_PREFIX:
  6765. {
  6766. /* TODO: memory64 offset type changes */
  6767. uint32 opcode1;
  6768. read_leb_uint32(p, p_end, opcode1);
  6769. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6770. is no larger than UINT8_MAX */
  6771. opcode = (uint8)opcode1;
  6772. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  6773. */
  6774. switch (opcode) {
  6775. case SIMD_v128_load:
  6776. case SIMD_v128_load8x8_s:
  6777. case SIMD_v128_load8x8_u:
  6778. case SIMD_v128_load16x4_s:
  6779. case SIMD_v128_load16x4_u:
  6780. case SIMD_v128_load32x2_s:
  6781. case SIMD_v128_load32x2_u:
  6782. case SIMD_v128_load8_splat:
  6783. case SIMD_v128_load16_splat:
  6784. case SIMD_v128_load32_splat:
  6785. case SIMD_v128_load64_splat:
  6786. case SIMD_v128_store:
  6787. /* memarg align */
  6788. skip_leb_uint32(p, p_end);
  6789. /* memarg offset*/
  6790. skip_leb_uint32(p, p_end);
  6791. break;
  6792. case SIMD_v128_const:
  6793. case SIMD_v8x16_shuffle:
  6794. /* immByte[16] immLaneId[16] */
  6795. CHECK_BUF1(p, p_end, 16);
  6796. p += 16;
  6797. break;
  6798. case SIMD_i8x16_extract_lane_s:
  6799. case SIMD_i8x16_extract_lane_u:
  6800. case SIMD_i8x16_replace_lane:
  6801. case SIMD_i16x8_extract_lane_s:
  6802. case SIMD_i16x8_extract_lane_u:
  6803. case SIMD_i16x8_replace_lane:
  6804. case SIMD_i32x4_extract_lane:
  6805. case SIMD_i32x4_replace_lane:
  6806. case SIMD_i64x2_extract_lane:
  6807. case SIMD_i64x2_replace_lane:
  6808. case SIMD_f32x4_extract_lane:
  6809. case SIMD_f32x4_replace_lane:
  6810. case SIMD_f64x2_extract_lane:
  6811. case SIMD_f64x2_replace_lane:
  6812. /* ImmLaneId */
  6813. CHECK_BUF(p, p_end, 1);
  6814. p++;
  6815. break;
  6816. case SIMD_v128_load8_lane:
  6817. case SIMD_v128_load16_lane:
  6818. case SIMD_v128_load32_lane:
  6819. case SIMD_v128_load64_lane:
  6820. case SIMD_v128_store8_lane:
  6821. case SIMD_v128_store16_lane:
  6822. case SIMD_v128_store32_lane:
  6823. case SIMD_v128_store64_lane:
  6824. /* memarg align */
  6825. skip_leb_uint32(p, p_end);
  6826. /* memarg offset*/
  6827. skip_leb_uint32(p, p_end);
  6828. /* ImmLaneId */
  6829. CHECK_BUF(p, p_end, 1);
  6830. p++;
  6831. break;
  6832. case SIMD_v128_load32_zero:
  6833. case SIMD_v128_load64_zero:
  6834. /* memarg align */
  6835. skip_leb_uint32(p, p_end);
  6836. /* memarg offset*/
  6837. skip_leb_uint32(p, p_end);
  6838. break;
  6839. default:
  6840. /*
  6841. * since latest SIMD specific used almost every value
  6842. * from 0x00 to 0xff, the default branch will present
  6843. * all opcodes without imm
  6844. * https://github.com/WebAssembly/simd/blob/main/proposals/simd/NewOpcodes.md
  6845. */
  6846. break;
  6847. }
  6848. break;
  6849. }
  6850. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  6851. #endif /* end of WASM_ENABLE_SIMD */
  6852. #if WASM_ENABLE_SHARED_MEMORY != 0
  6853. case WASM_OP_ATOMIC_PREFIX:
  6854. {
  6855. /* TODO: memory64 offset type changes */
  6856. uint32 opcode1;
  6857. /* atomic_op (u32_leb) + memarg (2 u32_leb) */
  6858. read_leb_uint32(p, p_end, opcode1);
  6859. /* opcode1 was checked in wasm_loader_prepare_bytecode and
  6860. is no larger than UINT8_MAX */
  6861. opcode = (uint8)opcode1;
  6862. if (opcode != WASM_OP_ATOMIC_FENCE) {
  6863. skip_leb_uint32(p, p_end); /* align */
  6864. skip_leb_mem_offset(p, p_end); /* offset */
  6865. }
  6866. else {
  6867. /* atomic.fence doesn't have memarg */
  6868. p++;
  6869. }
  6870. break;
  6871. }
  6872. #endif
  6873. #if WASM_ENABLE_DEBUG_INTERP != 0
  6874. case DEBUG_OP_BREAK:
  6875. {
  6876. WASMDebugInstance *debug_instance =
  6877. wasm_exec_env_get_instance(exec_env);
  6878. char original_opcode[1];
  6879. uint64 size = 1;
  6880. WASMModuleInstance *module_inst =
  6881. (WASMModuleInstance *)exec_env->module_inst;
  6882. uint64 offset = (p - 1) >= module_inst->module->load_addr
  6883. ? (p - 1) - module_inst->module->load_addr
  6884. : ~0;
  6885. if (debug_instance) {
  6886. if (wasm_debug_instance_get_obj_mem(debug_instance, offset,
  6887. original_opcode, &size)
  6888. && size == 1) {
  6889. LOG_VERBOSE("WASM loader find OP_BREAK , recover it "
  6890. "with %02x: ",
  6891. original_opcode[0]);
  6892. opcode = original_opcode[0];
  6893. goto op_break_retry;
  6894. }
  6895. }
  6896. break;
  6897. }
  6898. #endif
  6899. default:
  6900. return false;
  6901. }
  6902. }
  6903. (void)u8;
  6904. (void)exec_env;
  6905. return false;
  6906. fail:
  6907. return false;
  6908. }
  6909. #if WASM_ENABLE_FAST_INTERP != 0
  6910. #if WASM_DEBUG_PREPROCESSOR != 0
  6911. #define LOG_OP(...) os_printf(__VA_ARGS__)
  6912. #else
  6913. #define LOG_OP(...) (void)0
  6914. #endif
  6915. #define PATCH_ELSE 0
  6916. #define PATCH_END 1
  6917. typedef struct BranchBlockPatch {
  6918. struct BranchBlockPatch *next;
  6919. uint8 patch_type;
  6920. uint8 *code_compiled;
  6921. } BranchBlockPatch;
  6922. #endif
  6923. typedef struct BranchBlock {
  6924. uint8 label_type;
  6925. BlockType block_type;
  6926. uint8 *start_addr;
  6927. uint8 *else_addr;
  6928. uint8 *end_addr;
  6929. uint32 stack_cell_num;
  6930. #if WASM_ENABLE_GC != 0
  6931. uint32 reftype_map_num;
  6932. /* Indicate which local is used inside current block, used to validate
  6933. * local.get with non-nullable ref types */
  6934. uint8 *local_use_mask;
  6935. uint32 local_use_mask_size;
  6936. #endif
  6937. #if WASM_ENABLE_FAST_INTERP != 0
  6938. uint16 dynamic_offset;
  6939. uint8 *code_compiled;
  6940. BranchBlockPatch *patch_list;
  6941. /* This is used to save params frame_offset of of if block */
  6942. int16 *param_frame_offsets;
  6943. /* This is used to recover dynamic offset for else branch */
  6944. uint16 start_dynamic_offset;
  6945. #endif
  6946. /* Indicate the operand stack is in polymorphic state.
  6947. * If the opcode is one of unreachable/br/br_table/return, stack is marked
  6948. * to polymorphic state until the block's 'end' opcode is processed.
  6949. * If stack is in polymorphic state and stack is empty, instruction can
  6950. * pop any type of value directly without decreasing stack top pointer
  6951. * and stack cell num. */
  6952. bool is_stack_polymorphic;
  6953. } BranchBlock;
  6954. typedef struct WASMLoaderContext {
  6955. /* frame ref stack */
  6956. uint8 *frame_ref;
  6957. uint8 *frame_ref_bottom;
  6958. uint8 *frame_ref_boundary;
  6959. uint32 frame_ref_size;
  6960. uint32 stack_cell_num;
  6961. uint32 max_stack_cell_num;
  6962. #if WASM_ENABLE_GC != 0
  6963. /* frame reftype map stack */
  6964. WASMRefTypeMap *frame_reftype_map;
  6965. WASMRefTypeMap *frame_reftype_map_bottom;
  6966. WASMRefTypeMap *frame_reftype_map_boundary;
  6967. uint32 frame_reftype_map_size;
  6968. uint32 reftype_map_num;
  6969. uint32 max_reftype_map_num;
  6970. /* Current module */
  6971. WASMModule *module;
  6972. /* Current module's ref_type_set */
  6973. HashMap *ref_type_set;
  6974. /* Always point to local variable ref_type of
  6975. wasm_loader_prepare_bytecode */
  6976. WASMRefType *ref_type_tmp;
  6977. #endif
  6978. /* frame csp stack */
  6979. BranchBlock *frame_csp;
  6980. BranchBlock *frame_csp_bottom;
  6981. BranchBlock *frame_csp_boundary;
  6982. uint32 frame_csp_size;
  6983. uint32 csp_num;
  6984. uint32 max_csp_num;
  6985. #if WASM_ENABLE_FAST_INTERP != 0
  6986. /* frame offset stack */
  6987. int16 *frame_offset;
  6988. int16 *frame_offset_bottom;
  6989. int16 *frame_offset_boundary;
  6990. uint32 frame_offset_size;
  6991. int16 dynamic_offset;
  6992. int16 start_dynamic_offset;
  6993. int16 max_dynamic_offset;
  6994. /* preserved local offset */
  6995. int16 preserved_local_offset;
  6996. /* const buffer */
  6997. uint8 *const_buf;
  6998. uint16 num_const;
  6999. uint16 const_cell_num;
  7000. uint32 const_buf_size;
  7001. /* processed code */
  7002. uint8 *p_code_compiled;
  7003. uint8 *p_code_compiled_end;
  7004. uint32 code_compiled_size;
  7005. /* If the last opcode will be dropped, the peak memory usage will be larger
  7006. * than the final code_compiled_size, we record the peak size to ensure
  7007. * there will not be invalid memory access during second traverse */
  7008. uint32 code_compiled_peak_size;
  7009. #endif
  7010. } WASMLoaderContext;
  7011. typedef struct Const {
  7012. WASMValue value;
  7013. uint16 slot_index;
  7014. uint8 value_type;
  7015. } Const;
  7016. #define CHECK_CSP_PUSH() \
  7017. do { \
  7018. if (ctx->frame_csp >= ctx->frame_csp_boundary) { \
  7019. MEM_REALLOC( \
  7020. ctx->frame_csp_bottom, ctx->frame_csp_size, \
  7021. (uint32)(ctx->frame_csp_size + 8 * sizeof(BranchBlock))); \
  7022. ctx->frame_csp_size += (uint32)(8 * sizeof(BranchBlock)); \
  7023. ctx->frame_csp_boundary = \
  7024. ctx->frame_csp_bottom \
  7025. + ctx->frame_csp_size / sizeof(BranchBlock); \
  7026. ctx->frame_csp = ctx->frame_csp_bottom + ctx->csp_num; \
  7027. } \
  7028. } while (0)
  7029. #define CHECK_CSP_POP() \
  7030. do { \
  7031. if (ctx->csp_num < 1) { \
  7032. set_error_buf(error_buf, error_buf_size, \
  7033. "type mismatch: " \
  7034. "expect data but block stack was empty"); \
  7035. goto fail; \
  7036. } \
  7037. } while (0)
  7038. #if WASM_ENABLE_FAST_INTERP != 0
  7039. static bool
  7040. check_offset_push(WASMLoaderContext *ctx, char *error_buf,
  7041. uint32 error_buf_size)
  7042. {
  7043. uint32 cell_num = (uint32)(ctx->frame_offset - ctx->frame_offset_bottom);
  7044. if (ctx->frame_offset >= ctx->frame_offset_boundary) {
  7045. MEM_REALLOC(ctx->frame_offset_bottom, ctx->frame_offset_size,
  7046. ctx->frame_offset_size + 16);
  7047. ctx->frame_offset_size += 16;
  7048. ctx->frame_offset_boundary =
  7049. ctx->frame_offset_bottom + ctx->frame_offset_size / sizeof(int16);
  7050. ctx->frame_offset = ctx->frame_offset_bottom + cell_num;
  7051. }
  7052. return true;
  7053. fail:
  7054. return false;
  7055. }
  7056. static bool
  7057. check_offset_pop(WASMLoaderContext *ctx, uint32 cells)
  7058. {
  7059. if (ctx->frame_offset - cells < ctx->frame_offset_bottom)
  7060. return false;
  7061. return true;
  7062. }
  7063. static void
  7064. free_label_patch_list(BranchBlock *frame_csp)
  7065. {
  7066. BranchBlockPatch *label_patch = frame_csp->patch_list;
  7067. BranchBlockPatch *next;
  7068. while (label_patch != NULL) {
  7069. next = label_patch->next;
  7070. wasm_runtime_free(label_patch);
  7071. label_patch = next;
  7072. }
  7073. frame_csp->patch_list = NULL;
  7074. }
  7075. static void
  7076. free_all_label_patch_lists(BranchBlock *frame_csp, uint32 csp_num)
  7077. {
  7078. BranchBlock *tmp_csp = frame_csp;
  7079. for (uint32 i = 0; i < csp_num; i++) {
  7080. free_label_patch_list(tmp_csp);
  7081. tmp_csp++;
  7082. }
  7083. }
  7084. #endif /* end of WASM_ENABLE_FAST_INTERP */
  7085. #if WASM_ENABLE_GC != 0
  7086. static bool
  7087. wasm_loader_init_local_use_masks(WASMLoaderContext *ctx, uint32 local_count,
  7088. char *error_buf, uint32 error_buf_size)
  7089. {
  7090. BranchBlock *current_csp = ctx->frame_csp - 1;
  7091. uint32 local_mask_size;
  7092. if (local_count == 0) {
  7093. current_csp->local_use_mask_size = 0;
  7094. return true;
  7095. }
  7096. /* if current_csp->local_use_mask is not NULL, then it is re-init masks for
  7097. * else branch, we don't need to allocate memory again */
  7098. if (!current_csp->local_use_mask) {
  7099. local_mask_size = (local_count + 7) / sizeof(uint8);
  7100. if (!(current_csp->local_use_mask =
  7101. loader_malloc(local_mask_size, error_buf, error_buf_size))) {
  7102. return false;
  7103. }
  7104. current_csp->local_use_mask_size = local_mask_size;
  7105. }
  7106. else {
  7107. local_mask_size = current_csp->local_use_mask_size;
  7108. bh_assert(current_csp->label_type == LABEL_TYPE_IF);
  7109. }
  7110. if (current_csp->label_type != LABEL_TYPE_FUNCTION) {
  7111. /* For non-function blocks, inherit the use status from parent block */
  7112. BranchBlock *parent_csp = current_csp - 1;
  7113. bh_assert(parent_csp >= ctx->frame_csp_bottom);
  7114. bh_assert(parent_csp->local_use_mask);
  7115. bh_memcpy_s(current_csp->local_use_mask, local_mask_size,
  7116. parent_csp->local_use_mask, local_mask_size);
  7117. }
  7118. return true;
  7119. }
  7120. static void
  7121. wasm_loader_destroy_curr_local_use_masks(WASMLoaderContext *ctx)
  7122. {
  7123. BranchBlock *current_csp = ctx->frame_csp - 1;
  7124. bh_assert(current_csp->local_use_mask
  7125. || current_csp->local_use_mask_size == 0);
  7126. if (current_csp->local_use_mask) {
  7127. wasm_runtime_free(current_csp->local_use_mask);
  7128. }
  7129. current_csp->local_use_mask = NULL;
  7130. current_csp->local_use_mask_size = 0;
  7131. }
  7132. static void
  7133. wasm_loader_clean_all_local_use_masks(WASMLoaderContext *ctx)
  7134. {
  7135. BranchBlock *tmp_csp = ctx->frame_csp_bottom;
  7136. uint32 i;
  7137. for (i = 0; i < ctx->csp_num; i++) {
  7138. if (tmp_csp->local_use_mask) {
  7139. wasm_runtime_free(tmp_csp->local_use_mask);
  7140. tmp_csp->local_use_mask = NULL;
  7141. tmp_csp->local_use_mask_size = 0;
  7142. }
  7143. tmp_csp++;
  7144. }
  7145. }
  7146. static void
  7147. wasm_loader_mask_local(WASMLoaderContext *ctx, uint32 index)
  7148. {
  7149. BranchBlock *current_csp = ctx->frame_csp - 1;
  7150. uint32 byte_offset = index / sizeof(uint8);
  7151. uint32 bit_offset = index % sizeof(uint8);
  7152. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7153. bh_assert(current_csp->local_use_mask);
  7154. current_csp->local_use_mask[byte_offset] |= (1 << bit_offset);
  7155. }
  7156. static bool
  7157. wasm_loader_get_local_status(WASMLoaderContext *ctx, uint32 index)
  7158. {
  7159. BranchBlock *current_csp = ctx->frame_csp - 1;
  7160. uint32 byte_offset = index / sizeof(uint8);
  7161. uint32 bit_offset = index % sizeof(uint8);
  7162. bh_assert(byte_offset < current_csp->local_use_mask_size);
  7163. bh_assert(current_csp->local_use_mask);
  7164. return (current_csp->local_use_mask[byte_offset] & (1 << bit_offset))
  7165. ? true
  7166. : false;
  7167. }
  7168. #endif /* end of WASM_ENABLE_GC != 0 */
  7169. static void
  7170. wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
  7171. {
  7172. if (ctx) {
  7173. if (ctx->frame_ref_bottom)
  7174. wasm_runtime_free(ctx->frame_ref_bottom);
  7175. #if WASM_ENABLE_GC != 0
  7176. if (ctx->frame_reftype_map_bottom)
  7177. wasm_runtime_free(ctx->frame_reftype_map_bottom);
  7178. #endif
  7179. if (ctx->frame_csp_bottom) {
  7180. #if WASM_ENABLE_FAST_INTERP != 0
  7181. free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
  7182. #endif
  7183. #if WASM_ENABLE_GC != 0
  7184. wasm_loader_clean_all_local_use_masks(ctx);
  7185. #endif
  7186. wasm_runtime_free(ctx->frame_csp_bottom);
  7187. }
  7188. #if WASM_ENABLE_FAST_INTERP != 0
  7189. if (ctx->frame_offset_bottom)
  7190. wasm_runtime_free(ctx->frame_offset_bottom);
  7191. if (ctx->const_buf)
  7192. wasm_runtime_free(ctx->const_buf);
  7193. #endif
  7194. wasm_runtime_free(ctx);
  7195. }
  7196. }
  7197. static WASMLoaderContext *
  7198. wasm_loader_ctx_init(WASMFunction *func, char *error_buf, uint32 error_buf_size)
  7199. {
  7200. WASMLoaderContext *loader_ctx =
  7201. loader_malloc(sizeof(WASMLoaderContext), error_buf, error_buf_size);
  7202. if (!loader_ctx)
  7203. return NULL;
  7204. loader_ctx->frame_ref_size = 32;
  7205. if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref = loader_malloc(
  7206. loader_ctx->frame_ref_size, error_buf, error_buf_size)))
  7207. goto fail;
  7208. loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom + 32;
  7209. #if WASM_ENABLE_GC != 0
  7210. loader_ctx->frame_reftype_map_size = sizeof(WASMRefTypeMap) * 16;
  7211. if (!(loader_ctx->frame_reftype_map_bottom = loader_ctx->frame_reftype_map =
  7212. loader_malloc(loader_ctx->frame_reftype_map_size, error_buf,
  7213. error_buf_size)))
  7214. goto fail;
  7215. loader_ctx->frame_reftype_map_boundary =
  7216. loader_ctx->frame_reftype_map_bottom + 16;
  7217. #endif
  7218. loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
  7219. if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp = loader_malloc(
  7220. loader_ctx->frame_csp_size, error_buf, error_buf_size)))
  7221. goto fail;
  7222. loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
  7223. #if WASM_ENABLE_EXCE_HANDLING != 0
  7224. func->exception_handler_count = 0;
  7225. #endif
  7226. #if WASM_ENABLE_FAST_INTERP != 0
  7227. loader_ctx->frame_offset_size = sizeof(int16) * 32;
  7228. if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
  7229. loader_malloc(loader_ctx->frame_offset_size, error_buf,
  7230. error_buf_size)))
  7231. goto fail;
  7232. loader_ctx->frame_offset_boundary = loader_ctx->frame_offset_bottom + 32;
  7233. loader_ctx->num_const = 0;
  7234. loader_ctx->const_buf_size = sizeof(Const) * 8;
  7235. if (!(loader_ctx->const_buf = loader_malloc(loader_ctx->const_buf_size,
  7236. error_buf, error_buf_size)))
  7237. goto fail;
  7238. if (func->param_cell_num >= (int32)INT16_MAX - func->local_cell_num) {
  7239. set_error_buf(error_buf, error_buf_size,
  7240. "fast interpreter offset overflow");
  7241. goto fail;
  7242. }
  7243. loader_ctx->start_dynamic_offset = loader_ctx->dynamic_offset =
  7244. loader_ctx->max_dynamic_offset =
  7245. func->param_cell_num + func->local_cell_num;
  7246. #endif
  7247. return loader_ctx;
  7248. fail:
  7249. wasm_loader_ctx_destroy(loader_ctx);
  7250. return NULL;
  7251. }
  7252. static bool
  7253. check_stack_push(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7254. uint32 error_buf_size)
  7255. {
  7256. uint32 cell_num_needed = wasm_value_type_cell_num(type);
  7257. if (ctx->frame_ref + cell_num_needed > ctx->frame_ref_boundary) {
  7258. /* Increase the frame ref stack */
  7259. MEM_REALLOC(ctx->frame_ref_bottom, ctx->frame_ref_size,
  7260. ctx->frame_ref_size + 16);
  7261. ctx->frame_ref_size += 16;
  7262. ctx->frame_ref_boundary = ctx->frame_ref_bottom + ctx->frame_ref_size;
  7263. ctx->frame_ref = ctx->frame_ref_bottom + ctx->stack_cell_num;
  7264. }
  7265. #if WASM_ENABLE_GC != 0
  7266. if (wasm_is_type_multi_byte_type(type)
  7267. && ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7268. /* Increase the frame reftype map stack */
  7269. bh_assert(
  7270. (uint32)((ctx->frame_reftype_map - ctx->frame_reftype_map_bottom)
  7271. * sizeof(WASMRefTypeMap))
  7272. == ctx->frame_reftype_map_size);
  7273. MEM_REALLOC(ctx->frame_reftype_map_bottom, ctx->frame_reftype_map_size,
  7274. ctx->frame_reftype_map_size
  7275. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7276. ctx->frame_reftype_map =
  7277. ctx->frame_reftype_map_bottom
  7278. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7279. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7280. ctx->frame_reftype_map_boundary =
  7281. ctx->frame_reftype_map_bottom
  7282. + ctx->frame_reftype_map_size / ((uint32)sizeof(WASMRefTypeMap));
  7283. }
  7284. #endif
  7285. return true;
  7286. fail:
  7287. return false;
  7288. }
  7289. static bool
  7290. wasm_loader_push_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7291. uint32 error_buf_size)
  7292. {
  7293. uint32 type_cell_num = wasm_value_type_cell_num(type);
  7294. uint32 i;
  7295. if (!check_stack_push(ctx, type, error_buf, error_buf_size))
  7296. return false;
  7297. #if WASM_ENABLE_GC != 0
  7298. if (wasm_is_type_multi_byte_type(type)) {
  7299. WASMRefType *ref_type;
  7300. if (!(ref_type =
  7301. reftype_set_insert(ctx->ref_type_set, ctx->ref_type_tmp,
  7302. error_buf, error_buf_size))) {
  7303. return false;
  7304. }
  7305. if (ctx->frame_reftype_map >= ctx->frame_reftype_map_boundary) {
  7306. /* Increase the frame reftype map stack */
  7307. bh_assert((uint32)((ctx->frame_reftype_map
  7308. - ctx->frame_reftype_map_bottom)
  7309. * sizeof(WASMRefTypeMap))
  7310. == ctx->frame_reftype_map_size);
  7311. MEM_REALLOC(ctx->frame_reftype_map_bottom,
  7312. ctx->frame_reftype_map_size,
  7313. ctx->frame_reftype_map_size
  7314. + (uint32)sizeof(WASMRefTypeMap) * 8);
  7315. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom
  7316. + ctx->frame_reftype_map_size
  7317. / ((uint32)sizeof(WASMRefTypeMap));
  7318. ctx->frame_reftype_map_size += (uint32)sizeof(WASMRefTypeMap) * 8;
  7319. ctx->frame_reftype_map_boundary =
  7320. ctx->frame_reftype_map_bottom
  7321. + ctx->frame_reftype_map_size
  7322. / ((uint32)sizeof(WASMRefTypeMap));
  7323. }
  7324. ctx->frame_reftype_map->index = ctx->stack_cell_num;
  7325. ctx->frame_reftype_map->ref_type = ref_type;
  7326. ctx->frame_reftype_map++;
  7327. ctx->reftype_map_num++;
  7328. if (ctx->reftype_map_num > ctx->max_reftype_map_num)
  7329. ctx->max_reftype_map_num = ctx->reftype_map_num;
  7330. }
  7331. #endif
  7332. for (i = 0; i < type_cell_num; i++)
  7333. *ctx->frame_ref++ = type;
  7334. ctx->stack_cell_num += type_cell_num;
  7335. if (ctx->stack_cell_num > ctx->max_stack_cell_num) {
  7336. ctx->max_stack_cell_num = ctx->stack_cell_num;
  7337. if (ctx->max_stack_cell_num > UINT16_MAX) {
  7338. set_error_buf(error_buf, error_buf_size,
  7339. "operand stack depth limit exceeded");
  7340. return false;
  7341. }
  7342. }
  7343. return true;
  7344. #if WASM_ENABLE_GC != 0
  7345. fail:
  7346. return false;
  7347. #endif
  7348. }
  7349. static bool
  7350. check_stack_top_values(WASMLoaderContext *ctx, uint8 *frame_ref,
  7351. int32 stack_cell_num,
  7352. #if WASM_ENABLE_GC != 0
  7353. WASMRefTypeMap *frame_reftype_map, int32 reftype_map_num,
  7354. #endif
  7355. uint8 type,
  7356. #if WASM_ENABLE_GC != 0
  7357. WASMRefType *ref_type,
  7358. #endif
  7359. char *error_buf, uint32 error_buf_size)
  7360. {
  7361. int32 type_cell_num = (int32)wasm_value_type_cell_num(type), i;
  7362. #if WASM_ENABLE_GC != 0
  7363. WASMRefType *frame_reftype = NULL;
  7364. #endif
  7365. if (stack_cell_num < type_cell_num) {
  7366. set_error_buf(error_buf, error_buf_size,
  7367. "type mismatch: expect data but stack was empty");
  7368. return false;
  7369. }
  7370. #if WASM_ENABLE_GC == 0
  7371. for (i = 0; i < type_cell_num; i++) {
  7372. if (*(frame_ref - 1 - i) != type) {
  7373. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7374. "type mismatch: expect ", type2str(type),
  7375. " but got other");
  7376. return false;
  7377. }
  7378. }
  7379. #else
  7380. if (wasm_is_type_multi_byte_type(*(frame_ref - 1))) {
  7381. bh_assert(reftype_map_num > 0);
  7382. frame_reftype = (frame_reftype_map - 1)->ref_type;
  7383. }
  7384. if (!wasm_reftype_is_subtype_of(*(frame_ref - 1), frame_reftype, type,
  7385. ref_type, ctx->module->types,
  7386. ctx->module->type_count)) {
  7387. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7388. "type mismatch: expect ", type2str(type),
  7389. " but got other");
  7390. return false;
  7391. }
  7392. for (i = 0; i < type_cell_num - 1; i++) {
  7393. if (*(frame_ref - 2 - i) != *(frame_ref - 1)) {
  7394. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  7395. "type mismatch: expect ", type2str(type),
  7396. " but got other");
  7397. return false;
  7398. }
  7399. }
  7400. #endif
  7401. return true;
  7402. }
  7403. static bool
  7404. check_stack_pop(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7405. uint32 error_buf_size)
  7406. {
  7407. int32 block_stack_cell_num =
  7408. (int32)(ctx->stack_cell_num - (ctx->frame_csp - 1)->stack_cell_num);
  7409. #if WASM_ENABLE_GC != 0
  7410. int32 reftype_map_num =
  7411. (int32)(ctx->reftype_map_num - (ctx->frame_csp - 1)->reftype_map_num);
  7412. #endif
  7413. if (block_stack_cell_num > 0) {
  7414. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY)
  7415. /* the stack top is a value of any type, return success */
  7416. return true;
  7417. }
  7418. #if WASM_ENABLE_GC != 0
  7419. if (wasm_is_type_reftype(type) && block_stack_cell_num > 0) {
  7420. uint8 stack_top_type = *(ctx->frame_ref - 1);
  7421. WASMRefType *stack_top_ref_type = NULL;
  7422. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7423. bh_assert(reftype_map_num > 0);
  7424. stack_top_ref_type = (*(ctx->frame_reftype_map - 1)).ref_type;
  7425. }
  7426. if (wasm_reftype_is_subtype_of(stack_top_type, stack_top_ref_type, type,
  7427. ctx->ref_type_tmp, ctx->module->types,
  7428. ctx->module->type_count)) {
  7429. if (wasm_is_type_multi_byte_type(stack_top_type)) {
  7430. uint32 ref_type_struct_size =
  7431. wasm_reftype_struct_size(stack_top_ref_type);
  7432. bh_memcpy_s(ctx->ref_type_tmp, (uint32)sizeof(WASMRefType),
  7433. stack_top_ref_type, ref_type_struct_size);
  7434. }
  7435. return true;
  7436. }
  7437. }
  7438. #endif
  7439. if (!check_stack_top_values(ctx, ctx->frame_ref, block_stack_cell_num,
  7440. #if WASM_ENABLE_GC != 0
  7441. ctx->frame_reftype_map, reftype_map_num,
  7442. #endif
  7443. type,
  7444. #if WASM_ENABLE_GC != 0
  7445. ctx->ref_type_tmp,
  7446. #endif
  7447. error_buf, error_buf_size)) {
  7448. return false;
  7449. }
  7450. return true;
  7451. }
  7452. static bool
  7453. wasm_loader_pop_frame_ref(WASMLoaderContext *ctx, uint8 type, char *error_buf,
  7454. uint32 error_buf_size)
  7455. {
  7456. BranchBlock *cur_block = ctx->frame_csp - 1;
  7457. int32 available_stack_cell =
  7458. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7459. uint32 cell_num_to_pop = wasm_value_type_cell_num(type);
  7460. /* Directly return success if current block is in stack
  7461. polymorphic state while stack is empty. */
  7462. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  7463. return true;
  7464. if (type == VALUE_TYPE_VOID)
  7465. return true;
  7466. if (!check_stack_pop(ctx, type, error_buf, error_buf_size))
  7467. return false;
  7468. bh_assert(available_stack_cell > 0);
  7469. if (*(ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  7470. type = VALUE_TYPE_ANY;
  7471. cell_num_to_pop = 1;
  7472. }
  7473. ctx->frame_ref -= cell_num_to_pop;
  7474. ctx->stack_cell_num -= cell_num_to_pop;
  7475. #if WASM_ENABLE_GC != 0
  7476. if (wasm_is_type_multi_byte_type(*ctx->frame_ref)) {
  7477. ctx->frame_reftype_map--;
  7478. ctx->reftype_map_num--;
  7479. }
  7480. #endif
  7481. return true;
  7482. }
  7483. #if WASM_ENABLE_GC != 0
  7484. /* Get the stack top element of current block */
  7485. static bool
  7486. wasm_loader_get_frame_ref_top(WASMLoaderContext *ctx, uint8 *p_type,
  7487. WASMRefType **p_ref_type, char *error_buf,
  7488. uint32 error_buf_size)
  7489. {
  7490. BranchBlock *cur_block = ctx->frame_csp - 1;
  7491. int32 available_stack_cell =
  7492. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  7493. if (available_stack_cell <= 0) {
  7494. /* Directly return success if current block is in stack
  7495. polymorphic state while stack is empty. */
  7496. if (cur_block->is_stack_polymorphic) {
  7497. *p_type = VALUE_TYPE_ANY;
  7498. return true;
  7499. }
  7500. else {
  7501. set_error_buf(
  7502. error_buf, error_buf_size,
  7503. "type mismatch: expect data but block stack was empty");
  7504. return false;
  7505. }
  7506. }
  7507. *p_type = *(ctx->frame_ref - 1);
  7508. if (wasm_is_type_multi_byte_type(*p_type)) {
  7509. int32 available_reftype_map =
  7510. (int32)(ctx->reftype_map_num
  7511. - (ctx->frame_csp - 1)->reftype_map_num);
  7512. bh_assert(available_reftype_map > 0);
  7513. (void)available_reftype_map;
  7514. *p_ref_type = (ctx->frame_reftype_map - 1)->ref_type;
  7515. }
  7516. return true;
  7517. }
  7518. #if WASM_ENABLE_FAST_INTERP != 0
  7519. static bool
  7520. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  7521. char *error_buf, uint32 error_buf_size);
  7522. #endif
  7523. /* Check whether the stack top elem is a heap object, and if yes,
  7524. pop and return it */
  7525. static bool
  7526. wasm_loader_pop_heap_obj(WASMLoaderContext *ctx, uint8 *p_type,
  7527. WASMRefType *ref_ht_ret, char *error_buf,
  7528. uint32 error_buf_size)
  7529. {
  7530. uint8 type = 0;
  7531. WASMRefType *ref_type = NULL;
  7532. /* Get stack top element */
  7533. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7534. error_buf_size)) {
  7535. return false;
  7536. }
  7537. if (type != VALUE_TYPE_ANY /* block isn't in stack polymorphic state */
  7538. /* stack top isn't a ref type */
  7539. && !wasm_is_type_reftype(type)) {
  7540. set_error_buf(error_buf, error_buf_size,
  7541. "type mismatch: expect heap object but got others");
  7542. return false;
  7543. }
  7544. /* POP stack top */
  7545. if (wasm_is_type_multi_byte_type(type)) {
  7546. bh_assert(ref_type);
  7547. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7548. wasm_reftype_struct_size(ref_type));
  7549. }
  7550. #if WASM_ENABLE_FAST_INTERP != 0
  7551. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7552. error_buf_size)) {
  7553. return false;
  7554. }
  7555. #else
  7556. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7557. return false;
  7558. }
  7559. #endif
  7560. if (p_type)
  7561. *p_type = type;
  7562. if (wasm_is_type_multi_byte_type(type) && ref_ht_ret) {
  7563. bh_memcpy_s(ref_ht_ret, sizeof(WASMRefType), ref_type,
  7564. wasm_reftype_struct_size(ref_type));
  7565. }
  7566. return true;
  7567. }
  7568. /* Check whether the stack top elem is subtype of (ref null ht),
  7569. and if yes, pop it and return the converted (ref ht) */
  7570. static bool
  7571. wasm_loader_pop_nullable_ht(WASMLoaderContext *ctx, uint8 *p_type,
  7572. WASMRefType *ref_ht_ret, char *error_buf,
  7573. uint32 error_buf_size)
  7574. {
  7575. uint8 type = 0;
  7576. WASMRefType ref_type = { 0 };
  7577. if (!wasm_loader_pop_heap_obj(ctx, &type, &ref_type, error_buf,
  7578. error_buf_size)) {
  7579. return false;
  7580. }
  7581. /* Convert to related (ref ht) and return */
  7582. if (type >= REF_TYPE_ARRAYREF && type <= REF_TYPE_NULLFUNCREF) {
  7583. /* Return (ref array/struct/i31/eq/any/extern/func/none/noextern/nofunc)
  7584. */
  7585. wasm_set_refheaptype_common(&ref_ht_ret->ref_ht_common, false,
  7586. HEAP_TYPE_ARRAY
  7587. + (type - REF_TYPE_ARRAYREF));
  7588. type = ref_ht_ret->ref_type;
  7589. }
  7590. else if (wasm_is_reftype_htref_nullable(type)
  7591. || wasm_is_reftype_htref_non_nullable(type)) {
  7592. bh_memcpy_s(ref_ht_ret, (uint32)sizeof(WASMRefType), &ref_type,
  7593. wasm_reftype_struct_size(&ref_type));
  7594. /* Convert to (ref ht) */
  7595. ref_ht_ret->ref_ht_common.ref_type = REF_TYPE_HT_NON_NULLABLE;
  7596. ref_ht_ret->ref_ht_common.nullable = false;
  7597. type = ref_ht_ret->ref_type;
  7598. }
  7599. *p_type = type;
  7600. return true;
  7601. }
  7602. /* Check whether the stack top elem is (ref null $t) or (ref $t),
  7603. and if yes, pop it and return the type_idx */
  7604. static bool
  7605. wasm_loader_pop_nullable_typeidx(WASMLoaderContext *ctx, uint8 *p_type,
  7606. uint32 *p_type_idx, char *error_buf,
  7607. uint32 error_buf_size)
  7608. {
  7609. uint8 type = 0;
  7610. int32 type_idx = -1;
  7611. WASMRefType *ref_type = NULL;
  7612. /* Get stack top element */
  7613. if (!wasm_loader_get_frame_ref_top(ctx, &type, &ref_type, error_buf,
  7614. error_buf_size)) {
  7615. return false;
  7616. }
  7617. if (type != VALUE_TYPE_ANY) {
  7618. /* stack top isn't (ref null $t) */
  7619. if (!((wasm_is_reftype_htref_nullable(type)
  7620. || wasm_is_reftype_htref_non_nullable(type))
  7621. && wasm_is_refheaptype_typeidx(&ref_type->ref_ht_common))) {
  7622. set_error_buf(error_buf, error_buf_size,
  7623. "type mismatch: expect (ref null $t) but got others");
  7624. return false;
  7625. }
  7626. type_idx = ref_type->ref_ht_typeidx.type_idx;
  7627. bh_memcpy_s(ctx->ref_type_tmp, sizeof(WASMRefType), ref_type,
  7628. wasm_reftype_struct_size(ref_type));
  7629. }
  7630. /* POP stack top */
  7631. #if WASM_ENABLE_FAST_INTERP != 0
  7632. if (!wasm_loader_pop_frame_ref_offset(ctx, type, error_buf,
  7633. error_buf_size)) {
  7634. return false;
  7635. }
  7636. #else
  7637. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size)) {
  7638. return false;
  7639. }
  7640. #endif
  7641. /* Convert to type_idx and return */
  7642. *p_type = type;
  7643. if (type != VALUE_TYPE_ANY)
  7644. *p_type_idx = (uint32)type_idx;
  7645. return true;
  7646. }
  7647. #endif /* WASM_ENABLE_GC != 0 */
  7648. #if WASM_ENABLE_FAST_INTERP == 0
  7649. static bool
  7650. wasm_loader_push_pop_frame_ref(WASMLoaderContext *ctx, uint8 pop_cnt,
  7651. uint8 type_push, uint8 type_pop, char *error_buf,
  7652. uint32 error_buf_size)
  7653. {
  7654. for (int i = 0; i < pop_cnt; i++) {
  7655. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  7656. error_buf_size))
  7657. return false;
  7658. }
  7659. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  7660. return false;
  7661. return true;
  7662. }
  7663. #endif
  7664. static bool
  7665. wasm_loader_push_frame_csp(WASMLoaderContext *ctx, uint8 label_type,
  7666. BlockType block_type, uint8 *start_addr,
  7667. char *error_buf, uint32 error_buf_size)
  7668. {
  7669. CHECK_CSP_PUSH();
  7670. memset(ctx->frame_csp, 0, sizeof(BranchBlock));
  7671. ctx->frame_csp->label_type = label_type;
  7672. ctx->frame_csp->block_type = block_type;
  7673. ctx->frame_csp->start_addr = start_addr;
  7674. ctx->frame_csp->stack_cell_num = ctx->stack_cell_num;
  7675. #if WASM_ENABLE_GC != 0
  7676. ctx->frame_csp->reftype_map_num = ctx->reftype_map_num;
  7677. #endif
  7678. #if WASM_ENABLE_FAST_INTERP != 0
  7679. ctx->frame_csp->dynamic_offset = ctx->dynamic_offset;
  7680. ctx->frame_csp->patch_list = NULL;
  7681. #endif
  7682. ctx->frame_csp++;
  7683. ctx->csp_num++;
  7684. if (ctx->csp_num > ctx->max_csp_num) {
  7685. ctx->max_csp_num = ctx->csp_num;
  7686. if (ctx->max_csp_num > UINT16_MAX) {
  7687. set_error_buf(error_buf, error_buf_size,
  7688. "label stack depth limit exceeded");
  7689. return false;
  7690. }
  7691. }
  7692. return true;
  7693. fail:
  7694. return false;
  7695. }
  7696. static bool
  7697. wasm_loader_pop_frame_csp(WASMLoaderContext *ctx, char *error_buf,
  7698. uint32 error_buf_size)
  7699. {
  7700. CHECK_CSP_POP();
  7701. #if WASM_ENABLE_FAST_INTERP != 0
  7702. if ((ctx->frame_csp - 1)->param_frame_offsets)
  7703. wasm_runtime_free((ctx->frame_csp - 1)->param_frame_offsets);
  7704. #endif
  7705. ctx->frame_csp--;
  7706. ctx->csp_num--;
  7707. return true;
  7708. fail:
  7709. return false;
  7710. }
  7711. #if WASM_ENABLE_FAST_INTERP != 0
  7712. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  7713. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  7714. #define emit_label(opcode) \
  7715. do { \
  7716. wasm_loader_emit_ptr(loader_ctx, handle_table[opcode]); \
  7717. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7718. } while (0)
  7719. #define skip_label() \
  7720. do { \
  7721. wasm_loader_emit_backspace(loader_ctx, sizeof(void *)); \
  7722. LOG_OP("\ndelete last op\n"); \
  7723. } while (0)
  7724. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7725. #if UINTPTR_MAX == UINT64_MAX
  7726. #define emit_label(opcode) \
  7727. do { \
  7728. int32 offset = \
  7729. (int32)((uint8 *)handle_table[opcode] - (uint8 *)handle_table[0]); \
  7730. /* emit int32 relative offset in 64-bit target */ \
  7731. wasm_loader_emit_uint32(loader_ctx, offset); \
  7732. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7733. } while (0)
  7734. #else
  7735. #define emit_label(opcode) \
  7736. do { \
  7737. uint32 label_addr = (uint32)(uintptr_t)handle_table[opcode]; \
  7738. /* emit uint32 label address in 32-bit target */ \
  7739. wasm_loader_emit_uint32(loader_ctx, label_addr); \
  7740. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7741. } while (0)
  7742. #endif
  7743. #define skip_label() \
  7744. do { \
  7745. wasm_loader_emit_backspace(loader_ctx, sizeof(int32)); \
  7746. LOG_OP("\ndelete last op\n"); \
  7747. } while (0)
  7748. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  7749. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  7750. #define emit_label(opcode) \
  7751. do { \
  7752. wasm_loader_emit_uint8(loader_ctx, opcode); \
  7753. LOG_OP("\nemit_op [%02x]\t", opcode); \
  7754. } while (0)
  7755. #define skip_label() \
  7756. do { \
  7757. wasm_loader_emit_backspace(loader_ctx, sizeof(uint8)); \
  7758. LOG_OP("\ndelete last op\n"); \
  7759. } while (0)
  7760. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  7761. #define emit_empty_label_addr_and_frame_ip(type) \
  7762. do { \
  7763. if (!add_label_patch_to_list(loader_ctx->frame_csp - 1, type, \
  7764. loader_ctx->p_code_compiled, error_buf, \
  7765. error_buf_size)) \
  7766. goto fail; \
  7767. /* label address, to be patched */ \
  7768. wasm_loader_emit_ptr(loader_ctx, NULL); \
  7769. } while (0)
  7770. #define emit_br_info(frame_csp, is_br) \
  7771. do { \
  7772. if (!wasm_loader_emit_br_info(loader_ctx, frame_csp, is_br, error_buf, \
  7773. error_buf_size)) \
  7774. goto fail; \
  7775. } while (0)
  7776. #define LAST_OP_OUTPUT_I32() \
  7777. (last_op >= WASM_OP_I32_EQZ && last_op <= WASM_OP_I32_ROTR) \
  7778. || (last_op == WASM_OP_I32_LOAD || last_op == WASM_OP_F32_LOAD) \
  7779. || (last_op >= WASM_OP_I32_LOAD8_S && last_op <= WASM_OP_I32_LOAD16_U) \
  7780. || (last_op >= WASM_OP_F32_ABS && last_op <= WASM_OP_F32_COPYSIGN) \
  7781. || (last_op >= WASM_OP_I32_WRAP_I64 \
  7782. && last_op <= WASM_OP_I32_TRUNC_U_F64) \
  7783. || (last_op >= WASM_OP_F32_CONVERT_S_I32 \
  7784. && last_op <= WASM_OP_F32_DEMOTE_F64) \
  7785. || (last_op == WASM_OP_I32_REINTERPRET_F32) \
  7786. || (last_op == WASM_OP_F32_REINTERPRET_I32) \
  7787. || (last_op == EXT_OP_COPY_STACK_TOP)
  7788. #define LAST_OP_OUTPUT_I64() \
  7789. (last_op >= WASM_OP_I64_CLZ && last_op <= WASM_OP_I64_ROTR) \
  7790. || (last_op >= WASM_OP_F64_ABS && last_op <= WASM_OP_F64_COPYSIGN) \
  7791. || (last_op == WASM_OP_I64_LOAD || last_op == WASM_OP_F64_LOAD) \
  7792. || (last_op >= WASM_OP_I64_LOAD8_S && last_op <= WASM_OP_I64_LOAD32_U) \
  7793. || (last_op >= WASM_OP_I64_EXTEND_S_I32 \
  7794. && last_op <= WASM_OP_I64_TRUNC_U_F64) \
  7795. || (last_op >= WASM_OP_F64_CONVERT_S_I32 \
  7796. && last_op <= WASM_OP_F64_PROMOTE_F32) \
  7797. || (last_op == WASM_OP_I64_REINTERPRET_F64) \
  7798. || (last_op == WASM_OP_F64_REINTERPRET_I64) \
  7799. || (last_op == EXT_OP_COPY_STACK_TOP_I64)
  7800. #define GET_CONST_OFFSET(type, val) \
  7801. do { \
  7802. if (!(wasm_loader_get_const_offset(loader_ctx, type, &val, \
  7803. &operand_offset, error_buf, \
  7804. error_buf_size))) \
  7805. goto fail; \
  7806. } while (0)
  7807. #define GET_CONST_F32_OFFSET(type, fval) \
  7808. do { \
  7809. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7810. &operand_offset, error_buf, \
  7811. error_buf_size))) \
  7812. goto fail; \
  7813. } while (0)
  7814. #define GET_CONST_F64_OFFSET(type, fval) \
  7815. do { \
  7816. if (!(wasm_loader_get_const_offset(loader_ctx, type, &fval, \
  7817. &operand_offset, error_buf, \
  7818. error_buf_size))) \
  7819. goto fail; \
  7820. } while (0)
  7821. #define emit_operand(ctx, offset) \
  7822. do { \
  7823. wasm_loader_emit_int16(ctx, offset); \
  7824. LOG_OP("%d\t", offset); \
  7825. } while (0)
  7826. #define emit_byte(ctx, byte) \
  7827. do { \
  7828. wasm_loader_emit_uint8(ctx, byte); \
  7829. LOG_OP("%d\t", byte); \
  7830. } while (0)
  7831. #define emit_uint32(ctx, value) \
  7832. do { \
  7833. wasm_loader_emit_uint32(ctx, value); \
  7834. LOG_OP("%d\t", value); \
  7835. } while (0)
  7836. #define emit_uint64(ctx, value) \
  7837. do { \
  7838. wasm_loader_emit_const(ctx, &value, false); \
  7839. LOG_OP("%lld\t", value); \
  7840. } while (0)
  7841. #define emit_float32(ctx, value) \
  7842. do { \
  7843. wasm_loader_emit_const(ctx, &value, true); \
  7844. LOG_OP("%f\t", value); \
  7845. } while (0)
  7846. #define emit_float64(ctx, value) \
  7847. do { \
  7848. wasm_loader_emit_const(ctx, &value, false); \
  7849. LOG_OP("%f\t", value); \
  7850. } while (0)
  7851. static bool
  7852. wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
  7853. {
  7854. if (!(ctx->p_code_compiled =
  7855. loader_malloc(ctx->code_compiled_peak_size, NULL, 0)))
  7856. return false;
  7857. ctx->p_code_compiled_end =
  7858. ctx->p_code_compiled + ctx->code_compiled_peak_size;
  7859. /* clean up frame ref */
  7860. memset(ctx->frame_ref_bottom, 0, ctx->frame_ref_size);
  7861. ctx->frame_ref = ctx->frame_ref_bottom;
  7862. ctx->stack_cell_num = 0;
  7863. #if WASM_ENABLE_GC != 0
  7864. /* clean up reftype map */
  7865. memset(ctx->frame_reftype_map_bottom, 0, ctx->frame_reftype_map_size);
  7866. ctx->frame_reftype_map = ctx->frame_reftype_map_bottom;
  7867. ctx->reftype_map_num = 0;
  7868. #endif
  7869. /* clean up frame csp */
  7870. memset(ctx->frame_csp_bottom, 0, ctx->frame_csp_size);
  7871. ctx->frame_csp = ctx->frame_csp_bottom;
  7872. ctx->csp_num = 0;
  7873. ctx->max_csp_num = 0;
  7874. /* clean up frame offset */
  7875. memset(ctx->frame_offset_bottom, 0, ctx->frame_offset_size);
  7876. ctx->frame_offset = ctx->frame_offset_bottom;
  7877. ctx->dynamic_offset = ctx->start_dynamic_offset;
  7878. /* init preserved local offsets */
  7879. ctx->preserved_local_offset = ctx->max_dynamic_offset;
  7880. /* const buf is reserved */
  7881. return true;
  7882. }
  7883. static void
  7884. increase_compiled_code_space(WASMLoaderContext *ctx, int32 size)
  7885. {
  7886. ctx->code_compiled_size += size;
  7887. if (ctx->code_compiled_size >= ctx->code_compiled_peak_size) {
  7888. ctx->code_compiled_peak_size = ctx->code_compiled_size;
  7889. }
  7890. }
  7891. static void
  7892. wasm_loader_emit_const(WASMLoaderContext *ctx, void *value, bool is_32_bit)
  7893. {
  7894. uint32 size = is_32_bit ? sizeof(uint32) : sizeof(uint64);
  7895. if (ctx->p_code_compiled) {
  7896. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7897. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7898. #endif
  7899. bh_memcpy_s(ctx->p_code_compiled,
  7900. (uint32)(ctx->p_code_compiled_end - ctx->p_code_compiled),
  7901. value, size);
  7902. ctx->p_code_compiled += size;
  7903. }
  7904. else {
  7905. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7906. bh_assert((ctx->code_compiled_size & 1) == 0);
  7907. #endif
  7908. increase_compiled_code_space(ctx, size);
  7909. }
  7910. }
  7911. static void
  7912. wasm_loader_emit_uint32(WASMLoaderContext *ctx, uint32 value)
  7913. {
  7914. if (ctx->p_code_compiled) {
  7915. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7916. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7917. #endif
  7918. STORE_U32(ctx->p_code_compiled, value);
  7919. ctx->p_code_compiled += sizeof(uint32);
  7920. }
  7921. else {
  7922. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7923. bh_assert((ctx->code_compiled_size & 1) == 0);
  7924. #endif
  7925. increase_compiled_code_space(ctx, sizeof(uint32));
  7926. }
  7927. }
  7928. static void
  7929. wasm_loader_emit_int16(WASMLoaderContext *ctx, int16 value)
  7930. {
  7931. if (ctx->p_code_compiled) {
  7932. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7933. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7934. #endif
  7935. STORE_U16(ctx->p_code_compiled, (uint16)value);
  7936. ctx->p_code_compiled += sizeof(int16);
  7937. }
  7938. else {
  7939. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7940. bh_assert((ctx->code_compiled_size & 1) == 0);
  7941. #endif
  7942. increase_compiled_code_space(ctx, sizeof(uint16));
  7943. }
  7944. }
  7945. static void
  7946. wasm_loader_emit_uint8(WASMLoaderContext *ctx, uint8 value)
  7947. {
  7948. if (ctx->p_code_compiled) {
  7949. *(ctx->p_code_compiled) = value;
  7950. ctx->p_code_compiled += sizeof(uint8);
  7951. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7952. ctx->p_code_compiled++;
  7953. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7954. #endif
  7955. }
  7956. else {
  7957. increase_compiled_code_space(ctx, sizeof(uint8));
  7958. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7959. increase_compiled_code_space(ctx, sizeof(uint8));
  7960. bh_assert((ctx->code_compiled_size & 1) == 0);
  7961. #endif
  7962. }
  7963. }
  7964. static void
  7965. wasm_loader_emit_ptr(WASMLoaderContext *ctx, void *value)
  7966. {
  7967. if (ctx->p_code_compiled) {
  7968. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7969. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7970. #endif
  7971. STORE_PTR(ctx->p_code_compiled, value);
  7972. ctx->p_code_compiled += sizeof(void *);
  7973. }
  7974. else {
  7975. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7976. bh_assert((ctx->code_compiled_size & 1) == 0);
  7977. #endif
  7978. increase_compiled_code_space(ctx, sizeof(void *));
  7979. }
  7980. }
  7981. static void
  7982. wasm_loader_emit_backspace(WASMLoaderContext *ctx, uint32 size)
  7983. {
  7984. if (ctx->p_code_compiled) {
  7985. ctx->p_code_compiled -= size;
  7986. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7987. if (size == sizeof(uint8)) {
  7988. ctx->p_code_compiled--;
  7989. bh_assert(((uintptr_t)ctx->p_code_compiled & 1) == 0);
  7990. }
  7991. #endif
  7992. }
  7993. else {
  7994. ctx->code_compiled_size -= size;
  7995. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  7996. if (size == sizeof(uint8)) {
  7997. ctx->code_compiled_size--;
  7998. bh_assert((ctx->code_compiled_size & 1) == 0);
  7999. }
  8000. #endif
  8001. }
  8002. }
  8003. static bool
  8004. preserve_referenced_local(WASMLoaderContext *loader_ctx, uint8 opcode,
  8005. uint32 local_index, uint32 local_type,
  8006. bool *preserved, char *error_buf,
  8007. uint32 error_buf_size)
  8008. {
  8009. uint32 i = 0;
  8010. int16 preserved_offset = (int16)local_index;
  8011. *preserved = false;
  8012. while (i < loader_ctx->stack_cell_num) {
  8013. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8014. /* move previous local into dynamic space before a set/tee_local opcode
  8015. */
  8016. if (loader_ctx->frame_offset_bottom[i] == (int16)local_index) {
  8017. if (!(*preserved)) {
  8018. *preserved = true;
  8019. skip_label();
  8020. preserved_offset = loader_ctx->preserved_local_offset;
  8021. if (loader_ctx->p_code_compiled) {
  8022. bh_assert(preserved_offset != (int16)local_index);
  8023. }
  8024. if (is_32bit_type(local_type)) {
  8025. /* Only increase preserve offset in the second traversal */
  8026. if (loader_ctx->p_code_compiled)
  8027. loader_ctx->preserved_local_offset++;
  8028. emit_label(EXT_OP_COPY_STACK_TOP);
  8029. }
  8030. else {
  8031. if (loader_ctx->p_code_compiled)
  8032. loader_ctx->preserved_local_offset += 2;
  8033. emit_label(EXT_OP_COPY_STACK_TOP_I64);
  8034. }
  8035. emit_operand(loader_ctx, local_index);
  8036. emit_operand(loader_ctx, preserved_offset);
  8037. emit_label(opcode);
  8038. }
  8039. loader_ctx->frame_offset_bottom[i] = preserved_offset;
  8040. }
  8041. if (is_32bit_type(cur_type))
  8042. i++;
  8043. else
  8044. i += 2;
  8045. }
  8046. (void)error_buf;
  8047. (void)error_buf_size;
  8048. return true;
  8049. }
  8050. static bool
  8051. preserve_local_for_block(WASMLoaderContext *loader_ctx, uint8 opcode,
  8052. char *error_buf, uint32 error_buf_size)
  8053. {
  8054. uint32 i = 0;
  8055. bool preserve_local;
  8056. /* preserve locals before blocks to ensure that "tee/set_local" inside
  8057. blocks will not influence the value of these locals */
  8058. while (i < loader_ctx->stack_cell_num) {
  8059. int16 cur_offset = loader_ctx->frame_offset_bottom[i];
  8060. uint8 cur_type = loader_ctx->frame_ref_bottom[i];
  8061. if ((cur_offset < loader_ctx->start_dynamic_offset)
  8062. && (cur_offset >= 0)) {
  8063. if (!(preserve_referenced_local(loader_ctx, opcode, cur_offset,
  8064. cur_type, &preserve_local,
  8065. error_buf, error_buf_size)))
  8066. return false;
  8067. }
  8068. if (is_32bit_type(cur_type)) {
  8069. i++;
  8070. }
  8071. else {
  8072. i += 2;
  8073. }
  8074. }
  8075. return true;
  8076. }
  8077. static bool
  8078. add_label_patch_to_list(BranchBlock *frame_csp, uint8 patch_type,
  8079. uint8 *p_code_compiled, char *error_buf,
  8080. uint32 error_buf_size)
  8081. {
  8082. BranchBlockPatch *patch =
  8083. loader_malloc(sizeof(BranchBlockPatch), error_buf, error_buf_size);
  8084. if (!patch) {
  8085. return false;
  8086. }
  8087. patch->patch_type = patch_type;
  8088. patch->code_compiled = p_code_compiled;
  8089. if (!frame_csp->patch_list) {
  8090. frame_csp->patch_list = patch;
  8091. patch->next = NULL;
  8092. }
  8093. else {
  8094. patch->next = frame_csp->patch_list;
  8095. frame_csp->patch_list = patch;
  8096. }
  8097. return true;
  8098. }
  8099. static void
  8100. apply_label_patch(WASMLoaderContext *ctx, uint8 depth, uint8 patch_type)
  8101. {
  8102. BranchBlock *frame_csp = ctx->frame_csp - depth;
  8103. BranchBlockPatch *node = frame_csp->patch_list;
  8104. BranchBlockPatch *node_prev = NULL, *node_next;
  8105. if (!ctx->p_code_compiled)
  8106. return;
  8107. while (node) {
  8108. node_next = node->next;
  8109. if (node->patch_type == patch_type) {
  8110. STORE_PTR(node->code_compiled, ctx->p_code_compiled);
  8111. if (node_prev == NULL) {
  8112. frame_csp->patch_list = node_next;
  8113. }
  8114. else {
  8115. node_prev->next = node_next;
  8116. }
  8117. wasm_runtime_free(node);
  8118. }
  8119. else {
  8120. node_prev = node;
  8121. }
  8122. node = node_next;
  8123. }
  8124. }
  8125. static bool
  8126. wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
  8127. bool is_br, char *error_buf, uint32 error_buf_size)
  8128. {
  8129. /* br info layout:
  8130. * a) arity of target block
  8131. * b) total cell num of arity values
  8132. * c) each arity value's cell num
  8133. * d) each arity value's src frame offset
  8134. * e) each arity values's dst dynamic offset
  8135. * f) branch target address
  8136. *
  8137. * Note: b-e are omitted when arity is 0 so that
  8138. * interpreter can recover the br info quickly.
  8139. */
  8140. BlockType *block_type = &frame_csp->block_type;
  8141. uint8 *types = NULL, cell;
  8142. #if WASM_ENABLE_GC != 0
  8143. WASMRefTypeMap *reftype_maps;
  8144. uint32 reftype_map_count;
  8145. #endif
  8146. uint32 arity = 0;
  8147. int32 i;
  8148. int16 *frame_offset = ctx->frame_offset;
  8149. uint16 dynamic_offset;
  8150. /* Note: loop's arity is different from if and block. loop's arity is
  8151. * its parameter count while if and block arity is result count.
  8152. */
  8153. #if WASM_ENABLE_GC == 0
  8154. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8155. arity = block_type_get_param_types(block_type, &types);
  8156. else
  8157. arity = block_type_get_result_types(block_type, &types);
  8158. #else
  8159. if (frame_csp->label_type == LABEL_TYPE_LOOP)
  8160. arity = block_type_get_param_types(block_type, &types, &reftype_maps,
  8161. &reftype_map_count);
  8162. else
  8163. arity = block_type_get_result_types(block_type, &types, &reftype_maps,
  8164. &reftype_map_count);
  8165. #endif
  8166. /* Part a */
  8167. emit_uint32(ctx, arity);
  8168. if (arity) {
  8169. /* Part b */
  8170. emit_uint32(ctx, wasm_get_cell_num(types, arity));
  8171. /* Part c */
  8172. for (i = (int32)arity - 1; i >= 0; i--) {
  8173. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8174. emit_byte(ctx, cell);
  8175. }
  8176. /* Part d */
  8177. for (i = (int32)arity - 1; i >= 0; i--) {
  8178. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8179. frame_offset -= cell;
  8180. emit_operand(ctx, *(int16 *)(frame_offset));
  8181. }
  8182. /* Part e */
  8183. dynamic_offset =
  8184. frame_csp->dynamic_offset + wasm_get_cell_num(types, arity);
  8185. if (is_br)
  8186. ctx->dynamic_offset = dynamic_offset;
  8187. for (i = (int32)arity - 1; i >= 0; i--) {
  8188. cell = (uint8)wasm_value_type_cell_num(types[i]);
  8189. dynamic_offset -= cell;
  8190. emit_operand(ctx, dynamic_offset);
  8191. }
  8192. }
  8193. /* Part f */
  8194. if (frame_csp->label_type == LABEL_TYPE_LOOP) {
  8195. wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
  8196. }
  8197. else {
  8198. if (!add_label_patch_to_list(frame_csp, PATCH_END, ctx->p_code_compiled,
  8199. error_buf, error_buf_size))
  8200. return false;
  8201. /* label address, to be patched */
  8202. wasm_loader_emit_ptr(ctx, NULL);
  8203. }
  8204. return true;
  8205. }
  8206. static bool
  8207. wasm_loader_push_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8208. bool disable_emit, int16 operand_offset,
  8209. char *error_buf, uint32 error_buf_size)
  8210. {
  8211. if (type == VALUE_TYPE_VOID)
  8212. return true;
  8213. /* only check memory overflow in first traverse */
  8214. if (ctx->p_code_compiled == NULL) {
  8215. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8216. return false;
  8217. }
  8218. if (disable_emit)
  8219. *(ctx->frame_offset)++ = operand_offset;
  8220. else {
  8221. emit_operand(ctx, ctx->dynamic_offset);
  8222. *(ctx->frame_offset)++ = ctx->dynamic_offset;
  8223. ctx->dynamic_offset++;
  8224. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8225. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8226. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8227. goto fail;
  8228. }
  8229. }
  8230. }
  8231. if (is_32bit_type(type))
  8232. return true;
  8233. if (ctx->p_code_compiled == NULL) {
  8234. if (!check_offset_push(ctx, error_buf, error_buf_size))
  8235. return false;
  8236. }
  8237. ctx->frame_offset++;
  8238. if (!disable_emit) {
  8239. ctx->dynamic_offset++;
  8240. if (ctx->dynamic_offset > ctx->max_dynamic_offset) {
  8241. ctx->max_dynamic_offset = ctx->dynamic_offset;
  8242. if (ctx->max_dynamic_offset >= INT16_MAX) {
  8243. goto fail;
  8244. }
  8245. }
  8246. }
  8247. return true;
  8248. fail:
  8249. set_error_buf(error_buf, error_buf_size,
  8250. "fast interpreter offset overflow");
  8251. return false;
  8252. }
  8253. /* This function should be in front of wasm_loader_pop_frame_ref
  8254. as they both use ctx->stack_cell_num, and ctx->stack_cell_num
  8255. will be modified by wasm_loader_pop_frame_ref */
  8256. static bool
  8257. wasm_loader_pop_frame_offset(WASMLoaderContext *ctx, uint8 type,
  8258. char *error_buf, uint32 error_buf_size)
  8259. {
  8260. /* if ctx->frame_csp equals ctx->frame_csp_bottom,
  8261. then current block is the function block */
  8262. uint32 depth = ctx->frame_csp > ctx->frame_csp_bottom ? 1 : 0;
  8263. BranchBlock *cur_block = ctx->frame_csp - depth;
  8264. int32 available_stack_cell =
  8265. (int32)(ctx->stack_cell_num - cur_block->stack_cell_num);
  8266. /* Directly return success if current block is in stack
  8267. * polymorphic state while stack is empty. */
  8268. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  8269. return true;
  8270. if (type == VALUE_TYPE_VOID)
  8271. return true;
  8272. if (is_32bit_type(type)) {
  8273. /* Check the offset stack bottom to ensure the frame offset
  8274. stack will not go underflow. But we don't thrown error
  8275. and return true here, because the error msg should be
  8276. given in wasm_loader_pop_frame_ref */
  8277. if (!check_offset_pop(ctx, 1))
  8278. return true;
  8279. ctx->frame_offset -= 1;
  8280. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8281. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8282. ctx->dynamic_offset -= 1;
  8283. }
  8284. else {
  8285. if (!check_offset_pop(ctx, 2))
  8286. return true;
  8287. ctx->frame_offset -= 2;
  8288. if ((*(ctx->frame_offset) > ctx->start_dynamic_offset)
  8289. && (*(ctx->frame_offset) < ctx->max_dynamic_offset))
  8290. ctx->dynamic_offset -= 2;
  8291. }
  8292. emit_operand(ctx, *(ctx->frame_offset));
  8293. (void)error_buf;
  8294. (void)error_buf_size;
  8295. return true;
  8296. }
  8297. static bool
  8298. wasm_loader_push_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8299. bool disable_emit, int16 operand_offset,
  8300. char *error_buf, uint32 error_buf_size)
  8301. {
  8302. if (!(wasm_loader_push_frame_offset(ctx, type, disable_emit, operand_offset,
  8303. error_buf, error_buf_size)))
  8304. return false;
  8305. if (!(wasm_loader_push_frame_ref(ctx, type, error_buf, error_buf_size)))
  8306. return false;
  8307. return true;
  8308. }
  8309. static bool
  8310. wasm_loader_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 type,
  8311. char *error_buf, uint32 error_buf_size)
  8312. {
  8313. /* put wasm_loader_pop_frame_offset in front of wasm_loader_pop_frame_ref */
  8314. if (!wasm_loader_pop_frame_offset(ctx, type, error_buf, error_buf_size))
  8315. return false;
  8316. if (!wasm_loader_pop_frame_ref(ctx, type, error_buf, error_buf_size))
  8317. return false;
  8318. return true;
  8319. }
  8320. static bool
  8321. wasm_loader_push_pop_frame_ref_offset(WASMLoaderContext *ctx, uint8 pop_cnt,
  8322. uint8 type_push, uint8 type_pop,
  8323. bool disable_emit, int16 operand_offset,
  8324. char *error_buf, uint32 error_buf_size)
  8325. {
  8326. uint8 i;
  8327. for (i = 0; i < pop_cnt; i++) {
  8328. if (!wasm_loader_pop_frame_offset(ctx, type_pop, error_buf,
  8329. error_buf_size))
  8330. return false;
  8331. if (!wasm_loader_pop_frame_ref(ctx, type_pop, error_buf,
  8332. error_buf_size))
  8333. return false;
  8334. }
  8335. if (!wasm_loader_push_frame_offset(ctx, type_push, disable_emit,
  8336. operand_offset, error_buf,
  8337. error_buf_size))
  8338. return false;
  8339. if (!wasm_loader_push_frame_ref(ctx, type_push, error_buf, error_buf_size))
  8340. return false;
  8341. return true;
  8342. }
  8343. static bool
  8344. wasm_loader_get_const_offset(WASMLoaderContext *ctx, uint8 type, void *value,
  8345. int16 *offset, char *error_buf,
  8346. uint32 error_buf_size)
  8347. {
  8348. int8 bytes_to_increase;
  8349. int16 operand_offset = 0;
  8350. Const *c;
  8351. /* Search existing constant */
  8352. for (c = (Const *)ctx->const_buf;
  8353. (uint8 *)c < ctx->const_buf + ctx->num_const * sizeof(Const); c++) {
  8354. /* TODO: handle v128 type? */
  8355. if ((type == c->value_type)
  8356. && ((type == VALUE_TYPE_I64 && *(int64 *)value == c->value.i64)
  8357. || (type == VALUE_TYPE_I32 && *(int32 *)value == c->value.i32)
  8358. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8359. || (type == VALUE_TYPE_FUNCREF
  8360. && *(int32 *)value == c->value.i32)
  8361. || (type == VALUE_TYPE_EXTERNREF
  8362. && *(int32 *)value == c->value.i32)
  8363. #endif
  8364. || (type == VALUE_TYPE_F64
  8365. && (0 == memcmp(value, &(c->value.f64), sizeof(float64))))
  8366. || (type == VALUE_TYPE_F32
  8367. && (0
  8368. == memcmp(value, &(c->value.f32), sizeof(float32)))))) {
  8369. operand_offset = c->slot_index;
  8370. break;
  8371. }
  8372. if (is_32bit_type(c->value_type))
  8373. operand_offset += 1;
  8374. else
  8375. operand_offset += 2;
  8376. }
  8377. if ((uint8 *)c == ctx->const_buf + ctx->num_const * sizeof(Const)) {
  8378. /* New constant, append to the const buffer */
  8379. if ((type == VALUE_TYPE_F64) || (type == VALUE_TYPE_I64)) {
  8380. bytes_to_increase = 2;
  8381. }
  8382. else {
  8383. bytes_to_increase = 1;
  8384. }
  8385. /* The max cell num of const buffer is 32768 since the valid index range
  8386. * is -32768 ~ -1. Return an invalid index 0 to indicate the buffer is
  8387. * full */
  8388. if (ctx->const_cell_num > INT16_MAX - bytes_to_increase + 1) {
  8389. *offset = 0;
  8390. return true;
  8391. }
  8392. if ((uint8 *)c == ctx->const_buf + ctx->const_buf_size) {
  8393. MEM_REALLOC(ctx->const_buf, ctx->const_buf_size,
  8394. ctx->const_buf_size + 4 * sizeof(Const));
  8395. ctx->const_buf_size += 4 * sizeof(Const);
  8396. c = (Const *)(ctx->const_buf + ctx->num_const * sizeof(Const));
  8397. }
  8398. c->value_type = type;
  8399. switch (type) {
  8400. case VALUE_TYPE_F64:
  8401. bh_memcpy_s(&(c->value.f64), sizeof(WASMValue), value,
  8402. sizeof(float64));
  8403. ctx->const_cell_num += 2;
  8404. /* The const buf will be reversed, we use the second cell */
  8405. /* of the i64/f64 const so the final offset is correct */
  8406. operand_offset++;
  8407. break;
  8408. case VALUE_TYPE_I64:
  8409. c->value.i64 = *(int64 *)value;
  8410. ctx->const_cell_num += 2;
  8411. operand_offset++;
  8412. break;
  8413. case VALUE_TYPE_F32:
  8414. bh_memcpy_s(&(c->value.f32), sizeof(WASMValue), value,
  8415. sizeof(float32));
  8416. ctx->const_cell_num++;
  8417. break;
  8418. case VALUE_TYPE_I32:
  8419. c->value.i32 = *(int32 *)value;
  8420. ctx->const_cell_num++;
  8421. break;
  8422. #if WASM_ENABLE_REF_TYPES != 0 && WASM_ENABLE_GC == 0
  8423. case VALUE_TYPE_EXTERNREF:
  8424. case VALUE_TYPE_FUNCREF:
  8425. c->value.i32 = *(int32 *)value;
  8426. ctx->const_cell_num++;
  8427. break;
  8428. #endif
  8429. default:
  8430. break;
  8431. }
  8432. c->slot_index = operand_offset;
  8433. ctx->num_const++;
  8434. LOG_OP("#### new const [%d]: %ld\n", ctx->num_const,
  8435. (int64)c->value.i64);
  8436. }
  8437. /* use negative index for const */
  8438. operand_offset = -(operand_offset + 1);
  8439. *offset = operand_offset;
  8440. return true;
  8441. fail:
  8442. return false;
  8443. }
  8444. /*
  8445. PUSH(POP)_XXX = push(pop) frame_ref + push(pop) frame_offset
  8446. -- Mostly used for the binary / compare operation
  8447. PUSH(POP)_OFFSET_TYPE only push(pop) the frame_offset stack
  8448. -- Mostly used in block / control instructions
  8449. The POP will always emit the offset on the top of the frame_offset stack
  8450. PUSH can be used in two ways:
  8451. 1. directly PUSH:
  8452. PUSH_XXX();
  8453. will allocate a dynamic space and emit
  8454. 2. silent PUSH:
  8455. operand_offset = xxx; disable_emit = true;
  8456. PUSH_XXX();
  8457. only push the frame_offset stack, no emit
  8458. */
  8459. #define TEMPLATE_PUSH(Type) \
  8460. do { \
  8461. if (!wasm_loader_push_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8462. disable_emit, operand_offset, \
  8463. error_buf, error_buf_size)) \
  8464. goto fail; \
  8465. } while (0)
  8466. #define TEMPLATE_PUSH_REF(Type) \
  8467. do { \
  8468. if (!wasm_loader_push_frame_ref_offset(loader_ctx, Type, disable_emit, \
  8469. operand_offset, error_buf, \
  8470. error_buf_size)) \
  8471. goto fail; \
  8472. } while (0)
  8473. #define TEMPLATE_POP(Type) \
  8474. do { \
  8475. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, VALUE_TYPE_##Type, \
  8476. error_buf, error_buf_size)) \
  8477. goto fail; \
  8478. } while (0)
  8479. #define TEMPLATE_POP_REF(Type) \
  8480. do { \
  8481. if (!wasm_loader_pop_frame_ref_offset(loader_ctx, Type, error_buf, \
  8482. error_buf_size)) \
  8483. goto fail; \
  8484. } while (0)
  8485. #define PUSH_OFFSET_TYPE(type) \
  8486. do { \
  8487. if (!(wasm_loader_push_frame_offset(loader_ctx, type, disable_emit, \
  8488. operand_offset, error_buf, \
  8489. error_buf_size))) \
  8490. goto fail; \
  8491. } while (0)
  8492. #define POP_OFFSET_TYPE(type) \
  8493. do { \
  8494. if (!(wasm_loader_pop_frame_offset(loader_ctx, type, error_buf, \
  8495. error_buf_size))) \
  8496. goto fail; \
  8497. } while (0)
  8498. #define POP_AND_PUSH(type_pop, type_push) \
  8499. do { \
  8500. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8501. loader_ctx, 1, type_push, type_pop, disable_emit, \
  8502. operand_offset, error_buf, error_buf_size))) \
  8503. goto fail; \
  8504. } while (0)
  8505. /* type of POPs should be the same */
  8506. #define POP2_AND_PUSH(type_pop, type_push) \
  8507. do { \
  8508. if (!(wasm_loader_push_pop_frame_ref_offset( \
  8509. loader_ctx, 2, type_push, type_pop, disable_emit, \
  8510. operand_offset, error_buf, error_buf_size))) \
  8511. goto fail; \
  8512. } while (0)
  8513. #else /* WASM_ENABLE_FAST_INTERP */
  8514. #define TEMPLATE_PUSH(Type) \
  8515. do { \
  8516. if (!(wasm_loader_push_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8517. error_buf, error_buf_size))) \
  8518. goto fail; \
  8519. } while (0)
  8520. #define TEMPLATE_PUSH_REF(Type) \
  8521. do { \
  8522. if (!(wasm_loader_push_frame_ref(loader_ctx, Type, error_buf, \
  8523. error_buf_size))) \
  8524. goto fail; \
  8525. } while (0)
  8526. #define TEMPLATE_POP(Type) \
  8527. do { \
  8528. if (!(wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_##Type, \
  8529. error_buf, error_buf_size))) \
  8530. goto fail; \
  8531. } while (0)
  8532. #define TEMPLATE_POP_REF(Type) \
  8533. do { \
  8534. if (!(wasm_loader_pop_frame_ref(loader_ctx, Type, error_buf, \
  8535. error_buf_size))) \
  8536. goto fail; \
  8537. } while (0)
  8538. #define POP_AND_PUSH(type_pop, type_push) \
  8539. do { \
  8540. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 1, type_push, \
  8541. type_pop, error_buf, \
  8542. error_buf_size))) \
  8543. goto fail; \
  8544. } while (0)
  8545. /* type of POPs should be the same */
  8546. #define POP2_AND_PUSH(type_pop, type_push) \
  8547. do { \
  8548. if (!(wasm_loader_push_pop_frame_ref(loader_ctx, 2, type_push, \
  8549. type_pop, error_buf, \
  8550. error_buf_size))) \
  8551. goto fail; \
  8552. } while (0)
  8553. #endif /* WASM_ENABLE_FAST_INTERP */
  8554. #define PUSH_I32() TEMPLATE_PUSH(I32)
  8555. #define PUSH_F32() TEMPLATE_PUSH(F32)
  8556. #define PUSH_I64() TEMPLATE_PUSH(I64)
  8557. #define PUSH_F64() TEMPLATE_PUSH(F64)
  8558. #define PUSH_V128() TEMPLATE_PUSH(V128)
  8559. #define PUSH_FUNCREF() TEMPLATE_PUSH(FUNCREF)
  8560. #define PUSH_EXTERNREF() TEMPLATE_PUSH(EXTERNREF)
  8561. #define PUSH_REF(Type) TEMPLATE_PUSH_REF(Type)
  8562. #define POP_REF(Type) TEMPLATE_POP_REF(Type)
  8563. #define PUSH_MEM_OFFSET() TEMPLATE_PUSH_REF(mem_offset_type)
  8564. #define PUSH_PAGE_COUNT() PUSH_MEM_OFFSET()
  8565. #define POP_I32() TEMPLATE_POP(I32)
  8566. #define POP_F32() TEMPLATE_POP(F32)
  8567. #define POP_I64() TEMPLATE_POP(I64)
  8568. #define POP_F64() TEMPLATE_POP(F64)
  8569. #define POP_V128() TEMPLATE_POP(V128)
  8570. #define POP_FUNCREF() TEMPLATE_POP(FUNCREF)
  8571. #define POP_EXTERNREF() TEMPLATE_POP(EXTERNREF)
  8572. #define POP_STRINGREF() TEMPLATE_POP(STRINGREF)
  8573. #define POP_MEM_OFFSET() TEMPLATE_POP_REF(mem_offset_type)
  8574. #if WASM_ENABLE_FAST_INTERP != 0
  8575. static bool
  8576. reserve_block_ret(WASMLoaderContext *loader_ctx, uint8 opcode,
  8577. bool disable_emit, char *error_buf, uint32 error_buf_size)
  8578. {
  8579. int16 operand_offset = 0;
  8580. BranchBlock *block = (opcode == WASM_OP_ELSE) ? loader_ctx->frame_csp - 1
  8581. : loader_ctx->frame_csp;
  8582. BlockType *block_type = &block->block_type;
  8583. uint8 *return_types = NULL;
  8584. #if WASM_ENABLE_GC != 0
  8585. WASMRefTypeMap *reftype_maps = NULL;
  8586. uint32 reftype_map_count;
  8587. #endif
  8588. uint32 return_count = 0, value_count = 0, total_cel_num = 0;
  8589. int32 i = 0;
  8590. int16 dynamic_offset, dynamic_offset_org, *frame_offset = NULL,
  8591. *frame_offset_org = NULL;
  8592. #if WASM_ENABLE_GC == 0
  8593. return_count = block_type_get_result_types(block_type, &return_types);
  8594. #else
  8595. return_count = block_type_get_result_types(
  8596. block_type, &return_types, &reftype_maps, &reftype_map_count);
  8597. #endif
  8598. /* If there is only one return value, use EXT_OP_COPY_STACK_TOP/_I64 instead
  8599. * of EXT_OP_COPY_STACK_VALUES for interpreter performance. */
  8600. if (return_count == 1) {
  8601. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[0]);
  8602. if (cell <= 2 /* V128 isn't supported whose cell num is 4 */
  8603. && block->dynamic_offset != *(loader_ctx->frame_offset - cell)) {
  8604. /* insert op_copy before else opcode */
  8605. if (opcode == WASM_OP_ELSE)
  8606. skip_label();
  8607. emit_label(cell == 1 ? EXT_OP_COPY_STACK_TOP
  8608. : EXT_OP_COPY_STACK_TOP_I64);
  8609. emit_operand(loader_ctx, *(loader_ctx->frame_offset - cell));
  8610. emit_operand(loader_ctx, block->dynamic_offset);
  8611. if (opcode == WASM_OP_ELSE) {
  8612. *(loader_ctx->frame_offset - cell) = block->dynamic_offset;
  8613. }
  8614. else {
  8615. loader_ctx->frame_offset -= cell;
  8616. loader_ctx->dynamic_offset = block->dynamic_offset;
  8617. PUSH_OFFSET_TYPE(return_types[0]);
  8618. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8619. }
  8620. if (opcode == WASM_OP_ELSE)
  8621. emit_label(opcode);
  8622. }
  8623. return true;
  8624. }
  8625. /* Copy stack top values to block's results which are in dynamic space.
  8626. * The instruction format:
  8627. * Part a: values count
  8628. * Part b: all values total cell num
  8629. * Part c: each value's cell_num, src offset and dst offset
  8630. * Part d: each value's src offset and dst offset
  8631. * Part e: each value's dst offset
  8632. */
  8633. frame_offset = frame_offset_org = loader_ctx->frame_offset;
  8634. dynamic_offset = dynamic_offset_org =
  8635. block->dynamic_offset + wasm_get_cell_num(return_types, return_count);
  8636. /* First traversal to get the count of values needed to be copied. */
  8637. for (i = (int32)return_count - 1; i >= 0; i--) {
  8638. uint8 cells = (uint8)wasm_value_type_cell_num(return_types[i]);
  8639. frame_offset -= cells;
  8640. dynamic_offset -= cells;
  8641. if (dynamic_offset != *frame_offset) {
  8642. value_count++;
  8643. total_cel_num += cells;
  8644. }
  8645. }
  8646. if (value_count) {
  8647. uint32 j = 0;
  8648. uint8 *emit_data = NULL, *cells = NULL;
  8649. int16 *src_offsets = NULL;
  8650. uint16 *dst_offsets = NULL;
  8651. uint64 size =
  8652. (uint64)value_count
  8653. * (sizeof(*cells) + sizeof(*src_offsets) + sizeof(*dst_offsets));
  8654. /* Allocate memory for the emit data */
  8655. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  8656. return false;
  8657. cells = emit_data;
  8658. src_offsets = (int16 *)(cells + value_count);
  8659. dst_offsets = (uint16 *)(src_offsets + value_count);
  8660. /* insert op_copy before else opcode */
  8661. if (opcode == WASM_OP_ELSE)
  8662. skip_label();
  8663. emit_label(EXT_OP_COPY_STACK_VALUES);
  8664. /* Part a) */
  8665. emit_uint32(loader_ctx, value_count);
  8666. /* Part b) */
  8667. emit_uint32(loader_ctx, total_cel_num);
  8668. /* Second traversal to get each value's cell num, src offset and dst
  8669. * offset. */
  8670. frame_offset = frame_offset_org;
  8671. dynamic_offset = dynamic_offset_org;
  8672. for (i = (int32)return_count - 1, j = 0; i >= 0; i--) {
  8673. uint8 cell = (uint8)wasm_value_type_cell_num(return_types[i]);
  8674. frame_offset -= cell;
  8675. dynamic_offset -= cell;
  8676. if (dynamic_offset != *frame_offset) {
  8677. /* cell num */
  8678. cells[j] = cell;
  8679. /* src offset */
  8680. src_offsets[j] = *frame_offset;
  8681. /* dst offset */
  8682. dst_offsets[j] = dynamic_offset;
  8683. j++;
  8684. }
  8685. if (opcode == WASM_OP_ELSE) {
  8686. *frame_offset = dynamic_offset;
  8687. }
  8688. else {
  8689. loader_ctx->frame_offset = frame_offset;
  8690. loader_ctx->dynamic_offset = dynamic_offset;
  8691. PUSH_OFFSET_TYPE(return_types[i]);
  8692. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  8693. loader_ctx->frame_offset = frame_offset_org;
  8694. loader_ctx->dynamic_offset = dynamic_offset_org;
  8695. }
  8696. }
  8697. bh_assert(j == value_count);
  8698. /* Emit the cells, src_offsets and dst_offsets */
  8699. for (j = 0; j < value_count; j++)
  8700. emit_byte(loader_ctx, cells[j]);
  8701. for (j = 0; j < value_count; j++)
  8702. emit_operand(loader_ctx, src_offsets[j]);
  8703. for (j = 0; j < value_count; j++)
  8704. emit_operand(loader_ctx, dst_offsets[j]);
  8705. if (opcode == WASM_OP_ELSE)
  8706. emit_label(opcode);
  8707. wasm_runtime_free(emit_data);
  8708. }
  8709. return true;
  8710. fail:
  8711. return false;
  8712. }
  8713. #endif /* WASM_ENABLE_FAST_INTERP */
  8714. #define RESERVE_BLOCK_RET() \
  8715. do { \
  8716. if (!reserve_block_ret(loader_ctx, opcode, disable_emit, error_buf, \
  8717. error_buf_size)) \
  8718. goto fail; \
  8719. } while (0)
  8720. #define PUSH_TYPE(type) \
  8721. do { \
  8722. if (!(wasm_loader_push_frame_ref(loader_ctx, type, error_buf, \
  8723. error_buf_size))) \
  8724. goto fail; \
  8725. } while (0)
  8726. #define POP_TYPE(type) \
  8727. do { \
  8728. if (!(wasm_loader_pop_frame_ref(loader_ctx, type, error_buf, \
  8729. error_buf_size))) \
  8730. goto fail; \
  8731. } while (0)
  8732. #if WASM_ENABLE_GC == 0
  8733. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8734. do { \
  8735. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8736. _start_addr, error_buf, \
  8737. error_buf_size)) \
  8738. goto fail; \
  8739. } while (0)
  8740. #define POP_CSP() \
  8741. do { \
  8742. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8743. goto fail; \
  8744. } while (0)
  8745. #else
  8746. #define PUSH_CSP(label_type, block_type, _start_addr) \
  8747. do { \
  8748. if (!wasm_loader_push_frame_csp(loader_ctx, label_type, block_type, \
  8749. _start_addr, error_buf, \
  8750. error_buf_size)) \
  8751. goto fail; \
  8752. if (!wasm_loader_init_local_use_masks(loader_ctx, local_count, \
  8753. error_buf, error_buf_size)) { \
  8754. goto fail; \
  8755. } \
  8756. } while (0)
  8757. #define POP_CSP() \
  8758. do { \
  8759. wasm_loader_destroy_curr_local_use_masks(loader_ctx); \
  8760. if (!wasm_loader_pop_frame_csp(loader_ctx, error_buf, error_buf_size)) \
  8761. goto fail; \
  8762. } while (0)
  8763. #endif /* end of WASM_ENABLE_GC == 0 */
  8764. #if WASM_ENABLE_GC == 0
  8765. #define GET_LOCAL_REFTYPE() (void)0
  8766. #else
  8767. #define GET_LOCAL_REFTYPE() \
  8768. do { \
  8769. if (wasm_is_type_multi_byte_type(local_type)) { \
  8770. WASMRefType *_ref_type; \
  8771. if (local_idx < param_count) \
  8772. _ref_type = wasm_reftype_map_find( \
  8773. param_reftype_maps, param_reftype_map_count, local_idx); \
  8774. else \
  8775. _ref_type = wasm_reftype_map_find(local_reftype_maps, \
  8776. local_reftype_map_count, \
  8777. local_idx - param_count); \
  8778. bh_assert(_ref_type); \
  8779. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), _ref_type, \
  8780. wasm_reftype_struct_size(_ref_type)); \
  8781. } \
  8782. } while (0)
  8783. #endif /* end of WASM_ENABLE_GC == 0 */
  8784. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  8785. do { \
  8786. read_leb_uint32(p, p_end, local_idx); \
  8787. if (local_idx >= param_count + local_count) { \
  8788. set_error_buf(error_buf, error_buf_size, "unknown local"); \
  8789. goto fail; \
  8790. } \
  8791. local_type = local_idx < param_count \
  8792. ? param_types[local_idx] \
  8793. : local_types[local_idx - param_count]; \
  8794. local_offset = local_offsets[local_idx]; \
  8795. GET_LOCAL_REFTYPE(); \
  8796. } while (0)
  8797. static bool
  8798. check_memory(WASMModule *module, char *error_buf, uint32 error_buf_size)
  8799. {
  8800. if (module->memory_count == 0 && module->import_memory_count == 0) {
  8801. set_error_buf(error_buf, error_buf_size, "unknown memory");
  8802. return false;
  8803. }
  8804. return true;
  8805. }
  8806. #define CHECK_MEMORY() \
  8807. do { \
  8808. if (!check_memory(module, error_buf, error_buf_size)) \
  8809. goto fail; \
  8810. } while (0)
  8811. static bool
  8812. check_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8813. uint32 error_buf_size)
  8814. {
  8815. uint8 mem_access_aligns[] = {
  8816. 2, 3, 2, 3, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, /* loads */
  8817. 2, 3, 2, 3, 0, 1, 0, 1, 2 /* stores */
  8818. };
  8819. bh_assert(opcode >= WASM_OP_I32_LOAD && opcode <= WASM_OP_I64_STORE32);
  8820. if (align > mem_access_aligns[opcode - WASM_OP_I32_LOAD]) {
  8821. set_error_buf(error_buf, error_buf_size,
  8822. "alignment must not be larger than natural");
  8823. return false;
  8824. }
  8825. return true;
  8826. }
  8827. #if WASM_ENABLE_SIMD != 0
  8828. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  8829. static bool
  8830. check_simd_memory_access_align(uint8 opcode, uint32 align, char *error_buf,
  8831. uint32 error_buf_size)
  8832. {
  8833. uint8 mem_access_aligns[] = {
  8834. 4, /* load */
  8835. 3, 3, 3, 3, 3, 3, /* load and extend */
  8836. 0, 1, 2, 3, /* load and splat */
  8837. 4, /* store */
  8838. };
  8839. uint8 mem_access_aligns_load_lane[] = {
  8840. 0, 1, 2, 3, /* load lane */
  8841. 0, 1, 2, 3, /* store lane */
  8842. 2, 3 /* store zero */
  8843. };
  8844. if (!((opcode <= SIMD_v128_store)
  8845. || (SIMD_v128_load8_lane <= opcode
  8846. && opcode <= SIMD_v128_load64_zero))) {
  8847. set_error_buf(error_buf, error_buf_size,
  8848. "the opcode doesn't include memarg");
  8849. return false;
  8850. }
  8851. if ((opcode <= SIMD_v128_store
  8852. && align > mem_access_aligns[opcode - SIMD_v128_load])
  8853. || (SIMD_v128_load8_lane <= opcode && opcode <= SIMD_v128_load64_zero
  8854. && align > mem_access_aligns_load_lane[opcode
  8855. - SIMD_v128_load8_lane])) {
  8856. set_error_buf(error_buf, error_buf_size,
  8857. "alignment must not be larger than natural");
  8858. return false;
  8859. }
  8860. return true;
  8861. }
  8862. static bool
  8863. check_simd_access_lane(uint8 opcode, uint8 lane, char *error_buf,
  8864. uint32 error_buf_size)
  8865. {
  8866. switch (opcode) {
  8867. case SIMD_i8x16_extract_lane_s:
  8868. case SIMD_i8x16_extract_lane_u:
  8869. case SIMD_i8x16_replace_lane:
  8870. if (lane >= 16) {
  8871. goto fail;
  8872. }
  8873. break;
  8874. case SIMD_i16x8_extract_lane_s:
  8875. case SIMD_i16x8_extract_lane_u:
  8876. case SIMD_i16x8_replace_lane:
  8877. if (lane >= 8) {
  8878. goto fail;
  8879. }
  8880. break;
  8881. case SIMD_i32x4_extract_lane:
  8882. case SIMD_i32x4_replace_lane:
  8883. case SIMD_f32x4_extract_lane:
  8884. case SIMD_f32x4_replace_lane:
  8885. if (lane >= 4) {
  8886. goto fail;
  8887. }
  8888. break;
  8889. case SIMD_i64x2_extract_lane:
  8890. case SIMD_i64x2_replace_lane:
  8891. case SIMD_f64x2_extract_lane:
  8892. case SIMD_f64x2_replace_lane:
  8893. if (lane >= 2) {
  8894. goto fail;
  8895. }
  8896. break;
  8897. case SIMD_v128_load8_lane:
  8898. case SIMD_v128_load16_lane:
  8899. case SIMD_v128_load32_lane:
  8900. case SIMD_v128_load64_lane:
  8901. case SIMD_v128_store8_lane:
  8902. case SIMD_v128_store16_lane:
  8903. case SIMD_v128_store32_lane:
  8904. case SIMD_v128_store64_lane:
  8905. case SIMD_v128_load32_zero:
  8906. case SIMD_v128_load64_zero:
  8907. {
  8908. uint8 max_lanes[] = { 16, 8, 4, 2, 16, 8, 4, 2, 4, 2 };
  8909. if (lane >= max_lanes[opcode - SIMD_v128_load8_lane]) {
  8910. goto fail;
  8911. }
  8912. break;
  8913. }
  8914. default:
  8915. goto fail;
  8916. }
  8917. return true;
  8918. fail:
  8919. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8920. return false;
  8921. }
  8922. static bool
  8923. check_simd_shuffle_mask(V128 mask, char *error_buf, uint32 error_buf_size)
  8924. {
  8925. uint8 i;
  8926. for (i = 0; i != 16; ++i) {
  8927. if (mask.i8x16[i] < 0 || mask.i8x16[i] >= 32) {
  8928. set_error_buf(error_buf, error_buf_size, "invalid lane index");
  8929. return false;
  8930. }
  8931. }
  8932. return true;
  8933. }
  8934. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  8935. #endif /* end of WASM_ENABLE_SIMD */
  8936. #if WASM_ENABLE_SHARED_MEMORY != 0
  8937. static bool
  8938. check_memory_align_equal(uint8 opcode, uint32 align, char *error_buf,
  8939. uint32 error_buf_size)
  8940. {
  8941. uint8 wait_notify_aligns[] = { 2, 2, 3 };
  8942. uint8 mem_access_aligns[] = {
  8943. 2, 3, 0, 1, 0, 1, 2,
  8944. };
  8945. uint8 expect;
  8946. bh_assert((opcode <= WASM_OP_ATOMIC_WAIT64)
  8947. || (opcode >= WASM_OP_ATOMIC_I32_LOAD
  8948. && opcode <= WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U));
  8949. if (opcode <= WASM_OP_ATOMIC_WAIT64) {
  8950. expect = wait_notify_aligns[opcode - WASM_OP_ATOMIC_NOTIFY];
  8951. }
  8952. else {
  8953. /* 7 opcodes in every group */
  8954. expect = mem_access_aligns[(opcode - WASM_OP_ATOMIC_I32_LOAD) % 7];
  8955. }
  8956. if (align != expect) {
  8957. set_error_buf(error_buf, error_buf_size,
  8958. "alignment isn't equal to natural");
  8959. return false;
  8960. }
  8961. return true;
  8962. }
  8963. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  8964. static bool
  8965. wasm_loader_check_br(WASMLoaderContext *loader_ctx, uint32 depth, uint8 opcode,
  8966. char *error_buf, uint32 error_buf_size)
  8967. {
  8968. BranchBlock *target_block, *cur_block;
  8969. BlockType *target_block_type;
  8970. uint8 type, *types = NULL, *frame_ref;
  8971. uint32 arity = 0;
  8972. int32 i, available_stack_cell;
  8973. uint16 cell_num;
  8974. #if WASM_ENABLE_GC != 0
  8975. WASMRefTypeMap *frame_reftype_map;
  8976. WASMRefTypeMap *reftype_maps = NULL, *reftype_map = NULL;
  8977. WASMRefType *ref_type;
  8978. uint32 reftype_map_count = 0;
  8979. int32 available_reftype_map;
  8980. bool is_type_multi_byte;
  8981. #endif
  8982. uint8 *frame_ref_old = loader_ctx->frame_ref;
  8983. uint8 *frame_ref_after_popped = NULL;
  8984. uint8 frame_ref_tmp[4] = { 0 };
  8985. uint8 *frame_ref_buf = frame_ref_tmp;
  8986. uint32 stack_cell_num_old = loader_ctx->stack_cell_num;
  8987. #if WASM_ENABLE_GC != 0
  8988. WASMRefTypeMap *frame_reftype_map_old = loader_ctx->frame_reftype_map;
  8989. WASMRefTypeMap *frame_reftype_map_after_popped = NULL;
  8990. WASMRefTypeMap frame_reftype_map_tmp[4] = { 0 };
  8991. WASMRefTypeMap *frame_reftype_map_buf = frame_reftype_map_tmp;
  8992. uint32 reftype_map_num_old = loader_ctx->reftype_map_num;
  8993. #endif
  8994. #if WASM_ENABLE_FAST_INTERP != 0
  8995. int16 *frame_offset_old = loader_ctx->frame_offset;
  8996. int16 *frame_offset_after_popped = NULL;
  8997. int16 frame_offset_tmp[4] = { 0 };
  8998. int16 *frame_offset_buf = frame_offset_tmp;
  8999. uint16 dynamic_offset_old = (loader_ctx->frame_csp - 1)->dynamic_offset;
  9000. #endif
  9001. bool ret = false;
  9002. bh_assert(loader_ctx->csp_num > 0);
  9003. if (loader_ctx->csp_num - 1 < depth) {
  9004. set_error_buf(error_buf, error_buf_size,
  9005. "unknown label, "
  9006. "unexpected end of section or function");
  9007. return false;
  9008. }
  9009. cur_block = loader_ctx->frame_csp - 1;
  9010. target_block = loader_ctx->frame_csp - (depth + 1);
  9011. target_block_type = &target_block->block_type;
  9012. frame_ref = loader_ctx->frame_ref;
  9013. #if WASM_ENABLE_GC != 0
  9014. frame_reftype_map = loader_ctx->frame_reftype_map;
  9015. #endif
  9016. /* Note: loop's arity is different from if and block. loop's arity is
  9017. * its parameter count while if and block arity is result count.
  9018. */
  9019. #if WASM_ENABLE_GC == 0
  9020. if (target_block->label_type == LABEL_TYPE_LOOP)
  9021. arity = block_type_get_param_types(target_block_type, &types);
  9022. else
  9023. arity = block_type_get_result_types(target_block_type, &types);
  9024. #else
  9025. if (target_block->label_type == LABEL_TYPE_LOOP)
  9026. arity = block_type_get_param_types(target_block_type, &types,
  9027. &reftype_maps, &reftype_map_count);
  9028. else
  9029. arity = block_type_get_result_types(target_block_type, &types,
  9030. &reftype_maps, &reftype_map_count);
  9031. #endif
  9032. /* If the stack is in polymorphic state, just clear the stack
  9033. * and then re-push the values to make the stack top values
  9034. * match block type. */
  9035. if (cur_block->is_stack_polymorphic) {
  9036. #if WASM_ENABLE_GC != 0
  9037. int32 j = reftype_map_count - 1;
  9038. #endif
  9039. for (i = (int32)arity - 1; i >= 0; i--) {
  9040. #if WASM_ENABLE_GC != 0
  9041. if (wasm_is_type_multi_byte_type(types[i])) {
  9042. bh_assert(reftype_maps[j].index == i);
  9043. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9044. reftype_maps[j].ref_type,
  9045. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9046. j--;
  9047. }
  9048. #endif
  9049. #if WASM_ENABLE_FAST_INTERP != 0
  9050. POP_OFFSET_TYPE(types[i]);
  9051. #endif
  9052. POP_TYPE(types[i]);
  9053. }
  9054. /* Backup stack data since it may be changed in the below
  9055. push operations, and the stack data may be used when
  9056. checking other target blocks of opcode br_table */
  9057. if (opcode == WASM_OP_BR_TABLE) {
  9058. uint64 total_size;
  9059. frame_ref_after_popped = loader_ctx->frame_ref;
  9060. total_size = (uint64)sizeof(uint8)
  9061. * (frame_ref_old - frame_ref_after_popped);
  9062. if (total_size > sizeof(frame_ref_tmp)
  9063. && !(frame_ref_buf = loader_malloc(total_size, error_buf,
  9064. error_buf_size))) {
  9065. goto fail;
  9066. }
  9067. bh_memcpy_s(frame_ref_buf, (uint32)total_size,
  9068. frame_ref_after_popped, (uint32)total_size);
  9069. #if WASM_ENABLE_GC != 0
  9070. frame_reftype_map_after_popped = loader_ctx->frame_reftype_map;
  9071. total_size =
  9072. (uint64)sizeof(WASMRefTypeMap)
  9073. * (frame_reftype_map_old - frame_reftype_map_after_popped);
  9074. if (total_size > sizeof(frame_reftype_map_tmp)
  9075. && !(frame_reftype_map_buf = loader_malloc(
  9076. total_size, error_buf, error_buf_size))) {
  9077. goto fail;
  9078. }
  9079. bh_memcpy_s(frame_reftype_map_buf, (uint32)total_size,
  9080. frame_reftype_map_after_popped, (uint32)total_size);
  9081. #endif
  9082. #if WASM_ENABLE_FAST_INTERP != 0
  9083. frame_offset_after_popped = loader_ctx->frame_offset;
  9084. total_size = (uint64)sizeof(int16)
  9085. * (frame_offset_old - frame_offset_after_popped);
  9086. if (total_size > sizeof(frame_offset_tmp)
  9087. && !(frame_offset_buf = loader_malloc(total_size, error_buf,
  9088. error_buf_size))) {
  9089. goto fail;
  9090. }
  9091. bh_memcpy_s(frame_offset_buf, (uint32)total_size,
  9092. frame_offset_after_popped, (uint32)total_size);
  9093. #endif
  9094. }
  9095. #if WASM_ENABLE_GC != 0
  9096. j = 0;
  9097. #endif
  9098. for (i = 0; i < (int32)arity; i++) {
  9099. #if WASM_ENABLE_GC != 0
  9100. if (wasm_is_type_multi_byte_type(types[i])) {
  9101. bh_assert(reftype_maps[j].index == i);
  9102. bh_memcpy_s(loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9103. reftype_maps[j].ref_type,
  9104. wasm_reftype_struct_size(reftype_maps[j].ref_type));
  9105. j++;
  9106. }
  9107. #endif
  9108. #if WASM_ENABLE_FAST_INTERP != 0
  9109. bool disable_emit = true;
  9110. int16 operand_offset = 0;
  9111. PUSH_OFFSET_TYPE(types[i]);
  9112. #endif
  9113. PUSH_TYPE(types[i]);
  9114. }
  9115. #if WASM_ENABLE_FAST_INTERP != 0
  9116. emit_br_info(target_block, opcode == WASM_OP_BR);
  9117. #endif
  9118. /* Restore the stack data, note that frame_ref_bottom,
  9119. frame_reftype_map_bottom, frame_offset_bottom may be
  9120. re-allocated in the above push operations */
  9121. if (opcode == WASM_OP_BR_TABLE) {
  9122. uint32 total_size;
  9123. /* The stack operand num should not be smaller than before
  9124. after pop and push operations */
  9125. bh_assert(loader_ctx->stack_cell_num >= stack_cell_num_old);
  9126. loader_ctx->stack_cell_num = stack_cell_num_old;
  9127. loader_ctx->frame_ref =
  9128. loader_ctx->frame_ref_bottom + stack_cell_num_old;
  9129. total_size = (uint32)(sizeof(uint8)
  9130. * (frame_ref_old - frame_ref_after_popped));
  9131. bh_memcpy_s((uint8 *)loader_ctx->frame_ref - total_size, total_size,
  9132. frame_ref_buf, total_size);
  9133. #if WASM_ENABLE_GC != 0
  9134. /* The stack operand num should not be smaller than before
  9135. after pop and push operations */
  9136. bh_assert(loader_ctx->reftype_map_num >= reftype_map_num_old);
  9137. loader_ctx->reftype_map_num = reftype_map_num_old;
  9138. loader_ctx->frame_reftype_map =
  9139. loader_ctx->frame_reftype_map_bottom + reftype_map_num_old;
  9140. total_size = (uint32)(sizeof(WASMRefTypeMap)
  9141. * (frame_reftype_map_old
  9142. - frame_reftype_map_after_popped));
  9143. bh_memcpy_s((uint8 *)loader_ctx->frame_reftype_map - total_size,
  9144. total_size, frame_reftype_map_buf, total_size);
  9145. #endif
  9146. #if WASM_ENABLE_FAST_INTERP != 0
  9147. loader_ctx->frame_offset =
  9148. loader_ctx->frame_offset_bottom + stack_cell_num_old;
  9149. total_size =
  9150. (uint32)(sizeof(int16)
  9151. * (frame_offset_old - frame_offset_after_popped));
  9152. bh_memcpy_s((uint8 *)loader_ctx->frame_offset - total_size,
  9153. total_size, frame_offset_buf, total_size);
  9154. (loader_ctx->frame_csp - 1)->dynamic_offset = dynamic_offset_old;
  9155. #endif
  9156. }
  9157. ret = true;
  9158. goto cleanup_and_return;
  9159. }
  9160. available_stack_cell =
  9161. (int32)(loader_ctx->stack_cell_num - cur_block->stack_cell_num);
  9162. #if WASM_ENABLE_GC != 0
  9163. available_reftype_map =
  9164. (int32)(loader_ctx->reftype_map_num
  9165. - (loader_ctx->frame_csp - 1)->reftype_map_num);
  9166. reftype_map = reftype_maps ? reftype_maps + reftype_map_count - 1 : NULL;
  9167. #endif
  9168. /* Check stack top values match target block type */
  9169. for (i = (int32)arity - 1; i >= 0; i--) {
  9170. type = types[i];
  9171. #if WASM_ENABLE_GC != 0
  9172. ref_type = NULL;
  9173. is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9174. if (is_type_multi_byte) {
  9175. bh_assert(reftype_map);
  9176. ref_type = reftype_map->ref_type;
  9177. }
  9178. #endif
  9179. if (available_stack_cell <= 0 && cur_block->is_stack_polymorphic)
  9180. break;
  9181. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9182. #if WASM_ENABLE_GC != 0
  9183. frame_reftype_map, available_reftype_map,
  9184. #endif
  9185. type,
  9186. #if WASM_ENABLE_GC != 0
  9187. ref_type,
  9188. #endif
  9189. error_buf, error_buf_size)) {
  9190. goto fail;
  9191. }
  9192. cell_num = wasm_value_type_cell_num(types[i]);
  9193. frame_ref -= cell_num;
  9194. available_stack_cell -= cell_num;
  9195. #if WASM_ENABLE_GC != 0
  9196. if (is_type_multi_byte) {
  9197. frame_reftype_map--;
  9198. available_reftype_map--;
  9199. reftype_map--;
  9200. }
  9201. #endif
  9202. }
  9203. #if WASM_ENABLE_FAST_INTERP != 0
  9204. emit_br_info(target_block, opcode == WASM_OP_BR);
  9205. #endif
  9206. ret = true;
  9207. cleanup_and_return:
  9208. fail:
  9209. if (frame_ref_buf && frame_ref_buf != frame_ref_tmp)
  9210. wasm_runtime_free(frame_ref_buf);
  9211. #if WASM_ENABLE_GC != 0
  9212. if (frame_reftype_map_buf && frame_reftype_map_buf != frame_reftype_map_tmp)
  9213. wasm_runtime_free(frame_reftype_map_buf);
  9214. #endif
  9215. #if WASM_ENABLE_FAST_INTERP != 0
  9216. if (frame_offset_buf && frame_offset_buf != frame_offset_tmp)
  9217. wasm_runtime_free(frame_offset_buf);
  9218. #endif
  9219. return ret;
  9220. }
  9221. static BranchBlock *
  9222. check_branch_block(WASMLoaderContext *loader_ctx, uint8 **p_buf, uint8 *buf_end,
  9223. uint8 opcode, char *error_buf, uint32 error_buf_size)
  9224. {
  9225. uint8 *p = *p_buf, *p_end = buf_end;
  9226. BranchBlock *frame_csp_tmp;
  9227. uint32 depth;
  9228. read_leb_uint32(p, p_end, depth);
  9229. if (!wasm_loader_check_br(loader_ctx, depth, opcode, error_buf,
  9230. error_buf_size)) {
  9231. goto fail;
  9232. }
  9233. frame_csp_tmp = loader_ctx->frame_csp - depth - 1;
  9234. *p_buf = p;
  9235. return frame_csp_tmp;
  9236. fail:
  9237. return NULL;
  9238. }
  9239. #if WASM_ENABLE_EXCE_HANDLING != 0
  9240. static BranchBlock *
  9241. check_branch_block_for_delegate(WASMLoaderContext *loader_ctx, uint8 **p_buf,
  9242. uint8 *buf_end, char *error_buf,
  9243. uint32 error_buf_size)
  9244. {
  9245. uint8 *p = *p_buf, *p_end = buf_end;
  9246. BranchBlock *frame_csp_tmp;
  9247. uint32 depth;
  9248. read_leb_uint32(p, p_end, depth);
  9249. /*
  9250. * Note: "delegate 0" means the surrounding block, not the
  9251. * try-delegate block itself.
  9252. *
  9253. * Note: the caller hasn't popped the try-delegate frame yet.
  9254. */
  9255. bh_assert(loader_ctx->csp_num > 0);
  9256. if (loader_ctx->csp_num - 1 <= depth) {
  9257. #if WASM_ENABLE_SPEC_TEST == 0
  9258. set_error_buf(error_buf, error_buf_size, "unknown delegate label");
  9259. #else
  9260. set_error_buf(error_buf, error_buf_size, "unknown label");
  9261. #endif
  9262. goto fail;
  9263. }
  9264. frame_csp_tmp = loader_ctx->frame_csp - depth - 2;
  9265. #if WASM_ENABLE_FAST_INTERP != 0
  9266. emit_br_info(frame_csp_tmp, false);
  9267. #endif
  9268. *p_buf = p;
  9269. return frame_csp_tmp;
  9270. fail:
  9271. return NULL;
  9272. }
  9273. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  9274. static bool
  9275. check_block_stack(WASMLoaderContext *loader_ctx, BranchBlock *block,
  9276. char *error_buf, uint32 error_buf_size)
  9277. {
  9278. BlockType *block_type = &block->block_type;
  9279. uint8 *return_types = NULL;
  9280. uint32 return_count = 0;
  9281. int32 available_stack_cell, return_cell_num, i;
  9282. uint8 *frame_ref = NULL;
  9283. #if WASM_ENABLE_GC != 0
  9284. WASMRefTypeMap *frame_reftype_map;
  9285. WASMRefTypeMap *return_reftype_maps = NULL, *return_reftype_map;
  9286. WASMRefType *ref_type;
  9287. uint32 param_count, return_reftype_map_count = 0;
  9288. int32 available_reftype_map =
  9289. (int32)(loader_ctx->reftype_map_num - block->reftype_map_num);
  9290. #endif
  9291. available_stack_cell =
  9292. (int32)(loader_ctx->stack_cell_num - block->stack_cell_num);
  9293. #if WASM_ENABLE_GC == 0
  9294. return_count = block_type_get_result_types(block_type, &return_types);
  9295. #else
  9296. return_count = block_type_get_result_types(block_type, &return_types,
  9297. &return_reftype_maps,
  9298. &return_reftype_map_count);
  9299. param_count =
  9300. block_type->is_value_type ? 0 : block_type->u.type->param_count;
  9301. (void)param_count;
  9302. #endif
  9303. return_cell_num =
  9304. return_count > 0 ? wasm_get_cell_num(return_types, return_count) : 0;
  9305. /* If the stack is in polymorphic state, just clear the stack
  9306. * and then re-push the values to make the stack top values
  9307. * match block type. */
  9308. if (block->is_stack_polymorphic) {
  9309. #if WASM_ENABLE_GC != 0
  9310. int32 j = return_reftype_map_count - 1;
  9311. #endif
  9312. for (i = (int32)return_count - 1; i >= 0; i--) {
  9313. #if WASM_ENABLE_GC != 0
  9314. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9315. bh_assert(return_reftype_maps[j].index == i + param_count);
  9316. bh_memcpy_s(
  9317. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9318. return_reftype_maps[j].ref_type,
  9319. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9320. j--;
  9321. }
  9322. #endif
  9323. #if WASM_ENABLE_FAST_INTERP != 0
  9324. POP_OFFSET_TYPE(return_types[i]);
  9325. #endif
  9326. POP_TYPE(return_types[i]);
  9327. }
  9328. /* Check stack is empty */
  9329. if (loader_ctx->stack_cell_num != block->stack_cell_num) {
  9330. set_error_buf(
  9331. error_buf, error_buf_size,
  9332. "type mismatch: stack size does not match block type");
  9333. goto fail;
  9334. }
  9335. #if WASM_ENABLE_GC != 0
  9336. j = 0;
  9337. #endif
  9338. for (i = 0; i < (int32)return_count; i++) {
  9339. #if WASM_ENABLE_GC != 0
  9340. if (wasm_is_type_multi_byte_type(return_types[i])) {
  9341. bh_assert(return_reftype_maps[j].index == i + param_count);
  9342. bh_memcpy_s(
  9343. loader_ctx->ref_type_tmp, sizeof(WASMRefType),
  9344. return_reftype_maps[j].ref_type,
  9345. wasm_reftype_struct_size(return_reftype_maps[j].ref_type));
  9346. j++;
  9347. }
  9348. #endif
  9349. #if WASM_ENABLE_FAST_INTERP != 0
  9350. bool disable_emit = true;
  9351. int16 operand_offset = 0;
  9352. PUSH_OFFSET_TYPE(return_types[i]);
  9353. #endif
  9354. PUSH_TYPE(return_types[i]);
  9355. }
  9356. return true;
  9357. }
  9358. if (available_stack_cell != return_cell_num) {
  9359. #if WASM_ENABLE_EXCE_HANDLING != 0
  9360. /* testspec: this error message format is expected by try_catch.wast */
  9361. snprintf(
  9362. error_buf, error_buf_size, "type mismatch: %s requires [%s]%s[%s]",
  9363. block->label_type == LABEL_TYPE_TRY
  9364. || (block->label_type == LABEL_TYPE_CATCH
  9365. && return_cell_num > 0)
  9366. ? "instruction"
  9367. : "block",
  9368. return_cell_num > 0 ? type2str(return_types[0]) : "",
  9369. " but stack has ",
  9370. available_stack_cell > 0 ? type2str(*(loader_ctx->frame_ref - 1))
  9371. : "");
  9372. goto fail;
  9373. #else
  9374. set_error_buf(error_buf, error_buf_size,
  9375. "type mismatch: stack size does not match block type");
  9376. goto fail;
  9377. #endif
  9378. }
  9379. /* Check stack values match return types */
  9380. frame_ref = loader_ctx->frame_ref;
  9381. #if WASM_ENABLE_GC != 0
  9382. frame_reftype_map = loader_ctx->frame_reftype_map;
  9383. return_reftype_map =
  9384. return_reftype_map_count
  9385. ? return_reftype_maps + return_reftype_map_count - 1
  9386. : NULL;
  9387. #endif
  9388. for (i = (int32)return_count - 1; i >= 0; i--) {
  9389. uint8 type = return_types[i];
  9390. #if WASM_ENABLE_GC != 0
  9391. bool is_type_multi_byte = wasm_is_type_multi_byte_type(type);
  9392. ref_type = NULL;
  9393. if (is_type_multi_byte) {
  9394. bh_assert(return_reftype_map);
  9395. ref_type = return_reftype_map->ref_type;
  9396. }
  9397. #endif
  9398. if (!check_stack_top_values(loader_ctx, frame_ref, available_stack_cell,
  9399. #if WASM_ENABLE_GC != 0
  9400. frame_reftype_map, available_reftype_map,
  9401. #endif
  9402. type,
  9403. #if WASM_ENABLE_GC != 0
  9404. ref_type,
  9405. #endif
  9406. error_buf, error_buf_size))
  9407. return false;
  9408. frame_ref -= wasm_value_type_cell_num(return_types[i]);
  9409. available_stack_cell -= wasm_value_type_cell_num(return_types[i]);
  9410. #if WASM_ENABLE_GC != 0
  9411. if (is_type_multi_byte) {
  9412. frame_reftype_map--;
  9413. available_reftype_map--;
  9414. return_reftype_map--;
  9415. }
  9416. #endif
  9417. }
  9418. return true;
  9419. fail:
  9420. return false;
  9421. }
  9422. #if WASM_ENABLE_FAST_INTERP != 0
  9423. /* Copy parameters to dynamic space.
  9424. * 1) POP original parameter out;
  9425. * 2) Push and copy original values to dynamic space.
  9426. * The copy instruction format:
  9427. * Part a: param count
  9428. * Part b: all param total cell num
  9429. * Part c: each param's cell_num, src offset and dst offset
  9430. * Part d: each param's src offset
  9431. * Part e: each param's dst offset
  9432. */
  9433. static bool
  9434. copy_params_to_dynamic_space(WASMLoaderContext *loader_ctx, bool is_if_block,
  9435. char *error_buf, uint32 error_buf_size)
  9436. {
  9437. bool ret = false;
  9438. int16 *frame_offset = NULL;
  9439. uint8 *cells = NULL, cell;
  9440. int16 *src_offsets = NULL;
  9441. uint8 *emit_data = NULL;
  9442. uint32 i;
  9443. BranchBlock *block = loader_ctx->frame_csp - 1;
  9444. BlockType *block_type = &block->block_type;
  9445. WASMFuncType *wasm_type = block_type->u.type;
  9446. uint32 param_count = block_type->u.type->param_count;
  9447. int16 condition_offset = 0;
  9448. bool disable_emit = false;
  9449. int16 operand_offset = 0;
  9450. uint64 size = (uint64)param_count * (sizeof(*cells) + sizeof(*src_offsets));
  9451. bh_assert(size > 0);
  9452. /* For if block, we also need copy the condition operand offset. */
  9453. if (is_if_block)
  9454. size += sizeof(*cells) + sizeof(*src_offsets);
  9455. /* Allocate memory for the emit data */
  9456. if (!(emit_data = loader_malloc(size, error_buf, error_buf_size)))
  9457. return false;
  9458. cells = emit_data;
  9459. src_offsets = (int16 *)(cells + param_count);
  9460. if (is_if_block)
  9461. condition_offset = *loader_ctx->frame_offset;
  9462. /* POP original parameter out */
  9463. for (i = 0; i < param_count; i++) {
  9464. POP_OFFSET_TYPE(wasm_type->types[param_count - i - 1]);
  9465. wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
  9466. }
  9467. frame_offset = loader_ctx->frame_offset;
  9468. /* Get each param's cell num and src offset */
  9469. for (i = 0; i < param_count; i++) {
  9470. cell = (uint8)wasm_value_type_cell_num(wasm_type->types[i]);
  9471. cells[i] = cell;
  9472. src_offsets[i] = *frame_offset;
  9473. frame_offset += cell;
  9474. }
  9475. /* emit copy instruction */
  9476. emit_label(EXT_OP_COPY_STACK_VALUES);
  9477. /* Part a) */
  9478. emit_uint32(loader_ctx, is_if_block ? param_count + 1 : param_count);
  9479. /* Part b) */
  9480. emit_uint32(loader_ctx, is_if_block ? wasm_type->param_cell_num + 1
  9481. : wasm_type->param_cell_num);
  9482. /* Part c) */
  9483. for (i = 0; i < param_count; i++)
  9484. emit_byte(loader_ctx, cells[i]);
  9485. if (is_if_block)
  9486. emit_byte(loader_ctx, 1);
  9487. /* Part d) */
  9488. for (i = 0; i < param_count; i++)
  9489. emit_operand(loader_ctx, src_offsets[i]);
  9490. if (is_if_block)
  9491. emit_operand(loader_ctx, condition_offset);
  9492. /* Part e) */
  9493. /* Push to dynamic space. The push will emit the dst offset. */
  9494. for (i = 0; i < param_count; i++)
  9495. PUSH_OFFSET_TYPE(wasm_type->types[i]);
  9496. if (is_if_block)
  9497. PUSH_OFFSET_TYPE(VALUE_TYPE_I32);
  9498. ret = true;
  9499. fail:
  9500. /* Free the emit data */
  9501. wasm_runtime_free(emit_data);
  9502. return ret;
  9503. }
  9504. #endif
  9505. #if WASM_ENABLE_GC == 0
  9506. #define RESET_REFTYPE_MAP_STACK() (void)0
  9507. #else
  9508. #define RESET_REFTYPE_MAP_STACK() \
  9509. do { \
  9510. loader_ctx->reftype_map_num = \
  9511. (loader_ctx->frame_csp - 1)->reftype_map_num; \
  9512. loader_ctx->frame_reftype_map = loader_ctx->frame_reftype_map_bottom \
  9513. + loader_ctx->reftype_map_num; \
  9514. } while (0)
  9515. #endif
  9516. /* reset the stack to the state of before entering the last block */
  9517. #if WASM_ENABLE_FAST_INTERP != 0
  9518. #define RESET_STACK() \
  9519. do { \
  9520. loader_ctx->stack_cell_num = \
  9521. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9522. loader_ctx->frame_ref = \
  9523. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9524. loader_ctx->frame_offset = \
  9525. loader_ctx->frame_offset_bottom + loader_ctx->stack_cell_num; \
  9526. RESET_REFTYPE_MAP_STACK(); \
  9527. } while (0)
  9528. #else
  9529. #define RESET_STACK() \
  9530. do { \
  9531. loader_ctx->stack_cell_num = \
  9532. (loader_ctx->frame_csp - 1)->stack_cell_num; \
  9533. loader_ctx->frame_ref = \
  9534. loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num; \
  9535. RESET_REFTYPE_MAP_STACK(); \
  9536. } while (0)
  9537. #endif
  9538. /* set current block's stack polymorphic state */
  9539. #define SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(flag) \
  9540. do { \
  9541. BranchBlock *_cur_block = loader_ctx->frame_csp - 1; \
  9542. _cur_block->is_stack_polymorphic = flag; \
  9543. } while (0)
  9544. #define BLOCK_HAS_PARAM(block_type) \
  9545. (!block_type.is_value_type && block_type.u.type->param_count > 0)
  9546. #define PRESERVE_LOCAL_FOR_BLOCK() \
  9547. do { \
  9548. if (!(preserve_local_for_block(loader_ctx, opcode, error_buf, \
  9549. error_buf_size))) { \
  9550. goto fail; \
  9551. } \
  9552. } while (0)
  9553. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  9554. static bool
  9555. get_table_elem_type(const WASMModule *module, uint32 table_idx,
  9556. uint8 *p_elem_type, void **p_ref_type, char *error_buf,
  9557. uint32 error_buf_size)
  9558. {
  9559. if (!check_table_index(module, table_idx, error_buf, error_buf_size)) {
  9560. return false;
  9561. }
  9562. if (table_idx < module->import_table_count) {
  9563. if (p_elem_type)
  9564. *p_elem_type = module->import_tables[table_idx].u.table.elem_type;
  9565. #if WASM_ENABLE_GC != 0
  9566. if (p_ref_type)
  9567. *((WASMRefType **)p_ref_type) =
  9568. module->import_tables[table_idx].u.table.elem_ref_type;
  9569. #endif
  9570. }
  9571. else {
  9572. if (p_elem_type)
  9573. *p_elem_type =
  9574. module->tables[module->import_table_count + table_idx]
  9575. .elem_type;
  9576. #if WASM_ENABLE_GC != 0
  9577. if (p_ref_type)
  9578. *((WASMRefType **)p_ref_type) =
  9579. module->tables[module->import_table_count + table_idx]
  9580. .elem_ref_type;
  9581. #endif
  9582. }
  9583. return true;
  9584. }
  9585. static bool
  9586. get_table_seg_elem_type(const WASMModule *module, uint32 table_seg_idx,
  9587. uint8 *p_elem_type, void **p_elem_ref_type,
  9588. char *error_buf, uint32 error_buf_size)
  9589. {
  9590. if (table_seg_idx >= module->table_seg_count) {
  9591. set_error_buf_v(error_buf, error_buf_size, "unknown elem segment %u",
  9592. table_seg_idx);
  9593. return false;
  9594. }
  9595. if (p_elem_type) {
  9596. *p_elem_type = module->table_segments[table_seg_idx].elem_type;
  9597. }
  9598. #if WASM_ENABLE_GC != 0
  9599. if (p_elem_ref_type)
  9600. *((WASMRefType **)p_elem_ref_type) =
  9601. module->table_segments[table_seg_idx].elem_ref_type;
  9602. #endif
  9603. return true;
  9604. }
  9605. #endif
  9606. #if WASM_ENABLE_LOAD_CUSTOM_SECTION != 0
  9607. const uint8 *
  9608. wasm_loader_get_custom_section(WASMModule *module, const char *name,
  9609. uint32 *len)
  9610. {
  9611. WASMCustomSection *section = module->custom_section_list;
  9612. while (section) {
  9613. if ((section->name_len == strlen(name))
  9614. && (memcmp(section->name_addr, name, section->name_len) == 0)) {
  9615. if (len) {
  9616. *len = section->content_len;
  9617. }
  9618. return section->content_addr;
  9619. }
  9620. section = section->next;
  9621. }
  9622. return NULL;
  9623. }
  9624. #endif
  9625. static bool
  9626. wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
  9627. uint32 cur_func_idx, char *error_buf,
  9628. uint32 error_buf_size)
  9629. {
  9630. uint8 *p = func->code, *p_end = func->code + func->code_size, *p_org;
  9631. uint32 param_count, local_count, global_count;
  9632. uint8 *param_types, *local_types, local_type, global_type, mem_offset_type;
  9633. BlockType func_block_type;
  9634. uint16 *local_offsets, local_offset;
  9635. uint32 type_idx, func_idx, local_idx, global_idx, table_idx;
  9636. uint32 table_seg_idx, data_seg_idx, count, align, i;
  9637. mem_offset_t mem_offset;
  9638. int32 i32_const = 0;
  9639. int64 i64_const;
  9640. uint8 opcode;
  9641. bool return_value = false;
  9642. WASMLoaderContext *loader_ctx;
  9643. BranchBlock *frame_csp_tmp;
  9644. #if WASM_ENABLE_GC != 0
  9645. WASMRefTypeMap *param_reftype_maps, *local_reftype_maps;
  9646. uint32 param_reftype_map_count, local_reftype_map_count;
  9647. int32 heap_type;
  9648. WASMRefType wasm_ref_type = { 0 };
  9649. bool need_ref_type_map;
  9650. #endif
  9651. #if WASM_ENABLE_FAST_INTERP != 0
  9652. uint8 *func_const_end, *func_const = NULL;
  9653. int16 operand_offset = 0;
  9654. uint8 last_op = 0;
  9655. bool disable_emit, preserve_local = false, if_condition_available = true;
  9656. float32 f32_const;
  9657. float64 f64_const;
  9658. LOG_OP("\nProcessing func | [%d] params | [%d] locals | [%d] return\n",
  9659. func->param_cell_num, func->local_cell_num, func->ret_cell_num);
  9660. #endif
  9661. #if WASM_ENABLE_MEMORY64 != 0
  9662. bool is_memory64 = has_module_memory64(module);
  9663. mem_offset_type = is_memory64 ? VALUE_TYPE_I64 : VALUE_TYPE_I32;
  9664. #else
  9665. mem_offset_type = VALUE_TYPE_I32;
  9666. #endif
  9667. global_count = module->import_global_count + module->global_count;
  9668. param_count = func->func_type->param_count;
  9669. param_types = func->func_type->types;
  9670. func_block_type.is_value_type = false;
  9671. func_block_type.u.type = func->func_type;
  9672. local_count = func->local_count;
  9673. local_types = func->local_types;
  9674. local_offsets = func->local_offsets;
  9675. #if WASM_ENABLE_GC != 0
  9676. param_reftype_maps = func->func_type->ref_type_maps;
  9677. param_reftype_map_count = func->func_type->ref_type_map_count;
  9678. local_reftype_maps = func->local_ref_type_maps;
  9679. local_reftype_map_count = func->local_ref_type_map_count;
  9680. #endif
  9681. if (!(loader_ctx = wasm_loader_ctx_init(func, error_buf, error_buf_size))) {
  9682. goto fail;
  9683. }
  9684. #if WASM_ENABLE_GC != 0
  9685. loader_ctx->module = module;
  9686. loader_ctx->ref_type_set = module->ref_type_set;
  9687. loader_ctx->ref_type_tmp = &wasm_ref_type;
  9688. #endif
  9689. #if WASM_ENABLE_FAST_INTERP != 0
  9690. /* For the first traverse, the initial value of preserved_local_offset has
  9691. * not been determined, we use the INT16_MAX to represent that a slot has
  9692. * been copied to preserve space. For second traverse, this field will be
  9693. * set to the appropriate value in wasm_loader_ctx_reinit.
  9694. * This is for Issue #1230,
  9695. * https://github.com/bytecodealliance/wasm-micro-runtime/issues/1230, the
  9696. * drop opcodes need to know which slots are preserved, so those slots will
  9697. * not be treated as dynamically allocated slots */
  9698. loader_ctx->preserved_local_offset = INT16_MAX;
  9699. re_scan:
  9700. if (loader_ctx->code_compiled_size > 0) {
  9701. if (!wasm_loader_ctx_reinit(loader_ctx)) {
  9702. set_error_buf(error_buf, error_buf_size, "allocate memory failed");
  9703. goto fail;
  9704. }
  9705. p = func->code;
  9706. func->code_compiled = loader_ctx->p_code_compiled;
  9707. func->code_compiled_size = loader_ctx->code_compiled_size;
  9708. }
  9709. #endif
  9710. PUSH_CSP(LABEL_TYPE_FUNCTION, func_block_type, p);
  9711. while (p < p_end) {
  9712. opcode = *p++;
  9713. #if WASM_ENABLE_FAST_INTERP != 0
  9714. p_org = p;
  9715. disable_emit = false;
  9716. emit_label(opcode);
  9717. #endif
  9718. switch (opcode) {
  9719. case WASM_OP_UNREACHABLE:
  9720. RESET_STACK();
  9721. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  9722. break;
  9723. case WASM_OP_NOP:
  9724. #if WASM_ENABLE_FAST_INTERP != 0
  9725. skip_label();
  9726. #endif
  9727. break;
  9728. case WASM_OP_IF:
  9729. {
  9730. #if WASM_ENABLE_FAST_INTERP != 0
  9731. BranchBlock *parent_block = loader_ctx->frame_csp - 1;
  9732. int32 available_stack_cell =
  9733. (int32)(loader_ctx->stack_cell_num
  9734. - parent_block->stack_cell_num);
  9735. if (available_stack_cell <= 0
  9736. && parent_block->is_stack_polymorphic)
  9737. if_condition_available = false;
  9738. else
  9739. if_condition_available = true;
  9740. PRESERVE_LOCAL_FOR_BLOCK();
  9741. #endif
  9742. #if WASM_ENABLE_GC == 0
  9743. POP_I32();
  9744. #endif
  9745. goto handle_op_block_and_loop;
  9746. }
  9747. case WASM_OP_BLOCK:
  9748. case WASM_OP_LOOP:
  9749. #if WASM_ENABLE_EXCE_HANDLING != 0
  9750. case WASM_OP_TRY:
  9751. if (opcode == WASM_OP_TRY) {
  9752. /*
  9753. * keep track of exception handlers to account for
  9754. * memory allocation
  9755. */
  9756. func->exception_handler_count++;
  9757. /*
  9758. * try is a block
  9759. * do nothing special, but execution continues to
  9760. * to handle_op_block_and_loop,
  9761. * and that be pushes the csp
  9762. */
  9763. }
  9764. #endif
  9765. #if WASM_ENABLE_FAST_INTERP != 0
  9766. PRESERVE_LOCAL_FOR_BLOCK();
  9767. #endif
  9768. handle_op_block_and_loop:
  9769. {
  9770. uint8 value_type;
  9771. BlockType block_type;
  9772. #if WASM_ENABLE_FAST_INTERP != 0
  9773. uint32 available_params = 0;
  9774. #endif
  9775. CHECK_BUF(p, p_end, 1);
  9776. value_type = read_uint8(p);
  9777. if (is_byte_a_type(value_type)) {
  9778. /* If the first byte is one of these special values:
  9779. * 0x40/0x7F/0x7E/0x7D/0x7C, take it as the type of
  9780. * the single return value. */
  9781. block_type.is_value_type = true;
  9782. block_type.u.value_type.type = value_type;
  9783. #if WASM_ENABLE_WAMR_COMPILER != 0
  9784. if (value_type == VALUE_TYPE_V128)
  9785. module->is_simd_used = true;
  9786. else if (value_type == VALUE_TYPE_FUNCREF
  9787. || value_type == VALUE_TYPE_EXTERNREF)
  9788. module->is_ref_types_used = true;
  9789. #endif
  9790. #if WASM_ENABLE_GC != 0
  9791. if (value_type != VALUE_TYPE_VOID) {
  9792. p_org = p;
  9793. p--;
  9794. if (!resolve_value_type((const uint8 **)&p, p_end,
  9795. module, module->type_count,
  9796. &need_ref_type_map,
  9797. &wasm_ref_type, false,
  9798. error_buf, error_buf_size)) {
  9799. goto fail;
  9800. }
  9801. if (need_ref_type_map) {
  9802. block_type.u.value_type.ref_type_map.index = 0;
  9803. if (!(block_type.u.value_type.ref_type_map
  9804. .ref_type = reftype_set_insert(
  9805. module->ref_type_set, &wasm_ref_type,
  9806. error_buf, error_buf_size))) {
  9807. goto fail;
  9808. }
  9809. }
  9810. /* Set again as the type might be changed, e.g.
  9811. (ref null any) to anyref */
  9812. block_type.u.value_type.type = wasm_ref_type.ref_type;
  9813. #if WASM_ENABLE_FAST_INTERP == 0
  9814. while (p_org < p) {
  9815. #if WASM_ENABLE_DEBUG_INTERP != 0
  9816. if (!record_fast_op(module, p_org, *p_org,
  9817. error_buf, error_buf_size)) {
  9818. goto fail;
  9819. }
  9820. #endif
  9821. /* Ignore extra bytes for interpreter */
  9822. *p_org++ = WASM_OP_NOP;
  9823. }
  9824. #endif
  9825. }
  9826. #endif /* end of WASM_ENABLE_GC != 0 */
  9827. }
  9828. else {
  9829. uint32 type_index;
  9830. /* Resolve the leb128 encoded type index as block type */
  9831. p--;
  9832. p_org = p - 1;
  9833. read_leb_uint32(p, p_end, type_index);
  9834. if (type_index >= module->type_count) {
  9835. set_error_buf(error_buf, error_buf_size,
  9836. "unknown type");
  9837. goto fail;
  9838. }
  9839. block_type.is_value_type = false;
  9840. block_type.u.type =
  9841. (WASMFuncType *)module->types[type_index];
  9842. #if WASM_ENABLE_FAST_INTERP == 0
  9843. /* If block use type index as block type, change the opcode
  9844. * to new extended opcode so that interpreter can resolve
  9845. * the block quickly.
  9846. */
  9847. #if WASM_ENABLE_DEBUG_INTERP != 0
  9848. if (!record_fast_op(module, p_org, *p_org, error_buf,
  9849. error_buf_size)) {
  9850. goto fail;
  9851. }
  9852. #endif
  9853. *p_org = EXT_OP_BLOCK + (opcode - WASM_OP_BLOCK);
  9854. #endif
  9855. }
  9856. #if WASM_ENABLE_GC != 0
  9857. if (opcode == WASM_OP_IF) {
  9858. POP_I32();
  9859. }
  9860. #endif
  9861. /* Pop block parameters from stack */
  9862. if (BLOCK_HAS_PARAM(block_type)) {
  9863. WASMFuncType *wasm_type = block_type.u.type;
  9864. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  9865. #if WASM_ENABLE_FAST_INTERP != 0
  9866. uint32 cell_num;
  9867. available_params = block_type.u.type->param_count;
  9868. #endif
  9869. for (i = 0; i < block_type.u.type->param_count; i++) {
  9870. int32 available_stack_cell =
  9871. (int32)(loader_ctx->stack_cell_num
  9872. - cur_block->stack_cell_num);
  9873. if (available_stack_cell <= 0
  9874. && cur_block->is_stack_polymorphic) {
  9875. #if WASM_ENABLE_FAST_INTERP != 0
  9876. available_params = i;
  9877. #endif
  9878. break;
  9879. }
  9880. POP_TYPE(
  9881. wasm_type->types[wasm_type->param_count - i - 1]);
  9882. #if WASM_ENABLE_FAST_INTERP != 0
  9883. /* decrease the frame_offset pointer accordingly to keep
  9884. * consistent with frame_ref stack */
  9885. cell_num = wasm_value_type_cell_num(
  9886. wasm_type->types[wasm_type->param_count - i - 1]);
  9887. loader_ctx->frame_offset -= cell_num;
  9888. #endif
  9889. }
  9890. }
  9891. PUSH_CSP(LABEL_TYPE_BLOCK + (opcode - WASM_OP_BLOCK),
  9892. block_type, p);
  9893. /* Pass parameters to block */
  9894. if (BLOCK_HAS_PARAM(block_type)) {
  9895. for (i = 0; i < block_type.u.type->param_count; i++) {
  9896. #if WASM_ENABLE_FAST_INTERP != 0
  9897. uint32 cell_num = wasm_value_type_cell_num(
  9898. block_type.u.type->types[i]);
  9899. if (i >= available_params) {
  9900. /* If there isn't enough data on stack, push a dummy
  9901. * offset to keep the stack consistent with
  9902. * frame_ref.
  9903. * Since the stack is already in polymorphic state,
  9904. * the opcode will not be executed, so the dummy
  9905. * offset won't cause any error */
  9906. uint32 n;
  9907. for (n = 0; n < cell_num; n++) {
  9908. if (loader_ctx->p_code_compiled == NULL) {
  9909. if (!check_offset_push(loader_ctx,
  9910. error_buf,
  9911. error_buf_size))
  9912. goto fail;
  9913. }
  9914. *loader_ctx->frame_offset++ = 0;
  9915. }
  9916. }
  9917. else {
  9918. loader_ctx->frame_offset += cell_num;
  9919. }
  9920. #endif
  9921. PUSH_TYPE(block_type.u.type->types[i]);
  9922. }
  9923. }
  9924. #if WASM_ENABLE_FAST_INTERP != 0
  9925. if (opcode == WASM_OP_BLOCK || opcode == WASM_OP_LOOP) {
  9926. skip_label();
  9927. if (BLOCK_HAS_PARAM(block_type)) {
  9928. /* Make sure params are in dynamic space */
  9929. if (!copy_params_to_dynamic_space(
  9930. loader_ctx, false, error_buf, error_buf_size))
  9931. goto fail;
  9932. }
  9933. if (opcode == WASM_OP_LOOP) {
  9934. (loader_ctx->frame_csp - 1)->code_compiled =
  9935. loader_ctx->p_code_compiled;
  9936. }
  9937. }
  9938. #if WASM_ENABLE_EXCE_HANDLING != 0
  9939. else if (opcode == WASM_OP_TRY) {
  9940. skip_label();
  9941. }
  9942. #endif
  9943. else if (opcode == WASM_OP_IF) {
  9944. BranchBlock *block = loader_ctx->frame_csp - 1;
  9945. /* If block has parameters, we should make sure they are in
  9946. * dynamic space. Otherwise, when else branch is missing,
  9947. * the later opcode may consume incorrect operand offset.
  9948. * Spec case:
  9949. * (func (export "params-id") (param i32) (result i32)
  9950. * (i32.const 1)
  9951. * (i32.const 2)
  9952. * (if (param i32 i32) (result i32 i32) (local.get 0)
  9953. * (then)) (i32.add)
  9954. * )
  9955. *
  9956. * So we should emit a copy instruction before the if.
  9957. *
  9958. * And we also need to save the parameter offsets and
  9959. * recover them before entering else branch.
  9960. *
  9961. */
  9962. if (BLOCK_HAS_PARAM(block_type)) {
  9963. uint64 size;
  9964. /* In polymorphic state, there may be no if condition on
  9965. * the stack, so the offset may not emitted */
  9966. if (if_condition_available) {
  9967. /* skip the if condition operand offset */
  9968. wasm_loader_emit_backspace(loader_ctx,
  9969. sizeof(int16));
  9970. }
  9971. /* skip the if label */
  9972. skip_label();
  9973. /* Emit a copy instruction */
  9974. if (!copy_params_to_dynamic_space(
  9975. loader_ctx, true, error_buf, error_buf_size))
  9976. goto fail;
  9977. /* Emit the if instruction */
  9978. emit_label(opcode);
  9979. /* Emit the new condition operand offset */
  9980. POP_OFFSET_TYPE(VALUE_TYPE_I32);
  9981. /* Save top param_count values of frame_offset stack, so
  9982. * that we can recover it before executing else branch
  9983. */
  9984. size = sizeof(int16)
  9985. * (uint64)block_type.u.type->param_cell_num;
  9986. if (!(block->param_frame_offsets = loader_malloc(
  9987. size, error_buf, error_buf_size)))
  9988. goto fail;
  9989. bh_memcpy_s(block->param_frame_offsets, (uint32)size,
  9990. loader_ctx->frame_offset
  9991. - size / sizeof(int16),
  9992. (uint32)size);
  9993. }
  9994. block->start_dynamic_offset = loader_ctx->dynamic_offset;
  9995. emit_empty_label_addr_and_frame_ip(PATCH_ELSE);
  9996. emit_empty_label_addr_and_frame_ip(PATCH_END);
  9997. }
  9998. #endif
  9999. break;
  10000. }
  10001. #if WASM_ENABLE_EXCE_HANDLING != 0
  10002. case WASM_OP_THROW:
  10003. {
  10004. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10005. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10006. uint8 label_type = cur_block->label_type;
  10007. uint32 tag_index = 0;
  10008. read_leb_int32(p, p_end, tag_index);
  10009. /* check validity of tag_index against module->tag_count */
  10010. /* check tag index is within the tag index space */
  10011. if (tag_index >= module->import_tag_count + module->tag_count) {
  10012. snprintf(error_buf, error_buf_size, "unknown tag %d",
  10013. tag_index);
  10014. goto fail;
  10015. }
  10016. /* the tag_type is stored in either the WASMTag (section tags)
  10017. * or WASMTagImport (import tag) */
  10018. WASMType *tag_type = NULL;
  10019. if (tag_index < module->import_tag_count) {
  10020. tag_type = module->import_tags[tag_index].u.tag.tag_type;
  10021. }
  10022. else {
  10023. tag_type =
  10024. module->tags[tag_index - module->import_tag_count]
  10025. ->tag_type;
  10026. }
  10027. if (tag_type->result_count != 0) {
  10028. set_error_buf(error_buf, error_buf_size,
  10029. "tag type signature does not return void");
  10030. goto fail;
  10031. }
  10032. int32 available_stack_cell =
  10033. (int32)(loader_ctx->stack_cell_num
  10034. - cur_block->stack_cell_num);
  10035. /* Check stack values match return types by comparing tag param
  10036. * types with stack cells */
  10037. uint8 *frame_ref = loader_ctx->frame_ref;
  10038. for (int tti = (int32)tag_type->param_count - 1; tti >= 0;
  10039. tti--) {
  10040. if (!check_stack_top_values(
  10041. loader_ctx, frame_ref, available_stack_cell,
  10042. tag_type->types[tti], error_buf, error_buf_size)) {
  10043. snprintf(error_buf, error_buf_size,
  10044. "type mismatch: instruction requires [%s] but "
  10045. "stack has [%s]",
  10046. tag_type->param_count > 0
  10047. ? type2str(tag_type->types[tti])
  10048. : "",
  10049. available_stack_cell > 0
  10050. ? type2str(*(loader_ctx->frame_ref - 1))
  10051. : "");
  10052. goto fail;
  10053. }
  10054. frame_ref -= wasm_value_type_cell_num(tag_type->types[tti]);
  10055. available_stack_cell -=
  10056. wasm_value_type_cell_num(tag_type->types[tti]);
  10057. }
  10058. /* throw is stack polymorphic */
  10059. (void)label_type;
  10060. RESET_STACK();
  10061. break;
  10062. }
  10063. case WASM_OP_RETHROW:
  10064. {
  10065. /* must be done before checking branch block */
  10066. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10067. /* check the target catching block: LABEL_TYPE_CATCH */
  10068. if (!(frame_csp_tmp =
  10069. check_branch_block(loader_ctx, &p, p_end, opcode,
  10070. error_buf, error_buf_size)))
  10071. goto fail;
  10072. if (frame_csp_tmp->label_type != LABEL_TYPE_CATCH
  10073. && frame_csp_tmp->label_type != LABEL_TYPE_CATCH_ALL) {
  10074. /* trap according to spectest (rethrow.wast) */
  10075. set_error_buf(error_buf, error_buf_size,
  10076. "invalid rethrow label");
  10077. goto fail;
  10078. }
  10079. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10080. uint8 label_type = cur_block->label_type;
  10081. (void)label_type;
  10082. /* rethrow is stack polymorphic */
  10083. RESET_STACK();
  10084. break;
  10085. }
  10086. case WASM_OP_DELEGATE:
  10087. {
  10088. /* check target block is valid */
  10089. if (!(frame_csp_tmp = check_branch_block_for_delegate(
  10090. loader_ctx, &p, p_end, error_buf, error_buf_size)))
  10091. goto fail;
  10092. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10093. uint8 label_type = cur_block->label_type;
  10094. (void)label_type;
  10095. /* DELEGATE ends the block */
  10096. POP_CSP();
  10097. break;
  10098. }
  10099. case WASM_OP_CATCH:
  10100. {
  10101. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10102. uint8 label_type = cur_block->label_type;
  10103. uint32 tag_index = 0;
  10104. read_leb_int32(p, p_end, tag_index);
  10105. /* check validity of tag_index against module->tag_count */
  10106. /* check tag index is within the tag index space */
  10107. if (tag_index >= module->import_tag_count + module->tag_count) {
  10108. LOG_VERBOSE("In %s, unknown tag at WASM_OP_CATCH\n",
  10109. __FUNCTION__);
  10110. set_error_buf(error_buf, error_buf_size, "unknown tag");
  10111. goto fail;
  10112. }
  10113. /* the tag_type is stored in either the WASMTag (section tags)
  10114. * or WASMTagImport (import tag) */
  10115. WASMType *func_type = NULL;
  10116. if (tag_index < module->import_tag_count) {
  10117. func_type = module->import_tags[tag_index].u.tag.tag_type;
  10118. }
  10119. else {
  10120. func_type =
  10121. module->tags[tag_index - module->import_tag_count]
  10122. ->tag_type;
  10123. }
  10124. if (func_type->result_count != 0) {
  10125. set_error_buf(error_buf, error_buf_size,
  10126. "tag type signature does not return void");
  10127. goto fail;
  10128. }
  10129. /* check validity of current label (expect LABEL_TYPE_TRY or
  10130. * LABEL_TYPE_CATCH) */
  10131. if ((LABEL_TYPE_CATCH != label_type)
  10132. && (LABEL_TYPE_TRY != label_type)) {
  10133. set_error_buf(error_buf, error_buf_size,
  10134. "Unexpected block sequence encountered.");
  10135. goto fail;
  10136. }
  10137. BlockType new_block_type;
  10138. new_block_type.is_value_type = false;
  10139. new_block_type.u.type = func_type;
  10140. /*
  10141. * replace frame_csp by LABEL_TYPE_CATCH
  10142. */
  10143. cur_block->label_type = LABEL_TYPE_CATCH;
  10144. /* RESET_STACK removes the values pushed in TRY or pervious
  10145. * CATCH Blocks */
  10146. RESET_STACK();
  10147. /* push types on the stack according to caught type */
  10148. if (BLOCK_HAS_PARAM(new_block_type)) {
  10149. for (i = 0; i < new_block_type.u.type->param_count; i++)
  10150. PUSH_TYPE(new_block_type.u.type->types[i]);
  10151. }
  10152. break;
  10153. }
  10154. case WASM_OP_CATCH_ALL:
  10155. {
  10156. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10157. /* expecting a TRY or CATCH, anything else will be considered an
  10158. * error */
  10159. if ((LABEL_TYPE_CATCH != cur_block->label_type)
  10160. && (LABEL_TYPE_TRY != cur_block->label_type)) {
  10161. set_error_buf(error_buf, error_buf_size,
  10162. "Unexpected block sequence encountered.");
  10163. goto fail;
  10164. }
  10165. /* no immediates */
  10166. /* replace frame_csp by LABEL_TYPE_CATCH_ALL */
  10167. cur_block->label_type = LABEL_TYPE_CATCH_ALL;
  10168. /* RESET_STACK removes the values pushed in TRY or pervious
  10169. * CATCH Blocks */
  10170. RESET_STACK();
  10171. /* catch_all has no tagtype and therefore no parameters */
  10172. break;
  10173. }
  10174. #endif /* end of WASM_ENABLE_EXCE_HANDLING != 0 */
  10175. case WASM_OP_ELSE:
  10176. {
  10177. BranchBlock *block = NULL;
  10178. BlockType block_type;
  10179. if (loader_ctx->csp_num < 2
  10180. /* the matched if isn't found */
  10181. || (loader_ctx->frame_csp - 1)->label_type != LABEL_TYPE_IF
  10182. /* duplicated else is found */
  10183. || (loader_ctx->frame_csp - 1)->else_addr) {
  10184. set_error_buf(
  10185. error_buf, error_buf_size,
  10186. "opcode else found without matched opcode if");
  10187. goto fail;
  10188. }
  10189. block = loader_ctx->frame_csp - 1;
  10190. /* check whether if branch's stack matches its result type */
  10191. if (!check_block_stack(loader_ctx, block, error_buf,
  10192. error_buf_size))
  10193. goto fail;
  10194. block->else_addr = p - 1;
  10195. block_type = block->block_type;
  10196. #if WASM_ENABLE_GC != 0
  10197. if (!wasm_loader_init_local_use_masks(
  10198. loader_ctx, local_count, error_buf, error_buf_size)) {
  10199. goto fail;
  10200. }
  10201. #endif
  10202. #if WASM_ENABLE_FAST_INTERP != 0
  10203. /* if the result of if branch is in local or const area, add a
  10204. * copy op */
  10205. RESERVE_BLOCK_RET();
  10206. emit_empty_label_addr_and_frame_ip(PATCH_END);
  10207. apply_label_patch(loader_ctx, 1, PATCH_ELSE);
  10208. #endif
  10209. RESET_STACK();
  10210. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(false);
  10211. /* Pass parameters to if-false branch */
  10212. if (BLOCK_HAS_PARAM(block_type)) {
  10213. for (i = 0; i < block_type.u.type->param_count; i++)
  10214. PUSH_TYPE(block_type.u.type->types[i]);
  10215. }
  10216. #if WASM_ENABLE_FAST_INTERP != 0
  10217. /* Recover top param_count values of frame_offset stack */
  10218. if (BLOCK_HAS_PARAM((block_type))) {
  10219. uint32 size;
  10220. size = sizeof(int16) * block_type.u.type->param_cell_num;
  10221. bh_memcpy_s(loader_ctx->frame_offset, size,
  10222. block->param_frame_offsets, size);
  10223. loader_ctx->frame_offset += (size / sizeof(int16));
  10224. }
  10225. loader_ctx->dynamic_offset = block->start_dynamic_offset;
  10226. #endif
  10227. break;
  10228. }
  10229. case WASM_OP_END:
  10230. {
  10231. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10232. /* check whether block stack matches its result type */
  10233. if (!check_block_stack(loader_ctx, cur_block, error_buf,
  10234. error_buf_size))
  10235. goto fail;
  10236. /* if no else branch, and return types do not match param types,
  10237. report failure */
  10238. if (cur_block->label_type == LABEL_TYPE_IF
  10239. && !cur_block->else_addr) {
  10240. uint32 block_param_count = 0, block_ret_count = 0;
  10241. uint8 *block_param_types = NULL, *block_ret_types = NULL;
  10242. BlockType *cur_block_type = &cur_block->block_type;
  10243. #if WASM_ENABLE_GC != 0
  10244. uint32 block_param_reftype_map_count;
  10245. uint32 block_ret_reftype_map_count;
  10246. WASMRefTypeMap *block_param_reftype_maps;
  10247. WASMRefTypeMap *block_ret_reftype_maps;
  10248. #endif
  10249. block_param_count = block_type_get_param_types(
  10250. cur_block_type, &block_param_types
  10251. #if WASM_ENABLE_GC != 0
  10252. ,
  10253. &block_param_reftype_maps,
  10254. &block_param_reftype_map_count
  10255. #endif
  10256. );
  10257. block_ret_count = block_type_get_result_types(
  10258. cur_block_type, &block_ret_types
  10259. #if WASM_ENABLE_GC != 0
  10260. ,
  10261. &block_ret_reftype_maps, &block_ret_reftype_map_count
  10262. #endif
  10263. );
  10264. if (block_param_count != block_ret_count
  10265. || (block_param_count
  10266. && memcmp(block_param_types, block_ret_types,
  10267. block_param_count))) {
  10268. set_error_buf(error_buf, error_buf_size,
  10269. "type mismatch: else branch missing");
  10270. goto fail;
  10271. }
  10272. #if WASM_ENABLE_GC != 0
  10273. if (block_param_reftype_map_count
  10274. != block_ret_reftype_map_count
  10275. || (block_param_reftype_map_count
  10276. && memcmp(block_param_reftype_maps,
  10277. block_ret_reftype_maps,
  10278. sizeof(WASMRefTypeMap)
  10279. * block_param_reftype_map_count))) {
  10280. set_error_buf(error_buf, error_buf_size,
  10281. "type mismatch: else branch missing");
  10282. goto fail;
  10283. }
  10284. #endif
  10285. }
  10286. POP_CSP();
  10287. #if WASM_ENABLE_FAST_INTERP != 0
  10288. skip_label();
  10289. /* copy the result to the block return address */
  10290. RESERVE_BLOCK_RET();
  10291. apply_label_patch(loader_ctx, 0, PATCH_END);
  10292. free_label_patch_list(loader_ctx->frame_csp);
  10293. if (loader_ctx->frame_csp->label_type == LABEL_TYPE_FUNCTION) {
  10294. int32 idx;
  10295. uint8 ret_type;
  10296. emit_label(WASM_OP_RETURN);
  10297. for (idx = (int32)func->func_type->result_count - 1;
  10298. idx >= 0; idx--) {
  10299. ret_type = *(func->func_type->types
  10300. + func->func_type->param_count + idx);
  10301. POP_OFFSET_TYPE(ret_type);
  10302. }
  10303. }
  10304. #endif
  10305. if (loader_ctx->csp_num > 0) {
  10306. loader_ctx->frame_csp->end_addr = p - 1;
  10307. }
  10308. else {
  10309. /* end of function block, function will return */
  10310. if (p < p_end) {
  10311. set_error_buf(error_buf, error_buf_size,
  10312. "section size mismatch");
  10313. goto fail;
  10314. }
  10315. }
  10316. break;
  10317. }
  10318. case WASM_OP_BR:
  10319. {
  10320. if (!(frame_csp_tmp =
  10321. check_branch_block(loader_ctx, &p, p_end, opcode,
  10322. error_buf, error_buf_size)))
  10323. goto fail;
  10324. RESET_STACK();
  10325. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10326. break;
  10327. }
  10328. case WASM_OP_BR_IF:
  10329. {
  10330. POP_I32();
  10331. if (!(frame_csp_tmp =
  10332. check_branch_block(loader_ctx, &p, p_end, opcode,
  10333. error_buf, error_buf_size)))
  10334. goto fail;
  10335. break;
  10336. }
  10337. case WASM_OP_BR_TABLE:
  10338. {
  10339. uint32 depth = 0, default_arity, arity = 0;
  10340. BranchBlock *target_block;
  10341. BlockType *target_block_type;
  10342. #if WASM_ENABLE_FAST_INTERP == 0
  10343. BrTableCache *br_table_cache = NULL;
  10344. uint8 *p_depth_begin, *p_depth, *p_opcode = p - 1;
  10345. uint32 j;
  10346. #endif
  10347. read_leb_uint32(p, p_end, count);
  10348. #if WASM_ENABLE_FAST_INTERP != 0
  10349. emit_uint32(loader_ctx, count);
  10350. #endif
  10351. POP_I32();
  10352. /* Get each depth and check it */
  10353. p_org = p;
  10354. for (i = 0; i <= count; i++) {
  10355. read_leb_uint32(p, p_end, depth);
  10356. bh_assert(loader_ctx->csp_num > 0);
  10357. if (loader_ctx->csp_num - 1 < depth) {
  10358. set_error_buf(error_buf, error_buf_size,
  10359. "unknown label, "
  10360. "unexpected end of section or function");
  10361. goto fail;
  10362. }
  10363. }
  10364. p = p_org;
  10365. /* Get the default block's arity */
  10366. target_block = loader_ctx->frame_csp - (depth + 1);
  10367. target_block_type = &target_block->block_type;
  10368. default_arity = block_type_get_arity(target_block_type,
  10369. target_block->label_type);
  10370. #if WASM_ENABLE_FAST_INTERP == 0
  10371. p_depth_begin = p_depth = p;
  10372. #endif
  10373. for (i = 0; i <= count; i++) {
  10374. p_org = p;
  10375. read_leb_uint32(p, p_end, depth);
  10376. p = p_org;
  10377. /* Get the target block's arity and check it */
  10378. target_block = loader_ctx->frame_csp - (depth + 1);
  10379. target_block_type = &target_block->block_type;
  10380. arity = block_type_get_arity(target_block_type,
  10381. target_block->label_type);
  10382. if (arity != default_arity) {
  10383. set_error_buf(error_buf, error_buf_size,
  10384. "type mismatch: br_table targets must "
  10385. "all use same result type");
  10386. goto fail;
  10387. }
  10388. if (!(frame_csp_tmp =
  10389. check_branch_block(loader_ctx, &p, p_end, opcode,
  10390. error_buf, error_buf_size))) {
  10391. goto fail;
  10392. }
  10393. #if WASM_ENABLE_FAST_INTERP == 0
  10394. if (br_table_cache) {
  10395. br_table_cache->br_depths[i] = depth;
  10396. }
  10397. else {
  10398. if (depth > 255) {
  10399. /* The depth cannot be stored in one byte,
  10400. create br_table cache to store each depth */
  10401. #if WASM_ENABLE_DEBUG_INTERP != 0
  10402. if (!record_fast_op(module, p_opcode, *p_opcode,
  10403. error_buf, error_buf_size)) {
  10404. goto fail;
  10405. }
  10406. #endif
  10407. if (!(br_table_cache = loader_malloc(
  10408. offsetof(BrTableCache, br_depths)
  10409. + sizeof(uint32)
  10410. * (uint64)(count + 1),
  10411. error_buf, error_buf_size))) {
  10412. goto fail;
  10413. }
  10414. *p_opcode = EXT_OP_BR_TABLE_CACHE;
  10415. br_table_cache->br_table_op_addr = p_opcode;
  10416. br_table_cache->br_count = count;
  10417. /* Copy previous depths which are one byte */
  10418. for (j = 0; j < i; j++) {
  10419. br_table_cache->br_depths[j] = p_depth_begin[j];
  10420. }
  10421. br_table_cache->br_depths[i] = depth;
  10422. bh_list_insert(module->br_table_cache_list,
  10423. br_table_cache);
  10424. }
  10425. else {
  10426. /* The depth can be stored in one byte, use the
  10427. byte of the leb to store it */
  10428. *p_depth++ = (uint8)depth;
  10429. }
  10430. }
  10431. #endif
  10432. }
  10433. #if WASM_ENABLE_FAST_INTERP == 0
  10434. /* Set the tailing bytes to nop */
  10435. if (br_table_cache)
  10436. p_depth = p_depth_begin;
  10437. while (p_depth < p)
  10438. *p_depth++ = WASM_OP_NOP;
  10439. #endif
  10440. RESET_STACK();
  10441. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10442. break;
  10443. }
  10444. case WASM_OP_RETURN:
  10445. {
  10446. int32 idx;
  10447. uint8 ret_type;
  10448. for (idx = (int32)func->func_type->result_count - 1; idx >= 0;
  10449. idx--) {
  10450. ret_type = *(func->func_type->types
  10451. + func->func_type->param_count + idx);
  10452. #if WASM_ENABLE_FAST_INTERP != 0
  10453. /* emit the offset after return opcode */
  10454. POP_OFFSET_TYPE(ret_type);
  10455. #endif
  10456. POP_TYPE(ret_type);
  10457. }
  10458. RESET_STACK();
  10459. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10460. break;
  10461. }
  10462. case WASM_OP_CALL:
  10463. #if WASM_ENABLE_TAIL_CALL != 0
  10464. case WASM_OP_RETURN_CALL:
  10465. #endif
  10466. #if WASM_ENABLE_GC != 0
  10467. case WASM_OP_CALL_REF:
  10468. case WASM_OP_RETURN_CALL_REF:
  10469. #endif
  10470. {
  10471. WASMFuncType *func_type;
  10472. uint8 type;
  10473. int32 idx;
  10474. #if WASM_ENABLE_GC != 0
  10475. WASMRefType *ref_type;
  10476. uint32 type_idx1;
  10477. int32 j;
  10478. #endif
  10479. #if WASM_ENABLE_GC != 0
  10480. if (opcode == WASM_OP_CALL_REF
  10481. || opcode == WASM_OP_RETURN_CALL_REF) {
  10482. read_leb_uint32(p, p_end, type_idx1);
  10483. if (!check_type_index(module, module->type_count, type_idx1,
  10484. error_buf, error_buf_size)) {
  10485. goto fail;
  10486. }
  10487. if (module->types[type_idx1]->type_flag != WASM_TYPE_FUNC) {
  10488. set_error_buf(error_buf, error_buf_size,
  10489. "unkown function type");
  10490. goto fail;
  10491. }
  10492. if (!wasm_loader_pop_nullable_typeidx(loader_ctx, &type,
  10493. &type_idx, error_buf,
  10494. error_buf_size)) {
  10495. goto fail;
  10496. }
  10497. if (type == VALUE_TYPE_ANY) {
  10498. type_idx = type_idx1;
  10499. }
  10500. if (!check_type_index(module, module->type_count, type_idx,
  10501. error_buf, error_buf_size)) {
  10502. goto fail;
  10503. }
  10504. if (module->types[type_idx]->type_flag != WASM_TYPE_FUNC) {
  10505. set_error_buf(error_buf, error_buf_size,
  10506. "unkown function type");
  10507. goto fail;
  10508. }
  10509. if (!wasm_func_type_is_super_of(
  10510. (WASMFuncType *)module->types[type_idx1],
  10511. (WASMFuncType *)module->types[type_idx])) {
  10512. set_error_buf(error_buf, error_buf_size,
  10513. "function type mismatch");
  10514. goto fail;
  10515. }
  10516. func_type = (WASMFuncType *)module->types[type_idx];
  10517. }
  10518. else
  10519. #endif
  10520. {
  10521. read_leb_uint32(p, p_end, func_idx);
  10522. #if WASM_ENABLE_FAST_INTERP != 0
  10523. /* we need to emit func_idx before arguments */
  10524. emit_uint32(loader_ctx, func_idx);
  10525. #endif
  10526. if (!check_function_index(module, func_idx, error_buf,
  10527. error_buf_size)) {
  10528. goto fail;
  10529. }
  10530. if (func_idx < module->import_function_count)
  10531. func_type = module->import_functions[func_idx]
  10532. .u.function.func_type;
  10533. else
  10534. func_type =
  10535. module
  10536. ->functions[func_idx
  10537. - module->import_function_count]
  10538. ->func_type;
  10539. }
  10540. if (func_type->param_count > 0) {
  10541. #if WASM_ENABLE_GC != 0
  10542. j = (int32)(func_type->result_ref_type_maps
  10543. - func_type->ref_type_maps - 1);
  10544. #endif
  10545. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10546. idx--) {
  10547. #if WASM_ENABLE_GC != 0
  10548. if (wasm_is_type_multi_byte_type(
  10549. func_type->types[idx])) {
  10550. ref_type = func_type->ref_type_maps[j].ref_type;
  10551. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10552. ref_type,
  10553. wasm_reftype_struct_size(ref_type));
  10554. j--;
  10555. }
  10556. #endif
  10557. #if WASM_ENABLE_FAST_INTERP != 0
  10558. POP_OFFSET_TYPE(func_type->types[idx]);
  10559. #endif
  10560. POP_TYPE(func_type->types[idx]);
  10561. }
  10562. }
  10563. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10564. if (opcode == WASM_OP_CALL || opcode == WASM_OP_CALL_REF) {
  10565. #endif
  10566. #if WASM_ENABLE_GC != 0
  10567. j = (int32)(func_type->result_ref_type_maps
  10568. - func_type->ref_type_maps);
  10569. #endif
  10570. for (i = 0; i < func_type->result_count; i++) {
  10571. #if WASM_ENABLE_GC != 0
  10572. if (wasm_is_type_multi_byte_type(
  10573. func_type->types[func_type->param_count + i])) {
  10574. ref_type = func_type->ref_type_maps[j].ref_type;
  10575. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  10576. ref_type,
  10577. wasm_reftype_struct_size(ref_type));
  10578. j++;
  10579. }
  10580. #endif
  10581. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10582. #if WASM_ENABLE_FAST_INTERP != 0
  10583. /* Here we emit each return value's dynamic_offset. But
  10584. * in fact these offsets are continuous, so interpreter
  10585. * only need to get the first return value's offset.
  10586. */
  10587. PUSH_OFFSET_TYPE(
  10588. func_type->types[func_type->param_count + i]);
  10589. #endif
  10590. }
  10591. #if WASM_ENABLE_TAIL_CALL != 0 || WASM_ENABLE_GC != 0
  10592. }
  10593. else {
  10594. #if WASM_ENABLE_GC == 0
  10595. if (func_type->result_count
  10596. != func->func_type->result_count) {
  10597. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10598. "type mismatch: expect ",
  10599. func->func_type->result_count,
  10600. " return values but got other");
  10601. goto fail;
  10602. }
  10603. for (i = 0; i < func_type->result_count; i++) {
  10604. type = func->func_type
  10605. ->types[func->func_type->param_count + i];
  10606. if (func_type->types[func_type->param_count + i]
  10607. != type) {
  10608. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10609. "type mismatch: expect ",
  10610. type2str(type), " but got other");
  10611. goto fail;
  10612. }
  10613. }
  10614. #else
  10615. if (!wasm_func_type_result_is_subtype_of(
  10616. func_type, func->func_type, module->types,
  10617. module->type_count)) {
  10618. set_error_buf(
  10619. error_buf, error_buf_size,
  10620. "type mismatch: invalid func result types");
  10621. goto fail;
  10622. }
  10623. #endif
  10624. RESET_STACK();
  10625. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10626. }
  10627. #endif
  10628. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10629. || WASM_ENABLE_WAMR_COMPILER != 0
  10630. func->has_op_func_call = true;
  10631. #endif
  10632. (void)type;
  10633. break;
  10634. }
  10635. /*
  10636. * if disable reference type: call_indirect typeidx, 0x00
  10637. * if enable reference type: call_indirect typeidx, tableidx
  10638. */
  10639. case WASM_OP_CALL_INDIRECT:
  10640. #if WASM_ENABLE_TAIL_CALL != 0
  10641. case WASM_OP_RETURN_CALL_INDIRECT:
  10642. #endif
  10643. {
  10644. int32 idx;
  10645. WASMFuncType *func_type;
  10646. read_leb_uint32(p, p_end, type_idx);
  10647. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10648. read_leb_uint32(p, p_end, table_idx);
  10649. #else
  10650. CHECK_BUF(p, p_end, 1);
  10651. table_idx = read_uint8(p);
  10652. #endif
  10653. if (!check_table_index(module, table_idx, error_buf,
  10654. error_buf_size)) {
  10655. goto fail;
  10656. }
  10657. #if WASM_ENABLE_FAST_INTERP != 0
  10658. /* we need to emit before arguments */
  10659. #if WASM_ENABLE_TAIL_CALL != 0
  10660. emit_byte(loader_ctx, opcode);
  10661. #endif
  10662. emit_uint32(loader_ctx, type_idx);
  10663. emit_uint32(loader_ctx, table_idx);
  10664. #endif
  10665. /* skip elem idx */
  10666. POP_I32();
  10667. if (type_idx >= module->type_count) {
  10668. set_error_buf(error_buf, error_buf_size, "unknown type");
  10669. goto fail;
  10670. }
  10671. func_type = (WASMFuncType *)module->types[type_idx];
  10672. if (func_type->param_count > 0) {
  10673. for (idx = (int32)(func_type->param_count - 1); idx >= 0;
  10674. idx--) {
  10675. #if WASM_ENABLE_FAST_INTERP != 0
  10676. POP_OFFSET_TYPE(func_type->types[idx]);
  10677. #endif
  10678. POP_TYPE(func_type->types[idx]);
  10679. }
  10680. }
  10681. #if WASM_ENABLE_TAIL_CALL != 0
  10682. if (opcode == WASM_OP_CALL_INDIRECT) {
  10683. #endif
  10684. for (i = 0; i < func_type->result_count; i++) {
  10685. PUSH_TYPE(func_type->types[func_type->param_count + i]);
  10686. #if WASM_ENABLE_FAST_INTERP != 0
  10687. PUSH_OFFSET_TYPE(
  10688. func_type->types[func_type->param_count + i]);
  10689. #endif
  10690. }
  10691. #if WASM_ENABLE_TAIL_CALL != 0
  10692. }
  10693. else {
  10694. uint8 type;
  10695. if (func_type->result_count
  10696. != func->func_type->result_count) {
  10697. set_error_buf_v(error_buf, error_buf_size, "%s%u%s",
  10698. "type mismatch: expect ",
  10699. func->func_type->result_count,
  10700. " return values but got other");
  10701. goto fail;
  10702. }
  10703. for (i = 0; i < func_type->result_count; i++) {
  10704. type = func->func_type
  10705. ->types[func->func_type->param_count + i];
  10706. if (func_type->types[func_type->param_count + i]
  10707. != type) {
  10708. set_error_buf_v(error_buf, error_buf_size, "%s%s%s",
  10709. "type mismatch: expect ",
  10710. type2str(type), " but got other");
  10711. goto fail;
  10712. }
  10713. }
  10714. RESET_STACK();
  10715. SET_CUR_BLOCK_STACK_POLYMORPHIC_STATE(true);
  10716. }
  10717. #endif
  10718. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  10719. || WASM_ENABLE_WAMR_COMPILER != 0
  10720. func->has_op_func_call = true;
  10721. #endif
  10722. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  10723. func->has_op_call_indirect = true;
  10724. #endif
  10725. break;
  10726. }
  10727. case WASM_OP_DROP:
  10728. {
  10729. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10730. int32 available_stack_cell =
  10731. (int32)(loader_ctx->stack_cell_num
  10732. - cur_block->stack_cell_num);
  10733. if (available_stack_cell <= 0
  10734. && !cur_block->is_stack_polymorphic) {
  10735. set_error_buf(error_buf, error_buf_size,
  10736. "type mismatch, opcode drop was found "
  10737. "but stack was empty");
  10738. goto fail;
  10739. }
  10740. if (available_stack_cell > 0) {
  10741. #if WASM_ENABLE_GC != 0
  10742. if (wasm_is_type_multi_byte_type(
  10743. *(loader_ctx->frame_ref - 1))) {
  10744. bh_assert((int32)(loader_ctx->reftype_map_num
  10745. - cur_block->reftype_map_num)
  10746. > 0);
  10747. loader_ctx->frame_reftype_map--;
  10748. loader_ctx->reftype_map_num--;
  10749. }
  10750. #endif
  10751. if (is_32bit_type(*(loader_ctx->frame_ref - 1))) {
  10752. loader_ctx->frame_ref--;
  10753. loader_ctx->stack_cell_num--;
  10754. #if WASM_ENABLE_FAST_INTERP != 0
  10755. skip_label();
  10756. loader_ctx->frame_offset--;
  10757. if ((*(loader_ctx->frame_offset)
  10758. > loader_ctx->start_dynamic_offset)
  10759. && (*(loader_ctx->frame_offset)
  10760. < loader_ctx->max_dynamic_offset))
  10761. loader_ctx->dynamic_offset--;
  10762. #endif
  10763. }
  10764. else if (is_64bit_type(*(loader_ctx->frame_ref - 1))) {
  10765. loader_ctx->frame_ref -= 2;
  10766. loader_ctx->stack_cell_num -= 2;
  10767. #if WASM_ENABLE_FAST_INTERP == 0
  10768. *(p - 1) = WASM_OP_DROP_64;
  10769. #endif
  10770. #if WASM_ENABLE_FAST_INTERP != 0
  10771. skip_label();
  10772. loader_ctx->frame_offset -= 2;
  10773. if ((*(loader_ctx->frame_offset)
  10774. > loader_ctx->start_dynamic_offset)
  10775. && (*(loader_ctx->frame_offset)
  10776. < loader_ctx->max_dynamic_offset))
  10777. loader_ctx->dynamic_offset -= 2;
  10778. #endif
  10779. }
  10780. #if WASM_ENABLE_SIMD != 0
  10781. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10782. else if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_V128) {
  10783. loader_ctx->frame_ref -= 4;
  10784. loader_ctx->stack_cell_num -= 4;
  10785. }
  10786. #endif
  10787. #endif
  10788. else {
  10789. set_error_buf(error_buf, error_buf_size,
  10790. "type mismatch");
  10791. goto fail;
  10792. }
  10793. }
  10794. else {
  10795. #if WASM_ENABLE_FAST_INTERP != 0
  10796. skip_label();
  10797. #endif
  10798. }
  10799. break;
  10800. }
  10801. case WASM_OP_SELECT:
  10802. {
  10803. uint8 ref_type;
  10804. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  10805. int32 available_stack_cell;
  10806. #if WASM_ENABLE_FAST_INTERP != 0
  10807. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10808. #endif
  10809. POP_I32();
  10810. available_stack_cell = (int32)(loader_ctx->stack_cell_num
  10811. - cur_block->stack_cell_num);
  10812. if (available_stack_cell <= 0
  10813. && !cur_block->is_stack_polymorphic) {
  10814. set_error_buf(error_buf, error_buf_size,
  10815. "type mismatch or invalid result arity, "
  10816. "opcode select was found "
  10817. "but stack was empty");
  10818. goto fail;
  10819. }
  10820. if (available_stack_cell > 0) {
  10821. switch (*(loader_ctx->frame_ref - 1)) {
  10822. case VALUE_TYPE_I32:
  10823. case VALUE_TYPE_F32:
  10824. case VALUE_TYPE_ANY:
  10825. break;
  10826. case VALUE_TYPE_I64:
  10827. case VALUE_TYPE_F64:
  10828. #if WASM_ENABLE_FAST_INTERP == 0
  10829. *(p - 1) = WASM_OP_SELECT_64;
  10830. #endif
  10831. #if WASM_ENABLE_FAST_INTERP != 0
  10832. if (loader_ctx->p_code_compiled) {
  10833. uint8 opcode_tmp = WASM_OP_SELECT_64;
  10834. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10835. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10836. *(void **)(p_code_compiled_tmp
  10837. - sizeof(void *)) =
  10838. handle_table[opcode_tmp];
  10839. #else
  10840. #if UINTPTR_MAX == UINT64_MAX
  10841. /* emit int32 relative offset in 64-bit target
  10842. */
  10843. int32 offset =
  10844. (int32)((uint8 *)handle_table[opcode_tmp]
  10845. - (uint8 *)handle_table[0]);
  10846. *(int32 *)(p_code_compiled_tmp
  10847. - sizeof(int32)) = offset;
  10848. #else
  10849. /* emit uint32 label address in 32-bit target */
  10850. *(uint32 *)(p_code_compiled_tmp
  10851. - sizeof(uint32)) =
  10852. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10853. #endif
  10854. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10855. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10856. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10857. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10858. #else
  10859. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10860. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10861. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10862. }
  10863. #endif /* end of WASM_ENABLE_FAST_INTERP */
  10864. break;
  10865. #if WASM_ENABLE_SIMD != 0
  10866. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  10867. case VALUE_TYPE_V128:
  10868. break;
  10869. #endif /* (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  10870. #endif /* WASM_ENABLE_SIMD != 0 */
  10871. default:
  10872. {
  10873. set_error_buf(error_buf, error_buf_size,
  10874. "type mismatch");
  10875. goto fail;
  10876. }
  10877. }
  10878. ref_type = *(loader_ctx->frame_ref - 1);
  10879. #if WASM_ENABLE_FAST_INTERP != 0
  10880. POP_OFFSET_TYPE(ref_type);
  10881. POP_TYPE(ref_type);
  10882. POP_OFFSET_TYPE(ref_type);
  10883. POP_TYPE(ref_type);
  10884. PUSH_OFFSET_TYPE(ref_type);
  10885. PUSH_TYPE(ref_type);
  10886. #else
  10887. POP2_AND_PUSH(ref_type, ref_type);
  10888. #endif
  10889. }
  10890. else {
  10891. #if WASM_ENABLE_FAST_INTERP != 0
  10892. PUSH_OFFSET_TYPE(VALUE_TYPE_ANY);
  10893. #endif
  10894. PUSH_TYPE(VALUE_TYPE_ANY);
  10895. }
  10896. break;
  10897. }
  10898. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  10899. case WASM_OP_SELECT_T:
  10900. {
  10901. uint8 vec_len, type;
  10902. #if WASM_ENABLE_GC != 0
  10903. WASMRefType *ref_type = NULL;
  10904. #endif
  10905. #if WASM_ENABLE_FAST_INTERP != 0
  10906. uint8 *p_code_compiled_tmp = loader_ctx->p_code_compiled;
  10907. #endif
  10908. read_leb_uint32(p, p_end, vec_len);
  10909. if (vec_len != 1) {
  10910. /* typed select must have exactly one result */
  10911. set_error_buf(error_buf, error_buf_size,
  10912. "invalid result arity");
  10913. goto fail;
  10914. }
  10915. #if WASM_ENABLE_GC == 0
  10916. CHECK_BUF(p, p_end, 1);
  10917. type = read_uint8(p);
  10918. if (!is_value_type(type)) {
  10919. set_error_buf(error_buf, error_buf_size,
  10920. "unknown value type");
  10921. goto fail;
  10922. }
  10923. #else
  10924. p_org = p + 1;
  10925. if (!resolve_value_type((const uint8 **)&p, p_end, module,
  10926. module->type_count, &need_ref_type_map,
  10927. &wasm_ref_type, false, error_buf,
  10928. error_buf_size)) {
  10929. goto fail;
  10930. }
  10931. type = wasm_ref_type.ref_type;
  10932. if (need_ref_type_map) {
  10933. if (!(ref_type = reftype_set_insert(
  10934. module->ref_type_set, &wasm_ref_type, error_buf,
  10935. error_buf_size))) {
  10936. goto fail;
  10937. }
  10938. }
  10939. #if WASM_ENABLE_FAST_INTERP == 0
  10940. while (p_org < p) {
  10941. #if WASM_ENABLE_DEBUG_INTERP != 0
  10942. if (!record_fast_op(module, p_org, *p_org, error_buf,
  10943. error_buf_size)) {
  10944. goto fail;
  10945. }
  10946. #endif
  10947. /* Ignore extra bytes for interpreter */
  10948. *p_org++ = WASM_OP_NOP;
  10949. }
  10950. #endif
  10951. #endif /* end of WASM_ENABLE_GC == 0 */
  10952. POP_I32();
  10953. #if WASM_ENABLE_FAST_INTERP != 0
  10954. if (loader_ctx->p_code_compiled) {
  10955. uint8 opcode_tmp = WASM_OP_SELECT;
  10956. if (type == VALUE_TYPE_V128) {
  10957. #if (WASM_ENABLE_SIMD == 0) \
  10958. || ((WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0))
  10959. set_error_buf(error_buf, error_buf_size,
  10960. "SIMD v128 type isn't supported");
  10961. goto fail;
  10962. #endif
  10963. }
  10964. else {
  10965. if (type == VALUE_TYPE_F64 || type == VALUE_TYPE_I64)
  10966. opcode_tmp = WASM_OP_SELECT_64;
  10967. #if WASM_ENABLE_GC != 0
  10968. if (wasm_is_type_reftype(type))
  10969. opcode_tmp = WASM_OP_SELECT_T;
  10970. #endif
  10971. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  10972. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10973. *(void **)(p_code_compiled_tmp - sizeof(void *)) =
  10974. handle_table[opcode_tmp];
  10975. #else
  10976. #if UINTPTR_MAX == UINT64_MAX
  10977. /* emit int32 relative offset in 64-bit target */
  10978. int32 offset = (int32)((uint8 *)handle_table[opcode_tmp]
  10979. - (uint8 *)handle_table[0]);
  10980. *(int32 *)(p_code_compiled_tmp - sizeof(int32)) =
  10981. offset;
  10982. #else
  10983. /* emit uint32 label address in 32-bit target */
  10984. *(uint32 *)(p_code_compiled_tmp - sizeof(uint32)) =
  10985. (uint32)(uintptr_t)handle_table[opcode_tmp];
  10986. #endif
  10987. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10988. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  10989. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  10990. *(p_code_compiled_tmp - 1) = opcode_tmp;
  10991. #else
  10992. *(p_code_compiled_tmp - 2) = opcode_tmp;
  10993. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  10994. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  10995. }
  10996. }
  10997. #endif /* WASM_ENABLE_FAST_INTERP != 0 */
  10998. POP_REF(type);
  10999. #if WASM_ENABLE_GC != 0
  11000. if (need_ref_type_map) {
  11001. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11002. wasm_reftype_struct_size(ref_type));
  11003. }
  11004. #endif
  11005. POP_REF(type);
  11006. #if WASM_ENABLE_GC != 0
  11007. if (need_ref_type_map) {
  11008. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11009. wasm_reftype_struct_size(ref_type));
  11010. }
  11011. #endif
  11012. PUSH_REF(type);
  11013. #if WASM_ENABLE_WAMR_COMPILER != 0
  11014. module->is_ref_types_used = true;
  11015. #endif
  11016. (void)vec_len;
  11017. break;
  11018. }
  11019. /* table.get x. tables[x]. [i32] -> [t] */
  11020. /* table.set x. tables[x]. [i32 t] -> [] */
  11021. case WASM_OP_TABLE_GET:
  11022. case WASM_OP_TABLE_SET:
  11023. {
  11024. uint8 decl_ref_type;
  11025. #if WASM_ENABLE_GC != 0
  11026. WASMRefType *ref_type;
  11027. #endif
  11028. read_leb_uint32(p, p_end, table_idx);
  11029. if (!get_table_elem_type(module, table_idx, &decl_ref_type,
  11030. #if WASM_ENABLE_GC != 0
  11031. (void **)&ref_type,
  11032. #else
  11033. NULL,
  11034. #endif
  11035. error_buf, error_buf_size))
  11036. goto fail;
  11037. #if WASM_ENABLE_GC != 0
  11038. if (wasm_is_type_multi_byte_type(decl_ref_type)) {
  11039. bh_assert(ref_type);
  11040. bh_memcpy_s(&wasm_ref_type, (uint32)sizeof(WASMRefType),
  11041. ref_type, wasm_reftype_struct_size(ref_type));
  11042. }
  11043. #endif
  11044. #if WASM_ENABLE_FAST_INTERP != 0
  11045. emit_uint32(loader_ctx, table_idx);
  11046. #endif
  11047. if (opcode == WASM_OP_TABLE_GET) {
  11048. POP_I32();
  11049. #if WASM_ENABLE_FAST_INTERP != 0
  11050. PUSH_OFFSET_TYPE(decl_ref_type);
  11051. #endif
  11052. PUSH_TYPE(decl_ref_type);
  11053. }
  11054. else {
  11055. #if WASM_ENABLE_FAST_INTERP != 0
  11056. POP_OFFSET_TYPE(decl_ref_type);
  11057. #endif
  11058. POP_TYPE(decl_ref_type);
  11059. POP_I32();
  11060. }
  11061. #if WASM_ENABLE_WAMR_COMPILER != 0
  11062. module->is_ref_types_used = true;
  11063. #endif
  11064. break;
  11065. }
  11066. case WASM_OP_REF_NULL:
  11067. {
  11068. uint8 ref_type;
  11069. #if WASM_ENABLE_GC == 0
  11070. CHECK_BUF(p, p_end, 1);
  11071. ref_type = read_uint8(p);
  11072. if (ref_type != VALUE_TYPE_FUNCREF
  11073. && ref_type != VALUE_TYPE_EXTERNREF) {
  11074. set_error_buf(error_buf, error_buf_size, "type mismatch");
  11075. goto fail;
  11076. }
  11077. #else
  11078. read_leb_int32(p, p_end, heap_type);
  11079. if (heap_type >= 0) {
  11080. if (!check_type_index(module, module->type_count, heap_type,
  11081. error_buf, error_buf_size)) {
  11082. goto fail;
  11083. }
  11084. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11085. true, heap_type);
  11086. ref_type = wasm_ref_type.ref_type;
  11087. }
  11088. else {
  11089. if (!wasm_is_valid_heap_type(heap_type)) {
  11090. set_error_buf(error_buf, error_buf_size,
  11091. "unknown type");
  11092. goto fail;
  11093. }
  11094. ref_type = (uint8)((int32)0x80 + heap_type);
  11095. }
  11096. #endif /* end of WASM_ENABLE_GC == 0 */
  11097. #if WASM_ENABLE_FAST_INTERP != 0
  11098. PUSH_OFFSET_TYPE(ref_type);
  11099. #endif
  11100. PUSH_TYPE(ref_type);
  11101. #if WASM_ENABLE_WAMR_COMPILER != 0
  11102. module->is_ref_types_used = true;
  11103. #endif
  11104. break;
  11105. }
  11106. case WASM_OP_REF_IS_NULL:
  11107. {
  11108. #if WASM_ENABLE_GC == 0
  11109. #if WASM_ENABLE_FAST_INTERP != 0
  11110. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11111. int32 block_stack_cell_num =
  11112. (int32)(loader_ctx->stack_cell_num
  11113. - cur_block->stack_cell_num);
  11114. if (block_stack_cell_num <= 0) {
  11115. if (!cur_block->is_stack_polymorphic) {
  11116. set_error_buf(
  11117. error_buf, error_buf_size,
  11118. "type mismatch: expect data but stack was empty");
  11119. goto fail;
  11120. }
  11121. }
  11122. else {
  11123. if (*(loader_ctx->frame_ref - 1) == VALUE_TYPE_FUNCREF
  11124. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_EXTERNREF
  11125. || *(loader_ctx->frame_ref - 1) == VALUE_TYPE_ANY) {
  11126. if (!wasm_loader_pop_frame_ref_offset(
  11127. loader_ctx, *(loader_ctx->frame_ref - 1),
  11128. error_buf, error_buf_size)) {
  11129. goto fail;
  11130. }
  11131. }
  11132. else {
  11133. set_error_buf(error_buf, error_buf_size,
  11134. "type mismatch");
  11135. goto fail;
  11136. }
  11137. }
  11138. #else
  11139. if (!wasm_loader_pop_frame_ref(loader_ctx, VALUE_TYPE_FUNCREF,
  11140. error_buf, error_buf_size)
  11141. && !wasm_loader_pop_frame_ref(loader_ctx,
  11142. VALUE_TYPE_EXTERNREF,
  11143. error_buf, error_buf_size)) {
  11144. goto fail;
  11145. }
  11146. #endif
  11147. #else /* else of WASM_ENABLE_GC == 0 */
  11148. uint8 type;
  11149. if (!wasm_loader_pop_heap_obj(loader_ctx, &type, &wasm_ref_type,
  11150. error_buf, error_buf_size)) {
  11151. goto fail;
  11152. }
  11153. #endif
  11154. PUSH_I32();
  11155. #if WASM_ENABLE_WAMR_COMPILER != 0
  11156. module->is_ref_types_used = true;
  11157. #endif
  11158. break;
  11159. }
  11160. case WASM_OP_REF_FUNC:
  11161. {
  11162. read_leb_uint32(p, p_end, func_idx);
  11163. if (!check_function_index(module, func_idx, error_buf,
  11164. error_buf_size)) {
  11165. goto fail;
  11166. }
  11167. /* Refer to a forward-declared function:
  11168. the function must be an import, exported, or present in
  11169. a table elem segment or global initializer to be used as
  11170. the operand to ref.func */
  11171. if (func_idx >= module->import_function_count) {
  11172. WASMTableSeg *table_seg = module->table_segments;
  11173. bool func_declared = false;
  11174. uint32 j;
  11175. for (i = 0; i < module->global_count; i++) {
  11176. if (module->globals[i].type == VALUE_TYPE_FUNCREF
  11177. && module->globals[i].init_expr.init_expr_type
  11178. == INIT_EXPR_TYPE_FUNCREF_CONST
  11179. && module->globals[i].init_expr.u.u32 == func_idx) {
  11180. func_declared = true;
  11181. break;
  11182. }
  11183. }
  11184. if (!func_declared) {
  11185. /* Check whether the function is declared in table segs,
  11186. note that it doesn't matter whether the table seg's
  11187. mode is passive, active or declarative. */
  11188. for (i = 0; i < module->table_seg_count;
  11189. i++, table_seg++) {
  11190. if (table_seg->elem_type == VALUE_TYPE_FUNCREF
  11191. #if WASM_ENABLE_GC != 0
  11192. /* elem type is (ref null? func) or
  11193. (ref null? $t) */
  11194. || ((table_seg->elem_type
  11195. == REF_TYPE_HT_NON_NULLABLE
  11196. || table_seg->elem_type
  11197. == REF_TYPE_HT_NULLABLE)
  11198. && (table_seg->elem_ref_type->ref_ht_common
  11199. .heap_type
  11200. == HEAP_TYPE_FUNC
  11201. || table_seg->elem_ref_type
  11202. ->ref_ht_common.heap_type
  11203. > 0))
  11204. #endif
  11205. ) {
  11206. for (j = 0; j < table_seg->value_count; j++) {
  11207. if (table_seg->init_values[j].u.ref_index
  11208. == func_idx) {
  11209. func_declared = true;
  11210. break;
  11211. }
  11212. }
  11213. }
  11214. }
  11215. }
  11216. if (!func_declared) {
  11217. /* Check whether the function is exported */
  11218. for (i = 0; i < module->export_count; i++) {
  11219. if (module->exports[i].kind == EXPORT_KIND_FUNC
  11220. && module->exports[i].index == func_idx) {
  11221. func_declared = true;
  11222. break;
  11223. }
  11224. }
  11225. }
  11226. if (!func_declared) {
  11227. set_error_buf(error_buf, error_buf_size,
  11228. "undeclared function reference");
  11229. goto fail;
  11230. }
  11231. }
  11232. #if WASM_ENABLE_FAST_INTERP != 0
  11233. emit_uint32(loader_ctx, func_idx);
  11234. #endif
  11235. #if WASM_ENABLE_GC == 0
  11236. PUSH_FUNCREF();
  11237. #else
  11238. if (func_idx < module->import_function_count)
  11239. type_idx =
  11240. module->import_functions[func_idx].u.function.type_idx;
  11241. else
  11242. type_idx = module
  11243. ->functions[func_idx
  11244. - module->import_function_count]
  11245. ->type_idx;
  11246. wasm_set_refheaptype_typeidx(&wasm_ref_type.ref_ht_typeidx,
  11247. false, type_idx);
  11248. PUSH_REF(wasm_ref_type.ref_type);
  11249. #endif
  11250. #if WASM_ENABLE_WAMR_COMPILER != 0
  11251. module->is_ref_types_used = true;
  11252. #endif
  11253. break;
  11254. }
  11255. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  11256. #if WASM_ENABLE_GC != 0
  11257. case WASM_OP_REF_AS_NON_NULL:
  11258. case WASM_OP_BR_ON_NULL:
  11259. {
  11260. uint8 type;
  11261. WASMRefType ref_type;
  11262. /* POP (ref null ht) and get the converted (ref ht) */
  11263. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11264. error_buf, error_buf_size)) {
  11265. goto fail;
  11266. }
  11267. if (opcode == WASM_OP_BR_ON_NULL) {
  11268. if (!(frame_csp_tmp =
  11269. check_branch_block(loader_ctx, &p, p_end, opcode,
  11270. error_buf, error_buf_size))) {
  11271. goto fail;
  11272. }
  11273. #if WASM_ENABLE_FAST_INTERP != 0
  11274. disable_emit = true;
  11275. #endif
  11276. }
  11277. /* PUSH the converted (ref ht) */
  11278. if (type != VALUE_TYPE_ANY) {
  11279. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), &ref_type,
  11280. sizeof(WASMRefType));
  11281. }
  11282. PUSH_REF(type);
  11283. break;
  11284. }
  11285. case WASM_OP_BR_ON_NON_NULL:
  11286. {
  11287. uint8 type;
  11288. WASMRefType ref_type;
  11289. uint32 available_stack_cell =
  11290. loader_ctx->stack_cell_num
  11291. - (loader_ctx->frame_csp - 1)->stack_cell_num;
  11292. /* POP (ref null ht) and get the converted (ref ht) */
  11293. if (!wasm_loader_pop_nullable_ht(loader_ctx, &type, &ref_type,
  11294. error_buf, error_buf_size)) {
  11295. goto fail;
  11296. }
  11297. #if WASM_ENABLE_FAST_INTERP != 0
  11298. disable_emit = true;
  11299. #endif
  11300. /* Temporarily PUSH back (ref ht), check brach block and
  11301. then POP it */
  11302. if (available_stack_cell
  11303. > 0) { /* stack isn't in polymorphic state */
  11304. if (type != VALUE_TYPE_ANY) {
  11305. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  11306. &ref_type, sizeof(WASMRefType));
  11307. }
  11308. PUSH_REF(type);
  11309. }
  11310. if (!(frame_csp_tmp =
  11311. check_branch_block(loader_ctx, &p, p_end, opcode,
  11312. error_buf, error_buf_size))) {
  11313. goto fail;
  11314. }
  11315. if (available_stack_cell
  11316. > 0) { /* stack isn't in polymorphic state */
  11317. POP_REF(type);
  11318. #if WASM_ENABLE_FAST_INTERP != 0
  11319. /* Erase the opnd offset emitted by POP_REF() */
  11320. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  11321. #endif
  11322. }
  11323. break;
  11324. }
  11325. case WASM_OP_REF_EQ:
  11326. POP_REF(REF_TYPE_EQREF);
  11327. POP_REF(REF_TYPE_EQREF);
  11328. PUSH_I32();
  11329. break;
  11330. #endif /* end of WASM_ENABLE_GC != 0 */
  11331. case WASM_OP_GET_LOCAL:
  11332. {
  11333. p_org = p - 1;
  11334. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11335. PUSH_TYPE(local_type);
  11336. #if WASM_ENABLE_GC != 0
  11337. /* Cannot get a non-nullable and unset local */
  11338. if (local_idx >= param_count
  11339. && wasm_is_reftype_htref_non_nullable(local_type)
  11340. && !wasm_loader_get_local_status(loader_ctx,
  11341. local_idx - param_count)) {
  11342. set_error_buf(error_buf, error_buf_size,
  11343. "uninitialized local");
  11344. goto fail;
  11345. }
  11346. #endif
  11347. #if WASM_ENABLE_FAST_INTERP != 0
  11348. /* Get Local is optimized out */
  11349. skip_label();
  11350. disable_emit = true;
  11351. operand_offset = local_offset;
  11352. PUSH_OFFSET_TYPE(local_type);
  11353. #else
  11354. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11355. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11356. if (local_offset < 0x80
  11357. #if WASM_ENABLE_GC != 0
  11358. && !wasm_is_type_reftype(local_type)
  11359. #endif
  11360. ) {
  11361. *p_org++ = EXT_OP_GET_LOCAL_FAST;
  11362. if (is_32bit_type(local_type)) {
  11363. *p_org++ = (uint8)local_offset;
  11364. }
  11365. else {
  11366. *p_org++ = (uint8)(local_offset | 0x80);
  11367. }
  11368. while (p_org < p) {
  11369. *p_org++ = WASM_OP_NOP;
  11370. }
  11371. }
  11372. #endif
  11373. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11374. break;
  11375. }
  11376. case WASM_OP_SET_LOCAL:
  11377. {
  11378. p_org = p - 1;
  11379. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11380. #if WASM_ENABLE_FAST_INTERP != 0
  11381. if (!(preserve_referenced_local(
  11382. loader_ctx, opcode, local_offset, local_type,
  11383. &preserve_local, error_buf, error_buf_size)))
  11384. goto fail;
  11385. if (local_offset < 256
  11386. #if WASM_ENABLE_GC != 0
  11387. && !wasm_is_type_reftype(local_type)
  11388. #endif
  11389. ) {
  11390. skip_label();
  11391. if ((!preserve_local) && (LAST_OP_OUTPUT_I32())) {
  11392. if (loader_ctx->p_code_compiled)
  11393. STORE_U16(loader_ctx->p_code_compiled - 2,
  11394. local_offset);
  11395. loader_ctx->frame_offset--;
  11396. loader_ctx->dynamic_offset--;
  11397. }
  11398. else if ((!preserve_local) && (LAST_OP_OUTPUT_I64())) {
  11399. if (loader_ctx->p_code_compiled)
  11400. STORE_U16(loader_ctx->p_code_compiled - 2,
  11401. local_offset);
  11402. loader_ctx->frame_offset -= 2;
  11403. loader_ctx->dynamic_offset -= 2;
  11404. }
  11405. else {
  11406. if (is_32bit_type(local_type)) {
  11407. emit_label(EXT_OP_SET_LOCAL_FAST);
  11408. emit_byte(loader_ctx, (uint8)local_offset);
  11409. }
  11410. else {
  11411. emit_label(EXT_OP_SET_LOCAL_FAST_I64);
  11412. emit_byte(loader_ctx, (uint8)local_offset);
  11413. }
  11414. POP_OFFSET_TYPE(local_type);
  11415. }
  11416. }
  11417. else { /* local index larger than 255, reserve leb */
  11418. emit_uint32(loader_ctx, local_idx);
  11419. POP_OFFSET_TYPE(local_type);
  11420. }
  11421. #else
  11422. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11423. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11424. if (local_offset < 0x80
  11425. #if WASM_ENABLE_GC != 0
  11426. && !wasm_is_type_reftype(local_type)
  11427. #endif
  11428. ) {
  11429. *p_org++ = EXT_OP_SET_LOCAL_FAST;
  11430. if (is_32bit_type(local_type)) {
  11431. *p_org++ = (uint8)local_offset;
  11432. }
  11433. else {
  11434. *p_org++ = (uint8)(local_offset | 0x80);
  11435. }
  11436. while (p_org < p) {
  11437. *p_org++ = WASM_OP_NOP;
  11438. }
  11439. }
  11440. #endif
  11441. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11442. #if WASM_ENABLE_GC != 0
  11443. if (local_idx >= param_count) {
  11444. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11445. }
  11446. #endif
  11447. POP_TYPE(local_type);
  11448. break;
  11449. }
  11450. case WASM_OP_TEE_LOCAL:
  11451. {
  11452. p_org = p - 1;
  11453. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  11454. #if WASM_ENABLE_FAST_INTERP != 0
  11455. /* If the stack is in polymorphic state, do fake pop and push on
  11456. offset stack to keep the depth of offset stack to be the
  11457. same with ref stack */
  11458. BranchBlock *cur_block = loader_ctx->frame_csp - 1;
  11459. if (cur_block->is_stack_polymorphic) {
  11460. POP_OFFSET_TYPE(local_type);
  11461. PUSH_OFFSET_TYPE(local_type);
  11462. }
  11463. #endif
  11464. POP_TYPE(local_type);
  11465. PUSH_TYPE(local_type);
  11466. #if WASM_ENABLE_FAST_INTERP != 0
  11467. if (!(preserve_referenced_local(
  11468. loader_ctx, opcode, local_offset, local_type,
  11469. &preserve_local, error_buf, error_buf_size)))
  11470. goto fail;
  11471. if (local_offset < 256
  11472. #if WASM_ENABLE_GC != 0
  11473. && !wasm_is_type_reftype(local_type)
  11474. #endif
  11475. ) {
  11476. skip_label();
  11477. if (is_32bit_type(local_type)) {
  11478. emit_label(EXT_OP_TEE_LOCAL_FAST);
  11479. emit_byte(loader_ctx, (uint8)local_offset);
  11480. }
  11481. else {
  11482. emit_label(EXT_OP_TEE_LOCAL_FAST_I64);
  11483. emit_byte(loader_ctx, (uint8)local_offset);
  11484. }
  11485. }
  11486. else { /* local index larger than 255, reserve leb */
  11487. emit_uint32(loader_ctx, local_idx);
  11488. }
  11489. emit_operand(loader_ctx,
  11490. *(loader_ctx->frame_offset
  11491. - wasm_value_type_cell_num(local_type)));
  11492. #else
  11493. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
  11494. && (WASM_ENABLE_FAST_JIT == 0) && (WASM_ENABLE_DEBUG_INTERP == 0)
  11495. if (local_offset < 0x80
  11496. #if WASM_ENABLE_GC != 0
  11497. && !wasm_is_type_reftype(local_type)
  11498. #endif
  11499. ) {
  11500. *p_org++ = EXT_OP_TEE_LOCAL_FAST;
  11501. if (is_32bit_type(local_type)) {
  11502. *p_org++ = (uint8)local_offset;
  11503. }
  11504. else {
  11505. *p_org++ = (uint8)(local_offset | 0x80);
  11506. }
  11507. while (p_org < p) {
  11508. *p_org++ = WASM_OP_NOP;
  11509. }
  11510. }
  11511. #endif
  11512. #endif /* end of WASM_ENABLE_FAST_INTERP != 0 */
  11513. #if WASM_ENABLE_GC != 0
  11514. if (local_idx >= param_count) {
  11515. wasm_loader_mask_local(loader_ctx, local_idx - param_count);
  11516. }
  11517. #endif
  11518. break;
  11519. }
  11520. case WASM_OP_GET_GLOBAL:
  11521. {
  11522. #if WASM_ENABLE_GC != 0
  11523. WASMRefType *ref_type;
  11524. #endif
  11525. p_org = p - 1;
  11526. read_leb_uint32(p, p_end, global_idx);
  11527. if (global_idx >= global_count) {
  11528. set_error_buf(error_buf, error_buf_size, "unknown global");
  11529. goto fail;
  11530. }
  11531. global_type =
  11532. global_idx < module->import_global_count
  11533. ? module->import_globals[global_idx].u.global.type
  11534. : module
  11535. ->globals[global_idx
  11536. - module->import_global_count]
  11537. .type;
  11538. #if WASM_ENABLE_GC != 0
  11539. ref_type =
  11540. global_idx < module->import_global_count
  11541. ? module->import_globals[global_idx].u.global.ref_type
  11542. : module
  11543. ->globals[global_idx
  11544. - module->import_global_count]
  11545. .ref_type;
  11546. if (wasm_is_type_multi_byte_type(global_type)) {
  11547. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11548. wasm_reftype_struct_size(ref_type));
  11549. }
  11550. #endif
  11551. PUSH_TYPE(global_type);
  11552. #if WASM_ENABLE_FAST_INTERP == 0
  11553. if (global_type == VALUE_TYPE_I64
  11554. || global_type == VALUE_TYPE_F64) {
  11555. #if WASM_ENABLE_DEBUG_INTERP != 0
  11556. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11557. error_buf_size)) {
  11558. goto fail;
  11559. }
  11560. #endif
  11561. *p_org = WASM_OP_GET_GLOBAL_64;
  11562. }
  11563. #else /* else of WASM_ENABLE_FAST_INTERP */
  11564. if (global_type == VALUE_TYPE_I64
  11565. || global_type == VALUE_TYPE_F64) {
  11566. skip_label();
  11567. emit_label(WASM_OP_GET_GLOBAL_64);
  11568. }
  11569. emit_uint32(loader_ctx, global_idx);
  11570. PUSH_OFFSET_TYPE(global_type);
  11571. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11572. break;
  11573. }
  11574. case WASM_OP_SET_GLOBAL:
  11575. {
  11576. bool is_mutable = false;
  11577. #if WASM_ENABLE_GC != 0
  11578. WASMRefType *ref_type;
  11579. #endif
  11580. p_org = p - 1;
  11581. read_leb_uint32(p, p_end, global_idx);
  11582. if (global_idx >= global_count) {
  11583. set_error_buf(error_buf, error_buf_size, "unknown global");
  11584. goto fail;
  11585. }
  11586. is_mutable =
  11587. global_idx < module->import_global_count
  11588. ? module->import_globals[global_idx].u.global.is_mutable
  11589. : module
  11590. ->globals[global_idx
  11591. - module->import_global_count]
  11592. .is_mutable;
  11593. if (!is_mutable) {
  11594. #if WASM_ENABLE_GC == 0
  11595. set_error_buf(error_buf, error_buf_size,
  11596. "global is immutable");
  11597. #else
  11598. set_error_buf(error_buf, error_buf_size,
  11599. "immutable global");
  11600. #endif
  11601. goto fail;
  11602. }
  11603. global_type =
  11604. global_idx < module->import_global_count
  11605. ? module->import_globals[global_idx].u.global.type
  11606. : module
  11607. ->globals[global_idx
  11608. - module->import_global_count]
  11609. .type;
  11610. #if WASM_ENABLE_GC != 0
  11611. ref_type =
  11612. global_idx < module->import_global_count
  11613. ? module->import_globals[global_idx].u.global.ref_type
  11614. : module
  11615. ->globals[global_idx
  11616. - module->import_global_count]
  11617. .ref_type;
  11618. if (wasm_is_type_multi_byte_type(global_type)) {
  11619. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType), ref_type,
  11620. wasm_reftype_struct_size(ref_type));
  11621. }
  11622. #endif
  11623. #if WASM_ENABLE_FAST_INTERP == 0
  11624. if (global_type == VALUE_TYPE_I64
  11625. || global_type == VALUE_TYPE_F64) {
  11626. #if WASM_ENABLE_DEBUG_INTERP != 0
  11627. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11628. error_buf_size)) {
  11629. goto fail;
  11630. }
  11631. #endif
  11632. *p_org = WASM_OP_SET_GLOBAL_64;
  11633. }
  11634. else if (module->aux_stack_size > 0
  11635. && global_idx == module->aux_stack_top_global_index) {
  11636. #if WASM_ENABLE_DEBUG_INTERP != 0
  11637. if (!record_fast_op(module, p_org, *p_org, error_buf,
  11638. error_buf_size)) {
  11639. goto fail;
  11640. }
  11641. #endif
  11642. *p_org = WASM_OP_SET_GLOBAL_AUX_STACK;
  11643. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11644. func->has_op_set_global_aux_stack = true;
  11645. #endif
  11646. }
  11647. #else /* else of WASM_ENABLE_FAST_INTERP */
  11648. if (global_type == VALUE_TYPE_I64
  11649. || global_type == VALUE_TYPE_F64) {
  11650. skip_label();
  11651. emit_label(WASM_OP_SET_GLOBAL_64);
  11652. }
  11653. else if (module->aux_stack_size > 0
  11654. && global_idx == module->aux_stack_top_global_index) {
  11655. skip_label();
  11656. emit_label(WASM_OP_SET_GLOBAL_AUX_STACK);
  11657. }
  11658. emit_uint32(loader_ctx, global_idx);
  11659. POP_OFFSET_TYPE(global_type);
  11660. #endif /* end of WASM_ENABLE_FAST_INTERP */
  11661. POP_TYPE(global_type);
  11662. break;
  11663. }
  11664. /* load */
  11665. case WASM_OP_I32_LOAD:
  11666. case WASM_OP_I32_LOAD8_S:
  11667. case WASM_OP_I32_LOAD8_U:
  11668. case WASM_OP_I32_LOAD16_S:
  11669. case WASM_OP_I32_LOAD16_U:
  11670. case WASM_OP_I64_LOAD:
  11671. case WASM_OP_I64_LOAD8_S:
  11672. case WASM_OP_I64_LOAD8_U:
  11673. case WASM_OP_I64_LOAD16_S:
  11674. case WASM_OP_I64_LOAD16_U:
  11675. case WASM_OP_I64_LOAD32_S:
  11676. case WASM_OP_I64_LOAD32_U:
  11677. case WASM_OP_F32_LOAD:
  11678. case WASM_OP_F64_LOAD:
  11679. /* store */
  11680. case WASM_OP_I32_STORE:
  11681. case WASM_OP_I32_STORE8:
  11682. case WASM_OP_I32_STORE16:
  11683. case WASM_OP_I64_STORE:
  11684. case WASM_OP_I64_STORE8:
  11685. case WASM_OP_I64_STORE16:
  11686. case WASM_OP_I64_STORE32:
  11687. case WASM_OP_F32_STORE:
  11688. case WASM_OP_F64_STORE:
  11689. {
  11690. #if WASM_ENABLE_FAST_INTERP != 0
  11691. /* change F32/F64 into I32/I64 */
  11692. if (opcode == WASM_OP_F32_LOAD) {
  11693. skip_label();
  11694. emit_label(WASM_OP_I32_LOAD);
  11695. }
  11696. else if (opcode == WASM_OP_F64_LOAD) {
  11697. skip_label();
  11698. emit_label(WASM_OP_I64_LOAD);
  11699. }
  11700. else if (opcode == WASM_OP_F32_STORE) {
  11701. skip_label();
  11702. emit_label(WASM_OP_I32_STORE);
  11703. }
  11704. else if (opcode == WASM_OP_F64_STORE) {
  11705. skip_label();
  11706. emit_label(WASM_OP_I64_STORE);
  11707. }
  11708. #endif
  11709. CHECK_MEMORY();
  11710. read_leb_uint32(p, p_end, align); /* align */
  11711. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  11712. if (!check_memory_access_align(opcode, align, error_buf,
  11713. error_buf_size)) {
  11714. goto fail;
  11715. }
  11716. #if WASM_ENABLE_FAST_INTERP != 0
  11717. emit_uint32(loader_ctx, mem_offset);
  11718. #endif
  11719. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11720. func->has_memory_operations = true;
  11721. #endif
  11722. switch (opcode) {
  11723. /* load */
  11724. case WASM_OP_I32_LOAD:
  11725. case WASM_OP_I32_LOAD8_S:
  11726. case WASM_OP_I32_LOAD8_U:
  11727. case WASM_OP_I32_LOAD16_S:
  11728. case WASM_OP_I32_LOAD16_U:
  11729. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  11730. break;
  11731. case WASM_OP_I64_LOAD:
  11732. case WASM_OP_I64_LOAD8_S:
  11733. case WASM_OP_I64_LOAD8_U:
  11734. case WASM_OP_I64_LOAD16_S:
  11735. case WASM_OP_I64_LOAD16_U:
  11736. case WASM_OP_I64_LOAD32_S:
  11737. case WASM_OP_I64_LOAD32_U:
  11738. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  11739. break;
  11740. case WASM_OP_F32_LOAD:
  11741. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F32);
  11742. break;
  11743. case WASM_OP_F64_LOAD:
  11744. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_F64);
  11745. break;
  11746. /* store */
  11747. case WASM_OP_I32_STORE:
  11748. case WASM_OP_I32_STORE8:
  11749. case WASM_OP_I32_STORE16:
  11750. POP_I32();
  11751. POP_MEM_OFFSET();
  11752. break;
  11753. case WASM_OP_I64_STORE:
  11754. case WASM_OP_I64_STORE8:
  11755. case WASM_OP_I64_STORE16:
  11756. case WASM_OP_I64_STORE32:
  11757. POP_I64();
  11758. POP_MEM_OFFSET();
  11759. break;
  11760. case WASM_OP_F32_STORE:
  11761. POP_F32();
  11762. POP_MEM_OFFSET();
  11763. break;
  11764. case WASM_OP_F64_STORE:
  11765. POP_F64();
  11766. POP_MEM_OFFSET();
  11767. break;
  11768. default:
  11769. break;
  11770. }
  11771. break;
  11772. }
  11773. case WASM_OP_MEMORY_SIZE:
  11774. CHECK_MEMORY();
  11775. /* reserved byte 0x00 */
  11776. if (*p++ != 0x00) {
  11777. set_error_buf(error_buf, error_buf_size,
  11778. "zero byte expected");
  11779. goto fail;
  11780. }
  11781. PUSH_PAGE_COUNT();
  11782. module->possible_memory_grow = true;
  11783. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11784. func->has_memory_operations = true;
  11785. #endif
  11786. break;
  11787. case WASM_OP_MEMORY_GROW:
  11788. CHECK_MEMORY();
  11789. /* reserved byte 0x00 */
  11790. if (*p++ != 0x00) {
  11791. set_error_buf(error_buf, error_buf_size,
  11792. "zero byte expected");
  11793. goto fail;
  11794. }
  11795. POP_AND_PUSH(mem_offset_type, mem_offset_type);
  11796. module->possible_memory_grow = true;
  11797. #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 \
  11798. || WASM_ENABLE_WAMR_COMPILER != 0
  11799. func->has_op_memory_grow = true;
  11800. #endif
  11801. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  11802. func->has_memory_operations = true;
  11803. #endif
  11804. break;
  11805. case WASM_OP_I32_CONST:
  11806. read_leb_int32(p, p_end, i32_const);
  11807. #if WASM_ENABLE_FAST_INTERP != 0
  11808. skip_label();
  11809. disable_emit = true;
  11810. GET_CONST_OFFSET(VALUE_TYPE_I32, i32_const);
  11811. if (operand_offset == 0) {
  11812. disable_emit = false;
  11813. emit_label(WASM_OP_I32_CONST);
  11814. emit_uint32(loader_ctx, i32_const);
  11815. }
  11816. #else
  11817. (void)i32_const;
  11818. #endif
  11819. PUSH_I32();
  11820. break;
  11821. case WASM_OP_I64_CONST:
  11822. read_leb_int64(p, p_end, i64_const);
  11823. #if WASM_ENABLE_FAST_INTERP != 0
  11824. skip_label();
  11825. disable_emit = true;
  11826. GET_CONST_OFFSET(VALUE_TYPE_I64, i64_const);
  11827. if (operand_offset == 0) {
  11828. disable_emit = false;
  11829. emit_label(WASM_OP_I64_CONST);
  11830. emit_uint64(loader_ctx, i64_const);
  11831. }
  11832. #endif
  11833. PUSH_I64();
  11834. break;
  11835. case WASM_OP_F32_CONST:
  11836. CHECK_BUF(p, p_end, sizeof(float32));
  11837. p += sizeof(float32);
  11838. #if WASM_ENABLE_FAST_INTERP != 0
  11839. skip_label();
  11840. disable_emit = true;
  11841. bh_memcpy_s((uint8 *)&f32_const, sizeof(float32), p_org,
  11842. sizeof(float32));
  11843. GET_CONST_F32_OFFSET(VALUE_TYPE_F32, f32_const);
  11844. if (operand_offset == 0) {
  11845. disable_emit = false;
  11846. emit_label(WASM_OP_F32_CONST);
  11847. emit_float32(loader_ctx, f32_const);
  11848. }
  11849. #endif
  11850. PUSH_F32();
  11851. break;
  11852. case WASM_OP_F64_CONST:
  11853. CHECK_BUF(p, p_end, sizeof(float64));
  11854. p += sizeof(float64);
  11855. #if WASM_ENABLE_FAST_INTERP != 0
  11856. skip_label();
  11857. disable_emit = true;
  11858. /* Some MCU may require 8-byte align */
  11859. bh_memcpy_s((uint8 *)&f64_const, sizeof(float64), p_org,
  11860. sizeof(float64));
  11861. GET_CONST_F64_OFFSET(VALUE_TYPE_F64, f64_const);
  11862. if (operand_offset == 0) {
  11863. disable_emit = false;
  11864. emit_label(WASM_OP_F64_CONST);
  11865. emit_float64(loader_ctx, f64_const);
  11866. }
  11867. #endif
  11868. PUSH_F64();
  11869. break;
  11870. case WASM_OP_I32_EQZ:
  11871. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11872. break;
  11873. case WASM_OP_I32_EQ:
  11874. case WASM_OP_I32_NE:
  11875. case WASM_OP_I32_LT_S:
  11876. case WASM_OP_I32_LT_U:
  11877. case WASM_OP_I32_GT_S:
  11878. case WASM_OP_I32_GT_U:
  11879. case WASM_OP_I32_LE_S:
  11880. case WASM_OP_I32_LE_U:
  11881. case WASM_OP_I32_GE_S:
  11882. case WASM_OP_I32_GE_U:
  11883. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11884. break;
  11885. case WASM_OP_I64_EQZ:
  11886. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11887. break;
  11888. case WASM_OP_I64_EQ:
  11889. case WASM_OP_I64_NE:
  11890. case WASM_OP_I64_LT_S:
  11891. case WASM_OP_I64_LT_U:
  11892. case WASM_OP_I64_GT_S:
  11893. case WASM_OP_I64_GT_U:
  11894. case WASM_OP_I64_LE_S:
  11895. case WASM_OP_I64_LE_U:
  11896. case WASM_OP_I64_GE_S:
  11897. case WASM_OP_I64_GE_U:
  11898. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11899. break;
  11900. case WASM_OP_F32_EQ:
  11901. case WASM_OP_F32_NE:
  11902. case WASM_OP_F32_LT:
  11903. case WASM_OP_F32_GT:
  11904. case WASM_OP_F32_LE:
  11905. case WASM_OP_F32_GE:
  11906. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  11907. break;
  11908. case WASM_OP_F64_EQ:
  11909. case WASM_OP_F64_NE:
  11910. case WASM_OP_F64_LT:
  11911. case WASM_OP_F64_GT:
  11912. case WASM_OP_F64_LE:
  11913. case WASM_OP_F64_GE:
  11914. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  11915. break;
  11916. case WASM_OP_I32_CLZ:
  11917. case WASM_OP_I32_CTZ:
  11918. case WASM_OP_I32_POPCNT:
  11919. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11920. break;
  11921. case WASM_OP_I32_ADD:
  11922. case WASM_OP_I32_SUB:
  11923. case WASM_OP_I32_MUL:
  11924. case WASM_OP_I32_DIV_S:
  11925. case WASM_OP_I32_DIV_U:
  11926. case WASM_OP_I32_REM_S:
  11927. case WASM_OP_I32_REM_U:
  11928. case WASM_OP_I32_AND:
  11929. case WASM_OP_I32_OR:
  11930. case WASM_OP_I32_XOR:
  11931. case WASM_OP_I32_SHL:
  11932. case WASM_OP_I32_SHR_S:
  11933. case WASM_OP_I32_SHR_U:
  11934. case WASM_OP_I32_ROTL:
  11935. case WASM_OP_I32_ROTR:
  11936. POP2_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  11937. break;
  11938. case WASM_OP_I64_CLZ:
  11939. case WASM_OP_I64_CTZ:
  11940. case WASM_OP_I64_POPCNT:
  11941. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11942. break;
  11943. case WASM_OP_I64_ADD:
  11944. case WASM_OP_I64_SUB:
  11945. case WASM_OP_I64_MUL:
  11946. case WASM_OP_I64_DIV_S:
  11947. case WASM_OP_I64_DIV_U:
  11948. case WASM_OP_I64_REM_S:
  11949. case WASM_OP_I64_REM_U:
  11950. case WASM_OP_I64_AND:
  11951. case WASM_OP_I64_OR:
  11952. case WASM_OP_I64_XOR:
  11953. case WASM_OP_I64_SHL:
  11954. case WASM_OP_I64_SHR_S:
  11955. case WASM_OP_I64_SHR_U:
  11956. case WASM_OP_I64_ROTL:
  11957. case WASM_OP_I64_ROTR:
  11958. POP2_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  11959. break;
  11960. case WASM_OP_F32_ABS:
  11961. case WASM_OP_F32_NEG:
  11962. case WASM_OP_F32_CEIL:
  11963. case WASM_OP_F32_FLOOR:
  11964. case WASM_OP_F32_TRUNC:
  11965. case WASM_OP_F32_NEAREST:
  11966. case WASM_OP_F32_SQRT:
  11967. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11968. break;
  11969. case WASM_OP_F32_ADD:
  11970. case WASM_OP_F32_SUB:
  11971. case WASM_OP_F32_MUL:
  11972. case WASM_OP_F32_DIV:
  11973. case WASM_OP_F32_MIN:
  11974. case WASM_OP_F32_MAX:
  11975. case WASM_OP_F32_COPYSIGN:
  11976. POP2_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F32);
  11977. break;
  11978. case WASM_OP_F64_ABS:
  11979. case WASM_OP_F64_NEG:
  11980. case WASM_OP_F64_CEIL:
  11981. case WASM_OP_F64_FLOOR:
  11982. case WASM_OP_F64_TRUNC:
  11983. case WASM_OP_F64_NEAREST:
  11984. case WASM_OP_F64_SQRT:
  11985. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11986. break;
  11987. case WASM_OP_F64_ADD:
  11988. case WASM_OP_F64_SUB:
  11989. case WASM_OP_F64_MUL:
  11990. case WASM_OP_F64_DIV:
  11991. case WASM_OP_F64_MIN:
  11992. case WASM_OP_F64_MAX:
  11993. case WASM_OP_F64_COPYSIGN:
  11994. POP2_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F64);
  11995. break;
  11996. case WASM_OP_I32_WRAP_I64:
  11997. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I32);
  11998. break;
  11999. case WASM_OP_I32_TRUNC_S_F32:
  12000. case WASM_OP_I32_TRUNC_U_F32:
  12001. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12002. break;
  12003. case WASM_OP_I32_TRUNC_S_F64:
  12004. case WASM_OP_I32_TRUNC_U_F64:
  12005. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  12006. break;
  12007. case WASM_OP_I64_EXTEND_S_I32:
  12008. case WASM_OP_I64_EXTEND_U_I32:
  12009. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I64);
  12010. break;
  12011. case WASM_OP_I64_TRUNC_S_F32:
  12012. case WASM_OP_I64_TRUNC_U_F32:
  12013. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  12014. break;
  12015. case WASM_OP_I64_TRUNC_S_F64:
  12016. case WASM_OP_I64_TRUNC_U_F64:
  12017. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12018. break;
  12019. case WASM_OP_F32_CONVERT_S_I32:
  12020. case WASM_OP_F32_CONVERT_U_I32:
  12021. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12022. break;
  12023. case WASM_OP_F32_CONVERT_S_I64:
  12024. case WASM_OP_F32_CONVERT_U_I64:
  12025. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F32);
  12026. break;
  12027. case WASM_OP_F32_DEMOTE_F64:
  12028. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_F32);
  12029. break;
  12030. case WASM_OP_F64_CONVERT_S_I32:
  12031. case WASM_OP_F64_CONVERT_U_I32:
  12032. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F64);
  12033. break;
  12034. case WASM_OP_F64_CONVERT_S_I64:
  12035. case WASM_OP_F64_CONVERT_U_I64:
  12036. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12037. break;
  12038. case WASM_OP_F64_PROMOTE_F32:
  12039. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_F64);
  12040. break;
  12041. case WASM_OP_I32_REINTERPRET_F32:
  12042. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  12043. break;
  12044. case WASM_OP_I64_REINTERPRET_F64:
  12045. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  12046. break;
  12047. case WASM_OP_F32_REINTERPRET_I32:
  12048. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_F32);
  12049. break;
  12050. case WASM_OP_F64_REINTERPRET_I64:
  12051. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_F64);
  12052. break;
  12053. case WASM_OP_I32_EXTEND8_S:
  12054. case WASM_OP_I32_EXTEND16_S:
  12055. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_I32);
  12056. break;
  12057. case WASM_OP_I64_EXTEND8_S:
  12058. case WASM_OP_I64_EXTEND16_S:
  12059. case WASM_OP_I64_EXTEND32_S:
  12060. POP_AND_PUSH(VALUE_TYPE_I64, VALUE_TYPE_I64);
  12061. break;
  12062. #if WASM_ENABLE_GC != 0
  12063. case WASM_OP_GC_PREFIX:
  12064. {
  12065. uint32 opcode1;
  12066. read_leb_uint32(p, p_end, opcode1);
  12067. #if WASM_ENABLE_FAST_INTERP != 0
  12068. emit_byte(loader_ctx, ((uint8)opcode1));
  12069. #endif
  12070. switch (opcode1) {
  12071. case WASM_OP_STRUCT_NEW:
  12072. case WASM_OP_STRUCT_NEW_DEFAULT:
  12073. {
  12074. read_leb_uint32(p, p_end, type_idx);
  12075. #if WASM_ENABLE_FAST_INTERP != 0
  12076. emit_uint32(loader_ctx, type_idx);
  12077. #endif
  12078. if (!check_type_index(module, module->type_count,
  12079. type_idx, error_buf,
  12080. error_buf_size)) {
  12081. goto fail;
  12082. }
  12083. if (module->types[type_idx]->type_flag
  12084. != WASM_TYPE_STRUCT) {
  12085. set_error_buf(error_buf, error_buf_size,
  12086. "unkown struct type");
  12087. goto fail;
  12088. }
  12089. if (opcode1 == WASM_OP_STRUCT_NEW) {
  12090. int32 j, k;
  12091. uint8 value_type;
  12092. uint32 ref_type_struct_size;
  12093. WASMStructType *struct_type =
  12094. (WASMStructType *)module->types[type_idx];
  12095. k = struct_type->ref_type_map_count - 1;
  12096. for (j = struct_type->field_count - 1; j >= 0;
  12097. j--) {
  12098. value_type = struct_type->fields[j].field_type;
  12099. if (wasm_is_type_reftype(value_type)) {
  12100. if (wasm_is_type_multi_byte_type(
  12101. value_type)) {
  12102. ref_type_struct_size =
  12103. wasm_reftype_struct_size(
  12104. struct_type->ref_type_maps[k]
  12105. .ref_type);
  12106. bh_memcpy_s(
  12107. &wasm_ref_type,
  12108. (uint32)sizeof(WASMRefType),
  12109. struct_type->ref_type_maps[k]
  12110. .ref_type,
  12111. ref_type_struct_size);
  12112. k--;
  12113. }
  12114. POP_REF(value_type);
  12115. }
  12116. else {
  12117. switch (value_type) {
  12118. case VALUE_TYPE_I32:
  12119. case PACKED_TYPE_I8:
  12120. case PACKED_TYPE_I16:
  12121. POP_I32();
  12122. break;
  12123. case VALUE_TYPE_I64:
  12124. POP_I64();
  12125. break;
  12126. case VALUE_TYPE_F32:
  12127. POP_F32();
  12128. break;
  12129. case VALUE_TYPE_F64:
  12130. POP_F64();
  12131. break;
  12132. default:
  12133. set_error_buf(error_buf,
  12134. error_buf_size,
  12135. "unknown type");
  12136. goto fail;
  12137. }
  12138. }
  12139. }
  12140. }
  12141. /* PUSH struct obj, (ref $t) */
  12142. wasm_set_refheaptype_typeidx(
  12143. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12144. PUSH_REF(wasm_ref_type.ref_type);
  12145. break;
  12146. }
  12147. case WASM_OP_STRUCT_GET:
  12148. case WASM_OP_STRUCT_GET_S:
  12149. case WASM_OP_STRUCT_GET_U:
  12150. case WASM_OP_STRUCT_SET:
  12151. {
  12152. WASMStructType *struct_type;
  12153. WASMRefType *ref_type = NULL;
  12154. uint32 field_idx;
  12155. uint8 field_type;
  12156. read_leb_uint32(p, p_end, type_idx);
  12157. #if WASM_ENABLE_FAST_INTERP != 0
  12158. emit_uint32(loader_ctx, type_idx);
  12159. #endif
  12160. if (!check_type_index(module, module->type_count,
  12161. type_idx, error_buf,
  12162. error_buf_size)) {
  12163. goto fail;
  12164. }
  12165. if (module->types[type_idx]->type_flag
  12166. != WASM_TYPE_STRUCT) {
  12167. set_error_buf(error_buf, error_buf_size,
  12168. "unknown struct type");
  12169. goto fail;
  12170. }
  12171. struct_type = (WASMStructType *)module->types[type_idx];
  12172. read_leb_uint32(p, p_end, field_idx);
  12173. #if WASM_ENABLE_FAST_INTERP != 0
  12174. emit_uint32(loader_ctx, field_idx);
  12175. #endif
  12176. if (field_idx >= struct_type->field_count) {
  12177. set_error_buf(error_buf, error_buf_size,
  12178. "unknown struct field");
  12179. goto fail;
  12180. }
  12181. if (opcode1 == WASM_OP_STRUCT_SET
  12182. && !(struct_type->fields[field_idx].field_flags
  12183. & 1)) {
  12184. set_error_buf(error_buf, error_buf_size,
  12185. "field is immutable");
  12186. goto fail;
  12187. }
  12188. field_type = struct_type->fields[field_idx].field_type;
  12189. if (is_packed_type(field_type)) {
  12190. if (opcode1 == WASM_OP_STRUCT_GET) {
  12191. set_error_buf(error_buf, error_buf_size,
  12192. "type mismatch");
  12193. goto fail;
  12194. }
  12195. else {
  12196. field_type = VALUE_TYPE_I32;
  12197. }
  12198. }
  12199. if (wasm_is_type_multi_byte_type(field_type)) {
  12200. ref_type = wasm_reftype_map_find(
  12201. struct_type->ref_type_maps,
  12202. struct_type->ref_type_map_count, field_idx);
  12203. bh_assert(ref_type);
  12204. }
  12205. if (opcode1 == WASM_OP_STRUCT_SET) {
  12206. /* POP field */
  12207. if (wasm_is_type_multi_byte_type(field_type)) {
  12208. bh_memcpy_s(&wasm_ref_type,
  12209. (uint32)sizeof(WASMRefType),
  12210. ref_type,
  12211. wasm_reftype_struct_size(ref_type));
  12212. }
  12213. POP_REF(field_type);
  12214. /* POP struct obj, (ref null $t) */
  12215. wasm_set_refheaptype_typeidx(
  12216. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12217. POP_REF(wasm_ref_type.ref_type);
  12218. }
  12219. else {
  12220. /* POP struct obj, (ref null $t) */
  12221. wasm_set_refheaptype_typeidx(
  12222. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12223. POP_REF(wasm_ref_type.ref_type);
  12224. /* PUSH field */
  12225. if (wasm_is_type_multi_byte_type(field_type)) {
  12226. bh_memcpy_s(&wasm_ref_type,
  12227. (uint32)sizeof(WASMRefType),
  12228. ref_type,
  12229. wasm_reftype_struct_size(ref_type));
  12230. }
  12231. PUSH_REF(field_type);
  12232. }
  12233. break;
  12234. }
  12235. case WASM_OP_ARRAY_NEW:
  12236. case WASM_OP_ARRAY_NEW_DEFAULT:
  12237. case WASM_OP_ARRAY_NEW_FIXED:
  12238. case WASM_OP_ARRAY_NEW_DATA:
  12239. case WASM_OP_ARRAY_NEW_ELEM:
  12240. {
  12241. WASMArrayType *array_type;
  12242. uint8 elem_type;
  12243. uint32 u32 = 0;
  12244. read_leb_uint32(p, p_end, type_idx);
  12245. #if WASM_ENABLE_FAST_INTERP != 0
  12246. emit_uint32(loader_ctx, type_idx);
  12247. #endif
  12248. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED
  12249. || opcode1 == WASM_OP_ARRAY_NEW_DATA
  12250. || opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12251. read_leb_uint32(p, p_end, u32);
  12252. #if WASM_ENABLE_FAST_INTERP != 0
  12253. emit_uint32(loader_ctx, u32);
  12254. #endif
  12255. }
  12256. if (!check_array_type(module, type_idx, error_buf,
  12257. error_buf_size)) {
  12258. goto fail;
  12259. }
  12260. if (opcode1 != WASM_OP_ARRAY_NEW_FIXED) {
  12261. /* length */
  12262. POP_I32();
  12263. }
  12264. array_type = (WASMArrayType *)module->types[type_idx];
  12265. elem_type = array_type->elem_type;
  12266. if (opcode1 == WASM_OP_ARRAY_NEW
  12267. || opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12268. if (wasm_is_type_multi_byte_type(elem_type)) {
  12269. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12270. array_type->elem_ref_type,
  12271. wasm_reftype_struct_size(
  12272. array_type->elem_ref_type));
  12273. }
  12274. if (is_packed_type(elem_type)) {
  12275. elem_type = VALUE_TYPE_I32;
  12276. }
  12277. if (opcode1 == WASM_OP_ARRAY_NEW_FIXED) {
  12278. uint32 N = u32;
  12279. for (i = 0; i < N; i++) {
  12280. if (wasm_is_type_multi_byte_type(
  12281. elem_type)) {
  12282. bh_memcpy_s(
  12283. &wasm_ref_type, sizeof(WASMRefType),
  12284. array_type->elem_ref_type,
  12285. wasm_reftype_struct_size(
  12286. array_type->elem_ref_type));
  12287. }
  12288. POP_REF(elem_type);
  12289. }
  12290. }
  12291. else
  12292. POP_REF(elem_type);
  12293. }
  12294. else if (opcode1 == WASM_OP_ARRAY_NEW_DATA) {
  12295. /* offset of data segment */
  12296. POP_I32();
  12297. if (u32 >= module->data_seg_count) {
  12298. set_error_buf(error_buf, error_buf_size,
  12299. "unknown data segment");
  12300. goto fail;
  12301. }
  12302. if (wasm_is_type_reftype(elem_type)) {
  12303. set_error_buf(error_buf, error_buf_size,
  12304. "array elem type mismatch");
  12305. goto fail;
  12306. }
  12307. }
  12308. else if (opcode1 == WASM_OP_ARRAY_NEW_ELEM) {
  12309. WASMTableSeg *table_seg =
  12310. module->table_segments + u32;
  12311. /* offset of element segment */
  12312. POP_I32();
  12313. if (u32 >= module->table_seg_count) {
  12314. set_error_buf(error_buf, error_buf_size,
  12315. "unknown element segment");
  12316. goto fail;
  12317. }
  12318. if (!wasm_reftype_is_subtype_of(
  12319. table_seg->elem_type,
  12320. table_seg->elem_ref_type, elem_type,
  12321. array_type->elem_ref_type, module->types,
  12322. module->type_count)) {
  12323. set_error_buf(error_buf, error_buf_size,
  12324. "array elem type mismatch");
  12325. goto fail;
  12326. }
  12327. }
  12328. /* PUSH array obj, (ref $t) */
  12329. wasm_set_refheaptype_typeidx(
  12330. &wasm_ref_type.ref_ht_typeidx, false, type_idx);
  12331. PUSH_REF(wasm_ref_type.ref_type);
  12332. break;
  12333. }
  12334. case WASM_OP_ARRAY_GET:
  12335. case WASM_OP_ARRAY_GET_S:
  12336. case WASM_OP_ARRAY_GET_U:
  12337. case WASM_OP_ARRAY_SET:
  12338. {
  12339. uint8 elem_type;
  12340. WASMArrayType *array_type;
  12341. WASMRefType *ref_type = NULL;
  12342. read_leb_uint32(p, p_end, type_idx);
  12343. #if WASM_ENABLE_FAST_INTERP != 0
  12344. emit_uint32(loader_ctx, type_idx);
  12345. #endif
  12346. if (!check_array_type(module, type_idx, error_buf,
  12347. error_buf_size)) {
  12348. goto fail;
  12349. }
  12350. array_type = (WASMArrayType *)module->types[type_idx];
  12351. if (opcode1 == WASM_OP_ARRAY_SET
  12352. && !(array_type->elem_flags & 1)) {
  12353. set_error_buf(error_buf, error_buf_size,
  12354. "array is immutable");
  12355. goto fail;
  12356. }
  12357. elem_type = array_type->elem_type;
  12358. if (is_packed_type(elem_type)) {
  12359. if (opcode1 != WASM_OP_ARRAY_GET_S
  12360. && opcode1 != WASM_OP_ARRAY_GET_U
  12361. && opcode1 != WASM_OP_ARRAY_SET) {
  12362. set_error_buf(error_buf, error_buf_size,
  12363. "type mismatch");
  12364. goto fail;
  12365. }
  12366. else {
  12367. elem_type = VALUE_TYPE_I32;
  12368. }
  12369. }
  12370. ref_type = array_type->elem_ref_type;
  12371. if (opcode1 == WASM_OP_ARRAY_SET) {
  12372. /* POP elem to set */
  12373. if (wasm_is_type_multi_byte_type(elem_type)) {
  12374. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12375. ref_type,
  12376. wasm_reftype_struct_size(ref_type));
  12377. }
  12378. POP_REF(elem_type);
  12379. }
  12380. /* elem idx */
  12381. POP_I32();
  12382. /* POP array obj, (ref null $t) */
  12383. wasm_set_refheaptype_typeidx(
  12384. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12385. POP_REF(wasm_ref_type.ref_type);
  12386. if (opcode1 != WASM_OP_ARRAY_SET) {
  12387. /* PUSH elem */
  12388. if (wasm_is_type_multi_byte_type(elem_type)) {
  12389. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  12390. ref_type,
  12391. wasm_reftype_struct_size(ref_type));
  12392. }
  12393. PUSH_REF(elem_type);
  12394. }
  12395. break;
  12396. }
  12397. case WASM_OP_ARRAY_LEN:
  12398. {
  12399. POP_REF(REF_TYPE_ARRAYREF);
  12400. /* length */
  12401. PUSH_I32();
  12402. break;
  12403. }
  12404. case WASM_OP_ARRAY_FILL:
  12405. {
  12406. WASMArrayType *array_type;
  12407. uint8 elem_type;
  12408. /* typeidx */
  12409. read_leb_uint32(p, p_end, type_idx);
  12410. #if WASM_ENABLE_FAST_INTERP != 0
  12411. emit_uint32(loader_ctx, type_idx);
  12412. #endif
  12413. if (!check_array_type(module, type_idx, error_buf,
  12414. error_buf_size)) {
  12415. goto fail;
  12416. }
  12417. array_type = (WASMArrayType *)module->types[type_idx];
  12418. if (!(array_type->elem_flags & 1)) {
  12419. set_error_buf(error_buf, error_buf_size,
  12420. "array is immutable");
  12421. goto fail;
  12422. }
  12423. elem_type = array_type->elem_type;
  12424. if (is_packed_type(elem_type)) {
  12425. elem_type = VALUE_TYPE_I32;
  12426. }
  12427. POP_I32(); /* length */
  12428. #if WASM_ENABLE_FAST_INTERP != 0
  12429. POP_OFFSET_TYPE(elem_type);
  12430. #endif
  12431. POP_TYPE(elem_type);
  12432. POP_I32(); /* start */
  12433. /* POP array obj, (ref null $t) */
  12434. wasm_set_refheaptype_typeidx(
  12435. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12436. POP_REF(wasm_ref_type.ref_type);
  12437. break;
  12438. }
  12439. case WASM_OP_ARRAY_COPY:
  12440. {
  12441. uint32 src_type_idx;
  12442. uint8 src_elem_type, dst_elem_type;
  12443. WASMRefType src_ref_type = { 0 },
  12444. *src_elem_ref_type = NULL;
  12445. WASMRefType dst_ref_type = { 0 },
  12446. *dst_elem_ref_type = NULL;
  12447. WASMArrayType *array_type;
  12448. /* typeidx1 */
  12449. read_leb_uint32(p, p_end, type_idx);
  12450. #if WASM_ENABLE_FAST_INTERP != 0
  12451. emit_uint32(loader_ctx, type_idx);
  12452. #endif
  12453. /* typeidx2 */
  12454. read_leb_uint32(p, p_end, src_type_idx);
  12455. #if WASM_ENABLE_FAST_INTERP != 0
  12456. emit_uint32(loader_ctx, src_type_idx);
  12457. #endif
  12458. if (!check_array_type(module, type_idx, error_buf,
  12459. error_buf_size)) {
  12460. goto fail;
  12461. }
  12462. if (!check_array_type(module, src_type_idx, error_buf,
  12463. error_buf_size)) {
  12464. goto fail;
  12465. }
  12466. POP_I32();
  12467. POP_I32();
  12468. /* POP array obj, (ref null $t) */
  12469. wasm_set_refheaptype_typeidx(
  12470. &wasm_ref_type.ref_ht_typeidx, true, src_type_idx);
  12471. POP_REF(wasm_ref_type.ref_type);
  12472. bh_memcpy_s(&src_ref_type, (uint32)sizeof(WASMRefType),
  12473. &wasm_ref_type,
  12474. wasm_reftype_struct_size(&wasm_ref_type));
  12475. POP_I32();
  12476. /* POP array obj, (ref null $t) */
  12477. wasm_set_refheaptype_typeidx(
  12478. &wasm_ref_type.ref_ht_typeidx, true, type_idx);
  12479. POP_REF(wasm_ref_type.ref_type);
  12480. bh_memcpy_s(&dst_ref_type, (uint32)sizeof(WASMRefType),
  12481. &wasm_ref_type,
  12482. wasm_reftype_struct_size(&wasm_ref_type));
  12483. array_type = (WASMArrayType *)module->types[type_idx];
  12484. if (!(array_type->elem_flags & 1)) {
  12485. set_error_buf(error_buf, error_buf_size,
  12486. "destination array is immutable");
  12487. goto fail;
  12488. }
  12489. dst_elem_type = array_type->elem_type;
  12490. if (wasm_is_type_multi_byte_type(dst_elem_type)) {
  12491. dst_elem_ref_type = array_type->elem_ref_type;
  12492. }
  12493. array_type =
  12494. (WASMArrayType *)module->types[src_type_idx];
  12495. src_elem_type = array_type->elem_type;
  12496. if (wasm_is_type_multi_byte_type(src_elem_type)) {
  12497. src_elem_ref_type = array_type->elem_ref_type;
  12498. }
  12499. if (!wasm_reftype_is_subtype_of(
  12500. src_elem_type, src_elem_ref_type, dst_elem_type,
  12501. dst_elem_ref_type, module->types,
  12502. module->type_count)) {
  12503. set_error_buf(error_buf, error_buf_size,
  12504. "array types do not match");
  12505. goto fail;
  12506. }
  12507. break;
  12508. }
  12509. case WASM_OP_REF_I31:
  12510. {
  12511. POP_I32();
  12512. wasm_set_refheaptype_common(
  12513. &wasm_ref_type.ref_ht_common, false, HEAP_TYPE_I31);
  12514. PUSH_REF(wasm_ref_type.ref_type);
  12515. break;
  12516. }
  12517. case WASM_OP_I31_GET_S:
  12518. case WASM_OP_I31_GET_U:
  12519. {
  12520. POP_REF(REF_TYPE_I31REF);
  12521. PUSH_I32();
  12522. break;
  12523. }
  12524. case WASM_OP_REF_TEST:
  12525. case WASM_OP_REF_CAST:
  12526. case WASM_OP_REF_TEST_NULLABLE:
  12527. case WASM_OP_REF_CAST_NULLABLE:
  12528. {
  12529. uint8 type;
  12530. read_leb_int32(p, p_end, heap_type);
  12531. #if WASM_ENABLE_FAST_INTERP != 0
  12532. emit_uint32(loader_ctx, (uint32)heap_type);
  12533. #endif
  12534. if (heap_type >= 0) {
  12535. if (!check_type_index(module, module->type_count,
  12536. heap_type, error_buf,
  12537. error_buf_size)) {
  12538. goto fail;
  12539. }
  12540. }
  12541. else {
  12542. if (!wasm_is_valid_heap_type(heap_type)) {
  12543. set_error_buf(error_buf, error_buf_size,
  12544. "unknown type");
  12545. goto fail;
  12546. }
  12547. }
  12548. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12549. &wasm_ref_type, error_buf,
  12550. error_buf_size)) {
  12551. goto fail;
  12552. }
  12553. if (opcode1 == WASM_OP_REF_TEST
  12554. || opcode1 == WASM_OP_REF_TEST_NULLABLE)
  12555. PUSH_I32();
  12556. else {
  12557. bool nullable =
  12558. (opcode1 == WASM_OP_REF_CAST_NULLABLE) ? true
  12559. : false;
  12560. if (heap_type >= 0 || !nullable) {
  12561. wasm_set_refheaptype_typeidx(
  12562. &wasm_ref_type.ref_ht_typeidx, nullable,
  12563. heap_type);
  12564. PUSH_REF(wasm_ref_type.ref_type);
  12565. }
  12566. else {
  12567. PUSH_REF((uint8)((int32)0x80 + heap_type));
  12568. }
  12569. }
  12570. break;
  12571. }
  12572. case WASM_OP_BR_ON_CAST:
  12573. case WASM_OP_BR_ON_CAST_FAIL:
  12574. {
  12575. WASMRefType ref_type_tmp = { 0 }, ref_type1 = { 0 },
  12576. ref_type2 = { 0 }, ref_type_diff = { 0 };
  12577. uint8 type_tmp, castflags;
  12578. uint32 depth;
  12579. int32 heap_type_dst;
  12580. bool src_nullable, dst_nullable;
  12581. CHECK_BUF(p, p_end, 1);
  12582. castflags = read_uint8(p);
  12583. #if WASM_ENABLE_FAST_INTERP != 0
  12584. /* Emit heap_type firstly */
  12585. emit_byte(loader_ctx, castflags);
  12586. #endif
  12587. p_org = p;
  12588. read_leb_uint32(p, p_end, depth);
  12589. read_leb_int32(p, p_end, heap_type);
  12590. #if WASM_ENABLE_FAST_INTERP != 0
  12591. /* Emit heap_type firstly */
  12592. emit_uint32(loader_ctx, (uint32)heap_type);
  12593. #endif
  12594. read_leb_int32(p, p_end, heap_type_dst);
  12595. #if WASM_ENABLE_FAST_INTERP != 0
  12596. /* Emit heap_type firstly */
  12597. emit_uint32(loader_ctx, (uint32)heap_type_dst);
  12598. #endif
  12599. (void)depth;
  12600. /*
  12601. * castflags should be 0~3:
  12602. * 0: (non-null, non-null)
  12603. * 1: (null, non-null)
  12604. * 2: (non-null, null)
  12605. * 3: (null, null)
  12606. */
  12607. if (castflags > 3) {
  12608. set_error_buf(error_buf, error_buf_size,
  12609. "invalid castflags");
  12610. break;
  12611. }
  12612. src_nullable =
  12613. (castflags == 1) || (castflags == 3) ? true : false;
  12614. dst_nullable =
  12615. (castflags == 2) || (castflags == 3) ? true : false;
  12616. /* Pop and backup the stack top's ref type */
  12617. if (!wasm_loader_pop_heap_obj(loader_ctx, &type_tmp,
  12618. &ref_type_tmp, error_buf,
  12619. error_buf_size)) {
  12620. goto fail;
  12621. }
  12622. /* The reference type rt1 must be valid */
  12623. if (!init_ref_type(module, &ref_type1, src_nullable,
  12624. heap_type, error_buf,
  12625. error_buf_size)) {
  12626. goto fail;
  12627. }
  12628. /* The reference type rt2 must be valid. */
  12629. if (!init_ref_type(module, &ref_type2, dst_nullable,
  12630. heap_type_dst, error_buf,
  12631. error_buf_size)) {
  12632. goto fail;
  12633. }
  12634. calculate_reftype_diff(&ref_type_diff, &ref_type1,
  12635. &ref_type2);
  12636. /* The reference type rt2 must match rt1. */
  12637. if (!wasm_reftype_is_subtype_of(
  12638. ref_type2.ref_type, &ref_type2,
  12639. ref_type1.ref_type, &ref_type1, module->types,
  12640. module->type_count)) {
  12641. set_error_buf(error_buf, error_buf_size,
  12642. "type mismatch");
  12643. goto fail;
  12644. }
  12645. p = p_org;
  12646. /* Push ref type casted for branch block check */
  12647. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12648. /* The reference type rt2 must match rt′. */
  12649. type_tmp = ref_type2.ref_type;
  12650. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12651. bh_memcpy_s(
  12652. &wasm_ref_type,
  12653. wasm_reftype_struct_size(&ref_type2),
  12654. &ref_type2,
  12655. wasm_reftype_struct_size(&ref_type2));
  12656. }
  12657. }
  12658. else {
  12659. /* The reference type rt′1 must match rt′. */
  12660. type_tmp = ref_type_diff.ref_type;
  12661. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12662. bh_memcpy_s(
  12663. &wasm_ref_type,
  12664. wasm_reftype_struct_size(&ref_type_diff),
  12665. &ref_type_diff,
  12666. wasm_reftype_struct_size(&ref_type_diff));
  12667. }
  12668. }
  12669. PUSH_REF(type_tmp);
  12670. if (!(frame_csp_tmp = check_branch_block(
  12671. loader_ctx, &p, p_end, opcode, error_buf,
  12672. error_buf_size))) {
  12673. goto fail;
  12674. }
  12675. /* Ignore heap_types */
  12676. skip_leb_uint32(p, p_end);
  12677. skip_leb_uint32(p, p_end);
  12678. /* Restore the original stack top's ref type */
  12679. POP_REF(type_tmp);
  12680. #if WASM_ENABLE_FAST_INTERP != 0
  12681. /* Erase the opnd offset emitted by POP_REF() */
  12682. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12683. #endif
  12684. if (opcode1 == WASM_OP_BR_ON_CAST) {
  12685. type_tmp = ref_type_diff.ref_type;
  12686. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12687. bh_memcpy_s(
  12688. &wasm_ref_type,
  12689. wasm_reftype_struct_size(&ref_type_diff),
  12690. &ref_type_diff,
  12691. wasm_reftype_struct_size(&ref_type_diff));
  12692. }
  12693. }
  12694. else {
  12695. type_tmp = ref_type2.ref_type;
  12696. if (wasm_is_type_multi_byte_type(type_tmp)) {
  12697. bh_memcpy_s(
  12698. &wasm_ref_type,
  12699. wasm_reftype_struct_size(&ref_type2),
  12700. &ref_type2,
  12701. wasm_reftype_struct_size(&ref_type2));
  12702. }
  12703. }
  12704. PUSH_REF(type_tmp);
  12705. #if WASM_ENABLE_FAST_INTERP != 0
  12706. /* Erase the opnd offset emitted by PUSH_REF() */
  12707. wasm_loader_emit_backspace(loader_ctx, sizeof(uint16));
  12708. #endif
  12709. break;
  12710. }
  12711. case WASM_OP_ANY_CONVERT_EXTERN:
  12712. {
  12713. uint8 type;
  12714. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12715. &wasm_ref_type, error_buf,
  12716. error_buf_size)) {
  12717. goto fail;
  12718. }
  12719. if (!(type == REF_TYPE_EXTERNREF
  12720. || (type == REF_TYPE_HT_NON_NULLABLE
  12721. && wasm_ref_type.ref_ht_common.heap_type
  12722. == HEAP_TYPE_EXTERN)
  12723. || type == VALUE_TYPE_ANY)) {
  12724. set_error_buf(error_buf, error_buf_size,
  12725. "type mismatch");
  12726. goto fail;
  12727. }
  12728. if (type == REF_TYPE_EXTERNREF)
  12729. type = REF_TYPE_ANYREF;
  12730. else {
  12731. wasm_ref_type.ref_ht_common.heap_type =
  12732. HEAP_TYPE_ANY;
  12733. }
  12734. PUSH_REF(type);
  12735. break;
  12736. }
  12737. case WASM_OP_EXTERN_CONVERT_ANY:
  12738. {
  12739. uint8 type;
  12740. if (!wasm_loader_pop_heap_obj(loader_ctx, &type,
  12741. &wasm_ref_type, error_buf,
  12742. error_buf_size)) {
  12743. goto fail;
  12744. }
  12745. if (type == REF_TYPE_EXTERNREF
  12746. || ((type == REF_TYPE_HT_NULLABLE
  12747. || type == REF_TYPE_HT_NON_NULLABLE)
  12748. && wasm_ref_type.ref_ht_common.heap_type
  12749. == HEAP_TYPE_EXTERN)) {
  12750. set_error_buf(error_buf, error_buf_size,
  12751. "type mismatch");
  12752. goto fail;
  12753. }
  12754. if (type != REF_TYPE_HT_NON_NULLABLE) {
  12755. /* push (ref null extern) */
  12756. type = REF_TYPE_EXTERNREF;
  12757. }
  12758. else {
  12759. /* push (ref extern) */
  12760. type = REF_TYPE_HT_NON_NULLABLE;
  12761. wasm_set_refheaptype_common(
  12762. &wasm_ref_type.ref_ht_common, false,
  12763. HEAP_TYPE_EXTERN);
  12764. }
  12765. PUSH_REF(type);
  12766. break;
  12767. }
  12768. #if WASM_ENABLE_STRINGREF != 0
  12769. case WASM_OP_STRING_NEW_UTF8:
  12770. case WASM_OP_STRING_NEW_WTF16:
  12771. case WASM_OP_STRING_NEW_LOSSY_UTF8:
  12772. case WASM_OP_STRING_NEW_WTF8:
  12773. {
  12774. uint32 memidx;
  12775. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12776. func->has_memory_operations = true;
  12777. #endif
  12778. read_leb_uint32(p, p_end, memidx);
  12779. #if WASM_ENABLE_FAST_INTERP != 0
  12780. emit_uint32(loader_ctx, (uint32)memidx);
  12781. #endif
  12782. POP_I32();
  12783. POP_I32();
  12784. PUSH_REF(REF_TYPE_STRINGREF);
  12785. (void)memidx;
  12786. break;
  12787. }
  12788. case WASM_OP_STRING_CONST:
  12789. {
  12790. uint32 contents;
  12791. read_leb_uint32(p, p_end, contents);
  12792. #if WASM_ENABLE_FAST_INTERP != 0
  12793. emit_uint32(loader_ctx, (uint32)contents);
  12794. #endif
  12795. PUSH_REF(REF_TYPE_STRINGREF);
  12796. (void)contents;
  12797. break;
  12798. }
  12799. case WASM_OP_STRING_MEASURE_UTF8:
  12800. case WASM_OP_STRING_MEASURE_WTF8:
  12801. case WASM_OP_STRING_MEASURE_WTF16:
  12802. {
  12803. POP_STRINGREF();
  12804. PUSH_I32();
  12805. break;
  12806. }
  12807. case WASM_OP_STRING_ENCODE_UTF8:
  12808. case WASM_OP_STRING_ENCODE_WTF16:
  12809. case WASM_OP_STRING_ENCODE_LOSSY_UTF8:
  12810. case WASM_OP_STRING_ENCODE_WTF8:
  12811. {
  12812. uint32 memidx;
  12813. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12814. func->has_memory_operations = true;
  12815. #endif
  12816. read_leb_uint32(p, p_end, memidx);
  12817. #if WASM_ENABLE_FAST_INTERP != 0
  12818. emit_uint32(loader_ctx, (uint32)memidx);
  12819. #endif
  12820. POP_I32();
  12821. POP_STRINGREF();
  12822. PUSH_I32();
  12823. (void)memidx;
  12824. break;
  12825. }
  12826. case WASM_OP_STRING_CONCAT:
  12827. {
  12828. POP_STRINGREF();
  12829. POP_STRINGREF();
  12830. PUSH_REF(REF_TYPE_STRINGREF);
  12831. break;
  12832. }
  12833. case WASM_OP_STRING_EQ:
  12834. {
  12835. POP_STRINGREF();
  12836. POP_STRINGREF();
  12837. PUSH_I32();
  12838. break;
  12839. }
  12840. case WASM_OP_STRING_IS_USV_SEQUENCE:
  12841. {
  12842. POP_STRINGREF();
  12843. PUSH_I32();
  12844. break;
  12845. }
  12846. case WASM_OP_STRING_AS_WTF8:
  12847. {
  12848. POP_STRINGREF();
  12849. PUSH_REF(REF_TYPE_STRINGVIEWWTF8);
  12850. break;
  12851. }
  12852. case WASM_OP_STRINGVIEW_WTF8_ADVANCE:
  12853. {
  12854. POP_I32();
  12855. POP_I32();
  12856. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12857. PUSH_I32();
  12858. break;
  12859. }
  12860. case WASM_OP_STRINGVIEW_WTF8_ENCODE_UTF8:
  12861. case WASM_OP_STRINGVIEW_WTF8_ENCODE_LOSSY_UTF8:
  12862. case WASM_OP_STRINGVIEW_WTF8_ENCODE_WTF8:
  12863. {
  12864. uint32 memidx;
  12865. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12866. func->has_memory_operations = true;
  12867. #endif
  12868. read_leb_uint32(p, p_end, memidx);
  12869. #if WASM_ENABLE_FAST_INTERP != 0
  12870. emit_uint32(loader_ctx, (uint32)memidx);
  12871. #endif
  12872. POP_I32();
  12873. POP_I32();
  12874. POP_I32();
  12875. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12876. PUSH_I32();
  12877. PUSH_I32();
  12878. (void)memidx;
  12879. break;
  12880. }
  12881. case WASM_OP_STRINGVIEW_WTF8_SLICE:
  12882. {
  12883. POP_I32();
  12884. POP_I32();
  12885. POP_REF(REF_TYPE_STRINGVIEWWTF8);
  12886. PUSH_REF(REF_TYPE_STRINGREF);
  12887. break;
  12888. }
  12889. case WASM_OP_STRING_AS_WTF16:
  12890. {
  12891. POP_STRINGREF();
  12892. PUSH_REF(REF_TYPE_STRINGVIEWWTF16);
  12893. break;
  12894. }
  12895. case WASM_OP_STRINGVIEW_WTF16_LENGTH:
  12896. {
  12897. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12898. PUSH_I32();
  12899. break;
  12900. }
  12901. case WASM_OP_STRINGVIEW_WTF16_GET_CODEUNIT:
  12902. {
  12903. POP_I32();
  12904. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12905. PUSH_I32();
  12906. break;
  12907. }
  12908. case WASM_OP_STRINGVIEW_WTF16_ENCODE:
  12909. {
  12910. uint32 memidx;
  12911. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  12912. func->has_memory_operations = true;
  12913. #endif
  12914. read_leb_uint32(p, p_end, memidx);
  12915. #if WASM_ENABLE_FAST_INTERP != 0
  12916. emit_uint32(loader_ctx, (uint32)memidx);
  12917. #endif
  12918. POP_I32();
  12919. POP_I32();
  12920. POP_I32();
  12921. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12922. PUSH_I32();
  12923. (void)memidx;
  12924. break;
  12925. }
  12926. case WASM_OP_STRINGVIEW_WTF16_SLICE:
  12927. {
  12928. POP_I32();
  12929. POP_I32();
  12930. POP_REF(REF_TYPE_STRINGVIEWWTF16);
  12931. PUSH_REF(REF_TYPE_STRINGREF);
  12932. break;
  12933. }
  12934. case WASM_OP_STRING_AS_ITER:
  12935. {
  12936. POP_STRINGREF();
  12937. PUSH_REF(REF_TYPE_STRINGVIEWITER);
  12938. break;
  12939. }
  12940. case WASM_OP_STRINGVIEW_ITER_NEXT:
  12941. {
  12942. POP_REF(REF_TYPE_STRINGVIEWITER);
  12943. PUSH_I32();
  12944. break;
  12945. }
  12946. case WASM_OP_STRINGVIEW_ITER_ADVANCE:
  12947. case WASM_OP_STRINGVIEW_ITER_REWIND:
  12948. {
  12949. POP_I32();
  12950. POP_REF(REF_TYPE_STRINGVIEWITER);
  12951. PUSH_I32();
  12952. break;
  12953. }
  12954. case WASM_OP_STRINGVIEW_ITER_SLICE:
  12955. {
  12956. POP_I32();
  12957. POP_REF(REF_TYPE_STRINGVIEWITER);
  12958. PUSH_REF(REF_TYPE_STRINGREF);
  12959. break;
  12960. }
  12961. case WASM_OP_STRING_NEW_UTF8_ARRAY:
  12962. case WASM_OP_STRING_NEW_WTF16_ARRAY:
  12963. case WASM_OP_STRING_NEW_LOSSY_UTF8_ARRAY:
  12964. case WASM_OP_STRING_NEW_WTF8_ARRAY:
  12965. {
  12966. POP_I32();
  12967. POP_I32();
  12968. POP_REF(REF_TYPE_ARRAYREF);
  12969. PUSH_REF(REF_TYPE_STRINGREF);
  12970. break;
  12971. }
  12972. case WASM_OP_STRING_ENCODE_UTF8_ARRAY:
  12973. case WASM_OP_STRING_ENCODE_WTF16_ARRAY:
  12974. case WASM_OP_STRING_ENCODE_LOSSY_UTF8_ARRAY:
  12975. case WASM_OP_STRING_ENCODE_WTF8_ARRAY:
  12976. {
  12977. POP_I32();
  12978. POP_REF(REF_TYPE_ARRAYREF);
  12979. POP_STRINGREF();
  12980. PUSH_I32();
  12981. break;
  12982. }
  12983. #endif /* end of WASM_ENABLE_STRINGREF != 0 */
  12984. default:
  12985. set_error_buf_v(error_buf, error_buf_size,
  12986. "%s %02x %02x", "unsupported opcode",
  12987. 0xfb, opcode1);
  12988. goto fail;
  12989. }
  12990. break;
  12991. }
  12992. #endif /* end of WASM_ENABLE_GC != 0 */
  12993. case WASM_OP_MISC_PREFIX:
  12994. {
  12995. uint32 opcode1;
  12996. read_leb_uint32(p, p_end, opcode1);
  12997. #if WASM_ENABLE_FAST_INTERP != 0
  12998. emit_byte(loader_ctx, ((uint8)opcode1));
  12999. #endif
  13000. switch (opcode1) {
  13001. case WASM_OP_I32_TRUNC_SAT_S_F32:
  13002. case WASM_OP_I32_TRUNC_SAT_U_F32:
  13003. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I32);
  13004. break;
  13005. case WASM_OP_I32_TRUNC_SAT_S_F64:
  13006. case WASM_OP_I32_TRUNC_SAT_U_F64:
  13007. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I32);
  13008. break;
  13009. case WASM_OP_I64_TRUNC_SAT_S_F32:
  13010. case WASM_OP_I64_TRUNC_SAT_U_F32:
  13011. POP_AND_PUSH(VALUE_TYPE_F32, VALUE_TYPE_I64);
  13012. break;
  13013. case WASM_OP_I64_TRUNC_SAT_S_F64:
  13014. case WASM_OP_I64_TRUNC_SAT_U_F64:
  13015. POP_AND_PUSH(VALUE_TYPE_F64, VALUE_TYPE_I64);
  13016. break;
  13017. #if WASM_ENABLE_BULK_MEMORY != 0
  13018. case WASM_OP_MEMORY_INIT:
  13019. {
  13020. read_leb_uint32(p, p_end, data_seg_idx);
  13021. #if WASM_ENABLE_FAST_INTERP != 0
  13022. emit_uint32(loader_ctx, data_seg_idx);
  13023. #endif
  13024. if (module->import_memory_count == 0
  13025. && module->memory_count == 0)
  13026. goto fail_unknown_memory;
  13027. if (*p++ != 0x00)
  13028. goto fail_zero_byte_expected;
  13029. if (data_seg_idx >= module->data_seg_count) {
  13030. set_error_buf_v(error_buf, error_buf_size,
  13031. "unknown data segment %d",
  13032. data_seg_idx);
  13033. goto fail;
  13034. }
  13035. if (module->data_seg_count1 == 0)
  13036. goto fail_data_cnt_sec_require;
  13037. POP_I32();
  13038. POP_I32();
  13039. POP_MEM_OFFSET();
  13040. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13041. func->has_memory_operations = true;
  13042. #endif
  13043. #if WASM_ENABLE_WAMR_COMPILER != 0
  13044. module->is_bulk_memory_used = true;
  13045. #endif
  13046. break;
  13047. }
  13048. case WASM_OP_DATA_DROP:
  13049. {
  13050. read_leb_uint32(p, p_end, data_seg_idx);
  13051. #if WASM_ENABLE_FAST_INTERP != 0
  13052. emit_uint32(loader_ctx, data_seg_idx);
  13053. #endif
  13054. if (data_seg_idx >= module->data_seg_count) {
  13055. set_error_buf(error_buf, error_buf_size,
  13056. "unknown data segment");
  13057. goto fail;
  13058. }
  13059. if (module->data_seg_count1 == 0)
  13060. goto fail_data_cnt_sec_require;
  13061. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13062. func->has_memory_operations = true;
  13063. #endif
  13064. #if WASM_ENABLE_WAMR_COMPILER != 0
  13065. module->is_bulk_memory_used = true;
  13066. #endif
  13067. break;
  13068. }
  13069. case WASM_OP_MEMORY_COPY:
  13070. {
  13071. CHECK_BUF(p, p_end, sizeof(int16));
  13072. /* both src and dst memory index should be 0 */
  13073. if (*(int16 *)p != 0x0000)
  13074. goto fail_zero_byte_expected;
  13075. p += 2;
  13076. if (module->import_memory_count == 0
  13077. && module->memory_count == 0)
  13078. goto fail_unknown_memory;
  13079. POP_MEM_OFFSET();
  13080. POP_MEM_OFFSET();
  13081. POP_MEM_OFFSET();
  13082. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13083. func->has_memory_operations = true;
  13084. #endif
  13085. #if WASM_ENABLE_WAMR_COMPILER != 0
  13086. module->is_bulk_memory_used = true;
  13087. #endif
  13088. break;
  13089. }
  13090. case WASM_OP_MEMORY_FILL:
  13091. {
  13092. if (*p++ != 0x00) {
  13093. goto fail_zero_byte_expected;
  13094. }
  13095. if (module->import_memory_count == 0
  13096. && module->memory_count == 0) {
  13097. goto fail_unknown_memory;
  13098. }
  13099. POP_MEM_OFFSET();
  13100. POP_I32();
  13101. POP_MEM_OFFSET();
  13102. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13103. func->has_memory_operations = true;
  13104. #endif
  13105. #if WASM_ENABLE_WAMR_COMPILER != 0
  13106. module->is_bulk_memory_used = true;
  13107. #endif
  13108. break;
  13109. }
  13110. fail_zero_byte_expected:
  13111. set_error_buf(error_buf, error_buf_size,
  13112. "zero byte expected");
  13113. goto fail;
  13114. fail_unknown_memory:
  13115. set_error_buf(error_buf, error_buf_size,
  13116. "unknown memory 0");
  13117. goto fail;
  13118. fail_data_cnt_sec_require:
  13119. set_error_buf(error_buf, error_buf_size,
  13120. "data count section required");
  13121. goto fail;
  13122. #endif /* WASM_ENABLE_BULK_MEMORY */
  13123. #if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0
  13124. case WASM_OP_TABLE_INIT:
  13125. {
  13126. uint8 seg_type = 0, tbl_type = 0;
  13127. #if WASM_ENABLE_GC != 0
  13128. WASMRefType *seg_ref_type = NULL, *tbl_ref_type = NULL;
  13129. #endif
  13130. read_leb_uint32(p, p_end, table_seg_idx);
  13131. read_leb_uint32(p, p_end, table_idx);
  13132. if (!get_table_elem_type(module, table_idx, &tbl_type,
  13133. #if WASM_ENABLE_GC != 0
  13134. (void **)&tbl_ref_type,
  13135. #else
  13136. NULL,
  13137. #endif
  13138. error_buf, error_buf_size))
  13139. goto fail;
  13140. if (!get_table_seg_elem_type(module, table_seg_idx,
  13141. &seg_type,
  13142. #if WASM_ENABLE_GC != 0
  13143. (void **)&seg_ref_type,
  13144. #else
  13145. NULL,
  13146. #endif
  13147. error_buf, error_buf_size))
  13148. goto fail;
  13149. #if WASM_ENABLE_GC == 0
  13150. if (seg_type != tbl_type) {
  13151. set_error_buf(error_buf, error_buf_size,
  13152. "type mismatch");
  13153. goto fail;
  13154. }
  13155. #else
  13156. if (!wasm_reftype_is_subtype_of(
  13157. seg_type, seg_ref_type, tbl_type, tbl_ref_type,
  13158. module->types, module->type_count)) {
  13159. set_error_buf(error_buf, error_buf_size,
  13160. "type mismatch");
  13161. goto fail;
  13162. }
  13163. #endif
  13164. #if WASM_ENABLE_FAST_INTERP != 0
  13165. emit_uint32(loader_ctx, table_seg_idx);
  13166. emit_uint32(loader_ctx, table_idx);
  13167. #endif
  13168. POP_I32();
  13169. POP_I32();
  13170. POP_I32();
  13171. #if WASM_ENABLE_WAMR_COMPILER != 0
  13172. module->is_ref_types_used = true;
  13173. #endif
  13174. break;
  13175. }
  13176. case WASM_OP_ELEM_DROP:
  13177. {
  13178. read_leb_uint32(p, p_end, table_seg_idx);
  13179. if (!get_table_seg_elem_type(module, table_seg_idx,
  13180. NULL, NULL, error_buf,
  13181. error_buf_size))
  13182. goto fail;
  13183. #if WASM_ENABLE_FAST_INTERP != 0
  13184. emit_uint32(loader_ctx, table_seg_idx);
  13185. #endif
  13186. #if WASM_ENABLE_WAMR_COMPILER != 0
  13187. module->is_ref_types_used = true;
  13188. #endif
  13189. break;
  13190. }
  13191. case WASM_OP_TABLE_COPY:
  13192. {
  13193. uint8 src_type, dst_type;
  13194. #if WASM_ENABLE_GC != 0
  13195. WASMRefType *src_ref_type = NULL, *dst_ref_type = NULL;
  13196. #endif
  13197. uint32 src_tbl_idx, dst_tbl_idx;
  13198. read_leb_uint32(p, p_end, dst_tbl_idx);
  13199. if (!get_table_elem_type(module, dst_tbl_idx, &dst_type,
  13200. #if WASM_ENABLE_GC != 0
  13201. (void **)&dst_ref_type,
  13202. #else
  13203. NULL,
  13204. #endif
  13205. error_buf, error_buf_size))
  13206. goto fail;
  13207. read_leb_uint32(p, p_end, src_tbl_idx);
  13208. if (!get_table_elem_type(module, src_tbl_idx, &src_type,
  13209. #if WASM_ENABLE_GC != 0
  13210. (void **)&src_ref_type,
  13211. #else
  13212. NULL,
  13213. #endif
  13214. error_buf, error_buf_size))
  13215. goto fail;
  13216. #if WASM_ENABLE_GC == 0
  13217. if (src_type != dst_type) {
  13218. set_error_buf(error_buf, error_buf_size,
  13219. "type mismatch");
  13220. goto fail;
  13221. }
  13222. #else
  13223. if (!wasm_reftype_is_subtype_of(
  13224. src_type, src_ref_type, dst_type, dst_ref_type,
  13225. module->types, module->type_count)) {
  13226. set_error_buf(error_buf, error_buf_size,
  13227. "type mismatch");
  13228. goto fail;
  13229. }
  13230. #endif
  13231. #if WASM_ENABLE_FAST_INTERP != 0
  13232. emit_uint32(loader_ctx, dst_tbl_idx);
  13233. emit_uint32(loader_ctx, src_tbl_idx);
  13234. #endif
  13235. POP_I32();
  13236. POP_I32();
  13237. POP_I32();
  13238. #if WASM_ENABLE_WAMR_COMPILER != 0
  13239. module->is_ref_types_used = true;
  13240. #endif
  13241. break;
  13242. }
  13243. case WASM_OP_TABLE_SIZE:
  13244. {
  13245. read_leb_uint32(p, p_end, table_idx);
  13246. /* TODO: shall we create a new function to check
  13247. table idx instead of using below function? */
  13248. if (!get_table_elem_type(module, table_idx, NULL, NULL,
  13249. error_buf, error_buf_size))
  13250. goto fail;
  13251. #if WASM_ENABLE_FAST_INTERP != 0
  13252. emit_uint32(loader_ctx, table_idx);
  13253. #endif
  13254. PUSH_I32();
  13255. #if WASM_ENABLE_WAMR_COMPILER != 0
  13256. module->is_ref_types_used = true;
  13257. #endif
  13258. break;
  13259. }
  13260. case WASM_OP_TABLE_GROW:
  13261. case WASM_OP_TABLE_FILL:
  13262. {
  13263. uint8 decl_type;
  13264. #if WASM_ENABLE_GC != 0
  13265. WASMRefType *ref_type = NULL;
  13266. #endif
  13267. read_leb_uint32(p, p_end, table_idx);
  13268. if (!get_table_elem_type(module, table_idx, &decl_type,
  13269. #if WASM_ENABLE_GC != 0
  13270. (void **)&ref_type,
  13271. #else
  13272. NULL,
  13273. #endif
  13274. error_buf, error_buf_size))
  13275. goto fail;
  13276. #if WASM_ENABLE_GC != 0
  13277. if (wasm_is_type_multi_byte_type(decl_type)) {
  13278. bh_memcpy_s(&wasm_ref_type, sizeof(WASMRefType),
  13279. ref_type,
  13280. wasm_reftype_struct_size(ref_type));
  13281. }
  13282. #endif
  13283. if (opcode1 == WASM_OP_TABLE_GROW) {
  13284. if (table_idx < module->import_table_count) {
  13285. module->import_tables[table_idx]
  13286. .u.table.possible_grow = true;
  13287. }
  13288. else {
  13289. module
  13290. ->tables[table_idx
  13291. - module->import_table_count]
  13292. .possible_grow = true;
  13293. }
  13294. }
  13295. #if WASM_ENABLE_FAST_INTERP != 0
  13296. emit_uint32(loader_ctx, table_idx);
  13297. #endif
  13298. POP_I32();
  13299. #if WASM_ENABLE_FAST_INTERP != 0
  13300. POP_OFFSET_TYPE(decl_type);
  13301. #endif
  13302. POP_TYPE(decl_type);
  13303. if (opcode1 == WASM_OP_TABLE_GROW)
  13304. PUSH_I32();
  13305. else
  13306. POP_I32();
  13307. #if WASM_ENABLE_WAMR_COMPILER != 0
  13308. module->is_ref_types_used = true;
  13309. #endif
  13310. break;
  13311. }
  13312. #endif /* end of WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0 */
  13313. default:
  13314. set_error_buf_v(error_buf, error_buf_size,
  13315. "%s %02x %02x", "unsupported opcode",
  13316. 0xfc, opcode1);
  13317. goto fail;
  13318. }
  13319. break;
  13320. }
  13321. #if WASM_ENABLE_SIMD != 0
  13322. #if (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0)
  13323. case WASM_OP_SIMD_PREFIX:
  13324. {
  13325. /* TODO: memory64 offset type changes */
  13326. uint32 opcode1;
  13327. #if WASM_ENABLE_WAMR_COMPILER != 0
  13328. /* Mark the SIMD instruction is used in this module */
  13329. module->is_simd_used = true;
  13330. #endif
  13331. read_leb_uint32(p, p_end, opcode1);
  13332. /* follow the order of enum WASMSimdEXTOpcode in wasm_opcode.h
  13333. */
  13334. switch (opcode1) {
  13335. /* memory instruction */
  13336. case SIMD_v128_load:
  13337. case SIMD_v128_load8x8_s:
  13338. case SIMD_v128_load8x8_u:
  13339. case SIMD_v128_load16x4_s:
  13340. case SIMD_v128_load16x4_u:
  13341. case SIMD_v128_load32x2_s:
  13342. case SIMD_v128_load32x2_u:
  13343. case SIMD_v128_load8_splat:
  13344. case SIMD_v128_load16_splat:
  13345. case SIMD_v128_load32_splat:
  13346. case SIMD_v128_load64_splat:
  13347. {
  13348. CHECK_MEMORY();
  13349. read_leb_uint32(p, p_end, align); /* align */
  13350. if (!check_simd_memory_access_align(
  13351. opcode1, align, error_buf, error_buf_size)) {
  13352. goto fail;
  13353. }
  13354. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13355. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13356. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13357. func->has_memory_operations = true;
  13358. #endif
  13359. break;
  13360. }
  13361. case SIMD_v128_store:
  13362. {
  13363. CHECK_MEMORY();
  13364. read_leb_uint32(p, p_end, align); /* align */
  13365. if (!check_simd_memory_access_align(
  13366. opcode1, align, error_buf, error_buf_size)) {
  13367. goto fail;
  13368. }
  13369. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13370. POP_V128();
  13371. POP_I32();
  13372. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13373. func->has_memory_operations = true;
  13374. #endif
  13375. break;
  13376. }
  13377. /* basic operation */
  13378. case SIMD_v128_const:
  13379. {
  13380. CHECK_BUF1(p, p_end, 16);
  13381. p += 16;
  13382. PUSH_V128();
  13383. break;
  13384. }
  13385. case SIMD_v8x16_shuffle:
  13386. {
  13387. V128 mask;
  13388. CHECK_BUF1(p, p_end, 16);
  13389. mask = read_i8x16(p, error_buf, error_buf_size);
  13390. p += 16;
  13391. if (!check_simd_shuffle_mask(mask, error_buf,
  13392. error_buf_size)) {
  13393. goto fail;
  13394. }
  13395. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13396. break;
  13397. }
  13398. case SIMD_v8x16_swizzle:
  13399. {
  13400. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13401. break;
  13402. }
  13403. /* splat operation */
  13404. case SIMD_i8x16_splat:
  13405. case SIMD_i16x8_splat:
  13406. case SIMD_i32x4_splat:
  13407. case SIMD_i64x2_splat:
  13408. case SIMD_f32x4_splat:
  13409. case SIMD_f64x2_splat:
  13410. {
  13411. uint8 pop_type[] = { VALUE_TYPE_I32, VALUE_TYPE_I32,
  13412. VALUE_TYPE_I32, VALUE_TYPE_I64,
  13413. VALUE_TYPE_F32, VALUE_TYPE_F64 };
  13414. POP_AND_PUSH(pop_type[opcode1 - SIMD_i8x16_splat],
  13415. VALUE_TYPE_V128);
  13416. break;
  13417. }
  13418. /* lane operation */
  13419. case SIMD_i8x16_extract_lane_s:
  13420. case SIMD_i8x16_extract_lane_u:
  13421. case SIMD_i8x16_replace_lane:
  13422. case SIMD_i16x8_extract_lane_s:
  13423. case SIMD_i16x8_extract_lane_u:
  13424. case SIMD_i16x8_replace_lane:
  13425. case SIMD_i32x4_extract_lane:
  13426. case SIMD_i32x4_replace_lane:
  13427. case SIMD_i64x2_extract_lane:
  13428. case SIMD_i64x2_replace_lane:
  13429. case SIMD_f32x4_extract_lane:
  13430. case SIMD_f32x4_replace_lane:
  13431. case SIMD_f64x2_extract_lane:
  13432. case SIMD_f64x2_replace_lane:
  13433. {
  13434. uint8 lane;
  13435. /* clang-format off */
  13436. uint8 replace[] = {
  13437. /*i8x16*/ 0x0, 0x0, VALUE_TYPE_I32,
  13438. /*i16x8*/ 0x0, 0x0, VALUE_TYPE_I32,
  13439. /*i32x4*/ 0x0, VALUE_TYPE_I32,
  13440. /*i64x2*/ 0x0, VALUE_TYPE_I64,
  13441. /*f32x4*/ 0x0, VALUE_TYPE_F32,
  13442. /*f64x2*/ 0x0, VALUE_TYPE_F64,
  13443. };
  13444. uint8 push_type[] = {
  13445. /*i8x16*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13446. VALUE_TYPE_V128,
  13447. /*i16x8*/ VALUE_TYPE_I32, VALUE_TYPE_I32,
  13448. VALUE_TYPE_V128,
  13449. /*i32x4*/ VALUE_TYPE_I32, VALUE_TYPE_V128,
  13450. /*i64x2*/ VALUE_TYPE_I64, VALUE_TYPE_V128,
  13451. /*f32x4*/ VALUE_TYPE_F32, VALUE_TYPE_V128,
  13452. /*f64x2*/ VALUE_TYPE_F64, VALUE_TYPE_V128,
  13453. };
  13454. /* clang-format on */
  13455. CHECK_BUF(p, p_end, 1);
  13456. lane = read_uint8(p);
  13457. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13458. error_buf_size)) {
  13459. goto fail;
  13460. }
  13461. if (replace[opcode1 - SIMD_i8x16_extract_lane_s]) {
  13462. if (!(wasm_loader_pop_frame_ref(
  13463. loader_ctx,
  13464. replace[opcode1
  13465. - SIMD_i8x16_extract_lane_s],
  13466. error_buf, error_buf_size)))
  13467. goto fail;
  13468. }
  13469. POP_AND_PUSH(
  13470. VALUE_TYPE_V128,
  13471. push_type[opcode1 - SIMD_i8x16_extract_lane_s]);
  13472. break;
  13473. }
  13474. /* i8x16 compare operation */
  13475. case SIMD_i8x16_eq:
  13476. case SIMD_i8x16_ne:
  13477. case SIMD_i8x16_lt_s:
  13478. case SIMD_i8x16_lt_u:
  13479. case SIMD_i8x16_gt_s:
  13480. case SIMD_i8x16_gt_u:
  13481. case SIMD_i8x16_le_s:
  13482. case SIMD_i8x16_le_u:
  13483. case SIMD_i8x16_ge_s:
  13484. case SIMD_i8x16_ge_u:
  13485. /* i16x8 compare operation */
  13486. case SIMD_i16x8_eq:
  13487. case SIMD_i16x8_ne:
  13488. case SIMD_i16x8_lt_s:
  13489. case SIMD_i16x8_lt_u:
  13490. case SIMD_i16x8_gt_s:
  13491. case SIMD_i16x8_gt_u:
  13492. case SIMD_i16x8_le_s:
  13493. case SIMD_i16x8_le_u:
  13494. case SIMD_i16x8_ge_s:
  13495. case SIMD_i16x8_ge_u:
  13496. /* i32x4 compare operation */
  13497. case SIMD_i32x4_eq:
  13498. case SIMD_i32x4_ne:
  13499. case SIMD_i32x4_lt_s:
  13500. case SIMD_i32x4_lt_u:
  13501. case SIMD_i32x4_gt_s:
  13502. case SIMD_i32x4_gt_u:
  13503. case SIMD_i32x4_le_s:
  13504. case SIMD_i32x4_le_u:
  13505. case SIMD_i32x4_ge_s:
  13506. case SIMD_i32x4_ge_u:
  13507. /* f32x4 compare operation */
  13508. case SIMD_f32x4_eq:
  13509. case SIMD_f32x4_ne:
  13510. case SIMD_f32x4_lt:
  13511. case SIMD_f32x4_gt:
  13512. case SIMD_f32x4_le:
  13513. case SIMD_f32x4_ge:
  13514. /* f64x2 compare operation */
  13515. case SIMD_f64x2_eq:
  13516. case SIMD_f64x2_ne:
  13517. case SIMD_f64x2_lt:
  13518. case SIMD_f64x2_gt:
  13519. case SIMD_f64x2_le:
  13520. case SIMD_f64x2_ge:
  13521. {
  13522. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13523. break;
  13524. }
  13525. /* v128 operation */
  13526. case SIMD_v128_not:
  13527. {
  13528. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13529. break;
  13530. }
  13531. case SIMD_v128_and:
  13532. case SIMD_v128_andnot:
  13533. case SIMD_v128_or:
  13534. case SIMD_v128_xor:
  13535. {
  13536. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13537. break;
  13538. }
  13539. case SIMD_v128_bitselect:
  13540. {
  13541. POP_V128();
  13542. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13543. break;
  13544. }
  13545. case SIMD_v128_any_true:
  13546. {
  13547. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13548. break;
  13549. }
  13550. /* Load Lane Operation */
  13551. case SIMD_v128_load8_lane:
  13552. case SIMD_v128_load16_lane:
  13553. case SIMD_v128_load32_lane:
  13554. case SIMD_v128_load64_lane:
  13555. case SIMD_v128_store8_lane:
  13556. case SIMD_v128_store16_lane:
  13557. case SIMD_v128_store32_lane:
  13558. case SIMD_v128_store64_lane:
  13559. {
  13560. uint8 lane;
  13561. CHECK_MEMORY();
  13562. read_leb_uint32(p, p_end, align); /* align */
  13563. if (!check_simd_memory_access_align(
  13564. opcode1, align, error_buf, error_buf_size)) {
  13565. goto fail;
  13566. }
  13567. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13568. CHECK_BUF(p, p_end, 1);
  13569. lane = read_uint8(p);
  13570. if (!check_simd_access_lane(opcode1, lane, error_buf,
  13571. error_buf_size)) {
  13572. goto fail;
  13573. }
  13574. POP_V128();
  13575. POP_I32();
  13576. if (opcode1 < SIMD_v128_store8_lane) {
  13577. PUSH_V128();
  13578. }
  13579. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13580. func->has_memory_operations = true;
  13581. #endif
  13582. break;
  13583. }
  13584. case SIMD_v128_load32_zero:
  13585. case SIMD_v128_load64_zero:
  13586. {
  13587. CHECK_MEMORY();
  13588. read_leb_uint32(p, p_end, align); /* align */
  13589. if (!check_simd_memory_access_align(
  13590. opcode1, align, error_buf, error_buf_size)) {
  13591. goto fail;
  13592. }
  13593. read_leb_uint32(p, p_end, mem_offset); /* offset */
  13594. POP_AND_PUSH(VALUE_TYPE_I32, VALUE_TYPE_V128);
  13595. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13596. func->has_memory_operations = true;
  13597. #endif
  13598. break;
  13599. }
  13600. /* Float conversion */
  13601. case SIMD_f32x4_demote_f64x2_zero:
  13602. case SIMD_f64x2_promote_low_f32x4_zero:
  13603. {
  13604. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13605. break;
  13606. }
  13607. /* i8x16 Operation */
  13608. case SIMD_i8x16_abs:
  13609. case SIMD_i8x16_neg:
  13610. case SIMD_i8x16_popcnt:
  13611. {
  13612. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13613. break;
  13614. }
  13615. case SIMD_i8x16_all_true:
  13616. case SIMD_i8x16_bitmask:
  13617. {
  13618. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13619. break;
  13620. }
  13621. case SIMD_i8x16_narrow_i16x8_s:
  13622. case SIMD_i8x16_narrow_i16x8_u:
  13623. {
  13624. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13625. break;
  13626. }
  13627. case SIMD_f32x4_ceil:
  13628. case SIMD_f32x4_floor:
  13629. case SIMD_f32x4_trunc:
  13630. case SIMD_f32x4_nearest:
  13631. {
  13632. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13633. break;
  13634. }
  13635. case SIMD_i8x16_shl:
  13636. case SIMD_i8x16_shr_s:
  13637. case SIMD_i8x16_shr_u:
  13638. {
  13639. POP_I32();
  13640. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13641. break;
  13642. }
  13643. case SIMD_i8x16_add:
  13644. case SIMD_i8x16_add_sat_s:
  13645. case SIMD_i8x16_add_sat_u:
  13646. case SIMD_i8x16_sub:
  13647. case SIMD_i8x16_sub_sat_s:
  13648. case SIMD_i8x16_sub_sat_u:
  13649. {
  13650. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13651. break;
  13652. }
  13653. case SIMD_f64x2_ceil:
  13654. case SIMD_f64x2_floor:
  13655. {
  13656. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13657. break;
  13658. }
  13659. case SIMD_i8x16_min_s:
  13660. case SIMD_i8x16_min_u:
  13661. case SIMD_i8x16_max_s:
  13662. case SIMD_i8x16_max_u:
  13663. {
  13664. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13665. break;
  13666. }
  13667. case SIMD_f64x2_trunc:
  13668. {
  13669. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13670. break;
  13671. }
  13672. case SIMD_i8x16_avgr_u:
  13673. {
  13674. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13675. break;
  13676. }
  13677. case SIMD_i16x8_extadd_pairwise_i8x16_s:
  13678. case SIMD_i16x8_extadd_pairwise_i8x16_u:
  13679. case SIMD_i32x4_extadd_pairwise_i16x8_s:
  13680. case SIMD_i32x4_extadd_pairwise_i16x8_u:
  13681. /* i16x8 operation */
  13682. case SIMD_i16x8_abs:
  13683. case SIMD_i16x8_neg:
  13684. {
  13685. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13686. break;
  13687. }
  13688. case SIMD_i16x8_q15mulr_sat_s:
  13689. {
  13690. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13691. break;
  13692. }
  13693. case SIMD_i16x8_all_true:
  13694. case SIMD_i16x8_bitmask:
  13695. {
  13696. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13697. break;
  13698. }
  13699. case SIMD_i16x8_narrow_i32x4_s:
  13700. case SIMD_i16x8_narrow_i32x4_u:
  13701. {
  13702. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13703. break;
  13704. }
  13705. case SIMD_i16x8_extend_low_i8x16_s:
  13706. case SIMD_i16x8_extend_high_i8x16_s:
  13707. case SIMD_i16x8_extend_low_i8x16_u:
  13708. case SIMD_i16x8_extend_high_i8x16_u:
  13709. {
  13710. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13711. break;
  13712. }
  13713. case SIMD_i16x8_shl:
  13714. case SIMD_i16x8_shr_s:
  13715. case SIMD_i16x8_shr_u:
  13716. {
  13717. POP_I32();
  13718. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13719. break;
  13720. }
  13721. case SIMD_i16x8_add:
  13722. case SIMD_i16x8_add_sat_s:
  13723. case SIMD_i16x8_add_sat_u:
  13724. case SIMD_i16x8_sub:
  13725. case SIMD_i16x8_sub_sat_s:
  13726. case SIMD_i16x8_sub_sat_u:
  13727. {
  13728. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13729. break;
  13730. }
  13731. case SIMD_f64x2_nearest:
  13732. {
  13733. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13734. break;
  13735. }
  13736. case SIMD_i16x8_mul:
  13737. case SIMD_i16x8_min_s:
  13738. case SIMD_i16x8_min_u:
  13739. case SIMD_i16x8_max_s:
  13740. case SIMD_i16x8_max_u:
  13741. case SIMD_i16x8_avgr_u:
  13742. case SIMD_i16x8_extmul_low_i8x16_s:
  13743. case SIMD_i16x8_extmul_high_i8x16_s:
  13744. case SIMD_i16x8_extmul_low_i8x16_u:
  13745. case SIMD_i16x8_extmul_high_i8x16_u:
  13746. {
  13747. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13748. break;
  13749. }
  13750. /* i32x4 operation */
  13751. case SIMD_i32x4_abs:
  13752. case SIMD_i32x4_neg:
  13753. {
  13754. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13755. break;
  13756. }
  13757. case SIMD_i32x4_all_true:
  13758. case SIMD_i32x4_bitmask:
  13759. {
  13760. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13761. break;
  13762. }
  13763. case SIMD_i32x4_extend_low_i16x8_s:
  13764. case SIMD_i32x4_extend_high_i16x8_s:
  13765. case SIMD_i32x4_extend_low_i16x8_u:
  13766. case SIMD_i32x4_extend_high_i16x8_u:
  13767. {
  13768. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13769. break;
  13770. }
  13771. case SIMD_i32x4_shl:
  13772. case SIMD_i32x4_shr_s:
  13773. case SIMD_i32x4_shr_u:
  13774. {
  13775. POP_I32();
  13776. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13777. break;
  13778. }
  13779. case SIMD_i32x4_add:
  13780. case SIMD_i32x4_sub:
  13781. case SIMD_i32x4_mul:
  13782. case SIMD_i32x4_min_s:
  13783. case SIMD_i32x4_min_u:
  13784. case SIMD_i32x4_max_s:
  13785. case SIMD_i32x4_max_u:
  13786. case SIMD_i32x4_dot_i16x8_s:
  13787. case SIMD_i32x4_extmul_low_i16x8_s:
  13788. case SIMD_i32x4_extmul_high_i16x8_s:
  13789. case SIMD_i32x4_extmul_low_i16x8_u:
  13790. case SIMD_i32x4_extmul_high_i16x8_u:
  13791. {
  13792. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13793. break;
  13794. }
  13795. /* i64x2 operation */
  13796. case SIMD_i64x2_abs:
  13797. case SIMD_i64x2_neg:
  13798. {
  13799. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13800. break;
  13801. }
  13802. case SIMD_i64x2_all_true:
  13803. case SIMD_i64x2_bitmask:
  13804. {
  13805. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_I32);
  13806. break;
  13807. }
  13808. case SIMD_i64x2_extend_low_i32x4_s:
  13809. case SIMD_i64x2_extend_high_i32x4_s:
  13810. case SIMD_i64x2_extend_low_i32x4_u:
  13811. case SIMD_i64x2_extend_high_i32x4_u:
  13812. {
  13813. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13814. break;
  13815. }
  13816. case SIMD_i64x2_shl:
  13817. case SIMD_i64x2_shr_s:
  13818. case SIMD_i64x2_shr_u:
  13819. {
  13820. POP_I32();
  13821. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13822. break;
  13823. }
  13824. case SIMD_i64x2_add:
  13825. case SIMD_i64x2_sub:
  13826. case SIMD_i64x2_mul:
  13827. case SIMD_i64x2_eq:
  13828. case SIMD_i64x2_ne:
  13829. case SIMD_i64x2_lt_s:
  13830. case SIMD_i64x2_gt_s:
  13831. case SIMD_i64x2_le_s:
  13832. case SIMD_i64x2_ge_s:
  13833. case SIMD_i64x2_extmul_low_i32x4_s:
  13834. case SIMD_i64x2_extmul_high_i32x4_s:
  13835. case SIMD_i64x2_extmul_low_i32x4_u:
  13836. case SIMD_i64x2_extmul_high_i32x4_u:
  13837. {
  13838. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13839. break;
  13840. }
  13841. /* f32x4 operation */
  13842. case SIMD_f32x4_abs:
  13843. case SIMD_f32x4_neg:
  13844. case SIMD_f32x4_sqrt:
  13845. {
  13846. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13847. break;
  13848. }
  13849. case SIMD_f32x4_add:
  13850. case SIMD_f32x4_sub:
  13851. case SIMD_f32x4_mul:
  13852. case SIMD_f32x4_div:
  13853. case SIMD_f32x4_min:
  13854. case SIMD_f32x4_max:
  13855. case SIMD_f32x4_pmin:
  13856. case SIMD_f32x4_pmax:
  13857. {
  13858. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13859. break;
  13860. }
  13861. /* f64x2 operation */
  13862. case SIMD_f64x2_abs:
  13863. case SIMD_f64x2_neg:
  13864. case SIMD_f64x2_sqrt:
  13865. {
  13866. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13867. break;
  13868. }
  13869. case SIMD_f64x2_add:
  13870. case SIMD_f64x2_sub:
  13871. case SIMD_f64x2_mul:
  13872. case SIMD_f64x2_div:
  13873. case SIMD_f64x2_min:
  13874. case SIMD_f64x2_max:
  13875. case SIMD_f64x2_pmin:
  13876. case SIMD_f64x2_pmax:
  13877. {
  13878. POP2_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13879. break;
  13880. }
  13881. case SIMD_i32x4_trunc_sat_f32x4_s:
  13882. case SIMD_i32x4_trunc_sat_f32x4_u:
  13883. case SIMD_f32x4_convert_i32x4_s:
  13884. case SIMD_f32x4_convert_i32x4_u:
  13885. case SIMD_i32x4_trunc_sat_f64x2_s_zero:
  13886. case SIMD_i32x4_trunc_sat_f64x2_u_zero:
  13887. case SIMD_f64x2_convert_low_i32x4_s:
  13888. case SIMD_f64x2_convert_low_i32x4_u:
  13889. {
  13890. POP_AND_PUSH(VALUE_TYPE_V128, VALUE_TYPE_V128);
  13891. break;
  13892. }
  13893. default:
  13894. {
  13895. if (error_buf != NULL) {
  13896. snprintf(error_buf, error_buf_size,
  13897. "WASM module load failed: "
  13898. "invalid opcode 0xfd %02x.",
  13899. opcode1);
  13900. }
  13901. goto fail;
  13902. }
  13903. }
  13904. break;
  13905. }
  13906. #endif /* end of (WASM_ENABLE_WAMR_COMPILER != 0) || (WASM_ENABLE_JIT != 0) */
  13907. #endif /* end of WASM_ENABLE_SIMD */
  13908. #if WASM_ENABLE_SHARED_MEMORY != 0
  13909. case WASM_OP_ATOMIC_PREFIX:
  13910. {
  13911. uint32 opcode1;
  13912. read_leb_uint32(p, p_end, opcode1);
  13913. #if WASM_ENABLE_FAST_INTERP != 0
  13914. emit_byte(loader_ctx, opcode1);
  13915. #endif
  13916. if (opcode1 != WASM_OP_ATOMIC_FENCE) {
  13917. CHECK_MEMORY();
  13918. read_leb_uint32(p, p_end, align); /* align */
  13919. read_leb_mem_offset(p, p_end, mem_offset); /* offset */
  13920. if (!check_memory_align_equal(opcode1, align, error_buf,
  13921. error_buf_size)) {
  13922. goto fail;
  13923. }
  13924. #if WASM_ENABLE_FAST_INTERP != 0
  13925. emit_uint32(loader_ctx, mem_offset);
  13926. #endif
  13927. }
  13928. #if WASM_ENABLE_JIT != 0 || WASM_ENABLE_WAMR_COMPILER != 0
  13929. func->has_memory_operations = true;
  13930. #endif
  13931. switch (opcode1) {
  13932. case WASM_OP_ATOMIC_NOTIFY:
  13933. POP_I32();
  13934. POP_MEM_OFFSET();
  13935. PUSH_I32();
  13936. break;
  13937. case WASM_OP_ATOMIC_WAIT32:
  13938. POP_I64();
  13939. POP_I32();
  13940. POP_MEM_OFFSET();
  13941. PUSH_I32();
  13942. break;
  13943. case WASM_OP_ATOMIC_WAIT64:
  13944. POP_I64();
  13945. POP_I64();
  13946. POP_MEM_OFFSET();
  13947. PUSH_I32();
  13948. break;
  13949. case WASM_OP_ATOMIC_FENCE:
  13950. /* reserved byte 0x00 */
  13951. if (*p++ != 0x00) {
  13952. set_error_buf(error_buf, error_buf_size,
  13953. "zero byte expected");
  13954. goto fail;
  13955. }
  13956. break;
  13957. case WASM_OP_ATOMIC_I32_LOAD:
  13958. case WASM_OP_ATOMIC_I32_LOAD8_U:
  13959. case WASM_OP_ATOMIC_I32_LOAD16_U:
  13960. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I32);
  13961. break;
  13962. case WASM_OP_ATOMIC_I32_STORE:
  13963. case WASM_OP_ATOMIC_I32_STORE8:
  13964. case WASM_OP_ATOMIC_I32_STORE16:
  13965. POP_I32();
  13966. POP_MEM_OFFSET();
  13967. break;
  13968. case WASM_OP_ATOMIC_I64_LOAD:
  13969. case WASM_OP_ATOMIC_I64_LOAD8_U:
  13970. case WASM_OP_ATOMIC_I64_LOAD16_U:
  13971. case WASM_OP_ATOMIC_I64_LOAD32_U:
  13972. POP_AND_PUSH(mem_offset_type, VALUE_TYPE_I64);
  13973. break;
  13974. case WASM_OP_ATOMIC_I64_STORE:
  13975. case WASM_OP_ATOMIC_I64_STORE8:
  13976. case WASM_OP_ATOMIC_I64_STORE16:
  13977. case WASM_OP_ATOMIC_I64_STORE32:
  13978. POP_I64();
  13979. POP_MEM_OFFSET();
  13980. break;
  13981. case WASM_OP_ATOMIC_RMW_I32_ADD:
  13982. case WASM_OP_ATOMIC_RMW_I32_ADD8_U:
  13983. case WASM_OP_ATOMIC_RMW_I32_ADD16_U:
  13984. case WASM_OP_ATOMIC_RMW_I32_SUB:
  13985. case WASM_OP_ATOMIC_RMW_I32_SUB8_U:
  13986. case WASM_OP_ATOMIC_RMW_I32_SUB16_U:
  13987. case WASM_OP_ATOMIC_RMW_I32_AND:
  13988. case WASM_OP_ATOMIC_RMW_I32_AND8_U:
  13989. case WASM_OP_ATOMIC_RMW_I32_AND16_U:
  13990. case WASM_OP_ATOMIC_RMW_I32_OR:
  13991. case WASM_OP_ATOMIC_RMW_I32_OR8_U:
  13992. case WASM_OP_ATOMIC_RMW_I32_OR16_U:
  13993. case WASM_OP_ATOMIC_RMW_I32_XOR:
  13994. case WASM_OP_ATOMIC_RMW_I32_XOR8_U:
  13995. case WASM_OP_ATOMIC_RMW_I32_XOR16_U:
  13996. case WASM_OP_ATOMIC_RMW_I32_XCHG:
  13997. case WASM_OP_ATOMIC_RMW_I32_XCHG8_U:
  13998. case WASM_OP_ATOMIC_RMW_I32_XCHG16_U:
  13999. POP_I32();
  14000. POP_MEM_OFFSET();
  14001. PUSH_I32();
  14002. break;
  14003. case WASM_OP_ATOMIC_RMW_I64_ADD:
  14004. case WASM_OP_ATOMIC_RMW_I64_ADD8_U:
  14005. case WASM_OP_ATOMIC_RMW_I64_ADD16_U:
  14006. case WASM_OP_ATOMIC_RMW_I64_ADD32_U:
  14007. case WASM_OP_ATOMIC_RMW_I64_SUB:
  14008. case WASM_OP_ATOMIC_RMW_I64_SUB8_U:
  14009. case WASM_OP_ATOMIC_RMW_I64_SUB16_U:
  14010. case WASM_OP_ATOMIC_RMW_I64_SUB32_U:
  14011. case WASM_OP_ATOMIC_RMW_I64_AND:
  14012. case WASM_OP_ATOMIC_RMW_I64_AND8_U:
  14013. case WASM_OP_ATOMIC_RMW_I64_AND16_U:
  14014. case WASM_OP_ATOMIC_RMW_I64_AND32_U:
  14015. case WASM_OP_ATOMIC_RMW_I64_OR:
  14016. case WASM_OP_ATOMIC_RMW_I64_OR8_U:
  14017. case WASM_OP_ATOMIC_RMW_I64_OR16_U:
  14018. case WASM_OP_ATOMIC_RMW_I64_OR32_U:
  14019. case WASM_OP_ATOMIC_RMW_I64_XOR:
  14020. case WASM_OP_ATOMIC_RMW_I64_XOR8_U:
  14021. case WASM_OP_ATOMIC_RMW_I64_XOR16_U:
  14022. case WASM_OP_ATOMIC_RMW_I64_XOR32_U:
  14023. case WASM_OP_ATOMIC_RMW_I64_XCHG:
  14024. case WASM_OP_ATOMIC_RMW_I64_XCHG8_U:
  14025. case WASM_OP_ATOMIC_RMW_I64_XCHG16_U:
  14026. case WASM_OP_ATOMIC_RMW_I64_XCHG32_U:
  14027. POP_I64();
  14028. POP_MEM_OFFSET();
  14029. PUSH_I64();
  14030. break;
  14031. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  14032. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  14033. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  14034. POP_I32();
  14035. POP_I32();
  14036. POP_MEM_OFFSET();
  14037. PUSH_I32();
  14038. break;
  14039. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  14040. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  14041. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  14042. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  14043. POP_I64();
  14044. POP_I64();
  14045. POP_MEM_OFFSET();
  14046. PUSH_I64();
  14047. break;
  14048. default:
  14049. set_error_buf_v(error_buf, error_buf_size,
  14050. "%s %02x %02x", "unsupported opcode",
  14051. 0xfe, opcode1);
  14052. goto fail;
  14053. }
  14054. break;
  14055. }
  14056. #endif /* end of WASM_ENABLE_SHARED_MEMORY */
  14057. default:
  14058. set_error_buf_v(error_buf, error_buf_size, "%s %02x",
  14059. "unsupported opcode", opcode);
  14060. goto fail;
  14061. }
  14062. #if WASM_ENABLE_FAST_INTERP != 0
  14063. last_op = opcode;
  14064. #endif
  14065. }
  14066. if (loader_ctx->csp_num > 0) {
  14067. if (cur_func_idx < module->function_count - 1)
  14068. /* Function with missing end marker (between two functions) */
  14069. set_error_buf(error_buf, error_buf_size, "END opcode expected");
  14070. else
  14071. /* Function with missing end marker
  14072. (at EOF or end of code sections) */
  14073. set_error_buf(error_buf, error_buf_size,
  14074. "unexpected end of section or function, "
  14075. "or section size mismatch");
  14076. goto fail;
  14077. }
  14078. #if WASM_ENABLE_FAST_INTERP != 0
  14079. if (loader_ctx->p_code_compiled == NULL)
  14080. goto re_scan;
  14081. func->const_cell_num = loader_ctx->const_cell_num;
  14082. if (func->const_cell_num > 0) {
  14083. int32 j;
  14084. if (!(func->consts = func_const = loader_malloc(
  14085. func->const_cell_num * 4, error_buf, error_buf_size)))
  14086. goto fail;
  14087. func_const_end = func->consts + func->const_cell_num * 4;
  14088. /* reverse the const buf */
  14089. for (j = loader_ctx->num_const - 1; j >= 0; j--) {
  14090. Const *c = (Const *)(loader_ctx->const_buf + j * sizeof(Const));
  14091. if (c->value_type == VALUE_TYPE_F64
  14092. || c->value_type == VALUE_TYPE_I64) {
  14093. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14094. &(c->value.f64), (uint32)sizeof(int64));
  14095. func_const += sizeof(int64);
  14096. }
  14097. else {
  14098. bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
  14099. &(c->value.f32), (uint32)sizeof(int32));
  14100. func_const += sizeof(int32);
  14101. }
  14102. }
  14103. }
  14104. func->max_stack_cell_num = loader_ctx->preserved_local_offset
  14105. - loader_ctx->start_dynamic_offset + 1;
  14106. #else
  14107. func->max_stack_cell_num = loader_ctx->max_stack_cell_num;
  14108. #endif
  14109. func->max_block_num = loader_ctx->max_csp_num;
  14110. return_value = true;
  14111. fail:
  14112. wasm_loader_ctx_destroy(loader_ctx);
  14113. (void)table_idx;
  14114. (void)table_seg_idx;
  14115. (void)data_seg_idx;
  14116. (void)i64_const;
  14117. (void)local_offset;
  14118. (void)p_org;
  14119. (void)mem_offset;
  14120. (void)align;
  14121. return return_value;
  14122. }